📄 batterypdd.c
字号:
/*******************************************************************************
* Copyright (c) Wolfson Microelectronics plc. All rights reserved.
*
* This software as well as any related documentation may only be used or
* copied in accordance with the terms of the Wolfson Microelectronic plc's
* non-disclosure agreement (NDA). The information in this file is furnished
* for informational use only, is subject to change without notice, and
* should not be construed as a commitment by Wolfson Microelectronics plc.
* Wolfson Microelectronics plc assumes no responsibility or liability for
* any errors or inaccuracies that may appear in this document or any software
* that may be provided in association with this document.
*
* Except as permitted by the NDA, no part of this document may be reproduced,
* stored in a retrieval system, or transmitted in any form or by any means
* without the express written consent of Wolfson Microelectronics plc.
* *//**
* @file batterypdd.c
* @brief Battery Driver PDD layer.
*
* Provides a PDD for the Mainstone II Wolfson Battery Driver.
*
* @version $Id$
*
* @Warning
* This software is specifically written for Wolfson devices. It may not be
* used with other devices.
******************************************************************************/
/*
* Include files
*/
#include <battimpl.h>
#ifdef XSC1
# include <xsc1.h>
#elif BVDMAIN
# include <bvd1.h>
#endif
#include "WMTypes.h"
#include "WMConfig.h"
#include "WMDevice.h"
#include "WMAuxADC.h"
#include "WMPower.h"
#include "WMPlatform.h"
#include "batterypdd.h"
/*
* Global Definitions
*/
/** Would have used defines, but seems to be a bug/problem with putting defines
inside the MAKELONG macro */
static int mainBatteryLevels = 3;
static int backupBatteryLevels = 3;
static WM_DEVICE_HANDLE handle_BatteryDevice;
/*
* Functions
*/
/******************************************************************************
* Function: BatteryPDDDeinitialize *//**
*
* @brief This function allows the battery PDD to perform hardware-specific
* cleanup.
*
* (no parameters)
*****************************************************************************/
void WINAPI BatteryPDDDeinitialize()
{
DEBUGMSG( ZONE_FUNCTION,
( TEXT( "Entering BatteryPDDDeinitialize...\r\n" ) ) );
if ( handle_BatteryDevice )
{
WMAuxADCShutdown( handle_BatteryDevice );
DEBUGMSG( ZONE_PDD,
( TEXT( "Battery device successfully deinitialised.\r\n" ) ) );
}
else {
DEBUGMSG( ZONE_PDD,
( TEXT( "Battery device failed to deinitialise.\r\n" ) ) );
}
handle_BatteryDevice = NULL;
DEBUGMSG( ZONE_FUNCTION,
( TEXT( "Successfully completed BatteryPDDDeinitialize...\r\n" ) ) );
}
/******************************************************************************
* Function: BatteryPDDGetLevels *//**
*
* @brief This function indicates how many battery levels are reported in the
* BatteryFlag and BackupBatteryFlag members of the
* SYSTEM_POWER_STATUS_EX2 structure filled in by BatteryPDDGetStatus.
*
* (no parameters)
*
* @return The low word reports the number of battery levels, from zero to
* three, in the main battery.
* The high word reports the number of battery levels, from zero to
* three, in the backup battery.
*
*****************************************************************************/
long BatteryPDDGetLevels()
{
long BatteryLevels = MAKELONG(mainBatteryLevels, backupBatteryLevels);
DEBUGMSG( ZONE_FUNCTION,
( TEXT( "Entering BatteryPDDGetLevels...\r\n" ) ) );
DEBUGMSG( ZONE_PDD,
( TEXT( "Battery has %d main levels, %d backup levels.\r\n" ),
LOWORD(BatteryLevels), HIWORD(BatteryLevels) ) );
DEBUGMSG( ZONE_FUNCTION,
( TEXT( "Successfully completed BatteryPDDGetLevels...\r\n" ) ) );
return BatteryLevels;
}
/******************************************************************************
* Function: BatteryPDDGetStatus *//**
*
* @brief This function obtains the most current battery and power status
* available on the platform. It fills in the structures pointed to by
* its parameters.
*
* @param pstatus Pointer to a SYSTEM_POWER_STATUS_EX2 structure.
*
* @param pfBatteriesChangedSinceLastCall
* Pointer to a flag that the function sets to TRUE if the user
* replaced or changed the system's batteries since the last call to
* this function.
*
* @return Boolean that indicates success.
*
*****************************************************************************/
BOOL WINAPI BatteryPDDGetStatus( PSYSTEM_POWER_STATUS_EX2 pstatus,
PBOOL pfBatteriesChangedSinceLastCall )
{
static int adcType = WM_BATTERY_ADC_BMON;
static WM_REGVAL voltage = WM_BATTERY_MAXIMUM_VOLTAGE;
static WM_REGVAL previous_voltage;
static WM_REGVAL temperature = 0;
static BOOL expectsample = TRUE;
BOOL gotsample = FALSE;
BOOL OnAC = FALSE;
float voltage_percent = 0;
WMSTATUS status;
DEBUGMSG( ZONE_FUNCTION,
( TEXT( "Entering BatteryPDDGetStatus...\r\n" ) ) );
previous_voltage = voltage;
if ( IS_WM9713_FAMILY( handle_BatteryDevice ) ) {
adcType = WM_BATTERY_ADC_BMON;
}
/** Read the ADC. */
if (adcType == WM_BATTERY_ADC_BMON )
{
status = WMSampleADC( handle_BatteryDevice, adcType, &voltage );
if ( WM_ERROR( status ))
{
if (expectsample)
{
goto error_volt_samplefailed;
}
else
{
expectsample = TRUE;
}
}
else
{
gotsample = TRUE;
}
}
#if WM_BATTERY_MONITOR_TEMPERATURE
else
{
status = WMSampleADC( handle_BatteryDevice, adcType, &temperature );
if ( WM_ERROR( status ))
{
if (expectsample)
{
goto error_temp_samplefailed;
}
else
{
expectsample = TRUE;
}
}
else
{
gotsample = TRUE;
}
}
#endif /* WM_BATTERY_MONITOR_TEMPERATURE */
#if WM_BATTERY_MONITOR_TEMPERATURE
if ( IS_WM9713_FAMILY( handle_BatteryDevice ) )
{
/** Change the ADC to monitor the temperature */
adcType = WM_BATTERY_ADC_TEMPERATURE;
/** Now select the AUX1 ADC. */
status = WMAuxADCSelect( handle_BatteryDevice, adcType );
if ( WM_ERROR( status ) )
{
goto error_switchfailed;
}
status = WMSampleADC( handle_BatteryDevice, adcType, &temperature );
if ( WM_ERROR( status ) )
{
goto error_temp_samplefailed;
}
}
else
{
if (gotsample) {
status = WMAuxADCStop( handle_BatteryDevice, adcType );
if ( WM_ERROR( status ) )
{
goto error_switchfailed;
}
if (adcType == WM_BATTERY_ADC_BMON)
{
adcType = WM_BATTERY_ADC_TEMPERATURE;
}
else
{
adcType = WM_BATTERY_ADC_BMON;
}
status = WMAuxADCSelect ( handle_BatteryDevice, adcType );
if ( WM_ERROR( status ) )
{
if (adcType == WM_BATTERY_ADC_BMON)
{
adcType = WM_BATTERY_ADC_TEMPERATURE;
}
else
{
adcType = WM_BATTERY_ADC_BMON;
}
WMAuxADCStart (handle_BatteryDevice, adcType);
goto error_switchfailed;
}
/** If we have selected a new AuxADC, it takes a not insignificant
* period of time till the next sample is expected. As we expect
* to get back no data on the next occasion, we simply don't worry
* about any no data errors.
*/
expectsample = FALSE;
status = WMAuxADCStart ( handle_BatteryDevice, adcType );
if ( WM_ERROR( status ) )
{
if (adcType == WM_BATTERY_ADC_BMON)
{
adcType = WM_BATTERY_ADC_TEMPERATURE;
}
else
{
adcType = WM_BATTERY_ADC_BMON;
}
WMAuxADCSelect (handle_BatteryDevice, adcType);
WMAuxADCStart ( handle_BatteryDevice, adcType );
goto error_switchfailed;
}
}
}
#endif /* WM_BATTERY_MONITOR_TEMPERATURE */
#if 0
/** Whether the AC is on or off can be calculated using a hackish method of
simply comparing the last two samples, and if the first is greater than
the second, assume we are on battery power. */
if ( ( OnAC ) && ( voltage > previous_voltage ) )
/** AC ON, Battery OFF */
{
OnAC = TRUE;
}
else /** !OnAC | ( voltage =< previous_voltage ) */
/** AC OFF, Battery ON */
{
OnAC = FALSE;
}
#endif
DEBUGMSG( ZONE_BATTERY,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -