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.
AN3985
Application note
The firmware runs either on the STM8L1528-EVAL evaluation board or the STM8LDISCOVERY 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
M24LR64-RDual interface EEPROM (I2C & RF) with 64-kbit memory size
M24LR16-E
MCUMicrocontroller Unit
MSBMost Significant Bit
NFCNear Field Communication
PORPower On Reset
RTCReal Time Clock
RFRadio Frequency
RFUReserved for Future Use
RFIDRadio Frequency Identification
STVDST Visual Develop
UIDUnique Identifier
Write_DO_CfgWrite Digital Output Configuration (M24LRxx-E command)
Write_EH_CfgWrite Energy Harvesting Configuration (M24LRxx-E command)
Dual interface EEPROM (I2C & RF) with 16-kbit memory size and energy
harvesting functionality
XOReXclusive OR
Doc ID 022255 Rev 29/105
Acronyms and notational conventionsAN3985
2.2 Notational conventions
The following conventions and notations apply in this document unless otherwise stated.
2.2.1 Binary number representation
Binary numbers are represented by strings of digits 0 and 1 shown with the Most Significant
Bit (MSB) on the left, the Least Significant Bit (LSB) on the right, and “0b” added at the
beginning.
For example: 0b11110101
2.2.2 Hexadecimal number representation
Hexadecimal numbers are represented by using the numbers 0 to 9, the characters A - F,
and a “0x” added at the beginning. The Most Significant Byte (MSB) is shown on the left and
the Least Significant Byte (LSB) on the right.
For example: 0xF5
2.2.3 Decimal number representation
Decimal numbers are represented as is, without any trailing character.
For example: 245
10/105Doc ID 022255 Rev 2
AN3985Overview
MS19958V1
ISO15693 RF
MCU
CR95HF
Contactless tag
CR95HF
library
ISO15693 RF
Memory products
Dual IC M24LR*
products
RF communication
3 Overview
3.1 CR95HF overview
The CR95HF is a Radio Frequency (RF) transceiver Integrated Circuit (IC) for 13.56 MHz
contactless tags, which includes ISO/IEC 14443, ISO/IEC 15693 and ISO/IEC 18092
protocols. It manages the RF communication with Radio Frequency Identification (RFID) or
Near Field Communication (NFC) tags. It includes frame coding, RF modulation and
contactless tag response decoding.
The CR95HF is a slave device and must be controlled by a host (Microcontroller Unit). This
library is an interface between user application function and standard peripheral driver.
The library was written in compliance with ANSI C standards.
Figure 1 describes a typical application block diagram.
Figure 1.Typical application block diagram
For more details concerning the CR95HF, please refer to CR95HF datasheet.
3.2 Library overview
The library described in this application note is composed of three layers:
●A low level layer supporting the commands described in the CR95HF datasheet. This
level is fully supported by the CR95HF library.
●An intermediate layer based on ISO/IEC 15693 specification
●A product layer supporting the commands described in the LRIxK and M24LRxx
datasheets
3.2.1 Example of an application architecture
The library can be downloaded from http://www.st.com.
Figure 2 shows the interaction between a typical user application and the CR95HF library
layers.
Doc ID 022255 Rev 211/105
OverviewAN3985
MS19959V1
User functions
(user application specific)
Standard peripheral driver
(MCU specific layer)
CR95HF library
CR95HF library layer
(embeds the commands defined in CR95HF datasheet)
Protocol layer (embeds commands defined in ISO specification)
ISO15693 library layer
Product layer (embeds commands defined in the product datasheet)
LRI*k layerM24LR* layer
Figure 2.Interaction between typical user application and CR95HF library layers
3.2.2 Types
The CR95HF library functions use the following ANSI C compliant types defined in the
stm8l15x.h file.
Signed integer types
typedefsigned charint8_t;
typedef signed short int16_t;
typedef signed longint32_t;
typedef int32_ts32;
typedef int16_ts16;
typedef int8_ts8;
This layer supports all the commands defined in the CR95HF datasheet. Each CR95HF
command corresponds to a specific function. As an example, the function calling the ECHO
command is u8 CR95HF_Echo (void).
Additional functions are described in Table 8: CR95HF layer additional functions.
Application developers can use the functions described in Table 7: CR95HF layer functions
based on CR95HF commands to create their own higher level functions.
Refer to the CR95HF datasheet for more details on the commands.
4.2 Structures
4.2.1 Command format
A command from MCU to CR95HF is composed of three fields:
●command code
●length
●data
The dedicated structure is CR95HF_CommandSending.
Ta bl e 2 lists the fields of a generic command to CR95HF, the number of bytes and the name
of the dedicated structure member.
Table 2.Command fields formats
FieldLengthDedicated structure
Command code1 bytecmd_code
Length1 bytedata_lenght
DataUp to 255 bytesdata
The structure definition of a command to CR95HF is:
Note:This structure is defined in CR95HF_structure.h file.
For more details about idle command, refer to CR95HF datasheet.
Doc ID 022255 Rev 215/105
CR95HF low level layerAN3985
4.3 CR95HF layer functions
The tables below list the functions available in a CR95HF layer.
Table 7.CR95HF layer functions based on CR95HF commands
Function nameDescription
CR95HF_IDNSends an Idn command.
CR95HF_EchoSends an Echo command.
CR95HF_ProtocolSelectSends a ProtocolSelect command.
CR95HF_SendRecvSends a SendRecv command.
CR95HF_IdleSends an Idle command.
CR95HF_Rd_Wake_up_RegReads the wake-up Register command.
CR95HF_Rd_Analog_Register_Config_B
Reads the analog register configuration B command.
CR95HF_BaudRateSends a BaudRate command.
Table 8.CR95HF layer additional functions
Function nameDescription
CR95HF_SendEOFSends an EOF pulse.
CR95HF_FieldOffSwitches off the RF field.
Table 9.Low Power mode functions
Function nameDescription
CR95HF_HibernateSends an hibernate command to CR95HF.
CR95HF_SleepSends a Sleep command to CR95HF.
CR95HF_IdlebyTimerfunction
Sends a Idle command to CR95HF. The wake-up
source is its internal timer.
CR95HF_TagDetectingSends a Tag detecting command to CR95HF.
Table 10.CR95HF layer IS functions
Function nameDescription
CR95HF_IsReaderResultOKChecks if the returned code is successful.
CR95HF_Is_IRQ_in_Interrupt_Wake_up
_Condition
CR95HF_Is_NSS_Interrupt_Wake_up_
Condition
CR95HF_Is_Tag_Detected_Wake_up_
Condition
CR95HF_Is_Timeout_Wake_up_
Condition
Checks if a pulse on IRQ_IN
condition.
Checks if a pulse on NSS pad is a wake-up
condition.
Checks if a tag detector state is a wake-up
condition.
Checks if an internal timer is a wake-up condition.
pad is a wake-up
16/105Doc ID 022255 Rev 2
AN3985CR95HF low level layer
Table 11.CR95HF layer advanced functions
Function nameDescription
CR95HF_Modify_Baud_RateChanges the UART baud rate of MCU and
CR95HF.
CR95HF_Idle_Detector_CalibrationCarries out a calibration for tag detector mode.
CR95HF_Wait_Wake_up_From_IdleWaits for CR95HF to exit the idle state and retrieve
what caused the wake-up.
CR95HF_Wake_up_CR95HFWakes up CR95HF when exists idle mode.
4.3.1 Command functions
CR95HF_IDN function
This function sends an IDN command to the CR95HF device. It returns its version number.
Table 12.CR95HF_IDN function description
Prototypeu8 CR95HF_IDN (CR95HF_ResponseReceiving* Response);
Input parameterNone
Output parameterResponse: pointer on the response structure
Return parameter
CR95HF_SUCCESS_CODE: The command is successful.
CR95HF_ERROR_CODE: The command failed.
CR95HF_Echo function
This function sends an EchoCode command to the CR95HF which returns an Echo code
response (0x55). The Echo function checks that communications can be started between
the MCU and the CR95HF.
CR95HF_SUCCESS_CODE: The command is successful.
CR95HF_ERROR_CODE: The command failed.
Doc ID 022255 Rev 217/105
CR95HF low level layerAN3985
CR95HF_ProtocolSelect function
This function sends a Protocol Select command to the CR95HF. It selects the RF
communication protocol, configures RF parameters, and switches the RF field on.To set up
communications with a contactless tag, the Protocol Select command shall be sent to the
CR95HF before the SendRecv command.
Table 14.CR95HF_ProtocolSelect function description
1. It is recommended to set the append CRC bit (see Chapter 5.3.4: CRC16 management)
3
0b1: wait for SOF
0b0: 100% modulation
2
0b1: 10% modulation
0b0: single subcarrier
1
0b1: single subcarrier
0b0: don’t append CRC
0
0b1: append CRC
(1)
For more detail concerning this parameter, please refer to the CR95HF datasheet.
18/105Doc ID 022255 Rev 2
AN3985CR95HF low level layer
CR95HF_SendRecv function
This function sends a SendRecv command. The parameter passed to the command is the
frame, which is coded according to the protocol previously selected by issuing a Protocol
Select command. The CR95HF encodes the frame, transmits it at 13.56 MHz, and decodes
the contactless tag response.
Table 16.CR95HF_SendRecv function description
Prototype
Input parameterCommand: the command structure to send to a contactless tag
Output parameterResponse: pointer on the response structure
Return parameter
The Parameters of input parameter depend on the selected protocol. Ta bl e 1 5 gives
ISO/IEC 15693 protocol parameters.
In case of a correct response, the SendRecv response sent to MCU is as follows:
WakeUpFlags: Specifies wake-up condition.
EnterCtrlL: first byte of setting to enter to Idle mode
EnterCtrlH: second byte of setting to enter to Idle mode
WUCtrlL: first byte of setting to wake-up from Idle mode
WUCtrlH: second byte of setting to wake-up from Idle mode
LeaveCtrlL: first byte of setting to leave Idle mode
LeaveCtrlH: second byte of setting to leave Idle mode
WUPreiod: period of time between two tags detection
OscStart: waiting time to stabilize HFO
DacStart: waiting time to stabilize DAC
DacDataL: lower compare value for tag detection
DacDataH: higher compare value for tag detection
SwingsCnt: number of HF swings during tag detection
MaxSleep: maximum number of tag detection trials before timeout
Output parameterIdle_Config: pointer on the Idle structure
Return parameterCR95HF_SUCCESS_CODE: The command is successful.
Note:Some Low power mode commands using the Idle command are defined in Chapter 4.3.3:
Low power mode functions
CR95HF_Rd_Wake_up_Reg function
This function reads the wake-up register of the CR95HF to determine what has caused the
awakening of the CR95HF after an Idle command.
Table 20.CR95HF_Rd_Wake_up_Reg function description
Response: pointer on the structure to fill in with the data retrieved
Idle_Config: pointer on the structure to update
CR95HF_SUCCESS_CODE: The command is successful.
CR95HF_ERROR_INVALID_CMD_LENGTH:The command failed,
invalid length.
CR95HF_ERROR_CODE: Command failed, response contains the
error code.
20/105Doc ID 022255 Rev 2
AN3985CR95HF low level layer
CR95HF_Rd_Analog_Register_Config_B function
This function reads the analog register configuration B of the CR95HF to check the analog
configuration Register containing the modulation depth and reader chain gain.
Note:A CR95HF_Set_Analog_Register_Config_B_Index command must be issued before
reading the register.
Table 21.CR95HF_Rd_Analog_Register_Config_B function description
This function sends a BaudRate command to the CR95HF. It allows to configure the UART
baudrate.
Table 22.CR95HF_Baud_Rate function description
Prototypeu8 CR95HF_Baud_Rate(u8 New_Baud_Rate);
Input parameterNew_Baud_Rate: new baud rate = 13.56/(2*BaudRate+2) Mbps
Output parameterpResponse: pointer on the CR95HF response
CR95HF_SUCCESS_CODE: The command is successful.
Return parameter
CR95HF_ERROR_CODE: Unsuccessful command, UART
communication may be lost.
Doc ID 022255 Rev 221/105
CR95HF low level layerAN3985
4.3.2 Additional functions
CR95HF_SendEOF function
This function sends an EOF. This can be used for instance in an ISO/IEC 15693 inventory or
write command. Right after the EOF, the tag could answer a command (Write or Inventory),
so the response is received and written into the provided structure.
CR95HF_SUCCESS_CODE: The field was successfully turned off
CR95HF_ERROR_INVALID_PROTOCOL: The selected protocol is wrong.
CR95HF_ERROR_INVALID_CMD_LENGTH: The command length is invalid.
CR95HF_ERROR_CODE: The command failed
4.3.3 Low power mode functions
These functions send an idle command with some parameters forced into the function.
CR95HF_Hibernate function
This function sends an Hibernate command to CR95HF and turns the hibernate state on.
This is the lowest consumption state of CR95HF. Only a pulse on
the CR95HF.
Input parameterWakeUpSource: the source of wake-up for CR95HF
Output parameter
Idle_Config: pointer on the structure which contains data about some idle
parameters to update
Return parameter CR95HF_SUCCESS_CODE: The command is successful.
1. e.g.: for a wake-up by a pulse on SPI_NSS pad, use CR95HF_IDLE_WAKEUP_SPIN_SS value. For a
wake- up by a pulse on IRQ_IN
pad, use CR95HF_IDLE_WAKEUP_IRQ_in value.
CR95HF_TagDetecting function
This function sends a Tag detecting command to CR95HF. The CR95HF turns in a low
power mode and periodically checks if a contactless tag is in the operating value. In this last
case, the CR95HF will wake-up by itself.
FALSE_CODE: An interrupt on IRQ_IN pin is not a wake-up condition.
24/105Doc ID 022255 Rev 2
AN3985CR95HF low level layer
Is_NSS_Interrupt_Wake_up_Condition function
This function returns whether an interrupt on NSS is a wake-up condition or not.
Table 31.Is_NSS_Interrupt_Wake_up_Condition function description
Prototypeu8 CR95HF_Is_NSS_Interrupt_Wake_up_Condition(u8 WUFlags);
Input parameterWUFlags: the byte containing the expected data
Output parameter None
Return parameter
TRUE_CODE: An interrupt on IRQ_IN
FALSE_CODE: An interrupt on IRQ_IN is not a wake-up condition.
is a wake-up condition.
Is_Tag_Detected_Wake_up_Condition function
This function returns whether a tag detection is a wake-up condition or not.
Table 32.Is_Tag_Detected_Wake_up_Condition function description
Prototypeu8 CR95HF_Is_Tag_Detected_Wake_up_Condition_(u8 WUFlags);
Input parameterWUFlags: the byte containing the expected data
Output parameter None
Return parameter
TRUE_CODE: A tag detection is a wake-up condition.
FALSE_CODE: A tag detection is not a wake-up condition.
Is_Timeout_Wake_up_Condition function
This function returns whether the internal timer is a wake-up condition.
Table 33.Is_Tag_Detected_Wake_up_Condition function description
Prototypeu8 CR95HF_Is_Timeout_Wake_up_Condition(u8 WUFlags);
Input parameterWUFlags: the byte containing the expected data
Output parameter None
Return parameter
TRUE_CODE: An interrupt on IRQ_IN
FALSE_CODE: An interrupt on IRQ_IN
is a wake-up condition.
is not a wake-up condition.
Doc ID 022255 Rev 225/105
CR95HF low level layerAN3985
4.3.5 Advanced functions
CR95HF_Modify_Baud_Rate function
This function is a procedure to change baud rates in UART communication. The function
calls the CR95HF_Baud_Rate function and changes the MCU internal baud rate. In case of
a failure, the function tries to recover the communication by returning into the previous baud
rate or into the default baud rate.
Table 34.CR95HF_Modify_Baud_Rate function description
Protocol_Settings: pointer on the structure to update the protocol select. (Turn
RF field off)
Idle_Config: pointer on the structure to write the high compare value and low
compare value for tag detection and the last wake-up reason.
CR95HF_SUCCESS_CODE: The command is successful.
CR95HF_ERROR_CODE: The command failed.
26/105Doc ID 022255 Rev 2
AN3985CR95HF low level layer
CR95HF_Wait_Wake_up_From_Idle function
The purpose of this function is to wait for CR95HF to exit the idle state and retrieve what
caused the wake-up. The method proposed is to wait for the CR95HF answer and then to
send several echo commands in order to resume communication and clear the CR95HF
response FIFO stack.
Table 36.CR95HF_Wait_Wake_up_From_Idle function description
pointer on the structure to update with the new last reason of wake-up
Input parameter
Output parameter
Return parameter
Idle_Config:
Number_of_DELAY_BEFORE_TIMEOUT_250MS: number of periods of
250ms duration to wait before aborting the procedure
Idle_Config: pointer on the structure to write the High compare value and low
compare value for tag detection and the last wake-up reason
EXIT_COMMUNICATION_OK (CR95HF_SUCCESS_CODE): communication is
possible.
EXIT_COMMUNICATION_TIMEOUT: communication is not possible.
This function waits for CR95HF to wake-up on its own (internal timer, tag detection).
After this, the CR95HF_Wake_up_CR95HF function may be called to wake-up the CR95HF
with NNS or IRQ_IN
interrupt.
CR95HF_Wake_up_CR95HF function
This function wakes up the CR95HF device from idle mode. The method proposed is to
send several echo commands in order to resume communication and clear the CR95HF
response FIFO stack.
Table 37.CR95HF_Wake_up_CR95HF function description
Prototypeu8 CR95HF_Wake_up_CR95HF(CR95HF_Idle_Config* Idle_Config);
Input parameterIdle_Config: contains the wake-up condition
Output parameter None
Return parameter
CR95HF_SUCCESS_CODE: the CR95HF woke up, communication is possible.
CR95HF_ERROR_CODE: fail to wake-up the CR95HF, either the CR95HF waits
for another event (tag detection, timer) or communication cannot be resumed.
Doc ID 022255 Rev 227/105
CR95HF low level layerAN3985
AI18875
Start
Echo
Echo
response?
Select
protocol
Selected?
SendRecv
Another
protocol?
No
Error code
Yes
No
Yes
Yes
4.3.6 Application example: protocol selection and communication
To communicate with a contactless tag, the application must first select the RF protocol by
sending a ProtocolSelect command. Then the application can use SendRecv commands to
send data to a contactless tag. The user can select another protocol or change RF
parameters (e.g. choose another data rate) by issuing again a ProtocolSelect command
(refer to Figure 3).
Figure 3.Function flowchart example
28/105Doc ID 022255 Rev 2
AN3985ISO/IEC 15693 layer
5 ISO/IEC 15693 layer
This layer is composed of:
●the source file: iso15693_command.c
●the included file: iso15693_command.h
5.1 Overview
The ISO/IEC 15693 specification defines a set of commands to handle one or more
contactless tags, and some commands of a higher level such as read or write commands.
This layer supports all the commands defined in the ISO/IEC 15693 specification and
integrates some functions to facilitate the use of the ISO/IEC 15693 commands.
5.2 Structures
This layer uses a structure to store the information of an ISO/IEC contactless tag.
5.2.1 Structures of command and response of an ISO/IEC 15693 tag
The ISO15693_tag structure contains the different fields associated to an ISO/IEC15693
contactless tag.
Table 38.ISO15693_tag structure description
NameNumber of bytesComment
command_ok1
error_code1
response_flags1Response flag provided by the tag
dataField
receivedDataLength1
UID_TagISO15693_UID_SIZEUID of the contactless tag
AFI_Tag1AFI of the contactless tag
DSFID_Tag1DSFID of the contactless tag
ICRef1
memSize
ISO15693_DATA_RECEIVED_MAX
_SIZE
ISO15693_MEMSIZE_MAX_WORD_
BYTE_SIZE
The command and the response
are ok, the structure is ok.
Error code of a contactless tag or
CRC error
Whole data transmitted by the tag
and CR95HF control byte
Length of data transmitted by the
Ta g
ICRef of the Tag (only for those
manufactured by
STMicroelectronics).
Memfield of getsystem info
command
Doc ID 022255 Rev 229/105
ISO/IEC 15693 layerAN3985
This structure is defined in ISO15693_structure.h.
The commands defined by ISO/IEC 15693 specification have the following structure:
SOFRequest flagCommand codeDataCRCEOF
With:
●SOF: start of frame
●Request flag: 1 byte
●Command code: 1 byte
●Data: 1 or more byte
●CRC: 2 bytes
●EOF: end of frame
5.3.1 SOF and EOF
The SOF and EOF are managed by CR95HF device.
5.3.2 Request flag management
The request flags byte is managed by the user application. Each bit or flag specifies the
actions to be carried out by the contactless tag and whether the corresponding fields are
present or not.
The request flags is a byte integrated on all ISO/IEC 15693 commands, which specifies the
actions to be performed by the contactless tag.
The meaning of bit 1, 2 and 4 is the same for all ISO/IEC 15693 commands.
Bit 3 (Inventory_flag) of the request flag defines the contents of the 4 MSBs (bits 5 to 8).
When bit 3 is reset (0), bits 5 to 8 contain the contactless tag selection criteria. When bit 3 is
set (1), bits 5 to 8 define the contactless tag Inventory parameters.
30/105Doc ID 022255 Rev 2
Loading...
+ 75 hidden pages
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.