123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #include "app_chargerpin_conn_detect.h"
- #include "usr_config.h"
- #include "bsp_time.h"
- #include "system.h"
- #include "hal_led.h"
- #include "nrf_gpio.h"
- #include "hal_ble_client.h"
- #include "hal_ble_host.h"
- #include "app_flash.h"
- #include "hal_battery.h"
- #include "ble_comm.h"
- #include "app_pair_chargerpin.h"
- #include "hal_charge.h"
- #include "fml_adc.h"
- /************************ 函数声明 ***********************************/
- /************************ 变量 ***********************************/
- static uint8_t hal_charge_state = BLE_CHARGE_PULLOUT;
- static uint8_t pair_state =0;
- static void pair_start_cb(){
- hal_charge_state = BLE_CHARGE_PULLOUT;
- pair_state =1;
- }
- static void pair_done_cb(){
- pair_state =0;
- }
- PAIR_START_REGISTER(pair_start_cb);
- PAIR_DONE_REGISTER(pair_done_cb);
- /********************************************************/
- uint8_t hal_charge_Getstate(void)
- {
- return hal_charge_state;
- }
- static void hal_send_charge(void)
- {
- uint8_t sbuf[2]={0};
- uint8_t L = 0;
- if(mFlash.isHost) sbuf[L++] = 0;
- else sbuf[L++] = 1;
- sbuf[L++] = hal_charge_state;
- BLE_Client_Tx_Send(0,BLE_CHARGE,sbuf,L);
- }
- void cb_BLE_Client_R_CHARGE(void* handle)
- {
- BLE_Client_Rx_t* target = handle;
- DEBUG_LOG("!!!!!!!!cb_BLE_Client_R_CHARGE\n");
- hal_send_charge();
- BLE_Host_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
- }
- void cb_BLE_Host_R_CHARGE(void* handle)
- {
- BLE_Host_Rx_t* target = handle;
- BLE_Client_Tx_Send(0,BLE_CHARGE,target->pDat,target->datLen);
- // DEBUG_LOG("cb_BLE_Host_R_CHARGE:%d,%d,%d\n",target->pDat[0],target->pDat[1],target->datLen);
- }
- static void hal_charge_Process(void)
- {
- pair_line_t CHARGE_state = app_chargepin_pairline();
-
- if(1 == pair_state)return;//配对不检测充电
-
- if(CHARGE != CHARGE_state){ //没充电
- if(hal_charge_state!=BLE_CHARGE_PULLOUT){ DEBUG_LOG("charge out...\n");
- hal_charge_state = BLE_CHARGE_PULLOUT;
- Process_SetHoldOn(hal_charge_Process,0);
- hal_send_charge();
- }
- return;
- }
-
- if(GetBatteryPersent() >=100){
- if(hal_charge_state!=BLE_CHARGE_DONE){ DEBUG_LOG("charge done:...\n");
- hal_charge_state = BLE_CHARGE_DONE;
- Process_SetHoldOn(hal_charge_Process,1);
- hal_send_charge();
- }
- }else{ //正在充电
- if(hal_charge_state!=BLE_CHARGE_INSERT){ DEBUG_LOG("charge in...\n");
- hal_charge_state = BLE_CHARGE_INSERT;
- Process_SetHoldOn(hal_charge_Process,1);
- hal_send_charge();
- }
- }
- }
- static void hal_charge_monitor_process(void)
- {
- #define BATTERY_VOL_THRESHOLD_MIN 2500 //2.5V
- #define BATTERY_VOL_THRESHOLD_MAX 4000 //4V
- #define CHARGE_VOL_THRESHOLD 80 //充电前和充电期间的充电电压变化值,单位mv
-
- //监测电池和充电数据
- int16_t charge_vol = 0;
- // int16_t charge_threshold = 0;
- static uint32_t charge_cycle = 50;
- static int16_t before_charge_vol = 0; //充电前的电压值
- static int16_t charge_vol_max = 0; //充电期间最大的电压值
- int16_t adcval =0;
- if(BLE_CHARGE_PULLOUT == hal_charge_state)//没充电
- {
- if(-1 ==fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
- charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
- before_charge_vol = before_charge_vol > charge_vol ? charge_vol : before_charge_vol;
- charge_vol_max = 0;
- charge_cycle = 50;
- }else //充电
- {
- /* 过筛50轮 */
- if(charge_cycle != 0){
- if(-1 ==fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
- charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
- charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
- charge_cycle--;
- return;
- }
- if(-1 == fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adcval))return;
- charge_vol = ADC_RESULT_IN_MILLI_VOLTS(adcval);
- charge_vol_max = charge_vol_max < charge_vol ? charge_vol : charge_vol_max;
- // bat_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_BAT_CHANNEL))*5/3;
-
- //当电池电量没满,充电(经测试,电量没满的充电电压跟电池电压有关,最小充电电压100+mv)
- // charge_threshold = charge_vol_max - before_charge_vol; //充电前和充电期间的充电电压变化值
- }
- }
- void hal_charge_init(void)
- {
- BLE_Client_Rx_Regist(BLE_CHARGE,cb_BLE_Client_R_CHARGE);
- BLE_Host_Rx_Regist(BLE_CHARGE,cb_BLE_Host_R_CHARGE);
- Process_Start(1000,"charge",hal_charge_Process);
- hal_charge_Process();
-
- Process_Start(10,"charge_monitor",hal_charge_monitor_process);
- }
|