Index: firmware/App/Drivers/Battery.c =================================================================== diff -u -r84bb8663dae71170e1743a2bed37b0aa47465b47 -r72350346b44261f2a9bbd23220ea11978f635039 --- firmware/App/Drivers/Battery.c (.../Battery.c) (revision 84bb8663dae71170e1743a2bed37b0aa47465b47) +++ firmware/App/Drivers/Battery.c (.../Battery.c) (revision 72350346b44261f2a9bbd23220ea11978f635039) @@ -34,29 +34,33 @@ #define BATTERY_CHARGER_STATUS_AC_PRESENT_MASK 0x8000 ///< Battery charger status AC present bit mask. #define BATTERY_PACK_REL_STATE_OF_CHARGE_CMD 0x0D ///< Command to get battery pack relative state of charge. -#define BATTERY_PACK_REMAINING_CAPACITY_CMD 0x0F ///< Command to get battery pack remaining capacity. +#define BATTERY_PACK_STATUS_CMD 0x16 ///< Command to get battery pack status. #define BATTERY_COMM_TIME_OUT_MS 1 ///< Battery communication time out in ms. #define BATTERY_MONITOR_INTERVAL_MS 750 ///< Battery monitor interval in ms. #define AC_POWER_LOST_PERSISTENT_COUNT 3 ///< AC power lost persistent count before alarming. +#define BATTERY_COMM_FAULT_PERSISTENTCE_COUNT 5 ///< Battery communication fault persistent count before alarming. // ********** private data ********** static U16 batteryStatus = 0; ///< Battery current status. static U16 batteryRelStateOfCharge_pct = 0; ///< Battery pack relative state of charge. +static U16 batteryPackStatus = 0; ///< Battery pack current status. static U32 lastBatteryMonitorTime = 0; ///< Previous battery monitor time. static U32 lostACPowerPersistentCount = 0; ///< Persistent count for AC power lost alarm. static BOOL hasBatteryChargerStatus = FALSE; ///< Flag indicates if battery charger status has been obtained. +static U32 commFaultPersistentCount = 0; ///< Persistence count for battery comm fault. // ********** private function prototypes ********** static void setupI2CDriver( void ); -static BOOL startCommTx( U32 slaveAddr ); static BOOL waitForTxReady( void ); static BOOL waitForRxReady( void ); static BOOL waitForAccessReady( void ); static void generateStopCondition( void ); -static BOOL getBatteryData( U08 command, U16 * dataPtr ); +static BOOL getBatteryData( U32 slaveAddr, U08 command, U16 * dataPtr ); +static BOOL startCommTx( U32 slaveAddr ); +static BOOL getData( U08 command, U16 * dataPtr ); /*********************************************************************//** * @brief @@ -67,6 +71,14 @@ *************************************************************************/ void initBattery( void ) { + batteryStatus = 0; + batteryRelStateOfCharge_pct = 0; + batteryPackStatus = 0; + lastBatteryMonitorTime = 0; + lostACPowerPersistentCount = 0; + hasBatteryChargerStatus = FALSE; + commFaultPersistentCount = 0; + setupI2CDriver(); } @@ -87,34 +99,35 @@ { hasBatteryChargerStatus = TRUE; - if ( TRUE == startCommTx( BATTERY_CHARGER_SLAVE_ADDRESS ) ) + if ( TRUE == getBatteryData( BATTERY_CHARGER_SLAVE_ADDRESS, BATTERY_CHARGER_STATUS_CMD, &batteryStatus ) ) { - if ( TRUE == getBatteryData( BATTERY_CHARGER_STATUS_CMD, &batteryStatus ) ) + if ( ( batteryStatus & BATTERY_CHARGER_STATUS_AC_PRESENT_MASK ) == 0 ) { - if ( ( batteryStatus & BATTERY_CHARGER_STATUS_AC_PRESENT_MASK ) == 0 ) + if ( ++lostACPowerPersistentCount > AC_POWER_LOST_PERSISTENT_COUNT ) { - if ( ++lostACPowerPersistentCount > AC_POWER_LOST_PERSISTENT_COUNT ) - { - #ifndef DISABLE_BATT_COMM - SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_AC_POWER_LOST, batteryStatus ); + SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_AC_POWER_LOST, batteryStatus ); #endif - } } - else - { - lostACPowerPersistentCount = 0; - } } + else + { + lostACPowerPersistentCount = 0; + } } } else { hasBatteryChargerStatus = FALSE; - if ( TRUE == startCommTx( BATTERY_PACK_SLAVE_ADDRESS ) ) + getBatteryData( BATTERY_PACK_SLAVE_ADDRESS, BATTERY_PACK_REL_STATE_OF_CHARGE_CMD, &batteryRelStateOfCharge_pct ); + + if ( TRUE == getBatteryData( BATTERY_PACK_SLAVE_ADDRESS, BATTERY_PACK_STATUS_CMD, &batteryPackStatus ) ) { - getBatteryData( BATTERY_PACK_REL_STATE_OF_CHARGE_CMD, &batteryRelStateOfCharge_pct ); + if ( 0 != batteryPackStatus ) + { + SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_AC_POWER_LOST, batteryPackStatus ); + } } } } @@ -150,52 +163,6 @@ /*********************************************************************//** * @brief - * The startComm function starts i2c communication and verifies slave devices ack. - * @details Inputs: none - * @details Outputs: starts i2c comm in master transmit mode - * @param slaveAddr slave device address - * @return none - *************************************************************************/ -static BOOL startCommTx( U32 slaveAddr ) -{ - BOOL result = FALSE; - - i2cSetSlaveAdd( i2cREG1, slaveAddr ); - - if ( FALSE == i2cIsBusBusy( i2cREG1 ) ) - { - i2cSetDirection( i2cREG1, I2C_TRANSMITTER ); - i2cSetMode( i2cREG1, I2C_MASTER ); - i2cSetStart( i2cREG1 ); - - if ( TRUE == waitForAccessReady() ) - { - if ( 0 == ( i2cREG1->STR & ( (U32)I2C_NACK | (U32)I2C_AL ) ) ) - { - result = TRUE; - } - else - { - generateStopCondition(); -#ifndef DISABLE_BATT_COMM - SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_BATTERY_COMM_FAULT, slaveAddr ); -#endif - } - } - - } - - if ( FALSE == result ) - { - // Reset i2c bus if cannot communicate with battery slave devices - setupI2CDriver(); - } - - return result; -} - -/*********************************************************************//** - * @brief * The waitForTxReady function checks for transmit ready status from i2c * driver with a timeout. * @details Inputs: none @@ -278,17 +245,93 @@ /*********************************************************************//** * @brief - * The getBatteryStatus function send command to smart battery charger controller - * interface to get battery status. + * The getBatteryData function starts i2c communication with battery device + * and get data based on given command. * @details Inputs: none + * @details Outputs: get data from battery device + * @param slaveAddr battery slave device address + * @param command command to send to the slave device + * @param dataPtr data pointer to store command response data + * @return none + *************************************************************************/ +static BOOL getBatteryData( U32 slaveAddr, U08 command, U16 * dataPtr ) +{ + BOOL result = FALSE; + + if ( TRUE == startCommTx( slaveAddr ) ) + { + if ( TRUE == getData( command, dataPtr ) ) + { + result = TRUE; + } + } + + return result; +} + +/*********************************************************************//** + * @brief + * The startComm function starts i2c communication and verifies slave devices ack. + * @details Inputs: none + * @details Outputs: starts i2c comm in master transmit mode + * @param slaveAddr slave device address + * @return none + *************************************************************************/ +static BOOL startCommTx( U32 slaveAddr ) +{ + BOOL result = FALSE; + + i2cSetSlaveAdd( i2cREG1, slaveAddr ); + + if ( FALSE == i2cIsBusBusy( i2cREG1 ) ) + { + i2cSetDirection( i2cREG1, I2C_TRANSMITTER ); + i2cSetMode( i2cREG1, I2C_MASTER ); + i2cSetStart( i2cREG1 ); + + if ( TRUE == waitForAccessReady() ) + { + if ( 0 != ( i2cREG1->STR & ( (U32)I2C_NACK | (U32)I2C_AL ) ) ) + { + commFaultPersistentCount = 0; + result = TRUE; + } + else + { + generateStopCondition(); + if ( commFaultPersistentCount++ > BATTERY_COMM_FAULT_PERSISTENTCE_COUNT ) + { +#ifndef DISABLE_BATT_COMM + SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_BATTERY_COMM_FAULT, slaveAddr ); +#endif + } + } + } + + } + + if ( FALSE == result ) + { + // Reset i2c bus if cannot communicate with battery slave devices + setupI2CDriver(); + } + + return result; +} + +/*********************************************************************//** + * @brief + * The getData function send command to battery interface to get data. + * @details Inputs: none * @details Outputs: get the battery data based on given command * @param command command to send to the slave device * @param dataPtr data pointer to store command response data - * @return TRUE if received battery status, otherwise FALSE + * @return TRUE if received battery data, otherwise FALSE *************************************************************************/ -static BOOL getBatteryData( U08 command, U16 * dataPtr ) +static BOOL getData( U08 command, U16 * dataPtr ) { BOOL result = FALSE; + U16 batteryData = 0; if ( TRUE == waitForTxReady() ) { @@ -305,11 +348,12 @@ { // Due to the double buffer, the master must generate the stop condition after the (message size - 1)th data i2cSetStop( i2cREG1 ); - *dataPtr = i2cReceiveByte( i2cREG1); + batteryData = i2cReceiveByte( i2cREG1); if ( TRUE == waitForRxReady() ) { - *dataPtr = ( *dataPtr | ( i2cReceiveByte( i2cREG1) << 8 ) ); + batteryData = ( batteryData | ( i2cReceiveByte( i2cREG1) << 8 ) ); + *dataPtr = batteryData; result = TRUE; } }