app_pair_chargerpin.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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 "hal_mt.h"
  13. //发送协议
  14. //Host MAC[0] MAC[1] MAC[2]>>>>>> Client
  15. //Client MAC[3] MAC[4] MAC[5]>>>>>> Host
  16. #define HOST_PEAR 1
  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. static uint8_t leddisplay_count=0;
  27. unsigned static char rxbuf[DATA_LENGTH];
  28. unsigned static char txbuf[DATA_LENGTH];
  29. __IO static char writefig=0;
  30. unsigned static char savebuf[DATA_LENGTH];
  31. static char one_ms_status = 0;
  32. static void chargerpin_one_ms_pcs(void* t)
  33. {
  34. unsigned int ms = *(unsigned int*)t;
  35. static unsigned int bit_width = 0;
  36. static unsigned int now_bit = 0;
  37. static unsigned int delay_send_count = 0;
  38. static unsigned int delay_reply_count = 0;
  39. static unsigned int delay_check_count = 0;
  40. static unsigned int delay_ledon_count = 0;
  41. static int bitindex = 0;
  42. static int bytesindes = 0;
  43. static int value = 0;
  44. static int txrxcount = 0;
  45. static char lowbitdelay=0;
  46. switch (one_ms_status)
  47. {
  48. case 0://init
  49. bit_width = 0;
  50. now_bit = 0;
  51. delay_send_count = 0;
  52. delay_reply_count = 0;
  53. delay_check_count = 0;
  54. delay_ledon_count = 0;
  55. bitindex = 0;
  56. bytesindes = 0;
  57. value = 0;
  58. txrxcount = 0;
  59. one_ms_status = 0;
  60. lowbitdelay=0;
  61. nrf_gpio_cfg(
  62. PIN_CHARGING,
  63. NRF_GPIO_PIN_DIR_OUTPUT,
  64. NRF_GPIO_PIN_INPUT_CONNECT,
  65. NRF_GPIO_PIN_NOPULL,
  66. NRF_GPIO_PIN_D0H1,
  67. NRF_GPIO_PIN_NOSENSE);
  68. nrf_gpio_cfg_watcher(PIN_CHARGING);
  69. one_ms_status = 1;
  70. nrf_gpio_pin_clear(PIN_CHARGING);
  71. PAIR_CHARGERPIN_PRINT("nrf_gpio_cfg_watcher(PIN_CHARGING); clear.%d\r\n", ms);
  72. break;
  73. case 1:
  74. if (now_bit == nrf_gpio_pin_read(PIN_CHARGING))
  75. {
  76. bit_width++;
  77. }
  78. else
  79. {
  80. //由高电平切换到低电平,下降信号
  81. if (now_bit == 1)
  82. {
  83. now_bit = 0;
  84. if ((bit_width > 1.5 * BITWIDTH - LOW_BIT_DELAY/3) && (bit_width < 1.5 * BITWIDTH + LOW_BIT_DELAY/3))
  85. {
  86. one_ms_status = 3;
  87. txrxcount = 1.5 * BITWIDTH;
  88. value = 30;
  89. if (mFlash.isHost == 0)
  90. {
  91. delay_ledon_count = 1.5 * BITWIDTH + DELAY_SEND;
  92. PAIR_CHARGERPIN_PRINT("delay_ledon_count = 1.5 * BITWIDTH + DELAY_SEND.%d\r\n", txrxcount);
  93. }
  94. else
  95. {
  96. delay_ledon_count = 2.5 * BITWIDTH + DELAY_SEND + BITWIDTH * 8 * DATA_LENGTH + 1.5 * BITWIDTH + DELAY_REPLY;
  97. PAIR_CHARGERPIN_PRINT("delay_ledon_count = 2.5 * BITWIDTH + DELAY_SEND + BITWIDTH*8*DATA_LENGTH + 1.5 * BITWIDTH + DELAY_REPLY .%d\r\n", txrxcount);
  98. }
  99. PAIR_CHARGERPIN_PRINT("-----------------------RX MODE start .%d\r\n", bit_width);
  100. break;
  101. }
  102. else
  103. {
  104. if (mFlash.isHost)
  105. {
  106. delay_send_count = 1; //左边鞋发送数据
  107. }
  108. PAIR_CHARGERPIN_INFO("EXTI CHARGEING MOID.%d bit_width %d \r\n", ms,bit_width);
  109. }
  110. }
  111. else
  112. {
  113. now_bit = 1;
  114. delay_send_count = 0;
  115. delay_reply_count = 0;
  116. delay_check_count = 0;
  117. nrf_gpio_pin_write(PIN_LED_RUN, 0);
  118. delay_ledon_count = 0;
  119. PAIR_CHARGERPIN_PRINT("delay_ledon_count = 0 .%d\r\n", txrxcount);
  120. PAIR_CHARGERPIN_INFO("INTO CHARGEING MOID.%d\r\n", ms);
  121. }
  122. bit_width = 0;
  123. }
  124. if (delay_send_count != 0)
  125. {
  126. delay_send_count++;
  127. }
  128. if (delay_send_count > DELAY_SEND)
  129. {
  130. unsigned char macbuff[32];
  131. delay_send_count = 0;
  132. PAIR_CHARGERPIN_PRINT("BEGIN send MAC.%d\r\n", ms);
  133. Get_MACaddr(macbuff);
  134. macbuff[DATA_LENGTH - 1] = 0;
  135. for (int i = 0; i < DATA_LENGTH - 1; i++)
  136. {
  137. macbuff[DATA_LENGTH - 1] += macbuff[i];
  138. }
  139. memcpy(txbuf, macbuff, DATA_LENGTH);
  140. PAIR_CHARGERPIN_PRINT("host_mac send.%d\r\n", ms);
  141. one_ms_status = 2;
  142. txrxcount = 0;
  143. for (int i = 0; i < DATA_LENGTH; i++)
  144. {
  145. PAIR_CHARGERPIN_PRINT("txbuf.%x\r\n", txbuf[i]);
  146. }
  147. break;
  148. }
  149. if (delay_reply_count != 0)
  150. {
  151. delay_reply_count++;
  152. }
  153. if (delay_reply_count > DELAY_REPLY)
  154. {
  155. unsigned char macbuff[16];
  156. delay_reply_count = 0;
  157. PAIR_CHARGERPIN_PRINT("BEGIN send MAC.%d\r\n", ms);
  158. Get_MACaddr(macbuff);
  159. memcpy(txbuf, &macbuff[3], DATA_LENGTH);
  160. txbuf[DATA_LENGTH - 1] = 0;
  161. for (int i = 0; i < DATA_LENGTH - 1; i++)
  162. {
  163. txbuf[DATA_LENGTH - 1] += txbuf[i];
  164. }
  165. PAIR_CHARGERPIN_PRINT("host_mac send.%d\r\n", ms);
  166. one_ms_status = 2;
  167. txrxcount = 0;
  168. for (int i = 0; i < DATA_LENGTH; i++)
  169. {
  170. PAIR_CHARGERPIN_PRINT("txbuf.%x\r\n", txbuf[i]);
  171. }
  172. break;
  173. }
  174. if (delay_check_count != 0)
  175. {
  176. delay_check_count++;
  177. }
  178. if (delay_check_count > DELAY_CHECK)
  179. {
  180. delay_check_count = 0;
  181. one_ms_status = 4;
  182. txrxcount = 0;
  183. PAIR_CHARGERPIN_PRINT("CHECK start.%d\r\n", ms);
  184. break;
  185. }
  186. else if (delay_check_count > 0)
  187. {
  188. if (now_bit > 0)
  189. {
  190. delay_check_count = 0;
  191. PAIR_CHARGERPIN_PRINT("CANCEL CHECK.%d\r\n", ms);
  192. }
  193. }
  194. break;
  195. case 2://tx
  196. {
  197. lowbitdelay++;
  198. static char send_interrupt = 0;
  199. if ((now_bit == 0) && (nrf_gpio_pin_read(PIN_CHARGING)) && (lowbitdelay>LOW_BIT_DELAY))
  200. {
  201. if (mFlash.isHost)
  202. {
  203. one_ms_status = 1;
  204. nrf_gpio_pin_clear(PIN_CHARGING);
  205. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  206. delay_ledon_count = 0;
  207. PAIR_CHARGERPIN_PRINT("delay_ledon_count1 = 0;\r\n", txrxcount);
  208. PAIR_CHARGERPIN_PRINT("EXTI SEND MODE.CAN NOT SET LOW %d\r\n", txrxcount);
  209. send_interrupt = 0;
  210. break;
  211. }
  212. else
  213. {
  214. nrf_gpio_pin_clear(PIN_CHARGING);
  215. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  216. send_interrupt = 1;
  217. }
  218. }
  219. if ((txrxcount == 0) && (send_interrupt == 0))
  220. {
  221. nrf_gpio_pin_set(PIN_CHARGING);
  222. PAIR_CHARGERPIN_PRINT("set.%d\r\n", ms);
  223. now_bit = 1;
  224. }
  225. if ((txrxcount == 1.5 * BITWIDTH) && (send_interrupt == 0))
  226. {
  227. nrf_gpio_pin_clear(PIN_CHARGING);
  228. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  229. now_bit = 0;
  230. lowbitdelay=0;
  231. }
  232. if ((txrxcount >= (int)(2.5 * BITWIDTH) && (txrxcount - (int)(2.5 * BITWIDTH)) % BITWIDTH == 0) && (send_interrupt == 0))
  233. {
  234. bytesindes = ((txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH) / 8;
  235. bitindex = 7 - ((txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH) % 8;
  236. nrf_gpio_pin_write(PIN_CHARGING, (txbuf[bytesindes] >> bitindex) & 0x01);
  237. lowbitdelay=0;
  238. PAIR_CHARGERPIN_PRINT("write.%d %d\r\n", ((txbuf[bytesindes] >> bitindex) & 0x01), ms);
  239. now_bit = (txbuf[bytesindes] >> bitindex) & 0x01;
  240. PAIR_CHARGERPIN_PRINT("%d %d %d %d %x\r\n", (txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH
  241. , bytesindes
  242. , bitindex
  243. , (txbuf[bytesindes] >> bitindex) & 0x01
  244. , txbuf[bytesindes]
  245. );
  246. }
  247. if (send_interrupt)
  248. {
  249. if (nrf_gpio_pin_read(PIN_CHARGING) == 0)
  250. {
  251. send_interrupt = 0;
  252. delay_ledon_count = DELAY_SEND;
  253. PAIR_CHARGERPIN_PRINT("delay_ledon_count = DELAY_SEND;.%d\r\n", txrxcount);
  254. PAIR_CHARGERPIN_PRINT("RX SEND MODE MAC START.%d\r\n", txrxcount);
  255. txrxcount = 0;
  256. break;
  257. }
  258. }
  259. if (txrxcount == BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1)
  260. {
  261. nrf_gpio_pin_clear(PIN_CHARGING);
  262. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  263. if (mFlash.isHost == 0)
  264. {
  265. delay_check_count = 1;
  266. }
  267. }
  268. if(delay_check_count!=0)delay_check_count++;
  269. if (txrxcount >= BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1 + LOW_BIT_DELAY)
  270. {
  271. one_ms_status = 1;
  272. send_interrupt = 0;
  273. now_bit = 0;
  274. bit_width = 0;
  275. PAIR_CHARGERPIN_PRINT("SEDN DONE .%d pin one_ms_status %d\r\n", ms,nrf_gpio_pin_read(PIN_CHARGING));
  276. break;
  277. }
  278. txrxcount++;
  279. }
  280. break;
  281. case 3://rx
  282. if (now_bit == nrf_gpio_pin_read(PIN_CHARGING))
  283. {
  284. bit_width++;
  285. }
  286. else
  287. {
  288. if (now_bit == 1)
  289. {
  290. now_bit = 0;
  291. if ((bit_width > 1.5 * BITWIDTH - 3) && (bit_width < 1.8 * BITWIDTH + 3))
  292. {
  293. txrxcount = 1.5 * BITWIDTH;
  294. value = 30;
  295. delay_ledon_count = DELAY_SEND + 1.5 * BITWIDTH;
  296. PAIR_CHARGERPIN_PRINT("DELAY_SEND + 1.5 * BITWIDTHt .%d\r\n", txrxcount);
  297. PAIR_CHARGERPIN_PRINT("RX MODE restart .%d\r\n", ms);
  298. }
  299. }
  300. else
  301. {
  302. now_bit = 1;
  303. }
  304. bit_width = 0;
  305. }
  306. if (txrxcount >= (int)(3.5 * BITWIDTH) && (txrxcount - (int)(3.5 * BITWIDTH)) % BITWIDTH == 0)
  307. {
  308. bytesindes = ((txrxcount - (int)(3.5 * BITWIDTH)) / BITWIDTH) / 8;
  309. bitindex = 7 - ((txrxcount - (int)(3.5 * BITWIDTH)) / BITWIDTH) % 8;
  310. if (value > 0)
  311. {
  312. rxbuf[bytesindes] |= 0x01 << bitindex;
  313. }
  314. else
  315. {
  316. rxbuf[bytesindes] &= ~(0x01 << bitindex);
  317. }
  318. PAIR_CHARGERPIN_PRINT("%d %d %d %d %x\r\n", txrxcount
  319. , bytesindes
  320. , bitindex
  321. , value
  322. , rxbuf[bytesindes]
  323. );
  324. value = 0;
  325. nrf_gpio_pin_toggle(PIN_LED_RUN);
  326. }
  327. else
  328. {
  329. if (now_bit)
  330. {
  331. value++;
  332. }
  333. else
  334. {
  335. value--;
  336. }
  337. }
  338. if (txrxcount >= BITWIDTH * (8 * DATA_LENGTH + 2.5))
  339. {
  340. unsigned char dataerrcheck = 0;
  341. unsigned char crc = 0;
  342. one_ms_status = 1;
  343. PAIR_CHARGERPIN_PRINT("RECEVIC DONE .%d\r\n", txrxcount);
  344. nrf_gpio_pin_clear(PIN_LED_RUN);
  345. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  346. for (int i = 0; i < DATA_LENGTH - 1; i++)
  347. {
  348. crc += rxbuf[i];
  349. }
  350. for (int i = 0; i < DATA_LENGTH - 1; i++)
  351. {
  352. if (rxbuf[i] == 0)
  353. {
  354. dataerrcheck++;
  355. }
  356. }
  357. if (dataerrcheck == DATA_LENGTH - 1)
  358. {
  359. PAIR_CHARGERPIN_PRINT("CRC ERROR ALL 0x00.%x\r\n", crc);
  360. delay_ledon_count = 0;
  361. PAIR_CHARGERPIN_PRINT("delay_ledon_count 2= 0;\r\n", txrxcount);
  362. break;
  363. }
  364. dataerrcheck = 0;
  365. for (int i = 0; i < DATA_LENGTH - 1; i++)
  366. {
  367. if (rxbuf[i] == 0xff)
  368. {
  369. dataerrcheck++;
  370. }
  371. }
  372. if (dataerrcheck == DATA_LENGTH - 1)
  373. {
  374. PAIR_CHARGERPIN_PRINT("CRC ERROR ALL 0xFF.%x\r\n", crc);
  375. delay_ledon_count = 0;
  376. PAIR_CHARGERPIN_PRINT("delay_ledon_count 3= 0;\r\n", txrxcount);
  377. break;
  378. }
  379. if (crc == rxbuf[DATA_LENGTH - 1])
  380. {
  381. if (mFlash.isHost)
  382. {
  383. delay_check_count = 1; //延时校验计数器
  384. }
  385. else
  386. {
  387. delay_reply_count = 1; //延时回复计数器
  388. }
  389. PAIR_CHARGERPIN_PRINT("---------------CRC SUCCESS .%x----------------\r\n", crc);
  390. }
  391. else
  392. {
  393. PAIR_CHARGERPIN_PRINT("CRC ERROR .%x\r\n", crc);
  394. delay_ledon_count = 0;
  395. PAIR_CHARGERPIN_PRINT("delay_ledon_count 4= 0;\r\n", txrxcount);
  396. }
  397. for (int i = 0; i < DATA_LENGTH; i++)
  398. {
  399. PAIR_CHARGERPIN_PRINT("rxbuf[%d].%x\r\n",i, rxbuf[i]);
  400. }
  401. }
  402. txrxcount++;
  403. break;
  404. case 4:
  405. if (nrf_gpio_pin_read(PIN_CHARGING))
  406. {
  407. value++;
  408. }
  409. else
  410. {
  411. value--;
  412. }
  413. if (mFlash.isHost)
  414. {
  415. if (txrxcount == 0)
  416. {
  417. value = 0;
  418. nrf_gpio_pin_set(PIN_CHARGING);
  419. PAIR_CHARGERPIN_PRINT("set.%d\r\n", ms);
  420. }
  421. if (txrxcount == 3 * BITWIDTH)
  422. {
  423. nrf_gpio_pin_clear(PIN_CHARGING);
  424. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", delay_ledon_count);
  425. }
  426. if (txrxcount >= 3 * BITWIDTH + LOW_BIT_DELAY)
  427. {
  428. value = 0;
  429. one_ms_status = 1;
  430. now_bit = 0;
  431. bit_width = 0;
  432. }
  433. }
  434. else
  435. {
  436. if (txrxcount == 0)
  437. {
  438. value = 0;
  439. }
  440. if (txrxcount > 3 * BITWIDTH)
  441. {
  442. if (value < 2 * BITWIDTH)
  443. {
  444. PAIR_CHARGERPIN_PRINT("CHECK ERROR .%d\r\n", txrxcount);
  445. delay_ledon_count = 0;
  446. PAIR_CHARGERPIN_PRINT("delay_ledon_count 5= 0;\r\n", txrxcount);
  447. }
  448. else
  449. {
  450. PAIR_CHARGERPIN_PRINT("CHECK DONE .%d\r\n", txrxcount);
  451. }
  452. value = 0;
  453. one_ms_status = 1;
  454. nrf_gpio_pin_clear(PIN_CHARGING);
  455. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  456. now_bit = 1;
  457. bit_width = 0;
  458. }
  459. }
  460. txrxcount++;
  461. break;
  462. }
  463. if (delay_ledon_count != 0)
  464. {
  465. delay_ledon_count++;
  466. }
  467. if (delay_ledon_count == DELAY_LEDON)
  468. {
  469. PAIR_CHARGERPIN_PRINT("LEDCOPEN.%d\r\n", ms);
  470. memcpy(savebuf,rxbuf,DATA_LENGTH-1);
  471. writefig=1;
  472. }
  473. if ((delay_ledon_count > DELAY_LEDON )
  474. && (delay_ledon_count - DELAY_LEDON == LEDON_KEEP))
  475. {
  476. delay_ledon_count = 0;
  477. PAIR_CHARGERPIN_PRINT("delay_ledon_count 6= 0;\r\n", txrxcount);
  478. PAIR_CHARGERPIN_PRINT("LEDCLOSE.%d\r\n", ms);
  479. }
  480. }
  481. void pair_led_pcs(void)
  482. {
  483. leddisplay_count++;
  484. if(leddisplay_count%2==0)
  485. {
  486. LED_Start(LED_PAIR,COLOR_BLACK);
  487. }
  488. else
  489. {
  490. LED_Start(LED_PAIR,COLOR_WHITE);
  491. }
  492. if(leddisplay_count>20)
  493. {
  494. leddisplay_count=0;
  495. LED_Stop(LED_PAIR);
  496. Process_SetHoldOn(pair_led_pcs,0);
  497. Process_Stop(pair_led_pcs);
  498. }
  499. }
  500. void stop_pair_led(void)
  501. {
  502. if(leddisplay_count==0)return;
  503. LED_Start(LED_PAIR,COLOR_BLACK);
  504. Process_SetHoldOn(pair_led_pcs,0);
  505. Process_Stop(pair_led_pcs);
  506. leddisplay_count=0;
  507. }
  508. PAIR_START_REGISTER(stop_pair_led);
  509. void mtrun(void)
  510. {
  511. // MT_Run(200);
  512. }
  513. PAIR_START_REGISTER(mtrun);
  514. //写配对相关的flish
  515. static int pair_writeflish(unsigned char* macAddr_L, unsigned char* macAddr_R)
  516. {
  517. uint8_t offest =0;
  518. if(mFlash.isHost){offest =3;}
  519. if (memcmp(macAddr_R, mFlash.mClient.macAddr+offest, 3) != 0)
  520. {
  521. goto writein;
  522. }
  523. if (mFlash.mClient.isConfig != 'C')
  524. {
  525. goto writein;
  526. }
  527. PAIR_CHARGERPIN_PRINT("mFlash.mClient.isConfig:%X\n", mFlash.mClient.isConfig);
  528. PAIR_CHARGERPIN_PRINT("mFlash.mClient.macAddr:%02X %02X %02X\n", mFlash.mClient.macAddr[0], mFlash.mClient.macAddr[1], mFlash.mClient.macAddr[2]);
  529. PAIR_CHARGERPIN_PRINT("mFlash.macHost:%02X %02X %02X\n", mFlash.macHost[0], mFlash.macHost[1], mFlash.macHost[2]);
  530. return 0;
  531. writein:
  532. mFlash.mClient.isConfig = 'C';
  533. uint8_t i=0;
  534. for (i = 0; i < 6; i++){mFlash.macHost[i] = macAddr_L[i];}
  535. for (i = 0; i < 3; i++)
  536. mFlash.mClient.macAddr[i+offest] = macAddr_R[i]; //从机自身mac地址
  537. //保存数据到备份区域里面
  538. for( i=0; i<RecordMacAddrL; i++){
  539. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  540. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i];//从机地址
  541. }
  542. mBackup.hardVersion = mFlash.mClient.hardVersion;
  543. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  544. mBackup.isConfig = mFlash.mClient.isConfig;
  545. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Pair,"pair save backup fail");
  546. DEBUG_LOG("mFlash.mClient.isConfig:%X\n", mFlash.mClient.isConfig);
  547. DEBUG_LOG("mFlash.mClient.macAddr:%02X %02X %02X\n", mFlash.mClient.macAddr[0], mFlash.mClient.macAddr[1], mFlash.mClient.macAddr[2]);
  548. DEBUG_LOG("mFlash.macHost:%02X %02X %02X \n", mFlash.macHost[0], mFlash.macHost[1], mFlash.macHost[2]);
  549. if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair,"pair clear step fail");return 0;}
  550. memset(&mFlash.mStep,0,sizeof(FlashStep_t));
  551. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair,"Flash_SaveInfomation fail");return 0;}
  552. return 1;
  553. }
  554. static void load_adv_name_from_flish(void)
  555. {
  556. char buf[16];
  557. memset(buf, 0, sizeof(buf));
  558. advertising_stop();
  559. sprintf(buf, "%02X%02X%02X%02X%02X%02X", mFlash.mClient.macAddr[0],mFlash.mClient.macAddr[1],mFlash.mClient.macAddr[2],mFlash.macHost[3],mFlash.macHost[4],mFlash.macHost[5]);
  560. DEBUG_LOG("advName(%d):%s\n", strlen(buf), buf);
  561. slave_set_adv_name(buf, strlen(buf));
  562. slave_adv_init();
  563. }
  564. static void load_scan_name_from_flish(void)
  565. {
  566. char buf[16];
  567. memset(buf, 0, sizeof(buf));
  568. sprintf(buf, "%02X%02X%02X%02X%02X%02X", mFlash.macHost[0], mFlash.macHost[1], mFlash.macHost[2], mFlash.mClient.macAddr[3], mFlash.mClient.macAddr[4], mFlash.mClient.macAddr[5]);
  569. DEBUG_LOG("scanName1(%d):%s\n", strlen(buf), buf);
  570. host_set_scan_name(buf, strlen(buf));
  571. }
  572. static unsigned char reconnect_pcs_status=0;
  573. static void reconnect_pcs(void)
  574. {
  575. switch(reconnect_pcs_status)
  576. {
  577. case 0:
  578. if(mFlash.isHost)
  579. {
  580. reconnect_pcs_status=1;
  581. if(host_isconnect()==1)host_disconnect();
  582. }
  583. else
  584. {
  585. reconnect_pcs_status=2;
  586. if(slave_isconnect()==1)slave_disconnect();
  587. }
  588. break;
  589. case 1:
  590. if(host_isconnect()==0)
  591. {
  592. load_scan_name_from_flish();
  593. scan_start();
  594. reconnect_pcs_status=4;
  595. }
  596. break;
  597. case 2:
  598. if(slave_isconnect()==0)
  599. {
  600. load_adv_name_from_flish();
  601. advertising_start();
  602. reconnect_pcs_status=4;
  603. }
  604. break;
  605. case 4:
  606. Process_SetHoldOn(reconnect_pcs,0);
  607. Process_Stop(reconnect_pcs);
  608. reconnect_pcs_status=5;
  609. leddisplay_count =0;
  610. Process_Start(500,"pair_led_pcs",pair_led_pcs);
  611. Process_SetHoldOn(pair_led_pcs,1);
  612. break;
  613. case 5:
  614. break;
  615. }
  616. }
  617. static void app_pair_chargerpin_pcs(void)
  618. {
  619. int rev=0;
  620. if(writefig==1)
  621. {
  622. unsigned char Lbuff[6];
  623. unsigned char Rbuff[3];
  624. Get_MACaddr(Lbuff);
  625. Rbuff[0]=savebuf[0];Rbuff[1]=savebuf[1];Rbuff[2]=savebuf[2];
  626. rev=pair_writeflish(Lbuff,Rbuff);
  627. if(rev)
  628. {
  629. Process_Start(10,"reconnect_pcs",reconnect_pcs);
  630. Process_SetHoldOn(reconnect_pcs,1);
  631. reconnect_pcs_status=0;
  632. }
  633. else
  634. {
  635. leddisplay_count =0;
  636. Process_Start(500,"pair_led_pcs",pair_led_pcs);
  637. Process_SetHoldOn(pair_led_pcs,1);
  638. }
  639. writefig=0;
  640. }
  641. }
  642. void pair_start_pcs(void)
  643. {
  644. extern void *pair_start$$Base;
  645. extern void *pair_start$$Limit;
  646. typedef void (*main_init_t)(void);
  647. for (int p = (unsigned int)&pair_start$$Base; p < (unsigned int)&pair_start$$Limit; p += 4)
  648. {
  649. (*(main_init_t *)p)();
  650. }
  651. }
  652. void pair_done_pcs(void)
  653. {
  654. extern void *pair_done$$Base;
  655. extern void *pair_done$$Limit;
  656. typedef void (*main_init_t)(void);
  657. for (int p = (unsigned int)&pair_done$$Base; p < (unsigned int)&pair_done$$Limit; p += 4)
  658. {
  659. (*(main_init_t *)p)();
  660. }
  661. }
  662. typedef enum
  663. {
  664. CHARGE=0,
  665. CONNECT_NO_CHARGE,
  666. DISCONNECT_CHARGE,
  667. }pair_line_t;
  668. #define ADC_CHECKMS 1000
  669. #define DELAY_PAIR_COUNT 100
  670. static uint32_t pulluppcs_timer=0;
  671. static pair_line_t pair_line = DISCONNECT_CHARGE;
  672. static short HostChargeADC_value =0;
  673. static volatile uint8_t Host_Pull_State =0;
  674. static void app_pair_host_process(void);
  675. static void ReadAdc_timcallback(void* t){
  676. if(1 == Host_Pull_State){
  677. if(TIME_GetTicks() - pulluppcs_timer >= 3){
  678. uint32_t errcode = ADC_ReadChargePin(PIN_CHARGING_CHANNEL);
  679. PAIR_CHARGERPIN_PRINT("11111 %d\n",errcode);
  680. if(ADC_OP_SUCCESS == errcode){Host_Pull_State =2;
  681. pulluppcs_timer = TIME_GetTicks();
  682. }
  683. else if(TIME_GetTicks() - pulluppcs_timer >= 10){
  684. DEBUG_LOG(">>>>> restart adc \r\n");
  685. ADC_Disable();
  686. ADC_SetPinChannel(PIN_CHARGING,PIN_CHARGING_CHANNEL,NRF_GPIO_PIN_NOPULL);
  687. ADC_Initialize();
  688. Host_Pull_State =0;
  689. }
  690. }
  691. }
  692. else if(Host_Pull_State ==2){
  693. uint8_t state = bsp_Get_ChargePinADC(&HostChargeADC_value);
  694. if(state){Host_Pull_State =3;
  695. nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_NOPULL);
  696. DEBUG_LOG(">>>>> ReadAdc:%d\r\n",HostChargeADC_value);
  697. }
  698. else if(TIME_GetTicks() - pulluppcs_timer >= 10){
  699. DEBUG_LOG(">>>>> restart adc \r\n");
  700. ADC_Disable();
  701. ADC_SetPinChannel(PIN_CHARGING,PIN_CHARGING_CHANNEL,NRF_GPIO_PIN_NOPULL);
  702. ADC_Initialize();
  703. Host_Pull_State =0;
  704. }
  705. }
  706. }
  707. static void app_pair_host_process(void)
  708. {
  709. static uint8_t status=0;
  710. static uint32_t delay_pair_count=0;
  711. static uint32_t overtime=0;
  712. static uint8_t ishost=0;
  713. if(mFlash.isHost != ishost){ishost = mFlash.isHost;
  714. TIME_UnRegist(ReadAdc_timcallback);
  715. delay_pair_count=0;
  716. overtime=0;
  717. status =0;
  718. pair_line = DISCONNECT_CHARGE;
  719. Host_Pull_State =0;
  720. ADC_Disable();
  721. ADC_SetPinChannel(PIN_CHARGING,PIN_CHARGING_CHANNEL,NRF_GPIO_PIN_NOPULL);
  722. ADC_Initialize();
  723. }
  724. if(!mFlash.isHost)return;
  725. switch(status)
  726. {
  727. case 0:
  728. ADC_Disable();
  729. ADC_SetPinChannel(PIN_CHARGING,PIN_CHARGING_CHANNEL,NRF_GPIO_PIN_NOPULL);
  730. ADC_Initialize();
  731. Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS);
  732. Process_SetHoldOn(app_pair_host_process,0);
  733. nrf_gpio_pin_write(PIN_LED_RUN, 0);
  734. pair_done_pcs();
  735. status=1;
  736. break;
  737. case 1:
  738. if(0 == Host_Pull_State ){Host_Pull_State=1;
  739. nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_PULLUP);
  740. pulluppcs_timer = TIME_GetTicks();
  741. TIME_Regist(ReadAdc_timcallback);
  742. Process_SetHoldOn(app_pair_host_process,1);
  743. Process_UpdatePeroid(app_pair_host_process,1);
  744. }
  745. else if(3 == Host_Pull_State){
  746. Host_Pull_State =0;
  747. Process_SetHoldOn(app_pair_host_process,0);
  748. Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS);
  749. PAIR_CHARGERPIN_PRINT( "ADC ( %d ):isHost %d pair_line %d\n", HostChargeADC_value,mFlash.isHost,pair_line);
  750. if((HostChargeADC_value < CHARGE_ADC) && (pair_line == CHARGE)){
  751. PAIR_CHARGERPIN_PRINT("--------------------------------------------------------EXIT CHARGE_ADC \n");
  752. }
  753. if(HostChargeADC_value>=CHARGE_ADC){
  754. PAIR_CHARGERPIN_PRINT(" CHARGE_ADC \n");
  755. if((pair_line != CHARGE)){pair_line=CHARGE;
  756. PAIR_CHARGERPIN_PRINT("--------------------------------------------------------------------INTO CHARGE_ADC \n");
  757. }
  758. }
  759. else {
  760. if((HostChargeADC_value<=DISCON_L_MAX_ADC)&&(HostChargeADC_value>=DISCON_L_MIN_ADC)){
  761. PAIR_CHARGERPIN_PRINT("L DISCON_L_ADC \n");
  762. pair_line=DISCONNECT_CHARGE;
  763. }
  764. else if((HostChargeADC_value<=CON_L_MAX_ADC)&&(HostChargeADC_value>=CON_L_MIN_ADC)){
  765. PAIR_CHARGERPIN_PRINT("L CON_L_MAX_ADC \n");
  766. if(pair_line!=CONNECT_NO_CHARGE){pair_line=CONNECT_NO_CHARGE;
  767. Process_Start(ADC_CHECKMS,"app_pair_host_process",app_pair_host_process);
  768. Process_UpdatePeroid(app_pair_host_process,10);
  769. Process_SetHoldOn(app_pair_host_process,1);
  770. Host_Pull_State =3;
  771. overtime=TIME_GetTicks();
  772. pair_start_pcs();
  773. status=2;
  774. nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_PULLUP);
  775. delay_pair_count=TIME_GetTicks();
  776. PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG HIGH__________ %d \n",TIME_GetTicks());
  777. }
  778. }
  779. }
  780. }
  781. else{
  782. PAIR_CHARGERPIN_PRINT("Host_Pull_State:%d\n",Host_Pull_State);
  783. }
  784. break;
  785. case 2://
  786. if(TIME_GetTicks()- delay_pair_count > ADC_CHECKMS){
  787. TIME_Regist(chargerpin_one_ms_pcs);
  788. delay_pair_count=TIME_GetTicks();
  789. one_ms_status = 0;
  790. status=3;
  791. PAIR_CHARGERPIN_PRINT(" TIME_Regist(chargerpin_one_ms_pcs); %d \n",TIME_GetTicks());
  792. }
  793. break;
  794. case 3:
  795. if(TIME_GetTicks()- delay_pair_count > DELAY_PAIR_COUNT){
  796. delay_pair_count=TIME_GetTicks();
  797. nrf_gpio_pin_set(PIN_CHARGING);
  798. PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG HIG; %d \n",TIME_GetTicks());
  799. status=4;
  800. }
  801. break;
  802. case 4:
  803. if(TIME_GetTicks()- delay_pair_count>(DELAY_PAIR_COUNT+100)){
  804. nrf_gpio_pin_clear(PIN_CHARGING);
  805. PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG LOW; %d \n",TIME_GetTicks());
  806. status=5;
  807. }
  808. break;
  809. case 5:
  810. if(writefig==1){
  811. app_pair_chargerpin_pcs();
  812. TIME_UnRegist(chargerpin_one_ms_pcs);
  813. status=0;
  814. // MT_Run(500);
  815. Host_Pull_State =0;
  816. PAIR_CHARGERPIN_PRINT(" ---------------------------PAIR SUCESS------------- \n");
  817. }
  818. if(TIME_GetTicks()-overtime > DELAY_LEDON+ADC_CHECKMS + DELAY_PAIR_COUNT + 100 + 300){
  819. LED_Stop(LED_PAIR);
  820. TIME_UnRegist(chargerpin_one_ms_pcs);
  821. status=0;
  822. Host_Pull_State =0;
  823. PAIR_CHARGERPIN_PRINT(" ---------------------------PAIR OVER TIME %d------------- \n",TIME_GetTicks());
  824. }
  825. break;
  826. default:break;
  827. }
  828. }
  829. static void app_pair_client_process(void)
  830. {
  831. static unsigned char status=0;
  832. short ADC_value=0;
  833. static uint32_t overtime=0;
  834. static uint8_t ishost =0;
  835. if(mFlash.isHost != ishost){ishost = mFlash.isHost;
  836. overtime=0;
  837. status =0;
  838. pair_line = DISCONNECT_CHARGE;
  839. ADC_Disable();
  840. ADC_SetPinChannel(PIN_CHARGING,PIN_CHARGING_CHANNEL,NRF_GPIO_PIN_NOPULL);
  841. ADC_Initialize();
  842. }
  843. if(mFlash.isHost)return;
  844. switch(status)
  845. {
  846. case 0:
  847. ADC_Disable();
  848. ADC_SetPinChannel(PIN_CHARGING,PIN_CHARGING_CHANNEL,NRF_GPIO_PIN_NOPULL);
  849. ADC_Initialize();
  850. Process_UpdatePeroid(app_pair_client_process,ADC_CHECKMS);
  851. Process_SetHoldOn(app_pair_client_process,0);
  852. nrf_gpio_pin_write(PIN_LED_RUN, 0);
  853. pair_done_pcs();
  854. status=1;
  855. break;
  856. case 1:
  857. if(ADC_OP_SUCCESS!=ADC_Read(PIN_CHARGING_CHANNEL,&ADC_value)){
  858. PAIR_CHARGERPIN_PRINT("ADC_Read ERROR %d\n",mFlash.isHost);
  859. }
  860. PAIR_CHARGERPIN_PRINT( "ADC ( %d ):isHost %d pair_line %d\n", ADC_value,mFlash.isHost,pair_line);
  861. if(ADC_value>=CHARGE_ADC && CHARGE != pair_line){
  862. pair_line = CHARGE;
  863. PAIR_CHARGERPIN_PRINT(" CHARGE_ADC \n");
  864. }
  865. else if((ADC_value >= CONN_MIN_ADC)&&(ADC_value <= CONN_MAX_ADC))
  866. {
  867. PAIR_CHARGERPIN_PRINT(" CONN_ADC %d \n",pair_line);
  868. if(CONNECT_NO_CHARGE != pair_line){pair_line=CONNECT_NO_CHARGE;
  869. PAIR_CHARGERPIN_PRINT(" Process_SetHoldOn(adc_check,1); ms %d\n",TIME_GetTicks());
  870. Process_UpdatePeroid(app_pair_client_process,10);
  871. Process_SetHoldOn(app_pair_client_process,1);
  872. overtime=TIME_GetTicks();
  873. pair_start_pcs();
  874. status=2;
  875. TIME_Regist(chargerpin_one_ms_pcs);
  876. one_ms_status = 0;
  877. }
  878. }
  879. else{
  880. if(ADC_value<=DISCON_R_MAX_ADC && DISCONNECT_CHARGE != pair_line){
  881. pair_line =DISCONNECT_CHARGE;
  882. PAIR_CHARGERPIN_PRINT("R DISCON_R_MAX_ADC \n");
  883. }
  884. }
  885. break;
  886. case 2:
  887. if(writefig==1){
  888. app_pair_chargerpin_pcs();
  889. TIME_UnRegist(chargerpin_one_ms_pcs);
  890. status=0;
  891. // MT_Run(500);
  892. PAIR_CHARGERPIN_PRINT(" ---------------------------PAIR SUCESS------------- \n");
  893. }
  894. if(TIME_GetTicks()-overtime > DELAY_LEDON+ADC_CHECKMS + DELAY_PAIR_COUNT + 100 + 300)
  895. {
  896. LED_Stop(LED_PAIR);
  897. TIME_UnRegist(chargerpin_one_ms_pcs);
  898. status=0;
  899. PAIR_CHARGERPIN_PRINT(" ---------------------------PAIR OVER TIME %d------------- \n",TIME_GetTicks());
  900. }
  901. break;
  902. default:status=0;break;
  903. }
  904. }
  905. void write_on(void)
  906. {
  907. LED_Start(LED_PAIR,COLOR_WHITE);
  908. }
  909. PAIR_START_REGISTER(write_on);
  910. void app_pair_chargerpin_Init(void)
  911. {
  912. Process_Start(ADC_CHECKMS,"app_pair_client",app_pair_client_process);
  913. Process_Start(ADC_CHECKMS,"app_pair_host",app_pair_host_process);
  914. }