#include "app_chargerpin_conn_detect.h" #include "system.h" #include "bsp_time.h" #include "bsp_adc.h" #include "ble_comm.h" #include "hal_led.h" #include "hal_mt.h" #include "app_flash.h" #include "app_pair_chargerpin.h" #include "app_one_wire.h" //=================debug============= #include "ringframe.h" extern char logbuftemp[50]; extern int logbuftemp_len ; extern ringframe_t battlog; #define log(...) {logbuftemp_len = sprintf(logbuftemp,__VA_ARGS__); while(ringframe_in(&battlog,logbuftemp,logbuftemp_len)!=0){ringframe_throw(&battlog);}} //void cb_BLE_Client_PRADC(void* handle) //{ //// BLE_Client_Rx_t* target = handle; // DEBUG_LOG("cb_BLE_Client_ERR:%d,%d\n", 1, 1); // BLE_Client_Rx_Regist(BLE_ERR, cb_BLE_Client_ERR); //} //=================debug============= //==================================================================================== #define ADC_CHECKMS 1000 #define PAIROVERTIME 3000 #define DELAY_PAIR_COUNT 100 struct{ char statu; unsigned int pulluppcs_timer; short adc_value; pair_line_t pair_line; unsigned int delay_pair_count; Event into_event; Event over_event; }host_ram={0,0,0,DISCONNECT_CHARGE,0,NULL,NULL}; extern void app_pair_host_process(void); void app_pair_host_init(Event into,Event over) { host_ram.adc_value=0; host_ram.delay_pair_count=0; host_ram.pulluppcs_timer=0; host_ram.statu=0; host_ram.pair_line=DISCONNECT_CHARGE; host_ram.into_event=into; host_ram.over_event=over; Process_Start(ADC_CHECKMS, "app_pair_host_process", app_pair_host_process); } void app_pair_host_uninit(void) { Process_Stop(app_pair_host_process); } #define DISCON_HOST_MAX_ADC 1200 #define DISCON_HOST_MIN_ADC 600 #define CON_HOST_MAX_ADC DISCON_HOST_MIN_ADC #define CON_HOST_MIN_ADC 300 static void null_pcs(void) { } #define UPPULL_MS 3 static void ReadAdc_timcallback(void *t) { if(TIME_GetTicks()-host_ram.pulluppcs_timerUPPULL_MS) { uint8_t state = bsp_Get_ChargePinADC(&host_ram.adc_value); if(state==1)//adc读取成功 { host_ram.statu=2; app_pair_host_process(); } else { TIME_UnRegist(ReadAdc_timcallback); nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL); Process_Stop(null_pcs); Except_TxError(EXCEPT_Pair,"adc overtimer"); PAIR_CHARGERPIN_PRINT("bsp_Get_ChargePinADC %d===========%d===========================================ERROR %d\n",host_ram.adc_value,TIME_GetTicks(), state); } } } //------------------------------------------- unsigned char shefcheck_buff[5]; char shefcheck_event_fag=0; check_callbackt check_callbackt_ram; void shefcheck_event(unsigned char *buff, int length) { shefcheck_event_fag=1; } void check_callback_regist(check_callbackt evt) { check_callbackt_ram=evt; } //------------------------------------------- static void app_pair_host_process(void) { for(int i=1;i>0;i--) { switch(host_ram.statu) { case 0: ADC_Disable(); ADC_SetPinChannel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_NOPULL); ADC_Initialize(); host_ram.statu=1; break; case 1: if(mFlash.isHost!=HOST_SHOSE) { app_pair_host_uninit(); app_pair_chargerpin_Init(); PAIR_CHARGERPIN_PRINT("mFlash.isHost change %d %d\n",TIME_GetTicks(),mFlash.isHost ); } //==============================s if (ADC_OP_SUCCESS != ADC_Read(PIN_CHARGING_CHANNEL, &host_ram.adc_value)) { PAIR_CHARGERPIN_PRINT("ADC_Read ERROR %d\n", host_ram.adc_value); } PAIR_CHARGERPIN_PRINT("ADC f( %d ):isHost %d Ticks %d\n", host_ram.adc_value, mFlash.isHost,TIME_GetTicks()); if(host_ram.adc_value>2800) { if(check_callbackt_ram) { host_ram.statu=11; host_ram.delay_pair_count=TIME_GetTicks(); Process_UpdatePeroid(app_pair_host_process,100); Process_SetHoldOn(app_pair_host_process,1); one_byte_receive_init(shefcheck_buff,2,shefcheck_event); shefcheck_event_fag=0; i=2; break; } else { PAIR_CHARGERPIN_PRINT("check_callbackt_ram NULL\n"); } } //==============================e nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_PULLUP); PAIR_CHARGERPIN_PRINT("nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_PULLUP); %d \n",TIME_GetTicks() ); host_ram.pulluppcs_timer = TIME_GetTicks(); Process_Start(0, "null_pcs", null_pcs); Process_SetHoldOn(null_pcs, 1); TIME_Regist(ReadAdc_timcallback); break; case 2: TIME_UnRegist(ReadAdc_timcallback); nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL); Process_Stop(null_pcs); log("ad:%d,%d\n",host_ram.adc_value, host_ram.pair_line); PAIR_CHARGERPIN_PRINT("ADC ( %d ): pair_line %d\n", host_ram.adc_value, host_ram.pair_line); if(host_ram.adc_value>DISCON_HOST_MAX_ADC) { PAIR_CHARGERPIN_PRINT("CHARGE\n"); if(host_ram.pair_line!=CHARGE) { PAIR_CHARGERPIN_PRINT("INTO CHARGE %d\n", 1); host_ram.pair_line=CHARGE; } host_ram.statu=1; } else if(host_ram.adc_value>DISCON_HOST_MIN_ADC) { PAIR_CHARGERPIN_PRINT("DISCONNECT_CHARGE\n"); if(host_ram.pair_line!=DISCONNECT_CHARGE) { PAIR_CHARGERPIN_PRINT("INTO DISCONNECT_CHARGE %d\n", 1); host_ram.pair_line=DISCONNECT_CHARGE; } host_ram.statu=1; } else if(host_ram.adc_value>CON_HOST_MIN_ADC) { PAIR_CHARGERPIN_PRINT("CONNECT_NO_CHARGE\n"); if(host_ram.pair_line!=CONNECT_NO_CHARGE) { host_ram.pair_line=CONNECT_NO_CHARGE; nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_PULLUP); host_ram.delay_pair_count=TIME_GetTicks(); Process_UpdatePeroid(app_pair_host_process,1); Process_SetHoldOn(app_pair_host_process,1); host_ram.statu=3; PAIR_CHARGERPIN_PRINT("INTO CONNECT_NO_CHARGE %d\n", nrf_gpio_pin_read(PIN_CHARGING)); } else { host_ram.statu=1; } } else { PAIR_CHARGERPIN_PRINT("ADCVALUE ======================================================ERROR %d\n", host_ram.adc_value); host_ram.statu=1; } break; case 3://上拉让从机发现配对信号 if ((TIME_GetTicks() - host_ram.delay_pair_count > ADC_CHECKMS+ADC_CHECKMS)) { PAIR_CHARGERPIN_PRINT(" UP pull over time %d \n", TIME_GetTicks()); log("pull over time\n"); nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL); Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS); Process_SetHoldOn(app_pair_host_process,0); host_ram.statu=1; } else if(nrf_gpio_pin_read(PIN_CHARGING))//读到上拉信号以后,说明client已经连接上 { PAIR_CHARGERPIN_PRINT(" nrf_gpio_pin_read(PIN_CHARGING SUCCESS \n", TIME_GetTicks()); host_ram.statu=4; host_ram.delay_pair_count=TIME_GetTicks(); nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL); } break; case 4: if(nrf_gpio_pin_read(PIN_CHARGING)==0) { PAIR_CHARGERPIN_PRINT(" into_event %d \n", TIME_GetTicks()); host_ram.statu=5; host_ram.delay_pair_count=TIME_GetTicks(); ADC_Disable(); ADC_RemovePinChannel(PIN_CHARGING, PIN_CHARGING_CHANNEL); ADC_Initialize(); log("into_event-H\n"); host_ram.into_event(); } else if(TIME_GetTicks() - host_ram.delay_pair_count > 500) { PAIR_CHARGERPIN_PRINT(" wait low over time %d\n", nrf_gpio_pin_read(PIN_CHARGING)); host_ram.statu=0; Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS); Process_SetHoldOn(app_pair_host_process,0); i=3; } break; case 5: if (TIME_GetTicks() - host_ram.delay_pair_count > PAIROVERTIME) { PAIR_CHARGERPIN_PRINT(" over_event %d\n", TIME_GetTicks()); host_ram.statu=0; host_ram.over_event(); Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS); Process_SetHoldOn(app_pair_host_process,0); i=3; } break; //==============================s case 11: if((TIME_GetTicks() - host_ram.delay_pair_count > 5000)||(shefcheck_event_fag)) { if(shefcheck_event_fag) { if(check_callbackt_ram)check_callbackt_ram(shefcheck_buff[0]); } else { PAIR_CHARGERPIN_PRINT(" =====================wait low over time %d\n", nrf_gpio_pin_read(PIN_CHARGING)); } host_ram.statu=0; Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS); Process_SetHoldOn(app_pair_host_process,0); one_byte_receive_uninit(); i=3; } break; //==============================s } } } //============================================================================================================ extern void app_pair_client_process(void); struct{ char statu; short adc_value; pair_line_t pair_line; unsigned int delay_pair_count; Event into_event; Event over_event; }client_ram={0,0,DISCONNECT_CHARGE,0,NULL,NULL}; #define CONN_MAX_ADC 1000 #define CONN_MIN_ADC 600 void app_pair_client_init(Event into,Event over) { client_ram.adc_value=0; client_ram.delay_pair_count=0; client_ram.statu=0; client_ram.pair_line=DISCONNECT_CHARGE; client_ram.into_event=into; client_ram.over_event=over; Process_Start(ADC_CHECKMS, "app_pair_client_process", app_pair_client_process); } void app_pair_client_uninit(void) { Process_Stop(app_pair_client_process); } static void app_pair_client_process(void) { for(int i=1;i>0;i--) { switch(client_ram.statu) { case 0: ADC_Disable(); ADC_SetPinChannel(PIN_CHARGING, PIN_CHARGING_CHANNEL, NRF_GPIO_PIN_NOPULL); ADC_Initialize(); client_ram.statu=1; break; case 1: if(mFlash.isHost==HOST_SHOSE) { app_pair_client_uninit(); app_pair_chargerpin_Init(); PAIR_CHARGERPIN_PRINT("mFlash.isHost change %d %d\n",TIME_GetTicks(),mFlash.isHost ); } if (ADC_OP_SUCCESS != ADC_Read(PIN_CHARGING_CHANNEL, &client_ram.adc_value)) { PAIR_CHARGERPIN_PRINT("ADC_Read ERROR %d\n", client_ram.adc_value); } log("ad:%d,%d\n",client_ram.adc_value, client_ram.pair_line); PAIR_CHARGERPIN_PRINT("ADC ( %d ):isHost %d Ticks %d\n", client_ram.adc_value, mFlash.isHost,TIME_GetTicks()); //==============================s if(client_ram.adc_value>2800) { if(check_callbackt_ram) { client_ram.statu=11; client_ram.delay_pair_count=TIME_GetTicks(); Process_UpdatePeroid(app_pair_client_process,100); Process_SetHoldOn(app_pair_client_process,1); one_byte_receive_init(shefcheck_buff,2,shefcheck_event); shefcheck_event_fag=0; i=2; break; } else { PAIR_CHARGERPIN_PRINT("check_callbackt_ram NULL\n"); } } //==============================e if(client_ram.adc_value>CONN_MAX_ADC) { PAIR_CHARGERPIN_PRINT("CHARGE\n"); if(client_ram.pair_line!=CHARGE) { PAIR_CHARGERPIN_PRINT("INTO CHARGE %d\n", 1); client_ram.pair_line=CHARGE; } } else if(client_ram.adc_value>CONN_MIN_ADC) { PAIR_CHARGERPIN_PRINT("CONNECT_NO_CHARGE\n"); if(client_ram.pair_line!=CONNECT_NO_CHARGE) { PAIR_CHARGERPIN_PRINT("INTO CONNECT_NO_CHARGE %d\n", 1); client_ram.pair_line=CONNECT_NO_CHARGE; ADC_Disable(); ADC_RemovePinChannel(PIN_CHARGING, PIN_CHARGING_CHANNEL); ADC_Initialize(); nrf_gpio_cfg( PIN_CHARGING, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_CONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_D0H1, NRF_GPIO_PIN_NOSENSE); nrf_gpio_cfg_watcher(PIN_CHARGING); nrf_gpio_pin_set(PIN_CHARGING);//拉高100ms client_ram.delay_pair_count=TIME_GetTicks(); Process_UpdatePeroid(app_pair_client_process,1); Process_SetHoldOn(app_pair_client_process,1); client_ram.statu=2; } } else { if(client_ram.pair_line!=DISCONNECT_CHARGE) { PAIR_CHARGERPIN_PRINT("INTO DISCONNECT_CHARGE %d\n", 1); client_ram.pair_line=DISCONNECT_CHARGE; } } break; case 2: if(TIME_GetTicks() - client_ram.delay_pair_count > 100)//等100ms { nrf_gpio_pin_clear(PIN_CHARGING); client_ram.statu=3; client_ram.delay_pair_count=TIME_GetTicks(); } break; case 3: if(nrf_gpio_pin_read(PIN_CHARGING)==0) { log("into_event-C\n"); client_ram.into_event(); client_ram.delay_pair_count=TIME_GetTicks(); PAIR_CHARGERPIN_PRINT(" into_event-C %d\n", TIME_GetTicks()); PAIR_CHARGERPIN_PRINT(" wait pair %d\n", nrf_gpio_pin_read(PIN_CHARGING)); client_ram.statu=4; } else if(TIME_GetTicks() - client_ram.delay_pair_count > 100) { PAIR_CHARGERPIN_PRINT(" wait low over time %d\n", nrf_gpio_pin_read(PIN_CHARGING)); client_ram.statu=0; Process_UpdatePeroid(app_pair_client_process,ADC_CHECKMS); Process_SetHoldOn(app_pair_client_process,0); i=3; } break; case 4: if (TIME_GetTicks() - client_ram.delay_pair_count > PAIROVERTIME) { client_ram.statu=0; client_ram.over_event(); Process_UpdatePeroid(app_pair_client_process,ADC_CHECKMS); Process_SetHoldOn(app_pair_client_process,0); PAIR_CHARGERPIN_PRINT(" over_event %d\n", TIME_GetTicks()); i=3; } break; //==============================s case 11: if((TIME_GetTicks() - client_ram.delay_pair_count > 5000)||(shefcheck_event_fag)) { if(shefcheck_event_fag) { if(check_callbackt_ram)check_callbackt_ram(shefcheck_buff[0]); } else { PAIR_CHARGERPIN_PRINT(" =====================wait low over time %d\n", nrf_gpio_pin_read(PIN_CHARGING)); } client_ram.statu=0; Process_UpdatePeroid(app_pair_client_process,ADC_CHECKMS); Process_SetHoldOn(app_pair_client_process,0); one_byte_receive_uninit(); i=3; } break; //==============================s } } }