Advertisement
Guest User

Untitled

a guest
Dec 16th, 2022
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. /**************************************************************************/
  2. /*!
  3. @file PN532.c
  4. @author Luca Faccin
  5. @license BSD (see license.txt)
  6.  
  7. This is a port of the Adafruit PN532 Driver for the ESP32 using only the I2C Bus
  8. Driver for NXP's PN532 NFC/13.56MHz RFID Transceiver
  9.  
  10. @section HISTORY
  11. v 1.0 Basic port of the v 2.1 of the Adafruit PN532 Driver
  12. */
  13. /**************************************************************************/
  14.  
  15. #ifndef PN532_H
  16. #define PN532_H
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdbool.h>
  21. #include "esp_system.h"
  22. #include "freertos/FreeRTOS.h"
  23. #include "freertos/event_groups.h"
  24. #include "driver/i2c.h"
  25.  
  26. #define PN532_PREAMBLE (0x00)
  27. #define PN532_STARTCODE1 (0x00)
  28. #define PN532_STARTCODE2 (0xFF)
  29. #define PN532_POSTAMBLE (0x00)
  30.  
  31. #define PN532_HOSTTOPN532 (0xD4)
  32. #define PN532_PN532TOHOST (0xD5)
  33.  
  34. // PN532 Commands
  35. #define PN532_COMMAND_DIAGNOSE (0x00)
  36. #define PN532_COMMAND_GETFIRMWAREVERSION (0x02)
  37. #define PN532_COMMAND_GETGENERALSTATUS (0x04)
  38. #define PN532_COMMAND_READREGISTER (0x06)
  39. #define PN532_COMMAND_WRITEREGISTER (0x08)
  40. #define PN532_COMMAND_READGPIO (0x0C)
  41. #define PN532_COMMAND_WRITEGPIO (0x0E)
  42. #define PN532_COMMAND_SETSERIALBAUDRATE (0x10)
  43. #define PN532_COMMAND_SETPARAMETERS (0x12)
  44. #define PN532_COMMAND_SAMCONFIGURATION (0x14)
  45. #define PN532_COMMAND_POWERDOWN (0x16)
  46. #define PN532_COMMAND_RFCONFIGURATION (0x32)
  47. #define PN532_COMMAND_RFREGULATIONTEST (0x58)
  48. #define PN532_COMMAND_INJUMPFORDEP (0x56)
  49. #define PN532_COMMAND_INJUMPFORPSL (0x46)
  50. #define PN532_COMMAND_INLISTPASSIVETARGET (0x4A)
  51. #define PN532_COMMAND_INATR (0x50)
  52. #define PN532_COMMAND_INPSL (0x4E)
  53. #define PN532_COMMAND_INDATAEXCHANGE (0x40)
  54. #define PN532_COMMAND_INCOMMUNICATETHRU (0x42)
  55. #define PN532_COMMAND_INDESELECT (0x44)
  56. #define PN532_COMMAND_INRELEASE (0x52)
  57. #define PN532_COMMAND_INSELECT (0x54)
  58. #define PN532_COMMAND_INAUTOPOLL (0x60)
  59. #define PN532_COMMAND_TGINITASTARGET (0x8C)
  60. #define PN532_COMMAND_TGSETGENERALBYTES (0x92)
  61. #define PN532_COMMAND_TGGETDATA (0x86)
  62. #define PN532_COMMAND_TGSETDATA (0x8E)
  63. #define PN532_COMMAND_TGSETMETADATA (0x94)
  64. #define PN532_COMMAND_TGGETINITIATORCOMMAND (0x88)
  65. #define PN532_COMMAND_TGRESPONSETOINITIATOR (0x90)
  66. #define PN532_COMMAND_TGGETTARGETSTATUS (0x8A)
  67.  
  68. #define PN532_RESPONSE_INDATAEXCHANGE (0x41)
  69. #define PN532_RESPONSE_INLISTPASSIVETARGET (0x4B)
  70.  
  71. #define PN532_WAKEUP (0x55)
  72.  
  73. #define PN532_SPI_STATREAD (0x02)
  74. #define PN532_SPI_DATAWRITE (0x01)
  75. #define PN532_SPI_DATAREAD (0x03)
  76. #define PN532_SPI_READY (0x01)
  77.  
  78. #define PN532_I2C_ADDRESS (0x48)
  79. #define PN532_I2C_READ_ADDRESS (0x49)
  80. #define PN532_I2C_READBIT (0x01)
  81. #define PN532_I2C_BUSY (0x00)
  82. #define PN532_I2C_READY (0x01)
  83. #define PN532_I2C_READYTIMEOUT (20)
  84.  
  85. #define PN532_MIFARE_ISO14443A (0x00)
  86.  
  87. // Mifare Commands
  88. #define MIFARE_CMD_AUTH_A (0x60)
  89. #define MIFARE_CMD_AUTH_B (0x61)
  90. #define MIFARE_CMD_READ (0x30)
  91. #define MIFARE_CMD_WRITE (0xA0)
  92. #define MIFARE_CMD_TRANSFER (0xB0)
  93. #define MIFARE_CMD_DECREMENT (0xC0)
  94. #define MIFARE_CMD_INCREMENT (0xC1)
  95. #define MIFARE_CMD_STORE (0xC2)
  96. #define MIFARE_ULTRALIGHT_CMD_WRITE (0xA2)
  97.  
  98. // Prefixes for NDEF Records (to identify record type)
  99. #define NDEF_URIPREFIX_NONE (0x00)
  100. #define NDEF_URIPREFIX_HTTP_WWWDOT (0x01)
  101. #define NDEF_URIPREFIX_HTTPS_WWWDOT (0x02)
  102. #define NDEF_URIPREFIX_HTTP (0x03)
  103. #define NDEF_URIPREFIX_HTTPS (0x04)
  104. #define NDEF_URIPREFIX_TEL (0x05)
  105. #define NDEF_URIPREFIX_MAILTO (0x06)
  106. #define NDEF_URIPREFIX_FTP_ANONAT (0x07)
  107. #define NDEF_URIPREFIX_FTP_FTPDOT (0x08)
  108. #define NDEF_URIPREFIX_FTPS (0x09)
  109. #define NDEF_URIPREFIX_SFTP (0x0A)
  110. #define NDEF_URIPREFIX_SMB (0x0B)
  111. #define NDEF_URIPREFIX_NFS (0x0C)
  112. #define NDEF_URIPREFIX_FTP (0x0D)
  113. #define NDEF_URIPREFIX_DAV (0x0E)
  114. #define NDEF_URIPREFIX_NEWS (0x0F)
  115. #define NDEF_URIPREFIX_TELNET (0x10)
  116. #define NDEF_URIPREFIX_IMAP (0x11)
  117. #define NDEF_URIPREFIX_RTSP (0x12)
  118. #define NDEF_URIPREFIX_URN (0x13)
  119. #define NDEF_URIPREFIX_POP (0x14)
  120. #define NDEF_URIPREFIX_SIP (0x15)
  121. #define NDEF_URIPREFIX_SIPS (0x16)
  122. #define NDEF_URIPREFIX_TFTP (0x17)
  123. #define NDEF_URIPREFIX_BTSPP (0x18)
  124. #define NDEF_URIPREFIX_BTL2CAP (0x19)
  125. #define NDEF_URIPREFIX_BTGOEP (0x1A)
  126. #define NDEF_URIPREFIX_TCPOBEX (0x1B)
  127. #define NDEF_URIPREFIX_IRDAOBEX (0x1C)
  128. #define NDEF_URIPREFIX_FILE (0x1D)
  129. #define NDEF_URIPREFIX_URN_EPC_ID (0x1E)
  130. #define NDEF_URIPREFIX_URN_EPC_TAG (0x1F)
  131. #define NDEF_URIPREFIX_URN_EPC_PAT (0x20)
  132. #define NDEF_URIPREFIX_URN_EPC_RAW (0x21)
  133. #define NDEF_URIPREFIX_URN_EPC (0x22)
  134. #define NDEF_URIPREFIX_URN_NFC (0x23)
  135.  
  136. #define PN532_GPIO_VALIDATIONBIT (0x80)
  137. #define PN532_GPIO_P30 (0)
  138. #define PN532_GPIO_P31 (1)
  139. #define PN532_GPIO_P32 (2)
  140. #define PN532_GPIO_P33 (3)
  141. #define PN532_GPIO_P34 (4)
  142. #define PN532_GPIO_P35 (5)
  143.  
  144.  
  145. #define I2C_WRITE_TIMEOUT 1000 //ms
  146. #define I2C_READ_TIMEOUT 1000 //ms
  147. #define IRQ_WAIT_TIMEOUT 1000 //ms
  148.  
  149. //Initialize the I2C the the reset/IRQ pint
  150. bool init_PN532_I2C(uint8_t sda, uint8_t scl,uint8_t reset,uint8_t irq,i2c_port_t i2c_port_number);
  151.  
  152. // Generic PN532 functions
  153. bool SAMConfig(void);
  154. uint32_t getPN532FirmwareVersion(void);
  155. bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout);
  156. bool writeGPIO(uint8_t pinstate);
  157. uint8_t readGPIO(void);
  158. bool setPassiveActivationRetries(uint8_t maxRetries);
  159.  
  160. // ISO14443A functions
  161. bool readPassiveTargetID(uint8_t cardbaudrate, uint8_t * uid, uint8_t * uidLength, uint16_t timeout); //timeout 0 means no timeout - will block forever.
  162. bool inDataExchange(uint8_t * send, uint8_t sendLength, uint8_t * response, uint8_t * responseLength);
  163. bool inListPassiveTarget();
  164.  
  165. // Mifare Classic functions
  166. bool mifareclassic_IsFirstBlock (uint32_t uiBlock);
  167. bool mifareclassic_IsTrailerBlock (uint32_t uiBlock);
  168. uint8_t mifareclassic_AuthenticateBlock (uint8_t * uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t * keyData);
  169. uint8_t mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t * data);
  170. uint8_t mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t * data);
  171. uint8_t mifareclassic_FormatNDEF (void);
  172. uint8_t mifareclassic_WriteNDEFURI (uint8_t sectorNumber, uint8_t uriIdentifier, const char * url);
  173.  
  174. // Mifare Ultralight functions
  175. uint8_t mifareultralight_ReadPage (uint8_t page, uint8_t * buffer);
  176. uint8_t mifareultralight_WritePage (uint8_t page, uint8_t * data);
  177.  
  178. // NTAG2xx functions
  179. uint8_t ntag2xx_ReadPage (uint8_t page, uint8_t * buffer);
  180. uint8_t ntag2xx_WritePage (uint8_t page, uint8_t * data);
  181. uint8_t ntag2xx_WriteNDEFURI (uint8_t uriIdentifier, char * url, uint8_t dataLen);
  182.  
  183.  
  184. //Target command
  185. bool initiate_as_target_106();
  186.  
  187. #endif
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement