app_pair_chargerpin.c 26 KB

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