⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 headset_battery.c

📁 bc5_stereo:bluetooth stereo Headset CODE 支持A2DP HSP 和 HSP 。可作为车载免提。BlueLab 2007环境下编译
💻 C
字号:
/****************************************************************************
Copyright (C) Cambridge Silicon Radio Ltd. 2005

FILE NAME
    headset_battery.c

DESCRIPTION
    This file contains the battery specific functionality.  This includes
	battery voltage and temperature monitoring

NOTES
	Certain assumptions have been made on the assignment of the analog input
	signals:
	
	AIO_0 is connected to VBAT via a potential divider
	
	VBAT = AIO_0 *   Ra
				   -------
				   Ra + Rb
				   
	Where the divisor ratio of Ra and Rb is configured from persistent store
				   
	AIO_1 is connected to a thermister used to measure the battery temperature
	fed from an PIO output through a 10k resistor
*/


/****************************************************************************
    Header files
*/

#include "headset_battery.h"
#include "headset_debug.h"
#include "headset_private.h"
#include "headset_statemanager.h"

/*#include <pio.h>*/
#include <battery.h>




#ifdef DEBUG_BAT
#define BT_DEBUG(x) DEBUG(x)
#else
#define BT_DEBUG(x) 
#endif   


/* Local static functions */
static void batteryNormal(Task pTask , power_type * power  );
static void batteryLow(Task pTask, power_type* power);
static void batteryShutdown(Task pTask);
static void aio_handler(Task task, MessageId id, Message message);
static void handleBatteryVoltageReading(power_type* power, uint32 reading , Task pTask);


/****************************************************************************
  FUNCTIONS
*/

/****************************************************************************
NAME    
    batteryNormal
    
DESCRIPTION
  	Called when the battery voltage is detected to be in a Normal state
    
RETURNS
    void
*/
static void batteryNormal(Task pTask, power_type * power )
{
    BT_DEBUG(("PM Normal\n"));

    if (power->charger.state != disconnected) 
    	MessageSend(getAppTask(), EventOkBattery, 0);


}


/****************************************************************************
NAME    
    batteryLow
    
DESCRIPTION
  	Called when the battery voltage is detected to be in a Low state
    
RETURNS
    void
*/
static void batteryLow(Task pTask , power_type* power)
{
   
        /*we only want low battery reminders if the headset is ON and not charging*/
    if ( (power->charger.state == disconnected) && (stateManagerGetHfpState() != headsetPoweringOn) )
	{    
    	MessageSend(getAppTask(), EventLowBattery, 0); 

    }
}


/****************************************************************************
NAME    
    batteryShutdown
    
DESCRIPTION
  	Called when the battery voltage is detected to be in a Shutdown state
    
RETURNS
    void
*/
static void batteryShutdown(Task pTask)
{

	BT_DEBUG(("PM Shutdown\n"));
        /*we only want low battery reminders if the headset is ON and not charging*/
    if ( stateManagerGetHfpState() != headsetPoweringOn )
	{    
    
    	MessageSend(getAppTask(), EventLowBattery, 0); 
		MessageSend(getAppTask(), EventPowerOff, 0);
    	
	}	
}


/****************************************************************************
NAME    
    handleBatteryVoltageReading
    
DESCRIPTION
  	Calculate current battery voltage and check to determine if the level
	has fallen below either the low or critical thresholds.  If the voltage
	has fallen below the low threshold generate a low battery system event.
	If the level has fallen below the critical threshold level then initiate
	a headset power down sequence.
    
RETURNS
    void
*/
static void handleBatteryVoltageReading(power_type* power, uint32 reading , Task pTask)
{	
	/* Calculate the current battery voltage in mV */
	uint32 vb = ((reading * 1000) / power->config.battery.divisor_ratio);
	
	BT_DEBUG(("VBAT: %lumV\n", vb));
	
	/* Store current battery reading */
	power->vbat_task.current_reading = (int16)vb;
	
	BT_DEBUG(("BAT [%d][%d][%d]\n", 	
	    (uint16)(power->config.battery.high_threshold  * 20) ,
        (uint16)(power->config.battery.low_threshold      * 20) ,
    	(uint16)(power->config.battery.shutdown_threshold * 20) 
	)) ;
	
	
	/* Check current voltage level against configured thresholds */
	
	if (vb > (uint16)(power->config.battery.high_threshold  * 20) )
		batteryNormal( pTask, power );	
	else if(vb < (uint16)(power->config.battery.shutdown_threshold * 20) )
		batteryShutdown( pTask );
	else if(vb < (uint16)(power->config.battery.low_threshold      * 20) )
		batteryLow(pTask, power);
}


/****************************************************************************
NAME    
    aio_handler
    
DESCRIPTION
  	AIO readings arrive here for processing
    
RETURNS
    void
*/
static void aio_handler(Task task, MessageId id, Message message)
{
	uint32	reading;
	
	/* Get Power configuration data */
	power_type* power = ((hsTaskData *)getAppTask())->power;
	
	/* This function receives messages from the battery library */
	aioTask* this_task = (aioTask*)task;
	battery_reading_source source = this_task->source;
	
	switch(id)
	{
		case BATTERY_READING_MESSAGE :		
			/* New reading, extract reading in mV and handle accordingly */
			reading = (*((uint32*)message));
			
			/* Readings can either be AIO0 (Battery Voltage) or AIO1 (Battery Temperature) */
			switch(source)
			{
                case BATTERY_INTERNAL:
					/* Battery Voltage */
					handleBatteryVoltageReading(power, reading , task);
					break;
			
                case AIO0:
				case AIO1:			
				case VDD:
				case AIO2:
				case AIO3:
				default:
					break;
			}	
            /* If initial reading, revert back to default battery reading period */
            if (power->initial_reading)
            {
				DEBUG(("BATT : Initial reading\n")) ;
                power->initial_reading = FALSE;
            	BatteryInit(&power->vbat_task.state, &power->vbat_task.task, power->vbat_task.source, D_SEC(power->config.battery.monitoring_period));	           
            }
			break;

        
		default:
			break;
	}
}


/*****************************************************************************/
void batteryInit(power_type* power)
{
	/* Initialise the default battery readings */
	/*power->vbat_task.current_reading = power->config.charger.min_voltage;*/
	/*power->vth_task.current_reading = power->config.charger.min_temp;*/
    power->initial_reading = TRUE;
	   
	/* --- Battery Voltage --- */
	/* The battery voltage is monitored at all times.  Initialise the battery
	   library to read the battery voltage via BATTERY_INTERNAL */
	power->vbat_task.task.handler = aio_handler;
	power->vbat_task.source = BATTERY_INTERNAL;
		
    /* Read battery now */
    batteryRead(power);
}


/*****************************************************************************/
void batteryRead(power_type* power)
{
	power->initial_reading = TRUE;
	BatteryInit(&power->vbat_task.state, &power->vbat_task.task, power->vbat_task.source, 0);	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -