rfal_nfcb.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 \
  77. 13U /*!< Extended length of SENSB_RES with SFGI byte */
  78. #define RFAL_NFCB_SENSB_REQ_ADV_FEATURE \
  79. 0x20U /*!< Bit mask for Advance Feature in SENSB_REQ */
  80. #define RFAL_NFCB_SENSB_RES_FSCI_MASK \
  81. 0x0FU /*!< Bit mask for FSCI value in SENSB_RES */
  82. #define RFAL_NFCB_SENSB_RES_FSCI_SHIFT \
  83. 4U /*!< Shift for FSCI value in SENSB_RES */
  84. #define RFAL_NFCB_SENSB_RES_PROTO_RFU_MASK \
  85. 0x08U /*!< Bit mask for Protocol Type RFU in SENSB_RES */
  86. #define RFAL_NFCB_SENSB_RES_PROTO_TR2_MASK \
  87. 0x03U /*!< Bit mask for Protocol Type TR2 in SENSB_RES */
  88. #define RFAL_NFCB_SENSB_RES_PROTO_TR2_SHIFT \
  89. 1U /*!< Shift for Protocol Type TR2 in SENSB_RES */
  90. #define RFAL_NFCB_SENSB_RES_PROTO_ISO_MASK \
  91. 0x01U /*!< Bit mask Protocol Type ISO14443 Compliant in SENSB_RES */
  92. #define RFAL_NFCB_SENSB_RES_FWI_MASK \
  93. 0x0FU /*!< Bit mask for FWI value in SENSB_RES */
  94. #define RFAL_NFCB_SENSB_RES_FWI_SHIFT \
  95. 4U /*!< Bit mask for FWI value in SENSB_RES */
  96. #define RFAL_NFCB_SENSB_RES_ADC_MASK \
  97. 0x0CU /*!< Bit mask for ADC value in SENSB_RES */
  98. #define RFAL_NFCB_SENSB_RES_ADC_ADV_FEATURE_MASK \
  99. 0x08U /*!< Bit mask for ADC.Advanced Proto Features in SENSB_RES */
  100. #define RFAL_NFCB_SENSB_RES_ADC_PROPRIETARY_MASK \
  101. 0x04U /*!< Bit mask for ADC.Proprietary Application in SENSB_RES */
  102. #define RFAL_NFCB_SENSB_RES_FO_DID_MASK \
  103. 0x01U /*!< Bit mask for DID in SENSB_RES */
  104. #define RFAL_NFCB_SENSB_RES_FO_NAD_MASK \
  105. 0x02U /*!< Bit mask for DID in SENSB_RES */
  106. #define RFAL_NFCB_SENSB_RES_FO_MASK \
  107. 0x03U /*!< Bit mask for FO value in SENSB_RES (NAD and DID) */
  108. #define RFAL_NFCB_SENSB_RES_SFGI_MASK \
  109. 0x0FU /*!< Bit mask for SFGI in SENSB_RES */
  110. #define RFAL_NFCB_SENSB_RES_SFGI_SHIFT \
  111. 4U /*!< Shift for SFGI in SENSB_RES */
  112. /*
  113. ******************************************************************************
  114. * GLOBAL MACROS
  115. ******************************************************************************
  116. */
  117. /*! Get device's FSCI given its SENSB_RES Digital 1.1 7.6.2 */
  118. #define rfalNfcbGetFSCI(sensbRes) \
  119. ((((rfalNfcbSensbRes*)(sensbRes))->protInfo.FsciProType >> RFAL_NFCB_SENSB_RES_FSCI_SHIFT) & \
  120. RFAL_NFCB_SENSB_RES_FSCI_MASK)
  121. /*! Checks if the given NFC-B device indicates ISO-DEP support */
  122. #define rfalNfcbIsIsoDepSupported(dev) \
  123. ((((rfalNfcbListenDevice*)(dev))->sensbRes.protInfo.FsciProType & \
  124. RFAL_NFCB_SENSB_RES_PROTO_ISO_MASK) != 0U)
  125. /*
  126. ******************************************************************************
  127. * GLOBAL TYPES
  128. ******************************************************************************
  129. */
  130. /*! SENSB_REQ and ALLB_REQ param Digital 1.1 7.6.1 */
  131. typedef enum {
  132. RFAL_NFCB_SENS_CMD_ALLB_REQ = 0x08, /*!< ALLB_REQ (WUPB) */
  133. RFAL_NFCB_SENS_CMD_SENSB_REQ = 0x00 /*!< SENSB_REQ (REQB) */
  134. } rfalNfcbSensCmd;
  135. /*! Number of Slots (NI) codes used for NFC-B anti collision Digital 1.1 Table 26 */
  136. typedef enum {
  137. RFAL_NFCB_SLOT_NUM_1 = 0, /*!< N=0 : 1 slot */
  138. RFAL_NFCB_SLOT_NUM_2 = 1, /*!< N=1 : 2 slots */
  139. RFAL_NFCB_SLOT_NUM_4 = 2, /*!< N=2 : 4 slots */
  140. RFAL_NFCB_SLOT_NUM_8 = 3, /*!< N=3 : 8 slots */
  141. RFAL_NFCB_SLOT_NUM_16 = 4 /*!< N=4 : 16 slots */
  142. } rfalNfcbSlots;
  143. /*! SENSB_RES (ATQB) Application Data Format Digital 1.1 Table 28 */
  144. typedef struct {
  145. uint8_t AFI; /*!< Application Family Identifier */
  146. uint8_t CRC_B[RFAL_NFCB_CRC_LEN]; /*!< CRC_B of AID */
  147. uint8_t numApps; /*!< Number of Applications */
  148. } rfalNfcbSensbResAppData;
  149. /*! SENSB_RES Protocol Info format Digital 1.1 Table 29 */
  150. typedef struct {
  151. uint8_t
  152. BRC; /*!< Bit Rate Capability */
  153. uint8_t
  154. FsciProType; /*!< Frame Size Card Integer [4b] | Protocol Type[4 bits] */
  155. uint8_t
  156. FwiAdcFo; /*!< Frame Waiting Integer [4b] | Application Data Coding [2b] | Frame Options [2b] */
  157. uint8_t
  158. SFGI; /*!< Optional: Start-Up Frame Guard Time Integer[4b] | RFU [4b] */
  159. } rfalNfcbSensbResProtocolInfo;
  160. /*! SENSB_RES format Digital 1.1 7.6.2 */
  161. typedef struct {
  162. uint8_t cmd; /*!< SENSB_RES: 50h */
  163. uint8_t nfcid0[RFAL_NFCB_NFCID0_LEN]; /*!< NFC Identifier (PUPI)*/
  164. rfalNfcbSensbResAppData appData; /*!< Application Data */
  165. rfalNfcbSensbResProtocolInfo protInfo; /*!< Protocol Information */
  166. } rfalNfcbSensbRes;
  167. /*! NFC-B listener device (PICC) struct */
  168. typedef struct {
  169. uint8_t sensbResLen; /*!< SENSB_RES length */
  170. rfalNfcbSensbRes sensbRes; /*!< SENSB_RES */
  171. bool isSleep; /*!< Device sleeping flag */
  172. } rfalNfcbListenDevice;
  173. /*
  174. ******************************************************************************
  175. * GLOBAL FUNCTION PROTOTYPES
  176. ******************************************************************************
  177. */
  178. /*!
  179. *****************************************************************************
  180. * \brief Initialize NFC-B Poller mode
  181. *
  182. * This methods configures RFAL RF layer to perform as a
  183. * NFC-B Poller/RW (ISO14443B PCD) including all default timings
  184. *
  185. * It sets NFC-B parameters (AFI, PARAM) to default values
  186. *
  187. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  188. * \return ERR_NONE : No error
  189. *****************************************************************************
  190. */
  191. ReturnCode rfalNfcbPollerInitialize(void);
  192. /*!
  193. *****************************************************************************
  194. * \brief Set NFC-B Poller parameters
  195. *
  196. * This methods configures RFAL RF layer to perform as a
  197. * NFCA Poller/RW (ISO14443A PCD) including all default timings
  198. *
  199. * Additionally configures NFC-B specific parameters to be used on the
  200. * following communications
  201. *
  202. * \param[in] AFI : Application Family Identifier to be used
  203. * \param[in] PARAM : PARAM to be used, it announces whether Advanced
  204. * Features or Extended SENSB_RES is supported
  205. *
  206. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  207. * \return ERR_NONE : No error
  208. *****************************************************************************
  209. */
  210. ReturnCode rfalNfcbPollerInitializeWithParams(uint8_t AFI, uint8_t PARAM);
  211. /*!
  212. *****************************************************************************
  213. * \brief NFC-B Poller Check Presence
  214. *
  215. * This method checks if a NFC-B Listen device (PICC) is present on the field
  216. * by sending an ALLB_REQ (WUPB) or SENSB_REQ (REQB)
  217. *
  218. * \param[in] cmd : Indicate if to send an ALL_REQ or a SENS_REQ
  219. * \param[in] slots : The number of slots to be announced
  220. * \param[out] sensbRes : If received, the SENSB_RES
  221. * \param[out] sensbResLen : If received, the SENSB_RES length
  222. *
  223. *
  224. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  225. * \return ERR_PARAM : Invalid parameters
  226. * \return ERR_IO : Generic internal error
  227. * \return ERR_TIMEOUT : Timeout error, no listener device detected
  228. * \return ERR_RF_COLLISION : Collision detected one or more device in the field
  229. * \return ERR_PAR : Parity error detected, one or more device in the field
  230. * \return ERR_CRC : CRC error detected, one or more device in the field
  231. * \return ERR_FRAMING : Framing error detected, one or more device in the field
  232. * \return ERR_PROTO : Protocol error detected, invalid SENSB_RES received
  233. * \return ERR_NONE : No error, SENSB_RES received
  234. *****************************************************************************
  235. */
  236. ReturnCode rfalNfcbPollerCheckPresence(
  237. rfalNfcbSensCmd cmd,
  238. rfalNfcbSlots slots,
  239. rfalNfcbSensbRes* sensbRes,
  240. uint8_t* sensbResLen);
  241. /*!
  242. *****************************************************************************
  243. * \brief NFC-B Poller Sleep
  244. *
  245. * This function is used to send the SLPB_REQ (HLTB) command to put the PICC with
  246. * the given NFCID0 to state HALT so that they do not reply to further SENSB_REQ
  247. * commands (only to ALLB_REQ)
  248. *
  249. * \param[in] nfcid0 : NFCID of the device to be put to Sleep
  250. *
  251. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  252. * \return ERR_PARAM : Invalid parameters
  253. * \return ERR_IO : Generic internal error
  254. * \return ERR_NONE : No error
  255. *****************************************************************************
  256. */
  257. ReturnCode rfalNfcbPollerSleep(const uint8_t* nfcid0);
  258. /*!
  259. *****************************************************************************
  260. * \brief NFC-B Poller Slot Marker
  261. *
  262. * This method selects a NFC-B Slot marker frame
  263. *
  264. * \param[in] slotCode : Slot Code [1-15]
  265. * \param[out] sensbRes : If received, the SENSB_RES
  266. * \param[out] sensbResLen : If received, the SENSB_RES length
  267. *
  268. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  269. * \return ERR_PARAM : Invalid parameters
  270. * \return ERR_IO : Generic internal error
  271. * \return ERR_TIMEOUT : Timeout error
  272. * \return ERR_PAR : Parity error detected
  273. * \return ERR_CRC : CRC error detected
  274. * \return ERR_FRAMING : Framing error detected
  275. * \return ERR_PROTO : Protocol error detected
  276. * \return ERR_NONE : No error, SEL_RES received
  277. *****************************************************************************
  278. */
  279. ReturnCode
  280. rfalNfcbPollerSlotMarker(uint8_t slotCode, rfalNfcbSensbRes* sensbRes, uint8_t* sensbResLen);
  281. /*!
  282. *****************************************************************************
  283. * \brief NFC-B Technology Detection
  284. *
  285. * This method performs NFC-B Technology Detection as defined in the spec
  286. * given in the compliance mode
  287. *
  288. * \param[in] compMode : compliance mode to be performed
  289. * \param[out] sensbRes : location to store the SENSB_RES, if received
  290. * \param[out] sensbResLen : length of the SENSB_RES, if received
  291. *
  292. * \return ERR_WRONG_STATE : RFAL not initialized or incorrect mode
  293. * \return ERR_PARAM : Invalid parameters
  294. * \return ERR_IO : Generic internal error
  295. * \return ERR_NONE : No error, one or more device in the field
  296. *****************************************************************************
  297. */
  298. ReturnCode rfalNfcbPollerTechnologyDetection(
  299. rfalComplianceMode compMode,
  300. rfalNfcbSensbRes* sensbRes,
  301. uint8_t* sensbResLen);
  302. /*!
  303. *****************************************************************************
  304. * \brief NFC-B Poller Collision Resolution
  305. *
  306. * NFC-B Collision resolution Listener device/card (PICC) as
  307. * defined in Activity 1.1 9.3.5
  308. *
  309. * This function is used to perform collision resolution for detection in case
  310. * of multiple NFC Forum Devices with Technology B detected.
  311. * Target with valid SENSB_RES will be stored in devInfo and nfcbDevCount incremented.
  312. *
  313. * \param[in] compMode : compliance mode to be performed
  314. * \param[in] devLimit : device limit value, and size nfcbDevList
  315. * \param[out] nfcbDevList : NFC-B listener device info
  316. * \param[out] devCnt : devices found counter
  317. *
  318. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  319. * \return ERR_PARAM : Invalid parameters
  320. * \return ERR_IO : Generic internal error
  321. * \return ERR_PROTO : Protocol error detected
  322. * \return ERR_NONE : No error
  323. *****************************************************************************
  324. */
  325. ReturnCode rfalNfcbPollerCollisionResolution(
  326. rfalComplianceMode compMode,
  327. uint8_t devLimit,
  328. rfalNfcbListenDevice* nfcbDevList,
  329. uint8_t* devCnt);
  330. /*!
  331. *****************************************************************************
  332. * \brief NFC-B Poller Collision Resolution Slotted
  333. *
  334. * NFC-B Collision resolution Listener device/card (PICC). The sequence can
  335. * be configured to be according to NFC Forum Activity 1.1 9.3.5, ISO10373
  336. * or EMVCo
  337. *
  338. * This function is used to perform collision resolution for detection in case
  339. * of multiple NFC Forum Devices with Technology B are detected.
  340. * Target with valid SENSB_RES will be stored in devInfo and nfcbDevCount incremented.
  341. *
  342. * This method provides the means to perform a collision resolution loop with specific
  343. * initial and end number of slots. This allows to user to start the loop already with
  344. * greater number of slots, and or limit the end number of slots. At the end a flag
  345. * indicating whether there were collisions pending is returned.
  346. *
  347. * If RFAL_COMPLIANCE_MODE_ISO is used \a initSlots must be set to RFAL_NFCB_SLOT_NUM_1
  348. *
  349. *
  350. * \param[in] compMode : compliance mode to be performed
  351. * \param[in] devLimit : device limit value, and size nfcbDevList
  352. * \param[in] initSlots : number of slots to open initially
  353. * \param[in] endSlots : number of slots when to stop collision resolution
  354. * \param[out] nfcbDevList : NFC-B listener device info
  355. * \param[out] devCnt : devices found counter
  356. * \param[out] colPending : flag indicating whether collision are still pending
  357. *
  358. * \return ERR_WRONG_STATE : RFAL not initialized or mode not set
  359. * \return ERR_PARAM : Invalid parameters
  360. * \return ERR_IO : Generic internal error
  361. * \return ERR_PROTO : Protocol error detected
  362. * \return ERR_NONE : No error
  363. *****************************************************************************
  364. */
  365. ReturnCode rfalNfcbPollerSlottedCollisionResolution(
  366. rfalComplianceMode compMode,
  367. uint8_t devLimit,
  368. rfalNfcbSlots initSlots,
  369. rfalNfcbSlots endSlots,
  370. rfalNfcbListenDevice* nfcbDevList,
  371. uint8_t* devCnt,
  372. bool* colPending);
  373. /*!
  374. *****************************************************************************
  375. * \brief NFC-B TR2 code to FDT
  376. *
  377. * Converts the TR2 code as defined in Digital 1.1 Table 33 Minimum
  378. * TR2 Coding to Frame Delay Time (FDT) in 1/Fc
  379. *
  380. * \param[in] tr2Code : TR2 code as defined in Digital 1.1 Table 33
  381. *
  382. * \return FDT in 1/Fc
  383. *****************************************************************************
  384. */
  385. uint32_t rfalNfcbTR2ToFDT(uint8_t tr2Code);
  386. #endif /* RFAL_NFCB_H */
  387. /**
  388. * @}
  389. *
  390. * @}
  391. *
  392. * @}
  393. */