nrfx_nfct.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * Copyright (c) 2018 - 2019, 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. /**
  58. * @brief NFCT hardware states.
  59. */
  60. typedef enum
  61. {
  62. NRFX_NFCT_STATE_DISABLED = NRF_NFCT_TASK_DISABLE, ///< NFC Tag is disabled (no sensing of an external NFC field).
  63. NRFX_NFCT_STATE_SENSING = NRF_NFCT_TASK_SENSE, ///< NFC Tag is sensing whether there is an external NFC field.
  64. NRFX_NFCT_STATE_ACTIVATED = NRF_NFCT_TASK_ACTIVATE, ///< NFC Tag is powered-up (see @ref nrfx_nfct_active_state_t for possible substates).
  65. } nrfx_nfct_state_t;
  66. /**
  67. * @brief NFC tag states, when NFCT hardware is activated.
  68. *
  69. * @details These states are substates of the @ref NRFX_NFCT_STATE_ACTIVATED state.
  70. */
  71. typedef enum
  72. {
  73. NRFX_NFCT_ACTIVE_STATE_IDLE = NRF_NFCT_TASK_GOIDLE, ///< NFC Tag is activated and idle (not selected by a reader).
  74. NRFX_NFCT_ACTIVE_STATE_SLEEP = NRF_NFCT_TASK_GOSLEEP, ///< NFC Tag is sleeping.
  75. NRFX_NFCT_ACTIVE_STATE_DEFAULT, ///< NFC Tag is either sleeping or idle, depending on the previous state before being selected by a poller.
  76. } nrfx_nfct_active_state_t;
  77. /**
  78. * @brief NFCT driver event types, passed to the upper-layer callback function
  79. * provided during the initialization.
  80. */
  81. typedef enum
  82. {
  83. NRFX_NFCT_EVT_FIELD_DETECTED = NRF_NFCT_INT_FIELDDETECTED_MASK, ///< External NFC field is detected.
  84. NRFX_NFCT_EVT_FIELD_LOST = NRF_NFCT_INT_FIELDLOST_MASK, ///< External NFC Field is lost.
  85. NRFX_NFCT_EVT_SELECTED = NRF_NFCT_INT_SELECTED_MASK, ///< Tag was selected by the poller.
  86. NRFX_NFCT_EVT_RX_FRAMESTART = NRF_NFCT_INT_RXFRAMESTART_MASK, ///< Data frame reception started.
  87. NRFX_NFCT_EVT_RX_FRAMEEND = NRF_NFCT_INT_RXFRAMEEND_MASK, ///< Data frame is received.
  88. NRFX_NFCT_EVT_TX_FRAMESTART = NRF_NFCT_INT_TXFRAMESTART_MASK, ///< Data frame transmission started.
  89. NRFX_NFCT_EVT_TX_FRAMEEND = NRF_NFCT_INT_TXFRAMEEND_MASK, ///< Data frame is transmitted.
  90. NRFX_NFCT_EVT_ERROR = NRF_NFCT_INT_ERROR_MASK, ///< Error occurred in an NFC communication.
  91. } nrfx_nfct_evt_id_t;
  92. /**
  93. * @brief NFCT timing-related error types.
  94. */
  95. typedef enum
  96. {
  97. NRFX_NFCT_ERROR_FRAMEDELAYTIMEOUT, ///< No response frame was transmitted to the poller in the transmit window.
  98. NRFX_NFCT_ERROR_NUM, ///< Total number of possible errors.
  99. } nrfx_nfct_error_t;
  100. /**
  101. * @brief NFCT driver parameter types.
  102. */
  103. typedef enum
  104. {
  105. NRFX_NFCT_PARAM_ID_FDT, ///< NFC-A Frame Delay Time parameter.
  106. NRFX_NFCT_PARAM_ID_SEL_RES, ///< Value of the 'Protocol' field in the NFC-A SEL_RES frame.
  107. NRFX_NFCT_PARAM_ID_NFCID1, ///< NFC-A NFCID1 setting (NFC tag identifier).
  108. } nrfx_nfct_param_id_t;
  109. /**
  110. * @brief NFCID1 descriptor.
  111. */
  112. typedef struct
  113. {
  114. uint8_t const * p_id; ///< NFCID1 data.
  115. uint8_t id_size; ///< NFCID1 size.
  116. } nrfx_nfct_nfcid1_t;
  117. /**
  118. * @brief NFCT driver parameter descriptor.
  119. */
  120. typedef struct
  121. {
  122. nrfx_nfct_param_id_t id; ///< Type of parameter.
  123. union
  124. {
  125. uint32_t fdt; ///< NFC-A Frame Delay Time. Filled when nrfx_nfct_param_t::id is @ref NRFX_NFCT_PARAM_ID_FDT.
  126. 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.
  127. 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.
  128. } data;
  129. } nrfx_nfct_param_t;
  130. /**
  131. * @brief NFCT driver RX/TX buffer descriptor.
  132. */
  133. typedef struct
  134. {
  135. uint32_t data_size; ///< RX/TX buffer size.
  136. uint8_t const * p_data; ///< RX/TX buffer.
  137. } nrfx_nfct_data_desc_t;
  138. /**
  139. * @brief Structure used to describe the @ref NRFX_NFCT_EVT_RX_FRAMEEND event type.
  140. */
  141. typedef struct
  142. {
  143. uint32_t rx_status; ///< RX error status.
  144. nrfx_nfct_data_desc_t rx_data; ///< RX buffer.
  145. } nrfx_nfct_evt_rx_frameend_t;
  146. /**
  147. * @brief Structure used to describe the @ref NRFX_NFCT_EVT_TX_FRAMESTART event type.
  148. */
  149. typedef struct
  150. {
  151. nrfx_nfct_data_desc_t tx_data; ///< TX buffer.
  152. } nrfx_nfct_evt_tx_framestart_t;
  153. /**
  154. * @brief Structure used to describe the @ref NRFX_NFCT_EVT_ERROR event type.
  155. */
  156. typedef struct
  157. {
  158. nrfx_nfct_error_t reason; ///< Reason for error.
  159. } nrfx_nfct_evt_error_t;
  160. /**
  161. * @brief NFCT driver event.
  162. */
  163. typedef struct
  164. {
  165. nrfx_nfct_evt_id_t evt_id; ///< Type of event.
  166. union
  167. {
  168. 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.
  169. 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.
  170. nrfx_nfct_evt_error_t error; ///< Error data. Filled when nrfx_nfct_evt_t::evt_id is @ref NRFX_NFCT_EVT_ERROR.
  171. } params;
  172. } nrfx_nfct_evt_t;
  173. /**
  174. * @brief Callback descriptor to pass events from the NFCT driver to the upper layer.
  175. *
  176. * @param[in] p_event Pointer to the event descriptor.
  177. */
  178. typedef void (*nrfx_nfct_handler_t)(nrfx_nfct_evt_t const * p_event);
  179. /**
  180. * @brief NFCT driver configuration structure.
  181. */
  182. typedef struct
  183. {
  184. 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. */
  185. nrfx_nfct_handler_t cb; ///< Callback.
  186. } nrfx_nfct_config_t;
  187. /**
  188. * @brief Function for initializing the NFCT driver.
  189. *
  190. * @param[in] p_config Pointer to the NFCT driver configuration structure.
  191. *
  192. * @retval NRFX_SUCCESS If the NFCT driver was initialized successfully.
  193. * @retval NRFX_ERROR_INVALID_STATE If the NFCT driver is already initialized.
  194. */
  195. nrfx_err_t nrfx_nfct_init(nrfx_nfct_config_t const * p_config);
  196. /**
  197. * @brief Function for uninitializing the NFCT driver.
  198. *
  199. * After uninitialization, the instance is in disabled state.
  200. */
  201. void nrfx_nfct_uninit(void);
  202. /**
  203. * @brief Function for starting the NFC subsystem.
  204. *
  205. * After this function completes, NFC readers are able to detect the tag.
  206. */
  207. void nrfx_nfct_enable(void);
  208. /**
  209. * @brief Function for disabling the NFCT driver.
  210. *
  211. * After this function returns, NFC readers are no longer able to connect
  212. * to the tag.
  213. */
  214. void nrfx_nfct_disable(void);
  215. /**
  216. * @brief Function for checking whether the external NFC field is present in the range of the tag.
  217. *
  218. * @retval true If the NFC field is present.
  219. * @retval false If no NFC field is present.
  220. */
  221. bool nrfx_nfct_field_check(void);
  222. /**
  223. * @brief Function for preparing the NFCT driver for receiving an NFC frame.
  224. *
  225. * @param[in] p_rx_data Pointer to the RX buffer.
  226. */
  227. void nrfx_nfct_rx(nrfx_nfct_data_desc_t const * p_rx_data);
  228. /**
  229. * @brief Function for transmitting an NFC frame.
  230. *
  231. * @param[in] p_tx_data Pointer to the TX buffer.
  232. * @param[in] delay_mode Delay mode of the NFCT frame timer.
  233. *
  234. * @retval NRFX_SUCCESS If the operation was successful.
  235. * @retval NRFX_ERROR_INVALID_LENGTH If the TX buffer size is invalid.
  236. */
  237. nrfx_err_t nrfx_nfct_tx(nrfx_nfct_data_desc_t const * p_tx_data,
  238. nrf_nfct_frame_delay_mode_t delay_mode);
  239. /**
  240. * @brief Function for moving the NFCT to a new state.
  241. *
  242. * @note The HFCLK must be running before activating the NFCT with
  243. * @ref NRFX_NFCT_STATE_ACTIVATED.
  244. *
  245. * @param[in] state The required state.
  246. */
  247. void nrfx_nfct_state_force(nrfx_nfct_state_t state);
  248. /**
  249. * @brief Function for moving the NFCT to a new initial substate within @ref NRFX_NFCT_STATE_ACTIVATED.
  250. *
  251. * @param[in] sub_state The required substate.
  252. */
  253. void nrfx_nfct_init_substate_force(nrfx_nfct_active_state_t sub_state);
  254. /**
  255. * @brief Function for setting the NFC communication parameter.
  256. *
  257. * @note Parameter validation for length and acceptable values.
  258. *
  259. * @param[in] p_param Pointer to parameter descriptor.
  260. *
  261. * @retval NRFX_SUCCESS If the operation was successful.
  262. * @retval NRFX_ERROR_INVALID_PARAM If the parameter data is invalid.
  263. */
  264. nrfx_err_t nrfx_nfct_parameter_set(nrfx_nfct_param_t const * p_param);
  265. /**
  266. * @brief Function for getting default bytes for NFCID1.
  267. *
  268. * @param[in,out] p_nfcid1_buff In: empty buffer for data;
  269. * Out: buffer with the NFCID1 default data. These values
  270. * can be used to fill the Type 2 Tag Internal Bytes.
  271. * @param[in] nfcid1_buff_len Length of the NFCID1 buffer.
  272. *
  273. * @retval NRFX_SUCCESS If the operation was successful.
  274. * @retval NRFX_ERROR_INVALID_LENGTH If length of the NFCID buffer is different than
  275. * @ref NRFX_NFCT_NFCID1_SINGLE_SIZE,
  276. * @ref NRFX_NFCT_NFCID1_DOUBLE_SIZE, or
  277. * @ref NRFX_NFCT_NFCID1_TRIPLE_SIZE.
  278. */
  279. nrfx_err_t nrfx_nfct_nfcid1_default_bytes_get(uint8_t * const p_nfcid1_buff,
  280. uint32_t nfcid1_buff_len);
  281. /**
  282. * @brief Function for enabling the automatic collision resolution.
  283. *
  284. * @details As defined by the NFC Forum Digital Protocol Technical Specification (and ISO 14443-3),
  285. * the automatic collision resolution is implemented in the NFCT hardware.
  286. * This function allows enabling and disabling this feature.
  287. */
  288. void nrfx_nfct_autocolres_enable(void);
  289. /**
  290. * @brief Function for disabling the automatic collision resolution.
  291. *
  292. * @details See also details in @ref nrfx_nfct_autocolres_enable.
  293. */
  294. void nrfx_nfct_autocolres_disable(void);
  295. void nrfx_nfct_irq_handler(void);
  296. /** @} */
  297. #ifdef __cplusplus
  298. }
  299. #endif
  300. /**
  301. * @defgroup nrfx_nfct_fixes NFCT driver fixes and workarounds
  302. * @{
  303. * @ingroup nrf_nfct
  304. * @brief Fixes for hardware-related anomalies.
  305. *
  306. * If you are using the nRF52832 chip, the workarounds for the following anomalies are applied:
  307. * - 79. NFCT: A false EVENTS_FIELDDETECTED event occurs after the field is lost.
  308. * - 116. NFCT does not release HFCLK when switching from ACTIVATED to SENSE mode.
  309. * To implement the first workaround, an instance of NRF_TIMER is used. After the NFC field is detected,
  310. * the timing module periodically polls its state to determine when the field is turned off.
  311. * To implement the second workaround, power reset is used to release the clock acquired by NFCT
  312. * after the field is turned off. Note that the NFCT register configuration is restored to defaults.
  313. *
  314. * If you are using the nRF52840 chip, rev. Engineering A, the workarounds for the following anomalies
  315. * are applied:
  316. * - 98. NFCT: The NFCT is not able to communicate with the peer.
  317. * - 116. NFCT does not release HFCLK when switching from ACTIVATED to SENSE mode.
  318. * - 144. NFCT: Not optimal NFC performance
  319. *
  320. * If you are using the nRF52840 chip, rev. 1, or rev. Engineering B or C, the workarounds for the following
  321. * anomalies are applied:
  322. * - 190. NFCT: Event FIELDDETECTED can be generated too early.
  323. * To implement this workaround, an instance of NRF_TIMER is used. After the NFC field is detected,
  324. * the timing module measures the necessary waiting period after which NFCT can be activated.
  325. * This debouncing technique is used to filter possible field instabilities.
  326. *
  327. * The application of the implemented workarounds for the nRF52840 chip is determined at runtime and depends
  328. * on the chip variant.
  329. *
  330. * The current code contains a patch for the anomaly 25 (NFCT: Reset value of
  331. * SENSRES register is incorrect), so that the module now works on Windows Phone.
  332. * @}
  333. */
  334. #endif // NRFX_NFCT_H__