background image

January 2017

Doc ID022016 Rev 4

1/47

1

AN3954

Application note

Developing your own Visual Basic or C/C++ application 

  on a CR95HF demonstration board 

  

Introduction

This application note explains how to develop an application using either Visual Basic or 
C/C++ language to drive a CR95HF demonstration board from a host computer. 

CR95HF demonstration board is a kit that allows the user to evaluate the performance of 
STMicroelectronics CR95HF, a 13.56 MHz multiprotocol contactless transceiver. It includes 
a ready-to-use board to interface with the CR95HF host PC demonstration software through 
an USB interface. The CR95HF demonstration board is included in the M24LR-
DISCOVERY kit (see 

Figure 1

).

The board is powered through the USB port, and no external power supply is required. It is 
made up of a CR95HF, a 48 mm x 34 mm, 13.56 MHz inductive etched antenna and its 
associated tuning components. The CR95HF communicates with the STM32F103CB 32-bit 
core microcontroller via the SPI bus.

A dynamic link library (DLL) file is used by the host computer to manage several functions 
and to communicate with both the STM32 MCU and the CR95HF

Source code examples described in this application note and additional demonstrations can 
be found in the package STSW-95HF002, which can be downloaded from 

www.st.com

Some basic examples are available in C/C++, additional examples are available in a Visual 
Basic 6 project, among them NDEF message management on Type 4A and Type 5, 
ST25DV user interface and FTM demonstrations. DLL file is also available in this package.

Figure 1. CR95HF demonstration board embedded in the M24LR-Discovery kit

For more information, refer to the following documents, available on 

www.st.com

:

M24LR-DISCOVERY databrief 

CR95HF datasheet 

User manual UM1084 “CR95HF development software”

STSW-95HF002 source code example

www.st.com

Summary of Contents for AN3954

Page 1: ... mm 13 56 MHz inductive etched antenna and its associated tuning components The CR95HF communicates with the STM32F103CB 32 bit core microcontroller via the SPI bus A dynamic link library DLL file is used by the host computer to manage several functions and to communicate with both the STM32 MCU and the CR95HF Source code examples described in this application note and additional demonstrations ca...

Page 2: ...2 MCU 12 2 2 1 CR95HFDll_Echo 12 2 2 2 CR95HFDLL_getMCUrev 14 2 2 3 CR95HFDLL_getHardwareVersion 16 2 2 4 CR95HFDLL_getInterfacePinState 18 2 3 Functions to communicate with the CR95HF IC 20 2 3 1 CR95HFDll_Idn 20 2 3 2 CR95HFDll_Select 22 2 3 3 CR95HFDll_STCmd 26 2 3 4 CR95HFDll_FieldOff 31 2 3 5 CR95HFDll_ResetSPI 33 2 3 6 CR95HFDll_SendIRQPulse 35 2 3 7 CR95HFDll_Polling_Reading 36 2 3 8 CR95HF...

Page 3: ...Doc ID022016 Rev 4 3 47 AN3954 List of tables 3 List of tables Table 1 List of error codes 45 Table 2 Document revision history 46 ...

Page 4: ...n board embedded in the M24LR Discovery kit 1 Figure 2 Typical application block diagram 5 Figure 3 Function CR95HFDLL_USBconnect 6 Figure 4 Driving the MCU on the CR95HF demonstration board from the PC 6 Figure 5 Driving the CR95HF from the PC 7 Figure 6 Sending frames directly to the tag 7 ...

Page 5: ...unication Through the USB port of the computer the user can drive the board communicate with the STM32 MCU manage the CR95HF IC and send requests to an RF tag Figure 2 Typical application block diagram 1 2 Using the DLL system file A DLL file CR95HF dll is provided to drive the CR95HF demonstration board from your computer copy this file to the system folder of your computer C Windows system32 Thi...

Page 6: ...host computer Figure 3 Function CR95HFDLL_USBconnect A set of functions are available to drive the STM32 MCU see Figure 4 CR95HFDll_Echo verifies that the CR95HF demonstration board is connected to the USB port of the computer CR95HFDLL_getMCUrev reads the firmware revision of the STM32 MCU CR95HFDLL_getHardwareVersion reads the hardware version of the boards available on latest MCU revision board...

