app_pair_chargerpin.c 24 KB

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