app_pair_chargerpin.c 9.1 KB

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