user_nfc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. #include "user_nfc.h"
  2. #include "user_spi.h"
  3. #include "lpcd.h"
  4. static nfc_work_s nfc_work = {
  5. .work_type = NFC_TYPE_RST_START,
  6. .nfc_delay_count = 0,
  7. .left_init_stat = false,
  8. .right_init_stat = false,
  9. .left_loss_delay = 0,
  10. .right_loss_delay = 0,
  11. .left_ver = 0,
  12. .left_ID = 0,
  13. .right_ver = 0,
  14. .right_ID = 0,
  15. .left_card_stat = NFC_CARD_NULL,
  16. .right_card_stat = NFC_CARD_NULL,
  17. .left_idA = {0},
  18. .right_idA = {0},
  19. .left_idB = {0},
  20. .right_idB = {0},
  21. .left_charge_stat = false,
  22. .right_charge_stat = false,
  23. };
  24. static unsigned char uid_length = 0;
  25. static unsigned char CT[2];//卡类型
  26. static unsigned char IDA[15];//存A卡ID
  27. static unsigned char PassWd[6]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
  28. static unsigned char RWDATA[16];
  29. static uint8_t nfc_cs_idx = 0;
  30. static vUser_nfc_upload_card_stat_func xUpload_card_stat = NULL;
  31. tmosTaskID user_nfc_task_id = INVALID_TASK_ID;
  32. static uint16_t user_nfc_task_process_event(uint8_t task_id, uint16_t events);
  33. /*******************************************************************************
  34. * @函数名称 user_nfc_gpio_init
  35. * @函数说明 GPIO初始化
  36. * @输入参数 无
  37. * @输出参数 无
  38. * @返回参数 无
  39. *******************************************************************************/
  40. static void user_nfc_gpio_init(void)
  41. {
  42. GPIO_InitTypeDef GPIO_InitStructure = {0};
  43. USER_NFC_GPIO_RCC_ENABLE;
  44. //输出端口初始化
  45. GPIO_InitStructure.GPIO_Pin = LEFT_CS_PIN;
  46. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  47. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  48. GPIO_Init(LEFT_CS_GPIO, &GPIO_InitStructure);
  49. LEFT_CS_OFF;
  50. GPIO_InitStructure.GPIO_Pin = RIGHT_CS_PIN;
  51. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  52. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  53. GPIO_Init(RIGHT_CS_GPIO, &GPIO_InitStructure);
  54. RIGHT_CS_OFF;
  55. GPIO_InitStructure.GPIO_Pin = LEFT_RST_PIN;
  56. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  57. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  58. GPIO_Init(LEFT_RST_GPIO, &GPIO_InitStructure);
  59. LEFT_RST_DISABLE;
  60. GPIO_InitStructure.GPIO_Pin = RIGHT_RST_PIN;
  61. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  62. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  63. GPIO_Init(RIGHT_RST_GPIO, &GPIO_InitStructure);
  64. RIGHT_RST_DISABLE;
  65. //输入端口初始化
  66. GPIO_InitStructure.GPIO_Pin = LEFT_IRQ_PIN;
  67. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  68. GPIO_Init(LEFT_IRQ_GPIO, &GPIO_InitStructure);
  69. GPIO_InitStructure.GPIO_Pin = RIGHT_IRQ_PIN;
  70. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  71. GPIO_Init(RIGHT_IRQ_GPIO, &GPIO_InitStructure);
  72. }
  73. /*********************************************************************************************************
  74. ** Function name: NFC_HardReset
  75. ** Descriptions: FM175xx硬件复位
  76. ** input parameters: N/A
  77. ** output parameters: N/A
  78. ** Returned value: true:操作成功 ERROR:操作失败
  79. *********************************************************************************************************/
  80. void NFC_HardReset(void)
  81. {
  82. if(nfc_cs_idx == 0)
  83. {
  84. LEFT_RST_ENABLE;
  85. }
  86. else{
  87. RIGHT_RST_ENABLE;
  88. }
  89. Delay_Ms(1);
  90. if(nfc_cs_idx == 0)
  91. {
  92. LEFT_RST_DISABLE;
  93. }
  94. else{
  95. RIGHT_RST_DISABLE;
  96. }
  97. Delay_Ms(1);
  98. }
  99. /*********************************************************************************************************
  100. ** Function name: NFC_HPD
  101. ** Descriptions: 硬件低功耗操作
  102. ** input parameters: mode = 1 退出HPD模式 ,mode = 0 进入HPD模式
  103. ** output parameters: N/A
  104. ** Returned value: NONE
  105. *********************************************************************************************************/
  106. void NFC_HardPowerdown(uint8_t mode)
  107. {
  108. if(mode == 0x00)
  109. {
  110. if(nfc_cs_idx == 0)
  111. {
  112. LEFT_RST_ENABLE;
  113. }
  114. else{
  115. RIGHT_RST_ENABLE;
  116. } /* 读卡芯片进入硬件低功耗 */
  117. }
  118. else{
  119. if(nfc_cs_idx == 0)
  120. {
  121. LEFT_RST_DISABLE;
  122. }
  123. else{
  124. RIGHT_RST_DISABLE;
  125. } /* 读卡芯片退出硬件低功耗 */
  126. }
  127. }
  128. /*******************************************************************************
  129. * @函数名称 ReadRawRC
  130. * @函数说明 读寄存器
  131. * @输入参数 无
  132. * @输出参数 无
  133. * @返回参数 无
  134. *****************************************************************************/
  135. unsigned char ReadRawRC(unsigned char Address)
  136. {
  137. unsigned char ucAddr;
  138. unsigned char ucResult=0;
  139. ucAddr = ((Address<<1)&0x7E)|0x80;
  140. if(nfc_cs_idx == 0)
  141. {
  142. LEFT_CS_ON;
  143. }
  144. else{
  145. RIGHT_CS_ON;
  146. }
  147. SPI_ReadWriteByte(ucAddr);//向总线写多个数据
  148. //SPI_WriteNBytes(SPI1,&ucAddr,1);
  149. ucResult = SPI_ReadWriteByte(0xFF);//向总线读多个数据
  150. //SPI_ReadNBytes(SPI1,&ucResult,1);
  151. if(nfc_cs_idx == 0)
  152. {
  153. LEFT_CS_OFF;
  154. }
  155. else{
  156. RIGHT_CS_OFF;
  157. }
  158. return ucResult;
  159. }
  160. /*******************************************************************************
  161. * @函数名称 WriteRawRC
  162. * @函数说明 写寄存器
  163. * @输入参数 无
  164. * @输出参数 无
  165. * @返回参数 无
  166. *****************************************************************************/
  167. void WriteRawRC(unsigned char Address, unsigned char value)
  168. {
  169. unsigned char ucAddr;
  170. uint8_t write_buffer[2]={0};
  171. ucAddr = ((Address<<1)&0x7E);
  172. if(nfc_cs_idx == 0)
  173. {
  174. LEFT_CS_ON;
  175. }
  176. else{
  177. RIGHT_CS_ON;
  178. }
  179. SPI_ReadWriteByte(ucAddr);//向总线写多个数据
  180. SPI_ReadWriteByte(value);//向总线写多个数据
  181. if(nfc_cs_idx == 0)
  182. {
  183. LEFT_CS_OFF;
  184. }
  185. else{
  186. RIGHT_CS_OFF;
  187. }
  188. }
  189. /*******************************************************************************
  190. * @函数名称 SetBitMask
  191. * @函数说明 设置位
  192. * @输入参数 无
  193. * @输出参数 无
  194. * @返回参数 无
  195. *****************************************************************************/
  196. void SetBitMask(unsigned char reg,unsigned char mask)
  197. {
  198. char tmp = 0x0;
  199. tmp = ReadRawRC(reg);
  200. WriteRawRC(reg,tmp | mask); // set bit mask
  201. }
  202. /*******************************************************************************
  203. * @函数名称 ClearBitMask
  204. * @函数说明 清除位
  205. * @输入参数 无
  206. * @输出参数 无
  207. * @返回参数 无
  208. *****************************************************************************/
  209. void ClearBitMask(unsigned char reg,unsigned char mask)
  210. {
  211. char tmp = 0x0;
  212. tmp = ReadRawRC(reg);
  213. WriteRawRC(reg, tmp & ~mask); // clear bit mask
  214. }
  215. /*******************************************************************************
  216. * @函数名称 Card_Check
  217. * @函数说明 检测卡片类型以及卡片的信息
  218. * @输入参数 无
  219. * @输出参数 无
  220. * @返回参数 无
  221. *****************************************************************************/
  222. static bool Card_Check(void)
  223. {
  224. bool stat = false;
  225. stat = TyteA_Read(CT, IDA);
  226. if(stat == true)
  227. {
  228. if(LEFT_TYPE_CARD_CHECK == nfc_work.work_type)
  229. {
  230. memset(nfc_work.left_idA, 0, 8);
  231. if(CT[0] == 0x04 && CT[1] == 0x00)
  232. {
  233. nfc_work.left_idA[4] = IDA[0];
  234. nfc_work.left_idA[5] = IDA[1];
  235. nfc_work.left_idA[6] = IDA[2];
  236. nfc_work.left_idA[7] = IDA[3];
  237. }
  238. else if(CT[0] == 0x44 && CT[1] == 0x00){
  239. nfc_work.left_idA[1] = IDA[1];
  240. nfc_work.left_idA[2] = IDA[2];
  241. nfc_work.left_idA[3] = IDA[3];
  242. nfc_work.left_idA[4] = IDA[5];
  243. nfc_work.left_idA[5] = IDA[6];
  244. nfc_work.left_idA[6] = IDA[7];
  245. nfc_work.left_idA[7] = IDA[8];
  246. }
  247. }
  248. else if(RIGHT_TYPE_CARD_CHECK == nfc_work.work_type)
  249. {
  250. memset(nfc_work.right_idA, 0, 8);
  251. if(CT[0] == 0x04 && CT[1] == 0x00)
  252. {
  253. nfc_work.right_idA[4] = IDA[0];
  254. nfc_work.right_idA[5] = IDA[1];
  255. nfc_work.right_idA[6] = IDA[2];
  256. nfc_work.right_idA[7] = IDA[3];
  257. }
  258. else if(CT[0] == 0x44 && CT[1] == 0x00){
  259. nfc_work.right_idA[1] = IDA[1];
  260. nfc_work.right_idA[2] = IDA[2];
  261. nfc_work.right_idA[3] = IDA[3];
  262. nfc_work.right_idA[4] = IDA[5];
  263. nfc_work.right_idA[5] = IDA[6];
  264. nfc_work.right_idA[6] = IDA[7];
  265. nfc_work.right_idA[7] = IDA[8];
  266. }
  267. }
  268. }
  269. return stat;
  270. }
  271. /*******************************************************************************
  272. * @函数名称 user_nfc_task
  273. * @函数说明 任务执行程序
  274. * @输入参数 无
  275. * @输出参数 无
  276. * @返回参数 无
  277. *****************************************************************************/
  278. static void prvUser_nfc_upload_card_stat(uint8_t idx, bool stat, unsigned char *card)
  279. {
  280. PRINT("%s read card = %s,%02X%02X%02X%02X\n",(idx == LEFT_IDX)?"left":"right", (stat == true)?"true":"false",
  281. card[4], card[5], card[6], card[7]);
  282. if(xUpload_card_stat != NULL)
  283. {
  284. xUpload_card_stat(idx, stat, card);
  285. }
  286. }
  287. /*******************************************************************************
  288. * @函数名称 user_nfc_task
  289. * @函数说明 任务执行程序
  290. * @输入参数 无
  291. * @输出参数 无
  292. * @返回参数 无
  293. *****************************************************************************/
  294. static void user_nfc_task(void)
  295. {
  296. bool stat = false;
  297. if(nfc_work.nfc_delay_count > 0)
  298. {
  299. nfc_work.nfc_delay_count--;
  300. }
  301. else{
  302. switch(nfc_work.work_type)
  303. {
  304. case NFC_TYPE_RST_START:{
  305. PRINT("NFC_TYPE_RST_START\n");
  306. RIGHT_RST_DISABLE;
  307. LEFT_RST_DISABLE;
  308. Delay_Ms(2);
  309. RIGHT_RST_ENABLE;
  310. LEFT_RST_ENABLE;
  311. Delay_Ms(2);
  312. RIGHT_RST_DISABLE;
  313. LEFT_RST_DISABLE;
  314. nfc_work.nfc_delay_count = 2;
  315. nfc_work.work_type = NFC_TYPE_RST_END;
  316. }break;
  317. case NFC_TYPE_RST_END:{
  318. PRINT("NFC_TYPE_RST_END\n");
  319. nfc_work.nfc_delay_count = 2;
  320. nfc_work.work_type = LEFT_TYPE_INIT;
  321. }break;
  322. case LEFT_TYPE_INIT:{
  323. nfc_cs_idx = 0;
  324. uint8_t id = Read_Reg(VersionReg);
  325. PRINT("LEFT_TYPE_INIT,id=0x%02X\n", id);
  326. Lpcd_Init_Register(); /* 扩展寄存器初始化 */
  327. nfc_work.nfc_delay_count = 2;
  328. nfc_work.work_type = RIGHT_TYPE_INIT;
  329. nfc_work.left_init_stat = true;
  330. }break;
  331. case RIGHT_TYPE_INIT:{
  332. nfc_cs_idx = 1;
  333. uint8_t id = Read_Reg(VersionReg);
  334. PRINT("RIGHT_TYPE_INIT,id=0x%02X\n", id);
  335. Lpcd_Init_Register(); /* 扩展寄存器初始化 */
  336. nfc_work.nfc_delay_count = 2;
  337. nfc_work.work_type = NFC_CARD_CHECK_DELAY;
  338. nfc_work.right_init_stat = true;
  339. }break;
  340. case LEFT_TYPE_CARD_CHECK:{
  341. // PRINT("LEFT_TYPE_CARD_CHECK\n");
  342. nfc_cs_idx = 0;
  343. stat = Card_Check();
  344. if(nfc_work.right_charge_stat == false)
  345. {
  346. nfc_work.work_type = RIGHT_TYPE_CARD_CHECK;
  347. }
  348. else{
  349. nfc_work.work_type = NFC_CARD_CHECK_DELAY;
  350. }
  351. nfc_work.nfc_delay_count = 50;
  352. if(stat == true)
  353. {
  354. nfc_work.left_loss_delay = ID_LOSE_DELAY_COUNT;
  355. if(nfc_work.left_card_stat != NFC_CARD_IN)
  356. {
  357. nfc_work.left_card_stat = NFC_CARD_IN;
  358. prvUser_nfc_upload_card_stat(LEFT_IDX, true, nfc_work.left_idA);
  359. }
  360. }
  361. else{
  362. if(nfc_work.left_loss_delay > 0)
  363. {
  364. nfc_work.left_loss_delay--;
  365. }
  366. else{
  367. memset(nfc_work.left_idA, 0, 8);
  368. if(nfc_work.left_card_stat != NFC_CARD_NO)
  369. {
  370. nfc_work.left_card_stat = NFC_CARD_NO;
  371. prvUser_nfc_upload_card_stat(LEFT_IDX, false, nfc_work.left_idA);
  372. }
  373. }
  374. }
  375. }break;
  376. case RIGHT_TYPE_CARD_CHECK:{
  377. // PRINT("RIGHT_TYPE_CARD_CHECK\n");
  378. nfc_cs_idx = 1;
  379. stat = Card_Check();
  380. nfc_work.work_type = NFC_CARD_CHECK_DELAY;
  381. nfc_work.nfc_delay_count = 10;
  382. if(stat == true)
  383. {
  384. nfc_work.right_loss_delay = ID_LOSE_DELAY_COUNT;
  385. if(nfc_work.right_card_stat != NFC_CARD_IN)
  386. {
  387. nfc_work.right_card_stat = NFC_CARD_IN;
  388. prvUser_nfc_upload_card_stat(RIGHT_IDX, true, nfc_work.right_idA);
  389. }
  390. }
  391. else{
  392. if(nfc_work.right_loss_delay > 0)
  393. {
  394. nfc_work.right_loss_delay--;
  395. }
  396. else{
  397. memset(nfc_work.right_idA, 0, 8);
  398. if(nfc_work.right_card_stat != NFC_CARD_NO)
  399. {
  400. nfc_work.right_card_stat = NFC_CARD_NO;
  401. prvUser_nfc_upload_card_stat(RIGHT_IDX, false, nfc_work.right_idA);
  402. }
  403. }
  404. }
  405. }break;
  406. case NFC_CARD_CHECK_DELAY:{
  407. // PRINT("NFC_CARD_CHECK_DELAY\n");
  408. if(nfc_work.left_charge_stat == false)
  409. {
  410. nfc_work.work_type = LEFT_TYPE_CARD_CHECK;
  411. }
  412. else if(nfc_work.right_charge_stat == false)
  413. {
  414. nfc_work.work_type = RIGHT_TYPE_CARD_CHECK;
  415. }
  416. nfc_work.nfc_delay_count = 40;
  417. }break;
  418. default:
  419. break;
  420. }
  421. }
  422. }
  423. /*******************************************************************************
  424. * @函数名称 vUser_nfc_set_upload_key_stat_func
  425. * @函数说明 设置上报NFC读卡信息的回调函数
  426. * @输入参数 func:函数指针
  427. * @输出参数 无
  428. * @返回参数 无
  429. *******************************************************************************/
  430. void vUser_nfc_set_upload_key_stat_func(vUser_nfc_upload_card_stat_func func)
  431. {
  432. xUpload_card_stat = func;
  433. }
  434. /*******************************************************************************
  435. * @函数名称 user_nfc_task_process_event
  436. * @函数说明 task的event处理回调函数,需要在注册task时候,传进去
  437. * @输入参数 task_id:任务ID
  438. events:事件
  439. * @输出参数 无
  440. * @返回参数 无
  441. *******************************************************************************/
  442. static uint16_t user_nfc_task_process_event(uint8_t task_id, uint16_t events)
  443. {
  444. //event 处理
  445. if(events & (1 << NFC_QUEUE_TIME))
  446. {
  447. user_nfc_task();
  448. return (events ^ (1 << NFC_QUEUE_TIME)); //异或的方式清除该事件运行标志,并返回未运行的事件标志
  449. }
  450. return 0;
  451. }
  452. /*******************************************************************************
  453. * @函数名称 vUser_nfc_task_start
  454. * @函数说明 NFC任务开始
  455. * @输入参数 无
  456. * @输出参数 无
  457. * @返回参数 无
  458. *******************************************************************************/
  459. void vUser_nfc_task_start(void)
  460. {
  461. bStatus_t stat = tmos_start_reload_task(user_nfc_task_id, (1 << NFC_QUEUE_TIME), MS1_TO_SYSTEM_TIME(USER_NFC_INTERVAL));
  462. PRINT("NFC task start=%d\n", stat);
  463. }
  464. /*******************************************************************************
  465. * @函数名称 vUser_nfc_init
  466. * @函数说明 初始化
  467. * @输入参数 无
  468. * @输出参数 无
  469. * @返回参数 无
  470. *****************************************************************************/
  471. void vUser_nfc_init(void)
  472. {
  473. user_nfc_gpio_init();
  474. user_spi_Init();
  475. user_nfc_task_id = TMOS_ProcessEventRegister(user_nfc_task_process_event);
  476. PRINT("user_nfc_task_id=%d\n", user_nfc_task_id);
  477. }
  478. /*******************************************************************************
  479. * @函数名称 vUser_nfc_set_charge_stat
  480. * @函数说明 设置无线充电状态
  481. * @输入参数 无
  482. * @输出参数 无
  483. * @返回参数 无
  484. *****************************************************************************/
  485. void vUser_nfc_set_charge_stat(uint8_t idx, bool stat)
  486. {
  487. if(idx == LEFT_IDX)
  488. {
  489. nfc_work.left_charge_stat = stat;
  490. }
  491. else if(idx == RIGHT_IDX)
  492. {
  493. nfc_work.right_charge_stat = stat;
  494. }
  495. }