X-Git-Url: http://git.kpe.io/?p=avr_bc100.git;a=blobdiff_plain;f=BaseTinyFirmware%2FGCC%2Fbattery.c;fp=BaseTinyFirmware%2FGCC%2Fbattery.c;h=97c9f3bbce6ba8409460cc56e9483d385e1bf29b;hp=b3617118cd562fd4f29df63ad09e3ec1bcfb9861;hb=65612c4c7df34cdae10f9427ace6fd9e9e430d05;hpb=89173ec9cd6f33843bf27c45be0f2f9be3f5f0ce diff --git a/BaseTinyFirmware/GCC/battery.c b/BaseTinyFirmware/GCC/battery.c index b361711..97c9f3b 100644 --- a/BaseTinyFirmware/GCC/battery.c +++ b/BaseTinyFirmware/GCC/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$ @@ -124,7 +124,7 @@ const NTC_Lookup_t NTC[NTC_TABLE_SIZE] = { }; */ - + //****************************************************************************** // Functions //****************************************************************************** @@ -132,7 +132,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. */ @@ -140,14 +140,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. } @@ -176,7 +176,7 @@ unsigned char BatteryStatusRefresh(void) unsigned char sreg_saved; unsigned int adcs_VBAT_tmp; signed int adcs_avgIBAT_tmp; - + BattData.Present = FALSE; BattData.Charged = FALSE; BattData.Low = TRUE; @@ -208,7 +208,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 @@ -250,13 +250,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); @@ -286,14 +286,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); eeprom_write_byte(&BattEEPROM[page][i], temp); } - + if (OWI_ComputeCRC8(OWI_ReceiveByte(OWIBUS),crc) == 0) { success = TRUE; // Data read OK } @@ -301,7 +301,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. } @@ -309,7 +309,10 @@ unsigned char BatteryDataRefresh(void) // Erase local EEPROM page if there were any errors during transfer. if (!success) { for (i=0; i<32; i++) { - eeprom_write_byte(&BattEEPROM[page][i], 0); + // mthomas: avoid unneeded writes + if ( eeprom_read_byte( &BattEEPROM[page][i] ) != 0 ) { + eeprom_write_byte(&BattEEPROM[page][i], 0); + } } } } @@ -338,10 +341,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)); } @@ -352,7 +355,7 @@ void EnableBattery(unsigned char bat) void DisableBatteries(void) { // Turn off LEDs and disconnect batteries. - PORTB &= ~((1<= NTC[i].ADCV) { BattData.Temperature = (i<<2) ; - BattData.ADCSteps = NTC[i].ADCsteps; + BattData.ADCSteps = NTC[i].ADCsteps; BattData.Temperature -= ((adcs_rawNTC_tmp - NTC[i].ADCV)<<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; }