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