📄 headset_charger.c
字号:
/****************************************************************************Copyright (C) Cambridge Silicon Radio Ltd. 2005-2006Part of BlueLab 3.6.2-releaseFILE NAME headset_charger.cDESCRIPTION This file contains the battery charging functionality for NiMH batteries. NOTES *//**************************************************************************** Header files*/#include "headset_private.h"#include "headset_charger.h"#include "headset_battery.h"#include <pio.h>#include <vm.h>#ifdef CHARGER_DEBUG#define CH_DEBUG(x) DEBUG(x)#else#define CH_DEBUG(x) #endif #define SET_CHARGER_STATE(s) (power->charger.state = s);#define VBAT_OFFSET (100)#define TBAT_OFFSET (2)#define VBAT_NOMINAL (2400)#define VBAT_ABNORMAL (3700)/* Private messages for controlling battery charging process */#define CHARGER_MESSAGE_BASE (0)enum{ CHARGER_UPDATE = CHARGER_MESSAGE_BASE};/* Forward declaration of charger task handler */void charger_handler(Task task, MessageId id, Message message);/* Local function prototypes */static uint16 getVbatThreshold(power_type* power);static void resetFastCharge(power_type* power);static void startFastCharge(power_type* power);static void endFastCharge(power_type* power);static bool monitorCharge(power_type* power);static void trickleCharge(power_type* power);static void chargerError(power_type* power);static bool dvDetect(power_type* power);static bool dtDetect(power_type* power);static bool fastChargeTimeExpired(power_type* power);static void updateBatteryTemperature(power_type* power);/****************************************************************************NAME getVbatThreshold DESCRIPTION This function is called to get the threshold used to determine if -dV detection should be enabled RETURNS void*/static uint16 getVbatThreshold(power_type* power){ uint16 vthres = VBAT_NOMINAL; /* Note vdet_m is actually vdet_m * 10000 vdet_c is actually vdet_c * 1000 vthres is in mV (*1000) */ if(power->config.charger.vdet_m != 0 && power->config.charger.vdet_c !=0) { vthres = (((int16)power->config.charger.vdet_m * (int16)batteryGetCurrentTemperature(power))/(int16)100) + ((int16)power->config.charger.vdet_c); } CH_DEBUG(("VDET: %dmv\n", vthres)); return vthres;}/****************************************************************************NAME setChargeEnable DESCRIPTION This function is called to set/reset the charge enable pio line RETURNS void*/static void setChargeEnable(bool state){ hsTaskData * theHeadset = (hsTaskData *) getAppTask(); if(theHeadset->PIO.ChargerEnablePIOEnabled) { PioSetDir(1 << theHeadset->PIO.ChargerEnablePIO, 1 << theHeadset->PIO.ChargerEnablePIO); if(state) PioSet(1 << theHeadset->PIO.ChargerEnablePIO, ~0); else PioSet(1 << theHeadset->PIO.ChargerEnablePIO, 0); }}/****************************************************************************NAME resetFastCharge DESCRIPTION This function is called to reset the fast charge algorithm RETURNS void*/static void resetFastCharge(power_type* power){ CH_DEBUG(("CH: FC Reset\n")); /* Reset -dV peak detection */ power->charger.dv_peak = 0; power->charger.prev_dv_peak = 0; /* Reset dT level */ power->charger.dt_level = batteryGetCurrentTemperature(power); power->charger.prev_dt_level = power->charger.dt_level; /* Enable fast charge timer */ power->charger.fast_charge_inhibit = FALSE; /* Reset fast charge timer */ power->charger.fast_charge_timer = 0;}/****************************************************************************NAME startFastCharge DESCRIPTION This function is called to start the fast charge algorithm RETURNS void*/static void startFastCharge(power_type* power){ CH_DEBUG(("CH: FC Start\n")); /* Fast charge state */ SET_CHARGER_STATE(fast_charge); /* Enable fast charge enable PIO line */ setChargeEnable(TRUE); /* Send fast charge event */ MessageSend(getAppTask(), EventFastCharge, 0); /* Start fast charge */ power->charger.fast_charge_inhibit = FALSE;}/****************************************************************************NAME endFastCharge DESCRIPTION This function is called to end the fast charge algorithm RETURNS void*/static void endFastCharge(power_type* power){ CH_DEBUG(("CH: FC End\n")); /* end fast charging */ power->charger.fast_charge_inhibit = TRUE; /* Disable fast charge enable PIO line */ setChargeEnable(FALSE); /* Fast cycle has completed, move into trickle charge state */ trickleCharge(power);}/****************************************************************************NAME dvDetect DESCRIPTION This function monitors the voltage of the battery during charging and attempts to detect the -dV point. This indicates that the battery is fully charged and the fast charge cycle should be terminated. Fast charging beyond this point may result in permanent damage to the battery. RETURNS void*/static bool dvDetect(power_type* power){ bool detected = FALSE; /* Get the current battery voltage level */ int16 vb = batteryGetCurrentVoltage(power); /* Get the -dV threshold */ int16 dv = power->config.charger.dv_voltage; /* Check for -dV termination condition */ if(vb >= power->charger.dv_peak) { /* Record peak */ power->charger.dv_peak = vb; } else { /* Look for -dV threshold */ if((power->charger.dv_peak - vb) > dv) detected = TRUE; } CH_DEBUG(("CH: vb=%d dv=%d peak=%d\n",vb, dv, power->charger.dv_peak)); return detected;}/****************************************************************************NAME dtDetect DESCRIPTION This function monitors the temperature of the battery. If the temperature rise of the battery exceeds the a defined level (dt) then fast charge will terminate. Charging the battery outside of defined limits can cause damage to the battery. RETURNS void*/static bool dtDetect(power_type* power){ bool detected = FALSE; /* Get the current battery temperature level */ int16 tb = batteryGetCurrentTemperature(power); int16 vb = batteryGetCurrentVoltage(power); /* Get the dT threshold */ int16 dt = power->config.charger.dt; CH_DEBUG(("CH: dT=%d\n",tb - power->charger.dt_level)); CH_DEBUG(("CH: dT=%d\n",tb - power->charger.prev_dt_level)); /* If the battery temperature has increased by greater than dt then terminate fast charge */ if( ((tb - power->charger.dt_level) > dt) || ((tb - power->charger.prev_dt_level) > dt) ) { if((power->charger.dv_peak - vb) > 0) detected = TRUE; } /* Record current battery temperature for next update */ power->charger.prev_dt_level = power->charger.dt_level; power->charger.dt_level = tb; return detected;}/****************************************************************************NAME fastChargeTimerExpired DESCRIPTION This function monitors the amount of time the current fast charge cycle has been active. Note that the timer counts in charger unpdate units (power->config.charger.update_period) RETURNS void*/static bool fastChargeTimeExpired(power_type* power){ CH_DEBUG(("CH: T=%d\n",power->charger.fast_charge_timer)); if(power->charger.fast_charge_timer > power->config.charger.time) return TRUE; else return FALSE;}/****************************************************************************NAME updateBatterytemperature DESCRIPTION This function records the current temperature of the battery RETURNS void*/static void updateBatteryTemperature(power_type* power){ /* Get the current battery temperature level */ int16 tb = batteryGetCurrentTemperature(power); /* Record current battery temperature */ power->charger.dt_level = tb; power->charger.prev_dt_level = tb;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -