app_pair_chargerpin - 副本.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  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 1
  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. //
  126. // if (delay_send_count != 0)
  127. // {
  128. // delay_send_count++;
  129. // }
  130. // if (delay_send_count > DELAY_SEND)
  131. // {
  132. // unsigned char macbuff[32];
  133. // delay_send_count = 0;
  134. // PAIR_CHARGERPIN_PRINT("BEGIN send MAC.%d\r\n", ms);
  135. // Get_MACaddr(macbuff);
  136. // macbuff[DATA_LENGTH - 1] = 0;
  137. // for (int i = 0; i < DATA_LENGTH - 1; i++)
  138. // {
  139. // macbuff[DATA_LENGTH - 1] += macbuff[i];
  140. // }
  141. // memcpy(txbuf, macbuff, DATA_LENGTH);
  142. // PAIR_CHARGERPIN_PRINT("host_mac send.%d\r\n", ms);
  143. // one_ms_status = 2;
  144. //
  145. // txrxcount = 0;
  146. // for (int i = 0; i < DATA_LENGTH; i++)
  147. // {
  148. // PAIR_CHARGERPIN_PRINT("txbuf.%x\r\n", txbuf[i]);
  149. // }
  150. // break;
  151. // }
  152. // if (delay_reply_count != 0)
  153. // {
  154. // delay_reply_count++;
  155. // }
  156. // if (delay_reply_count > DELAY_REPLY)
  157. // {
  158. // unsigned char macbuff[16];
  159. // delay_reply_count = 0;
  160. // PAIR_CHARGERPIN_PRINT("BEGIN send MAC.%d\r\n", ms);
  161. // Get_MACaddr(macbuff);
  162. // memcpy(txbuf, &macbuff[3], DATA_LENGTH);
  163. // txbuf[DATA_LENGTH - 1] = 0;
  164. // for (int i = 0; i < DATA_LENGTH - 1; i++)
  165. // {
  166. // txbuf[DATA_LENGTH - 1] += txbuf[i];
  167. // }
  168. // PAIR_CHARGERPIN_PRINT("host_mac send.%d\r\n", ms);
  169. // one_ms_status = 2;
  170. // txrxcount = 0;
  171. // for (int i = 0; i < DATA_LENGTH; i++)
  172. // {
  173. // PAIR_CHARGERPIN_PRINT("txbuf.%x\r\n", txbuf[i]);
  174. // }
  175. // break;
  176. // }
  177. // if (delay_check_count != 0)
  178. // {
  179. // delay_check_count++;
  180. // }
  181. // if (delay_check_count > DELAY_CHECK)
  182. // {
  183. // delay_check_count = 0;
  184. // one_ms_status = 4;
  185. // txrxcount = 0;
  186. // PAIR_CHARGERPIN_PRINT("CHECK start.%d\r\n", ms);
  187. // break;
  188. // }
  189. // else if (delay_check_count > 0)
  190. // {
  191. // if (now_bit > 0)
  192. // {
  193. // delay_check_count = 0;
  194. // PAIR_CHARGERPIN_PRINT("CANCEL CHECK.%d\r\n", ms);
  195. // }
  196. // }
  197. // break;
  198. // case 2://tx
  199. // {
  200. // lowbitdelay++;
  201. // static char send_interrupt = 0;
  202. // if ((now_bit == 0) && (nrf_gpio_pin_read(PIN_CHARGING)) && (lowbitdelay>LOW_BIT_DELAY))
  203. // {
  204. // if (mFlash.isHost)
  205. // {
  206. // one_ms_status = 1;
  207. // nrf_gpio_pin_clear(PIN_CHARGING);
  208. // PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  209. // delay_ledon_count = 0;
  210. // PAIR_CHARGERPIN_PRINT("delay_ledon_count1 = 0;\r\n", txrxcount);
  211. // PAIR_CHARGERPIN_PRINT("EXTI SEND MODE.CAN NOT SET LOW %d\r\n", txrxcount);
  212. // send_interrupt = 0;
  213. // break;
  214. // }
  215. // else
  216. // {
  217. // nrf_gpio_pin_clear(PIN_CHARGING);
  218. // PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  219. // send_interrupt = 1;
  220. // }
  221. // }
  222. // if ((txrxcount == 0) && (send_interrupt == 0))
  223. // {
  224. // nrf_gpio_pin_set(PIN_CHARGING);
  225. // PAIR_CHARGERPIN_PRINT("set.%d\r\n", ms);
  226. // now_bit = 1;
  227. // }
  228. // if ((txrxcount == 1.5 * BITWIDTH) && (send_interrupt == 0))
  229. // {
  230. // nrf_gpio_pin_clear(PIN_CHARGING);
  231. // PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  232. // now_bit = 0;
  233. // lowbitdelay=0;
  234. // }
  235. // if ((txrxcount >= (int)(2.5 * BITWIDTH) && (txrxcount - (int)(2.5 * BITWIDTH)) % BITWIDTH == 0) && (send_interrupt == 0))
  236. // {
  237. // bytesindes = ((txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH) / 8;
  238. // bitindex = 7 - ((txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH) % 8;
  239. // nrf_gpio_pin_write(PIN_CHARGING, (txbuf[bytesindes] >> bitindex) & 0x01);
  240. // lowbitdelay=0;
  241. // PAIR_CHARGERPIN_PRINT("write.%d %d\r\n", ((txbuf[bytesindes] >> bitindex) & 0x01), ms);
  242. // now_bit = (txbuf[bytesindes] >> bitindex) & 0x01;
  243. // PAIR_CHARGERPIN_PRINT("%d %d %d %d %x\r\n", (txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH
  244. // , bytesindes
  245. // , bitindex
  246. // , (txbuf[bytesindes] >> bitindex) & 0x01
  247. // , txbuf[bytesindes]
  248. // );
  249. // }
  250. // if (send_interrupt)
  251. // {
  252. // if (nrf_gpio_pin_read(PIN_CHARGING) == 0)
  253. // {
  254. // send_interrupt = 0;
  255. // delay_ledon_count = DELAY_SEND;
  256. // PAIR_CHARGERPIN_PRINT("delay_ledon_count = DELAY_SEND;.%d\r\n", txrxcount);
  257. // PAIR_CHARGERPIN_PRINT("RX SEND MODE MAC START.%d\r\n", txrxcount);
  258. // txrxcount = 0;
  259. // break;
  260. // }
  261. // }
  262. //
  263. // if (txrxcount == BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1)
  264. // {
  265. // nrf_gpio_pin_clear(PIN_CHARGING);
  266. // PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  267. // if (mFlash.isHost == 0)
  268. // {
  269. // delay_check_count = 1;
  270. // }
  271. // }
  272. // if(delay_check_count!=0)delay_check_count++;
  273. // if (txrxcount >= BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1 + LOW_BIT_DELAY)
  274. // {
  275. // one_ms_status = 1;
  276. // send_interrupt = 0;
  277. // now_bit = 0;
  278. // bit_width = 0;
  279. // PAIR_CHARGERPIN_PRINT("SEDN DONE .%d pin one_ms_status %d\r\n", ms,nrf_gpio_pin_read(PIN_CHARGING));
  280. // break;
  281. // }
  282. // txrxcount++;
  283. // }
  284. // break;
  285. // case 3://rx
  286. // if (now_bit == nrf_gpio_pin_read(PIN_CHARGING))
  287. // {
  288. // bit_width++;
  289. // }
  290. // else
  291. // {
  292. // if (now_bit == 1)
  293. // {
  294. // now_bit = 0;
  295. // if ((bit_width > 1.5 * BITWIDTH - 3) && (bit_width < 1.8 * BITWIDTH + 3))
  296. // {
  297. // txrxcount = 1.5 * BITWIDTH;
  298. // value = 30;
  299. // delay_ledon_count = DELAY_SEND + 1.5 * BITWIDTH;
  300. // PAIR_CHARGERPIN_PRINT("DELAY_SEND + 1.5 * BITWIDTHt .%d\r\n", txrxcount);
  301. // PAIR_CHARGERPIN_PRINT("RX MODE restart .%d\r\n", ms);
  302. // }
  303. // }
  304. // else
  305. // {
  306. // now_bit = 1;
  307. // }
  308. // bit_width = 0;
  309. // }
  310. // if (txrxcount >= (int)(3.5 * BITWIDTH) && (txrxcount - (int)(3.5 * BITWIDTH)) % BITWIDTH == 0)
  311. // {
  312. // bytesindes = ((txrxcount - (int)(3.5 * BITWIDTH)) / BITWIDTH) / 8;
  313. // bitindex = 7 - ((txrxcount - (int)(3.5 * BITWIDTH)) / BITWIDTH) % 8;
  314. // if (value > 0)
  315. // {
  316. // rxbuf[bytesindes] |= 0x01 << bitindex;
  317. // }
  318. // else
  319. // {
  320. // rxbuf[bytesindes] &= ~(0x01 << bitindex);
  321. // }
  322. // PAIR_CHARGERPIN_PRINT("%d %d %d %d %x\r\n", txrxcount
  323. // , bytesindes
  324. // , bitindex
  325. // , value
  326. // , rxbuf[bytesindes]
  327. // );
  328. // value = 0;
  329. // nrf_gpio_pin_toggle(PIN_LED_RUN);
  330. // }
  331. // else
  332. // {
  333. // if (now_bit)
  334. // {
  335. // value++;
  336. // }
  337. // else
  338. // {
  339. // value--;
  340. // }
  341. // }
  342. // if (txrxcount >= BITWIDTH * (8 * DATA_LENGTH + 2.5))
  343. // {
  344. // unsigned char dataerrcheck = 0;
  345. // unsigned char crc = 0;
  346. // one_ms_status = 1;
  347. // PAIR_CHARGERPIN_PRINT("RECEVIC DONE .%d\r\n", txrxcount);
  348. // nrf_gpio_pin_clear(PIN_LED_RUN);
  349. // PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  350. // for (int i = 0; i < DATA_LENGTH - 1; i++)
  351. // {
  352. // crc += rxbuf[i];
  353. // }
  354. // for (int i = 0; i < DATA_LENGTH - 1; i++)
  355. // {
  356. // if (rxbuf[i] == 0)
  357. // {
  358. // dataerrcheck++;
  359. // }
  360. // }
  361. // if (dataerrcheck == DATA_LENGTH - 1)
  362. // {
  363. // PAIR_CHARGERPIN_PRINT("CRC ERROR ALL 0x00.%x\r\n", crc);
  364. // delay_ledon_count = 0;
  365. // PAIR_CHARGERPIN_PRINT("delay_ledon_count 2= 0;\r\n", txrxcount);
  366. // break;
  367. // }
  368. // dataerrcheck = 0;
  369. // for (int i = 0; i < DATA_LENGTH - 1; i++)
  370. // {
  371. // if (rxbuf[i] == 0xff)
  372. // {
  373. // dataerrcheck++;
  374. // }
  375. // }
  376. // if (dataerrcheck == DATA_LENGTH - 1)
  377. // {
  378. // PAIR_CHARGERPIN_PRINT("CRC ERROR ALL 0xFF.%x\r\n", crc);
  379. // delay_ledon_count = 0;
  380. // PAIR_CHARGERPIN_PRINT("delay_ledon_count 3= 0;\r\n", txrxcount);
  381. // break;
  382. // }
  383. // if (crc == rxbuf[DATA_LENGTH - 1])
  384. // {
  385. // if (mFlash.isHost)
  386. // {
  387. // delay_check_count = 1; //延时校验计数器
  388. // }
  389. // else
  390. // {
  391. // delay_reply_count = 1; //延时回复计数器
  392. // }
  393. // PAIR_CHARGERPIN_PRINT("---------------CRC SUCCESS .%x----------------\r\n", crc);
  394. // }
  395. // else
  396. // {
  397. // PAIR_CHARGERPIN_PRINT("CRC ERROR .%x\r\n", crc);
  398. // delay_ledon_count = 0;
  399. // PAIR_CHARGERPIN_PRINT("delay_ledon_count 4= 0;\r\n", txrxcount);
  400. // }
  401. // for (int i = 0; i < DATA_LENGTH; i++)
  402. // {
  403. // PAIR_CHARGERPIN_PRINT("rxbuf[%d].%x\r\n",i, rxbuf[i]);
  404. // }
  405. // }
  406. // txrxcount++;
  407. // break;
  408. // case 4:
  409. // if (nrf_gpio_pin_read(PIN_CHARGING))
  410. // {
  411. // value++;
  412. // }
  413. // else
  414. // {
  415. // value--;
  416. // }
  417. // if (mFlash.isHost)
  418. // {
  419. // if (txrxcount == 0)
  420. // {
  421. // value = 0;
  422. // nrf_gpio_pin_set(PIN_CHARGING);
  423. // PAIR_CHARGERPIN_PRINT("set.%d\r\n", ms);
  424. // }
  425. // if (txrxcount == 3 * BITWIDTH)
  426. // {
  427. // nrf_gpio_pin_clear(PIN_CHARGING);
  428. // PAIR_CHARGERPIN_PRINT("clear.%d\r\n", delay_ledon_count);
  429. // }
  430. // if (txrxcount >= 3 * BITWIDTH + LOW_BIT_DELAY)
  431. // {
  432. // value = 0;
  433. // one_ms_status = 1;
  434. // now_bit = 0;
  435. // bit_width = 0;
  436. // }
  437. // }
  438. // else
  439. // {
  440. // if (txrxcount == 0)
  441. // {
  442. // value = 0;
  443. // }
  444. // if (txrxcount > 3 * BITWIDTH)
  445. // {
  446. // if (value < 2 * BITWIDTH)
  447. // {
  448. // PAIR_CHARGERPIN_PRINT("CHECK ERROR .%d\r\n", txrxcount);
  449. // delay_ledon_count = 0;
  450. // PAIR_CHARGERPIN_PRINT("delay_ledon_count 5= 0;\r\n", txrxcount);
  451. // }
  452. // else
  453. // {
  454. // PAIR_CHARGERPIN_PRINT("CHECK DONE .%d\r\n", txrxcount);
  455. // }
  456. // value = 0;
  457. // one_ms_status = 1;
  458. // nrf_gpio_pin_clear(PIN_CHARGING);
  459. // PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  460. // now_bit = 1;
  461. // bit_width = 0;
  462. // }
  463. // }
  464. // txrxcount++;
  465. // break;
  466. // }
  467. // if (delay_ledon_count != 0)
  468. // {
  469. // delay_ledon_count++;
  470. // }
  471. // if (delay_ledon_count == DELAY_LEDON)
  472. // {
  473. // PAIR_CHARGERPIN_PRINT("LEDCOPEN.%d\r\n", ms);
  474. // memcpy(savebuf,rxbuf,DATA_LENGTH-1);
  475. // writefig=1;
  476. // }
  477. // if ((delay_ledon_count > DELAY_LEDON )
  478. // && (delay_ledon_count - DELAY_LEDON == LEDON_KEEP))
  479. // {
  480. // delay_ledon_count = 0;
  481. // PAIR_CHARGERPIN_PRINT("delay_ledon_count 6= 0;\r\n", txrxcount);
  482. // PAIR_CHARGERPIN_PRINT("LEDCLOSE.%d\r\n", ms);
  483. // }
  484. //}
  485. //void pair_led_pcs(void)
  486. //{
  487. // leddisplay_count++;
  488. // if(leddisplay_count%2==0)
  489. // {
  490. // LED_Start(LED_PAIR,COLOR_BLACK);
  491. // PAIR_CHARGERPIN_PRINT("LED_Start(LED_PAIR,COLOR_BLACK);.%d\r\n");
  492. // }
  493. // else
  494. // {
  495. // LED_Start(LED_PAIR,COLOR_WHITE);
  496. // }
  497. // if(leddisplay_count>20)
  498. // {
  499. // leddisplay_count=0;
  500. // LED_Stop(LED_PAIR);PAIR_CHARGERPIN_PRINT("!!!!!!!!---->>>LED_Stop(LED_PAIR);\r\n");
  501. // Process_SetHoldOn(pair_led_pcs,0);
  502. // Process_Stop(pair_led_pcs);
  503. // }
  504. //}
  505. //void stop_pair_led(void)
  506. //{
  507. // if(leddisplay_count==0)return;
  508. // Process_SetHoldOn(pair_led_pcs,0);
  509. // Process_Stop(pair_led_pcs);
  510. // leddisplay_count=0;
  511. //}
  512. //PAIR_START_REGISTER(stop_pair_led);
  513. //void mtrun(void)
  514. //{
  515. // //MT_Run(200);
  516. //}
  517. //PAIR_START_REGISTER(mtrun);
  518. ////写配对相关的flish
  519. //static int pair_writeflish(unsigned char* macAddr_L, unsigned char* macAddr_R)
  520. //{
  521. // uint8_t offest =0;
  522. // if(mFlash.isHost){offest =3;}
  523. //
  524. // if (memcmp(macAddr_R, mFlash.mClient.macAddr+offest, 3) != 0)
  525. // {
  526. // goto writein;
  527. // }
  528. // if (mFlash.mClient.isConfig != 'C')
  529. // {
  530. // goto writein;
  531. // }
  532. // PAIR_CHARGERPIN_PRINT("mFlash.mClient.isConfig:%X\n", mFlash.mClient.isConfig);
  533. // PAIR_CHARGERPIN_PRINT("mFlash.mClient.macAddr:%02X %02X %02X\n", mFlash.mClient.macAddr[0], mFlash.mClient.macAddr[1], mFlash.mClient.macAddr[2]);
  534. // PAIR_CHARGERPIN_PRINT("mFlash.macHost:%02X %02X %02X\n", mFlash.macHost[0], mFlash.macHost[1], mFlash.macHost[2]);
  535. // return 0;
  536. //writein:
  537. // mFlash.mClient.isConfig = 'C';
  538. // uint8_t i=0;
  539. // for (i = 0; i < 6; i++){mFlash.macHost[i] = macAddr_L[i];}
  540. //
  541. // for (i = 0; i < 3; i++)
  542. // mFlash.mClient.macAddr[i+offest] = macAddr_R[i]; //从机自身mac地址
  543. //
  544. // //保存数据到备份区域里面
  545. // for( i=0; i<RecordMacAddrL; i++){
  546. // mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  547. // mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i];//从机地址
  548. // }
  549. //
  550. // mBackup.hardVersion = mFlash.mClient.hardVersion;
  551. // mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  552. // mBackup.isConfig = mFlash.mClient.isConfig;
  553. //
  554. // if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Pair,"pair save backup fail");
  555. //
  556. // DEBUG_LOG("mFlash.mClient.isConfig:%X\n", mFlash.mClient.isConfig);
  557. // DEBUG_LOG("mFlash.mClient.macAddr:%02X %02X %02X\n", mFlash.mClient.macAddr[0], mFlash.mClient.macAddr[1], mFlash.mClient.macAddr[2]);
  558. // DEBUG_LOG("mFlash.macHost:%02X %02X %02X \n", mFlash.macHost[0], mFlash.macHost[1], mFlash.macHost[2]);
  559. // if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair,"pair clear step fail");return 0;}
  560. // memset(&mFlash.mStep,0,sizeof(FlashStep_t));
  561. // if(Flash_SaveInfomation() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair,"Flash_SaveInfomation fail");return 0;}
  562. // return 1;
  563. //}
  564. //static void load_adv_name_from_flish(void)
  565. //{
  566. // char buf[16];
  567. // memset(buf, 0, sizeof(buf));
  568. // advertising_stop();
  569. // 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]);
  570. // DEBUG_LOG("advName(%d):%s\n", strlen(buf), buf);
  571. // slave_set_adv_name(buf, strlen(buf));
  572. // slave_adv_init();
  573. //}
  574. //static void load_scan_name_from_flish(void)
  575. //{
  576. // char buf[16];
  577. // memset(buf, 0, sizeof(buf));
  578. // 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]);
  579. // DEBUG_LOG("scanName1(%d):%s\n", strlen(buf), buf);
  580. // host_set_scan_name(buf, strlen(buf));
  581. //}
  582. //static unsigned char reconnect_pcs_status=0;
  583. //static void reconnect_pcs(void)
  584. //{
  585. // switch(reconnect_pcs_status)
  586. // {
  587. // case 0:
  588. // if(mFlash.isHost)
  589. // {
  590. // reconnect_pcs_status=1;
  591. // if(host_isconnect()==1)host_disconnect();
  592. // }
  593. // else
  594. // {
  595. // reconnect_pcs_status=2;
  596. // if(slave_isconnect()==1)slave_disconnect();
  597. // }
  598. // break;
  599. // case 1:
  600. // if(host_isconnect()==0)
  601. // {
  602. // load_scan_name_from_flish();
  603. // scan_start();
  604. // reconnect_pcs_status=4;
  605. // }
  606. // break;
  607. // case 2:
  608. // if(slave_isconnect()==0)
  609. // {
  610. // load_adv_name_from_flish();
  611. // advertising_start();
  612. // reconnect_pcs_status=4;
  613. // }
  614. // break;
  615. // case 4:
  616. // Process_SetHoldOn(reconnect_pcs,0);
  617. // Process_Stop(reconnect_pcs);
  618. // reconnect_pcs_status=5;
  619. // leddisplay_count =0;
  620. // Process_Start(500,"pair_led_pcs",pair_led_pcs);
  621. // Process_SetHoldOn(pair_led_pcs,1);
  622. // break;
  623. // case 5:
  624. //
  625. // break;
  626. // }
  627. //}
  628. //static void app_pair_chargerpin_pcs(void)
  629. //{
  630. // int rev=0;
  631. // if(writefig==1)
  632. // {
  633. // unsigned char Lbuff[6];
  634. // unsigned char Rbuff[3];
  635. // Get_MACaddr(Lbuff);
  636. // Rbuff[0]=savebuf[0];Rbuff[1]=savebuf[1];Rbuff[2]=savebuf[2];
  637. // rev=pair_writeflish(Lbuff,Rbuff);
  638. //
  639. // if(rev)
  640. // {
  641. // Process_Start(10,"reconnect_pcs",reconnect_pcs);
  642. // Process_SetHoldOn(reconnect_pcs,1);
  643. // reconnect_pcs_status=0;
  644. // }
  645. // else
  646. // {
  647. // leddisplay_count =0;
  648. // Process_Start(500,"pair_led_pcs",pair_led_pcs);
  649. // Process_SetHoldOn(pair_led_pcs,1);
  650. // }
  651. // writefig=0;
  652. // }
  653. //}
  654. //void pair_start_pcs(void)
  655. //{
  656. // extern void *pair_start$$Base;
  657. // extern void *pair_start$$Limit;
  658. // typedef void (*main_init_t)(void);
  659. // for (int p = (unsigned int)&pair_start$$Base; p < (unsigned int)&pair_start$$Limit; p += 4)
  660. // {
  661. // (*(main_init_t *)p)();
  662. // }
  663. //}
  664. //void pair_done_pcs(void)
  665. //{
  666. // extern void *pair_done$$Base;
  667. // extern void *pair_done$$Limit;
  668. // typedef void (*main_init_t)(void);
  669. // for (int p = (unsigned int)&pair_done$$Base; p < (unsigned int)&pair_done$$Limit; p += 4)
  670. // {
  671. // (*(main_init_t *)p)();
  672. // }
  673. //}
  674. //typedef enum
  675. //{
  676. // CHARGE=0,
  677. // CONNECT_NO_CHARGE,
  678. // DISCONNECT_CHARGE,
  679. //}pair_line_t;
  680. //#define ADC_CHECKMS 1000
  681. //#define DELAY_PAIR_COUNT 100
  682. //void null_pcs(void)
  683. //{
  684. //}
  685. //short left_adc_value=0;
  686. //void adc_cab(short adc_value)
  687. //{
  688. // left_adc_value=adc_value;
  689. // adc_check();
  690. //
  691. // PAIR_CHARGERPIN_PRINT("ADC_Read_CallBack_Once call -%d\n",adc_value);
  692. //
  693. //}
  694. //unsigned int pulluppcs_timer=0;
  695. //void tim_read_cb(void *t)
  696. //{
  697. // if(TIME_GetTicks()-pulluppcs_timer>=3)
  698. // {
  699. // PAIR_CHARGERPIN_PRINT("tim_read_cb 3ms \n",mFlash.isHost);
  700. // if(ADC_OP_SUCCESS!=ADC_Read_CallBack_Once(PIN_CHARGING_CHANNEL,adc_cab))
  701. // {
  702. // adc_check();
  703. // PAIR_CHARGERPIN_PRINT("adc_check init \n",mFlash.isHost);
  704. // }
  705. // Process_Stop(null_pcs);
  706. // TIME_UnRegist(tim_read_cb);
  707. // }
  708. //}
  709. //static pair_line_t pair_line = DISCONNECT_CHARGE;
  710. //void Delegate_up_pin(void *t)
  711. //{
  712. //typedef void (*cbdull)(void);
  713. // cbdull cbh = adc_check;
  714. // if(TIME_GetTicks()-pulluppcs_timer>=3)
  715. // {
  716. // TIME_UnRegist(Delegate_up_pin);
  717. // Process_Stop(null_pcs);
  718. // PAIR_CHARGERPIN_PRINT("Delegate_up_pin %d\n",mFlash.isHost);
  719. // }
  720. //}
  721. //void adc_check(void)
  722. //{
  723. // static unsigned char status=0;
  724. // short ADC_value=0;
  725. //
  726. // static unsigned int delay_pair_count=0;
  727. // static unsigned int overtime=0;
  728. // static unsigned char ishost;
  729. // switch(status)
  730. // {
  731. // case 0:
  732. // ishost=mFlash.isHost;
  733. // ADC_Disable();
  734. // ADC_SetPinChannel(PIN_CHARGING,PIN_CHARGING_CHANNEL,NRF_GPIO_PIN_NOPULL);
  735. // ADC_Initialize();
  736. // Process_UpdatePeroid(adc_check,ADC_CHECKMS);
  737. // Process_SetHoldOn(adc_check,0);
  738. // nrf_gpio_pin_write(PIN_LED_RUN, 0);
  739. // pair_done_pcs();
  740. // status=1;
  741. // break;
  742. // case 1:
  743. // if(ishost!=mFlash.isHost)
  744. // {
  745. // status=0;
  746. // PAIR_CHARGERPIN_PRINT("mFlash.isHost change %d\n",mFlash.isHost);
  747. // }
  748. //
  749. // if(ADC_OP_SUCCESS!=ADC_Read(PIN_CHARGING_CHANNEL,&ADC_value))
  750. // {
  751. // PAIR_CHARGERPIN_PRINT("ADC_Read ERROR %d\n",mFlash.isHost);
  752. // }
  753. //
  754. // if(mFlash.isHost)
  755. // {
  756. // pulluppcs_timer=TIME_GetTicks();
  757. // nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_NOPULL);
  758. // }
  759. //
  760. // PAIR_CHARGERPIN_PRINT( "ADC ( %d ):isHost %d pair_line %d\n", ADC_value,mFlash.isHost,pair_line);
  761. //
  762. // if((ADC_value<CHARGE_ADC)&&(pair_line == CHARGE)&&(mFlash.isHost))
  763. // {
  764. //// Process_Stop(charge_uppin_pcs);
  765. // PAIR_CHARGERPIN_PRINT("--------------------------------------------------------EXIT CHARGE_ADC \n");
  766. // }
  767. //
  768. // if(ADC_value>=CHARGE_ADC)
  769. // {
  770. // PAIR_CHARGERPIN_PRINT(" CHARGE_ADC \n");
  771. // if((pair_line != CHARGE)&&(mFlash.isHost))
  772. // {
  773. // PAIR_CHARGERPIN_PRINT("--------------------------------------------------------------------INTO CHARGE_ADC \n");
  774. //// Process_Start(ADC_CHECKMS,"charge_uppin_pcs",charge_uppin_pcs);
  775. // }
  776. // pair_line=CHARGE;
  777. // }
  778. // else if((ADC_value>=CONN_MIN_ADC)&&(ADC_value<=CONN_MAX_ADC)&&(mFlash.isHost==0))
  779. // {
  780. // PAIR_CHARGERPIN_PRINT(" CONN_ADC %d \n",pair_line);
  781. // connect:
  782. // if(pair_line!=CONNECT_NO_CHARGE)
  783. // {
  784. // PAIR_CHARGERPIN_PRINT(" Process_SetHoldOn(adc_check,1); ms %d\n",TIME_GetTicks());
  785. //
  786. // if(mFlash.isHost)Process_Start(ADC_CHECKMS,"adc_check",adc_check);
  787. //
  788. // Process_UpdatePeroid(adc_check,10);
  789. // Process_SetHoldOn(adc_check,1);
  790. // overtime=TIME_GetTicks();
  791. // pair_start_pcs();
  792. //
  793. // if(mFlash.isHost)
  794. // {
  795. // status=2;
  796. // nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_PULLUP);
  797. // delay_pair_count=TIME_GetTicks();
  798. // PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG HIGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH; %d \n",TIME_GetTicks());
  799. // }
  800. // else
  801. // {
  802. // status=5;
  803. // TIME_Regist(chargerpin_one_ms_pcs);
  804. // one_ms_status = 0;
  805. // }
  806. // }
  807. // pair_line=CONNECT_NO_CHARGE;
  808. // }
  809. // else
  810. // {
  811. // if(mFlash.isHost)
  812. // {
  813. // if((ADC_value<=DISCON_L_MAX_ADC)&&(ADC_value>=DISCON_L_MIN_ADC))
  814. // {
  815. // PAIR_CHARGERPIN_PRINT("L DISCON_L_ADC \n");
  816. // pair_line=DISCONNECT_CHARGE;
  817. // }
  818. // else if((ADC_value<=CON_L_MAX_ADC)&&(ADC_value>=CON_L_MIN_ADC) && !MT_GetState())
  819. // {
  820. // PAIR_CHARGERPIN_PRINT("L CON_L_MAX_ADC \n");
  821. // goto connect;
  822. // }
  823. // }
  824. // else
  825. // {
  826. // if(ADC_value<=DISCON_R_MAX_ADC)
  827. // {
  828. // PAIR_CHARGERPIN_PRINT("R DISCON_R_MAX_ADC \n");
  829. // pair_line=DISCONNECT_CHARGE;
  830. // }
  831. // }
  832. // }
  833. // break;
  834. // case 2://
  835. // if(TIME_GetTicks()- delay_pair_count > ADC_CHECKMS)
  836. // {
  837. // TIME_Regist(chargerpin_one_ms_pcs);
  838. // one_ms_status = 0;
  839. // status=3;
  840. // PAIR_CHARGERPIN_PRINT(" TIME_Regist(chargerpin_one_ms_pcs); %d \n",TIME_GetTicks());
  841. // }
  842. // break;
  843. // case 3:
  844. // if(TIME_GetTicks()- delay_pair_count > DELAY_PAIR_COUNT)
  845. // {
  846. // nrf_gpio_pin_set(PIN_CHARGING);
  847. // PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG HIG; %d \n",TIME_GetTicks());
  848. // status=4;
  849. // }
  850. // break;
  851. // case 4:
  852. // if(TIME_GetTicks()- delay_pair_count>(DELAY_PAIR_COUNT+100))
  853. // {
  854. // nrf_gpio_pin_clear(PIN_CHARGING);
  855. // PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG LOW; %d \n",TIME_GetTicks());
  856. // status=5;
  857. // }
  858. // break;
  859. // case 5:
  860. // if(writefig==1)
  861. // {
  862. // app_pair_chargerpin_pcs();
  863. // TIME_UnRegist(chargerpin_one_ms_pcs);
  864. // status=0;
  865. //// MT_Run(500);
  866. // PAIR_CHARGERPIN_PRINT(" ---------------------------PAIR SUCESS------------- \n");
  867. // }
  868. // if(TIME_GetTicks()-overtime > DELAY_LEDON+ADC_CHECKMS + DELAY_PAIR_COUNT + 100 + 300)
  869. // {
  870. // TIME_UnRegist(chargerpin_one_ms_pcs);
  871. // status=0;
  872. // PAIR_CHARGERPIN_PRINT(" ---------------------------PAIR OVER TIME %d------------- \n",TIME_GetTicks());
  873. // }
  874. // break;
  875. // }
  876. //}
  877. //void write_on(void)
  878. //{
  879. // LED_Stop(LED_PAIR);
  880. // LED_Start(LED_PAIR,COLOR_WHITE);
  881. // PAIR_CHARGERPIN_PRINT(" --------------------------LED_Start(LED_PAIR,COLOR_WHITE)------------- \n");
  882. //}
  883. //PAIR_START_REGISTER(write_on);
  884. //static volatile uint32_t adctim =0;
  885. ////void app_adc_test(void){
  886. //// int16_t adc =0;
  887. //// uint8_t state =0;
  888. //// uint32_t errcode = ADC_ReadChargePin(PIN_CHARGING_CHANNEL);
  889. //// if(errcode==0){
  890. //// Process_SetHoldOn(app_adc_test,1);
  891. //// }
  892. //// DEBUG_LOG(">>>>> app_adc_test:%d\r\n",errcode);
  893. //// state = bsp_Get_ChargePinADC(&adc);
  894. //// if(state){
  895. //// Process_SetHoldOn(app_adc_test,0);
  896. //// nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_NOPULL);
  897. //// DEBUG_LOG(">>>>> ReadAdc:%d\r\n",adc);
  898. //// }
  899. ////}
  900. ////void ReadAdc_timcallback(void* t){
  901. ////
  902. //// int16_t adc =0;
  903. //// uint8_t state =0;
  904. //// static volatile uint8_t temp =0;
  905. //// if(0 == temp){temp =1;
  906. //// nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_PULLUP);
  907. //// adctim = TIME_GetTicks();
  908. //// Process_SetHoldOn(app_adc_test,1);
  909. //// }
  910. //// else if(TIME_GetTicks() - adctim >= 3){adctim = TIME_GetTicks();
  911. //// uint32_t errcode = ADC_ReadChargePin(PIN_CHARGING_CHANNEL);
  912. //// if(errcode){
  913. //// temp =0;
  914. //// TIME_Regist(ReadAdc_timcallback);
  915. //// nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_NOPULL);
  916. //// DEBUG_LOG(">>>>> ReadAdc:%d\r\n",adc);
  917. //// }
  918. //// }
  919. ////}
  920. //static volatile uint8_t temp =0;
  921. //void app_adc_test(void){
  922. // nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_PULLUP);
  923. // temp =0;
  924. // adctim = TIME_GetTicks();
  925. // Process_SetHoldOn(app_adc_test,1);
  926. //}
  927. //void ReadAdc_timcallback(void* t){
  928. //
  929. // int16_t adc =0;
  930. // uint8_t state =0;
  931. //
  932. // if(0 == temp){
  933. // if(TIME_GetTicks() - adctim >= 3){
  934. // uint32_t errcode = ADC_ReadChargePin(PIN_CHARGING_CHANNEL);
  935. // if(ADC_OP_SUCCESS == errcode){temp =1;
  936. // }
  937. // }
  938. // }
  939. // else if(temp ==1){
  940. // state = bsp_Get_ChargePinADC(&adc);
  941. // if(state){temp =2;
  942. // Process_SetHoldOn(app_adc_test,0);
  943. // nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_NOPULL);
  944. // DEBUG_LOG(">>>>> ReadAdc:%d\r\n",adc);
  945. // }
  946. // }
  947. //
  948. //}
  949. //void app_pair_chargerpin_Init(void)
  950. //{
  951. // Process_Start(ADC_CHECKMS,"adc_check",adc_check);
  952. //// Process_Start(1000,"app_adc_test",app_adc_test);
  953. //// TIME_Regist(ReadAdc_timcallback);
  954. //// ADC_Disable();
  955. //// ADC_SetPinChannel(PIN_CHARGING,PIN_CHARGING_CHANNEL,NRF_GPIO_PIN_NOPULL);
  956. //// ADC_Initialize();
  957. //}
  958. #include "usr_config.h"
  959. #include "bsp_time.h"
  960. #include "system.h"
  961. #include "nrf_gpio.h"
  962. #include "app_charge.h"
  963. #include "app_flash.h"
  964. #include "ble_comm.h"
  965. #include "bsp_adc.h"
  966. #include "app_pair_chargerpin.h"
  967. #include "hal_led.h"
  968. #include "hal_charge.h"
  969. #include "hal_mt.h"
  970. extern void adc_check(void);
  971. //发送协议
  972. //Host MAC[0] MAC[1] MAC[2]>>>>>> Client
  973. //Client MAC[3] MAC[4] MAC[5]>>>>>> Host
  974. #define HOST_PEAR 1
  975. #define PIN_LED_RUN 2
  976. #define DATA_LENGTH 4
  977. #define BITWIDTH 30
  978. #define LOW_BIT_DELAY 15
  979. #define DELAY_SEND 100
  980. #define DELAY_REPLY 100
  981. #define DELAY_CHECK 100
  982. #define DELAY_LEDON (BITWIDTH*(8*DATA_LENGTH+3)*2+DELAY_SEND+DELAY_REPLY+DELAY_CHECK+5*BITWIDTH)
  983. #define LEDON_KEEP 100
  984. static uint8_t leddisplay_count=0;
  985. unsigned static char rxbuf[DATA_LENGTH];
  986. unsigned static char txbuf[DATA_LENGTH];
  987. __IO static char writefig=0;
  988. unsigned static char savebuf[DATA_LENGTH];
  989. static char one_ms_status = 0;
  990. static void chargerpin_one_ms_pcs(void* t)
  991. {
  992. unsigned int ms = *(unsigned int*)t;
  993. static unsigned int bit_width = 0;
  994. static unsigned int now_bit = 0;
  995. static unsigned int delay_send_count = 0;
  996. static unsigned int delay_reply_count = 0;
  997. static unsigned int delay_check_count = 0;
  998. static unsigned int delay_ledon_count = 0;
  999. static int bitindex = 0;
  1000. static int bytesindes = 0;
  1001. static int value = 0;
  1002. static int txrxcount = 0;
  1003. static char lowbitdelay=0;
  1004. switch (one_ms_status)
  1005. {
  1006. case 0://init
  1007. bit_width = 0;
  1008. now_bit = 0;
  1009. delay_send_count = 0;
  1010. delay_reply_count = 0;
  1011. delay_check_count = 0;
  1012. delay_ledon_count = 0;
  1013. bitindex = 0;
  1014. bytesindes = 0;
  1015. value = 0;
  1016. txrxcount = 0;
  1017. one_ms_status = 0;
  1018. lowbitdelay=0;
  1019. nrf_gpio_cfg(
  1020. PIN_CHARGING,
  1021. NRF_GPIO_PIN_DIR_OUTPUT,
  1022. NRF_GPIO_PIN_INPUT_CONNECT,
  1023. NRF_GPIO_PIN_NOPULL,
  1024. NRF_GPIO_PIN_D0H1,
  1025. NRF_GPIO_PIN_NOSENSE);
  1026. nrf_gpio_cfg_watcher(PIN_CHARGING);
  1027. one_ms_status = 1;
  1028. nrf_gpio_pin_clear(PIN_CHARGING);
  1029. PAIR_CHARGERPIN_PRINT("nrf_gpio_cfg_watcher(PIN_CHARGING); clear.%d\r\n", ms);
  1030. break;
  1031. case 1:
  1032. if (now_bit == nrf_gpio_pin_read(PIN_CHARGING))
  1033. {
  1034. bit_width++;
  1035. }
  1036. else
  1037. {
  1038. //由高电平切换到低电平,下降信号
  1039. if (now_bit == 1)
  1040. {
  1041. now_bit = 0;
  1042. if ((bit_width > 1.5 * BITWIDTH - LOW_BIT_DELAY/3) && (bit_width < 1.5 * BITWIDTH + LOW_BIT_DELAY/3))
  1043. {
  1044. one_ms_status = 3;
  1045. txrxcount = 1.5 * BITWIDTH;
  1046. value = 30;
  1047. if (mFlash.isHost == 0)
  1048. {
  1049. delay_ledon_count = 1.5 * BITWIDTH + DELAY_SEND;
  1050. PAIR_CHARGERPIN_PRINT("delay_ledon_count = 1.5 * BITWIDTH + DELAY_SEND.%d\r\n", txrxcount);
  1051. }
  1052. else
  1053. {
  1054. delay_ledon_count = 2.5 * BITWIDTH + DELAY_SEND + BITWIDTH * 8 * DATA_LENGTH + 1.5 * BITWIDTH + DELAY_REPLY;
  1055. PAIR_CHARGERPIN_PRINT("delay_ledon_count = 2.5 * BITWIDTH + DELAY_SEND + BITWIDTH*8*DATA_LENGTH + 1.5 * BITWIDTH + DELAY_REPLY .%d\r\n", txrxcount);
  1056. }
  1057. PAIR_CHARGERPIN_PRINT("-----------------------RX MODE start .%d\r\n", bit_width);
  1058. break;
  1059. }
  1060. else
  1061. {
  1062. if (mFlash.isHost)
  1063. {
  1064. delay_send_count = 1; //左边鞋发送数据
  1065. }
  1066. PAIR_CHARGERPIN_INFO("EXTI CHARGEING MOID.%d bit_width %d \r\n", ms,bit_width);
  1067. }
  1068. }
  1069. else
  1070. {
  1071. now_bit = 1;
  1072. delay_send_count = 0;
  1073. delay_reply_count = 0;
  1074. delay_check_count = 0;
  1075. nrf_gpio_pin_write(PIN_LED_RUN, 0);
  1076. delay_ledon_count = 0;
  1077. PAIR_CHARGERPIN_PRINT("delay_ledon_count = 0 .%d\r\n", txrxcount);
  1078. PAIR_CHARGERPIN_INFO("INTO CHARGEING MOID.%d\r\n", ms);
  1079. }
  1080. bit_width = 0;
  1081. }
  1082. if (delay_send_count != 0)
  1083. {
  1084. delay_send_count++;
  1085. }
  1086. if (delay_send_count > DELAY_SEND)
  1087. {
  1088. unsigned char macbuff[32];
  1089. delay_send_count = 0;
  1090. PAIR_CHARGERPIN_PRINT("BEGIN send MAC.%d\r\n", ms);
  1091. Get_MACaddr(macbuff);
  1092. macbuff[DATA_LENGTH - 1] = 0;
  1093. for (int i = 0; i < DATA_LENGTH - 1; i++)
  1094. {
  1095. macbuff[DATA_LENGTH - 1] += macbuff[i];
  1096. }
  1097. memcpy(txbuf, macbuff, DATA_LENGTH);
  1098. PAIR_CHARGERPIN_PRINT("host_mac send.%d\r\n", ms);
  1099. one_ms_status = 2;
  1100. txrxcount = 0;
  1101. for (int i = 0; i < DATA_LENGTH; i++)
  1102. {
  1103. PAIR_CHARGERPIN_PRINT("txbuf.%x\r\n", txbuf[i]);
  1104. }
  1105. break;
  1106. }
  1107. if (delay_reply_count != 0)
  1108. {
  1109. delay_reply_count++;
  1110. }
  1111. if (delay_reply_count > DELAY_REPLY)
  1112. {
  1113. unsigned char macbuff[16];
  1114. delay_reply_count = 0;
  1115. PAIR_CHARGERPIN_PRINT("BEGIN send MAC.%d\r\n", ms);
  1116. Get_MACaddr(macbuff);
  1117. memcpy(txbuf, &macbuff[3], DATA_LENGTH);
  1118. txbuf[DATA_LENGTH - 1] = 0;
  1119. for (int i = 0; i < DATA_LENGTH - 1; i++)
  1120. {
  1121. txbuf[DATA_LENGTH - 1] += txbuf[i];
  1122. }
  1123. PAIR_CHARGERPIN_PRINT("host_mac send.%d\r\n", ms);
  1124. one_ms_status = 2;
  1125. txrxcount = 0;
  1126. for (int i = 0; i < DATA_LENGTH; i++)
  1127. {
  1128. PAIR_CHARGERPIN_PRINT("txbuf.%x\r\n", txbuf[i]);
  1129. }
  1130. break;
  1131. }
  1132. if (delay_check_count != 0)
  1133. {
  1134. delay_check_count++;
  1135. }
  1136. if (delay_check_count > DELAY_CHECK)
  1137. {
  1138. delay_check_count = 0;
  1139. one_ms_status = 4;
  1140. txrxcount = 0;
  1141. PAIR_CHARGERPIN_PRINT("CHECK start.%d\r\n", ms);
  1142. break;
  1143. }
  1144. else if (delay_check_count > 0)
  1145. {
  1146. if (now_bit > 0)
  1147. {
  1148. delay_check_count = 0;
  1149. PAIR_CHARGERPIN_PRINT("CANCEL CHECK.%d\r\n", ms);
  1150. }
  1151. }
  1152. break;
  1153. case 2://tx
  1154. {
  1155. lowbitdelay++;
  1156. static char send_interrupt = 0;
  1157. if ((now_bit == 0) && (nrf_gpio_pin_read(PIN_CHARGING)) && (lowbitdelay>LOW_BIT_DELAY))
  1158. {
  1159. if (mFlash.isHost)
  1160. {
  1161. one_ms_status = 1;
  1162. nrf_gpio_pin_clear(PIN_CHARGING);
  1163. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  1164. delay_ledon_count = 0;
  1165. PAIR_CHARGERPIN_PRINT("delay_ledon_count1 = 0;\r\n", txrxcount);
  1166. PAIR_CHARGERPIN_PRINT("EXTI SEND MODE.CAN NOT SET LOW %d\r\n", txrxcount);
  1167. send_interrupt = 0;
  1168. break;
  1169. }
  1170. else
  1171. {
  1172. nrf_gpio_pin_clear(PIN_CHARGING);
  1173. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  1174. send_interrupt = 1;
  1175. }
  1176. }
  1177. if ((txrxcount == 0) && (send_interrupt == 0))
  1178. {
  1179. nrf_gpio_pin_set(PIN_CHARGING);
  1180. PAIR_CHARGERPIN_PRINT("set.%d\r\n", ms);
  1181. now_bit = 1;
  1182. }
  1183. if ((txrxcount == 1.5 * BITWIDTH) && (send_interrupt == 0))
  1184. {
  1185. nrf_gpio_pin_clear(PIN_CHARGING);
  1186. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  1187. now_bit = 0;
  1188. lowbitdelay=0;
  1189. }
  1190. if ((txrxcount >= (int)(2.5 * BITWIDTH) && (txrxcount - (int)(2.5 * BITWIDTH)) % BITWIDTH == 0) && (send_interrupt == 0))
  1191. {
  1192. bytesindes = ((txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH) / 8;
  1193. bitindex = 7 - ((txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH) % 8;
  1194. nrf_gpio_pin_write(PIN_CHARGING, (txbuf[bytesindes] >> bitindex) & 0x01);
  1195. lowbitdelay=0;
  1196. PAIR_CHARGERPIN_PRINT("write.%d %d\r\n", ((txbuf[bytesindes] >> bitindex) & 0x01), ms);
  1197. now_bit = (txbuf[bytesindes] >> bitindex) & 0x01;
  1198. PAIR_CHARGERPIN_PRINT("%d %d %d %d %x\r\n", (txrxcount - (int)(2.5 * BITWIDTH)) / BITWIDTH
  1199. , bytesindes
  1200. , bitindex
  1201. , (txbuf[bytesindes] >> bitindex) & 0x01
  1202. , txbuf[bytesindes]
  1203. );
  1204. }
  1205. if (send_interrupt)
  1206. {
  1207. if (nrf_gpio_pin_read(PIN_CHARGING) == 0)
  1208. {
  1209. send_interrupt = 0;
  1210. delay_ledon_count = DELAY_SEND;
  1211. PAIR_CHARGERPIN_PRINT("delay_ledon_count = DELAY_SEND;.%d\r\n", txrxcount);
  1212. PAIR_CHARGERPIN_PRINT("RX SEND MODE MAC START.%d\r\n", txrxcount);
  1213. txrxcount = 0;
  1214. break;
  1215. }
  1216. }
  1217. if (txrxcount == BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1)
  1218. {
  1219. nrf_gpio_pin_clear(PIN_CHARGING);
  1220. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  1221. if (mFlash.isHost == 0)
  1222. {
  1223. delay_check_count = 1;
  1224. }
  1225. }
  1226. if(delay_check_count!=0)delay_check_count++;
  1227. if (txrxcount >= BITWIDTH * (8 * DATA_LENGTH + 2.5) - 1 + LOW_BIT_DELAY)
  1228. {
  1229. one_ms_status = 1;
  1230. send_interrupt = 0;
  1231. now_bit = 0;
  1232. bit_width = 0;
  1233. PAIR_CHARGERPIN_PRINT("SEDN DONE .%d pin one_ms_status %d\r\n", ms,nrf_gpio_pin_read(PIN_CHARGING));
  1234. break;
  1235. }
  1236. txrxcount++;
  1237. }
  1238. break;
  1239. case 3://rx
  1240. if (now_bit == nrf_gpio_pin_read(PIN_CHARGING))
  1241. {
  1242. bit_width++;
  1243. }
  1244. else
  1245. {
  1246. if (now_bit == 1)
  1247. {
  1248. now_bit = 0;
  1249. if ((bit_width > 1.5 * BITWIDTH - 3) && (bit_width < 1.8 * BITWIDTH + 3))
  1250. {
  1251. txrxcount = 1.5 * BITWIDTH;
  1252. value = 30;
  1253. delay_ledon_count = DELAY_SEND + 1.5 * BITWIDTH;
  1254. PAIR_CHARGERPIN_PRINT("DELAY_SEND + 1.5 * BITWIDTHt .%d\r\n", txrxcount);
  1255. PAIR_CHARGERPIN_PRINT("RX MODE restart .%d\r\n", ms);
  1256. }
  1257. }
  1258. else
  1259. {
  1260. now_bit = 1;
  1261. }
  1262. bit_width = 0;
  1263. }
  1264. if (txrxcount >= (int)(3.5 * BITWIDTH) && (txrxcount - (int)(3.5 * BITWIDTH)) % BITWIDTH == 0)
  1265. {
  1266. bytesindes = ((txrxcount - (int)(3.5 * BITWIDTH)) / BITWIDTH) / 8;
  1267. bitindex = 7 - ((txrxcount - (int)(3.5 * BITWIDTH)) / BITWIDTH) % 8;
  1268. if (value > 0)
  1269. {
  1270. rxbuf[bytesindes] |= 0x01 << bitindex;
  1271. }
  1272. else
  1273. {
  1274. rxbuf[bytesindes] &= ~(0x01 << bitindex);
  1275. }
  1276. PAIR_CHARGERPIN_PRINT("%d %d %d %d %x\r\n", txrxcount
  1277. , bytesindes
  1278. , bitindex
  1279. , value
  1280. , rxbuf[bytesindes]
  1281. );
  1282. value = 0;
  1283. nrf_gpio_pin_toggle(PIN_LED_RUN);
  1284. }
  1285. else
  1286. {
  1287. if (now_bit)
  1288. {
  1289. value++;
  1290. }
  1291. else
  1292. {
  1293. value--;
  1294. }
  1295. }
  1296. if (txrxcount >= BITWIDTH * (8 * DATA_LENGTH + 2.5))
  1297. {
  1298. unsigned char dataerrcheck = 0;
  1299. unsigned char crc = 0;
  1300. one_ms_status = 1;
  1301. PAIR_CHARGERPIN_PRINT("RECEVIC DONE .%d\r\n", txrxcount);
  1302. nrf_gpio_pin_clear(PIN_LED_RUN);
  1303. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  1304. for (int i = 0; i < DATA_LENGTH - 1; i++)
  1305. {
  1306. crc += rxbuf[i];
  1307. }
  1308. for (int i = 0; i < DATA_LENGTH - 1; i++)
  1309. {
  1310. if (rxbuf[i] == 0)
  1311. {
  1312. dataerrcheck++;
  1313. }
  1314. }
  1315. if (dataerrcheck == DATA_LENGTH - 1)
  1316. {
  1317. PAIR_CHARGERPIN_PRINT("CRC ERROR ALL 0x00.%x\r\n", crc);
  1318. delay_ledon_count = 0;
  1319. PAIR_CHARGERPIN_PRINT("delay_ledon_count 2= 0;\r\n", txrxcount);
  1320. break;
  1321. }
  1322. dataerrcheck = 0;
  1323. for (int i = 0; i < DATA_LENGTH - 1; i++)
  1324. {
  1325. if (rxbuf[i] == 0xff)
  1326. {
  1327. dataerrcheck++;
  1328. }
  1329. }
  1330. if (dataerrcheck == DATA_LENGTH - 1)
  1331. {
  1332. PAIR_CHARGERPIN_PRINT("CRC ERROR ALL 0xFF.%x\r\n", crc);
  1333. delay_ledon_count = 0;
  1334. PAIR_CHARGERPIN_PRINT("delay_ledon_count 3= 0;\r\n", txrxcount);
  1335. break;
  1336. }
  1337. if (crc == rxbuf[DATA_LENGTH - 1])
  1338. {
  1339. if (mFlash.isHost)
  1340. {
  1341. delay_check_count = 1; //延时校验计数器
  1342. }
  1343. else
  1344. {
  1345. delay_reply_count = 1; //延时回复计数器
  1346. }
  1347. PAIR_CHARGERPIN_PRINT("---------------CRC SUCCESS .%x----------------\r\n", crc);
  1348. }
  1349. else
  1350. {
  1351. PAIR_CHARGERPIN_PRINT("CRC ERROR .%x\r\n", crc);
  1352. delay_ledon_count = 0;
  1353. PAIR_CHARGERPIN_PRINT("delay_ledon_count 4= 0;\r\n", txrxcount);
  1354. }
  1355. for (int i = 0; i < DATA_LENGTH; i++)
  1356. {
  1357. PAIR_CHARGERPIN_PRINT("rxbuf[%d].%x\r\n",i, rxbuf[i]);
  1358. }
  1359. }
  1360. txrxcount++;
  1361. break;
  1362. case 4:
  1363. if (nrf_gpio_pin_read(PIN_CHARGING))
  1364. {
  1365. value++;
  1366. }
  1367. else
  1368. {
  1369. value--;
  1370. }
  1371. if (mFlash.isHost)
  1372. {
  1373. if (txrxcount == 0)
  1374. {
  1375. value = 0;
  1376. nrf_gpio_pin_set(PIN_CHARGING);
  1377. PAIR_CHARGERPIN_PRINT("set.%d\r\n", ms);
  1378. }
  1379. if (txrxcount == 3 * BITWIDTH)
  1380. {
  1381. nrf_gpio_pin_clear(PIN_CHARGING);
  1382. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", delay_ledon_count);
  1383. }
  1384. if (txrxcount >= 3 * BITWIDTH + LOW_BIT_DELAY)
  1385. {
  1386. value = 0;
  1387. one_ms_status = 1;
  1388. now_bit = 0;
  1389. bit_width = 0;
  1390. }
  1391. }
  1392. else
  1393. {
  1394. if (txrxcount == 0)
  1395. {
  1396. value = 0;
  1397. }
  1398. if (txrxcount > 3 * BITWIDTH)
  1399. {
  1400. if (value < 2 * BITWIDTH)
  1401. {
  1402. PAIR_CHARGERPIN_PRINT("CHECK ERROR .%d\r\n", txrxcount);
  1403. delay_ledon_count = 0;
  1404. PAIR_CHARGERPIN_PRINT("delay_ledon_count 5= 0;\r\n", txrxcount);
  1405. }
  1406. else
  1407. {
  1408. PAIR_CHARGERPIN_PRINT("CHECK DONE .%d\r\n", txrxcount);
  1409. }
  1410. value = 0;
  1411. one_ms_status = 1;
  1412. nrf_gpio_pin_clear(PIN_CHARGING);
  1413. PAIR_CHARGERPIN_PRINT("clear.%d\r\n", ms);
  1414. now_bit = 1;
  1415. bit_width = 0;
  1416. }
  1417. }
  1418. txrxcount++;
  1419. break;
  1420. }
  1421. if (delay_ledon_count != 0)
  1422. {
  1423. delay_ledon_count++;
  1424. }
  1425. if (delay_ledon_count == DELAY_LEDON)
  1426. {
  1427. PAIR_CHARGERPIN_PRINT("LEDCOPEN.%d\r\n", ms);
  1428. memcpy(savebuf,rxbuf,DATA_LENGTH-1);
  1429. writefig=1;
  1430. }
  1431. if ((delay_ledon_count > DELAY_LEDON )
  1432. && (delay_ledon_count - DELAY_LEDON == LEDON_KEEP))
  1433. {
  1434. delay_ledon_count = 0;
  1435. PAIR_CHARGERPIN_PRINT("delay_ledon_count 6= 0;\r\n", txrxcount);
  1436. PAIR_CHARGERPIN_PRINT("LEDCLOSE.%d\r\n", ms);
  1437. }
  1438. }
  1439. void pair_led_pcs(void)
  1440. {
  1441. leddisplay_count++;
  1442. if(leddisplay_count%2==0)
  1443. {
  1444. LED_Start(LED_PAIR,COLOR_BLACK);
  1445. }
  1446. else
  1447. {
  1448. LED_Start(LED_PAIR,COLOR_WHITE);
  1449. }
  1450. if(leddisplay_count>20)
  1451. {
  1452. leddisplay_count=0;
  1453. LED_Stop(LED_PAIR);
  1454. Process_SetHoldOn(pair_led_pcs,0);
  1455. Process_Stop(pair_led_pcs);
  1456. }
  1457. }
  1458. void stop_pair_led(void)
  1459. {
  1460. if(leddisplay_count==0)return;
  1461. LED_Start(LED_PAIR,COLOR_BLACK);
  1462. Process_SetHoldOn(pair_led_pcs,0);
  1463. Process_Stop(pair_led_pcs);
  1464. leddisplay_count=0;
  1465. }
  1466. PAIR_START_REGISTER(stop_pair_led);
  1467. void mtrun(void)
  1468. {
  1469. MT_Run(200);
  1470. }
  1471. PAIR_START_REGISTER(mtrun);
  1472. //写配对相关的flish
  1473. static int pair_writeflish(unsigned char* macAddr_L, unsigned char* macAddr_R)
  1474. {
  1475. uint8_t offest =0;
  1476. if(mFlash.isHost){offest =3;}
  1477. if (memcmp(macAddr_R, mFlash.mClient.macAddr+offest, 3) != 0)
  1478. {
  1479. goto writein;
  1480. }
  1481. if (mFlash.mClient.isConfig != 'C')
  1482. {
  1483. goto writein;
  1484. }
  1485. PAIR_CHARGERPIN_PRINT("mFlash.mClient.isConfig:%X\n", mFlash.mClient.isConfig);
  1486. PAIR_CHARGERPIN_PRINT("mFlash.mClient.macAddr:%02X %02X %02X\n", mFlash.mClient.macAddr[0], mFlash.mClient.macAddr[1], mFlash.mClient.macAddr[2]);
  1487. PAIR_CHARGERPIN_PRINT("mFlash.macHost:%02X %02X %02X\n", mFlash.macHost[0], mFlash.macHost[1], mFlash.macHost[2]);
  1488. return 0;
  1489. writein:
  1490. mFlash.mClient.isConfig = 'C';
  1491. uint8_t i=0;
  1492. for (i = 0; i < 6; i++){mFlash.macHost[i] = macAddr_L[i];}
  1493. for (i = 0; i < 3; i++)
  1494. mFlash.mClient.macAddr[i+offest] = macAddr_R[i]; //从机自身mac地址
  1495. //保存数据到备份区域里面
  1496. for( i=0; i<RecordMacAddrL; i++){
  1497. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  1498. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i];//从机地址
  1499. }
  1500. mBackup.hardVersion = mFlash.mClient.hardVersion;
  1501. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  1502. mBackup.isConfig = mFlash.mClient.isConfig;
  1503. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Pair,"pair save backup fail");
  1504. DEBUG_LOG("mFlash.mClient.isConfig:%X\n", mFlash.mClient.isConfig);
  1505. DEBUG_LOG("mFlash.mClient.macAddr:%02X %02X %02X\n", mFlash.mClient.macAddr[0], mFlash.mClient.macAddr[1], mFlash.mClient.macAddr[2]);
  1506. DEBUG_LOG("mFlash.macHost:%02X %02X %02X \n", mFlash.macHost[0], mFlash.macHost[1], mFlash.macHost[2]);
  1507. if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair,"pair clear step fail");return 0;}
  1508. memset(&mFlash.mStep,0,sizeof(FlashStep_t));
  1509. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS){Except_TxError(EXCEPT_Pair,"Flash_SaveInfomation fail");return 0;}
  1510. return 1;
  1511. }
  1512. static void load_adv_name_from_flish(void)
  1513. {
  1514. char buf[16];
  1515. memset(buf, 0, sizeof(buf));
  1516. advertising_stop();
  1517. 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]);
  1518. DEBUG_LOG("advName(%d):%s\n", strlen(buf), buf);
  1519. slave_set_adv_name(buf, strlen(buf));
  1520. slave_adv_init();
  1521. }
  1522. static void load_scan_name_from_flish(void)
  1523. {
  1524. char buf[16];
  1525. memset(buf, 0, sizeof(buf));
  1526. 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]);
  1527. DEBUG_LOG("scanName1(%d):%s\n", strlen(buf), buf);
  1528. host_set_scan_name(buf, strlen(buf));
  1529. }
  1530. static unsigned char reconnect_pcs_status=0;
  1531. static void reconnect_pcs(void)
  1532. {
  1533. switch(reconnect_pcs_status)
  1534. {
  1535. case 0:
  1536. if(mFlash.isHost)
  1537. {
  1538. reconnect_pcs_status=1;
  1539. if(host_isconnect()==1)host_disconnect();
  1540. }
  1541. else
  1542. {
  1543. reconnect_pcs_status=2;
  1544. if(slave_isconnect()==1)slave_disconnect();
  1545. }
  1546. break;
  1547. case 1:
  1548. if(host_isconnect()==0)
  1549. {
  1550. load_scan_name_from_flish();
  1551. scan_start();
  1552. reconnect_pcs_status=4;
  1553. }
  1554. break;
  1555. case 2:
  1556. if(slave_isconnect()==0)
  1557. {
  1558. load_adv_name_from_flish();
  1559. advertising_start();
  1560. reconnect_pcs_status=4;
  1561. }
  1562. break;
  1563. case 4:
  1564. Process_SetHoldOn(reconnect_pcs,0);
  1565. Process_Stop(reconnect_pcs);
  1566. reconnect_pcs_status=5;
  1567. leddisplay_count =0;
  1568. Process_Start(500,"pair_led_pcs",pair_led_pcs);
  1569. Process_SetHoldOn(pair_led_pcs,1);
  1570. break;
  1571. case 5:
  1572. break;
  1573. }
  1574. }
  1575. static void app_pair_chargerpin_pcs(void)
  1576. {
  1577. int rev=0;
  1578. if(writefig==1)
  1579. {
  1580. unsigned char Lbuff[6];
  1581. unsigned char Rbuff[3];
  1582. Get_MACaddr(Lbuff);
  1583. Rbuff[0]=savebuf[0];Rbuff[1]=savebuf[1];Rbuff[2]=savebuf[2];
  1584. rev=pair_writeflish(Lbuff,Rbuff);
  1585. if(rev)
  1586. {
  1587. Process_Start(10,"reconnect_pcs",reconnect_pcs);
  1588. Process_SetHoldOn(reconnect_pcs,1);
  1589. reconnect_pcs_status=0;
  1590. }
  1591. else
  1592. {
  1593. leddisplay_count =0;
  1594. Process_Start(500,"pair_led_pcs",pair_led_pcs);
  1595. Process_SetHoldOn(pair_led_pcs,1);
  1596. }
  1597. writefig=0;
  1598. }
  1599. }
  1600. void pair_start_pcs(void)
  1601. {
  1602. extern void *pair_start$$Base;
  1603. extern void *pair_start$$Limit;
  1604. typedef void (*main_init_t)(void);
  1605. for (int p = (unsigned int)&pair_start$$Base; p < (unsigned int)&pair_start$$Limit; p += 4)
  1606. {
  1607. (*(main_init_t *)p)();
  1608. }
  1609. }
  1610. void pair_done_pcs(void)
  1611. {
  1612. extern void *pair_done$$Base;
  1613. extern void *pair_done$$Limit;
  1614. typedef void (*main_init_t)(void);
  1615. for (int p = (unsigned int)&pair_done$$Base; p < (unsigned int)&pair_done$$Limit; p += 4)
  1616. {
  1617. (*(main_init_t *)p)();
  1618. }
  1619. }
  1620. typedef enum
  1621. {
  1622. CHARGE=0,
  1623. CONNECT_NO_CHARGE,
  1624. DISCONNECT_CHARGE,
  1625. }pair_line_t;
  1626. #define ADC_CHECKMS 1000
  1627. #define DELAY_PAIR_COUNT 100
  1628. void null_pcs(void)
  1629. {
  1630. }
  1631. short left_adc_value=0;
  1632. void adc_cab(short adc_value)
  1633. {
  1634. left_adc_value=adc_value;
  1635. adc_check();
  1636. PAIR_CHARGERPIN_PRINT("ADC_Read_CallBack_Once call -%d\n",adc_value);
  1637. }
  1638. unsigned int pulluppcs_timer=0;
  1639. void tim_read_cb(void *t)
  1640. {
  1641. if(TIME_GetTicks()-pulluppcs_timer>=3)
  1642. {
  1643. PAIR_CHARGERPIN_PRINT("tim_read_cb 3ms \n",mFlash.isHost);
  1644. if(ADC_OP_SUCCESS!=ADC_Read_CallBack_Once(PIN_CHARGING_CHANNEL,adc_cab))
  1645. {
  1646. adc_check();
  1647. PAIR_CHARGERPIN_PRINT("adc_check init \n",mFlash.isHost);
  1648. }
  1649. Process_Stop(null_pcs);
  1650. TIME_UnRegist(tim_read_cb);
  1651. }
  1652. }
  1653. static pair_line_t pair_line = DISCONNECT_CHARGE;
  1654. //#include "queue.h"
  1655. //extern queue_t Delegate;
  1656. static short Left_adc=0;
  1657. char pingup=0;
  1658. void Delegate_up_pin(void *t)
  1659. {
  1660. unsigned int count=TIME_GetTicks()-pulluppcs_timer;
  1661. if(count<3)
  1662. {
  1663. return;
  1664. }
  1665. if(count==3)
  1666. {
  1667. uint32_t errcode = ADC_ReadChargePin(PIN_CHARGING_CHANNEL);
  1668. }
  1669. else if((count>3)&&(count<300))
  1670. {
  1671. unsigned char state = bsp_Get_ChargePinADC(&Left_adc);
  1672. if(state){
  1673. TIME_UnRegist(Delegate_up_pin);
  1674. Process_Stop(null_pcs);
  1675. PAIR_CHARGERPIN_PRINT("Delegate_up_pin %d\n",mFlash.isHost);
  1676. adc_check();
  1677. }
  1678. }
  1679. else
  1680. {
  1681. PAIR_CHARGERPIN_PRINT("------------------------------adc overtime error -------------------\n",mFlash.isHost);
  1682. }
  1683. }
  1684. extern short ChargeADC_value;
  1685. void adc_check(void)
  1686. {
  1687. static unsigned char status=0;
  1688. short ADC_value=0;
  1689. static unsigned int delay_pair_count=0;
  1690. static unsigned int overtime=0;
  1691. static unsigned char ishost;
  1692. switch(status)
  1693. {
  1694. case 0:
  1695. ishost=mFlash.isHost;
  1696. ADC_Disable();
  1697. ADC_SetPinChannel(PIN_CHARGING,PIN_CHARGING_CHANNEL,NRF_GPIO_PIN_NOPULL);
  1698. ADC_Initialize();
  1699. Process_UpdatePeroid(adc_check,ADC_CHECKMS);
  1700. Process_SetHoldOn(adc_check,0);
  1701. nrf_gpio_pin_write(PIN_LED_RUN, 0);
  1702. pair_done_pcs();
  1703. status=1;
  1704. break;
  1705. case 1:
  1706. if(ishost!=mFlash.isHost)
  1707. {
  1708. status=0;
  1709. PAIR_CHARGERPIN_PRINT("mFlash.isHost change %d\n",mFlash.isHost);
  1710. }
  1711. if(mFlash.isHost)
  1712. {
  1713. if(pingup==0)
  1714. {
  1715. pingup=1;
  1716. nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_PULLUP);
  1717. pulluppcs_timer = TIME_GetTicks();
  1718. Process_Start(0,"null_pcs",null_pcs);
  1719. Process_SetHoldOn(null_pcs,1);
  1720. TIME_Regist(Delegate_up_pin);
  1721. return;
  1722. }
  1723. else
  1724. {
  1725. ADC_value=Left_adc;
  1726. nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_NOPULL);
  1727. pingup=0;
  1728. }
  1729. }
  1730. if(mFlash.isHost==0){
  1731. if(ADC_OP_SUCCESS!=ADC_Read(PIN_CHARGING_CHANNEL,&ADC_value))
  1732. {
  1733. PAIR_CHARGERPIN_PRINT("ADC_Read ERROR %d\n",mFlash.isHost);
  1734. }
  1735. }
  1736. PAIR_CHARGERPIN_PRINT( "ADC ( %d ):isHost %d pair_line %d\n", ADC_value,mFlash.isHost,pair_line);
  1737. ChargeADC_value = ADC_value;
  1738. if((ADC_value<CHARGE_ADC)&&(pair_line == CHARGE)&&(mFlash.isHost))
  1739. {
  1740. // Process_Stop(charge_uppin_pcs);
  1741. PAIR_CHARGERPIN_PRINT("--------------------------------------------------------EXIT CHARGE_ADC \n");
  1742. }
  1743. if(ADC_value>=CHARGE_ADC)
  1744. {
  1745. PAIR_CHARGERPIN_PRINT(" CHARGE_ADC \n");
  1746. if((pair_line != CHARGE)&&(mFlash.isHost))
  1747. {
  1748. PAIR_CHARGERPIN_PRINT("--------------------------------------------------------------------INTO CHARGE_ADC \n");
  1749. // Process_Start(ADC_CHECKMS,"charge_uppin_pcs",charge_uppin_pcs);
  1750. }
  1751. pair_line=CHARGE;
  1752. }
  1753. else if((ADC_value>=CONN_MIN_ADC)&&(ADC_value<=CONN_MAX_ADC)&&(mFlash.isHost==0))
  1754. {
  1755. PAIR_CHARGERPIN_PRINT(" CONN_ADC %d \n",pair_line);
  1756. connect:
  1757. if(pair_line!=CONNECT_NO_CHARGE)
  1758. {
  1759. PAIR_CHARGERPIN_PRINT(" Process_SetHoldOn(adc_check,1); ms %d\n",TIME_GetTicks());
  1760. if(mFlash.isHost)Process_Start(ADC_CHECKMS,"adc_check",adc_check);
  1761. Process_UpdatePeroid(adc_check,10);
  1762. Process_SetHoldOn(adc_check,1);
  1763. overtime=TIME_GetTicks();
  1764. pair_start_pcs();
  1765. if(mFlash.isHost)
  1766. {
  1767. status=2;
  1768. nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_PULLUP);
  1769. delay_pair_count=TIME_GetTicks();
  1770. PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG HIGHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH; %d \n",TIME_GetTicks());
  1771. }
  1772. else
  1773. {
  1774. status=5;
  1775. TIME_Regist(chargerpin_one_ms_pcs);
  1776. one_ms_status = 0;
  1777. }
  1778. }
  1779. pair_line=CONNECT_NO_CHARGE;
  1780. }
  1781. else
  1782. {
  1783. if(mFlash.isHost)
  1784. {
  1785. if((ADC_value<=DISCON_L_MAX_ADC)&&(ADC_value>=DISCON_L_MIN_ADC))
  1786. {
  1787. PAIR_CHARGERPIN_PRINT("L DISCON_L_ADC \n");
  1788. pair_line=DISCONNECT_CHARGE;
  1789. }
  1790. else if((ADC_value<=CON_L_MAX_ADC)&&(ADC_value>=CON_L_MIN_ADC))
  1791. {
  1792. PAIR_CHARGERPIN_PRINT("L CON_L_MAX_ADC \n");
  1793. goto connect;
  1794. }
  1795. }
  1796. else
  1797. {
  1798. if(ADC_value<=DISCON_R_MAX_ADC)
  1799. {
  1800. PAIR_CHARGERPIN_PRINT("R DISCON_R_MAX_ADC \n");
  1801. pair_line=DISCONNECT_CHARGE;
  1802. }
  1803. }
  1804. }
  1805. break;
  1806. case 2://
  1807. if(TIME_GetTicks()- delay_pair_count > ADC_CHECKMS)
  1808. {
  1809. TIME_Regist(chargerpin_one_ms_pcs);
  1810. one_ms_status = 0;
  1811. status=3;
  1812. PAIR_CHARGERPIN_PRINT(" TIME_Regist(chargerpin_one_ms_pcs); %d \n",TIME_GetTicks());
  1813. }
  1814. break;
  1815. case 3:
  1816. if(TIME_GetTicks()- delay_pair_count > DELAY_PAIR_COUNT)
  1817. {
  1818. nrf_gpio_pin_set(PIN_CHARGING);
  1819. PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG HIG; %d \n",TIME_GetTicks());
  1820. status=4;
  1821. }
  1822. break;
  1823. case 4:
  1824. if(TIME_GetTicks()- delay_pair_count>(DELAY_PAIR_COUNT+100))
  1825. {
  1826. nrf_gpio_pin_clear(PIN_CHARGING);
  1827. PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG LOW; %d \n",TIME_GetTicks());
  1828. status=5;
  1829. }
  1830. break;
  1831. case 5:
  1832. if(writefig==1)
  1833. {
  1834. app_pair_chargerpin_pcs();
  1835. TIME_UnRegist(chargerpin_one_ms_pcs);
  1836. status=0;
  1837. MT_Run(500);
  1838. PAIR_CHARGERPIN_PRINT(" ---------------------------PAIR SUCESS------------- \n");
  1839. }
  1840. if(TIME_GetTicks()-overtime > DELAY_LEDON+ADC_CHECKMS + DELAY_PAIR_COUNT + 100 + 300)
  1841. {
  1842. LED_Stop(LED_PAIR);
  1843. TIME_UnRegist(chargerpin_one_ms_pcs);
  1844. status=0;
  1845. PAIR_CHARGERPIN_PRINT(" ---------------------------PAIR OVER TIME %d------------- \n",TIME_GetTicks());
  1846. }
  1847. break;
  1848. }
  1849. }
  1850. void write_on(void)
  1851. {
  1852. LED_Start(LED_PAIR,COLOR_WHITE);
  1853. }
  1854. PAIR_START_REGISTER(write_on);
  1855. void app_pair_chargerpin_Init(void)
  1856. {
  1857. Process_Start(ADC_CHECKMS,"adc_check",adc_check);
  1858. }