EVE 1.0
nfc-nxp.h
Go to the documentation of this file.
1 #ifndef DRIVER_NFC_NXP_H
2 #define DRIVER_NFC_NXP_H
3 /**********************************************************************/
4 /**
5  * @file
6  * @brief Driver for NFC in EVE.
7  *
8  * @author DT, SE, Jetro AS
9  ***********************************************************************/
10 
11 #include <sys/etimer.h>
12 #include <dev/spi.h>
13 #include <core/rtos.h>
14 
15 #include <nfc_t2t_lib.h>
16 
17 #include "ph_NxpBuild_App.h"
18 #include "ph_NxpBuild.h"
19 
20 #include <ph_Status.h>
21 #include <ph_RefDefs.h>
22 #include <phbalReg.h>
23 
24 /* Reader Library Headers */
25 #include <ph_Status.h>
26 #include <phOsal.h>
27 #include <phbalReg.h>
28 /* PAL Headers */
29 #include <phpalI14443p3a.h>
30 #include <phpalI14443p4.h>
31 #include <phpalFelica.h>
32 #include <phpalI14443p3b.h>
33 #include <phpalI14443p4a.h>
34 #include <phpalI18092mPI.h>
35 #include <phpalMifare.h>
36 #include <phpalI14443p4mC.h>
37 #include <phpalSli15693.h>
38 #include <phpalI18000p3m3.h>
39 #include <phalI18000p3m3.h>
40 /* AL Headers */
41 #include <phalT1T.h>
42 #include <phceT4T.h>
43 #include <phalFelica.h>
44 /* Discovery Loop Headers */
45 #include <phacDiscLoop.h>
46 
47 
48 /**
49  * \defgroup pn512 pn512 NFC chip driver
50  * \ingroup extdrv
51  * \{
52  *
53  * This NFC driver supports pn512 NFC chip connected with EVE SPI driver.
54  */
55 
56 /******************************************************************************
57  * NXP lib defines
58  *****************************************************************************/
59 
60 #ifdef NXPBUILD__PH_DEBUG
61 #include <stdio.h>
62 #define DEBUG_PRINTF(...) printf(__VA_ARGS__)
63 #else
64 #define DEBUG_PRINTF(...)
65 #endif
66 
67 /* prints if error is detected */
68 #define CHECK_STATUS(x) \
69  if ((x) != PH_ERR_SUCCESS) \
70 { \
71  DEBUG_PRINTF("%s: %d Error - (0x%04X) has occurred : 0xCCEE CC-Component ID, EE-Error code. Refer ph_Status.h\n", \
72  __FILE__, __LINE__, (x)); \
73 }
74 
75 /* if error is detected - prints and returns status */
76 #define CHECK_SUCCESS(x) \
77  if ((x) != PH_ERR_SUCCESS) \
78 { \
79  DEBUG_PRINTF("%s: %d Error - (0x%04X) has occurred : 0xCCEE CC-Component ID, EE-Error code. Refer ph_Status.h\n ", \
80  __FILE__, __LINE__, (x)); \
81  return (x); \
82 }
83 
84 /* if error is detected - prints and returns false */
85 #define ASSERT_STATUS(x) \
86  if ((x) != PH_ERR_SUCCESS) \
87 { \
88  DEBUG_PRINTF("%s: %d Error - (0x%04X) has occurred : 0xCCEE CC-Component ID, EE-Error code. Refer ph_Status.h\n ", \
89  __FILE__, __LINE__, (x)); \
90  return (false); \
91 }
92 
93 /* Set this to 1 for running ISO 10373-6 PICC test bench. When set to 1, code
94  * for creating WTX (for TEST_COMMAND3) in ISO test bench will be enabled. */
95 #define ISO_10373_6_PICC_TEST_BENCH 0
96 
97 /* Use application callback for processing UPDATE BINARY and proprietary commands */
98 #define USE_APP_CALLBACK 1
99 
100 /* Get content length from CC file. Initial content length is 0. */
101 #define GET_CCFILE_CONTENT_LEN(x) ((((uint16_t)(x[0]) << 8) & 0xFF00) | (x[1]))
102 
103 /* Get content length specified in file (first 2 bytes of file) for NDEF and proprietary files */
104 #define GET_FILE_CONTENT_LEN(x) ((((((uint16_t)(x[0]) << 8) & 0xFF00) | (x[1])) > 0)? \
105  (((((uint16_t)(x[0]) << 8) & 0xFF00) | (x[1])) + 2) : 0)
106 
107 #ifdef NXPBUILD__PH_DEBUG
108  #define DEBUG_ERROR_PRINT CHECK_STATUS
109  #define PRINT_INFO(...) DEBUG_PRINTF(__VA_ARGS__)
110 #else /* NXPBUILD__PH_DEBUG */
111  #define DEBUG_ERROR_PRINT(x)
112  #define PRINT_INFO(...)
113 #endif /* NXPBUILD__PH_DEBUG */
114 
115 #define SoftReset_reader_device(pHal) phhalHw_Rc523_Cmd_SoftReset(pHal)
116 
117 /******************************************************************************
118  * NFC driver public types
119  *****************************************************************************/
120 
121 struct nfc_nxp_t;
122 
123 /**
124  * @brief The callback is called on interrupt from pn512
125  * @param Nfc NFC driver descriptor
126  */
127 typedef void (*nfc_irq_callback_t)(const struct nfc_nxp_t* Nfc);
128 //bool NfcNxpLibInit(const struct nfc_nxp_t* Nfc_p);
129 
130 
131 /**
132  * @brief NFC driver descriptor: static structure for configuration of driver instance.
133  */
134 struct nfc_nxp_t
135 {
136  const struct nfc_ndef_changed_cb_table_t* NdefChangedCbTable; //!< table of interrupt handlers for supported NFC message types
137  struct nfc_nxp_state_t* State; //!< Pointer to the NFC state
138 
139  const struct spi_t* Spi; //!< Pointer to the SPI interface instance
140  const struct spi_cs_t SpiCs; //!< SPI CS settings
141  uint32_t SpiBaudrate; //!< SPI speed
142 
143  const struct adc_channel_t *AdcIcc; //!< ADC channel used for ICC measurement
144 
145  nfc_irq_callback_t NfcIrqCb; //!< interrupt handler
146 
147  uint8_t IrqPin; //!< IRQn pin
148  uint8_t ResetPin; //!< RSTPDn pin
149  uint8_t DetectPin; //!< Card detect sense enable pin
150 
151  /* Parameters for L3 activation during Autocoll */
152  uint8_t sens_res_def[2]; //!< ATQ bytes - needed for anti-collision
153  uint8_t nfc_id1_def[3]; //!< user defined bytes of the UID (one is hardcoded) - needed for anti-collision
154  uint8_t sel_res_def; //!< SAK (ISO18092mT or ISO14443P4 Card) - needed for anti-collision
155  uint8_t nfc_id3_def; //!< NFC3 byte - required for anti-collision
156  uint8_t poll_res_def[18];
157  uint8_t aAtsRes_def[5]; //!< ATS Response send for RATS: |TL|T0|TA|TB|TC|
158  uint8_t GI_def[13]; //!< General information bytes to be sent with ATR */
159 
160 };
161 
162 /**
163  * @brief holds current NFC instance state variables.
164  */
166 {
167 
168  bool Enabled; //!< The chip is enabled
169 
170  phbalReg_Stub_DataParams_t sBalReader; //!< BAL component holder
171  phhalHw_Nfc_Ic_DataParams_t sHal_Nfc_Ic; //!< HAL component holder for Nfc Ic's
172  uint8_t bHalBufferTx[256]; //!< HAL TX buffer. Size 256 - Based on maximum FSL
173  uint8_t bHalBufferRx[256]; //!< HAL RX buffer. Size 256 - Based on maximum FSL
174  void* pHal; //!< HAL pointer
175  phpalI14443p3a_Sw_DataParams_t spalI14443p3a; //!< PAL ISO I14443-A component
176  phpalI14443p4a_Sw_DataParams_t spalI14443p4a; //!< PAL ISO I14443-4A component
177  phpalI14443p3b_Sw_DataParams_t spalI14443p3b; //!< PAL ISO I14443-B component
178  phpalI14443p4_Sw_DataParams_t spalI14443p4; //!< PAL ISO I14443-4 component
179  phpalI14443p4mC_Sw_DataParams_t sPalI14443p4mC;
180  phpalFelica_Sw_DataParams_t spalFelica; //!< PAL Felica component
181  phpalI18092mPI_Sw_DataParams_t spalI18092mPI; //!< PAL ISO 18092 Initiator component
182  phpalMifare_Sw_DataParams_t spalMifare; //!< PAL Mifare component
183  phacDiscLoop_Sw_DataParams_t sDiscLoop; //!< Discovery loop component
184  phalT1T_Sw_DataParams_t alT1T; //!< AL T1T component
185  phceT4T_Sw_DataParams_t sCeT4T;
186  uint8_t aData[50]; //!< ATR response holder
187  uint8_t aAppBuffer[256];
188  uint8_t aCcFile[23]; //!< Mandatory Capability Container (CC) file. It's set using phceT4T_SetElementaryFile.
189  uint8_t* aNdefFile; //!< Mandatory NDEF file. It's set using phceT4T_SetElementaryFile.
190  uint16_t aNdefFileSize; //!< Mandatory NDEF file size.
191  uint8_t aAtsRes[5]; // ATS Response send for RATS: |TL|T0|TA|TB|TC|
192  uint16_t wAtsLength; // ATS length
193  uint8_t sens_res[2]; //!< ATQ bytes - needed for anti-collision
194  uint8_t nfc_id1[3]; //!< user defined bytes of the UID (one is hardcoded) - needed for anti-collision
195  uint8_t sel_res; //!< SAK (ISO18092mT or ISO14443P4 Card) - needed for anti-collision
196  uint8_t nfc_id3; //!< NFC3 byte - required for anti-collision
197  uint8_t poll_res[18];
198  uint8_t GI[13]; //!< General information bytes to be sent with ATR */
199 
200  #ifdef NXPBUILD__PHCE_T4T_PROPRIETARY
201  uint8_t aProprietaryFile[100]; //!< Optional proprietary file. It's set using phceT4T_SetElementaryFile.
202  #endif // NXPBUILD__PHCE_T4T_PROPRIETARY
203 
204  /// Semaphores for synchronization between application thread and reader library thread.
205  phOsal_SemaphoreHandle_t appstart; //!< Used to synchronize start of one cycle of application processing
206  phOsal_SemaphoreHandle_t appexit; //!< Used to synchronize end of one cycle of application processing
207  #if ISO_10373_6_PICC_TEST_BENCH
208  phOsal_SemaphoreHandle_t timerwtx; //!< Used in PICC ISO 10373-6 test bench to create WTX
209  #endif // ISO_10373_6_PICC_TEST_BENCH
210 
211  EventGroupHandle_t gphOsal_EventGroup; // Global Status of ISR Event Register Status to Receive Events from ISR
212  volatile phOsal_EventSource_t gphOsal_EvtLastSource; // Source of Events
213  volatile phOsal_EventDestination_t gphOsal_EvtLastDest; // Destination of Events
214  volatile phOsal_EventDetail_t gphOsal_EvtDetail; // Event Detail
215 
216 };
217 
218 /******************************************************************************
219  * NFC driver globals
220  *****************************************************************************/
221 extern const struct nfc_nxp_t* NfcNxp_ptr;
222 
223 /******************************************************************************
224  * NFC driver public functions
225  *****************************************************************************/
226 
227 /**
228  * @brief Initialises NFC instance structure, connects it to SPI.\n
229  * Powers up pn512 if supported
230  * @param Nfc NFC driver descriptor
231  */
232 bool NfcNxpLibInit(const struct nfc_nxp_t* Nfc);
233 
234 /**
235  * @brief Disables interrupts from pn512 and releases SPI.\n
236  * Powers down pn512 if supported by product HW.
237  * @param Nfc NFC driver descriptor
238  */
239 bool NfcNxpLibDisable(const struct nfc_nxp_t* Nfc);
240 
241 /**
242  * @brief Performs pn512 HW reset. pn512 switches to configuration mode after reset.\n
243  * NB! It should be verified that pn512 is in configuration mode with
244  * NfcNxpGetMode() function after reset before any subsequent operation.
245  * @param Nfc NFC driver descriptor
246  */
247 void NfcNxpResetReader(const struct nfc_nxp_t* Nfc);
248 
249 /**
250  * @brief Sets up pn512 registers according to preconfigured parameters.\n
251  * NB! pn512 should be in configuration mode.
252  * @param Nfc NFC driver descriptor
253  */
254 bool NfcNxpSetup(const struct nfc_nxp_t* Nfc);
255 
256 /**
257  * @brief Starts cards detection phase.
258  * During execution NFC lib is in the reader mode.\n
259  * NFC lib loops through all the available (configured) RF technologies.
260  * @param Nfc NFC driver descriptor
261  * @param Continue true: start technologies discovery loop and discover cards in the first available technology.
262  * false: discover cards in the next available technology.
263  * @return uint8_t Number of cards detected for the first available technology (Continue==false),
264  * or the next available technology ((Continue==true).
265  * Return value of 0 means there is no next technology.
266  */
267 uint8_t NfcNxpDiscovery(const struct nfc_nxp_t* Nfc, bool Continue);
268 
269 /**
270 * @brief Returns UID of one of detected tags
271 * \param [in] Nfc NFC driver descriptor
272 * \param [in] TagIndex Index of the card in the array of detected cards to read.
273 * \param [out] Uid UID of the requested card
274 * \param [out] UidLen Length of the UID returned
275 */
276 void NfcNxpGetUid(const struct nfc_nxp_t* Nfc, uint8_t TagIndex, uint8_t** Uid, uint8_t* UidLen);
277 
278 /**
279  * @brief Sets a memory buffer as an NDEF file for subsequent
280  * NfcNrfCardEmulation function call.
281  * @param Nfc NFC driver descriptor
282  * @param NdefFile Pointer to the buffer to set.
283  * @param NdefFileSize Full buffer length (not current data length!)
284  */
285 void NfcNxpSetNdefFile(const struct nfc_nxp_t* Nfc,
286  uint8_t* NdefFile, uint16_t NdefFileSize);
287 
288 /**
289  * @brief Starts external RF detection phase
290  * During execution NFC lib is in the card emulation mode.\n
291  * @param Nfc NFC driver descriptor
292  * param TypeName NDEF record type name - NULL terminated ASCII string
293  * param Data NDEF record data
294  * param DataLen NDEF record data length
295  */
296 void NfcNxpCardEmulation(const struct nfc_nxp_t* Nfc/*, const char* TypeName,
297  uint8_t* Data, uint32_t DataLen*/);
298 
299 /**
300  * @brief Driver's HW interrupt callback to be exposed to EVE config
301  * @param Nfc Pointer to NFC instance structure
302  */
303 void NfcNxpInterruptCb(const struct nfc_nxp_t* Nfc);
304 
305 
306 /** \} pn512 */
307 
308 
309 #endif // DRIVER_NFC_NXP_H
const struct spi_cs_t SpiCs
SPI CS settings.
Definition: nfc-nxp.h:140
phpalI14443p3b_Sw_DataParams_t spalI14443p3b
PAL ISO I14443-B component.
Definition: nfc-nxp.h:177
uint8_t sel_res_def
SAK (ISO18092mT or ISO14443P4 Card) - needed for anti-collision.
Definition: nfc-nxp.h:154
phpalFelica_Sw_DataParams_t spalFelica
PAL Felica component.
Definition: nfc-nxp.h:180
void NfcNxpGetUid(const struct nfc_nxp_t *Nfc, uint8_t TagIndex, uint8_t **Uid, uint8_t *UidLen)
Returns UID of one of detected tags.
void NfcNxpInterruptCb(const struct nfc_nxp_t *Nfc)
Driver&#39;s HW interrupt callback to be exposed to EVE config.
uint8_t sel_res
SAK (ISO18092mT or ISO14443P4 Card) - needed for anti-collision.
Definition: nfc-nxp.h:195
void * pHal
HAL pointer.
Definition: nfc-nxp.h:174
uint8_t NfcNxpDiscovery(const struct nfc_nxp_t *Nfc, bool Continue)
Starts cards detection phase. During execution NFC lib is in the reader mode. NFC lib loops through a...
const struct adc_channel_t * AdcIcc
ADC channel used for ICC measurement.
Definition: nfc-nxp.h:143
struct EventGroup_t * EventGroupHandle_t
Definition: rtos.h:582
phhalHw_Nfc_Ic_DataParams_t sHal_Nfc_Ic
HAL component holder for Nfc Ic&#39;s.
Definition: nfc-nxp.h:171
uint16_t aNdefFileSize
Mandatory NDEF file size.
Definition: nfc-nxp.h:190
uint8_t IrqPin
IRQn pin.
Definition: nfc-nxp.h:147
phpalI14443p3a_Sw_DataParams_t spalI14443p3a
PAL ISO I14443-A component.
Definition: nfc-nxp.h:175
phpalI18092mPI_Sw_DataParams_t spalI18092mPI
PAL ISO 18092 Initiator component.
Definition: nfc-nxp.h:181
uint8_t aAtsRes_def[5]
ATS Response send for RATS: |TL|T0|TA|TB|TC|.
Definition: nfc-nxp.h:157
Header file for the rtos emulation layer.
Definition: spi.h:113
const struct nfc_ndef_changed_cb_table_t * NdefChangedCbTable
table of interrupt handlers for supported NFC message types
Definition: nfc-nxp.h:136
NFC driver descriptor: static structure for configuration of driver instance.
Definition: nfc-nxp.h:134
Driver for the MCU&#39;s SPI blocks.
phpalI14443p4_Sw_DataParams_t spalI14443p4
PAL ISO I14443-4 component.
Definition: nfc-nxp.h:178
nfc_irq_callback_t NfcIrqCb
interrupt handler
Definition: nfc-nxp.h:145
bool NfcNxpLibInit(const struct nfc_nxp_t *Nfc)
Initialises NFC instance structure, connects it to SPI. Powers up pn512 if supported.
uint8_t nfc_id3
NFC3 byte - required for anti-collision.
Definition: nfc-nxp.h:196
struct nfc_nxp_state_t * State
Pointer to the NFC state.
Definition: nfc-nxp.h:137
void NfcNxpSetNdefFile(const struct nfc_nxp_t *Nfc, uint8_t *NdefFile, uint16_t NdefFileSize)
Sets a memory buffer as an NDEF file for subsequent NfcNrfCardEmulation function call.
void NfcNxpResetReader(const struct nfc_nxp_t *Nfc)
Performs pn512 HW reset. pn512 switches to configuration mode after reset. NB! It should be verified ...
phalT1T_Sw_DataParams_t alT1T
AL T1T component.
Definition: nfc-nxp.h:184
Table element of interrupt handlers called on NDEF write attempt for supported NFC message types...
Definition: nfc-ndef.h:58
uint8_t nfc_id3_def
NFC3 byte - required for anti-collision.
Definition: nfc-nxp.h:155
phbalReg_Stub_DataParams_t sBalReader
BAL component holder.
Definition: nfc-nxp.h:170
phacDiscLoop_Sw_DataParams_t sDiscLoop
Discovery loop component.
Definition: nfc-nxp.h:183
bool NfcNxpSetup(const struct nfc_nxp_t *Nfc)
Sets up pn512 registers according to preconfigured parameters. NB! pn512 should be in configuration m...
uint8_t GI_def[13]
General information bytes to be sent with ATR */.
Definition: nfc-nxp.h:158
phpalI14443p4a_Sw_DataParams_t spalI14443p4a
PAL ISO I14443-4A component.
Definition: nfc-nxp.h:176
phOsal_SemaphoreHandle_t appstart
Semaphores for synchronization between application thread and reader library thread.
Definition: nfc-nxp.h:205
phpalMifare_Sw_DataParams_t spalMifare
PAL Mifare component.
Definition: nfc-nxp.h:182
phOsal_SemaphoreHandle_t appexit
Used to synchronize end of one cycle of application processing.
Definition: nfc-nxp.h:206
void NfcNxpCardEmulation(const struct nfc_nxp_t *Nfc)
Starts external RF detection phase During execution NFC lib is in the card emulation mode...
uint32_t SpiBaudrate
SPI speed.
Definition: nfc-nxp.h:141
uint8_t nfc_id1_def[3]
user defined bytes of the UID (one is hardcoded) - needed for anti-collision
Definition: nfc-nxp.h:153
uint8_t * aNdefFile
Mandatory NDEF file. It&#39;s set using phceT4T_SetElementaryFile.
Definition: nfc-nxp.h:189
bool NfcNxpLibDisable(const struct nfc_nxp_t *Nfc)
Disables interrupts from pn512 and releases SPI. Powers down pn512 if supported by product HW...
bool Enabled
The chip is enabled.
Definition: nfc-nxp.h:168
const struct spi_t * Spi
Pointer to the SPI interface instance.
Definition: nfc-nxp.h:139
holds current NFC instance state variables.
Definition: nfc-nxp.h:165
uint8_t sens_res_def[2]
ATQ bytes - needed for anti-collision.
Definition: nfc-nxp.h:152
void(* nfc_irq_callback_t)(const struct nfc_nxp_t *Nfc)
The callback is called on interrupt from pn512.
Definition: nfc-nxp.h:127
Definition: spi.h:166
uint8_t ResetPin
RSTPDn pin.
Definition: nfc-nxp.h:148
uint8_t DetectPin
Card detect sense enable pin.
Definition: nfc-nxp.h:149