X-Git-Url: http://git.kpe.io/?p=avr_bc100.git;a=blobdiff_plain;f=BaseTinyFirmware%2FIAR%2Fbattery.c;fp=BaseTinyFirmware%2FIAR%2Fbattery.c;h=406e49492c50cee96fa8708be051abccf51d1496;hp=4486197e7f3957d222568d05d6106238b99b5c82;hb=65612c4c7df34cdae10f9427ace6fd9e9e430d05;hpb=89173ec9cd6f33843bf27c45be0f2f9be3f5f0ce diff --git a/BaseTinyFirmware/IAR/battery.c b/BaseTinyFirmware/IAR/battery.c index 4486197..406e494 100644 --- a/BaseTinyFirmware/IAR/battery.c +++ b/BaseTinyFirmware/IAR/battery.c @@ -13,14 +13,14 @@ * * \par Documentation - * For comprehensive code documentation, supported compilers, compiler + * For comprehensive code documentation, supported compilers, compiler * settings and supported devices see readme.html * * \author * Atmel Corporation: http://www.atmel.com \n * Support email: avr@atmel.com * - * + * * $Name$ * $Revision: 2299 $ * $RCSfile$ @@ -129,7 +129,7 @@ const NTC_Lookup_t NTC[NTC_TABLE_SIZE] = { * * Stores current capacity, then attempts to refresh battery status.\n * If the refresh is successful, old capacity is compared with the new one. - * + * * \retval FALSE Battery is disconnected, or capacity has changed. * \retval TRUE All OK. */ @@ -137,14 +137,14 @@ unsigned char BatteryCheck(void) { unsigned char success = TRUE; unsigned int oldCapacity; - + // Save to see if battery data has changed. - oldCapacity = BattData.Capacity; - + oldCapacity = BattData.Capacity; + if (!BatteryStatusRefresh()) { success = FALSE; // Battery not present or RID was invalid. } - + if (oldCapacity != BattData.Capacity) { success = FALSE; // Battery configuration has changed. } @@ -170,7 +170,7 @@ unsigned char BatteryStatusRefresh(void) { // Assume the worst.. unsigned char success = FALSE; - + BattData.Present = FALSE; BattData.Charged = FALSE; BattData.Low = TRUE; @@ -196,7 +196,7 @@ unsigned char BatteryStatusRefresh(void) // If we are not charging, yet VBAT is above safe limit, battery is present. // If we are charging and there's a current flowing, the battery is present. - + /*! \todo If ABORT_IF_PWM_MAX is defined this last check battery presence * check is redundant since charging will be aborted due to low current at * max duty cycle. That is preferrable since the charge current reading is @@ -238,13 +238,13 @@ unsigned char BatteryDataRefresh(void) unsigned char offset; unsigned char i, crc, family, temp, page; unsigned char success; - + // Look for EPROM and read 4 pages of 32 bytes each worth of data, if found. for (page = 0; page < 4; page++) { success = FALSE; - + if (OWI_DetectPresence(OWIBUS) == OWIBUS) { - + // Presence detected, check type and CRC. OWI_SendByte(OWI_ROM_READ, OWIBUS); family = OWI_ReceiveByte(OWIBUS); @@ -274,14 +274,14 @@ unsigned char BatteryDataRefresh(void) // and writing it to EEPROM. if (OWI_ComputeCRC8(OWI_ReceiveByte(OWIBUS),crc) == 0) { crc = 0; - + // Fill page with data. for (i=0; i<32; i++) { temp = OWI_ReceiveByte(OWIBUS); crc = OWI_ComputeCRC8(temp, crc); BattEEPROM[page][i] = temp; } - + if (OWI_ComputeCRC8(OWI_ReceiveByte(OWIBUS),crc) == 0) { success = TRUE; // Data read OK } @@ -289,7 +289,7 @@ unsigned char BatteryDataRefresh(void) } } else { // Wrong device type. } - } else { // No device found. + } else { // No device found. } } else { // No presence detected on one-wire bus. } @@ -297,7 +297,10 @@ unsigned char BatteryDataRefresh(void) // Erase local EEPROM page if there were any errors during transfer. if (!success) { for (i=0; i<32; i++) { - BattEEPROM[page][i] = 0; + // mthomas: avoid unneeded writes + if ( eeprom_read_byte( &BattEEPROM[page][i] ) != 0 ) { + BattEEPROM[page][i] = 0; + } } } } @@ -326,10 +329,10 @@ void EnableBattery(unsigned char bat) PORTB |= (1 << (PB4+bat)); // Disconnect other battery. - PORTB &= ~(1<<(PB5-bat)); + PORTB &= ~(1<<(PB5-bat)); do { // Let port switch settle. - } while (Time_Left(TIMER_GEN)); + } while (Time_Left(TIMER_GEN)); } @@ -340,7 +343,7 @@ void EnableBattery(unsigned char bat) void DisableBatteries(void) { // Turn off LEDs and disconnect batteries. - PORTB &= ~((1<= NTC[i].ADC) { BattData.Temperature = (i<<2) ; - BattData.ADCSteps = NTC[i].ADCsteps; + BattData.ADCSteps = NTC[i].ADCsteps; BattData.Temperature -= ((ADCS.rawNTC - NTC[i].ADC)<<1) / BattData.ADCSteps; - + found = TRUE; // Could be done with a break, but that violates MISRA. } } - - // For safety, is temperature is greater than the NTC + + // For safety, is temperature is greater than the NTC if (!found) { BattData.Temperature = 80; }