app_chargerpin_conn_detect.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. #include "app_chargerpin_conn_detect.h"
  2. #include "system.h"
  3. #include "bsp_time.h"
  4. #include "bsp_adc.h"
  5. #include "ble_comm.h"
  6. #include "hal_led.h"
  7. #include "hal_mt.h"
  8. #include "app_flash.h"
  9. #include "app_pair_chargerpin.h"
  10. #include "app_one_wire.h"
  11. //=================debug=============
  12. #include "ringframe.h"
  13. extern char logbuftemp[50];
  14. extern int logbuftemp_len ;
  15. extern ringframe_t battlog;
  16. #define log(...) {logbuftemp_len = sprintf(logbuftemp,__VA_ARGS__); while(ringframe_in(&battlog,logbuftemp,logbuftemp_len)!=0){ringframe_throw(&battlog);}}
  17. //void cb_BLE_Client_PRADC(void* handle)
  18. //{
  19. //// BLE_Client_Rx_t* target = handle;
  20. // DEBUG_LOG("cb_BLE_Client_ERR:%d,%d\n", 1, 1);
  21. // BLE_Client_Rx_Regist(BLE_ERR, cb_BLE_Client_ERR);
  22. //}
  23. //=================debug=============
  24. //====================================================================================
  25. #define ADC_CHECKMS 1000
  26. #define PAIROVERTIME 3000
  27. #define DELAY_PAIR_COUNT 100
  28. struct{
  29. char statu;
  30. unsigned int pulluppcs_timer;
  31. short adc_value;
  32. pair_line_t pair_line;
  33. unsigned int delay_pair_count;
  34. Event into_event;
  35. Event over_event;
  36. }host_ram={0,0,0,DISCONNECT_CHARGE,0,NULL,NULL};
  37. extern void app_pair_host_process(void);
  38. void app_pair_host_init(Event into,Event over)
  39. {
  40. host_ram.adc_value=0;
  41. host_ram.delay_pair_count=0;
  42. host_ram.pulluppcs_timer=0;
  43. host_ram.statu=0;
  44. host_ram.pair_line=DISCONNECT_CHARGE;
  45. host_ram.into_event=into;
  46. host_ram.over_event=over;
  47. Process_Start(ADC_CHECKMS, "app_pair_host_process", app_pair_host_process);
  48. }
  49. void app_pair_host_uninit(void)
  50. {
  51. Process_Stop(app_pair_host_process);
  52. }
  53. #define DISCON_HOST_MAX_ADC 1200
  54. #define DISCON_HOST_MIN_ADC 600
  55. #define CON_HOST_MAX_ADC DISCON_HOST_MIN_ADC
  56. #define CON_HOST_MIN_ADC 300
  57. static void null_pcs(void)
  58. {
  59. }
  60. #define UPPULL_MS 3
  61. static void ReadAdc_timcallback(void *t)
  62. {
  63. if(TIME_GetTicks()-host_ram.pulluppcs_timer<UPPULL_MS)return;
  64. if(TIME_GetTicks()-host_ram.pulluppcs_timer==UPPULL_MS)
  65. {
  66. uint32_t errcode = ADC_ReadChargePin(PIN_CHARGING_CHANNEL);
  67. if (ADC_OP_SUCCESS != errcode)
  68. {
  69. char buff[20];
  70. memset(buff,0,20);
  71. sprintf(buff,"ADC ERROR %d",errcode);
  72. Except_TxError(EXCEPT_Pair,buff);
  73. PAIR_CHARGERPIN_PRINT("ADC_ReadChargePin ======%d================================================ERROR %d\n",TIME_GetTicks(), errcode);
  74. }
  75. }
  76. if(TIME_GetTicks()-host_ram.pulluppcs_timer>UPPULL_MS)
  77. {
  78. uint8_t state = bsp_Get_ChargePinADC(&host_ram.adc_value);
  79. if(state==1)//adc读取成功
  80. {
  81. host_ram.statu=2;
  82. app_pair_host_process();
  83. }
  84. else
  85. {
  86. TIME_UnRegist(ReadAdc_timcallback);
  87. nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL);
  88. Process_Stop(null_pcs);
  89. Except_TxError(EXCEPT_Pair,"adc overtimer");
  90. PAIR_CHARGERPIN_PRINT("bsp_Get_ChargePinADC %d===========%d===========================================ERROR %d\n",host_ram.adc_value,TIME_GetTicks(), state);
  91. }
  92. }
  93. }
  94. //-------------------------------------------
  95. unsigned char shefcheck_buff[5];
  96. char shefcheck_event_fag=0;
  97. check_callbackt check_callbackt_ram;
  98. void shefcheck_event(unsigned char *buff, int length)
  99. {
  100. shefcheck_event_fag=1;
  101. }
  102. void check_callback_regist(check_callbackt evt)
  103. {
  104. check_callbackt_ram=evt;
  105. }
  106. //-------------------------------------------
  107. static void app_pair_host_process(void)
  108. {
  109. for(int i=1;i>0;i--)
  110. {
  111. switch(host_ram.statu)
  112. {
  113. case 0:
  114. ADC_Disable();
  115. ADC_SetPinChannel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_NOPULL);
  116. ADC_Initialize();
  117. host_ram.statu=1;
  118. break;
  119. case 1:
  120. if(mFlash.isHost!=HOST_SHOSE)
  121. {
  122. app_pair_host_uninit();
  123. app_pair_chargerpin_Init();
  124. PAIR_CHARGERPIN_PRINT("mFlash.isHost change %d %d\n",TIME_GetTicks(),mFlash.isHost );
  125. }
  126. //==============================s
  127. if (ADC_OP_SUCCESS != ADC_Read(PIN_CHARGING_CHANNEL, &host_ram.adc_value))
  128. {
  129. PAIR_CHARGERPIN_PRINT("ADC_Read ERROR %d\n", host_ram.adc_value);
  130. }
  131. PAIR_CHARGERPIN_PRINT("ADC f( %d ):isHost %d Ticks %d\n", host_ram.adc_value, mFlash.isHost,TIME_GetTicks());
  132. if(host_ram.adc_value>2800)
  133. {
  134. if(check_callbackt_ram)
  135. {
  136. host_ram.statu=11;
  137. host_ram.delay_pair_count=TIME_GetTicks();
  138. Process_UpdatePeroid(app_pair_host_process,100);
  139. Process_SetHoldOn(app_pair_host_process,1);
  140. one_byte_receive_init(shefcheck_buff,2,shefcheck_event);
  141. shefcheck_event_fag=0;
  142. i=2;
  143. break;
  144. }
  145. else
  146. {
  147. PAIR_CHARGERPIN_PRINT("check_callbackt_ram NULL\n");
  148. }
  149. }
  150. //==============================e
  151. nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_PULLUP);
  152. PAIR_CHARGERPIN_PRINT("nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_PULLUP); %d \n",TIME_GetTicks() );
  153. host_ram.pulluppcs_timer = TIME_GetTicks();
  154. Process_Start(0, "null_pcs", null_pcs);
  155. Process_SetHoldOn(null_pcs, 1);
  156. TIME_Regist(ReadAdc_timcallback);
  157. break;
  158. case 2:
  159. TIME_UnRegist(ReadAdc_timcallback);
  160. nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL);
  161. Process_Stop(null_pcs);
  162. log("ad:%d,%d\n",host_ram.adc_value, host_ram.pair_line);
  163. PAIR_CHARGERPIN_PRINT("ADC ( %d ): pair_line %d\n", host_ram.adc_value, host_ram.pair_line);
  164. if(host_ram.adc_value>DISCON_HOST_MAX_ADC)
  165. {
  166. PAIR_CHARGERPIN_PRINT("CHARGE\n");
  167. if(host_ram.pair_line!=CHARGE)
  168. {
  169. PAIR_CHARGERPIN_PRINT("INTO CHARGE %d\n", 1);
  170. host_ram.pair_line=CHARGE;
  171. }
  172. host_ram.statu=1;
  173. }
  174. else if(host_ram.adc_value>DISCON_HOST_MIN_ADC)
  175. {
  176. PAIR_CHARGERPIN_PRINT("DISCONNECT_CHARGE\n");
  177. if(host_ram.pair_line!=DISCONNECT_CHARGE)
  178. {
  179. PAIR_CHARGERPIN_PRINT("INTO DISCONNECT_CHARGE %d\n", 1);
  180. host_ram.pair_line=DISCONNECT_CHARGE;
  181. }
  182. host_ram.statu=1;
  183. }
  184. else if(host_ram.adc_value>CON_HOST_MIN_ADC)
  185. {
  186. PAIR_CHARGERPIN_PRINT("CONNECT_NO_CHARGE\n");
  187. if(host_ram.pair_line!=CONNECT_NO_CHARGE)
  188. {
  189. host_ram.pair_line=CONNECT_NO_CHARGE;
  190. nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_PULLUP);
  191. host_ram.delay_pair_count=TIME_GetTicks();
  192. Process_UpdatePeroid(app_pair_host_process,1);
  193. Process_SetHoldOn(app_pair_host_process,1);
  194. host_ram.statu=3;
  195. PAIR_CHARGERPIN_PRINT("INTO CONNECT_NO_CHARGE %d\n", nrf_gpio_pin_read(PIN_CHARGING));
  196. }
  197. else
  198. {
  199. host_ram.statu=1;
  200. }
  201. }
  202. else
  203. {
  204. PAIR_CHARGERPIN_PRINT("ADCVALUE ======================================================ERROR %d\n", host_ram.adc_value);
  205. host_ram.statu=1;
  206. }
  207. break;
  208. case 3://上拉让从机发现配对信号
  209. if ((TIME_GetTicks() - host_ram.delay_pair_count > ADC_CHECKMS+ADC_CHECKMS))
  210. {
  211. PAIR_CHARGERPIN_PRINT(" UP pull over time %d \n", TIME_GetTicks());
  212. log("pull over time\n");
  213. nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL);
  214. Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS);
  215. Process_SetHoldOn(app_pair_host_process,0);
  216. host_ram.statu=1;
  217. }
  218. else if(nrf_gpio_pin_read(PIN_CHARGING))//读到上拉信号以后,说明client已经连接上
  219. {
  220. PAIR_CHARGERPIN_PRINT(" nrf_gpio_pin_read(PIN_CHARGING SUCCESS \n", TIME_GetTicks());
  221. host_ram.statu=4;
  222. host_ram.delay_pair_count=TIME_GetTicks();
  223. nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL);
  224. }
  225. break;
  226. case 4:
  227. if(nrf_gpio_pin_read(PIN_CHARGING)==0)
  228. {
  229. PAIR_CHARGERPIN_PRINT(" into_event %d \n", TIME_GetTicks());
  230. host_ram.statu=5;
  231. host_ram.delay_pair_count=TIME_GetTicks();
  232. ADC_Disable();
  233. ADC_RemovePinChannel(PIN_CHARGING, PIN_CHARGING_CHANNEL);
  234. ADC_Initialize();
  235. log("into_event-H\n");
  236. host_ram.into_event();
  237. }
  238. else if(TIME_GetTicks() - host_ram.delay_pair_count > 500)
  239. {
  240. PAIR_CHARGERPIN_PRINT(" wait low over time %d\n", nrf_gpio_pin_read(PIN_CHARGING));
  241. host_ram.statu=0;
  242. Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS);
  243. Process_SetHoldOn(app_pair_host_process,0);
  244. i=3;
  245. }
  246. break;
  247. case 5:
  248. if (TIME_GetTicks() - host_ram.delay_pair_count > PAIROVERTIME)
  249. {
  250. PAIR_CHARGERPIN_PRINT(" over_event %d\n", TIME_GetTicks());
  251. host_ram.statu=0;
  252. host_ram.over_event();
  253. Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS);
  254. Process_SetHoldOn(app_pair_host_process,0);
  255. i=3;
  256. }
  257. break;
  258. //==============================s
  259. case 11:
  260. if((TIME_GetTicks() - host_ram.delay_pair_count > 5000)||(shefcheck_event_fag))
  261. {
  262. if(shefcheck_event_fag)
  263. {
  264. if(check_callbackt_ram)check_callbackt_ram(shefcheck_buff[0]);
  265. }
  266. else
  267. {
  268. PAIR_CHARGERPIN_PRINT(" =====================wait low over time %d\n", nrf_gpio_pin_read(PIN_CHARGING));
  269. }
  270. host_ram.statu=0;
  271. Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS);
  272. Process_SetHoldOn(app_pair_host_process,0);
  273. one_byte_receive_uninit();
  274. i=3;
  275. }
  276. break;
  277. //==============================s
  278. }
  279. }
  280. }
  281. //============================================================================================================
  282. extern void app_pair_client_process(void);
  283. struct{
  284. char statu;
  285. short adc_value;
  286. pair_line_t pair_line;
  287. unsigned int delay_pair_count;
  288. Event into_event;
  289. Event over_event;
  290. }client_ram={0,0,DISCONNECT_CHARGE,0,NULL,NULL};
  291. #define CONN_MAX_ADC 1000
  292. #define CONN_MIN_ADC 600
  293. void app_pair_client_init(Event into,Event over)
  294. {
  295. client_ram.adc_value=0;
  296. client_ram.delay_pair_count=0;
  297. client_ram.statu=0;
  298. client_ram.pair_line=DISCONNECT_CHARGE;
  299. client_ram.into_event=into;
  300. client_ram.over_event=over;
  301. Process_Start(ADC_CHECKMS, "app_pair_client_process", app_pair_client_process);
  302. }
  303. void app_pair_client_uninit(void)
  304. {
  305. Process_Stop(app_pair_client_process);
  306. }
  307. static void app_pair_client_process(void)
  308. {
  309. for(int i=1;i>0;i--)
  310. {
  311. switch(client_ram.statu)
  312. {
  313. case 0:
  314. ADC_Disable();
  315. ADC_SetPinChannel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_NOPULL);
  316. ADC_Initialize();
  317. client_ram.statu=1;
  318. break;
  319. case 1:
  320. if(mFlash.isHost==HOST_SHOSE)
  321. {
  322. app_pair_client_uninit();
  323. app_pair_chargerpin_Init();
  324. PAIR_CHARGERPIN_PRINT("mFlash.isHost change %d %d\n",TIME_GetTicks(),mFlash.isHost );
  325. }
  326. if (ADC_OP_SUCCESS != ADC_Read(PIN_CHARGING_CHANNEL, &client_ram.adc_value))
  327. {
  328. PAIR_CHARGERPIN_PRINT("ADC_Read ERROR %d\n", client_ram.adc_value);
  329. }
  330. log("ad:%d,%d\n",client_ram.adc_value, client_ram.pair_line);
  331. PAIR_CHARGERPIN_PRINT("ADC ( %d ):isHost %d Ticks %d\n", client_ram.adc_value, mFlash.isHost,TIME_GetTicks());
  332. //==============================s
  333. if(client_ram.adc_value>2800)
  334. {
  335. if(check_callbackt_ram)
  336. {
  337. client_ram.statu=11;
  338. client_ram.delay_pair_count=TIME_GetTicks();
  339. Process_UpdatePeroid(app_pair_client_process,100);
  340. Process_SetHoldOn(app_pair_client_process,1);
  341. one_byte_receive_init(shefcheck_buff,2,shefcheck_event);
  342. shefcheck_event_fag=0;
  343. i=2;
  344. break;
  345. }
  346. else
  347. {
  348. PAIR_CHARGERPIN_PRINT("check_callbackt_ram NULL\n");
  349. }
  350. }
  351. //==============================e
  352. if(client_ram.adc_value>CONN_MAX_ADC)
  353. {
  354. PAIR_CHARGERPIN_PRINT("CHARGE\n");
  355. if(client_ram.pair_line!=CHARGE)
  356. {
  357. PAIR_CHARGERPIN_PRINT("INTO CHARGE %d\n", 1);
  358. client_ram.pair_line=CHARGE;
  359. }
  360. }
  361. else if(client_ram.adc_value>CONN_MIN_ADC)
  362. {
  363. PAIR_CHARGERPIN_PRINT("CONNECT_NO_CHARGE\n");
  364. if(client_ram.pair_line!=CONNECT_NO_CHARGE)
  365. {
  366. PAIR_CHARGERPIN_PRINT("INTO CONNECT_NO_CHARGE %d\n", 1);
  367. client_ram.pair_line=CONNECT_NO_CHARGE;
  368. ADC_Disable();
  369. ADC_RemovePinChannel(PIN_CHARGING, PIN_CHARGING_CHANNEL);
  370. ADC_Initialize();
  371. nrf_gpio_cfg(
  372. PIN_CHARGING,
  373. NRF_GPIO_PIN_DIR_OUTPUT,
  374. NRF_GPIO_PIN_INPUT_CONNECT,
  375. NRF_GPIO_PIN_NOPULL,
  376. NRF_GPIO_PIN_D0H1,
  377. NRF_GPIO_PIN_NOSENSE);
  378. nrf_gpio_cfg_watcher(PIN_CHARGING);
  379. nrf_gpio_pin_set(PIN_CHARGING);//拉高100ms
  380. client_ram.delay_pair_count=TIME_GetTicks();
  381. Process_UpdatePeroid(app_pair_client_process,1);
  382. Process_SetHoldOn(app_pair_client_process,1);
  383. client_ram.statu=2;
  384. }
  385. }
  386. else
  387. {
  388. if(client_ram.pair_line!=DISCONNECT_CHARGE)
  389. {
  390. PAIR_CHARGERPIN_PRINT("INTO DISCONNECT_CHARGE %d\n", 1);
  391. client_ram.pair_line=DISCONNECT_CHARGE;
  392. }
  393. }
  394. break;
  395. case 2:
  396. if(TIME_GetTicks() - client_ram.delay_pair_count > 100)//等100ms
  397. {
  398. nrf_gpio_pin_clear(PIN_CHARGING);
  399. client_ram.statu=3;
  400. client_ram.delay_pair_count=TIME_GetTicks();
  401. }
  402. break;
  403. case 3:
  404. if(nrf_gpio_pin_read(PIN_CHARGING)==0)
  405. {
  406. log("into_event-C\n");
  407. client_ram.into_event();
  408. client_ram.delay_pair_count=TIME_GetTicks();
  409. PAIR_CHARGERPIN_PRINT(" into_event-C %d\n", TIME_GetTicks());
  410. PAIR_CHARGERPIN_PRINT(" wait pair %d\n", nrf_gpio_pin_read(PIN_CHARGING));
  411. client_ram.statu=4;
  412. }
  413. else if(TIME_GetTicks() - client_ram.delay_pair_count > 100)
  414. {
  415. PAIR_CHARGERPIN_PRINT(" wait low over time %d\n", nrf_gpio_pin_read(PIN_CHARGING));
  416. client_ram.statu=0;
  417. Process_UpdatePeroid(app_pair_client_process,ADC_CHECKMS);
  418. Process_SetHoldOn(app_pair_client_process,0);
  419. i=3;
  420. }
  421. break;
  422. case 4:
  423. if (TIME_GetTicks() - client_ram.delay_pair_count > PAIROVERTIME)
  424. {
  425. client_ram.statu=0;
  426. client_ram.over_event();
  427. Process_UpdatePeroid(app_pair_client_process,ADC_CHECKMS);
  428. Process_SetHoldOn(app_pair_client_process,0);
  429. PAIR_CHARGERPIN_PRINT(" over_event %d\n", TIME_GetTicks());
  430. i=3;
  431. }
  432. break;
  433. //==============================s
  434. case 11:
  435. if((TIME_GetTicks() - client_ram.delay_pair_count > 5000)||(shefcheck_event_fag))
  436. {
  437. if(shefcheck_event_fag)
  438. {
  439. if(check_callbackt_ram)check_callbackt_ram(shefcheck_buff[0]);
  440. }
  441. else
  442. {
  443. PAIR_CHARGERPIN_PRINT(" =====================wait low over time %d\n", nrf_gpio_pin_read(PIN_CHARGING));
  444. }
  445. client_ram.statu=0;
  446. Process_UpdatePeroid(app_pair_client_process,ADC_CHECKMS);
  447. Process_SetHoldOn(app_pair_client_process,0);
  448. one_byte_receive_uninit();
  449. i=3;
  450. }
  451. break;
  452. //==============================s
  453. }
  454. }
  455. }