2008-03-14 Martin Thomas <mthomas@rhrk.uni-kl.de>
[avr_bc100.git] / BaseTinyFirmware / GCC / battery.c
index e6cacd863849592bcb9794a7ab9f5b0def018a8f..b3617118cd562fd4f29df63ad09e3ec1bcfb9861 100644 (file)
@@ -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
@@ -172,6 +173,9 @@ unsigned char BatteryStatusRefresh(void)
 {\r
        // Assume the worst..\r
        unsigned char success = FALSE;\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
@@ -186,13 +190,19 @@ 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
@@ -205,7 +215,7 @@ unsigned char BatteryStatusRefresh(void)
        * 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
@@ -299,7 +309,7 @@ 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
+                               eeprom_write_byte(&BattEEPROM[page][i], 0);\r
                        }\r
                }\r
        }\r
@@ -358,12 +368,18 @@ void DisableBatteries(void)
 unsigned char RIDLookUp (void)\r
 {\r
        unsigned char i, found = FALSE;\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
@@ -401,15 +417,21 @@ void NTCLookUp (void)
 {\r
        unsigned char i;\r
        unsigned char found = FALSE;\r
+       unsigned char 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
+                       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