app_pair_chargerpin.c 9.1 KB

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