Index: firmware/App/Drivers/Battery.c =================================================================== diff -u -re010519fe8f7bdfcf518544d480baa15424a70a7 -r8b1a3c2d95817e6d7619783e86a91c3d32ea70b2 --- firmware/App/Drivers/Battery.c (.../Battery.c) (revision e010519fe8f7bdfcf518544d480baa15424a70a7) +++ firmware/App/Drivers/Battery.c (.../Battery.c) (revision 8b1a3c2d95817e6d7619783e86a91c3d32ea70b2) @@ -30,18 +30,19 @@ #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 25 ///< Battery communication time out in ms. +#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. // ********** private data ********** static U16 batteryStatus = 0; ///< Battery current status. static U32 lastBatteryMonitorTime = 0; ///< Previous battery monitor time. -static BOOL isBatteryCommTimeOut = FALSE; ///< Flag indicates battery communication time out. +static BOOL isBatteryStatusReceived = FALSE; ///< Flag indicates battery status received. // ********** private function prototypes ********** static void setupI2CDriver( void ); +static BOOL waitForTxReady( void ); static void getBatteryStatus( void ); /*********************************************************************//** @@ -70,26 +71,19 @@ { lastBatteryMonitorTime = getMSTimerCount(); - if ( FALSE == isBatteryCommTimeOut ) - { - getBatteryStatus(); - } - else - { - isBatteryCommTimeOut = FALSE; + getBatteryStatus(); - // Reset i2c bus if comm timeout - setupI2CDriver(); - } - - if ( ( batteryStatus & BATTERY_STATUS_AC_PRESENT_MASK ) == 0 ) + if ( TRUE == isBatteryStatusReceived ) { - SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_AC_POWER_LOST, (U32)batteryStatus ); + if ( ( batteryStatus & BATTERY_STATUS_AC_PRESENT_MASK ) == 0 ) + { + SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_AC_POWER_LOST, (U32)batteryStatus ); + } + else + { + clearAlarm( ALARM_ID_HD_AC_POWER_LOST ); + } } - else - { - clearAlarm( ALARM_ID_HD_AC_POWER_LOST ); - } } } @@ -110,6 +104,27 @@ /*********************************************************************//** * @brief + * The waitForTxReady function checks for transmit ready status from i2c + * driver with a timeout. + * @details Inputs: none + * @details Outputs: checked i2c transmit ready status + * @return TRUE if timeout, otherwise FALSE + *************************************************************************/ +static BOOL waitForTxReady( void ) +{ + U32 const startTime = getMSTimerCount(); + BOOL timeout = FALSE; + + while ( ( 0 == i2cIsTxReady( i2cREG1 ) ) && ( FALSE == timeout ) ) + { + timeout = didTimeout( startTime, BATTERY_COMM_TIME_OUT_MS ); + } + + return timeout; +} + +/*********************************************************************//** + * @brief * The getBatteryStatus function send command to smart battery charger controller * interface to get battery status. * @details Inputs: none @@ -118,28 +133,33 @@ *************************************************************************/ static void getBatteryStatus( void ) { - U32 const startCommTime = getMSTimerCount(); - if ( FALSE == i2cIsBusBusy( i2cREG1 ) ) { i2cSetMode( i2cREG1, I2C_MASTER ); i2cSetDirection( i2cREG1, I2C_TRANSMITTER ); i2cSetStart( i2cREG1 ); - i2cSendByte( i2cREG1, BATTERY_STATUS_CMD ); - while ( ( 0 == i2cIsTxReady( i2cREG1 ) ) || ( TRUE == isBatteryCommTimeOut ) ) + if ( FALSE == waitForTxReady() ) { - isBatteryCommTimeOut = didTimeout( startCommTime, BATTERY_COMM_TIME_OUT_MS ); + i2cSendByte( i2cREG1, BATTERY_STATUS_CMD ); } - if ( FALSE == isBatteryCommTimeOut ) + // Wait until command has been transmitted before start receiving command response + if ( FALSE == waitForTxReady() ) { i2cSetDirection( i2cREG1, I2C_RECEIVER ); i2cSetStart( i2cREG1 ); batteryStatus = i2cReceiveByte( i2cREG1); i2cSetStop( i2cREG1 ); batteryStatus = ( batteryStatus | ( i2cReceiveByte( i2cREG1) << 8 ) ); + + isBatteryStatusReceived = TRUE; } + else + { + // Reset i2c bus if comm timeout + setupI2CDriver(); + } } }