Page 7: ...ormed in SPI mode CR95HFDll_SendNSSPulse sends an interrupt pulse to the NSS pin of the CR95HF IC to wake it up This interrupt function can be used when the communication between the STM32 MCU and the CR95HF demonstration board is performed in UART mode CR95HFDll_Polling_Reading launches a polling sequence and reads the CR95HF answer buffer to retrieve the answer when it is not available in the FI...

Page 8: ...alled and that it is up to date If no DLL file is found it returns an error message Visual Basic language Declaration Public Declare Function CR95HFDll_GetDLLrev Lib CR95HF dll ByVal mystring As String As Long Prototype Dim lngStatus As Long Dim strAnswer As String 256 lngStatus CR95HFDll_GetDLLrev strAnswer Input parameter None Output parameter strAnswer Revision of the DLL installed on your PC s...

Page 9: ...our PC system Answer example 0 4 Returned value lresult 0 Source code example int Get_Dll_rev void char strAnswer 50 int iresult char entry3 printf n n n n iresult CR95HFDll_GetDLLrev strAnswer printf nGet Dll revision using CR95HFDll_GetDLLrev function n printf n request CR95HFDll_GetDLLrev strAnswer printf n answer if iresult 0 printf s DLL version n n r strAnswer else printf s Error no communic...

Page 10: ...demonstration board is not connected Source code example Private Sub cmdUSBconnect_Click Dim lngStatus As Long lngStatus CR95HFDLL_USBconnect If lngStatus 0 Then txtUSBconnectAnswer Text CR95HF demonstration board connected and ready to be used Else txtUSBconnectAnswer Text CR95HF demonstration board not connected End If End Sub C C language Declaration __declspec dllexport int __stdcall CR95HFDLL...

Page 11: ... if the USB Handle assigned by the CR95HFDLL_USBconnect is always valid Visual Basic language Declaration Public Declare Function CR95HFDLL_USBhandlecheck Lib CR95HF dll As Long Prototype Dim lngStatus as long lngStatus CR95HFDLL_USBhandlecheck Input parameter None Output parameter None Returned value lngStatus 0 the USB handle is valid 5 the USB handle is invalid and the CR95HF demonstration boar...

Page 12: ...ck function n printf n request CR95HFDll_USBhandlecheck printf n answer if iresult 0 printf DEMO CR95HF A is well connected n n r else printf Error CR95HF demonstration board not connected use CR95HFDLL_USBConnect n n r printf n n n n printf npress any key to continue printf n scanf s entry3 return iresult 2 2 Functions to communicate with the STM32 MCU 2 2 1 CR95HFDll_Echo This function sends a U...

Page 13: ...ample Private Sub cmdEcho_Click Dim strAnswer As String 256 Dim lngStatus As Long lngStatus CR95HFDll_Echo strAnswer If lngStatus 0 Then txtEchoAnswer Text strAnswer Else txtEchoAnswer Text No communication with CR95HF demonstration board End If End Sub C C language Declaration __declspec dllimport int __stdcall CR95HFDll_Echo char Stringreply Prototype int iresult char strAnswer 50 iresult CR95HF...

Page 14: ...nds back the revision number of its firmware Visual Basic language Declaration Public Declare Function CR95HFDLL_getMCUrev Lib CR95HF dll ByVal mystring As String As Long Prototype status CR95HFDLL_getMCUrev strAnswer Input parameter None Output parameter strAnswer Firmware revision of the STM32 MCU on CR95HF demonstration board Answer example 0003010300 Where 00 Status byte see Appendix A for err...

Page 15: ...0300 Where 00 Status byte see Appendix A for error codes 03 Size of answer in bytes 010300 Revision 1 3 0 Returned value iresult 0 No error 5 CR95HF demonstration board not connected Source code example int Get_MCU_rev void char strAnswer 50 int iresult char entry3 printf n n n n iresult CR95HFDLL_getMCUrev strAnswer printf nGet MCU revision using CR95HFDLL_getMCUrev function n printf n request CR...

Page 16: ...sic language Declaration Public Declare Function CR95HFDLL_getHardwareVersion Lib CR95HF dll ByVal mystring As String As Long Prototype lngStatus CR95HFDLL_getHardwareVersion strAnswer Input parameter None Output parameter strAnswer Hardware revision of the CR95HF demonstration board Answer example 00074D423938335F41 Where 00 Status byte see Appendix A for error codes 03 Size of answer in bytes 4D...

