123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- #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"
- //=================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 700
- #define CON_HOST_MAX_ADC DISCON_HOST_MIN_ADC
- #define CON_HOST_MIN_ADC 300
- static void null_pcs(void)
- {
- }
- #define UPPULL_MS 5
- 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);
- }
- }
- }
- 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 );
- }
- // 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());
- 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)||(nrf_gpio_pin_read(PIN_CHARGING)))
- {
- PAIR_CHARGERPIN_PRINT(" into_event %d \n", TIME_GetTicks());
- host_ram.statu=4;
- nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL);
- host_ram.delay_pair_count=TIME_GetTicks();
- host_ram.into_event();
- Process_UpdatePeroid(app_pair_host_process,ADC_CHECKMS);
- Process_SetHoldOn(app_pair_host_process,0);
- }
- break;
- case 4:
- 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();
- i=3;
- }
- break;
- }
- }
- }
- //============================================================================================================
- 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());
- 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;
-
- 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;
- 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);
-
- client_ram.into_event();
- PAIR_CHARGERPIN_PRINT(" into_event %d\n", TIME_GetTicks());
- PAIR_CHARGERPIN_PRINT(" wait pair %d\n", nrf_gpio_pin_read(PIN_CHARGING));
- }
- }
- 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)//等
- {
- nrf_gpio_pin_clear(PIN_CHARGING);
- client_ram.statu=3;
- Process_UpdatePeroid(app_pair_client_process,ADC_CHECKMS);
- Process_SetHoldOn(app_pair_client_process,0);
- }
- break;
- case 3:
- if (TIME_GetTicks() - client_ram.delay_pair_count > PAIROVERTIME)
- {
- client_ram.statu=0;
- client_ram.over_event();
- PAIR_CHARGERPIN_PRINT(" over_event %d\n", TIME_GetTicks());
- i=3;
- }
- break;
- }
- }
- }
|