app_one_wire.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. #include "usr_config.h"
  2. #include "bsp_time.h"
  3. #include "system.h"
  4. #include "nrf_gpio.h"
  5. #include "app_charge.h"
  6. #include "app_flash.h"
  7. #include "ble_comm.h"
  8. #include "bsp_adc.h"
  9. #include "app_pair_chargerpin.h"
  10. #include "hal_led.h"
  11. #include "hal_charge.h"
  12. #include "app_one_wire.h"
  13. //发送协议
  14. // Host MAC[0] MAC[1] MAC[2]>>>>>> Client
  15. // Client MAC[3] MAC[4] MAC[5]>>>>>> Host
  16. //===========================================================================================================
  17. #define PIN_LED_RUN 100
  18. #define DATA_LENGTH 4
  19. #define BITWIDTH 30
  20. #define LOW_BIT_DELAY 15
  21. #define DELAY_SEND 100
  22. #define DELAY_REPLY 100
  23. #define DELAY_CHECK 100
  24. #define DELAY_LEDON (BITWIDTH * (8 * DATA_LENGTH + 3) * 2 + DELAY_SEND + DELAY_REPLY + DELAY_CHECK + 5 * BITWIDTH)
  25. #define LEDON_KEEP 100
  26. unsigned static char rxbuf[DATA_LENGTH];
  27. unsigned static char txbuf[DATA_LENGTH];
  28. unsigned static char savebuf[DATA_LENGTH];
  29. typedef void (*_Event)(unsigned char*,int);
  30. _Event _success_evt;
  31. static char one_ms_status = 0;
  32. static void null_pcs(void)
  33. {}
  34. void chargerpin_one_ms_init(_Event success_evt)
  35. {
  36. one_ms_status = 0;
  37. _success_evt=success_evt;
  38. Process_Start(0, "null_pcs", null_pcs);
  39. Process_SetHoldOn(null_pcs, 1);
  40. TIME_Regist(chargerpin_one_ms_pcs);
  41. PAIR_CHARGERPIN_PRINT("-------------------------------------------chargerpin_one_ms_init.%d\r\n", 1);
  42. }
  43. void chargerpin_one_ms_uninit(void)
  44. {
  45. Process_Stop(null_pcs);
  46. TIME_UnRegist(chargerpin_one_ms_pcs);
  47. PAIR_CHARGERPIN_PRINT("-------------------------------------------chargerpin_one_ms_uninit.%d\r\n", 1);
  48. }
  49. void chargerpin_one_ms_pcs(void *t)
  50. {
  51. unsigned int ms = *(unsigned int *)t;
  52. static unsigned int bit_width = 0;
  53. static unsigned int now_bit = 0;
  54. static unsigned int delay_send_count = 0;
  55. static unsigned int delay_reply_count = 0;
  56. static unsigned int delay_check_count = 0;
  57. static unsigned int delay_ledon_count = 0;
  58. static int bitindex = 0;
  59. static int bytesindes = 0;
  60. static int value = 0;
  61. static int txrxcount = 0;
  62. static char lowbitdelay = 0;
  63. switch (one_ms_status)
  64. {
  65. case 0: // init
  66. bit_width = 0;
  67. now_bit = 0;
  68. delay_send_count = 0;
  69. delay_reply_count = 0;
  70. delay_check_count = 0;
  71. delay_ledon_count = 0;
  72. bitindex = 0;
  73. bytesindes = 0;
  74. value = 0;
  75. txrxcount = 0;
  76. one_ms_status = 0;
  77. lowbitdelay = 0;
  78. nrf_gpio_cfg(
  79. PIN_CHARGING,
  80. NRF_GPIO_PIN_DIR_OUTPUT,
  81. NRF_GPIO_PIN_INPUT_CONNECT,
  82. NRF_GPIO_PIN_NOPULL,
  83. NRF_GPIO_PIN_D0H1,
  84. NRF_GPIO_PIN_NOSENSE);
  85. nrf_gpio_cfg_watcher(PIN_CHARGING);
  86. one_ms_status = 1;
  87. nrf_gpio_pin_clear(PIN_CHARGING);
  88. PAIR_CHARGERPIN_PRINT("nrf_gpio_cfg_watcher(PIN_CHARGING); clear.%d\r\n", ms);
  89. break;
  90. case 1:
  91. if (now_bit == nrf_gpio_pin_read(PIN_CHARGING))
  92. {
  93. bit_width++;
  94. }
  95. else
  96. {
  97. //由高电平切换到低电平,下降信号
  98. if (now_bit == 1)
  99. {
  100. now_bit = 0;
  101. if ((bit_width > 1.5 * BITWIDTH - LOW_BIT_DELAY / 3) && (bit_width < 1.5 * BITWIDTH + LOW_BIT_DELAY / 3))
  102. {
  103. one_ms_status = 3;
  104. txrxcount = 1.5 * BITWIDTH;
  105. value = 30;
  106. if (mFlash.isHost == 0)
  107. {
  108. delay_ledon_count = 1.5 * BITWIDTH + DELAY_SEND;
  109. PAIR_CHARGERPIN_PRINT("delay_ledon_count = 1.5 * BITWIDTH + DELAY_SEND.%d\r\n", txrxcount);
  110. }
  111. else
  112. {
  113. delay_ledon_count = 2.5 * BITWIDTH + DELAY_SEND + BITWIDTH * 8 * DATA_LENGTH + 1.5 * BITWIDTH + DELAY_REPLY;
  114. PAIR_CHARGERPIN_PRINT("delay_ledon_count = 2.5 * BITWIDTH + DELAY_SEND + BITWIDTH*8*DATA_LENGTH + 1.5 * BITWIDTH + DELAY_REPLY .%d\r\n", txrxcount);
  115. }
  116. PAIR_CHARGERPIN_PRINT("-----------------------RX MODE start .%d\r\n", bit_width);
  117. break;
  118. }
  119. else
  120. {
  121. if (mFlash.isHost)
  122. {
  123. delay_send_count = 1; //左边鞋发送数据
  124. }
  125. PAIR_CHARGERPIN_INFO("EXTI CHARGEING MOID.%d bit_width %d \r\n", ms, bit_width);
  126. }
  127. }
  128. else
  129. {
  130. now_bit = 1;
  131. delay_send_count = 0;
  132. delay_reply_count = 0;
  133. delay_check_count = 0;
  134. nrf_gpio_pin_write(PIN_LED_RUN, 0);
  135. delay_ledon_count = 0;
  136. PAIR_CHARGERPIN_PRINT("0123delay_ledon_count = 0 .%d\r\n", txrxcount);
  137. PAIR_CHARGERPIN_INFO("INTO CHARGEING MOID.%d\r\n", ms);
  138. }
  139. bit_width = 0;
  140. }
  141. if (delay_send_count != 0)
  142. {
  143. delay_send_count++;
  144. }
  145. if (delay_send_count > DELAY_SEND)
  146. {
  147. unsigned char macbuff[32];
  148. delay_send_count = 0;
  149. PAIR_CHARGERPIN_PRINT("BEGIN send MAC.%d\r\n", ms);
  150. Get_MACaddr(macbuff);
  151. macbuff[DATA_LENGTH - 1] = 0;
  152. for (int i = 0; i < DATA_LENGTH - 1; i++)
  153. {
  154. macbuff[DATA_LENGTH - 1] += macbuff[i];
  155. }
  156. memcpy(txbuf, macbuff, DATA_LENGTH);
  157. PAIR_CHARGERPIN_PRINT("host_mac send.%d\r\n", ms);
  158. one_ms_status = 2;
  159. txrxcount = 0;
  160. for (int i = 0; i < DATA_LENGTH; i++)
  161. {
  162. PAIR_CHARGERPIN_PRINT("txbuf.%x\r\n", txbuf[i]);
  163. }
  164. break;
  165. }
  166. if (delay_reply_count != 0)
  167. {
  168. delay_reply_count++;
  169. }
  170. if (delay_reply_count > DELAY_REPLY)
  171. {
  172. unsigned char macbuff[16];
  173. delay_reply_count = 0;
  174. PAIR_CHARGERPIN_PRINT("BEGIN send MAC.%d\r\n", ms);
  175. Get_MACaddr(macbuff);
  176. memcpy(txbuf, &macbuff[3], DATA_LENGTH);
  177. txbuf[DATA_LENGTH - 1] = 0;
  178. for (int i = 0; i < DATA_LENGTH - 1; i++)
  179. {
  180. txbuf[DATA_LENGTH - 1] += txbuf[i];
  181. }
  182. PAIR_CHARGERPIN_PRINT("host_mac send.%d\r\n", ms);
  183. one_ms_status = 2;
  184. txrxcount = 0;
  185. for (int i = 0; i < DATA_LENGTH; i++)
  186. {
  187. PAIR_CHARGERPIN_PRINT("txbuf.%x\r\n", txbuf[i]);
  188. }
  189. break;
  190. }
  191. if (delay_check_count != 0)
  192. {
  193. delay_check_count++;
  194. }
  195. if (delay_check_count > DELAY_CHECK)
  196. {
  197. delay_check_count = 0;
  198. one_ms_status = 4;
  199. txrxcount = 0;
  200. PAIR_CHARGERPIN_PRINT("CHECK start.%d\r\n", ms);
  201. break;
  202. }
  203. else if (delay_check_count > 0)
  204. {
  205. if (now_bit > 0)
  206. {
  207. delay_check_count = 0;
  208. PAIR_CHARGERPIN_PRINT("CANCEL CHECK.%d\r\n", ms);
  209. }
  210. }
  211. break;
  212. case 2: // tx
  213. {
  214. lowbitdelay++;
  215. static char send_interrupt = 0;
  216. if ((now_bit == 0) && (nrf_gpio_pin_read(PIN_CHARGING)) && (lowbitdelay > LOW_BIT_DELAY))
  217. {
  218. if (mFlash.isHost)
  219. {
  220. one_ms_status = 1;
  221. nrf_gpio_pin_clear(PIN_CHARGING);
  222. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  223. delay_ledon_count = 0;
  224. PAIR_CHARGERPIN_PRINT("delay_ledon_count1 = 0;\r\n", txrxcount);
  225. PAIR_CHARGERPIN_PRINT("EXTI SEND MODE.CAN NOT SET LOW %d\r\n", txrxcount);
  226. send_interrupt = 0;
  227. break;
  228. }
  229. else
  230. {
  231. nrf_gpio_pin_clear(PIN_CHARGING);
  232. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  233. send_interrupt = 1;
  234. }
  235. }
  236. if ((txrxcount == 0) && (send_interrupt == 0))
  237. {
  238. nrf_gpio_pin_set(PIN_CHARGING);
  239. PAIR_CHARGERPIN_PRINT("set.%d\r\n", ms);
  240. now_bit = 1;
  241. }
  242. if ((txrxcount == 1.5 * BITWIDTH) && (send_interrupt == 0))
  243. {
  244. nrf_gpio_pin_clear(PIN_CHARGING);
  245. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  246. now_bit = 0;
  247. lowbitdelay = 0;
  248. }
  249. if ((txrxcount >= (int)(2.5 * BITWIDTH) && (txrxcount - (int)(2.5 * BITWIDTH)) % BITWIDTH == 0) && (send_interrupt == 0)&&(txrxcount < BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1))
  250. {
  251. bytesindes = ((txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH) / 8;
  252. bitindex = 7 - ((txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH) % 8;
  253. nrf_gpio_pin_write(PIN_CHARGING, (txbuf[bytesindes] >> bitindex) & 0x01);
  254. lowbitdelay = 0;
  255. PAIR_CHARGERPIN_PRINT("write.%d %d\r\n", ((txbuf[bytesindes] >> bitindex) & 0x01), txrxcount);
  256. now_bit = (txbuf[bytesindes] >> bitindex) & 0x01;
  257. PAIR_CHARGERPIN_PRINT("%d %d %d %d %x\r\n", (txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH, bytesindes, bitindex, (txbuf[bytesindes] >> bitindex) & 0x01, txbuf[bytesindes]);
  258. }
  259. if (send_interrupt)
  260. {
  261. if (nrf_gpio_pin_read(PIN_CHARGING) == 0)
  262. {
  263. send_interrupt = 0;
  264. delay_ledon_count = DELAY_SEND;
  265. PAIR_CHARGERPIN_PRINT("delay_ledon_count = DELAY_SEND;.%d\r\n", txrxcount);
  266. PAIR_CHARGERPIN_PRINT("RX SEND MODE MAC START.%d\r\n", txrxcount);
  267. txrxcount = 0;
  268. break;
  269. }
  270. }
  271. if (txrxcount == BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1)
  272. {
  273. nrf_gpio_pin_clear(PIN_CHARGING);
  274. PAIR_CHARGERPIN_PRINT("clearr.%d\r\n", ms);
  275. if (mFlash.isHost == 0)
  276. {
  277. delay_check_count = 1;
  278. }
  279. }
  280. if (delay_check_count != 0)
  281. {
  282. delay_check_count++;
  283. }
  284. if (txrxcount >= BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1 + LOW_BIT_DELAY)
  285. {
  286. one_ms_status = 1;
  287. send_interrupt = 0;
  288. now_bit = 0;
  289. bit_width = 0;
  290. PAIR_CHARGERPIN_PRINT("SEDN DONE .%d pin one_ms_status %d\r\n", ms, nrf_gpio_pin_read(PIN_CHARGING));//IO口高电平就有错
  291. break;
  292. }
  293. txrxcount++;
  294. }
  295. break;
  296. case 3: // rx
  297. if (now_bit == nrf_gpio_pin_read(PIN_CHARGING))
  298. {
  299. bit_width++;
  300. }
  301. else
  302. {
  303. if (now_bit == 1)
  304. {
  305. now_bit = 0;
  306. if ((bit_width > 1.5 * BITWIDTH - 3) && (bit_width < 1.8 * BITWIDTH + 3))
  307. {
  308. txrxcount = 1.5 * BITWIDTH;
  309. value = 30;
  310. delay_ledon_count = DELAY_SEND + 1.5 * BITWIDTH;
  311. PAIR_CHARGERPIN_PRINT("DELAY_SEND + 1.5 * BITWIDTHt .%d\r\n", txrxcount);
  312. PAIR_CHARGERPIN_PRINT("RX MODE restart .%d\r\n", ms);
  313. }
  314. }
  315. else
  316. {
  317. now_bit = 1;
  318. }
  319. bit_width = 0;
  320. }
  321. if (txrxcount >= (int)(3.5 * BITWIDTH) && (txrxcount - (int)(3.5 * BITWIDTH)) % BITWIDTH == 0)
  322. {
  323. bytesindes = ((txrxcount - (int)(3.5 * BITWIDTH)) / BITWIDTH) / 8;
  324. bitindex = 7 - ((txrxcount - (int)(3.5 * BITWIDTH)) / BITWIDTH) % 8;
  325. if (value > 0)
  326. {
  327. rxbuf[bytesindes] |= 0x01 << bitindex;
  328. }
  329. else
  330. {
  331. rxbuf[bytesindes] &= ~(0x01 << bitindex);
  332. }
  333. PAIR_CHARGERPIN_PRINT("%d %d %d %d %x\r\n", txrxcount, bytesindes, bitindex, value, rxbuf[bytesindes]);
  334. value = 0;
  335. nrf_gpio_pin_toggle(PIN_LED_RUN);
  336. }
  337. else
  338. {
  339. if (now_bit)
  340. {
  341. value++;
  342. }
  343. else
  344. {
  345. value--;
  346. }
  347. }
  348. if (txrxcount >= BITWIDTH * (8 * DATA_LENGTH + 2.5))
  349. {
  350. unsigned char dataerrcheck = 0;
  351. unsigned char crc = 0;
  352. one_ms_status = 1;
  353. PAIR_CHARGERPIN_PRINT("RECEVIC DONE .%d\r\n", txrxcount);
  354. nrf_gpio_pin_clear(PIN_LED_RUN);
  355. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  356. for (int i = 0; i < DATA_LENGTH - 1; i++)
  357. {
  358. crc += rxbuf[i];
  359. }
  360. for (int i = 0; i < DATA_LENGTH - 1; i++)
  361. {
  362. if (rxbuf[i] == 0)
  363. {
  364. dataerrcheck++;
  365. }
  366. }
  367. if (dataerrcheck == DATA_LENGTH - 1)
  368. {
  369. PAIR_CHARGERPIN_PRINT("CRC ERROR ALL 0x00.%x\r\n", crc);
  370. delay_ledon_count = 0;
  371. PAIR_CHARGERPIN_PRINT("delay_ledon_count 2= 0;\r\n", txrxcount);
  372. break;
  373. }
  374. dataerrcheck = 0;
  375. for (int i = 0; i < DATA_LENGTH - 1; i++)
  376. {
  377. if (rxbuf[i] == 0xff)
  378. {
  379. dataerrcheck++;
  380. }
  381. }
  382. if (dataerrcheck == DATA_LENGTH - 1)
  383. {
  384. PAIR_CHARGERPIN_PRINT("CRC ERROR ALL 0xFF.%x\r\n", crc);
  385. delay_ledon_count = 0;
  386. PAIR_CHARGERPIN_PRINT("delay_ledon_count 3= 0;\r\n", txrxcount);
  387. break;
  388. }
  389. if (crc == rxbuf[DATA_LENGTH - 1])
  390. {
  391. if (mFlash.isHost)
  392. {
  393. delay_check_count = 1; //延时校验计数器
  394. }
  395. else
  396. {
  397. delay_reply_count = 1; //延时回复计数器
  398. }
  399. PAIR_CHARGERPIN_PRINT("---------------CRC SUCCESS .%x----------------\r\n", crc);
  400. }
  401. else
  402. {
  403. PAIR_CHARGERPIN_PRINT("CRC ERROR .%x\r\n", crc);
  404. delay_ledon_count = 0;
  405. PAIR_CHARGERPIN_PRINT("delay_ledon_count 4= 0;\r\n", txrxcount);
  406. }
  407. for (int i = 0; i < DATA_LENGTH; i++)
  408. {
  409. PAIR_CHARGERPIN_PRINT("rxbuf[%d].%x\r\n", i, rxbuf[i]);
  410. }
  411. }
  412. txrxcount++;
  413. break;
  414. case 4:
  415. if (nrf_gpio_pin_read(PIN_CHARGING))
  416. {
  417. value++;
  418. }
  419. else
  420. {
  421. value--;
  422. }
  423. if (mFlash.isHost)
  424. {
  425. if (txrxcount == 0)
  426. {
  427. value = 0;
  428. nrf_gpio_pin_set(PIN_CHARGING);
  429. PAIR_CHARGERPIN_PRINT("set.%d\r\n", ms);
  430. }
  431. if (txrxcount == 3 * BITWIDTH)
  432. {
  433. nrf_gpio_pin_clear(PIN_CHARGING);
  434. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", delay_ledon_count);
  435. }
  436. if (txrxcount >= 3 * BITWIDTH + LOW_BIT_DELAY)
  437. {
  438. value = 0;
  439. one_ms_status = 1;
  440. now_bit = 0;
  441. bit_width = 0;
  442. }
  443. }
  444. else
  445. {
  446. if (txrxcount == 0)
  447. {
  448. value = 0;
  449. }
  450. if (txrxcount > 3 * BITWIDTH)
  451. {
  452. if (value < 2 * BITWIDTH)
  453. {
  454. PAIR_CHARGERPIN_PRINT("CHECK ERROR .%d\r\n", txrxcount);
  455. delay_ledon_count = 0;
  456. PAIR_CHARGERPIN_PRINT("delay_ledon_count 5= 0;\r\n", txrxcount);
  457. }
  458. else
  459. {
  460. PAIR_CHARGERPIN_PRINT("CHECK DONE .%d\r\n", txrxcount);
  461. }
  462. value = 0;
  463. one_ms_status = 1;
  464. nrf_gpio_pin_clear(PIN_CHARGING);
  465. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  466. now_bit = 1;
  467. bit_width = 0;
  468. }
  469. }
  470. txrxcount++;
  471. break;
  472. }
  473. if (delay_ledon_count != 0)
  474. {
  475. delay_ledon_count++;
  476. }
  477. if (delay_ledon_count == DELAY_LEDON)
  478. {
  479. PAIR_CHARGERPIN_PRINT("LEDCOPEN.%d\r\n", ms);
  480. memcpy(savebuf, rxbuf, DATA_LENGTH - 1);
  481. if(_success_evt)_success_evt(savebuf,DATA_LENGTH - 1);
  482. }
  483. if ((delay_ledon_count > DELAY_LEDON) && (delay_ledon_count - DELAY_LEDON == LEDON_KEEP))
  484. {
  485. delay_ledon_count = 0;
  486. PAIR_CHARGERPIN_PRINT("delay_ledon_count 6= 0;\r\n", txrxcount);
  487. PAIR_CHARGERPIN_PRINT("LEDCLOSE.%d\r\n", ms);
  488. }
  489. }
  490. //===========================================================================================================