nrfx_nfct.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /**
  2. * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRFX_NFCT_H__
  41. #define NRFX_NFCT_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_nfct.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_nfct NFCT driver
  49. * @{
  50. * @ingroup nrf_nfct
  51. * @brief Near Field Communication Tag (NFCT) peripheral driver.
  52. */
  53. #define NRFX_NFCT_NFCID1_SINGLE_SIZE 4u ///< Length of single-size NFCID1.
  54. #define NRFX_NFCT_NFCID1_DOUBLE_SIZE 7u ///< Length of double-size NFCID1.
  55. #define NRFX_NFCT_NFCID1_TRIPLE_SIZE 10u ///< Length of triple-size NFCID1.
  56. #define NRFX_NFCT_NFCID1_DEFAULT_LEN NRFX_NFCT_NFCID1_DOUBLE_SIZE ///< Default length of NFC ID. */
  57. /** @brief NFCT hardware states. */
  58. typedef enum
  59. {
  60. NRFX_NFCT_STATE_DISABLED = NRF_NFCT_TASK_DISABLE, ///< NFC Tag is disabled (no sensing of an external NFC field).
  61. NRFX_NFCT_STATE_SENSING = NRF_NFCT_TASK_SENSE, ///< NFC Tag is sensing whether there is an external NFC field.
  62. NRFX_NFCT_STATE_ACTIVATED = NRF_NFCT_TASK_ACTIVATE, ///< NFC Tag is powered-up (see @ref nrfx_nfct_active_state_t for possible substates).
  63. } nrfx_nfct_state_t;
  64. /**
  65. * @brief NFC tag states, when NFCT hardware is activated.
  66. *
  67. * @details These states are substates of the @ref NRFX_NFCT_STATE_ACTIVATED state.
  68. */
  69. typedef enum
  70. {
  71. NRFX_NFCT_ACTIVE_STATE_IDLE = NRF_NFCT_TASK_GOIDLE, ///< NFC Tag is activated and idle (not selected by a reader).
  72. NRFX_NFCT_ACTIVE_STATE_SLEEP = NRF_NFCT_TASK_GOSLEEP, ///< NFC Tag is sleeping.
  73. NRFX_NFCT_ACTIVE_STATE_DEFAULT, ///< NFC Tag is either sleeping or idle, depending on the previous state before being selected by a poller.
  74. } nrfx_nfct_active_state_t;
  75. /**
  76. * @brief NFCT driver event types, passed to the upper-layer callback function
  77. * provided during the initialization.
  78. */
  79. typedef enum
  80. {
  81. NRFX_NFCT_EVT_FIELD_DETECTED = NRF_NFCT_INT_FIELDDETECTED_MASK, ///< External NFC field is detected.
  82. NRFX_NFCT_EVT_FIELD_LOST = NRF_NFCT_INT_FIELDLOST_MASK, ///< External NFC Field is lost.
  83. NRFX_NFCT_EVT_SELECTED = NRF_NFCT_INT_SELECTED_MASK, ///< Tag was selected by the poller.
  84. NRFX_NFCT_EVT_RX_FRAMESTART = NRF_NFCT_INT_RXFRAMESTART_MASK, ///< Data frame reception started.
  85. NRFX_NFCT_EVT_RX_FRAMEEND = NRF_NFCT_INT_RXFRAMEEND_MASK, ///< Data frame is received.
  86. NRFX_NFCT_EVT_TX_FRAMESTART = NRF_NFCT_INT_TXFRAMESTART_MASK, ///< Data frame transmission started.
  87. NRFX_NFCT_EVT_TX_FRAMEEND = NRF_NFCT_INT_TXFRAMEEND_MASK, ///< Data frame is transmitted.
  88. NRFX_NFCT_EVT_ERROR = NRF_NFCT_INT_ERROR_MASK, ///< Error occurred in an NFC communication.
  89. } nrfx_nfct_evt_id_t;
  90. /** @brief NFCT timing-related error types. */
  91. typedef enum
  92. {
  93. NRFX_NFCT_ERROR_FRAMEDELAYTIMEOUT, ///< No response frame was transmitted to the poller in the transmit window.
  94. NRFX_NFCT_ERROR_NUM, ///< Total number of possible errors.
  95. } nrfx_nfct_error_t;
  96. /** @brief NFCT driver parameter types. */
  97. typedef enum
  98. {
  99. NRFX_NFCT_PARAM_ID_FDT, ///< NFC-A Frame Delay Time parameter.
  100. NRFX_NFCT_PARAM_ID_SEL_RES, ///< Value of the 'Protocol' field in the NFC-A SEL_RES frame.
  101. NRFX_NFCT_PARAM_ID_NFCID1, ///< NFC-A NFCID1 setting (NFC tag identifier).
  102. } nrfx_nfct_param_id_t;
  103. /** @brief NFCID1 descriptor. */
  104. typedef struct
  105. {
  106. uint8_t const * p_id; ///< NFCID1 data.
  107. uint8_t id_size; ///< NFCID1 size.
  108. } nrfx_nfct_nfcid1_t;
  109. /** @brief NFCT driver parameter descriptor. */
  110. typedef struct
  111. {
  112. nrfx_nfct_param_id_t id; ///< Type of parameter.
  113. union
  114. {
  115. uint32_t fdt; ///< NFC-A Frame Delay Time. Filled when nrfx_nfct_param_t::id is @ref NRFX_NFCT_PARAM_ID_FDT.
  116. uint8_t sel_res_protocol; ///< NFC-A value of the 'Protocol' field in the SEL_RES frame. Filled when nrfx_nfct_param_t::id is @ref NRFX_NFCT_PARAM_ID_SEL_RES.
  117. nrfx_nfct_nfcid1_t nfcid1; ///< NFC-A NFCID1 value (tag identifier). Filled when nrfx_nfct_param_t::id is @ref NRFX_NFCT_PARAM_ID_NFCID1.
  118. } data; ///< Union to store parameter data.
  119. } nrfx_nfct_param_t;
  120. /** @brief NFCT driver RX/TX buffer descriptor. */
  121. typedef struct
  122. {
  123. uint32_t data_size; ///< RX/TX buffer size.
  124. uint8_t const * p_data; ///< RX/TX buffer.
  125. } nrfx_nfct_data_desc_t;
  126. /** @brief Structure used to describe the @ref NRFX_NFCT_EVT_RX_FRAMEEND event type. */
  127. typedef struct
  128. {
  129. uint32_t rx_status; ///< RX error status.
  130. nrfx_nfct_data_desc_t rx_data; ///< RX buffer.
  131. } nrfx_nfct_evt_rx_frameend_t;
  132. /** @brief Structure used to describe the @ref NRFX_NFCT_EVT_TX_FRAMESTART event type. */
  133. typedef struct
  134. {
  135. nrfx_nfct_data_desc_t tx_data; ///< TX buffer.
  136. } nrfx_nfct_evt_tx_framestart_t;
  137. /** @brief Structure used to describe the @ref NRFX_NFCT_EVT_ERROR event type. */
  138. typedef struct
  139. {
  140. nrfx_nfct_error_t reason; ///< Reason for error.
  141. } nrfx_nfct_evt_error_t;
  142. /** @brief NFCT driver event. */
  143. typedef struct
  144. {
  145. nrfx_nfct_evt_id_t evt_id; ///< Type of event.
  146. union
  147. {
  148. nrfx_nfct_evt_rx_frameend_t rx_frameend; ///< End of the RX frame data. Filled when nrfx_nfct_evt_t::evt_id is @ref NRFX_NFCT_EVT_RX_FRAMEEND.
  149. nrfx_nfct_evt_tx_framestart_t tx_framestart; ///< Start of the TX frame data. Filled when nrfx_nfct_evt_t::evt_id is @ref NRFX_NFCT_EVT_TX_FRAMESTART.
  150. nrfx_nfct_evt_error_t error; ///< Error data. Filled when nrfx_nfct_evt_t::evt_id is @ref NRFX_NFCT_EVT_ERROR.
  151. } params; ///< Union to store event data.
  152. } nrfx_nfct_evt_t;
  153. /**
  154. * @brief Callback descriptor to pass events from the NFCT driver to the upper layer.
  155. *
  156. * @param[in] p_event Pointer to the event descriptor.
  157. *
  158. * @note @ref NRFX_NFCT_EVT_FIELD_DETECTED and @ref NRFX_NFCT_EVT_FIELD_LOST are generated only on field state transitions,
  159. * i.e. there will be no multiple events of the same type (out of the 2 mentioned) coming in a row.
  160. */
  161. typedef void (*nrfx_nfct_handler_t)(nrfx_nfct_evt_t const * p_event);
  162. /** @brief NFCT driver configuration structure. */
  163. typedef struct
  164. {
  165. uint32_t rxtx_int_mask; ///< Mask for enabling RX/TX events. Indicate which events must be forwarded to the upper layer by using @ref nrfx_nfct_evt_id_t. By default, no events are enabled. */
  166. nrfx_nfct_handler_t cb; ///< Callback.
  167. } nrfx_nfct_config_t;
  168. /**
  169. * @brief Function for initializing the NFCT driver.
  170. *
  171. * @param[in] p_config Pointer to the NFCT driver configuration structure.
  172. *
  173. * @retval NRFX_SUCCESS The NFCT driver was initialized successfully.
  174. * @retval NRFX_ERROR_INVALID_STATE The NFCT driver is already initialized.
  175. */
  176. nrfx_err_t nrfx_nfct_init(nrfx_nfct_config_t const * p_config);
  177. /**
  178. * @brief Function for uninitializing the NFCT driver.
  179. *
  180. * After uninitialization, the instance is in disabled state.
  181. */
  182. void nrfx_nfct_uninit(void);
  183. /**
  184. * @brief Function for starting the NFC subsystem.
  185. *
  186. * After this function completes, NFC readers are able to detect the tag.
  187. */
  188. void nrfx_nfct_enable(void);
  189. /**
  190. * @brief Function for disabling the NFCT driver.
  191. *
  192. * After this function returns, NFC readers are no longer able to connect
  193. * to the tag.
  194. */
  195. void nrfx_nfct_disable(void);
  196. /**
  197. * @brief Function for checking whether the external NFC field is present in the range of the tag.
  198. *
  199. * @retval true The NFC field is present.
  200. * @retval false No NFC field is present.
  201. */
  202. bool nrfx_nfct_field_check(void);
  203. /**
  204. * @brief Function for preparing the NFCT driver for receiving an NFC frame.
  205. *
  206. * @param[in] p_rx_data Pointer to the RX buffer.
  207. */
  208. void nrfx_nfct_rx(nrfx_nfct_data_desc_t const * p_rx_data);
  209. /**
  210. * @brief Function for transmitting an NFC frame.
  211. *
  212. * @param[in] p_tx_data Pointer to the TX buffer.
  213. * @param[in] delay_mode Delay mode of the NFCT frame timer.
  214. *
  215. * @retval NRFX_SUCCESS The operation was successful.
  216. * @retval NRFX_ERROR_INVALID_LENGTH The TX buffer size is invalid.
  217. */
  218. nrfx_err_t nrfx_nfct_tx(nrfx_nfct_data_desc_t const * p_tx_data,
  219. nrf_nfct_frame_delay_mode_t delay_mode);
  220. /**
  221. * @brief Function for moving the NFCT to a new state.
  222. *
  223. * @note The HFCLK must be running before activating the NFCT with
  224. * @ref NRFX_NFCT_STATE_ACTIVATED.
  225. *
  226. * @param[in] state The required state.
  227. */
  228. void nrfx_nfct_state_force(nrfx_nfct_state_t state);
  229. /**
  230. * @brief Function for moving the NFCT to a new initial substate within @ref NRFX_NFCT_STATE_ACTIVATED.
  231. *
  232. * @param[in] sub_state The required substate.
  233. */
  234. void nrfx_nfct_init_substate_force(nrfx_nfct_active_state_t sub_state);
  235. /**
  236. * @brief Function for setting the NFC communication parameter.
  237. *
  238. * @note Parameter validation for length and acceptable values.
  239. *
  240. * @param[in] p_param Pointer to parameter descriptor.
  241. *
  242. * @retval NRFX_SUCCESS The operation was successful.
  243. * @retval NRFX_ERROR_INVALID_PARAM The parameter data is invalid.
  244. */
  245. nrfx_err_t nrfx_nfct_parameter_set(nrfx_nfct_param_t const * p_param);
  246. /**
  247. * @brief Function for getting default bytes for NFCID1.
  248. *
  249. * @param[in,out] p_nfcid1_buff In: empty buffer for data;
  250. * Out: buffer with the NFCID1 default data. These values
  251. * can be used to fill the Type 2 Tag Internal Bytes.
  252. * @param[in] nfcid1_buff_len Length of the NFCID1 buffer.
  253. *
  254. * @retval NRFX_SUCCESS The operation was successful.
  255. * @retval NRFX_ERROR_INVALID_LENGTH Length of the NFCID buffer is different than
  256. * @ref NRFX_NFCT_NFCID1_SINGLE_SIZE,
  257. * @ref NRFX_NFCT_NFCID1_DOUBLE_SIZE, or
  258. * @ref NRFX_NFCT_NFCID1_TRIPLE_SIZE.
  259. */
  260. nrfx_err_t nrfx_nfct_nfcid1_default_bytes_get(uint8_t * const p_nfcid1_buff,
  261. uint32_t nfcid1_buff_len);
  262. /**
  263. * @brief Function for enabling the automatic collision resolution.
  264. *
  265. * @details As defined by the NFC Forum Digital Protocol Technical Specification (and ISO 14443-3),
  266. * the automatic collision resolution is implemented in the NFCT hardware.
  267. * This function allows enabling and disabling this feature.
  268. */
  269. void nrfx_nfct_autocolres_enable(void);
  270. /**
  271. * @brief Function for disabling the automatic collision resolution.
  272. *
  273. * @details See also details in @ref nrfx_nfct_autocolres_enable.
  274. */
  275. void nrfx_nfct_autocolres_disable(void);
  276. /** @} */
  277. void nrfx_nfct_irq_handler(void);
  278. #ifdef __cplusplus
  279. }
  280. #endif
  281. /**
  282. * @defgroup nrfx_nfct_fixes NFCT driver fixes and workarounds
  283. * @{
  284. * @ingroup nrf_nfct
  285. * @brief Fixes for hardware-related anomalies.
  286. *
  287. * If you are using the nRF52832 chip, the workarounds for the following anomalies are applied:
  288. * - 79. NFCT: A false EVENTS_FIELDDETECTED event occurs after the field is lost.
  289. * - 116. NFCT does not release HFCLK when switching from ACTIVATED to SENSE mode.
  290. * To implement the first workaround, an instance of NRF_TIMER is used. After the NFC field is detected,
  291. * the timing module periodically polls its state to determine when the field is turned off.
  292. * To implement the second workaround, power reset is used to release the clock acquired by NFCT
  293. * after the field is turned off. Note that the NFCT register configuration is restored to defaults.
  294. *
  295. * If you are using the nRF52840 chip, rev. Engineering A, the workarounds for the following anomalies
  296. * are applied:
  297. * - 98. NFCT: The NFCT is not able to communicate with the peer.
  298. * - 116. NFCT does not release HFCLK when switching from ACTIVATED to SENSE mode.
  299. * - 144. NFCT: Not optimal NFC performance
  300. *
  301. * If you are using the nRF52840 chip, rev. 1, or rev. Engineering B or C, the workarounds for the following
  302. * anomalies are applied:
  303. * - 190. NFCT: Event FIELDDETECTED can be generated too early.
  304. * To implement this workaround, an instance of NRF_TIMER is used. After the NFC field is detected,
  305. * the timing module measures the necessary waiting period after which NFCT can be activated.
  306. * This debouncing technique is used to filter possible field instabilities.
  307. *
  308. * The application of the implemented workarounds for the nRF52840 chip is determined at runtime and depends
  309. * on the chip variant.
  310. *
  311. * The current code contains a patch for the anomaly 25 (NFCT: Reset value of
  312. * SENSRES register is incorrect), so that the module now works on Windows Phone.
  313. * @}
  314. */
  315. #endif // NRFX_NFCT_H__