app_pair_chargerpin.c 29 KB

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