Page 17: ... Hardware Revision End If Else txtHardwareRev Text Error no communication with STM32 MCU End If End Sub C C language Declaration __declspec dllexport int __stdcall CR95HFDLL_getHardwareVersion char StringReply Prototype iresult CR95HFDLL_getHardwareVersion strAnswer Input parameter None Output parameter strAnswer Hardware revision of the CR95HF demonstration board Answer example 00074D423938335F41...

Page 18: ...tf n scanf s entry3 return iresult 2 2 4 CR95HFDLL_getInterfacePinState This function verifies the communication path between the STM32 MCU and the CR95HF IC either SPI or UART The STM32 MCU checks which communication configuration is selected on the CR95HF demonstration board Two solder bridges are available to select the SPI or UART configuration By default the board is delivered in the SPI conf...

Page 19: ...er Text strAnswer If Mid strAnswer 6 1 0 Then chkInterfacePinState 0 Value vbChecked chkInterfacePinState 1 Value vbUnchecked ElseIf Mid strAnswer 6 1 1 Then chkInterfacePinState 0 Value vbUnchecked chkInterfacePinState 1 Value vbChecked Else DoEvents error End If Else txtGetInterfacePinStateAnswer Text No answer from the DEMO CR95HF A board End If End Sub C C language Declaration __declspec dllex...

Page 20: ...nswer printf n answer if iresult 0 printf s Get Interface Pin State successfully n n r strAnswer if strAnswer 5 0 printf n c communication in UART mode strAnswer 3 else printf n c communication in SPI mode strAnswer 3 else printf n s No answer from CR95HF demonstration board n n r strAnswer printf n n n n printf npress any key to continue printf n scanf s entry3 return iresult 2 3 Functions to com...

Page 21: ...ror codes 0F Size of answer in bytes 4E4643204653324A41535430 ASCII transcription of the CR95HF IDN in this example IDN NFC FS2JAST0 00 protocol status A998 CRC value Returned value lngStatus 0 No error 5 CR95HF demonstration board not connected Source code example Private Sub cmdIDnRequest_Click Dim strAnswer As String 256 Dim lngStatus As Long lngStatus CR95HFDll_Idn strAnswer If lngStatus 0 The...

Page 22: ...nue printf n scanf s entry3 return iresult 2 3 2 CR95HFDll_Select This function sends a USB request to the STM32 MCU that prepares the CR95HF for communication by executing a Select request containing the selected RF parameters to the the CR95HF IC The STM32 MCU sends back the answer of the CR95HF if possible or returns an error code 1 if there is no answer In addition to selecting the correct RF ...

Page 23: ...e CR95HF datasheet recommends to complete the ISO14443A protocol configuration by optimizing the synchro between digital analog inputs sends CR95HFDLL_STCMD with 01 09043A005804 by modifying modulation gain send CR95HFDLL_STCMD with 01 0904680101D1 ISO14443B 03010180 Where 03 is the ISO 14443B configuration 010180 are the parameters Note The CR95HF datasheet recommends to complete the ISO144443B p...

Page 24: ...wer Else txtISO14443B_SelectAnswer Text Select protocol ISO14443B not executed End If End Sub C C language Declaration __declspec dllimport int __stdcall CR95HFDll_Select char StringCmd char Stringreply Prototype int iresult char strRequest 50 char strAnswer 50 iresult CR95HFDll_Select strRequest strAnswer Input parameter strConfiguration Selected RF communication protocol and certain protocol rel...

Page 25: ...an answer if the CR95HF demonstration board is configured correctly and the RF field is on Answer example 0000 Where 00 is the status byte see Appendix A for error codes 00 is the size of the answer Returned value iresult 0 No error 5 CR95HF demonstration board not connected 2 Empty argument error 3 Command parameter error Source code example int Select_ISO15693 void int iresult char strRequest 50...

Page 26: ...O14443B protocol selected n n r strAnswer else printf s Select command error n n r strAnswer printf n n n n printf npress any key to continue printf n scanf s entry3 return iresult 2 3 3 CR95HFDll_STCmd This function is used to send any request to the CR95HF IC The STM32 MCU receives the frame contained in the request and sends it directly to the CR95HF IC The STM32 MCU sends back the answer from ...

Page 27: ...t length 62 is the wakeup register value 01 is the register size 00 ST reserved Example 2 Select ISO 15693 protocol 010202010D Where 01 is the transparent command header byte 0202010D is the Protocol Select ISO 15693 frame 02 is the Protocol Select command 02 is the request length 010D are the Protocol Select command parameters Example 3 IDLE mode 01070E0B21007801180020606064743F01 Where 01 is the...

