Commit mthomas changes to GCC and port those changes to IAR
[avr_bc100.git] / BaseTinyFirmware / GCC / battery.c
index e6cacd863849592bcb9794a7ab9f5b0def018a8f..97c9f3bbce6ba8409460cc56e9483d385e1bf29b 100644 (file)
 \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
@@ -30,6 +30,7 @@
 \r
 #include <avr/io.h>\r
 #include <avr/eeprom.h>\r
+#include <avr/interrupt.h>\r
 \r
 #include "structs.h"\r
 #include "enums.h"\r
@@ -123,7 +124,7 @@ const NTC_Lookup_t NTC[NTC_TABLE_SIZE] = {
 };\r
 */\r
 \r
\r
+\r
 //******************************************************************************\r
 // Functions\r
 //******************************************************************************\r
@@ -131,7 +132,7 @@ const NTC_Lookup_t NTC[NTC_TABLE_SIZE] = {
  *\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
@@ -139,14 +140,14 @@ unsigned char BatteryCheck(void)
 {\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
@@ -172,7 +173,10 @@ unsigned char BatteryStatusRefresh(void)
 {\r
        // Assume the worst..\r
        unsigned char success = FALSE;\r
-       \r
+       unsigned char sreg_saved;\r
+       unsigned int adcs_VBAT_tmp;\r
+       signed int adcs_avgIBAT_tmp;\r
+\r
        BattData.Present = FALSE;\r
        BattData.Charged = FALSE;\r
        BattData.Low = TRUE;\r
@@ -186,26 +190,32 @@ unsigned char BatteryStatusRefresh(void)
        NTCLookUp();\r
        BattData.HasRID = RIDLookUp();\r
 \r
+       sreg_saved = SREG;\r
+       cli();\r
+       adcs_VBAT_tmp = ADCS.VBAT;\r
+       adcs_avgIBAT_tmp = ADCS.avgIBAT;\r
+       SREG = sreg_saved;\r
+\r
        // Is the battery voltage above minimum safe cell voltage?\r
-       if (ADCS.VBAT >= BAT_VOLTAGE_MIN) {\r
+       if (adcs_VBAT_tmp >= BAT_VOLTAGE_MIN) {\r
                BattData.Low = FALSE;\r
        }\r
 \r
        // Is the battery charged?\r
-       if (ADCS.VBAT >= BAT_VOLTAGE_LOW) {\r
+       if (adcs_VBAT_tmp >= BAT_VOLTAGE_LOW) {\r
                BattData.Charged = TRUE;\r
        }\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
        * not 100% proof.\r
        */\r
        if (((OCR1B == 0) && (!BattData.Low)) ||\r
-           ((OCR1B != 0) && (ADCS.avgIBAT > 0))) {\r
+           ((OCR1B != 0) && (adcs_avgIBAT_tmp > 0))) {\r
                BattData.Present = TRUE;\r
                success = TRUE;\r
        } else {\r
@@ -240,13 +250,13 @@ unsigned char BatteryDataRefresh(void)
        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
@@ -276,14 +286,14 @@ unsigned char BatteryDataRefresh(void)
                                        // 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
@@ -291,7 +301,7 @@ unsigned char BatteryDataRefresh(void)
                                        }\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
@@ -299,7 +309,10 @@ unsigned char BatteryDataRefresh(void)
          // 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
@@ -328,10 +341,10 @@ void EnableBattery(unsigned char bat)
        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
@@ -342,7 +355,7 @@ void EnableBattery(unsigned char bat)
 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
@@ -358,22 +371,28 @@ void DisableBatteries(void)
 unsigned char RIDLookUp (void)\r
 {\r
        unsigned char i, found = FALSE;\r
-               \r
+       unsigned int adcs_rawRID_tmp;\r
+       unsigned char sreg_saved;\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
-               if (ADCS.rawRID >= RID[i].Low) {\r
-                       if (ADCS.rawRID <= RID[i].High) {\r
+               sreg_saved = SREG;\r
+               cli();\r
+               adcs_rawRID_tmp = ADCS.rawRID;\r
+               SREG = sreg_saved;\r
+               if (adcs_rawRID_tmp >= RID[i].Low) {\r
+                       if (adcs_rawRID_tmp <= RID[i].High) {\r
                                BattData.Capacity = RID[i].Capacity;\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
@@ -381,7 +400,7 @@ unsigned char RIDLookUp (void)
                BattData.MaxTime = DEF_BAT_TIME_MAX;\r
                BattData.MinCurrent = DEF_BAT_CURRENT_MIN;\r
        }\r
-       \r
+\r
        return(found);\r
 }\r
 \r
@@ -401,21 +420,27 @@ void NTCLookUp (void)
 {\r
        unsigned char i;\r
        unsigned char found = FALSE;\r
-       \r
+       unsigned int adcs_rawNTC_tmp;\r
+       unsigned char sreg_saved;\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
        for (i=0 ; (i < NTC_TABLE_SIZE) && (!found); i++)       {\r
-               if (ADCS.rawNTC >= NTC[i].ADCV) {\r
+               sreg_saved = SREG;\r
+               cli();\r
+               adcs_rawNTC_tmp = ADCS.rawNTC;\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.Temperature -= ((ADCS.rawNTC - NTC[i].ADCV)<<1) / BattData.ADCSteps;\r
-                       \r
+                       BattData.ADCSteps = NTC[i].ADCsteps;\r
+                       BattData.Temperature -= ((adcs_rawNTC_tmp - NTC[i].ADCV)<<1) / BattData.ADCSteps;\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