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

📄 headset_battery.c

📁 bluelab的一个很好的例程
💻 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_private.h"
#include "headset_battery.h"

#include <pio.h>

#define BATTERY_LOW_REMINDER_MESSAGE 0xBA11


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

/* Forward declaration of battery measurement task handler function */
void aio_handler(Task task, MessageId id, Message message);

/* Local static functions */
static void batteryNormal(Task pTask );
static void batteryLow(Task pTask, power_type* power);
static void batteryShutdown(Task pTask);
static void handleBatteryVoltageReading(power_type* power, uint32 reading , Task pTask);
static void handleBatteryTemperatureReading(power_type* power, uint32 reading);

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

    BT_DEBUG(("PM Normal\n"));

	MessageSend(getAppTask(), EventOkBattery, 0);
    
    MessageCancelAll ( pTask , BATTERY_LOW_REMINDER_MESSAGE) ;

}


/****************************************************************************
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)
{
	BT_DEBUG(("PM Low\n"));
	
	MessageSend(getAppTask(), EventLowBattery, 0);
        
    /* Ensure there is only ever one sequence in the system*/
    MessageCancelAll ( pTask , BATTERY_LOW_REMINDER_MESSAGE) ;
        
	/* Send the next Reminder Message*/    
    MessageSendLater (pTask , BATTERY_LOW_REMINDER_MESSAGE , 0, D_SEC(power->config.battery.low_batt_remind_time_secs) ) ;
	MessageSendLater (pTask , BATTERY_LOW_REMINDER_MESSAGE , 0, D_SEC(power->config.battery.low_batt_remind_time_secs * 2) ) ;
}


/****************************************************************************
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"));
    
	MessageSend(getAppTask(), EventPowerOff, 0);
    
    MessageCancelAll ( pTask , BATTERY_LOW_REMINDER_MESSAGE) ;
}


/****************************************************************************
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;
	
	/* Check current voltage level against configured thresholds */
	if(vb > power->config.battery.high_threshold)
		batteryNormal(pTask);
	else if(vb < power->config.battery.shutdown_threshold)
		batteryShutdown(pTask);
	else if(vb < power->config.battery.low_threshold)
		batteryLow(pTask, power);
}


/****************************************************************************
NAME    
    handleBatteryTemperatureReading
    
DESCRIPTION
  	Calculate the current battery temperature
    
RETURNS
    void
*/
static void handleBatteryTemperatureReading(power_type* power, uint32 reading)
{
	/*
	   Calculate the current battery temperature in 'C 
	   tb ('C) = (th_m * (AIO1/1000)) + th_c + Td
	 */			
	int16 tb = ((((int32)power->config.charger.th_m * (int32)reading) + ((int32)power->config.charger.th_c * 1000)) / (int32)1000) + (int32)power->config.charger.td;
	
	BT_DEBUG(("TBAT: %d'C\n", tb));
	
	/* Store current temperature reading */
	power->vth_task.current_reading = tb;
}

/****************************************************************************
NAME    
    batteryGetCurrentVoltage
    
DESCRIPTION
  	Call this function to get the current battery voltage
    
RETURNS
    void
*/
uint16 batteryGetCurrentVoltage(power_type* power)
{
	return power->vbat_task.current_reading;
}


/****************************************************************************
NAME    
    batteryGetCurrentTemperature
    
DESCRIPTION
  	Call this function to get the current battery temperature
    
RETURNS
    void
*/
int16 batteryGetCurrentTemperature(power_type* power)
{
	return power->vth_task.current_reading;
}


/****************************************************************************
NAME    
    aio_handler
    
DESCRIPTION
  	AIO readings arrive here for processing
    
RETURNS
    void
*/
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 AIO0:
					/* Battery Voltage */
					handleBatteryVoltageReading(power, reading , task);
					break;
			
				case AIO1:
					/* Battery temperature */
					handleBatteryTemperatureReading(power, reading);
				break;
				

				case VDD:
				case AIO2:
				case AIO3:
				default:
					break;
			}	
			break;
        case BATTERY_LOW_REMINDER_MESSAGE:
            {
                BT_DEBUG(("Bat: Low Batt Rem[%d]\n", power->config.battery.low_batt_remind_time_secs)) ;
                /*send a low battery indication to the main task*/
                /*batteryLow(task, power) ;*/
				MessageSend(getAppTask(), EventLowBattery, 0);
				
            }
        break ;
        
		default:
			break;
	}
}


/****************************************************************************
NAME    
    batteryInit
    
DESCRIPTION
  	This function will initialise the battery sub-system.  The sub-system 
	manages the reading and calulation of the battery voltage and temperature
    
RETURNS
    void
*/
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;
	   
	/* --- Battery Voltage --- */
	/* The battery voltage is monitored at all times.  Initialise the battery
	   library to read the battery voltage via AIO_0 */
	power->vbat_task.task.handler = aio_handler;
	power->vbat_task.source = AIO0;
	BatteryInit(&power->vbat_task.state, &power->vbat_task.task, power->vbat_task.source, D_SEC(power->config.battery.monitoring_period));	
		
	/* --- Battery Temperature --- */
	/* The battery temperature is required for NiMH battery charging control */	  
	if(power->config.chemistry == nimh)
	{
		/* Enable the thermister hardware */
		PioSetDir(1 << power->config.charger.th_enable_pio, 1 << power->config.charger.th_enable_pio);
		PioSet(1 << power->config.charger.th_enable_pio, ~0);
		
		/* Initialise the battery library to read the thermister voltage via AIO_1 */
		power->vth_task.task.handler = aio_handler;
		power->vth_task.source = AIO1;
		BatteryInit(&power->vth_task.state, &power->vth_task.task, power->vth_task.source, D_SEC(power->config.battery.monitoring_period));	
	}
}

⌨️ 快捷键说明

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