rfal_nfcb.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /******************************************************************************
  2. * \attention
  3. *
  4. * <h2><center>&copy; COPYRIGHT 2020 STMicroelectronics</center></h2>
  5. *
  6. * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
  7. * You may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at:
  9. *
  10. * www.st.com/myliberty
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
  15. * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. ******************************************************************************/
  21. /*
  22. * PROJECT: ST25R391x firmware
  23. * Revision:
  24. * LANGUAGE: ISO C99
  25. */
  26. /*! \file rfal_nfcb.h
  27. *
  28. * \author Gustavo Patricio
  29. *
  30. * \brief Implementation of NFC-B (ISO14443B) helpers
  31. *
  32. * It provides a NFC-B Poller (ISO14443B PCD) interface and
  33. * also provides some NFC-B Listener (ISO14443B PICC) helpers
  34. *
  35. * The definitions and helpers methods provided by this module are only
  36. * up to ISO14443-3 layer (excluding ATTRIB)
  37. *
  38. *
  39. * \addtogroup RFAL
  40. * @{
  41. *
  42. * \addtogroup RFAL-AL
  43. * \brief RFAL Abstraction Layer
  44. * @{
  45. *
  46. * \addtogroup NFC-B
  47. * \brief RFAL NFC-B Module
  48. * @{
  49. *
  50. */
  51. #ifndef RFAL_NFCB_H
  52. #define RFAL_NFCB_H
  53. /*
  54. ******************************************************************************
  55. * INCLUDES
  56. ******************************************************************************
  57. */
  58. #include "platform.h"
  59. #include "st_errno.h"
  60. #include "rfal_rf.h"
  61. /*
  62. ******************************************************************************
  63. * GLOBAL DEFINES
  64. ******************************************************************************
  65. */
  66. #define RFAL_NFCB_FWTSENSB 7680U /*!< NFC-B FWT(SENSB) Digital 2.0 B.3 */
  67. #define RFAL_NFCB_DFWT 49152U /*!< NFC-B dFWT Delta 2.0 7.9.1.3 & B.3 */
  68. #define RFAL_NFCB_DTPOLL_10 rfalConvMsTo1fc(20) /*!< NFC-B Delta Tb Poll Digital 1.0 A.2 */
  69. #define RFAL_NFCB_DTPOLL_20 rfalConvMsTo1fc(17) /*!< NFC-B Delta Tb Poll Digital 2.1 B.3 */
  70. #define RFAL_NFCB_AFI 0x00U /*!< NFC-B default Application Family Digital 1.1 7.6.1.1 */
  71. #define RFAL_NFCB_PARAM 0x00U /*!< NFC-B default SENSB_REQ PARAM */
  72. #define RFAL_NFCB_CRC_LEN 2U /*!< NFC-B CRC length and CRC_B(AID) Digital 1.1 Table 28 */
  73. #define RFAL_NFCB_NFCID0_LEN 4U /*!< Length of NFC-B NFCID0 */
  74. #define RFAL_NFCB_CMD_LEN 1U /*!< Length of NFC-B Command */
  75. #define RFAL_NFCB_SENSB_RES_LEN 12U /*!< Standard length of SENSB_RES without SFGI byte */
  76. #define RFAL_NFCB_SENSB_RES_EXT_LEN 13U /*!< Extended length of SENSB_RES with SFGI byte */
  77. #define RFAL_NFCB_SENSB_REQ_ADV_FEATURE 0x20U /*!< Bit mask for Advance Feature in SENSB_REQ */
  78. #define RFAL_NFCB_SENSB_RES_FSCI_MASK 0x0FU /*!< Bit mask for FSCI value in SENSB_RES */
  79. #define RFAL_NFCB_SENSB_RES_FSCI_SHIFT 4U /*!< Shift for FSCI value in SENSB_RES */
  80. #define RFAL_NFCB_SENSB_RES_PROTO_RFU_MASK 0x08U /*!< Bit mask for Protocol Type RFU in SENSB_RES */
  81. #define RFAL_NFCB_SENSB_RES_PROTO_TR2_MASK 0x03U /*!< Bit mask for Protocol Type TR2 in SENSB_RES */
  82. #define RFAL_NFCB_SENSB_RES_PROTO_TR2_SHIFT 1U /*!< Shift for Protocol Type TR2 in SENSB_RES */
  83. #define RFAL_NFCB_SENSB_RES_PROTO_ISO_MASK 0x01U /*!< Bit mask Protocol Type ISO14443 Compliant in SENSB_RES */
  84. #define RFAL_NFCB_SENSB_RES_FWI_MASK 0x0FU /*!< Bit mask for FWI value in SENSB_RES */
  85. #define RFAL_NFCB_SENSB_RES_FWI_SHIFT 4U /*!< Bit mask for FWI value in SENSB_RES */
  86. #define RFAL_NFCB_SENSB_RES_ADC_MASK 0x0CU /*!< Bit mask for ADC value in SENSB_RES */
  87. #define RFAL_NFCB_SENSB_RES_ADC_ADV_FEATURE_MASK 0x08U /*!< Bit mask for ADC.Advanced Proto Features in SENSB_RES */
  88. #define RFAL_NFCB_SENSB_RES_ADC_PROPRIETARY_MASK 0x04U /*!< Bit mask for ADC.Proprietary Application in SENSB_RES */
  89. #define RFAL_NFCB_SENSB_RES_FO_DID_MASK 0x01U /*!< Bit mask for DID in SENSB_RES */
  90. #define RFAL_NFCB_SENSB_RES_FO_NAD_MASK 0x02U /*!< Bit mask for DID in SENSB_RES */
  91. #define RFAL_NFCB_SENSB_RES_FO_MASK 0x03U /*!< Bit mask for FO value in SENSB_RES (NAD and DID) */
  92. #define RFAL_NFCB_SENSB_RES_SFGI_MASK 0x0FU /*!< Bit mask for SFGI in SENSB_RES */
  93. #define RFAL_NFCB_SENSB_RES_SFGI_SHIFT 4U /*!< Shift for SFGI in SENSB_RES */
  94. /*
  95. ******************************************************************************
  96. * GLOBAL MACROS
  97. ******************************************************************************
  98. */
  99. /*! Get device's FSCI given its SENSB_RES Digital 1.1 7.6.2 */
  100. #define rfalNfcbGetFSCI( sensbRes ) ((((rfalNfcbSensbRes*)(sensbRes))->protInfo.FsciProType >> RFAL_NFCB_SENSB_RES_FSCI_SHIFT) & RFAL_NFCB_SENSB_RES_FSCI_MASK )
  101. /*! Checks if the given NFC-B device indicates ISO-DEP support */
  102. #define rfalNfcbIsIsoDepSupported( dev ) ( (((rfalNfcbListenDevice*)(dev))->sensbRes.protInfo.FsciProType & RFAL_NFCB_SENSB_RES_PROTO_ISO_MASK) != 0U )
  103. /*
  104. ******************************************************************************
  105. * GLOBAL TYPES
  106. ******************************************************************************
  107. */
  108. /*! SENSB_REQ and ALLB_REQ param Digital 1.1 7.6.1 */
  109. typedef enum
  110. {
  111. RFAL_NFCB_SENS_CMD_ALLB_REQ = 0x08, /*!< ALLB_REQ (WUPB) */
  112. RFAL_NFCB_SENS_CMD_SENSB_REQ = 0x00 /*!< SENSB_REQ (REQB) */
  113. } rfalNfcbSensCmd;
  114. /*! Number of Slots (NI) codes used for NFC-B anti collision Digital 1.1 Table 26 */
  115. typedef enum
  116. {
  117. RFAL_NFCB_SLOT_NUM_1 = 0, /*!< N=0 : 1 slot */
  118. RFAL_NFCB_SLOT_NUM_2 = 1, /*!< N=1 : 2 slots */
  119. RFAL_NFCB_SLOT_NUM_4 = 2, /*!< N=2 : 4 slots */
  120. RFAL_NFCB_SLOT_NUM_8 = 3, /*!< N=3 : 8 slots */
  121. RFAL_NFCB_SLOT_NUM_16 = 4 /*!< N=4 : 16 slots */
  122. }rfalNfcbSlots;
  123. /*! SENSB_RES (ATQB) Application Data Format Digital 1.1 Table 28 */
  124. typedef struct
  125. {
  126. uint8_t AFI; /*!< Application Family Identifier */
  127. uint8_t CRC_B[RFAL_NFCB_CRC_LEN]; /*!< CRC_B of AID */
  128. uint8_t numApps; /*!< Number of Applications */
  129. } rfalNfcbSensbResAppData;
  130. /*! SENSB_RES Protocol Info format Digital 1.1 Table 29 */
  131. typedef struct
  132. {
  133. uint8_t BRC; /*!< Bit Rate Capability */
  134. uint8_t FsciProType; /*!< Frame Size Card Integer [4b] | Protocol Type[4 bits] */
  135. uint8_t FwiAdcFo; /*!< Frame Waiting Integer [4b] | Application Data Coding [2b] | Frame Options [2b] */
  136. uint8_t SFGI; /*!< Optional: Start-Up Frame Guard Time Integer[4b] | RFU [4b] */
  137. } rfalNfcbSensbResProtocolInfo;
  138. /*! SENSB_RES format Digital 1.1 7.6.2 */
  139. typedef struct
  140. {
  141. uint8_t cmd; /*!< SENSB_RES: 50h */
  142. uint8_t nfcid0[RFAL_NFCB_NFCID0_LEN]; /*!< NFC Identifier (PUPI)*/
  143. rfalNfcbSensbResAppData appData; /*!< Application Data */
  144. rfalNfcbSensbResProtocolInfo protInfo; /*!< Protocol Information */
  145. } rfalNfcbSensbRes;
  146. /*! NFC-B listener device (PICC) struct */
  147. typedef struct
  148. {
  149. uint8_t sensbResLen; /*!< SENSB_RES length */
  150. rfalNfcbSensbRes sensbRes; /*!< SENSB_RES */
  151. bool isSleep; /*!< Device sleeping flag */
  152. }rfalNfcbListenDevice;
  153. /*
  154. ******************************************************************************
  155. * GLOBAL FUNCTION PROTOTYPES
  156. ******************************************************************************
  157. */
  158. /*!
  159. *****************************************************************************
  160. * \brief Initialize NFC-B Poller mode
  161. *
  162. * This methods configures RFAL RF layer to perform as a
  163. * NFC-B Poller/RW (ISO14443B PCD) including all default timings
  164. *
  165. * It sets NFC-B parameters (AFI, PARAM) to default values
  166. *
  167. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  168. * \return ERR_NONE : No error
  169. *****************************************************************************
  170. */
  171. ReturnCode rfalNfcbPollerInitialize( void );
  172. /*!
  173. *****************************************************************************
  174. * \brief Set NFC-B Poller parameters
  175. *
  176. * This methods configures RFAL RF layer to perform as a
  177. * NFCA Poller/RW (ISO14443A PCD) including all default timings
  178. *
  179. * Additionally configures NFC-B specific parameters to be used on the
  180. * following communications
  181. *
  182. * \param[in] AFI : Application Family Identifier to be used
  183. * \param[in] PARAM : PARAM to be used, it announces whether Advanced
  184. * Features or Extended SENSB_RES is supported
  185. *
  186. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  187. * \return ERR_NONE : No error
  188. *****************************************************************************
  189. */
  190. ReturnCode rfalNfcbPollerInitializeWithParams( uint8_t AFI, uint8_t PARAM );
  191. /*!
  192. *****************************************************************************
  193. * \brief NFC-B Poller Check Presence
  194. *
  195. * This method checks if a NFC-B Listen device (PICC) is present on the field
  196. * by sending an ALLB_REQ (WUPB) or SENSB_REQ (REQB)
  197. *
  198. * \param[in] cmd : Indicate if to send an ALL_REQ or a SENS_REQ
  199. * \param[in] slots : The number of slots to be announced
  200. * \param[out] sensbRes : If received, the SENSB_RES
  201. * \param[out] sensbResLen : If received, the SENSB_RES length
  202. *
  203. *
  204. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  205. * \return ERR_PARAM : Invalid parameters
  206. * \return ERR_IO : Generic internal error
  207. * \return ERR_TIMEOUT : Timeout error, no listener device detected
  208. * \return ERR_RF_COLLISION : Collision detected one or more device in the field
  209. * \return ERR_PAR : Parity error detected, one or more device in the field
  210. * \return ERR_CRC : CRC error detected, one or more device in the field
  211. * \return ERR_FRAMING : Framing error detected, one or more device in the field
  212. * \return ERR_PROTO : Protocol error detected, invalid SENSB_RES received
  213. * \return ERR_NONE : No error, SENSB_RES received
  214. *****************************************************************************
  215. */
  216. ReturnCode rfalNfcbPollerCheckPresence( rfalNfcbSensCmd cmd, rfalNfcbSlots slots, rfalNfcbSensbRes *sensbRes, uint8_t *sensbResLen );
  217. /*!
  218. *****************************************************************************
  219. * \brief NFC-B Poller Sleep
  220. *
  221. * This function is used to send the SLPB_REQ (HLTB) command to put the PICC with
  222. * the given NFCID0 to state HALT so that they do not reply to further SENSB_REQ
  223. * commands (only to ALLB_REQ)
  224. *
  225. * \param[in] nfcid0 : NFCID of the device to be put to Sleep
  226. *
  227. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  228. * \return ERR_PARAM : Invalid parameters
  229. * \return ERR_IO : Generic internal error
  230. * \return ERR_NONE : No error
  231. *****************************************************************************
  232. */
  233. ReturnCode rfalNfcbPollerSleep( const uint8_t* nfcid0 );
  234. /*!
  235. *****************************************************************************
  236. * \brief NFC-B Poller Slot Marker
  237. *
  238. * This method selects a NFC-B Slot marker frame
  239. *
  240. * \param[in] slotCode : Slot Code [1-15]
  241. * \param[out] sensbRes : If received, the SENSB_RES
  242. * \param[out] sensbResLen : If received, the SENSB_RES length
  243. *
  244. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  245. * \return ERR_PARAM : Invalid parameters
  246. * \return ERR_IO : Generic internal error
  247. * \return ERR_TIMEOUT : Timeout error
  248. * \return ERR_PAR : Parity error detected
  249. * \return ERR_CRC : CRC error detected
  250. * \return ERR_FRAMING : Framing error detected
  251. * \return ERR_PROTO : Protocol error detected
  252. * \return ERR_NONE : No error, SEL_RES received
  253. *****************************************************************************
  254. */
  255. ReturnCode rfalNfcbPollerSlotMarker( uint8_t slotCode, rfalNfcbSensbRes *sensbRes, uint8_t *sensbResLen );
  256. /*!
  257. *****************************************************************************
  258. * \brief NFC-B Technology Detection
  259. *
  260. * This method performs NFC-B Technology Detection as defined in the spec
  261. * given in the compliance mode
  262. *
  263. * \param[in] compMode : compliance mode to be performed
  264. * \param[out] sensbRes : location to store the SENSB_RES, if received
  265. * \param[out] sensbResLen : length of the SENSB_RES, if received
  266. *
  267. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  268. * \return ERR_PARAM : Invalid parameters
  269. * \return ERR_IO : Generic internal error
  270. * \return ERR_NONE : No error, one or more device in the field
  271. *****************************************************************************
  272. */
  273. ReturnCode rfalNfcbPollerTechnologyDetection( rfalComplianceMode compMode, rfalNfcbSensbRes *sensbRes, uint8_t *sensbResLen );
  274. /*!
  275. *****************************************************************************
  276. * \brief NFC-B Poller Collision Resolution
  277. *
  278. * NFC-B Collision resolution Listener device/card (PICC) as
  279. * defined in Activity 1.1 9.3.5
  280. *
  281. * This function is used to perform collision resolution for detection in case
  282. * of multiple NFC Forum Devices with Technology B detected.
  283. * Target with valid SENSB_RES will be stored in devInfo and nfcbDevCount incremented.
  284. *
  285. * \param[in] compMode : compliance mode to be performed
  286. * \param[in] devLimit : device limit value, and size nfcbDevList
  287. * \param[out] nfcbDevList : NFC-B listener device info
  288. * \param[out] devCnt : devices found counter
  289. *
  290. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  291. * \return ERR_PARAM : Invalid parameters
  292. * \return ERR_IO : Generic internal error
  293. * \return ERR_PROTO : Protocol error detected
  294. * \return ERR_NONE : No error
  295. *****************************************************************************
  296. */
  297. ReturnCode rfalNfcbPollerCollisionResolution( rfalComplianceMode compMode, uint8_t devLimit, rfalNfcbListenDevice *nfcbDevList, uint8_t *devCnt );
  298. /*!
  299. *****************************************************************************
  300. * \brief NFC-B Poller Collision Resolution Slotted
  301. *
  302. * NFC-B Collision resolution Listener device/card (PICC). The sequence can
  303. * be configured to be according to NFC Forum Activity 1.1 9.3.5, ISO10373
  304. * or EMVCo
  305. *
  306. * This function is used to perform collision resolution for detection in case
  307. * of multiple NFC Forum Devices with Technology B are detected.
  308. * Target with valid SENSB_RES will be stored in devInfo and nfcbDevCount incremented.
  309. *
  310. * This method provides the means to perform a collision resolution loop with specific
  311. * initial and end number of slots. This allows to user to start the loop already with
  312. * greater number of slots, and or limit the end number of slots. At the end a flag
  313. * indicating whether there were collisions pending is returned.
  314. *
  315. * If RFAL_COMPLIANCE_MODE_ISO is used \a initSlots must be set to RFAL_NFCB_SLOT_NUM_1
  316. *
  317. *
  318. * \param[in] compMode : compliance mode to be performed
  319. * \param[in] devLimit : device limit value, and size nfcbDevList
  320. * \param[in] initSlots : number of slots to open initially
  321. * \param[in] endSlots : number of slots when to stop collision resolution
  322. * \param[out] nfcbDevList : NFC-B listener device info
  323. * \param[out] devCnt : devices found counter
  324. * \param[out] colPending : flag indicating whether collision are still pending
  325. *
  326. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  327. * \return ERR_PARAM : Invalid parameters
  328. * \return ERR_IO : Generic internal error
  329. * \return ERR_PROTO : Protocol error detected
  330. * \return ERR_NONE : No error
  331. *****************************************************************************
  332. */
  333. ReturnCode rfalNfcbPollerSlottedCollisionResolution( rfalComplianceMode compMode, uint8_t devLimit, rfalNfcbSlots initSlots, rfalNfcbSlots endSlots, rfalNfcbListenDevice *nfcbDevList, uint8_t *devCnt, bool *colPending );
  334. /*!
  335. *****************************************************************************
  336. * \brief NFC-B TR2 code to FDT
  337. *
  338. * Converts the TR2 code as defined in Digital 1.1 Table 33 Minimum
  339. * TR2 Coding to Frame Delay Time (FDT) in 1/Fc
  340. *
  341. * \param[in] tr2Code : TR2 code as defined in Digital 1.1 Table 33
  342. *
  343. * \return FDT in 1/Fc
  344. *****************************************************************************
  345. */
  346. uint32_t rfalNfcbTR2ToFDT( uint8_t tr2Code );
  347. #endif /* RFAL_NFCB_H */
  348. /**
  349. * @}
  350. *
  351. * @}
  352. *
  353. * @}
  354. */