AN3985
Application note
Using the CR95HF library with STM8L microcontrollers
1
Introduction
This document describes the CR95HF library allowing an STM8L microcontroller to drive
the CR95HF 13.56 MHz multiprotocol contactless transceiver using an SPI or UART
interface, in order to perform wireless communications with ISO/IEC15693 contactless tag.
The library was developed to speed up the development of applications using the CR95HF.
The CR95HF library is composed of three layers:
■
■
■
Low level CR95HF layer
Standard ISO/IEC 15693 protocol layer
Product layer (LRIxK and Dual Interface EEPROM contactless tag)
The library code has been developed in ANSI C language, and validated on an STM8L
evaluation board.
The firmware runs either on the STM8L1528-EVAL evaluation board or the STM8L-
DISCOVERY board. The MCU can be either the STM8L152M8 (on the STM8L1528-Eval
board) or the STM8L152C6 (STM8L-DISCOVERY).
1.1
Reference documents
■
■
ISO/IEC 15693 specification
LRIxK ISO/IEC 15693 contactless memory with 64-bit UID, AFI, DSFID, anti-collision and
kill function datasheet
CR95HF 13.56-MHz multi-protocol contactless transceiver IC with SPI and UART serial
access datasheet
M24LRxx dual interface EEPROM with password protection datasheet
UM1037: STM8L1528-EVAL evaluation board user manual
UM0970: STM8L-DISCOVERY evaluation board user manual
■
■
■
■
February 2012
Doc ID 022255 Rev 2
1/105
www.st.com
Contents
AN3985
Contents
1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1
Reference documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2
Acronyms and notational conventions . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1
2.2
List of terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Notational conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.2.1
2.2.2
2.2.3
Binary number representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Hexadecimal number representation . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Decimal number representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.1
3.2
CR95HF overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Library overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2.1
3.2.2
Example of an application architecture . . . . . . . . . . . . . . . . . . . . . . . . . 11
Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4
CR95HF low level layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.1
4.2
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2.1
4.2.2
4.2.3
4.2.4
Command format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Response format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Protocol selection structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Idle structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.3
CR95HF layer functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.3.1
4.3.2
4.3.3
4.3.4
4.3.5
4.3.6
Command functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Additional functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Low power mode functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Is functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Advanced functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Application example: protocol selection and communication . . . . . . . . . 28
5
ISO/IEC 15693 layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.1
5.2
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Doc ID 022255 Rev 2
2/105
AN3985
5.2.1
Contents
Structures of command and response of an ISO/IEC 15693 tag . . . . . . 29
5.3
ISO/IEC 15693 command format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.3.1
5.3.2
5.3.3
5.3.4
SOF and EOF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Request flag management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Command code and Data management . . . . . . . . . . . . . . . . . . . . . . . . 32
CRC16 management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.4
ISO/IEC 15693 layer functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.4.1
5.4.2
5.4.3
5.4.4
5.4.5
5.4.6
5.4.7
Compute parameter byte functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
ISO/IEC 15693 command functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Build up functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Is Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Get functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
CRC16 functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Fill In functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
6
LRIxK layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
6.1
6.2
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Command format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
6.2.1
6.2.2
6.2.3
CRC16 management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Request flag management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Request flags and CR95HF_ProtocolSelect functions . . . . . . . . . . . . . 61
6.3
LRIxK layer commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
6.3.1
LRIxK command functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
7
M24LRxx layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
7.1
7.2
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Command format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
7.2.1
7.2.2
7.2.3
CRC16 management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Request flag management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Request flags and CR95HF_ProtocolSelect functions . . . . . . . . . . . . . 68
7.3
M24LRxx commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.3.1
7.3.2
M24LRxx command functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
M24LRxx Energy Harvesting functions . . . . . . . . . . . . . . . . . . . . . . . . . 81
8
Application example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
8.1
Main functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Doc ID 022255 Rev 2
3/105
Contents
8.1.1
8.1.2
8.1.3
8.1.4
8.1.5
8.1.6
8.1.7
8.1.8
8.1.9
AN3985
Board initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Display initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Test configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
ISO/IEC 15693 Protocol selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Tag hunting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
User application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Low power modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Communication test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
8.2
Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
8.2.1
8.2.2
8.2.3
STM8L discovery board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
STM8L evaluation board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
CR95HF plug board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
8.3
Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
8.3.1
8.3.2
ST Visual Develop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Cosmic compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
8.4
Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
8.4.1
8.4.2
Opening the Project: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Compilation / Debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
8.5
Compilation management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
8.5.1
8.5.2
Conditional compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Polling method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
8.6
8.7
8.8
Hardware layout and configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Pinout description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
8.7.1
PLUG-CR95HF-B Board pin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Switching between STM8L 1528-Eval and STM8L Discovery boards . . 103
9
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
4/105
Doc ID 022255 Rev 2
AN3985
List of tables
List of tables
Table 1.
Table 2.
Table 3.
Table 4.
Table 5.
Table 6.
Table 7.
Table 8.
Table 9.
Table 10.
Table 11.
Table 12.
Table 13.
Table 14.
Table 15.
Table 16.
Table 17.
Table 18.
Table 19.
Table 20.
Table 21.
Table 22.
Table 23.
Table 24.
Table 25.
Table 26.
Table 27.
Table 28.
Table 29.
Table 30.
Table 31.
Table 32.
Table 33.
Table 34.
Table 35.
Table 36.
Table 37.
Table 38.
Table 39.
Table 40.
Table 41.
Table 42.
Table 43.
Table 44.
Table 45.
Table 46.
Table 47.
Table 48.
Table 49.
Table 50.
Table 51.
List of terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Command fields formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Response field formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Protocol parameter formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Protocol values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Idle structure parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
CR95HF layer functions based on CR95HF commands . . . . . . . . . . . . . . . . . . . . . . . . . . 16
CR95HF layer additional functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Low Power mode functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
CR95HF layer IS functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
CR95HF layer advanced functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
CR95HF_IDN function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
CR95HF_Echo function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
CR95HF_ProtocolSelect function description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Input parameters settings for ISO/IEC 15693 protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
CR95HF_SendRecv function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
SendRecv correct response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
SendRecv error response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
CR95HF_Idle function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
CR95HF_Rd_Wake_up_Reg function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
CR95HF_Rd_Analog_Register_Config_B function description . . . . . . . . . . . . . . . . . . . . . 21
CR95HF_Baud_Rate function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
CR95HF_SendEOF function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
CR95HF_FieldOff function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
CR95HF_Hibernate function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
CR95HF_Sleep function description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
CR95HF_TagDetecting function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
CR95HF_IdlebyTimer function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
CR95HF_IsReaderResultOK function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Is_IRQ_in_Interupt_Wake_up_Condition function description . . . . . . . . . . . . . . . . . . . . . . 24
Is_NSS_Interrupt_Wake_up_Condition function description . . . . . . . . . . . . . . . . . . . . . . . 25
Is_Tag_Detected_Wake_up_Condition function description . . . . . . . . . . . . . . . . . . . . . . . 25
Is_Tag_Detected_Wake_up_Condition function description . . . . . . . . . . . . . . . . . . . . . . . 25
CR95HF_Modify_Baud_Rate function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
CR95HF_Idle_Detector_Calibration function description . . . . . . . . . . . . . . . . . . . . . . . . . . 26
CR95HF_Wait_Wake_up_From_Idle function description . . . . . . . . . . . . . . . . . . . . . . . . . 27
CR95HF_Wake_up_CR95HF function description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
ISO15693_tag structure description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Request flag bits description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Request flag bits values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Bit for request flag with inventory flag reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Bit for request flag with inventory flag reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Functions to compute parameter byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
ISO15693 library command based on ISO/IEC 15693 specification . . . . . . . . . . . . . . . . . 33
Functions to assemble ISO/IEC 15693 command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Get functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
CRC16 functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Structure Fill In functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
ISO15693_ComputeParameterByte_ProtocolSelect function description . . . . . . . . . . . . . 36
ComputeByte function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
ISO15693_Inventory function description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Doc ID 022255 Rev 2
5/105