app_pair_chargerpin.c 9.2 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_BLUE);
  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_BLUE);
  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. host_set_scan_name(buf, strlen(buf));
  160. advertising_stop();
  161. memset(buf,0,sizeof(buf));
  162. sprintf(buf,"SH_%02X%02X",mFlash.macHost[1], mFlash.macHost[0]);
  163. slave_set_adv_name(buf,strlen(buf));
  164. slave_adv_init();
  165. }
  166. static unsigned char reconnect_pcs_status = 0;
  167. static void reconnect_pcs(void)
  168. {
  169. switch (reconnect_pcs_status)
  170. {
  171. case 0:
  172. if (mFlash.isHost)
  173. {
  174. reconnect_pcs_status = 1;
  175. if (host_isconnect() == 1)
  176. {
  177. host_disconnect();
  178. }
  179. }
  180. else
  181. {
  182. reconnect_pcs_status = 2;
  183. if (slave_isconnect() == 1)
  184. {
  185. slave_disconnect();
  186. }
  187. }
  188. break;
  189. case 1:
  190. if (host_isconnect() == 0)
  191. {
  192. load_scan_name_from_flish();
  193. scan_start();
  194. advertising_start();
  195. reconnect_pcs_status = 4;
  196. }
  197. break;
  198. case 2:
  199. if (slave_isconnect() == 0)
  200. {
  201. load_adv_name_from_flish();
  202. advertising_start();
  203. reconnect_pcs_status = 4;
  204. }
  205. break;
  206. case 4:
  207. Process_SetHoldOn(reconnect_pcs, 0);
  208. Process_Stop(reconnect_pcs);
  209. reconnect_pcs_status = 5;
  210. start_pair_led();
  211. break;
  212. case 5:
  213. break;
  214. }
  215. }
  216. //===========================================================================================================
  217. extern void app_pair_chargerpin_pcs(void);
  218. void delsiflash(void)
  219. {
  220. app_pair_chargerpin_pcs();
  221. Process_Stop(delsiflash);
  222. }
  223. __IO static char writefig=0;
  224. unsigned char *savebuf;
  225. void sucess_ev_cb(unsigned char *buf,int len)
  226. {
  227. writefig=1;
  228. savebuf=buf;
  229. PAIR_CHARGERPIN_PRINT(" ======================PAIR SUCCESS============================= \n");
  230. Process_Start(10, "delsiflash", delsiflash);
  231. }
  232. void app_pair_chargerpin_pcs(void)
  233. {
  234. int rev = 0;
  235. if (writefig == 1)
  236. {
  237. unsigned char Lbuff[6];
  238. unsigned char Rbuff[3];
  239. Get_MACaddr(Lbuff);
  240. Rbuff[0] = savebuf[0];
  241. Rbuff[1] = savebuf[1];
  242. Rbuff[2] = savebuf[2];
  243. rev = pair_writeflish(Lbuff, Rbuff);
  244. if (rev)
  245. {
  246. Process_Start(10, "reconnect_pcs", reconnect_pcs);
  247. Process_SetHoldOn(reconnect_pcs, 1);
  248. reconnect_pcs_status = 0;
  249. }
  250. else
  251. {
  252. start_pair_led();
  253. }
  254. writefig = 0;
  255. }
  256. }
  257. //======================================================================
  258. void pair_start_pcs(void)
  259. {
  260. extern void *pair_start$$Base;
  261. extern void *pair_start$$Limit;
  262. typedef void (*main_init_t)(void);
  263. for (int p = (unsigned int)&pair_start$$Base; p < (unsigned int)&pair_start$$Limit; p += 4)
  264. {
  265. (*(main_init_t *)p)();
  266. }
  267. }
  268. void pair_done_pcs(void)
  269. {
  270. extern void *pair_done$$Base;
  271. extern void *pair_done$$Limit;
  272. typedef void (*main_init_t)(void);
  273. for (int p = (unsigned int)&pair_done$$Base; p < (unsigned int)&pair_done$$Limit; p += 4)
  274. {
  275. (*(main_init_t *)p)();
  276. }
  277. }
  278. //=================================================================================================
  279. void send_start(void)
  280. {
  281. #define DELAY_PAIR_COUNT 100
  282. static unsigned int delay_pair_count=0;
  283. static char status=0;
  284. switch(status)
  285. {
  286. case 0:
  287. delay_pair_count=TIME_GetTicks();
  288. Process_Start(1, "send_start", send_start);
  289. Process_SetHoldOn(send_start, 1);
  290. status=2;
  291. break;
  292. case 2:
  293. if(nrf_gpio_pin_read(PIN_CHARGING)==0)
  294. {
  295. PAIR_CHARGERPIN_PRINT(" ==============================nrf_gpio_pin_read(PIN_CHARGING)==0 \n");
  296. status=3;
  297. }
  298. break;
  299. case 3:
  300. if (TIME_GetTicks() - delay_pair_count > DELAY_PAIR_COUNT)
  301. {
  302. delay_pair_count = TIME_GetTicks();
  303. nrf_gpio_pin_set(PIN_CHARGING);
  304. PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG HIG; %d \n", TIME_GetTicks());
  305. status = 4;
  306. }
  307. break;
  308. case 4:
  309. if (TIME_GetTicks() - delay_pair_count > (DELAY_PAIR_COUNT + 100))
  310. {
  311. nrf_gpio_pin_clear(PIN_CHARGING);
  312. PAIR_CHARGERPIN_PRINT(" SEND PAIR TAG LOW; %d \n", TIME_GetTicks());
  313. status = 0;
  314. Process_Stop(send_start);
  315. }
  316. break;
  317. }
  318. }
  319. void into_event(void)
  320. {
  321. chargerpin_one_ms_init(sucess_ev_cb);
  322. pair_start_pcs();
  323. if(mFlash.isHost==1)
  324. {
  325. send_start();
  326. }
  327. }
  328. void over_event(void)
  329. {
  330. chargerpin_one_ms_uninit();
  331. pair_done_pcs();
  332. }
  333. void app_pair_chargerpin_Init(void)
  334. {
  335. check_callback_regist(selfcheck_trigger_callback);
  336. // mFlash.isHost=0;
  337. if(mFlash.isHost!=HOST_SHOSE)
  338. app_pair_client_init(into_event,over_event);
  339. if(mFlash.isHost==HOST_SHOSE)
  340. app_pair_host_init(into_event,over_event);
  341. }