Commit mthomas changes to GCC and port those changes to IAR
[avr_bc100.git] / BaseTinyFirmware / GCC / chargefunc.c
index 2194e569b7c462fad247a98abc72922e53cc8e1a..b3db020480f6a5991925d8bb41f2da57de7fd50e 100644 (file)
  *      AVR463: Charging NiMH Batteries with BC100\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
@@ -28,6 +28,7 @@
  ******************************************************************************/\r
 \r
 #include <avr/io.h>\r
+#include <avr/interrupt.h>\r
 \r
 #include "enums.h"\r
 #include "structs.h"\r
@@ -81,9 +82,11 @@ unsigned char ConstantCurrent(void)
 {\r
        unsigned char error = FALSE,\r
                      wasStopped = FALSE;\r
-       \r
+       unsigned char sreg_saved;\r
+       signed int adcs_avgIBAT_tmp;\r
+\r
        do      {\r
-      // Wait for ADC conversions to complete.\r
+               // Wait for ADC conversions to complete.\r
                ADC_Wait();\r
 \r
                // If Master has flagged for a charge inhibit, pause charging.\r
@@ -96,16 +99,21 @@ unsigned char ConstantCurrent(void)
                        // Continue charging!\r
                        if (wasStopped) {\r
                                wasStopped = FALSE;\r
-                               \r
+\r
                                // Timer variables are not reset by this.\r
                                Time_Start();\r
                        }\r
-         \r
+\r
+                       sreg_saved = SREG;\r
+                       cli();\r
+                       adcs_avgIBAT_tmp = ADCS.avgIBAT;\r
+                       SREG = sreg_saved;\r
+\r
                        // Adjust the charge current to within ChargeParameters.Current\r
                        // +/- BAT_CURRENT_HYST.\r
-                       if ((ADCS.avgIBAT < 0) ||\r
-                           (ADCS.avgIBAT < (ChargeParameters.Current - BAT_CURRENT_HYST))) {\r
-                                        \r
+                       if ((adcs_avgIBAT_tmp < 0) ||\r
+                           (adcs_avgIBAT_tmp < (ChargeParameters.Current - BAT_CURRENT_HYST))) {\r
+\r
                                if(!PWM_IncrementDutyCycle()) {\r
 #ifdef ABORT_IF_PWM_MAX\r
                                // If the duty cycle cannot be incremented, flag error and\r
@@ -115,9 +123,9 @@ unsigned char ConstantCurrent(void)
                                        error = TRUE;\r
 #endif\r
                                }\r
-                       } else if ((ADCS.avgIBAT >= 0) &&\r
-                                (ADCS.avgIBAT > (ChargeParameters.Current + BAT_CURRENT_HYST))) {\r
-                                        \r
+                       } else if ((adcs_avgIBAT_tmp >= 0) &&\r
+                                (adcs_avgIBAT_tmp > (ChargeParameters.Current + BAT_CURRENT_HYST))) {\r
+\r
                                if(!PWM_DecrementDutyCycle()) {\r
 #ifdef ABORT_IF_PWM_MIN\r
                                        // If the duty cycle cannot be decremented, flag error and\r
@@ -155,12 +163,14 @@ unsigned char ConstantVoltage(void)
 {\r
        unsigned char error = FALSE,\r
                      wasStopped = FALSE;\r
-       \r
+       unsigned char sreg_saved;\r
+       unsigned int adcs_VBAT_tmp;\r
+\r
        do{\r
-               \r
+\r
                // Wait for ADC conversions to complete.\r
                ADC_Wait();\r
-               \r
+\r
                // If Master has flagged for a charge inhibit, pause charging.\r
                // (This is to prevent damage during prolonged serial communication.)\r
                if (eeprom_read_byte(&BattControl[BattActive]) & BIT_BATTERY_CHARGE_INHIBIT) {\r
@@ -168,19 +178,24 @@ unsigned char ConstantVoltage(void)
                        Time_Stop();\r
                        OCR1B = 0;\r
                }\r
-               \r
+\r
                else {\r
                        // Continue charging!\r
                        if (wasStopped) {\r
                                wasStopped = FALSE;\r
-                               \r
+\r
                                // Timer variables aren't reset by this.\r
                                Time_Start();\r
                        }\r
-                       \r
+\r
+                       sreg_saved = SREG;\r
+                       cli();\r
+                       adcs_VBAT_tmp = ADCS.VBAT;\r
+                       SREG = sreg_saved;\r
+\r
                        // Adjust the charge voltage to within ChargeParameters.Voltage\r
                        // +/- BAT_VOLTAGE_HYST.\r
-                       if (ADCS.VBAT < (ChargeParameters.Voltage - BAT_VOLTAGE_HYST)) {\r
+                       if (adcs_VBAT_tmp < (ChargeParameters.Voltage - BAT_VOLTAGE_HYST)) {\r
 \r
                                if(!PWM_IncrementDutyCycle()) {\r
 #ifdef ABORT_IF_PWM_MAX\r
@@ -191,7 +206,7 @@ unsigned char ConstantVoltage(void)
                                        error = TRUE;\r
 #endif\r
                                }\r
-                       } else if (ADCS.VBAT > (ChargeParameters.Voltage + BAT_VOLTAGE_HYST)) {\r
+                       } else if (adcs_VBAT_tmp > (ChargeParameters.Voltage + BAT_VOLTAGE_HYST)) {\r
 \r
                                if(!PWM_DecrementDutyCycle()) {\r
 #ifdef ABORT_IF_PWM_MIN\r
@@ -222,7 +237,7 @@ unsigned char ConstantVoltage(void)
  *\r
  * The function also checks if the battery temperature is within limits,\r
  * if mains is OK, and if BatteryCheck() returns TRUE.\r
- * If an error is detected, the associated errorflag is set and \r
+ * If an error is detected, the associated errorflag is set and\r
  * ChargeParameters.NextState is changed to an appropriate state.\r
  *\r
  * \retval TRUE Halt now.\r
@@ -238,7 +253,10 @@ unsigned char ConstantVoltage(void)
 unsigned char HaltNow(void)\r
 {\r
        unsigned char i, halt = FALSE;\r
-       \r
+       unsigned int adcs_rawNTC_tmp, adcs_VBAT_tmp;\r
+       signed int adcs_avgIBAT_tmp;\r
+       unsigned char sreg_saved;\r
+\r
        // Wait for a full ADC-cycle to finish.\r
        ADC_Wait();\r
 \r
@@ -247,24 +265,30 @@ unsigned char HaltNow(void)
        // (Gets overridden if either mains is failing, or the battery changes.)\r
        for (i = 0x01; i != 0; i <<= 1) {\r
                if (HaltParameters.HaltFlags & i) {\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
                        switch (i) {\r
                        // Is VBAT less than the recorded maximum?\r
                        case HALT_VOLTAGE_DROP:\r
-\r
                                // Update VBATMax if VBAT is higher. Evaluate for halt otherwise.\r
-                               if (ADCS.VBAT > HaltParameters.VBATMax) {\r
-                                       HaltParameters.VBATMax = ADCS.VBAT;\r
-                               } else if((HaltParameters.VBATMax - ADCS.VBAT) >= \r
+                               if (adcs_VBAT_tmp > HaltParameters.VBATMax) {\r
+                                       HaltParameters.VBATMax = adcs_VBAT_tmp;\r
+                               } else if((HaltParameters.VBATMax - adcs_VBAT_tmp) >=\r
                                          HaltParameters.VoltageDrop) {\r
                                        halt = TRUE;\r
                                }\r
                        break;\r
 \r
-                       \r
+\r
                        // Has VBAT reached the maximum limit?\r
-                       case HALT_VOLTAGE_MAX:  \r
-                               \r
-                               if (ADCS.VBAT >= HaltParameters.VoltageMax) {\r
+                       case HALT_VOLTAGE_MAX:\r
+\r
+                               if (adcs_VBAT_tmp >= HaltParameters.VoltageMax) {\r
                                        halt = TRUE;\r
                                }\r
                                break;\r
@@ -272,61 +296,67 @@ unsigned char HaltNow(void)
 \r
                        // Has IBAT reached the minimum limit?\r
                        case HALT_CURRENT_MIN:\r
-                               \r
-                               if (ADCS.avgIBAT <= HaltParameters.CurrentMin) {\r
+\r
+                               if (adcs_avgIBAT_tmp <= HaltParameters.CurrentMin) {\r
                                        halt = TRUE;\r
                                }\r
                                break;\r
-       \r
-                               \r
+\r
+\r
                        // Is the temperature rising too fast?\r
                        case HALT_TEMPERATURE_RISE:\r
 \r
+                               sreg_saved = SREG;\r
+                               cli();\r
+                               adcs_rawNTC_tmp = ADCS.rawNTC;\r
+                               SREG = sreg_saved;\r
                                // If rawNTC has increased, the temperature has dropped.\r
                                // We can store this value for now, and start the timer.\r
                                // Otherwise, check if NTC has changed too fast.\r
-                               if (ADCS.rawNTC > HaltParameters.LastNTC) {\r
-                                       HaltParameters.LastNTC = ADCS.rawNTC;\r
+                               if (adcs_rawNTC_tmp > HaltParameters.LastNTC) {\r
+                                       HaltParameters.LastNTC = adcs_rawNTC_tmp;\r
                                        Time_Set(TIMER_TEMP,0,30,0);\r
-                                       \r
+\r
                                // Is the increase in temperature greater than the set threshold?\r
-                               } else  if ((HaltParameters.LastNTC - ADCS.rawNTC) >=\r
+                               } else  if ((HaltParameters.LastNTC - adcs_rawNTC_tmp) >=\r
                                  (BattData.ADCSteps * HaltParameters.TemperatureRise)) {\r
-                                       \r
-                                       // If this happened within a timeframe of 30 seconds, the \r
+\r
+                                       // If this happened within a timeframe of 30 seconds, the\r
                                        // temperature is rising faster than we want.\r
                                        // If not, update LastNTC and reset timer.\r
                                        if (Time_Left(TIMER_TEMP))  {\r
                                                halt = TRUE;\r
                                        } else {\r
-                                               HaltParameters.LastNTC = ADCS.rawNTC;\r
+                                               HaltParameters.LastNTC = adcs_rawNTC_tmp;\r
                                                Time_Set(TIMER_TEMP,0,30,0);\r
                                        }\r
                                }\r
                        break;\r
-       \r
-                       \r
+\r
+\r
                        // Is there any time left?\r
-                       case HALT_TIME:  \r
-                               \r
+                       case HALT_TIME:\r
+\r
                                if (!Time_Left(TIMER_CHG)) {\r
                                        halt = TRUE;\r
-                                       \r
-                                       // If exhaustion flagging is selected, stop the PWM, disable the \r
+\r
+                                       // If exhaustion flagging is selected, stop the PWM, disable the\r
                                        // battery and flag it as exhausted. Make ST_ERROR next state.\r
                                        if (HaltParameters.HaltFlags & HALT_FLAG_EXHAUSTION) {\r
                                                        PWM_Stop();\r
+\r
                                                        Battery_t tmp = eeprom_read_byte(&BattControl[BattActive]);\r
-                                                        tmp &= ~BIT_BATTERY_ENABLED; // Enabled = FALSE;\r
-                                                        eeprom_write_byte(&BattControl[BattActive], tmp);\r
+                                                                                        tmp &= ~BIT_BATTERY_ENABLED; // Enabled = FALSE;\r
+                                                                                        eeprom_write_byte(&BattControl[BattActive], tmp);\r
+\r
                                                        BattData.Exhausted = TRUE;\r
                                                        SetErrorFlag(ERR_BATTERY_EXHAUSTED);\r
                                                        ChargeParameters.NextState = ST_ERROR;\r
                                        }\r
                                }\r
                        break;\r
-                       \r
-                       \r
+\r
+\r
                        default:  // Shouldn't end up here, but is needed for MISRA compliance.\r
                        break;\r
                        }\r
@@ -338,7 +368,7 @@ unsigned char HaltNow(void)
        // Battery too cold or hot?\r
        if ((BattData.Temperature <= HaltParameters.TemperatureMin) ||\r
            (BattData.Temperature >= HaltParameters.TemperatureMax)) {\r
-                       \r
+\r
                PWM_Stop();\r
                SetErrorFlag(ERR_BATTERY_TEMPERATURE);\r
                ChargeParameters.NextState = ST_ERROR;\r