X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=BaseTinyFirmware%2FGCC%2Fbattery.c;h=97c9f3bbce6ba8409460cc56e9483d385e1bf29b;hb=65612c4c7df34cdae10f9427ace6fd9e9e430d05;hp=e6cacd863849592bcb9794a7ab9f5b0def018a8f;hpb=5b95e754a4af80c7389486ee874ac07c166a0867;p=avr_bc100.git diff --git a/BaseTinyFirmware/GCC/battery.c b/BaseTinyFirmware/GCC/battery.c index e6cacd8..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$ @@ -30,6 +30,7 @@ #include #include +#include #include "structs.h" #include "enums.h" @@ -123,7 +124,7 @@ const NTC_Lookup_t NTC[NTC_TABLE_SIZE] = { }; */ - + //****************************************************************************** // Functions //****************************************************************************** @@ -131,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. */ @@ -139,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. } @@ -172,7 +173,10 @@ unsigned char BatteryStatusRefresh(void) { // Assume the worst.. unsigned char success = FALSE; - + unsigned char sreg_saved; + unsigned int adcs_VBAT_tmp; + signed int adcs_avgIBAT_tmp; + BattData.Present = FALSE; BattData.Charged = FALSE; BattData.Low = TRUE; @@ -186,26 +190,32 @@ unsigned char BatteryStatusRefresh(void) NTCLookUp(); BattData.HasRID = RIDLookUp(); + sreg_saved = SREG; + cli(); + adcs_VBAT_tmp = ADCS.VBAT; + adcs_avgIBAT_tmp = ADCS.avgIBAT; + SREG = sreg_saved; + // Is the battery voltage above minimum safe cell voltage? - if (ADCS.VBAT >= BAT_VOLTAGE_MIN) { + if (adcs_VBAT_tmp >= BAT_VOLTAGE_MIN) { BattData.Low = FALSE; } // Is the battery charged? - if (ADCS.VBAT >= BAT_VOLTAGE_LOW) { + if (adcs_VBAT_tmp >= BAT_VOLTAGE_LOW) { BattData.Charged = TRUE; } // 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 * not 100% proof. */ if (((OCR1B == 0) && (!BattData.Low)) || - ((OCR1B != 0) && (ADCS.avgIBAT > 0))) { + ((OCR1B != 0) && (adcs_avgIBAT_tmp > 0))) { BattData.Present = TRUE; success = TRUE; } else { @@ -240,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); @@ -276,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 } @@ -291,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. } @@ -299,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); + } } } } @@ -328,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)); } @@ -342,7 +355,7 @@ void EnableBattery(unsigned char bat) void DisableBatteries(void) { // Turn off LEDs and disconnect batteries. - PORTB &= ~((1<= RID[i].Low) { - if (ADCS.rawRID <= RID[i].High) { + sreg_saved = SREG; + cli(); + adcs_rawRID_tmp = ADCS.rawRID; + SREG = sreg_saved; + if (adcs_rawRID_tmp >= RID[i].Low) { + if (adcs_rawRID_tmp <= RID[i].High) { BattData.Capacity = RID[i].Capacity; BattData.MaxCurrent = RID[i].Icharge; BattData.MaxTime = RID[i].tCutOff; BattData.MinCurrent = RID[i].ICutOff; - + found = TRUE; } } } - + // If no valid entry is found, use defaults and return FALSE. if (!found) { BattData.Capacity = DEF_BAT_CAPACITY; @@ -381,7 +400,7 @@ unsigned char RIDLookUp (void) BattData.MaxTime = DEF_BAT_TIME_MAX; BattData.MinCurrent = DEF_BAT_CURRENT_MIN; } - + return(found); } @@ -401,21 +420,27 @@ void NTCLookUp (void) { unsigned char i; unsigned char found = FALSE; - + unsigned int adcs_rawNTC_tmp; + unsigned char sreg_saved; + // Lookup in the NTC-table. Use the first entry which is equal or below // sampled NTC. Calculate temperature by using the index number, and the // difference between the measured NTC value and the one in the entry. for (i=0 ; (i < NTC_TABLE_SIZE) && (!found); i++) { - if (ADCS.rawNTC >= NTC[i].ADCV) { + sreg_saved = SREG; + cli(); + adcs_rawNTC_tmp = ADCS.rawNTC; + SREG = sreg_saved; + if (adcs_rawNTC_tmp >= NTC[i].ADCV) { BattData.Temperature = (i<<2) ; - BattData.ADCSteps = NTC[i].ADCsteps; - BattData.Temperature -= ((ADCS.rawNTC - NTC[i].ADCV)<<1) / BattData.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; }