app_one_wire.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. #if PAIR_CHARGERPIN_PRINTF
  52. unsigned int ms = *(unsigned int *)t;
  53. #endif
  54. static unsigned int bit_width = 0;
  55. static unsigned int now_bit = 0;
  56. static unsigned int delay_send_count = 0;
  57. static unsigned int delay_reply_count = 0;
  58. static unsigned int delay_check_count = 0;
  59. static unsigned int delay_ledon_count = 0;
  60. static int bitindex = 0;
  61. static int bytesindes = 0;
  62. static int value = 0;
  63. static int txrxcount = 0;
  64. static char lowbitdelay = 0;
  65. switch (one_ms_status)
  66. {
  67. case 0: // init
  68. bit_width = 0;
  69. now_bit = 0;
  70. delay_send_count = 0;
  71. delay_reply_count = 0;
  72. delay_check_count = 0;
  73. delay_ledon_count = 0;
  74. bitindex = 0;
  75. bytesindes = 0;
  76. value = 0;
  77. txrxcount = 0;
  78. one_ms_status = 0;
  79. lowbitdelay = 0;
  80. nrf_gpio_cfg(
  81. PIN_CHARGING,
  82. NRF_GPIO_PIN_DIR_OUTPUT,
  83. NRF_GPIO_PIN_INPUT_CONNECT,
  84. NRF_GPIO_PIN_NOPULL,
  85. NRF_GPIO_PIN_D0H1,
  86. NRF_GPIO_PIN_NOSENSE);
  87. nrf_gpio_cfg_watcher(PIN_CHARGING);
  88. one_ms_status = 1;
  89. nrf_gpio_pin_clear(PIN_CHARGING);
  90. PAIR_CHARGERPIN_PRINT("nrf_gpio_cfg_watcher(PIN_CHARGING); clear.%d\r\n", ms);
  91. break;
  92. case 1:
  93. if (now_bit == nrf_gpio_pin_read(PIN_CHARGING))
  94. {
  95. bit_width++;
  96. }
  97. else
  98. {
  99. //由高电平切换到低电平,下降信号
  100. if (now_bit == 1)
  101. {
  102. now_bit = 0;
  103. if ((bit_width > 1.5 * BITWIDTH - LOW_BIT_DELAY / 3) && (bit_width < 1.5 * BITWIDTH + LOW_BIT_DELAY / 3))
  104. {
  105. one_ms_status = 3;
  106. txrxcount = 1.5 * BITWIDTH;
  107. value = 30;
  108. if (mFlash.isHost == 0)
  109. {
  110. delay_ledon_count = 1.5 * BITWIDTH + DELAY_SEND;
  111. PAIR_CHARGERPIN_PRINT("delay_ledon_count = 1.5 * BITWIDTH + DELAY_SEND.%d\r\n", txrxcount);
  112. }
  113. else
  114. {
  115. delay_ledon_count = 2.5 * BITWIDTH + DELAY_SEND + BITWIDTH * 8 * DATA_LENGTH + 1.5 * BITWIDTH + DELAY_REPLY;
  116. PAIR_CHARGERPIN_PRINT("delay_ledon_count = 2.5 * BITWIDTH + DELAY_SEND + BITWIDTH*8*DATA_LENGTH + 1.5 * BITWIDTH + DELAY_REPLY .%d\r\n", txrxcount);
  117. }
  118. PAIR_CHARGERPIN_PRINT("-----------------------RX MODE start .%d\r\n", bit_width);
  119. break;
  120. }
  121. else
  122. {
  123. if (mFlash.isHost)
  124. {
  125. delay_send_count = 1; //左边鞋发送数据
  126. }
  127. PAIR_CHARGERPIN_INFO("EXTI CHARGEING MOID.%d bit_width %d \r\n", ms, bit_width);
  128. }
  129. }
  130. else
  131. {
  132. now_bit = 1;
  133. delay_send_count = 0;
  134. delay_reply_count = 0;
  135. delay_check_count = 0;
  136. nrf_gpio_pin_write(PIN_LED_RUN, 0);
  137. delay_ledon_count = 0;
  138. PAIR_CHARGERPIN_PRINT("0123delay_ledon_count = 0 .%d\r\n", txrxcount);
  139. PAIR_CHARGERPIN_INFO("INTO CHARGEING MOID.%d\r\n", ms);
  140. }
  141. bit_width = 0;
  142. }
  143. if (delay_send_count != 0)
  144. {
  145. delay_send_count++;
  146. }
  147. if (delay_send_count > DELAY_SEND)
  148. {
  149. unsigned char macbuff[32];
  150. delay_send_count = 0;
  151. PAIR_CHARGERPIN_PRINT("BEGIN send MAC.%d\r\n", ms);
  152. Get_MACaddr(macbuff);
  153. macbuff[DATA_LENGTH - 1] = 0;
  154. for (int i = 0; i < DATA_LENGTH - 1; i++)
  155. {
  156. macbuff[DATA_LENGTH - 1] += macbuff[i];
  157. }
  158. memcpy(txbuf, macbuff, DATA_LENGTH);
  159. PAIR_CHARGERPIN_PRINT("host_mac send.%d\r\n", ms);
  160. one_ms_status = 2;
  161. txrxcount = 0;
  162. for (int i = 0; i < DATA_LENGTH; i++)
  163. {
  164. PAIR_CHARGERPIN_PRINT("txbuf.%x\r\n", txbuf[i]);
  165. }
  166. break;
  167. }
  168. if (delay_reply_count != 0)
  169. {
  170. delay_reply_count++;
  171. }
  172. if (delay_reply_count > DELAY_REPLY)
  173. {
  174. unsigned char macbuff[16];
  175. delay_reply_count = 0;
  176. PAIR_CHARGERPIN_PRINT("BEGIN send MAC.%d\r\n", ms);
  177. Get_MACaddr(macbuff);
  178. memcpy(txbuf, &macbuff[3], DATA_LENGTH);
  179. txbuf[DATA_LENGTH - 1] = 0;
  180. for (int i = 0; i < DATA_LENGTH - 1; i++)
  181. {
  182. txbuf[DATA_LENGTH - 1] += txbuf[i];
  183. }
  184. PAIR_CHARGERPIN_PRINT("host_mac send.%d\r\n", ms);
  185. one_ms_status = 2;
  186. txrxcount = 0;
  187. for (int i = 0; i < DATA_LENGTH; i++)
  188. {
  189. PAIR_CHARGERPIN_PRINT("txbuf.%x\r\n", txbuf[i]);
  190. }
  191. break;
  192. }
  193. if (delay_check_count != 0)
  194. {
  195. delay_check_count++;
  196. }
  197. if (delay_check_count > DELAY_CHECK)
  198. {
  199. delay_check_count = 0;
  200. one_ms_status = 4;
  201. txrxcount = 0;
  202. PAIR_CHARGERPIN_PRINT("CHECK start.%d\r\n", ms);
  203. break;
  204. }
  205. else if (delay_check_count > 0)
  206. {
  207. if (now_bit > 0)
  208. {
  209. delay_check_count = 0;
  210. PAIR_CHARGERPIN_PRINT("CANCEL CHECK.%d\r\n", ms);
  211. }
  212. }
  213. break;
  214. case 2: // tx
  215. {
  216. lowbitdelay++;
  217. static char send_interrupt = 0;
  218. if ((now_bit == 0) && (nrf_gpio_pin_read(PIN_CHARGING)) && (lowbitdelay > LOW_BIT_DELAY))
  219. {
  220. if (mFlash.isHost)
  221. {
  222. one_ms_status = 1;
  223. nrf_gpio_pin_clear(PIN_CHARGING);
  224. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  225. delay_ledon_count = 0;
  226. PAIR_CHARGERPIN_PRINT("delay_ledon_count1 = 0;\r\n", txrxcount);
  227. PAIR_CHARGERPIN_PRINT("EXTI SEND MODE.CAN NOT SET LOW %d\r\n", txrxcount);
  228. send_interrupt = 0;
  229. break;
  230. }
  231. else
  232. {
  233. nrf_gpio_pin_clear(PIN_CHARGING);
  234. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  235. send_interrupt = 1;
  236. }
  237. }
  238. if ((txrxcount == 0) && (send_interrupt == 0))
  239. {
  240. nrf_gpio_pin_set(PIN_CHARGING);
  241. PAIR_CHARGERPIN_PRINT("set.%d\r\n", ms);
  242. now_bit = 1;
  243. }
  244. if ((txrxcount == 1.5 * BITWIDTH) && (send_interrupt == 0))
  245. {
  246. nrf_gpio_pin_clear(PIN_CHARGING);
  247. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  248. now_bit = 0;
  249. lowbitdelay = 0;
  250. }
  251. if ((txrxcount >= (int)(2.5 * BITWIDTH) && (txrxcount - (int)(2.5 * BITWIDTH)) % BITWIDTH == 0) && (send_interrupt == 0)&&(txrxcount < BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1))
  252. {
  253. bytesindes = ((txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH) / 8;
  254. bitindex = 7 - ((txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH) % 8;
  255. nrf_gpio_pin_write(PIN_CHARGING, (txbuf[bytesindes] >> bitindex) & 0x01);
  256. lowbitdelay = 0;
  257. PAIR_CHARGERPIN_PRINT("write.%d %d\r\n", ((txbuf[bytesindes] >> bitindex) & 0x01), txrxcount);
  258. now_bit = (txbuf[bytesindes] >> bitindex) & 0x01;
  259. PAIR_CHARGERPIN_PRINT("%d %d %d %d %x\r\n", (txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH, bytesindes, bitindex, (txbuf[bytesindes] >> bitindex) & 0x01, txbuf[bytesindes]);
  260. }
  261. if (send_interrupt)
  262. {
  263. if (nrf_gpio_pin_read(PIN_CHARGING) == 0)
  264. {
  265. send_interrupt = 0;
  266. delay_ledon_count = DELAY_SEND;
  267. PAIR_CHARGERPIN_PRINT("delay_ledon_count = DELAY_SEND;.%d\r\n", txrxcount);
  268. PAIR_CHARGERPIN_PRINT("RX SEND MODE MAC START.%d\r\n", txrxcount);
  269. txrxcount = 0;
  270. break;
  271. }
  272. }
  273. if (txrxcount == BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1)
  274. {
  275. nrf_gpio_pin_clear(PIN_CHARGING);
  276. PAIR_CHARGERPIN_PRINT("clearr.%d\r\n", ms);
  277. if (mFlash.isHost == 0)
  278. {
  279. delay_check_count = 1;
  280. }
  281. }
  282. if (delay_check_count != 0)
  283. {
  284. delay_check_count++;
  285. }
  286. if (txrxcount >= BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1 + LOW_BIT_DELAY)
  287. {
  288. one_ms_status = 1;
  289. send_interrupt = 0;
  290. now_bit = 0;
  291. bit_width = 0;
  292. PAIR_CHARGERPIN_PRINT("SEDN DONE .%d pin one_ms_status %d\r\n", ms, nrf_gpio_pin_read(PIN_CHARGING));//IO口高电平就有错
  293. break;
  294. }
  295. txrxcount++;
  296. }
  297. break;
  298. case 3: // rx
  299. if (now_bit == nrf_gpio_pin_read(PIN_CHARGING))
  300. {
  301. bit_width++;
  302. }
  303. else
  304. {
  305. if (now_bit == 1)
  306. {
  307. now_bit = 0;
  308. if ((bit_width > 1.5 * BITWIDTH - 3) && (bit_width < 1.8 * BITWIDTH + 3))
  309. {
  310. txrxcount = 1.5 * BITWIDTH;
  311. value = 30;
  312. delay_ledon_count = DELAY_SEND + 1.5 * BITWIDTH;
  313. PAIR_CHARGERPIN_PRINT("DELAY_SEND + 1.5 * BITWIDTHt .%d\r\n", txrxcount);
  314. PAIR_CHARGERPIN_PRINT("RX MODE restart .%d\r\n", ms);
  315. }
  316. }
  317. else
  318. {
  319. now_bit = 1;
  320. }
  321. bit_width = 0;
  322. }
  323. if (txrxcount >= (int)(3.5 * BITWIDTH) && (txrxcount - (int)(3.5 * BITWIDTH)) % BITWIDTH == 0)
  324. {
  325. bytesindes = ((txrxcount - (int)(3.5 * BITWIDTH)) / BITWIDTH) / 8;
  326. bitindex = 7 - ((txrxcount - (int)(3.5 * BITWIDTH)) / BITWIDTH) % 8;
  327. if (value > 0)
  328. {
  329. rxbuf[bytesindes] |= 0x01 << bitindex;
  330. }
  331. else
  332. {
  333. rxbuf[bytesindes] &= ~(0x01 << bitindex);
  334. }
  335. PAIR_CHARGERPIN_PRINT("%d %d %d %d %x\r\n", txrxcount, bytesindes, bitindex, value, rxbuf[bytesindes]);
  336. value = 0;
  337. nrf_gpio_pin_toggle(PIN_LED_RUN);
  338. }
  339. else
  340. {
  341. if (now_bit)
  342. {
  343. value++;
  344. }
  345. else
  346. {
  347. value--;
  348. }
  349. }
  350. if (txrxcount >= BITWIDTH * (8 * DATA_LENGTH + 2.5))
  351. {
  352. unsigned char dataerrcheck = 0;
  353. unsigned char crc = 0;
  354. one_ms_status = 1;
  355. PAIR_CHARGERPIN_PRINT("RECEVIC DONE .%d\r\n", txrxcount);
  356. nrf_gpio_pin_clear(PIN_LED_RUN);
  357. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  358. for (int i = 0; i < DATA_LENGTH - 1; i++)
  359. {
  360. crc += rxbuf[i];
  361. }
  362. for (int i = 0; i < DATA_LENGTH - 1; i++)
  363. {
  364. if (rxbuf[i] == 0)
  365. {
  366. dataerrcheck++;
  367. }
  368. }
  369. if (dataerrcheck == DATA_LENGTH - 1)
  370. {
  371. PAIR_CHARGERPIN_PRINT("CRC ERROR ALL 0x00.%x\r\n", crc);
  372. delay_ledon_count = 0;
  373. PAIR_CHARGERPIN_PRINT("delay_ledon_count 2= 0;\r\n", txrxcount);
  374. break;
  375. }
  376. dataerrcheck = 0;
  377. for (int i = 0; i < DATA_LENGTH - 1; i++)
  378. {
  379. if (rxbuf[i] == 0xff)
  380. {
  381. dataerrcheck++;
  382. }
  383. }
  384. if (dataerrcheck == DATA_LENGTH - 1)
  385. {
  386. PAIR_CHARGERPIN_PRINT("CRC ERROR ALL 0xFF.%x\r\n", crc);
  387. delay_ledon_count = 0;
  388. PAIR_CHARGERPIN_PRINT("delay_ledon_count 3= 0;\r\n", txrxcount);
  389. break;
  390. }
  391. if (crc == rxbuf[DATA_LENGTH - 1])
  392. {
  393. if (mFlash.isHost)
  394. {
  395. delay_check_count = 1; //延时校验计数器
  396. }
  397. else
  398. {
  399. delay_reply_count = 1; //延时回复计数器
  400. }
  401. PAIR_CHARGERPIN_PRINT("---------------CRC SUCCESS .%x----------------\r\n", crc);
  402. }
  403. else
  404. {
  405. PAIR_CHARGERPIN_PRINT("CRC ERROR .%x\r\n", crc);
  406. delay_ledon_count = 0;
  407. PAIR_CHARGERPIN_PRINT("delay_ledon_count 4= 0;\r\n", txrxcount);
  408. }
  409. for (int i = 0; i < DATA_LENGTH; i++)
  410. {
  411. PAIR_CHARGERPIN_PRINT("rxbuf[%d].%x\r\n", i, rxbuf[i]);
  412. }
  413. }
  414. txrxcount++;
  415. break;
  416. case 4:
  417. if (nrf_gpio_pin_read(PIN_CHARGING))
  418. {
  419. value++;
  420. }
  421. else
  422. {
  423. value--;
  424. }
  425. if (mFlash.isHost)
  426. {
  427. if (txrxcount == 0)
  428. {
  429. value = 0;
  430. nrf_gpio_pin_set(PIN_CHARGING);
  431. PAIR_CHARGERPIN_PRINT("set.%d\r\n", ms);
  432. }
  433. if (txrxcount == 3 * BITWIDTH)
  434. {
  435. nrf_gpio_pin_clear(PIN_CHARGING);
  436. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", delay_ledon_count);
  437. }
  438. if (txrxcount >= 3 * BITWIDTH + LOW_BIT_DELAY)
  439. {
  440. value = 0;
  441. one_ms_status = 1;
  442. now_bit = 0;
  443. bit_width = 0;
  444. }
  445. }
  446. else
  447. {
  448. if (txrxcount == 0)
  449. {
  450. value = 0;
  451. }
  452. if (txrxcount > 3 * BITWIDTH)
  453. {
  454. if (value < 2 * BITWIDTH)
  455. {
  456. PAIR_CHARGERPIN_PRINT("CHECK ERROR .%d\r\n", txrxcount);
  457. delay_ledon_count = 0;
  458. PAIR_CHARGERPIN_PRINT("delay_ledon_count 5= 0;\r\n", txrxcount);
  459. }
  460. else
  461. {
  462. PAIR_CHARGERPIN_PRINT("CHECK DONE .%d\r\n", txrxcount);
  463. }
  464. value = 0;
  465. one_ms_status = 1;
  466. nrf_gpio_pin_clear(PIN_CHARGING);
  467. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  468. now_bit = 1;
  469. bit_width = 0;
  470. }
  471. }
  472. txrxcount++;
  473. break;
  474. }
  475. if (delay_ledon_count != 0)
  476. {
  477. delay_ledon_count++;
  478. }
  479. if (delay_ledon_count == DELAY_LEDON)
  480. {
  481. PAIR_CHARGERPIN_PRINT("LEDCOPEN.%d\r\n", ms);
  482. memcpy(savebuf, rxbuf, DATA_LENGTH - 1);
  483. if(_success_evt)_success_evt(savebuf,DATA_LENGTH - 1);
  484. }
  485. if ((delay_ledon_count > DELAY_LEDON) && (delay_ledon_count - DELAY_LEDON == LEDON_KEEP))
  486. {
  487. delay_ledon_count = 0;
  488. PAIR_CHARGERPIN_PRINT("delay_ledon_count 6= 0;\r\n", txrxcount);
  489. PAIR_CHARGERPIN_PRINT("LEDCLOSE.%d\r\n", ms);
  490. }
  491. }
  492. //===========================================================================================================
  493. struct
  494. {
  495. char statu;
  496. unsigned int ms;
  497. unsigned int bit_width;
  498. int now_bit;
  499. char cf_BIT_WIDTH;
  500. short tim_cun;
  501. int bitindex;
  502. int byteindex;
  503. int receive_byteslength;
  504. unsigned char *buff;
  505. unsigned short length;
  506. int value ;
  507. _Event _evt;
  508. _Event_ERR _evt_err;
  509. }one_byte_ram;
  510. void one_byte_receive_Event_err_register(_Event_ERR _cb)
  511. {
  512. one_byte_ram._evt_err=_cb;
  513. }
  514. void one_byte_receive_init(unsigned char *recbuff,unsigned short len,_Event evt)
  515. {
  516. nrf_gpio_cfg(
  517. PIN_CHARGING,
  518. NRF_GPIO_PIN_DIR_OUTPUT,
  519. NRF_GPIO_PIN_INPUT_CONNECT,
  520. NRF_GPIO_PIN_NOPULL,
  521. NRF_GPIO_PIN_D0H1,
  522. NRF_GPIO_PIN_NOSENSE);
  523. nrf_gpio_cfg_watcher(PIN_CHARGING);
  524. one_byte_ram.statu = 1;
  525. nrf_gpio_pin_clear(PIN_CHARGING);
  526. one_byte_ram.now_bit = nrf_gpio_pin_read(PIN_CHARGING);
  527. //这里应该高电平进入
  528. one_byte_ram.bit_width=0;
  529. one_byte_ram.cf_BIT_WIDTH=30;
  530. one_byte_ram.buff=recbuff;
  531. one_byte_ram.length=len;
  532. one_byte_ram._evt=evt;
  533. Process_Start(0, "null_pcs", null_pcs);
  534. Process_SetHoldOn(null_pcs, 1);
  535. TIME_Regist(one_byte_receive_pcs);
  536. }
  537. void one_byte_receive_uninit(void)
  538. {
  539. Process_Stop(null_pcs);
  540. TIME_UnRegist(one_byte_receive_pcs);
  541. PAIR_CHARGERPIN_PRINT("-------------------------------------------chargerpin_one_ms_uninit.%d\r\n", 1);
  542. }
  543. void one_byte_receive_pcs(void*t)
  544. {
  545. for(int i=1;i>0;i--)
  546. {
  547. switch(one_byte_ram.statu)
  548. {
  549. case 1://等待完整的开始信号
  550. if (one_byte_ram.now_bit == nrf_gpio_pin_read(PIN_CHARGING))
  551. {
  552. one_byte_ram.bit_width++;
  553. }
  554. else
  555. {
  556. //由高电平切换到低电平,下降信号,下降沿
  557. if(one_byte_ram.now_bit==1)
  558. {
  559. one_byte_ram.now_bit=0;
  560. }
  561. else//上升沿
  562. {
  563. if((one_byte_ram.bit_width > 1.5*one_byte_ram.cf_BIT_WIDTH-5)&&(one_byte_ram.bit_width < 1.5*one_byte_ram.cf_BIT_WIDTH+5))
  564. {
  565. //已经收到1.5个位宽低电平的开始信号
  566. one_byte_ram.tim_cun=-1.5*one_byte_ram.cf_BIT_WIDTH+1;//第一个数据位开始的时刻,该变量的值为0
  567. one_byte_ram.statu=2;
  568. PAIR_CHARGERPIN_PRINT(" into receive mode %d;\r\n",one_byte_ram.tim_cun);
  569. }
  570. one_byte_ram.now_bit=1;
  571. }
  572. one_byte_ram.bit_width=0;
  573. }
  574. if(one_byte_ram.now_bit==0)
  575. {
  576. if(one_byte_ram.bit_width>1.5*one_byte_ram.cf_BIT_WIDTH+15)
  577. {
  578. if(one_byte_ram._evt_err)one_byte_ram._evt_err(1);//低电平长度错误
  579. }
  580. }
  581. break;
  582. case 2:
  583. one_byte_ram.tim_cun++;
  584. if(one_byte_ram.tim_cun==0)
  585. {
  586. one_byte_ram.statu=3;
  587. PAIR_CHARGERPIN_PRINT("one_byte_ram.tim_cun==0;%d\r\n",nrf_gpio_pin_read(PIN_CHARGING));
  588. i=2;
  589. one_byte_ram.value=0;
  590. }
  591. break;
  592. case 3:
  593. if(one_byte_ram.tim_cun>(one_byte_ram.length)*8*one_byte_ram.cf_BIT_WIDTH + 4*one_byte_ram.cf_BIT_WIDTH)//接收超时判断
  594. {
  595. PAIR_CHARGERPIN_PRINT("one_byte_ram receive overtime; %d \r\n",(one_byte_ram.length+1)*one_byte_ram.cf_BIT_WIDTH);
  596. one_byte_ram.statu=1;
  597. }
  598. if((one_byte_ram.tim_cun % (one_byte_ram.cf_BIT_WIDTH-1)==0)&&(one_byte_ram.tim_cun>0))
  599. {
  600. one_byte_ram.bitindex=one_byte_ram.tim_cun/one_byte_ram.cf_BIT_WIDTH;
  601. one_byte_ram.byteindex=one_byte_ram.bitindex/8;
  602. one_byte_ram.bitindex=one_byte_ram.bitindex%8;
  603. if(one_byte_ram.byteindex<one_byte_ram.length)
  604. {
  605. PAIR_CHARGERPIN_PRINT("%d %d %3d %d\r\n", one_byte_ram.bitindex,one_byte_ram.byteindex,one_byte_ram.value,one_byte_ram.value > 0);
  606. if (one_byte_ram.value > 0)
  607. {
  608. one_byte_ram.buff[one_byte_ram.byteindex] |= 0x01 <<(7 - one_byte_ram.bitindex);
  609. }
  610. else
  611. {
  612. one_byte_ram.buff[one_byte_ram.byteindex] &= ~(0x01 << (7 - one_byte_ram.bitindex));
  613. }
  614. }
  615. else if((one_byte_ram.bitindex==0)&&(one_byte_ram.byteindex==one_byte_ram.length))
  616. {
  617. //数据位接收完成
  618. PAIR_CHARGERPIN_PRINT(" data receive done length : %d \r\n data :",one_byte_ram.length);
  619. for (int k = 0; k < one_byte_ram.length; k++)
  620. {
  621. PAIR_CHARGERPIN_PRINT("%x ",one_byte_ram.buff[k]);
  622. }
  623. PAIR_CHARGERPIN_PRINT("\r\n");
  624. }
  625. one_byte_ram.value=0;
  626. }
  627. else
  628. {
  629. if(nrf_gpio_pin_read(PIN_CHARGING)==0)
  630. {
  631. one_byte_ram.value--;
  632. }
  633. else
  634. {
  635. one_byte_ram.value++;
  636. }
  637. }
  638. if (one_byte_ram.now_bit == nrf_gpio_pin_read(PIN_CHARGING))
  639. {
  640. one_byte_ram.bit_width++;
  641. }
  642. else
  643. {
  644. //由高电平切换到低电平,下降信号,下降沿
  645. if(one_byte_ram.now_bit==1)
  646. {
  647. if((one_byte_ram.bit_width > 1.5*one_byte_ram.cf_BIT_WIDTH-5)&&(one_byte_ram.bit_width < 1.5*one_byte_ram.cf_BIT_WIDTH+5))
  648. {
  649. if(one_byte_ram._evt)
  650. {
  651. one_byte_ram._evt(one_byte_ram.buff,one_byte_ram.byteindex);
  652. PAIR_CHARGERPIN_INFO("one_byte_ram.buff[0] %d one_byte_ram.byteindex %d \r\n", one_byte_ram.buff[0], one_byte_ram.byteindex);
  653. }
  654. PAIR_CHARGERPIN_PRINT(" one_byte_ram._evt \r\n");
  655. PAIR_CHARGERPIN_PRINT(" receive done tim %d; %d\r\n",one_byte_ram.tim_cun,one_byte_ram.bit_width);
  656. one_byte_ram.statu=1;
  657. }
  658. one_byte_ram.now_bit=0;
  659. }
  660. else//上升沿
  661. {
  662. one_byte_ram.now_bit=1;
  663. }
  664. one_byte_ram.bit_width=0;
  665. }
  666. one_byte_ram.tim_cun++;
  667. break;
  668. default:
  669. break;
  670. }
  671. }
  672. }