Page 28: ..._RdReg Text strAnswer Else txtSTCmd_RdReg Text Read register not executed properly End If End Sub Private Sub cmdSTCmd_ISO15693_Click Dim strAnswer As String 256 Dim lngStatus As Long lngStatus CR95HFDll_STCmd 010202010D strAnswer If lngStatus 0 Then txtSTCmd_ISO15693 Text strAnswer Else txtSTCmd_ISO15693 Text ISO15693 protocol not selected End If End Sub Private Sub cmdSTCmd_Idle_Click Dim strAns...

Page 29: ...keup register value 01 is the register size 00 ST reserved Example 2 Select ISO 15693 protocol 010202010D Where 01 is the transparent command header byte 0202010D is the Protocol Select ISO 15693 frame 02 is the Protocol Select command 02 is the request length 010D are the Protocol Select command parameters Example 3 IDLE mode 01070E0B21007801180020606064743F01 Where 01 is the transparent command ...

Page 30: ...ntf n n n n strcpy strRequest 010803620100 iresult CR95HFDll_STCmd strRequest strAnswer printf nRead register using CR95HFDll_STCmd function n printf n request CR95HFDll_STCmd s strAnswer strRequest printf n answer if iresult 0 printf s Read Register answer n n r strAnswer else printf s No answer from CR95HF IC n n r strAnswer printf n n n n printf npress any key to continue printf n scanf s entry...

Page 31: ...50 int iresult char entry3 printf n n n n strcpy strRequest 01070E0B21007801180020606064743F01 iresult CR95HFDll_STCmd strRequest strAnswer printf nIdle using CR95HFDll_STCmd function n printf n request CR95HFDll_STCmd s strAnswer strRequest printf n answer if iresult 0 printf s Idle answer n n r strAnswer else printf s No answer from CR95HF IC n n r strAnswer printf n n n n printf npress any key ...

Page 32: ...s byte see Appendix A for error codes 00 is the size of the answer Returned value lngStatus 0 No error 5 CR95HF demonstration board not connected Source code example Private Sub cmdFieldOff_Click Dim strAnswer As String 256 Dim lngStatus As Long lngStatus CR95HFDll_FieldOff strAnswer If lngStatus 0 Then txtFieldOffAnswer Text strAnswer Else txtFieldOffAnswer Text Field Off request not executed End...

Page 33: ...er printf n n n n printf npress any key to continue printf n scanf s entry3 return iresult 2 3 5 CR95HFDll_ResetSPI This function resets the CR95HF IC in case of a problem This function only resets the CR95HF IC and not the STM32 MCU Visual Basic language Declaration Public Declare Function CR95HFDll_ResetSPI Lib CR95HF dll ByVal mystring As String As Long Prototype Dim lngStatus As Long Dim strAn...

Page 34: ...esetSPI char stringreply Prototype int iresult char strAnswer 50 iresult CR95HFDll_ResetSPI strAnswer Input parameter None Output parameter strAnswer The CR95HF demonstration board sends back an answer if the SPI has been correctly reset Answer example 0000 Where 00 is the status byte see Appendix A for error codes 00 is the size of the answer Returned value iresult 0 No error 5 CR95HF demonstrati...

Page 35: ...e Lib CR95HF dll ByVal mystring As String As Long Prototype Dim lngStatus As Long Dim strAnswer As String 256 lngStatus CR95HFDll_SendIRQPulse strAnswer Input parameter None Output parameter strAnswer The CR95HF demonstration board sends back an answer if the IRQ pulse is correctly sent Answer example 0000 Where 00 is the status byte see Appendix A for error codes 00 is the size of the answer Retu...

Page 36: ...rror 5 CR95HF demonstration board not connected Source code example int Send_IRQ_Pulse void int iresult char strAnswer 50 char entry3 printf n n n n iresult CR95HFDll_SendIRQPulse strAnswer printf nSend IRQ Pulse using CR95HFDll_SendIRQPulse function n printf n request CR95HFDll_SendIRQPulse strAnswer printf n answer if iresult 0 printf s Send IRQ pulse successfully n n r strAnswer else printf s N...

Page 37: ...rivate Sub cmdPollingReading_Click Dim strAnswer As String 256 Dim lngStatus As Long lngStatus CR95HFDll_Polling_Reading strAnswer If lngStatus 0 Then txtPollingReadingAnswer Text strAnswer Else txtPollingReadingAnswer Text No answer from the CR95HF demonstration board End If End Sub C C language Declaration __declspec dllexport int __stdcall CR95HFDll_Polling_Reading char StringReply Prototype in...

Page 38: ... language Declaration Public Declare Function CR95HFDll_SendNSSPulse Lib CR95HF dll ByVal mystring As String As Long Prototype Dim strAnswer As String 256 Dim lngStatus As Long lngStatus CR95HFDll_SendNSSPulse strAnswer Input parameter None Output parameter strAnswer The CR95HF demonstration board sends back an answer if the NSS pulse is correctly sent Answer example 0000 Where 00 is the status by...

Page 39: ... Appendix A for error codes 00 is the size of the answer Returned value iresult 0 No error 5 CR95HF demonstration board not connected Source code example int Send_NSS_Pulse void int iresult char strAnswer 50 char entry3 printf n n n n iresult CR95HFDll_SendNSSPulse strAnswer printf nSend IRQ Pulse using CR95HFDll_SendNSSPulse function n printf n request CR95HFDll_SendNSSPulse strAnswer printf n an...

Page 40: ...e As String 256 lngStatus CR95HFDll_SendReceive strCR95HFrequest strTagResponse Input parameter strCR95HFrequest The RF Request to be sent by the CR95HF IC to the Tag with previously selected ISO format ISO 15693 Inventory example 260100 Where 260100 is the ISO 15693 Inventory command ISO 14443B Initiate example 0600 Where 0600 is the ISO 14443B Initiate command Output parameter strTagResponse The...

Page 41: ...If End Sub Private Sub cmdSendReceive_ISO14443B_Initiate_Click Dim strTagResponse As String 256 Dim lngStatus As Long lngStatus CR95HFDll_SendReceive 0600 strTagResponse If lngStatus 0 Then txtISO14443B_Initiate_Answer Text strTagResponse Else txtISO14443B_Initiate_Answer Text No answer from the Tag End If End Sub C C language Declaration __declspec dllimport int __stdcall CR95HFDll_SendReceive ch...

Page 42: ...6C125900 Where 80 is the status byte see Appendix A for error codes 04 is the length of the entire data field 6C is the data received from the tag 1259 is the original received CRC value 00 is the protocol error status Returned value iresult 0 No error 5 CR95HF demonstration board not connected 4 Communication error Source code example int Send_ISO15693_Inventory void int iresult char strRequest 5...

Page 43: ...trTagAnswer else printf No tag answer received n printf CR95HF answer s strTagAnswer printf n n n n printf npress any key to continue printf n scanf s entry3 return iresult 2 4 2 SendReceive Request and Response maximum data size Since DLL revision 1 2 and CR95HF demonstration board firmware revision 3 6 6 STSW M24LR007 the CR95HFDll_SendReceive function can send requests up to 256 bytes and recei...

Page 44: ...4 strTagResponse as String 1024 The CR95HF can also manage long frames on requests to increase Request maximum size up to 512 bytes as Long frame support This requires a patch to be sent to the device hence a new function has been added to the firmware To activate this mode user has to send 02 A8 hexadecimal code using CR95HFDll_STCmd function The expected response to this function is 00 02 03 97 ...

Page 45: ...or 8700 Frame wait time out OR no tag 8800 Invalid Start Of Frame 8900 Receive buffer overflow too many bytes received 8A00 Framing error start bit 0 stop bit 1 8B00 EGT time out for ISOIEC 14443 B 8C00 Invalid length Used in Felica when field length 3 8D00 CRC error Used in Felica protocol 8E00 Reception lost without EOF received 8F00 No field FD00 Time out no answer from Tag detected by the CR95...

Page 46: ...ll_ResetSPI Section 2 3 6 CR95HFDll_SendIRQPulse Section 2 4 1 CR95HFDll_SendReceive Updated returned value in Visual basic and C C for Section 2 2 1 CR95HFDll_Echo 09 Oct 2012 3 Added text to Section 1 Getting started Added Section 2 2 3 CR95HFDLL_getHardwareVersion Updated Input parameter in Section 2 3 2 and Section 2 3 3 Output parameter in Section 2 3 1 Returned value in Section 2 1 3 and Sec...

Page 47: ...chasers are solely responsible for the choice selection and use of ST products and ST assumes no liability for application assistance or the design of Purchasers products No license express or implied to any intellectual property right is granted by ST herein Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product...

Reviews: