app_pair_chargerpin.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #include "usr_config.h"
  2. #include "bsp_time.h"
  3. #include "selfcheck.h"
  4. #include "system.h"
  5. #include "nrf_gpio.h"
  6. #include "app_charge.h"
  7. #include "app_flash.h"
  8. #include "ble_comm.h"
  9. #include "bsp_adc.h"
  10. #include "app_pair_chargerpin.h"
  11. #include "app_chargerpin_conn_detect.h"
  12. #include "hal_led.h"
  13. #include "hal_charge.h"
  14. #include "hal_mt.h"
  15. #include "app_one_wire.h"
  16. //==========================================================================
  17. char open=0;
  18. static uint8_t leddisplay_count = 0;
  19. void pair_led_pcs(void)
  20. {
  21. leddisplay_count++;
  22. if (leddisplay_count % 2 == 0)
  23. {
  24. LED_Start(LED_PAIR, COLOR_BLACK);
  25. }
  26. else
  27. {
  28. LED_Start(LED_PAIR, COLOR_WHITE);
  29. }
  30. if (leddisplay_count > 20)
  31. {
  32. leddisplay_count = 0;
  33. LED_Stop(LED_PAIR);
  34. Process_SetHoldOn(pair_led_pcs, 0);
  35. Process_Stop(pair_led_pcs);
  36. }
  37. }
  38. extern void close_white(void);
  39. void start_pair_led(void)
  40. {
  41. close_white();
  42. leddisplay_count = 0;
  43. Process_Start(500, "pair_led_pcs", pair_led_pcs);
  44. Process_SetHoldOn(pair_led_pcs, 1);
  45. }
  46. void stop_pair_led(void)
  47. {
  48. if (leddisplay_count == 0)
  49. {
  50. return;
  51. }
  52. Process_Stop(pair_led_pcs);
  53. leddisplay_count = 0;
  54. LED_Stop(LED_PAIR);
  55. }
  56. void open_white(void)
  57. {
  58. LED_Start(LED_PAIR, COLOR_LOWWHITE);
  59. PAIR_CHARGERPIN_PRINT(" ======================open_white open_white============================= \n");
  60. open=1;
  61. }
  62. void close_white(void)
  63. {
  64. open=0;
  65. LED_Stop(LED_PAIR);
  66. }
  67. char white_isopen(void)
  68. {
  69. return open;
  70. }
  71. void r_led_start(void)
  72. {
  73. stop_pair_led();
  74. open_white();
  75. }
  76. PAIR_START_REGISTER(r_led_start);
  77. void r_led_done(void)
  78. {
  79. if(leddisplay_count!=0)open=0;
  80. else close_white();
  81. }
  82. PAIR_DONE_REGISTER(r_led_done);
  83. //===========================================================================================
  84. //写配对相关的flish
  85. static int pair_writeflish(unsigned char *macAddr_L, unsigned char *macAddr_R)
  86. {
  87. uint8_t offest = 0;
  88. if (mFlash.isHost)
  89. {
  90. offest = 3;
  91. }
  92. if (memcmp(macAddr_R, mFlash.mClient.macAddr + offest, 3) != 0)
  93. {
  94. goto writein;
  95. }
  96. if (mFlash.mClient.isConfig != 'C')
  97. {
  98. goto writein;
  99. }
  100. PAIR_CHARGERPIN_PRINT("mFlash.mClient.isConfig:%X\n", mFlash.mClient.isConfig);
  101. PAIR_CHARGERPIN_PRINT("mFlash.mClient.macAddr:%02X %02X %02X\n", mFlash.mClient.macAddr[0], mFlash.mClient.macAddr[1], mFlash.mClient.macAddr[2]);
  102. PAIR_CHARGERPIN_PRINT("mFlash.macHost:%02X %02X %02X\n", mFlash.macHost[0], mFlash.macHost[1], mFlash.macHost[2]);
  103. return 0;
  104. writein:
  105. mFlash.mClient.isConfig = 'C';
  106. uint8_t i = 0;
  107. for (i = 0; i < 6; i++)
  108. {
  109. mFlash.macHost[i] = macAddr_L[i];
  110. }
  111. for (i = 0; i < 3; i++)
  112. {
  113. mFlash.mClient.macAddr[i + offest] = macAddr_R[i]; //从机自身mac地址
  114. }
  115. //保存数据到备份区域里面
  116. for (i = 0; i < RecordMacAddrL; i++)
  117. {
  118. mBackup.macAddr_L[i] = mFlash.macHost[i]; //主机地址
  119. mBackup.macAddr_R[i] = mFlash.mClient.macAddr[i]; //从机地址
  120. }
  121. mBackup.hardVersion = mFlash.mClient.hardVersion;
  122. mBackup.sotfVersion = mFlash.mClient.sotfVersion;
  123. mBackup.isConfig = mFlash.mClient.isConfig;
  124. if (Flash_SaveBackup() != ZONE_OP_SUCCESS)
  125. {
  126. Except_TxError(EXCEPT_Pair, "pair save backup fail");
  127. }
  128. DEBUG_LOG("mFlash.mClient.isConfig:%X\n", mFlash.mClient.isConfig);
  129. DEBUG_LOG("mFlash.mClient.macAddr:%02X %02X %02X\n", mFlash.mClient.macAddr[0], mFlash.mClient.macAddr[1], mFlash.mClient.macAddr[2]);
  130. DEBUG_LOG("mFlash.macHost:%02X %02X %02X \n", mFlash.macHost[0], mFlash.macHost[1], mFlash.macHost[2]);
  131. if (Flash_DeleteAllStep() != ZONE_OP_SUCCESS)
  132. {
  133. Except_TxError(EXCEPT_Pair, "pair clear step fail");
  134. return 0;
  135. }
  136. memset(&mFlash.mStep, 0, sizeof(FlashStep_t));
  137. if (Flash_SaveInfomation() != ZONE_OP_SUCCESS)
  138. {
  139. Except_TxError(EXCEPT_Pair, "Flash_SaveInfomation fail");
  140. return 0;
  141. }
  142. return 1;
  143. }
  144. static void load_adv_name_from_flish(void)
  145. {
  146. char buf[16];
  147. memset(buf, 0, sizeof(buf));
  148. advertising_stop();
  149. 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]);
  150. DEBUG_LOG("advName(%d):%s\n", strlen(buf), buf);
  151. slave_set_adv_name(buf, strlen(buf));
  152. slave_adv_init();
  153. }
  154. static void load_scan_name_from_flish(void)
  155. {
  156. char buf[16];
  157. memset(buf, 0, sizeof(buf));
  158. 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]);
  159. DEBUG_LOG("scanName1(%d):%s\n", strlen(buf), buf);
  160. host_set_scan_name(buf, strlen(buf));
  161. advertising_stop();
  162. memset(buf,0,sizeof(buf));
  163. sprintf(buf,"SH_%02X%02X",mFlash.macHost[1], mFlash.macHost[0]);
  164. slave_set_adv_name(buf,strlen(buf));
  165. slave_adv_init();
  166. }
  167. static unsigned char reconnect_pcs_status = 0;
  168. static void reconnect_pcs(void)
  169. {
  170. switch (reconnect_pcs_status)
  171. {
  172. case 0:
  173. if (mFlash.isHost)
  174. {
  175. reconnect_pcs_status = 1;
  176. if (host_isconnect() == 1)
  177. {
  178. host_disconnect();
  179. }
  180. }
  181. else
  182. {
  183. reconnect_pcs_status = 2;
  184. if (slave_isconnect() == 1)
  185. {
  186. slave_disconnect();
  187. }
  188. }
  189. break;
  190. case 1:
  191. if (host_isconnect() == 0)
  192. {
  193. load_scan_name_from_flish();
  194. scan_start();
  195. advertising_start();
  196. reconnect_pcs_status = 4;
  197. }
  198. break;
  199. case 2:
  200. if (slave_isconnect() == 0)
  201. {
  202. load_adv_name_from_flish();
  203. advertising_start();
  204. reconnect_pcs_status = 4;
  205. }
  206. break;
  207. case 4:
  208. Process_SetHoldOn(reconnect_pcs, 0);
  209. Process_Stop(reconnect_pcs);
  210. reconnect_pcs_status = 5;
  211. start_pair_led();
  212. break;
  213. case 5:
  214. break;
  215. }
  216. }
  217. //===========================================================================================================
  218. extern void app_pair_chargerpin_pcs(void);
  219. void delsiflash(void)
  220. {
  221. app_pair_chargerpin_pcs();
  222. Process_Stop(delsiflash);
  223. }
  224. __IO static char writefig=0;
  225. unsigned char *savebuf;
  226. void sucess_ev_cb(unsigned char *buf,int len)
  227. {
  228. writefig=1;
  229. savebuf=buf;
  230. PAIR_CHARGERPIN_PRINT(" ======================PAIR SUCCESS============================= \n");
  231. Process_Start(10, "delsiflash", delsiflash);
  232. }
  233. void app_pair_chargerpin_pcs(void)
  234. {
  235. int rev = 0;
  236. if (writefig == 1)
  237. {
  238. unsigned char Lbuff[6];
  239. unsigned char Rbuff[3];
  240. Get_MACaddr(Lbuff);
  241. Rbuff[0] = savebuf[0];
  242. Rbuff[1] = savebuf[1];
  243. Rbuff[2] = savebuf[2];
  244. rev = pair_writeflish(Lbuff, Rbuff);
  245. if (rev)
  246. {
  247. Process_Start(10, "reconnect_pcs", reconnect_pcs);
  248. Process_SetHoldOn(reconnect_pcs, 1);
  249. reconnect_pcs_status = 0;
  250. }
  251. else
  252. {
  253. start_pair_led();
  254. }
  255. writefig = 0;
  256. }
  257. }
  258. //======================================================================
  259. void pair_start_pcs(void)
  260. {
  261. extern void *pair_start$$Base;
  262. extern void *pair_start$$Limit;
  263. typedef void (*main_init_t)(void);
  264. for (int p = (unsigned int)&pair_start$$Base; p < (unsigned int)&pair_start$$Limit; p += 4)
  265. {
  266. (*(main_init_t *)p)();
  267. }
  268. }
  269. void pair_done_pcs(void)
  270. {
  271. extern void *pair_done$$Base;
  272. extern void *pair_done$$Limit;
  273. typedef void (*main_init_t)(void);
  274. for (int p = (unsigned int)&pair_done$$Base; p < (unsigned int)&pair_done$$Limit; p += 4)
  275. {
  276. (*(main_init_t *)p)();
  277. }
  278. }
  279. //=================================================================================================
  280. void send_start(void)
  281. {
  282. #define DELAY_PAIR_COUNT 100
  283. static unsigned int delay_pair_count=0;
  284. static char status=0;
  285. switch(status)
  286. {
  287. case 0:
  288. delay_pair_count=TIME_GetTicks();
  289. Process_Start(1, "send_start", send_start);
  290. Process_SetHoldOn(send_start, 1);
  291. status=2;
  292. break;
  293. case 2:
  294. if(nrf_gpio_pin_read(PIN_CHARGING)==0)
  295. {
  296. PAIR_CHARGERPIN_PRINT(" ==============================nrf_gpio_pin_read(PIN_CHARGING)==0 \n");
  297. status=3;
  298. }
  299. break;
  300. case 3:
  301. if (TIME_GetTicks() - delay_pair_count > DELAY_PAIR_COUNT)
  302. {
  303. delay_pair_count = TIME_GetTicks();
  304. nrf_gpio_pin_set(PIN_CHARGING);
  305. PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG HIG; %d \n", TIME_GetTicks());
  306. status = 4;
  307. }
  308. break;
  309. case 4:
  310. if (TIME_GetTicks() - delay_pair_count > (DELAY_PAIR_COUNT + 100))
  311. {
  312. nrf_gpio_pin_clear(PIN_CHARGING);
  313. PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG LOW; %d \n", TIME_GetTicks());
  314. status = 0;
  315. Process_Stop(send_start);
  316. }
  317. break;
  318. }
  319. }
  320. void into_event(void)
  321. {
  322. chargerpin_one_ms_init(sucess_ev_cb);
  323. pair_start_pcs();
  324. if(mFlash.isHost==1)
  325. {
  326. send_start();
  327. }
  328. }
  329. void over_event(void)
  330. {
  331. chargerpin_one_ms_uninit();
  332. pair_done_pcs();
  333. }
  334. void app_pair_chargerpin_Init(void)
  335. {
  336. check_callback_regist(selfcheck_trigger_callback);
  337. // mFlash.isHost=0;
  338. if(mFlash.isHost!=HOST_SHOSE)
  339. app_pair_client_init(into_event,over_event);
  340. if(mFlash.isHost==HOST_SHOSE)
  341. app_pair_host_init(into_event,over_event);
  342. }