Index: firmware/App/Drivers/BatteryDriver.c =================================================================== diff -u -r502faee3d84565d510fa410510e96b0ccf6e08bd -rbbbc0d82d0f73ca7b0aa4e23fc9dbe38ee089655 --- firmware/App/Drivers/BatteryDriver.c (.../BatteryDriver.c) (revision 502faee3d84565d510fa410510e96b0ccf6e08bd) +++ firmware/App/Drivers/BatteryDriver.c (.../BatteryDriver.c) (revision bbbc0d82d0f73ca7b0aa4e23fc9dbe38ee089655) @@ -28,10 +28,13 @@ // ********** private definitions ********** -#define BATTERY_COMM_TIME_OUT_MS 2 ///< Battery communication time out in ms. +#define BATTERY_COMM_TIMEOUT_MS 2 ///< Battery communication time out in ms. #define BATTERY_CHARGER_SLAVE_ADDRESS 0x6B ///< Battery charger controller device address. #define BATTERY_PACK_SLAVE_ADDRESS 0x0B ///< Battery pack device address. +#define BATTERY_CHARGER_ADC_CONTROL_REGISTER 0x2BU +#define BATTERY_CHARGER_ADC_CONTROL_VALUE 0x8C + // ********** private data ********** static OVERRIDE_U32_T batteryI2cStatusRegister; ///< Battery I2C interrupt status register. @@ -50,11 +53,11 @@ 0x0d, ///< BATTERY_PACK_RELATIVE_STATE_OF_CHARGE 0x10, ///< BATTERY_PACK_FULL_CHARGE_CAPACITY 0x21, ///< BATTERY_CHARGER_STATUS - 0x08, ///< BATTERY_CHARGER_TS - 0x09, ///< BATTERY_CHARGER_VBAT + 0x37, ///< BATTERY_CHARGER_TS + 0x33, ///< BATTERY_CHARGER_VBAT 0x35, ///< BATTERY_CHARGER_VSYS 0x24, ///< BATTERY_CHARGER_FAULT - 0x0a, ///< BATTERY_CHARGER_CURRENT + 0x2F, ///< BATTERY_CHARGER_CURRENT }; // ********** private function prototypes ********** @@ -66,14 +69,15 @@ static void generateStopCondition( void ); static BOOL startCommTx( U32 slaveAddr ); static BOOL getData( U08 command, U16 * dataPtr ); +static BOOL writeChargerRegisterByte( U08 registerAddr, U08 registerData ); static void checkTooManyI2CCommFaults( void ); /*********************************************************************//** * @brief - * The initSetupI2cDriver function initializes the I2C driver for battery - * communication. + * The initSetupI2cDriver function initializes the battery I2C driver and + * battery I2C status override data. * @details \b Inputs: none - * @details \b Outputs: I2C driver initialized. + * @details \b Outputs: batteryI2cStatusRegister, I2C driver configuration * @return none *************************************************************************/ void initSetupI2cDriver( void ) @@ -87,6 +91,19 @@ /*********************************************************************//** * @brief + * The enableBatteryChargerAdc function enables and configures the battery + * charger ADC. + * @details \b Inputs: none + * @details \b Outputs: battery charger ADC control register + * @return TRUE if the charger ADC register write is successful, FALSE if not + *************************************************************************/ +BOOL enableBatteryChargerAdc( void ) +{ + return writeChargerRegisterByte( BATTERY_CHARGER_ADC_CONTROL_REGISTER, BATTERY_CHARGER_ADC_CONTROL_VALUE ); +} + +/*********************************************************************//** + * @brief * The setupI2CDriver function sets up the I2C driver in repeat mode to be * compatible with SMBus protocol. * @details \b Inputs: none @@ -104,7 +121,7 @@ * @brief * The waitForTxReady function checks for transmit ready status from the * I2C driver with a timeout. - * @details \b Inputs: none + * @details \b Inputs: i2cREG1 * @details \b Outputs: none * @return TRUE if I2C driver is ready to transmit, otherwise FALSE *************************************************************************/ @@ -115,7 +132,7 @@ while ( ( 0 == i2cIsTxReady( i2cREG1 ) ) && ( FALSE == timeout ) ) { - timeout = didTimeout( startTime, BATTERY_COMM_TIME_OUT_MS ); + timeout = didTimeout( startTime, BATTERY_COMM_TIMEOUT_MS ); } return ( TRUE == timeout ? FALSE : TRUE ); @@ -125,7 +142,7 @@ * @brief * The waitForRxReady function checks for receive ready status from the * I2C driver with a timeout. - * @details \b Inputs: none + * @details \b Inputs: i2cREG1 * @details \b Outputs: none * @return TRUE if I2C driver is ready to receive, otherwise FALSE *************************************************************************/ @@ -136,7 +153,7 @@ while ( ( 0 == i2cIsRxReady( i2cREG1 ) ) && ( FALSE == timeout ) ) { - timeout = didTimeout( startTime, BATTERY_COMM_TIME_OUT_MS ); + timeout = didTimeout( startTime, BATTERY_COMM_TIMEOUT_MS ); } return ( TRUE == timeout ? FALSE : TRUE ); @@ -146,7 +163,7 @@ * @brief * The waitForAccessReady function checks if I2C registers are ready * to be accessed. - * @details \b Inputs: none + * @details \b Inputs: i2cREG1 * @details \b Outputs: none * @return TRUE if I2C driver registers are ready to be accessed, otherwise FALSE *************************************************************************/ @@ -157,7 +174,7 @@ while ( ( 0 == ( i2cREG1->STR & (U32)I2C_ARDY ) ) && ( FALSE == timeout ) ) { - timeout = didTimeout( startTime, BATTERY_COMM_TIME_OUT_MS ); + timeout = didTimeout( startTime, BATTERY_COMM_TIMEOUT_MS ); } return ( TRUE == timeout ? FALSE : TRUE ); @@ -179,7 +196,7 @@ i2cSetStop( i2cREG1 ); while ( ( 0 == i2cIsStopDetected( i2cREG1 ) ) && ( FALSE == timeout ) ) { - timeout = didTimeout( startTime, BATTERY_COMM_TIME_OUT_MS ); + timeout = didTimeout( startTime, BATTERY_COMM_TIMEOUT_MS ); } } @@ -194,7 +211,7 @@ * @param dataPtr pointer to store the register data * @return TRUE if data received successfully, otherwise FALSE *************************************************************************/ -BOOL getBatteryData( BATTERY_DEVICE_ADDRESS_ENUM_T deviceSel, BATTERY_MANAGEMENT_ENUM_T registerSel, U32 * dataPtr ) +BOOL getBatteryData( BATTERY_DEVICE_ADDRESS_ENUM_T deviceSel, BATTERY_MANAGEMENT_ENUM_T registerSel, U32 *dataPtr ) { BOOL result = FALSE; if ( ( deviceSel < NUM_OF_BATTERY_DEVICES ) && ( registerSel < NUM_OF_BATTERY_REGISTERS -1 ) ) @@ -312,6 +329,49 @@ /*********************************************************************//** * @brief + * The writeChargerRegisterByte function writes one byte to the selected + * battery charger register. + * @details \b Alarm: ALARM_ID_TD_BATTERY_COMM_FAULT if too many battery + * communication failures are detected + * @details \b Inputs: none + * @details \b Outputs: battery charger register + * @param registerAddr battery charger register address + * @param registerData byte value to write to the charger register + * @return TRUE if the charger register write is successful, FALSE if not + *************************************************************************/ +static BOOL writeChargerRegisterByte( U08 registerAddr, U08 registerData ) +{ + BOOL result = FALSE; + + if ( TRUE == startCommTx( BATTERY_CHARGER_SLAVE_ADDRESS ) ) + { + if ( TRUE == waitForTxReady() ) + { + i2cSendByte( i2cREG1, registerAddr ); + + if ( TRUE == waitForTxReady() ) + { + i2cSendByte( i2cREG1, registerData ); + + if ( TRUE == waitForTxReady() ) + { + generateStopCondition(); + result = TRUE; + } + } + } + } + + if ( FALSE == result ) + { + setupI2CDriver(); + } + + return result; +} + +/*********************************************************************//** + * @brief * The getI2cStatusRegister function returns the current I2C status * register value. * @details \b Inputs: batteryI2cStatusRegister, i2cREG1->STR