adafruit_pn532.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Adafruit PN532 library adapted to use in nRF51 and nRF52
  3. *
  4. * Software License Agreement (BSD License)
  5. *
  6. * Copyright (c) 2012, Adafruit Industries
  7. * All rights reserved.
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the
  16. * names of its contributors may be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef ADAFRUIT_PN532__
  30. #define ADAFRUIT_PN532__
  31. #include <stdint.h>
  32. #include <stdbool.h>
  33. #include "sdk_errors.h"
  34. /** @file
  35. * @brief Adafruit PN532 NFC Shield library for reading and writing tags.
  36. *
  37. * @defgroup adafruit_pn532 Adafruit PN532 NFC Shield library
  38. * @{
  39. * @ingroup app_common
  40. * @brief Adafruit PN532 NFC Shield library for reading and writing tags.
  41. *
  42. * This library is an nRF51 and nRF52 port of the Adafruit PN532 library,
  43. * which is available on <a href="https://github.com/adafruit/Adafruit-PN532" target="_blank">GitHub</a>,
  44. * with some improvements and bugfixes. The library is responsible for
  45. * communicating with the Adafruit PN532 NFC Shield and using its main
  46. * functions.
  47. *
  48. * This library can be used with an <a href="https://www.adafruit.com/products/789" target="_blank">Adafruit PN532 NFC/RFID Controller Shield</a>.
  49. */
  50. /**
  51. * @defgroup nrf_external_adafruit_pn532_frame_header Frame header
  52. * @brief Macros related to the frame header and checksum parts.
  53. *
  54. *
  55. * Sizes of the header and checksum parts of the frame.
  56. * @{
  57. */
  58. #define HEADER_SEQUENCE_LENGTH 6
  59. #define CHECKSUM_SEQUENCE_LENGTH 2
  60. #define PN532_FRAME_OVERHEAD (HEADER_SEQUENCE_LENGTH + CHECKSUM_SEQUENCE_LENGTH)
  61. /** @} */
  62. /**
  63. * @defgroup nrf_external_adafruit_pn532_frame_tokens Frame tokens and offsets
  64. * @brief Macros related to frame tokens and offsets.
  65. *
  66. * @{
  67. *
  68. * @name Tokens
  69. * @brief Start and end location of frame token identifiers.
  70. * @{
  71. */
  72. #define PN532_PREAMBLE (0x00)
  73. #define PN532_STARTCODE1 (0x00)
  74. #define PN532_STARTCODE2 (0xFF)
  75. #define PN532_POSTAMBLE (0x00)
  76. /**
  77. * @}
  78. *
  79. * @name Offsets
  80. * @{
  81. */
  82. #define PN532_PREAMBLE_OFFSET 0
  83. #define PN532_STARTCODE1_OFFSET 1
  84. #define PN532_STARTCODE2_OFFSET 2
  85. #define PN532_LENGTH_OFFSET 3
  86. #define PN532_LENGTH_CS_OFFSET 4
  87. #define PN532_TFI_OFFSET 5
  88. #define PN532_DATA_OFFSET 6
  89. /**
  90. * @}
  91. * @}
  92. */
  93. /**
  94. * @defgroup nrf_external_adafruit_pn532_frame_direction_identifiers Frame direction identifiers
  95. * @brief Macro codes identifying the communication direction.
  96. *
  97. * Each frame contains one of these codes to identify whether this frame
  98. * was sent to or received from the Adafruit PN532 Shield.
  99. * @{
  100. */
  101. #define PN532_HOSTTOPN532 (0xD4)
  102. #define PN532_PN532TOHOST (0xD5)
  103. /** @} */
  104. /**
  105. * @defgroup nrf_external_adafruit_pn532_command_codes Command codes
  106. * @brief Macros for the available command codes.
  107. *
  108. * The following command codes are available in the Adafruit PN532 Shield.
  109. * @{
  110. */
  111. #define PN532_COMMAND_DIAGNOSE (0x00)
  112. #define PN532_COMMAND_GETFIRMWAREVERSION (0x02)
  113. #define PN532_COMMAND_GETGENERALSTATUS (0x04)
  114. #define PN532_COMMAND_READREGISTER (0x06)
  115. #define PN532_COMMAND_WRITEREGISTER (0x08)
  116. #define PN532_COMMAND_READGPIO (0x0C)
  117. #define PN532_COMMAND_WRITEGPIO (0x0E)
  118. #define PN532_COMMAND_SETSERIALBAUDRATE (0x10)
  119. #define PN532_COMMAND_SETPARAMETERS (0x12)
  120. #define PN532_COMMAND_SAMCONFIGURATION (0x14)
  121. #define PN532_COMMAND_POWERDOWN (0x16)
  122. #define PN532_COMMAND_RFCONFIGURATION (0x32)
  123. #define PN532_COMMAND_RFREGULATIONTEST (0x58)
  124. #define PN532_COMMAND_INJUMPFORDEP (0x56)
  125. #define PN532_COMMAND_INJUMPFORPSL (0x46)
  126. #define PN532_COMMAND_INLISTPASSIVETARGET (0x4A)
  127. #define PN532_COMMAND_INATR (0x50)
  128. #define PN532_COMMAND_INPSL (0x4E)
  129. #define PN532_COMMAND_INDATAEXCHANGE (0x40)
  130. #define PN532_COMMAND_INCOMMUNICATETHRU (0x42)
  131. #define PN532_COMMAND_INDESELECT (0x44)
  132. #define PN532_COMMAND_INRELEASE (0x52)
  133. #define PN532_COMMAND_INSELECT (0x54)
  134. #define PN532_COMMAND_INAUTOPOLL (0x60)
  135. #define PN532_COMMAND_TGINITASTARGET (0x8C)
  136. #define PN532_COMMAND_TGSETGENERALBYTES (0x92)
  137. #define PN532_COMMAND_TGGETDATA (0x86)
  138. #define PN532_COMMAND_TGSETDATA (0x8E)
  139. #define PN532_COMMAND_TGSETMETADATA (0x94)
  140. #define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88)
  141. #define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90)
  142. #define PN532_COMMAND_TGGETTARGETSTATUS (0x8A)
  143. /** @} */
  144. /**
  145. * @defgroup nrf_external_adafruit_pn532_mifare_command_codes Mifare command codes
  146. * @brief Macros for the available Mifare command codes.
  147. *
  148. * The following Mifare command codes are available in the Adafruit PN532 Shield.
  149. * @{
  150. */
  151. #define MIFARE_CMD_AUTH_A (0x60)
  152. #define MIFARE_CMD_AUTH_B (0x61)
  153. #define MIFARE_CMD_READ (0x30)
  154. #define MIFARE_CMD_WRITE (0xA0)
  155. #define MIFARE_CMD_TRANSFER (0xB0)
  156. #define MIFARE_CMD_DECREMENT (0xC0)
  157. #define MIFARE_CMD_INCREMENT (0xC1)
  158. #define MIFARE_CMD_STORE (0xC2)
  159. #define MIFARE_ULTRALIGHT_CMD_WRITE (0xA2)
  160. /** @} */
  161. /**
  162. * @defgroup nrf_external_adafruit_pn532_t2t Type 2 Tag specific parameters
  163. * @brief Macros for Type 2 Tag specific parameters.
  164. * @{
  165. */
  166. #define T2T_MAX_DATA_EXCHANGE 16 ///< Type 2 Tag maximal command data size (in bytes).
  167. #define T2T_PAGE_SIZE 4 ///< Type 2 Tag page/block size (in bytes).
  168. #define T2T_END_PAGE_OFFSET 3 ///< Offset of the last page/block in Type 2 Tag response payload.
  169. /** @} */
  170. /**
  171. * @defgroup nrf_external_adafruit_pn532_nfc_a NFC-A initialisation response parameters.
  172. * @brief Macros for NFC-A initialisation response parameters.
  173. * @{
  174. */
  175. #define SENS_RES_ANTICOLLISION_INFO_BYTE 0
  176. #define SENS_RES_PLATFORM_INFO_BYTE 1
  177. #define SENS_RES_SIZE 2
  178. #define MAX_NFC_A_ID_LEN 10
  179. /** @} */
  180. #define PN532_MIFARE_ISO14443A_BAUD (0x00) ///< Code identifying the baud rate for the ISO14443A (NFC-A) card type.
  181. #define PN532_I2C_ADDRESS (0x48 >> 1) ///< Address of the I2C peripheral of the Adafruit PN532 Shield.
  182. #ifndef PN532_PACKBUFF_SIZE
  183. #define PN532_PACKBUFF_SIZE 64
  184. #endif
  185. /**
  186. * @brief Basic information about detected NFC-A tag.
  187. */
  188. typedef struct
  189. {
  190. uint8_t sens_res[SENS_RES_SIZE]; ///< SENS_RES response bytes.
  191. uint8_t sel_res; ///< SEL_RES response byte.
  192. uint8_t nfc_id_len; ///< UID length.
  193. uint8_t nfc_id[MAX_NFC_A_ID_LEN]; ///< NFC-A UID.
  194. } nfc_a_tag_info;
  195. /**
  196. * @name Functions used for initialization
  197. *
  198. * @{ */
  199. /** @brief Function for initializing the communication with the Adafruit PN532 Shield.
  200. *
  201. * @note This library is not thread-safe, because it uses static buffers.
  202. *
  203. * @param[in] force If true, reinitialization of the library will be forced.
  204. *
  205. * @retval NRF_SUCCESS If the communication was initialized successfully. Otherwise,
  206. * an error code is returned.
  207. */
  208. ret_code_t adafruit_pn532_init(bool force);
  209. /** @brief Function for creating a new PN532 object using I2C.
  210. *
  211. * Before calling this function, PN532_IRQ and PN532_RESET must be configured.
  212. *
  213. * @retval NRF_SUCCESS If the object was created successfully. Otherwise,
  214. * an error code is returned.
  215. */
  216. ret_code_t adafruit_pn532_i2c_create(void);
  217. /** @} */
  218. /**
  219. * @name Generic functions for the Adafruit PN532 Shield
  220. *
  221. * @{ */
  222. /** @brief Function for configuring the Secure Access Module (SAM).
  223. *
  224. * This function configures the SAM to work in a mode specified in the mode parameter. For a reader
  225. * operation, use SAMCONFIGURATION_MODE_NORMAL.
  226. *
  227. * @param[in] mode Mode in which the PN532 Shield should work.
  228. *
  229. * @retval NRF_SUCCESS If the SAM was configured successfully. Otherwise,
  230. * an error code is returned.
  231. */
  232. ret_code_t adafruit_pn532_sam_config(uint8_t mode);
  233. /** @brief Function for entering power-down mode with I2C as wake-up source.
  234. *
  235. * @retval NRF_SUCCESS If power-down mode was entered successfully. Otherwise,
  236. * an error code is returned.
  237. */
  238. ret_code_t adafruit_pn532_power_down(void);
  239. /** @brief Function for waking up the PN532 Shield from power-down mode.
  240. *
  241. * @retval NRF_SUCCESS If the PN532 Shield woke up successfully. Otherwise,
  242. * an error code is returned.
  243. */
  244. ret_code_t adafruit_pn532_wake_up(void);
  245. /** @brief Function for checking the firmware version of the PN532 chip.
  246. *
  247. * @param[out] p_response The chip's firmware version and ID.
  248. *
  249. * @retval NRF_SUCCESS If the function completed successfully. Otherwise,
  250. * an error code is returned.
  251. */
  252. ret_code_t adafruit_pn532_firmware_version_get(uint32_t * p_response);
  253. /** @brief Function for sending a command and waiting a specified period for the ACK.
  254. *
  255. * @param[in] p_cmd Pointer to the command buffer.
  256. * @param[in] cmd_len The length of the command (in bytes).
  257. * @param[in] timeout Time-out (in ms) before giving up.
  258. *
  259. * @retval NRF_SUCCESS If the command was sent successfully. Otherwise,
  260. * an error code is returned.
  261. */
  262. ret_code_t adafruit_pn532_cmd_send(uint8_t * p_cmd, uint8_t cmd_len, uint16_t timeout);
  263. /** @brief Function for enabling the PN532 RF field.
  264. *
  265. * @retval NRF_SUCCESS If the RF field was enabled successfully. Otherwise,
  266. * an error code is returned.
  267. */
  268. ret_code_t adafruit_pn532_field_on(void);
  269. /** @brief Function for disabling the PN532 RF field.
  270. *
  271. * @retval NRF_SUCCESS If the RF field was disabled successfully. Otherwise,
  272. * an error code is returned.
  273. */
  274. ret_code_t adafruit_pn532_field_off(void);
  275. /** @} */
  276. /**
  277. * @name Functions for ISO14443A tags
  278. *
  279. * @{ */
  280. /** @brief Function for detecting an ISO14443A (NFC-A) target presence in the RF field.
  281. *
  282. * This function enables the RF field and scans for ISO14443A (NFC-A) targets present
  283. * in the field. The number of scan retries is set by the @ref adafruit_pn532_passive_activation_retries_set
  284. * function. By default, the maximum number of retries is set to unlimited, which means
  285. * that the PN532 Shield scans for targets until it finds one or the scan is
  286. * canceled. The @p timeout parameter specifies the time-out of the scan. If it is
  287. * set to a value greater than 0, the function exits with a failure if either the maximum number
  288. * of retries or the time-out has been reached. If the @p timeout parameter is set to 0,
  289. * a single scan is performed. When the ISO14443A (NFC-A) target is detected, the
  290. * PN532 module initializes communication and reads the basic initialization information
  291. * about NFC-A tag including SENS_RES, SEL_RES and UID. This information is retrieved by
  292. * NFC reader during Technology Detection and Collision Resolution Activities.
  293. *
  294. * @param[in,out] p_tag_info Pointer to the structure where NFC-A Tag
  295. * basic initialization information will be stored.
  296. * @param[in] timeout Time-out (in ms). 0 means that only a single
  297. * scan is performed.
  298. * If no tag is presented before the time-out,
  299. * the function returns NRF_ERROR_INTERNAL.
  300. *
  301. * @retval NRF_SUCCESS If the function completed successfully. Otherwise,
  302. * an error code is returned.
  303. */
  304. ret_code_t adafruit_pn532_nfc_a_target_init(nfc_a_tag_info * p_tag_info,
  305. uint16_t timeout);
  306. /** @brief Function for exchanging an Application Protocol Data Unit (APDU) with the currently enlisted peer.
  307. *
  308. * @param[in] p_send Pointer to the data to send.
  309. * @param[in] send_len Length of the data to send.
  310. * @param[out] p_response Pointer to the buffer for response data.
  311. * @param[in,out] p_response_len Pointer to the variable that stores
  312. * the length of the p_response buffer (as
  313. * input) and the length of the response data
  314. * (as output).
  315. *
  316. * @retval NRF_SUCCESS If the function completed successfully. Otherwise,
  317. * an error code is returned.
  318. */
  319. ret_code_t adafruit_pn532_in_data_exchange(uint8_t * p_send,
  320. uint8_t send_len,
  321. uint8_t * p_response,
  322. uint8_t * p_response_len);
  323. /** @brief Function for setting the MxRtyPassiveActivation parameter of the RFConfiguration register.
  324. *
  325. * This function sets the maximum number of retries when scanning for a tag.
  326. * The default is an unlimited number of retries.
  327. *
  328. * @param[in] max_retries 0xFF to wait forever. 0x00..0xFE to time out
  329. * after the specified number of retries.
  330. *
  331. * @retval NRF_SUCCESS If MxRtyPassiveActivation was set successfully. Otherwise,
  332. * an error code is returned.
  333. */
  334. ret_code_t adafruit_pn532_passive_activation_retries_set(uint8_t max_retries);
  335. /** @} */
  336. /**
  337. * @name Type 2 Tag related functions
  338. *
  339. * @{ */
  340. /** @brief Function for reading 4 pages/blocks within Type 2 Tag, starting with
  341. * the specified page/block number.
  342. *
  343. * This function reads 4 pages/blocks within Type 2 Tag at the specified page/block
  344. * number, using Type 2 Tag READ command.
  345. *
  346. * @param[in] start_page The page/block number (0..63 in most cases).
  347. * @param[out] p_buffer Pointer to the uint8_t array that will
  348. * hold the retrieved data (if any).
  349. *
  350. * @retval NRF_SUCCESS If the data was read successfully. Otherwise,
  351. * an error code is returned.
  352. */
  353. ret_code_t adafruit_pn532_tag2_read(uint8_t start_page, uint8_t * p_buffer);
  354. /** @brief Function for writing an entire 4-byte page/block to the Type 2 Tag at the specified
  355. * page/block address.
  356. *
  357. * This function writes a 4-byte sequence to the Type 2 Tag at the specified page/block, using
  358. * Type 2 Tag WRITE command.
  359. *
  360. * @param[in] page The page/block number to write (0..63 in most cases).
  361. * @param[in] p_data The uint8_t array that contains the data to write.
  362. * The data should be exactly 4 bytes long.
  363. *
  364. * @retval NRF_SUCCESS If the data was written successfully. Otherwise,
  365. * an error code is returned.
  366. */
  367. ret_code_t adafruit_pn532_tag2_page_write(uint8_t page, uint8_t * p_data);
  368. /** @brief Function for writing an NDEF URI record to Type 2 Tag at the specified page (4..nn).
  369. *
  370. * This function writes an NDEF URI record to Type 2 Tag at the specified page (4..nn). It uses
  371. * @ref adafruit_pn532_tag2_page_write to perform atomic writes.
  372. *
  373. * @param[in] uri_id The URI identifier code (0 = none, 0x01 =
  374. * "http://www.", and so on).
  375. * @param[in] p_url The URI text to write (null-terminated string).
  376. * @param[in] data_len The maximum number of bytes that can be stored in the
  377. * target device.
  378. *
  379. * @retval NRF_SUCCESS If the record was written successfully. Otherwise,
  380. * an error code is returned.
  381. */
  382. ret_code_t adafruit_pn532_ndef_uri_tag2_write(uint8_t uri_id, char * p_url, uint8_t data_len);
  383. /** @} */
  384. /**
  385. * @name Printing functions.
  386. *
  387. * @{ */
  388. /** @brief Function for printing NFC-A Tag Info descriptor.
  389. *
  390. * This function prints NFC-A Tag Info descriptor.
  391. *
  392. * @param[in] p_tag_info Pointer to the NFC-A Tag Info descriptor.
  393. */
  394. void adafruit_pn532_tag_info_printout(nfc_a_tag_info const * const p_tag_info);
  395. /** @} */
  396. /**
  397. * @name Low-level communication functions that utilize I2C and GPIO
  398. *
  399. * @{ */
  400. /** @brief Function for checking PN532 Shield readiness.
  401. *
  402. * @retval True If the PN532 Shield is ready with a response.
  403. * @retval False Otherwise.
  404. */
  405. bool adafruit_pn532_is_ready(void);
  406. /** @brief Function for waiting until the PN532 Shield is ready.
  407. *
  408. * @param[in] timeout Time-out (in ms) before giving up.
  409. *
  410. * @retval True If the PN532 Shield is ready.
  411. * @retval False Otherwise.
  412. */
  413. bool adafruit_pn532_waitready_ms(uint16_t timeout);
  414. /** @brief Function for reading the ACK frame.
  415. *
  416. * @retval NRF_SUCCESS If the ACK frame was read. Otherwise, an error code is returned.
  417. */
  418. ret_code_t adafruit_pn532_ack_read(void);
  419. /** @brief Function for reading n bytes of data from the PN532 Shield via I2C.
  420. *
  421. * @param[out] p_buff Pointer to the buffer where the data will be written.
  422. * @param[in] n Number of bytes to read.
  423. *
  424. * @retval NRF_SUCCESS If the data was read successfully. Otherwise,
  425. * an error code is returned.
  426. */
  427. ret_code_t adafruit_pn532_data_read(uint8_t * p_buff, uint8_t n);
  428. /** @brief Function for writing a command to the PN532 Shield.
  429. *
  430. * This function writes a command to the PN532 Shield and automatically inserts
  431. * the preamble and required frame details (such as checksum, length, ...)
  432. *
  433. * @param[in] p_cmd Pointer to the command buffer.
  434. * @param[in] cmd_len Command length in bytes.
  435. *
  436. * @retval NRF_SUCCESS If the command was written successfully. Otherwise,
  437. * an error code is returned.
  438. */
  439. ret_code_t adafruit_pn532_command_write(uint8_t * p_cmd, uint8_t cmd_len);
  440. /** @} */
  441. /**
  442. *@}
  443. **/
  444. #endif