adafruit_pn532.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. #ifdef PN532_DEBUG
  32. #define PN532_LOG printf
  33. #define PN532_LOG_HEX print_hex
  34. #else
  35. #define PN532_LOG(...)
  36. #define PN532_LOG_HEX(...)
  37. #endif
  38. #include <stdint.h>
  39. #include <stdbool.h>
  40. #include "sdk_errors.h"
  41. /** @file
  42. * @brief Adafruit PN532 NFC Shield library for reading and writing tags.
  43. *
  44. * @defgroup nrf_external_adafruit_pn532 Adafruit PN532 NFC Shield library
  45. * @{
  46. * @ingroup app_common
  47. * @brief Adafruit PN532 NFC Shield library for reading and writing tags.
  48. *
  49. * This library is an nRF51 and nRF52 port of the Adafruit PN532 library,
  50. * which is available on <a href="https://github.com/adafruit/Adafruit-PN532" target="_blank">GitHub</a>,
  51. * with some improvements and bugfixes. The library is responsible for
  52. * communicating with the Adafruit PN532 NFC Shield and using its main
  53. * functions.
  54. *
  55. * This library can be used with an <a href="https://www.adafruit.com/products/789" target="_blank">Adafruit PN532 NFC/RFID Controller Shield</a>.
  56. */
  57. /**
  58. * @defgroup nrf_external_adafruit_pn532_frame_header Frame header
  59. * @brief Macros related to the frame header and checksum parts.
  60. *
  61. *
  62. * Sizes of the header and checksum parts of the frame.
  63. * @{
  64. */
  65. #define HEADER_SEQUENCE_LENGTH 6
  66. #define CHECKSUM_SEQUENCE_LENGTH 2
  67. #define PN532_FRAME_OVERHEAD (HEADER_SEQUENCE_LENGTH + CHECKSUM_SEQUENCE_LENGTH)
  68. /** @} */
  69. /**
  70. * @defgroup nrf_external_adafruit_pn532_frame_tokens Frame tokens and offsets
  71. * @brief Macros related to frame tokens and offsets.
  72. *
  73. * @{
  74. *
  75. * @name Tokens
  76. * @brief Start and end location of frame token identifiers.
  77. * @{
  78. */
  79. #define PN532_PREAMBLE (0x00)
  80. #define PN532_STARTCODE1 (0x00)
  81. #define PN532_STARTCODE2 (0xFF)
  82. #define PN532_POSTAMBLE (0x00)
  83. /**
  84. * @}
  85. *
  86. * @name Offsets
  87. * @{
  88. */
  89. #define PN532_PREAMBLE_OFFSET 0
  90. #define PN532_STARTCODE1_OFFSET 1
  91. #define PN532_STARTCODE2_OFFSET 2
  92. #define PN532_LENGTH_OFFSET 3
  93. #define PN532_LENGTH_CS_OFFSET 4
  94. #define PN532_TFI_OFFSET 5
  95. #define PN532_DATA_OFFSET 6
  96. /**
  97. * @}
  98. * @}
  99. */
  100. /**
  101. * @defgroup nrf_external_adafruit_pn532_frame_direction_identifiers Frame direction identifiers
  102. * @brief Macro codes identifying the communication direction.
  103. *
  104. * Each frame contains one of these codes to identify whether this frame
  105. * was sent to or received from the Adafruit PN532 Shield.
  106. * @{
  107. */
  108. #define PN532_HOSTTOPN532 (0xD4)
  109. #define PN532_PN532TOHOST (0xD5)
  110. /** @} */
  111. /**
  112. * @defgroup nrf_external_adafruit_pn532_command_codes Command codes
  113. * @brief Macros for the available command codes.
  114. *
  115. * The following command codes are available in the Adafruit PN532 Shield.
  116. * @{
  117. */
  118. #define PN532_COMMAND_DIAGNOSE (0x00)
  119. #define PN532_COMMAND_GETFIRMWAREVERSION (0x02)
  120. #define PN532_COMMAND_GETGENERALSTATUS (0x04)
  121. #define PN532_COMMAND_READREGISTER (0x06)
  122. #define PN532_COMMAND_WRITEREGISTER (0x08)
  123. #define PN532_COMMAND_READGPIO (0x0C)
  124. #define PN532_COMMAND_WRITEGPIO (0x0E)
  125. #define PN532_COMMAND_SETSERIALBAUDRATE (0x10)
  126. #define PN532_COMMAND_SETPARAMETERS (0x12)
  127. #define PN532_COMMAND_SAMCONFIGURATION (0x14)
  128. #define PN532_COMMAND_POWERDOWN (0x16)
  129. #define PN532_COMMAND_RFCONFIGURATION (0x32)
  130. #define PN532_COMMAND_RFREGULATIONTEST (0x58)
  131. #define PN532_COMMAND_INJUMPFORDEP (0x56)
  132. #define PN532_COMMAND_INJUMPFORPSL (0x46)
  133. #define PN532_COMMAND_INLISTPASSIVETARGET (0x4A)
  134. #define PN532_COMMAND_INATR (0x50)
  135. #define PN532_COMMAND_INPSL (0x4E)
  136. #define PN532_COMMAND_INDATAEXCHANGE (0x40)
  137. #define PN532_COMMAND_INCOMMUNICATETHRU (0x42)
  138. #define PN532_COMMAND_INDESELECT (0x44)
  139. #define PN532_COMMAND_INRELEASE (0x52)
  140. #define PN532_COMMAND_INSELECT (0x54)
  141. #define PN532_COMMAND_INAUTOPOLL (0x60)
  142. #define PN532_COMMAND_TGINITASTARGET (0x8C)
  143. #define PN532_COMMAND_TGSETGENERALBYTES (0x92)
  144. #define PN532_COMMAND_TGGETDATA (0x86)
  145. #define PN532_COMMAND_TGSETDATA (0x8E)
  146. #define PN532_COMMAND_TGSETMETADATA (0x94)
  147. #define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88)
  148. #define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90)
  149. #define PN532_COMMAND_TGGETTARGETSTATUS (0x8A)
  150. /** @} */
  151. /**
  152. * @defgroup nrf_external_adafruit_pn532_mifare_command_codes Mifare command codes
  153. * @brief Macros for the available Mifare command codes.
  154. *
  155. * The following Mifare command codes are available in the Adafruit PN532 Shield.
  156. * @{
  157. */
  158. #define MIFARE_CMD_AUTH_A (0x60)
  159. #define MIFARE_CMD_AUTH_B (0x61)
  160. #define MIFARE_CMD_READ (0x30)
  161. #define MIFARE_CMD_WRITE (0xA0)
  162. #define MIFARE_CMD_TRANSFER (0xB0)
  163. #define MIFARE_CMD_DECREMENT (0xC0)
  164. #define MIFARE_CMD_INCREMENT (0xC1)
  165. #define MIFARE_CMD_STORE (0xC2)
  166. #define MIFARE_ULTRALIGHT_CMD_WRITE (0xA2)
  167. /** @} */
  168. /// Code identifying the baud rate for the ISO14443A card type.
  169. #define PN532_MIFARE_ISO14443A_BAUD (0x00)
  170. /// Address of the I2C peripheral of the Adafruit PN532 Shield.
  171. #define PN532_I2C_ADDRESS (0x48 >> 1)
  172. /// Size of the buffer used for sending commands and storing responses.
  173. #define PN532_PACKBUFFSIZ (64)
  174. /**
  175. * @brief Information about the communication between the host and the Adafruit PN532 Shield.
  176. */
  177. typedef struct
  178. {
  179. uint8_t _ss; //!< Slave select signal for SPI.
  180. uint8_t _clk; //!< Clock signal for SPI.
  181. uint8_t _mosi; //!< Master output, slave input signal for SPI.
  182. uint8_t _miso; //!< Master input, slave output signal for SPI.
  183. uint8_t _irq; //!< Interrupt pin for Adafruit.
  184. uint8_t _reset; //!< Reset pin for Adafruit.
  185. uint8_t _uid[7]; //!< ISO14443A UID.
  186. uint8_t _uidLen; //!< UID length.
  187. uint8_t _key[6]; //!< Mifare Classic key.
  188. uint8_t _inListedTag; //!< Tag number of inlisted tag.
  189. bool _usingSPI; //!< True if using SPI, false if using I2C.
  190. bool _hardwareSPI; //!< True if using hardware SPI, false if using software SPI.
  191. } adafruit_pn532;
  192. /**
  193. * @name Functions used for initialization
  194. *
  195. * @{ */
  196. /** @brief Function for initializing the communication with the Adafruit PN532 Shield.
  197. *
  198. * @note This library is not thread-safe, because it uses static buffers.
  199. *
  200. * @param[in] force If true, reinitialization of the library will be forced.
  201. *
  202. * @retval NRF_SUCCESS If the communication was initialized successfully. Otherwise,
  203. * an error code is returned.
  204. */
  205. ret_code_t adafruit_pn532_init(bool force);
  206. /** @brief Function for creating a new PN532 object using I2C.
  207. *
  208. * Before calling this function, PN532_IRQ and PN532_RESET must be configured.
  209. *
  210. * @retval NRF_SUCCESS If the object was created successfully. Otherwise,
  211. * an error code is returned.
  212. */
  213. ret_code_t adafruit_pn532_create_i2c(void);
  214. /** @} */
  215. /**
  216. * @name Generic functions for the Adafruit PN532 Shield
  217. *
  218. * @{ */
  219. /** @brief Function for configuring the Secure Access Module (SAM).
  220. *
  221. * This function configures the SAM to work in a mode specified in the mode parameter. For a reader
  222. * operation, use SAMCONFIGURATION_MODE_NORMAL.
  223. *
  224. * @param[in] mode Mode in which the PN532 Shield should work.
  225. *
  226. * @retval NRF_SUCCESS If the SAM was configured successfully. Otherwise,
  227. * an error code is returned.
  228. */
  229. ret_code_t adafruit_pn532_sam_config(uint8_t mode);
  230. /** @brief Function for entering power-down mode with I2C as wake-up source.
  231. *
  232. * @retval NRF_SUCCESS If power-down mode was entered successfully. Otherwise,
  233. * an error code is returned.
  234. */
  235. ret_code_t adafruit_pn532_power_down(void);
  236. /** @brief Function for waking up the PN532 Shield from power-down mode.
  237. *
  238. * @retval NRF_SUCCESS If the PN532 Shield woke up successfully. Otherwise,
  239. * an error code is returned.
  240. */
  241. ret_code_t adafruit_pn532_wake_up(void);
  242. /** @brief Function for checking the firmware version of the PN532 chip.
  243. *
  244. * @param[out] p_response The chip's firmware version and ID.
  245. *
  246. * @retval NRF_SUCCESS If the function completed successfully. Otherwise,
  247. * an error code is returned.
  248. */
  249. ret_code_t adafruit_pn532_get_firmware_version(uint32_t * p_response);
  250. /** @brief Function for sending a command and waiting a specified period for the ACK.
  251. *
  252. * @param[in] p_cmd Pointer to the command buffer.
  253. * @param[in] cmd_len The length of the command (in bytes).
  254. * @param[in] timeout Time-out (in ms) before giving up.
  255. *
  256. * @retval NRF_SUCCESS If the command was sent successfully. Otherwise,
  257. * an error code is returned.
  258. */
  259. ret_code_t adafruit_pn532_send_cmd(uint8_t * p_cmd, uint8_t cmd_len, uint16_t timeout);
  260. /** @brief Function for enabling the PN532 RF field.
  261. *
  262. * @retval NRF_SUCCESS If the RF field was enabled successfully. Otherwise,
  263. * an error code is returned.
  264. */
  265. ret_code_t adafruit_pn532_field_on(void);
  266. /** @brief Function for disabling the PN532 RF field.
  267. *
  268. * @retval NRF_SUCCESS If the RF field was disabled successfully. Otherwise,
  269. * an error code is returned.
  270. */
  271. ret_code_t adafruit_pn532_field_off(void);
  272. /** @} */
  273. /**
  274. * @name Functions for ISO14443A tags
  275. *
  276. * @{ */
  277. /** @brief Function for detecting an ISO14443A target presence in the RF field.
  278. *
  279. * This function enables the RF field and scans for ISO14443A targets present
  280. * in the field. The number of scan retries is set by the @ref adafruit_pn532_set_passive_activation_retries
  281. * function. By default, the maximum number of retries is set to unlimited, which means
  282. * that the PN532 Shield scans for targets until it finds one or the scan is
  283. * canceled. The @p timeout parameter specifies the time-out of the scan. If it is
  284. * set to a value greater than 0, the function exits with a failure if either the maximum number
  285. * of retries or the time-out has been reached. If the @p timeout parameter is set to 0,
  286. * a single scan is performed. When the ISO14443A target is detected, the
  287. * PN532 module initializes communication and reads the target's UID.
  288. *
  289. * @param[in] card_baudrate Baud rate of the card.
  290. * @param[out] p_uid Pointer to the array that will be populated
  291. * with the card's UID (up to 7 bytes).
  292. * @param[in,out] p_uid_len Pointer to the variable that stores
  293. * the length of the p_uid buffer (as input)
  294. * and the length of the target's UID that
  295. * was read (as output).
  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_read_passive_target_id(uint8_t card_baudrate,
  305. uint8_t * p_uid,
  306. uint8_t * p_uid_len,
  307. uint16_t timeout);
  308. /** @brief Function for exchanging an Application Protocol Data Unit (APDU) with the currently enlisted peer.
  309. *
  310. * @param[in] p_send Pointer to the data to send.
  311. * @param[in] send_len Length of the data to send.
  312. * @param[out] p_response Pointer to the buffer for response data.
  313. * @param[in,out] p_response_len Pointer to the variable that stores
  314. * the length of the p_response buffer (as
  315. * input) and the length of the response data
  316. * (as output).
  317. *
  318. * @retval NRF_SUCCESS If the function completed successfully. Otherwise,
  319. * an error code is returned.
  320. */
  321. ret_code_t adafruit_pn532_in_data_exchange(uint8_t * p_send,
  322. uint8_t send_len,
  323. uint8_t * p_response,
  324. uint8_t * p_response_len);
  325. /** @brief Function for setting the MxRtyPassiveActivation parameter of the RFConfiguration register.
  326. *
  327. * This function sets the maximum number of retries when scanning for a tag.
  328. * The default is an unlimited number of retries.
  329. *
  330. * @param[in] max_retries 0xFF to wait forever. 0x00..0xFE to time out
  331. * after the specified number of retries.
  332. *
  333. * @retval NRF_SUCCESS If MxRtyPassiveActivation was set successfully. Otherwise,
  334. * an error code is returned.
  335. */
  336. ret_code_t adafruit_pn532_set_passive_activation_retries(uint8_t max_retries);
  337. /** @} */
  338. /**
  339. * @name NTAG2xx functions
  340. *
  341. * @{ */
  342. /** @brief Function for reading an entire 4-byte page at the specified address.
  343. *
  344. * This function reads 4 bytes from the chosen page.
  345. *
  346. * @param[in] page The page 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_ntag2xx_read_page(uint8_t page, uint8_t * p_buffer);
  354. /** @brief Function for writing an entire 4-byte page at the specified block address.
  355. *
  356. * This function writes a 4-byte sequence to the specified page.
  357. *
  358. * @param[in] page The page number to write (0..63 in most cases).
  359. * @param[in] p_data The uint8_t array that contains the data to write.
  360. * The data should be exactly 4 bytes long.
  361. *
  362. * @retval NRF_SUCCESS If the data was written successfully. Otherwise,
  363. * an error code is returned.
  364. */
  365. ret_code_t adafruit_pn532_ntag2xx_write_page(uint8_t page, uint8_t * p_data);
  366. /** @brief Function for writing an NDEF URI record starting at the specified page (4..nn).
  367. *
  368. * This function assumes that the NTAG2xx card is already formatted to work as an NFC Forum Tag.
  369. *
  370. * @param[in] uri_id The URI identifier code (0 = none, 0x01 =
  371. * "http://www.", and so on).
  372. * @param[in] p_url The URI text to write (null-terminated string).
  373. * @param[in] data_len The maximum number of bytes that can be stored in the
  374. * target device.
  375. *
  376. * @retval NRF_SUCCESS If the record was written successfully. Otherwise,
  377. * an error code is returned.
  378. */
  379. ret_code_t adafruit_pn532_ntag2xx_write_ndef_uri(uint8_t uri_id, char * p_url, uint8_t data_len);
  380. /** @} */
  381. /**
  382. * @name Functions for displaying formatted text
  383. *
  384. * @{ */
  385. /** @brief Function for printing data in hexadecimal format.
  386. *
  387. * @param[in] p_data Pointer to the first byte of data to be printed.
  388. * @param[in] len Data length in bytes.
  389. */
  390. void print_hex(const uint8_t * p_data, const uint32_t len);
  391. /** @brief Function for printing a character in hexadecimal format.
  392. *
  393. * This function prints a hexadecimal value along with
  394. * the char equivalents in the following format:
  395. *
  396. * 00 00 00 00 00 00 ......
  397. *
  398. * @param[in] p_data Pointer to the first byte of data to be printed.
  399. * @param[in] len Data length in bytes.
  400. */
  401. void print_hex_char(const uint8_t * p_data, const uint32_t len);
  402. /** @} */
  403. /**
  404. * @name Low-level communication functions that utilize I2C and GPIO
  405. *
  406. * @{ */
  407. /** @brief Function for checking PN532 Shield readiness.
  408. *
  409. * @retval True If the PN532 Shield is ready with a response.
  410. * @retval False Otherwise.
  411. */
  412. bool adafruit_pn532_is_ready(void);
  413. /** @brief Function for waiting until the PN532 Shield is ready.
  414. *
  415. * @param[in] timeout Time-out (in ms) before giving up.
  416. *
  417. * @retval True If the PN532 Shield is ready.
  418. * @retval False Otherwise.
  419. */
  420. bool adafruit_pn532_waitready_ms(uint16_t timeout);
  421. /** @brief Function for reading the ACK frame.
  422. *
  423. * @retval NRF_SUCCESS If the ACK frame was read. Otherwise, an error code is returned.
  424. */
  425. ret_code_t adafruit_pn532_read_ack(void);
  426. /** @brief Function for reading n bytes of data from the PN532 Shield via I2C.
  427. *
  428. * @param[out] p_buff Pointer to the buffer where the data will be written.
  429. * @param[in] n Number of bytes to read.
  430. *
  431. * @retval NRF_SUCCESS If the data was read successfully. Otherwise,
  432. * an error code is returned.
  433. */
  434. ret_code_t adafruit_pn532_read_data(uint8_t * p_buff, uint8_t n);
  435. /** @brief Function for writing a command to the PN532 Shield.
  436. *
  437. * This function writes a command to the PN532 Shield and automatically inserts
  438. * the preamble and required frame details (such as checksum, length, ...)
  439. *
  440. * @param[in] p_cmd Pointer to the command buffer.
  441. * @param[in] cmd_len Command length in bytes.
  442. *
  443. * @retval NRF_SUCCESS If the command was written successfully. Otherwise,
  444. * an error code is returned.
  445. */
  446. ret_code_t adafruit_pn532_write_command(uint8_t * p_cmd, uint8_t cmd_len);
  447. /** @} */
  448. /**
  449. *@}
  450. **/
  451. #endif