app_chargerpin_conn_detect.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. //=================debug=============
  11. #include "ringframe.h"
  12. extern char logbuftemp[50];
  13. extern int logbuftemp_len ;
  14. extern ringframe_t battlog;
  15. #define log(...) {logbuftemp_len = sprintf(logbuftemp,__VA_ARGS__); while(ringframe_in(&battlog,logbuftemp,logbuftemp_len)!=0){ringframe_throw(&battlog);}}
  16. //void cb_BLE_Client_PRADC(void* handle)
  17. //{
  18. //// BLE_Client_Rx_t* target = handle;
  19. // DEBUG_LOG("cb_BLE_Client_ERR:%d,%d\n", 1, 1);
  20. // BLE_Client_Rx_Regist(BLE_ERR, cb_BLE_Client_ERR);
  21. //}
  22. //=================debug=============
  23. //====================================================================================
  24. #define ADC_CHECKMS 1000
  25. #define PAIROVERTIME 3000
  26. #define DELAY_PAIR_COUNT 100
  27. struct{
  28. char statu;
  29. unsigned int pulluppcs_timer;
  30. short adc_value;
  31. pair_line_t pair_line;
  32. unsigned int delay_pair_count;
  33. Event into_event;
  34. Event over_event;
  35. }host_ram={0,0,0,DISCONNECT_CHARGE,0,NULL,NULL};
  36. extern void app_pair_host_process(void);
  37. void app_pair_host_init(Event into,Event over)
  38. {
  39. host_ram.adc_value=0;
  40. host_ram.delay_pair_count=0;
  41. host_ram.pulluppcs_timer=0;
  42. host_ram.statu=0;
  43. host_ram.pair_line=DISCONNECT_CHARGE;
  44. host_ram.into_event=into;
  45. host_ram.over_event=over;
  46. Process_Start(ADC_CHECKMS, "app_pair_host_process", app_pair_host_process);
  47. }
  48. void app_pair_host_uninit(void)
  49. {
  50. Process_Stop(app_pair_host_process);
  51. }
  52. #define DISCON_HOST_MAX_ADC 1200
  53. #define DISCON_HOST_MIN_ADC 700
  54. #define CON_HOST_MAX_ADC DISCON_HOST_MIN_ADC
  55. #define CON_HOST_MIN_ADC 300
  56. static void null_pcs(void)
  57. {
  58. }
  59. #define UPPULL_MS 5
  60. static void ReadAdc_timcallback(void *t)
  61. {
  62. if(TIME_GetTicks()-host_ram.pulluppcs_timer<UPPULL_MS)return;
  63. if(TIME_GetTicks()-host_ram.pulluppcs_timer==UPPULL_MS)
  64. {
  65. uint32_t errcode = ADC_ReadChargePin(PIN_CHARGING_CHANNEL);
  66. if (ADC_OP_SUCCESS != errcode)
  67. {
  68. char buff[20];
  69. memset(buff,0,20);
  70. sprintf(buff,"ADC ERROR %d",errcode);
  71. Except_TxError(EXCEPT_Pair,buff);
  72. PAIR_CHARGERPIN_PRINT("ADC_ReadChargePin ======%d================================================ERROR %d\n",TIME_GetTicks(), errcode);
  73. }
  74. }
  75. if(TIME_GetTicks()-host_ram.pulluppcs_timer>UPPULL_MS)
  76. {
  77. uint8_t state = bsp_Get_ChargePinADC(&host_ram.adc_value);
  78. if(state==1)//adc读取成功
  79. {
  80. host_ram.statu=2;
  81. app_pair_host_process();
  82. }
  83. else
  84. {
  85. TIME_UnRegist(ReadAdc_timcallback);
  86. nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL);
  87. Process_Stop(null_pcs);
  88. Except_TxError(EXCEPT_Pair,"adc overtimer");
  89. PAIR_CHARGERPIN_PRINT("bsp_Get_ChargePinADC %d===========%d===========================================ERROR %d\n",host_ram.adc_value,TIME_GetTicks(), state);
  90. }
  91. }
  92. }
  93. static void app_pair_host_process(void)
  94. {
  95. for(int i=1;i>0;i--)
  96. {
  97. switch(host_ram.statu)
  98. {
  99. case 0:
  100. ADC_Disable();
  101. ADC_SetPinChannel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_NOPULL);
  102. ADC_Initialize();
  103. host_ram.statu=1;
  104. break;
  105. case 1:
  106. if(mFlash.isHost!=HOST_SHOSE)
  107. {
  108. app_pair_host_uninit();
  109. app_pair_chargerpin_Init();
  110. PAIR_CHARGERPIN_PRINT("mFlash.isHost change %d %d\n",TIME_GetTicks(),mFlash.isHost );
  111. }
  112. // if (ADC_OP_SUCCESS != ADC_Read(PIN_CHARGING_CHANNEL, &host_ram.adc_value))
  113. // {
  114. // PAIR_CHARGERPIN_PRINT("ADC_Read ERROR %d\n", host_ram.adc_value);
  115. // }
  116. // PAIR_CHARGERPIN_PRINT("ADC f( %d ):isHost %d Ticks %d\n", host_ram.adc_value, mFlash.isHost,TIME_GetTicks());
  117. nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_PULLUP);
  118. PAIR_CHARGERPIN_PRINT("nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_PULLUP); %d \n",TIME_GetTicks() );
  119. host_ram.pulluppcs_timer = TIME_GetTicks();
  120. Process_Start(0, "null_pcs", null_pcs);
  121. Process_SetHoldOn(null_pcs, 1);
  122. TIME_Regist(ReadAdc_timcallback);
  123. break;
  124. case 2:
  125. TIME_UnRegist(ReadAdc_timcallback);
  126. nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL);
  127. Process_Stop(null_pcs);
  128. log("ad:%d,%d\n",host_ram.adc_value, host_ram.pair_line);
  129. PAIR_CHARGERPIN_PRINT("ADC ( %d ): pair_line %d\n", host_ram.adc_value, host_ram.pair_line);
  130. if(host_ram.adc_value>DISCON_HOST_MAX_ADC)
  131. {
  132. PAIR_CHARGERPIN_PRINT("CHARGE\n");
  133. if(host_ram.pair_line!=CHARGE)
  134. {
  135. PAIR_CHARGERPIN_PRINT("INTO CHARGE %d\n", 1);
  136. host_ram.pair_line=CHARGE;
  137. }
  138. host_ram.statu=1;
  139. }
  140. else if(host_ram.adc_value>DISCON_HOST_MIN_ADC)
  141. {
  142. PAIR_CHARGERPIN_PRINT("DISCONNECT_CHARGE\n");
  143. if(host_ram.pair_line!=DISCONNECT_CHARGE)
  144. {
  145. PAIR_CHARGERPIN_PRINT("INTO DISCONNECT_CHARGE %d\n", 1);
  146. host_ram.pair_line=DISCONNECT_CHARGE;
  147. }
  148. host_ram.statu=1;
  149. }
  150. else if(host_ram.adc_value>CON_HOST_MIN_ADC)
  151. {
  152. PAIR_CHARGERPIN_PRINT("CONNECT_NO_CHARGE\n");
  153. if(host_ram.pair_line!=CONNECT_NO_CHARGE)
  154. {
  155. host_ram.pair_line=CONNECT_NO_CHARGE;
  156. nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_PULLUP);
  157. host_ram.delay_pair_count=TIME_GetTicks();
  158. Process_UpdatePeroid(app_pair_host_process,1);
  159. Process_SetHoldOn(app_pair_host_process,1);
  160. host_ram.statu=3;
  161. PAIR_CHARGERPIN_PRINT("INTO CONNECT_NO_CHARGE %d\n", nrf_gpio_pin_read(PIN_CHARGING));
  162. }
  163. else
  164. {
  165. host_ram.statu=1;
  166. }
  167. }
  168. else
  169. {
  170. PAIR_CHARGERPIN_PRINT("ADCVALUE ======================================================ERROR %d\n", host_ram.adc_value);
  171. host_ram.statu=1;
  172. }
  173. break;
  174. case 3://上拉让从机发现配对信号
  175. if ((TIME_GetTicks() - host_ram.delay_pair_count > ADC_CHECKMS)||(nrf_gpio_pin_read(PIN_CHARGING)))
  176. {
  177. PAIR_CHARGERPIN_PRINT(" into_event %d \n", TIME_GetTicks());
  178. host_ram.statu=4;
  179. nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL);
  180. host_ram.delay_pair_count=TIME_GetTicks();
  181. host_ram.into_event();
  182. Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS);
  183. Process_SetHoldOn(app_pair_host_process,0);
  184. }
  185. break;
  186. case 4:
  187. if (TIME_GetTicks() - host_ram.delay_pair_count > PAIROVERTIME)
  188. {
  189. PAIR_CHARGERPIN_PRINT(" over_event %d\n", TIME_GetTicks());
  190. host_ram.statu=0;
  191. host_ram.over_event();
  192. i=3;
  193. }
  194. break;
  195. }
  196. }
  197. }
  198. //============================================================================================================
  199. extern void app_pair_client_process(void);
  200. struct{
  201. char statu;
  202. short adc_value;
  203. pair_line_t pair_line;
  204. unsigned int delay_pair_count;
  205. Event into_event;
  206. Event over_event;
  207. }client_ram={0,0,DISCONNECT_CHARGE,0,NULL,NULL};
  208. #define CONN_MAX_ADC 1000
  209. #define CONN_MIN_ADC 600
  210. void app_pair_client_init(Event into,Event over)
  211. {
  212. client_ram.adc_value=0;
  213. client_ram.delay_pair_count=0;
  214. client_ram.statu=0;
  215. client_ram.pair_line=DISCONNECT_CHARGE;
  216. client_ram.into_event=into;
  217. client_ram.over_event=over;
  218. Process_Start(ADC_CHECKMS, "app_pair_client_process", app_pair_client_process);
  219. }
  220. void app_pair_client_uninit(void)
  221. {
  222. Process_Stop(app_pair_client_process);
  223. }
  224. static void app_pair_client_process(void)
  225. {
  226. for(int i=1;i>0;i--)
  227. {
  228. switch(client_ram.statu)
  229. {
  230. case 0:
  231. ADC_Disable();
  232. ADC_SetPinChannel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_NOPULL);
  233. ADC_Initialize();
  234. client_ram.statu=1;
  235. break;
  236. case 1:
  237. if(mFlash.isHost==HOST_SHOSE)
  238. {
  239. app_pair_client_uninit();
  240. app_pair_chargerpin_Init();
  241. PAIR_CHARGERPIN_PRINT("mFlash.isHost change %d %d\n",TIME_GetTicks(),mFlash.isHost );
  242. }
  243. if (ADC_OP_SUCCESS != ADC_Read(PIN_CHARGING_CHANNEL, &client_ram.adc_value))
  244. {
  245. PAIR_CHARGERPIN_PRINT("ADC_Read ERROR %d\n", client_ram.adc_value);
  246. }
  247. log("ad:%d,%d\n",client_ram.adc_value, client_ram.pair_line);
  248. PAIR_CHARGERPIN_PRINT("ADC ( %d ):isHost %d Ticks %d\n", client_ram.adc_value, mFlash.isHost,TIME_GetTicks());
  249. if(client_ram.adc_value>CONN_MAX_ADC)
  250. {
  251. PAIR_CHARGERPIN_PRINT("CHARGE\n");
  252. if(client_ram.pair_line!=CHARGE)
  253. {
  254. PAIR_CHARGERPIN_PRINT("INTO CHARGE %d\n", 1);
  255. client_ram.pair_line=CHARGE;
  256. }
  257. }
  258. else if(client_ram.adc_value>CONN_MIN_ADC)
  259. {
  260. PAIR_CHARGERPIN_PRINT("CONNECT_NO_CHARGE\n");
  261. if(client_ram.pair_line!=CONNECT_NO_CHARGE)
  262. {
  263. PAIR_CHARGERPIN_PRINT("INTO CONNECT_NO_CHARGE %d\n", 1);
  264. client_ram.pair_line=CONNECT_NO_CHARGE;
  265. client_ram.delay_pair_count=TIME_GetTicks();
  266. Process_UpdatePeroid(app_pair_client_process,1);
  267. Process_SetHoldOn(app_pair_client_process,1);
  268. client_ram.statu=2;
  269. nrf_gpio_cfg(
  270. PIN_CHARGING,
  271. NRF_GPIO_PIN_DIR_OUTPUT,
  272. NRF_GPIO_PIN_INPUT_CONNECT,
  273. NRF_GPIO_PIN_NOPULL,
  274. NRF_GPIO_PIN_D0H1,
  275. NRF_GPIO_PIN_NOSENSE);
  276. nrf_gpio_cfg_watcher(PIN_CHARGING);
  277. nrf_gpio_pin_set(PIN_CHARGING);
  278. client_ram.into_event();
  279. PAIR_CHARGERPIN_PRINT(" into_event %d\n", TIME_GetTicks());
  280. PAIR_CHARGERPIN_PRINT(" wait pair %d\n", nrf_gpio_pin_read(PIN_CHARGING));
  281. }
  282. }
  283. else
  284. {
  285. if(client_ram.pair_line!=DISCONNECT_CHARGE)
  286. {
  287. PAIR_CHARGERPIN_PRINT("INTO DISCONNECT_CHARGE %d\n", 1);
  288. client_ram.pair_line=DISCONNECT_CHARGE;
  289. }
  290. }
  291. break;
  292. case 2:
  293. if(TIME_GetTicks() - client_ram.delay_pair_count > 100)//等
  294. {
  295. nrf_gpio_pin_clear(PIN_CHARGING);
  296. client_ram.statu=3;
  297. Process_UpdatePeroid(app_pair_client_process,ADC_CHECKMS);
  298. Process_SetHoldOn(app_pair_client_process,0);
  299. }
  300. break;
  301. case 3:
  302. if (TIME_GetTicks() - client_ram.delay_pair_count > PAIROVERTIME)
  303. {
  304. client_ram.statu=0;
  305. client_ram.over_event();
  306. PAIR_CHARGERPIN_PRINT(" over_event %d\n", TIME_GetTicks());
  307. i=3;
  308. }
  309. break;
  310. }
  311. }
  312. }