rc522.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #ifndef __RC522_H__
  2. #define __RC522_H__
  3. #include "iocc2530.h"
  4. #include "public.h"
  5. //#define IC_SDA P2_0
  6. //#define IC_SCK P0_7
  7. //#define IC_MOSI P0_6
  8. //#define IC_MISO P0_5
  9. //#define IC_RST P0_4
  10. #define IC_SDA_PIN P2_0 //数据引脚
  11. #define IC_SDA_NUM GPIO_0
  12. #define IC_SDA_PORT P2DIR //设置方向 输入还是输出
  13. #define IC_SDA_PIN_MODE P2INP //设置输入模式 上拉下拉 三态 这里用三态 P10P11不能设置输入模式
  14. //引脚配置为输出;
  15. #define IC_SDA_PIN_OUT() { IC_SDA_PORT |= IC_SDA_NUM; asm("NOP");IC_SDA_PIN_MODE&=~IC_SDA_NUM; }
  16. #define IC_SCK_PIN P1_7 //数据引脚
  17. #define IC_SCK_NUM GPIO_7
  18. #define IC_SCK_PORT P1DIR //设置方向 输入还是输出
  19. #define IC_SCK_PIN_MODE P1INP //设置输入模式 上拉下拉 三态 这里用三态 P10P11不能设置输入模式
  20. //引脚配置为输出;
  21. #define IC_SCK_PIN_OUT() { IC_SCK_PORT |= IC_SCK_NUM; asm("NOP");IC_SCK_PIN_MODE&=~IC_SCK_NUM; }
  22. #define IC_MOSI_PIN P1_6 //数据引脚
  23. #define IC_MOSI_NUM GPIO_6
  24. #define IC_MOSI_PORT P1DIR //设置方向 输入还是输出
  25. #define IC_MOSI_PIN_MODE P1INP //设置输入模式 上拉下拉 三态 这里用三态 P10P11不能设置输入模式
  26. //引脚配置为输出;
  27. #define IC_MOSI_PIN_OUT() { IC_MOSI_PORT |= IC_MOSI_NUM; asm("NOP");IC_MOSI_PIN_MODE&=~IC_MOSI_NUM; }
  28. #define IC_MISO_PIN P1_5 //数据引脚
  29. #define IC_MISO_NUM GPIO_5
  30. #define IC_MISO_PORT P1DIR //设置方向 输入还是输出
  31. #define IC_MISO_PIN_MODE P1INP //设置输入模式 上拉下拉 三态 这里用三态 P10P11不能设置输入模式
  32. //引脚配置为输出;
  33. #define IC_MISO_PIN_OUT() { IC_MISO_PORT |= IC_MISO_NUM; asm("NOP");IC_MISO_PIN_MODE&=~IC_MISO_NUM; }
  34. #define IC_RST_PIN P1_4 //数据引脚
  35. #define IC_RST_NUM GPIO_4
  36. #define IC_RST_PORT P1DIR //设置方向 输入还是输出
  37. #define IC_RST_PIN_MODE P1INP //设置输入模式 上拉下拉 三态 这里用三态 P10P11不能设置输入模式
  38. //引脚配置为输出;
  39. #define IC_RST_PIN_IN() { IC_RST_PORT &=~IC_RST_NUM; asm("NOP");IC_RST_PIN_MODE&=~IC_RST_NUM; }
  40. char PcdRead(unsigned char addr,unsigned char *pData);
  41. void SPIWriteByte(unsigned char infor);
  42. unsigned char SPIReadByte(void);
  43. unsigned char ReadRawRC(unsigned char Address);
  44. void WriteRawRC(unsigned char Address, unsigned char value);
  45. void SetBitMask(unsigned char reg,unsigned char mask) ;
  46. void ClearBitMask(unsigned char reg,unsigned char mask) ;
  47. void PcdAntennaOn(void);
  48. void PcdAntennaOff(void);
  49. void PcdReset(void);
  50. void M500PcdConfigISOType(unsigned char type);
  51. char PcdComMF522(unsigned char Command, //RC522命令字
  52. unsigned char *pInData, //通过RC522发送到卡片的数据
  53. unsigned char InLenByte, //发送数据的字节长度
  54. unsigned char *pOutData, //接收到的卡片返回数据
  55. unsigned int *pOutLenBit) ;
  56. char PcdRequest(unsigned char req_code,unsigned char *pTagType);
  57. char PcdAnticoll(unsigned char *pSnr);
  58. void CalulateCRC(unsigned char *pIndata,unsigned char len,unsigned char *pOutData);
  59. char PcdSelect(unsigned char *pSnr);
  60. char PcdAuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr);
  61. char PcdWrite(unsigned char addr,unsigned char *pData);
  62. char PcdRead(unsigned char addr,unsigned char *pData);
  63. char PcdHalt(void);
  64. extern void PcdReset(void);//复位
  65. extern void M500PcdConfigISOType(unsigned char type);//工作方式
  66. extern char PcdRequest(unsigned char req_code,unsigned char *pTagType);//寻卡
  67. extern char PcdAnticoll(unsigned char *pSnr);//读卡号
  68. extern void RC522_HAL_Init(void);
  69. /////////////////////////////////////////////////////////////////////
  70. //MF522命令字
  71. /////////////////////////////////////////////////////////////////////
  72. #define PCD_IDLE 0x00 //取消当前命令
  73. #define PCD_AUTHENT 0x0E //验证密钥
  74. #define PCD_RECEIVE 0x08 //接收数据
  75. #define PCD_TRANSMIT 0x04 //发送数据
  76. #define PCD_TRANSCEIVE 0x0C //发送并接收数据
  77. #define PCD_RESETPHASE 0x0F //复位
  78. #define PCD_CALCCRC 0x03 //CRC计算
  79. /////////////////////////////////////////////////////////////////////
  80. //Mifare_One卡片命令字
  81. /////////////////////////////////////////////////////////////////////
  82. #define PICC_REQIDL 0x26 //寻天线区内未进入休眠状态
  83. #define PICC_REQALL 0x52 //寻天线区内全部卡
  84. #define PICC_ANTICOLL1 0x93 //防冲撞
  85. #define PICC_ANTICOLL2 0x95 //防冲撞
  86. #define PICC_AUTHENT1A 0x60 //验证A密钥
  87. #define PICC_AUTHENT1B 0x61 //验证B密钥
  88. #define PICC_READ 0x30 //读块
  89. #define PICC_WRITE 0xA0 //写块
  90. #define PICC_DECREMENT 0xC0 //扣款
  91. #define PICC_INCREMENT 0xC1 //充值
  92. #define PICC_RESTORE 0xC2 //调块数据到缓冲区
  93. #define PICC_TRANSFER 0xB0 //保存缓冲区中数据
  94. #define PICC_HALT 0x50 //休眠
  95. /////////////////////////////////////////////////////////////////////
  96. //MF522 FIFO长度定义
  97. /////////////////////////////////////////////////////////////////////
  98. #define DEF_FIFO_LENGTH 64 //FIFO size=64byte
  99. #define MAXRLEN 18
  100. /////////////////////////////////////////////////////////////////////
  101. //MF522寄存器定义
  102. /////////////////////////////////////////////////////////////////////
  103. // PAGE 0
  104. #define RFU00 0x00
  105. #define CommandReg 0x01
  106. #define ComIEnReg 0x02
  107. #define DivlEnReg 0x03
  108. #define ComIrqReg 0x04
  109. #define DivIrqReg 0x05
  110. #define ErrorReg 0x06
  111. #define Status1Reg 0x07
  112. #define Status2Reg 0x08
  113. #define FIFODataReg 0x09
  114. #define FIFOLevelReg 0x0A
  115. #define WaterLevelReg 0x0B
  116. #define ControlReg 0x0C
  117. #define BitFramingReg 0x0D
  118. #define CollReg 0x0E
  119. #define RFU0F 0x0F
  120. // PAGE 1
  121. #define RFU10 0x10
  122. #define ModeReg 0x11
  123. #define TxModeReg 0x12
  124. #define RxModeReg 0x13
  125. #define TxControlReg 0x14
  126. #define TxAutoReg 0x15
  127. #define TxSelReg 0x16
  128. #define RxSelReg 0x17
  129. #define RxThresholdReg 0x18
  130. #define DemodReg 0x19
  131. #define RFU1A 0x1A
  132. #define RFU1B 0x1B
  133. #define MifareReg 0x1C
  134. #define RFU1D 0x1D
  135. #define RFU1E 0x1E
  136. #define SerialSpeedReg 0x1F
  137. // PAGE 2
  138. #define RFU20 0x20
  139. #define CRCResultRegM 0x21
  140. #define CRCResultRegL 0x22
  141. #define RFU23 0x23
  142. #define ModWidthReg 0x24
  143. #define RFU25 0x25
  144. #define RFCfgReg 0x26
  145. #define GsNReg 0x27
  146. #define CWGsCfgReg 0x28
  147. #define ModGsCfgReg 0x29
  148. #define TModeReg 0x2A
  149. #define TPrescalerReg 0x2B
  150. #define TReloadRegH 0x2C
  151. #define TReloadRegL 0x2D
  152. #define TCounterValueRegH 0x2E
  153. #define TCounterValueRegL 0x2F
  154. // PAGE 3
  155. #define RFU30 0x30
  156. #define TestSel1Reg 0x31
  157. #define TestSel2Reg 0x32
  158. #define TestPinEnReg 0x33
  159. #define TestPinValueReg 0x34
  160. #define TestBusReg 0x35
  161. #define AutoTestReg 0x36
  162. #define VersionReg 0x37
  163. #define AnalogTestReg 0x38
  164. #define TestDAC1Reg 0x39
  165. #define TestDAC2Reg 0x3A
  166. #define TestADCReg 0x3B
  167. #define RFU3C 0x3C
  168. #define RFU3D 0x3D
  169. #define RFU3E 0x3E
  170. #define RFU3F 0x3F
  171. /////////////////////////////////////////////////////////////////////
  172. //和MF522通讯时返回的错误代码
  173. /////////////////////////////////////////////////////////////////////
  174. #define MI_OK 0x26
  175. #define MI_NOTAGERR 0xcc
  176. #define MI_ERR 0xbb
  177. #endif