ST AN3954 Application note

Introduction
This application note explains how to develop an application using either Visual Basic or C/C++ language to drive the DEMO-CR95HF-A demonstration board from a host computer.
The DEMO-CR95HF-A board is powered through the USB port and no external power supply is required. It is made up of a CR95HF contactless transceiver, a 48 x 34 mm 13.56 MHz inductive etched antenna and its associated tuning components. The CR95HF communicates with the STM32F103CB 32-bit core MCU via the SPI bus.
A dynamic link library (DLL) file is used by the host computer to manage several functions and communicate with the STM32 MCU and the CR95HF IC. It is included in the demo-cr95hf-a_dev_appli.zip version 2 (Visual Basic 6 and C/C++ source code examples) which can be downloaded from http://www.st.com.
AN3954
Application note
Developing your own Visual Basic or C/C++ application
on a DEMO-CR95HF-A demonstration board
An executable with a simple user interface is also available for both Visual Basic and C/C++ projects.

Figure 1. DEMO-CR95HF-A evaluation board

For more information, refer to the following documents available on www.st.com:
CR95HF: 13.56-MHz multi-protocol contactless transceiver IC with SPI and UART serial
access datasheet
DEMO-CR95HF-A: Demonstration kit for the CR95HF 13.56 MHz transceiver IC data
brief
UM1084: CR95HF development software user guide
December 2011 Doc ID 022016 Rev 2 1/42
www.st.com
Contents AN3954
Contents
1 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 Connecting the board to your computer . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Using the DLL system file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 Overview of DLL functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Function descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.1 Functions to check DLL installation and USB connection . . . . . . . . . . . . . 6
2.1.1 CR95HFDll_GetDLLrev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.1.2 CR95HFDLL_USBconnect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1.3 CR95HFDLL_USBhandlecheck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 Functions to communicate with the STM32 MCU . . . . . . . . . . . . . . . . . . 11
2.2.1 CR95HFDll_Echo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2.2 CR95HFDLL_getMCUrev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2.3 CR95HFDLL_getInterfacePinState . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 Functions to communicate with the CR95HF IC . . . . . . . . . . . . . . . . . . . 17
2.3.1 CR95HFDll_Idn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.3.2 CR95HFDll_Select . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3.3 CR95HFDll_STCmd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.3.4 CR95HFDll_FieldOff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.3.5 CR95HFDll_ResetSPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.3.6 CR95HFDll_SendIRQPulse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.3.7 CR95HFDll_Polling_Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.3.8 CR95HFDll_SendNSSPulse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.4 Functions to communicate with a RF tag . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.4.1 CR95HFDll_SendReceive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Appendix A Error codes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2/42 Doc ID 022016 Rev 2
AN3954 Getting started
-36
)3/2&
-#5
#2(&
#ONTACTLESS
TAG
#2(&
LIBRARY
2&
2&)$02/4/#/,
$%-/#2(&!
53"
(/34
#/-054%2
53"
#2$LLDLL

1 Getting started

1.1 Connecting the board to your computer

The DEMO-CR95HF-A is connected to the host computer through its USB port. No particular USB driver is needed. The HID USB driver (already included in all Windows operating systems) is used for communication.
Through the USB port of your computer, you can drive the board, communicate with the STM32 MCU, manage the CR95HF IC and send requests to a RF tag.

Figure 2. Typical application block diagram

1.2 Using the DLL system file

A DLL file (CR95HF.dll) is provided in order to drive the DEMO-CR95HF-A board from your computer.
Copy this file to the system folder of your computer (C:/Windows/system32/).
This DLL contains several functions to enable communication with the board.
Using this DLL, the host computer is able to manage several functions and communicate with the STM32 MCU and the CR95HF IC.

1.2.1 Overview of DLL functions

The CR95HFDll_GetDLLrev function reads the revision number of the DLL file. If the DLL file is not correctly installed in the system folder of the host computer, this function returns an error message (see Figure 3).
The CR95HFDLL_USBconnect function is required to connect the DEMO-CR95HF-A board to the host computer and communicate with it. It is available from DLL revision 0.5 and upper. The computer first checks if a DEMO-CR95HF-A is connected to the USB port. If so, it assigns a USB handle to the board (see Figure 3).
The CR95HFDLL_USBhandlecheck function can be used to verify if the DEMO-CR95HF-A is connected to the USB port of your computer. It is available from DLL revision 0.6 and upper. This function sends a request to the DEMO-CR95HF-A and waits for an acknowledge. An error code is sent if the communication with the board has been lost. In
Doc ID 022016 Rev 2 3/42
Getting started AN3954
-36
)3/2&
-#5
#2(&
#ONTACTLESS
TAG
#2(&
LIBRARY
2&
$%-/#2(&!
53"
(/34
#/-054%2
53"
REVISION
#2$LLDLL
-36
)3/2&
-#5
#2(&
#ONTACTLESS
TAG
#2(&
LIBRARY
2&
$%-/#2(&!
53"
(/34
#/-054%2
53"
#2$LLDLL
this case, the CR95HFDLL_USBconnect can be used to reconnect the DEMO-CR95HF-A to the host computer.
Figure 3. Typical application block diagram
A set of functions are available to drive the STM32 MCU (see Figure 4):
CR95HFDll_Echo: verifies that the DEMO-CR95HF-A board is connected to the USB
port of the computer.
CR95HFDLL_getMCUrev: reads the firmware revision of the STM32 MCU.
CR95HFDLL_getInterfacePinState: verifies the communication path between the
STM32 MCU and the CR95HF IC (either SPI or UART).
Figure 4. Typical application block diagram
Other functions can be used to send instructions to the CR95HF IC (see Figure 5):
CR95HFDll_Idn: gets the IDN of the CR95HF IC.
CR95HFDll_Select: configures the required RF protocol for the CR95HF IC (IS0
15693, ISO 14443B …) and starts the RF field on the antenna.
CR95HFDll_STCmd: sends commands to the CR95HF IC (refer to datasheet for a list
of possible commands).
CR95HFDll_FieldOff: stops the RF field of the CR95HF IC.
CR95HFDll_ResetSPI: resets the SPI interface of the CR95HF IC.
CR95HFDll_SendIRQPulse: sends an interruption pulse on the IRQ pin of the CR95HF
IC to wake it up. This interrupt function can be used when the communication between the STM32 MCU and the DEMO-CR95HF-A is performed 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 DEMO-CR95HF-A 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 FIFO answer buffer (e.g. after an IDLE command)
4/42 Doc ID 022016 Rev 2
AN3954 Getting started
-36
)3/2&
-#5
#2(&
#ONTACTLESS
TAG
#2(&
LIBRARY
2&
$%-/#2(&!
53"
(/34
#/-054%2
53"
#2$LLDLL
Figure 5. Typical application block diagram
Finally, the CR95HFDll_SendReceive function sends an RF frame to any tag present in the RF field and receives its answer according to the RF protocol previously defined using the Select command (see Figure 6).
Figure 6. Typical application block diagram
(/34
#/-054%2
#2$LLDLL
53"
53"
#2(&
LIBRARY
-#5
$%-/#2(&!
#2(&
)3/2&
2&
#ONTACTLESS
TAG
-36
Doc ID 022016 Rev 2 5/42
Function descriptions AN3954

2 Function descriptions

This section describes the various functions required to evaluate the DEMO-CR95HF-A demonstration board.

2.1 Functions to check DLL installation and USB connection

2.1.1 CR95HFDll_GetDLLrev

This function detects if the CR95HF.dll file has been installed in the system folder of the host PC system, reads its header and sends back its revision number.
This is used by software to ensure that the DLL is correctly installed 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 system.
Answer example: "0.4"
Returned value: lngStatus: 0
Source code example
Private Sub cmdGetDllRev_Click() Dim strAnswer As String * 256 Dim lngStatus As Long lngStatus = CR95HFDll_GetDLLrev(strAnswer) If (lngStatus = 0) Then
txtDllRev.Text = strAnswer
Else
txtDllRev.Text = "Error = no Dll found" End If End Sub
6/42 Doc ID 022016 Rev 2
AN3954 Function descriptions
C/C++ language
Declaration: __declspec(dllimport) int __stdcall
CR95HFDll_GetDLLrev(char* stringreply);
Prototype: int iresult;
char strAnswer[50]=""; iresult = CR95HFDll_GetDLLrev(strAnswer);
Input parameter: None
Output parameter: strAnswer: Revision of the DLL installed on your 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 communication with MCU\n\n\r", strAnswer);
printf("\n\n\n\n"); printf("\npress any key to continue"); printf("\n"); scanf("%s", &entry3);
return iresult; }
Doc ID 022016 Rev 2 7/42
Function descriptions AN3954

2.1.2 CR95HFDLL_USBconnect

This function can be used to connect the DEMO-CR95HF-A to the computer USB port. The computer then assigns an USB handle to the board.
Visual Basic language
Declaration: Public Declare Function CR95HFDLL_USBconnect
Lib"CR95HF.dll" () As Long
Prototype: Dim lngStatus As Long
lngStatus = CR95HFDLL_USBconnect()
Input parameter: None
Output parameter: None
Return value: lngStatus:
0: the DEMO-CR95HF is connected and ready to be used
1: DEMO-CR95HF-A board not connected
Source code example
Private Sub cmdUSBconnect_Click() Dim lngStatus As Long lngStatus = CR95HFDLL_USBconnect() If (lngStatus = 0) Then
txtUSBconnectAnswer.Text="DEMO-CR95HF-A connected and ready
to be used" Else
txtUSBconnectAnswer.Text = "DEMO-CR95HF-A not connected" End If End Sub
C/C++ language
Declaration: __declspec(dllexport) int __stdcall
CR95HFDLL_USBconnect(void);
Prototype: int iresult;
iresult= CR95HFDLL_USBconnect ();
Input parameter: None
Output parameter: None
Return value: iresult:
0: the DEMO-CR95HF is connected and ready to be used
1: DEMO-CR95HF-A board not connected
Source code example
int DEMO_CR95HF_A_USBconnect (void) {
int iresult; char entry3;
printf("\n\n\n\n");
8/42 Doc ID 022016 Rev 2
AN3954 Function descriptions
iresult= CR95HFDLL_USBconnect (); printf("\nCR95HFDll_USBconnect function:\n"); printf("\n --> request : CR95HFDll_USBconnect()"); printf("\n <-- answer : "); if (iresult == 0)
printf("DEMO-CR95HF-A is connected and ready to be used\n\n\r");
else
printf("Error : DEMO-CR95HF-A not ready to be used\n\n\r"); printf("\n\n\n\n"); printf("\npress any key to continue"); printf("\n"); scanf("%s", &entry3); return iresult;
}

