123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- #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_timer<UPPULL_MS)return;
- if(TIME_GetTicks()-host_ram.pulluppcs_timer==UPPULL_MS)
- {
- uint32_t errcode = ADC_ReadChargePin(PIN_CHARGING_CHANNEL);
- if (ADC_OP_SUCCESS != errcode)
- {
- char buff[20];
- memset(buff,0,20);
- sprintf(buff,"ADC ERROR %d",errcode);
- Except_TxError(EXCEPT_Pair,buff);
- PAIR_CHARGERPIN_PRINT("ADC_ReadChargePin ======%d================================================ERROR %d\n",TIME_GetTicks(), errcode);
- }
- }
- if(TIME_GetTicks()-host_ram.pulluppcs_timer>UPPULL_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
- }
- }
- }
|