Index: firmware/App/Drivers/Battery.c =================================================================== diff -u -ra8a0f729b78556c2c58a72bad942d04a9dead3f5 -rd2d9f19fd458bad756c2468ea10fceb0c11d4200 --- firmware/App/Drivers/Battery.c (.../Battery.c) (revision a8a0f729b78556c2c58a72bad942d04a9dead3f5) +++ firmware/App/Drivers/Battery.c (.../Battery.c) (revision d2d9f19fd458bad756c2468ea10fceb0c11d4200) @@ -27,25 +27,32 @@ // ********** private definitions ********** -#define BATTERY_SLAVE_ADDRESS 0x9 ///< Battery charger controller device address. -#define BATTERY_STATUS_CMD 0x13 ///< Command to get battery status. -#define BATTERY_STATUS_AC_PRESENT_MASK 0x8000 ///< Battery status AC present bit mask. -#define BATTERY_COMM_TIME_OUT_MS 1 ///< Battery communication time out in ms. -#define BATTERY_MONITOR_INTERVAL_MS MS_PER_SECOND ///< Battery monitor interval in ms. +#define BATTERY_CHARGER_SLAVE_ADDRESS 0x09 ///< Battery charger controller device address. +#define BATTERY_PACK_SLAVE_ADDRESS 0x0B ///< Battery pack device address. -#define AC_POWER_LOST_PERSISTENT_COUNT 3 ///< AC power lost persistent count before alarming. +#define BATTERY_CHARGER_STATUS_CMD 0x13 ///< Command to get battery charger status. +#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_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. // ********** private data ********** static U16 batteryStatus = 0; ///< Battery current status. +static U16 batteryRelStateOfCharge_pct = 0; ///< Battery pack relative state of charge. 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. // ********** private function prototypes ********** static void setupI2CDriver( void ); static BOOL waitForTxReady( void ); -static BOOL getBatteryStatus( void ); +static BOOL getBatteryData( U32 slaveAddr, U08 command, U16 * dataPtr ); /*********************************************************************//** * @brief @@ -57,7 +64,6 @@ void initBattery( void ) { setupI2CDriver(); - i2cSetSlaveAdd( i2cREG1, BATTERY_SLAVE_ADDRESS ); } /*********************************************************************//** @@ -74,31 +80,58 @@ lastBatteryMonitorTime = getMSTimerCount(); #ifndef DISABLE_BATT_COMM - if ( TRUE == getBatteryStatus() ) + if ( FALSE == hasBatteryChargerStatus ) { - if ( ( batteryStatus & BATTERY_STATUS_AC_PRESENT_MASK ) == 0 ) + hasBatteryChargerStatus = TRUE; + + if ( TRUE == getBatteryData( BATTERY_CHARGER_SLAVE_ADDRESS, BATTERY_CHARGER_STATUS_CMD, &batteryStatus ) ) { - if ( ++lostACPowerPersistentCount > AC_POWER_LOST_PERSISTENT_COUNT ) + if ( ( batteryStatus & BATTERY_CHARGER_STATUS_AC_PRESENT_MASK ) == 0 ) { - SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_AC_POWER_LOST, (U32)batteryStatus ); + if ( ++lostACPowerPersistentCount > AC_POWER_LOST_PERSISTENT_COUNT ) + { + SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_AC_POWER_LOST, batteryStatus ); + } } + else + { + lostACPowerPersistentCount = 0; + } } else { - lostACPowerPersistentCount = 0; + // Reset i2c bus if cannot communicate with battery charger + setupI2CDriver(); } } else { - // Reset i2c bus if cannot communicate with battery charger - setupI2CDriver(); + hasBatteryChargerStatus = FALSE; + + if ( FALSE == getBatteryData( BATTERY_PACK_SLAVE_ADDRESS, BATTERY_PACK_REL_STATE_OF_CHARGE_CMD, &batteryRelStateOfCharge_pct ) ) + { + setupI2CDriver(); + } } #endif } } /*********************************************************************//** * @brief + * The getBatteryRemainingPercent function returns the latest battery relative + * state of charge percentage. + * @details Inputs: none + * @details Outputs: none + * @return battery relative state of charge percentage + *************************************************************************/ +U16 getBatteryRemainingPercent( void ) +{ + return batteryRelStateOfCharge_pct; +} + +/*********************************************************************//** + * @brief * The setupI2CDriver function setups i2c driver in repeat mode to be * compatiable with SMBus protocol. * @details Inputs: none @@ -159,13 +192,17 @@ * The getBatteryStatus function send command to smart battery charger controller * interface to get battery status. * @details Inputs: none - * @details Outputs: get the current battery status + * @details Outputs: get the battery data based on given command + * @param slaveAddr slave device address + * @param command command to send to the slave device * @return TRUE if received battery status, otherwise FALSE *************************************************************************/ -static BOOL getBatteryStatus( void ) +static BOOL getBatteryData( U32 slaveAddr, U08 command, U16 * dataPtr ) { BOOL result = FALSE; + i2cSetSlaveAdd( i2cREG1, slaveAddr ); + if ( FALSE == i2cIsBusBusy( i2cREG1 ) ) { i2cSetMode( i2cREG1, I2C_MASTER ); @@ -174,7 +211,7 @@ if ( FALSE == waitForTxReady() ) { - i2cSendByte( i2cREG1, BATTERY_STATUS_CMD ); + i2cSendByte( i2cREG1, command ); } // Wait until command has been transmitted before start receiving command response @@ -187,11 +224,11 @@ { // Due to the double buffer, the master must generate the stop condition after the (message size - 1)th data i2cSetStop( i2cREG1 ); - batteryStatus = i2cReceiveByte( i2cREG1); + *dataPtr = i2cReceiveByte( i2cREG1); if ( FALSE == waitForRxReady() ) { - batteryStatus = ( batteryStatus | ( i2cReceiveByte( i2cREG1) << 8 ) ); + *dataPtr = ( *dataPtr | ( i2cReceiveByte( i2cREG1) << 8 ) ); result = TRUE; } } Index: firmware/App/Drivers/Battery.h =================================================================== diff -u -r569bc4e8119455be23c9d57353243e4479f4afcd -rd2d9f19fd458bad756c2468ea10fceb0c11d4200 --- firmware/App/Drivers/Battery.h (.../Battery.h) (revision 569bc4e8119455be23c9d57353243e4479f4afcd) +++ firmware/App/Drivers/Battery.h (.../Battery.h) (revision d2d9f19fd458bad756c2468ea10fceb0c11d4200) @@ -34,6 +34,8 @@ void initBattery( void ); void execBatteryMonitor( void ); + +U16 getBatteryRemainingPercent( void ); /**@}*/ Index: firmware/HD.dil =================================================================== diff -u -r890646a3e8079ada85b3d2ad056bf75b7e055054 -rd2d9f19fd458bad756c2468ea10fceb0c11d4200 --- firmware/HD.dil (.../HD.dil) (revision 890646a3e8079ada85b3d2ad056bf75b7e055054) +++ firmware/HD.dil (.../HD.dil) (revision d2d9f19fd458bad756c2468ea10fceb0c11d4200) @@ -7020,12 +7020,12 @@ DRIVER.I2C.VAR.I2C_DATACOUNT.VALUE=8 DRIVER.I2C.VAR.I2C_ADDRMODE.VALUE=7BIT_AMODE DRIVER.I2C.VAR.I2C_PORT_BIT0_FUN.VALUE=0 -DRIVER.I2C.VAR.I2C_PORT_BIT0_PDR.VALUE=0 +DRIVER.I2C.VAR.I2C_PORT_BIT0_PDR.VALUE=1 DRIVER.I2C.VAR.I2C_BC_VALUE.VALUE=0x0003 DRIVER.I2C.VAR.I2C_PORT_BIT1_FUN.VALUE=0 DRIVER.I2C.VAR.I2C_RM_ENA.VALUE=1 DRIVER.I2C.VAR.I2C_BC.VALUE=8_BIT -DRIVER.I2C.VAR.I2C_PORT_BIT1_PDR.VALUE=0 +DRIVER.I2C.VAR.I2C_PORT_BIT1_PDR.VALUE=1 DRIVER.I2C.VAR.I2C_TXRX_VALUE.VALUE=0 DRIVER.I2C.VAR.I2C_SCDLVL.VALUE=0 DRIVER.I2C.VAR.I2C_PORT_BIT0_PSL.VALUE=1 Index: firmware/include/i2c.h =================================================================== diff -u -r890646a3e8079ada85b3d2ad056bf75b7e055054 -rd2d9f19fd458bad756c2468ea10fceb0c11d4200 --- firmware/include/i2c.h (.../i2c.h) (revision 890646a3e8079ada85b3d2ad056bf75b7e055054) +++ firmware/include/i2c.h (.../i2c.h) (revision d2d9f19fd458bad756c2468ea10fceb0c11d4200) @@ -193,8 +193,8 @@ #define I2C_FUN_CONFIGVALUE 0U #define I2C_DIR_CONFIGVALUE ((uint32)((uint32)0U << 1U) \ | (uint32)((uint32)0U)) -#define I2C_ODR_CONFIGVALUE ((uint32)((uint32)0U << 1U) \ - | (uint32)((uint32)0U)) +#define I2C_ODR_CONFIGVALUE ((uint32)((uint32)1U << 1U) \ + | (uint32)((uint32)1U)) #define I2C_PD_CONFIGVALUE ((uint32)((uint32)0U << 1U) \ | (uint32)((uint32)0U)) #define I2C_PSL_CONFIGVALUE ((uint32)((uint32)1U << 1U) \ Index: firmware/source/i2c.c =================================================================== diff -u -r890646a3e8079ada85b3d2ad056bf75b7e055054 -rd2d9f19fd458bad756c2468ea10fceb0c11d4200 --- firmware/source/i2c.c (.../i2c.c) (revision 890646a3e8079ada85b3d2ad056bf75b7e055054) +++ firmware/source/i2c.c (.../i2c.c) (revision d2d9f19fd458bad756c2468ea10fceb0c11d4200) @@ -127,8 +127,8 @@ | (uint32)(0U); /* scl pin */ /** - set i2c pins open drain enable */ - i2cREG1->PDR = (uint32)((uint32)0U << 1U) /* sda pin */ - | (uint32)(0U); /* scl pin */ + i2cREG1->PDR = (uint32)((uint32)1U << 1U) /* sda pin */ + | (uint32)(1U); /* scl pin */ /** - set i2c pins pullup/pulldown enable */ i2cREG1->PDIS = (uint32)((uint32)0U << 1U) /* sda pin */