2.1.3 CR95HFDLL_USBhandlecheck

This function can be used to verify if the DEMO-CR95HF-A is correctly connected to the USB port of the computer and if the USB Handle attribué 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 1: the USB handle is invalid and the DEMO-CR95HF-A board
not connected
Source code example
Private Sub cmdUSBhandlecheck_Click() Dim lngStatus As Long txtUSBhandlecheckAnswer.Text = "" lngStatus = CR95HFDLL_USBhandlecheck() If (lngStatus = 0) Then txtUSBhandlecheckAnswer.Text = "DEMO-CR95HF-A connected" ElseIf (lngStatus = 5) Then txtUSBhandlecheckAnswer.Text = "DEMO-CR95HF-A not connected (use
CR95HFDLL_USBconnect to connect it)" End If
End Sub
Doc ID 022016 Rev 2 9/42
Function descriptions AN3954
C/C++ language
Declaration: __declspec(dllexport) int __stdcall
CR95HFDLL_USBhandcheck(void);
Prototype: int iresult;
iresult= CR95HFDLL_USBhandcheck();
Input parameter: None
Output parameter: None
Return value: iresult:
0: the USB handle is valid 1: the USB handle is invalid and the DEMO-CR95HF-A board
not connected
Source code example
int DEMO_CR95HF_A_USBhandlecheck (void) { int iresult; char entry3; printf("\n\n\n\n"); iresult= CR95HFDLL_USBhandlecheck (); printf("\nCR95HFDll_USBhandlecheck 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 : DEMO-CR95HF-A 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; }
10/42 Doc ID 022016 Rev 2
AN3954 Function descriptions

2.2 Functions to communicate with the STM32 MCU

2.2.1 CR95HFDll_Echo

This function sends a USB request to the STM32 MCU that executes an Echo request on the CR95HF. The STM32 MCU sends back the answer of the CR95HF, if possible, or returns an error code ‘1’ if there is no answer.
Note: The Echo request uses the EchoCode command to perform a serial interface echo. For
more information, refer to the CR95HF transceiver datasheet.
Visual Basic language
Declaration: Public Declare Function CR95HFDll_Echo Lib "CR95HF.dll"
(ByVal mystring As String) As Long
Prototype: Dim lngStatus as long
Dim strAnswer As String * 256 lngStatus = CR95HFDll_Echo(strAnswer)
Input parameter: None
Output parameter: strAnswer: The CR95HF IC answer to the Echo request is “5500” if
there is no error. Answer example: “5500”
Returned value: lngStatus:
0: No error 5: DEMO-CR95HF-A board not connected
Source code example
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 DEMO-CR95HF-A board" End If
End Sub
Doc ID 022016 Rev 2 11/42
Function descriptions AN3954
C/C++ language
Declaration: __declspec(dllimport) int __stdcall CR95HFDll_Echo(char
*Stringreply);
Prototype: int iresult;
char strAnswer[50]=""; iresult= CR95HFDll_Echo (strAnswer);
Input parameter:None
Output parameter: strAnswer: The CR95HF IC answer to the Echo request is “5500” if
there is no error. Answer example: “5500”
Returned value:iresult:
0: No error 5: DEMO-CR95HF-A board not connected
Source code example
int Echo (void) { char strAnswer[50]=""; int iresult; char entry3;
printf("\n\n\n\n");
iresult= CR95HFDll_Echo (strAnswer);
printf("\nEcho using CR95HFDll_Echo function:\n"); printf("\n --> request : CR95HFDll_Echo(strAnswer)"); printf("\n <-- answer : ");
if (iresult == 0)
printf("%s = Echo command answer\n\n\r", strAnswer);
else
printf("%s = No answer from MCU\n\n\r", strAnswer);
printf("\n\n\n\n"); printf("\npress any key to continue"); printf("\n"); scanf("%s", &entry3);
return iresult; }
12/42 Doc ID 022016 Rev 2
AN3954 Function descriptions

2.2.2 CR95HFDLL_getMCUrev

This function sends a USB request to the STM32 MCU on the DEMO-CR95HF-A board that sends 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 DEMO-
CR95HF-A board. Answer example: “0003010300”
Where:
00: Status byte (see Appendix A for error codes) 03: Size of answer (in bytes) 010300: Revision 1.3.0
Returned value: lngStatus:
0: No error 1: DEMO-CR95HF-A board not connected
Source code example
Private Sub cmdGetMCURev_Click() Dim strAnswer As String * 256 Dim lngStatus As Long
lngStatus = CR95HFDLL_getMCUrev(strAnswer)
If (lngStatus = 0) Then txtMCUrev.Text = strAnswer Else txtMCUrev.Text = "Error = no communication with STM32 MCU" End If
End Sub
C/C++ language
Declaration: __declspec(dllexport) int __stdcall
CR95HFDLL_getMCUrev(char* stringreply);
Prototype int iresult;
char strAnswer[50]=""; iresult = CR95HFDLL_getMCUrev(strAnswer);
Input parameter:None
Output parameter: strAnswer: Firmware revision of the STM32 MCU on DEMO-
CR95HF-A board. Answer example: “0003010300”
Where:
00: Status byte (see Appendix A for error codes)
Doc ID 022016 Rev 2 13/42
Loading...
+ 29 hidden pages