\r
*\r
* \par Documentation\r
- * For comprehensive code documentation, supported compilers, compiler \r
+ * For comprehensive code documentation, supported compilers, compiler\r
* settings and supported devices see readme.html\r
*\r
* \author\r
* Atmel Corporation: http://www.atmel.com \n\r
* Support email: avr@atmel.com\r
*\r
- * \r
+ *\r
* $Name$\r
* $Revision: 2299 $\r
* $RCSfile$\r
};\r
*/\r
\r
- \r
+\r
//******************************************************************************\r
// Functions\r
//******************************************************************************\r
*\r
* Stores current capacity, then attempts to refresh battery status.\n\r
* If the refresh is successful, old capacity is compared with the new one.\r
- * \r
+ *\r
* \retval FALSE Battery is disconnected, or capacity has changed.\r
* \retval TRUE All OK.\r
*/\r
{\r
unsigned char success = TRUE;\r
unsigned int oldCapacity;\r
- \r
+\r
// Save to see if battery data has changed.\r
- oldCapacity = BattData.Capacity; \r
- \r
+ oldCapacity = BattData.Capacity;\r
+\r
if (!BatteryStatusRefresh()) {\r
success = FALSE; // Battery not present or RID was invalid.\r
}\r
- \r
+\r
if (oldCapacity != BattData.Capacity) {\r
success = FALSE; // Battery configuration has changed.\r
}\r
unsigned char sreg_saved;\r
unsigned int adcs_VBAT_tmp;\r
signed int adcs_avgIBAT_tmp;\r
- \r
+\r
BattData.Present = FALSE;\r
BattData.Charged = FALSE;\r
BattData.Low = TRUE;\r
\r
// If we are not charging, yet VBAT is above safe limit, battery is present.\r
// If we are charging and there's a current flowing, the battery is present.\r
- \r
+\r
/*! \todo If ABORT_IF_PWM_MAX is defined this last check battery presence\r
* check is redundant since charging will be aborted due to low current at\r
* max duty cycle. That is preferrable since the charge current reading is\r
unsigned char offset;\r
unsigned char i, crc, family, temp, page;\r
unsigned char success;\r
- \r
+\r
// Look for EPROM and read 4 pages of 32 bytes each worth of data, if found.\r
for (page = 0; page < 4; page++) {\r
success = FALSE;\r
- \r
+\r
if (OWI_DetectPresence(OWIBUS) == OWIBUS) {\r
- \r
+\r
// Presence detected, check type and CRC.\r
OWI_SendByte(OWI_ROM_READ, OWIBUS);\r
family = OWI_ReceiveByte(OWIBUS);\r
// and writing it to EEPROM.\r
if (OWI_ComputeCRC8(OWI_ReceiveByte(OWIBUS),crc) == 0) {\r
crc = 0;\r
- \r
+\r
// Fill page with data.\r
for (i=0; i<32; i++) {\r
temp = OWI_ReceiveByte(OWIBUS);\r
crc = OWI_ComputeCRC8(temp, crc);\r
eeprom_write_byte(&BattEEPROM[page][i], temp);\r
}\r
- \r
+\r
if (OWI_ComputeCRC8(OWI_ReceiveByte(OWIBUS),crc) == 0) {\r
success = TRUE; // Data read OK\r
}\r
}\r
} else { // Wrong device type.\r
}\r
- } else { // No device found. \r
+ } else { // No device found.\r
}\r
} else { // No presence detected on one-wire bus.\r
}\r
// Erase local EEPROM page if there were any errors during transfer.\r
if (!success) {\r
for (i=0; i<32; i++) {\r
- eeprom_write_byte(&BattEEPROM[page][i], 0);\r
+ // mthomas: avoid unneeded writes\r
+ if ( eeprom_read_byte( &BattEEPROM[page][i] ) != 0 ) {\r
+ eeprom_write_byte(&BattEEPROM[page][i], 0);\r
+ }\r
}\r
}\r
}\r
PORTB |= (1 << (PB4+bat));\r
\r
// Disconnect other battery.\r
- PORTB &= ~(1<<(PB5-bat)); \r
+ PORTB &= ~(1<<(PB5-bat));\r
\r
do { // Let port switch settle.\r
- } while (Time_Left(TIMER_GEN)); \r
+ } while (Time_Left(TIMER_GEN));\r
}\r
\r
\r
void DisableBatteries(void)\r
{\r
// Turn off LEDs and disconnect batteries.\r
- PORTB &= ~((1<<PB4)|(1<<PB5)); \r
+ PORTB &= ~((1<<PB4)|(1<<PB5));\r
}\r
\r
\r
unsigned char i, found = FALSE;\r
unsigned int adcs_rawRID_tmp;\r
unsigned char sreg_saved;\r
- \r
+\r
// Lookup in the RID-table. If measured RID is within the limits\r
// of an entry, those data are used, and TRUE is returned.\r
for (i = 0 ; i < RID_TABLE_SIZE; i++) {\r
BattData.MaxCurrent = RID[i].Icharge;\r
BattData.MaxTime = RID[i].tCutOff;\r
BattData.MinCurrent = RID[i].ICutOff;\r
- \r
+\r
found = TRUE;\r
}\r
}\r
}\r
- \r
+\r
// If no valid entry is found, use defaults and return FALSE.\r
if (!found) {\r
BattData.Capacity = DEF_BAT_CAPACITY;\r
BattData.MaxTime = DEF_BAT_TIME_MAX;\r
BattData.MinCurrent = DEF_BAT_CURRENT_MIN;\r
}\r
- \r
+\r
return(found);\r
}\r
\r
{\r
unsigned char i;\r
unsigned char found = FALSE;\r
- unsigned char adcs_rawNTC_tmp;\r
+ unsigned int adcs_rawNTC_tmp;\r
unsigned char sreg_saved;\r
- \r
+\r
// Lookup in the NTC-table. Use the first entry which is equal or below\r
// sampled NTC. Calculate temperature by using the index number, and the\r
// difference between the measured NTC value and the one in the entry.\r
SREG = sreg_saved;\r
if (adcs_rawNTC_tmp >= NTC[i].ADCV) {\r
BattData.Temperature = (i<<2) ;\r
- BattData.ADCSteps = NTC[i].ADCsteps; \r
+ BattData.ADCSteps = NTC[i].ADCsteps;\r
BattData.Temperature -= ((adcs_rawNTC_tmp - NTC[i].ADCV)<<1) / BattData.ADCSteps;\r
- \r
+\r
found = TRUE; // Could be done with a break, but that violates MISRA.\r
}\r
}\r
- \r
- // For safety, is temperature is greater than the NTC \r
+\r
+ // For safety, is temperature is greater than the NTC\r
if (!found) {\r
BattData.Temperature = 80;\r
}\r