X-Git-Url: http://git.kpe.io/?p=avr_bc100.git;a=blobdiff_plain;f=BaseTinyFirmware%2FIAR%2Fstatefunc.c;fp=BaseTinyFirmware%2FIAR%2Fstatefunc.c;h=9dbdde18c4cf8c6bcc5a93b03471ea840d4f048d;hp=e34994f7d704e268184cd4910af2e0d2dbd9fc16;hb=89173ec9cd6f33843bf27c45be0f2f9be3f5f0ce;hpb=7997f136b12114035265bd082c854bff95fd73dc diff --git a/BaseTinyFirmware/IAR/statefunc.c b/BaseTinyFirmware/IAR/statefunc.c index e34994f..9dbdde1 100644 --- a/BaseTinyFirmware/IAR/statefunc.c +++ b/BaseTinyFirmware/IAR/statefunc.c @@ -16,14 +16,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$ @@ -60,6 +60,9 @@ unsigned char ErrorFlags; //!< \brief Holds error flags. //! \note See menu.h for definitions of states. unsigned char ErrorState; +//! \brief Set to 1 by the watchdog-timeout-ISR +//! \note added by Martin Thomas +volatile unsigned char WatchdogFlag; //****************************************************************************** // Functions @@ -88,17 +91,20 @@ unsigned char Initialize(unsigned char inp) // Disable interrupts while setting prescaler. __disable_interrupt(); - + CLKPR = (1< 8 MHz clock frequency. - + // Init 1-Wire(R) interface. OWI_Init(OWIBUS); - + // Clear on-chip EEPROM. - for (page = 0; page < 4; page++) { + for (page = 0; page < 4; page++) { for (i = 0; i < 32; i++) { - BattEEPROM[page][i] = 0; + // From mthomas GCC addition + if (BattEEPROM[page][i] != 0) { + BattEEPROM[page][i] = 0; + } } } @@ -114,12 +120,12 @@ unsigned char Initialize(unsigned char inp) ADC_Wait(); BatteryStatusRefresh(); } - + DisableBatteries(); - + BattActive = 0; // We have to start somewhere.. ErrorFlags = 0; - + // Init complete! Go to ST_BATCON next. return(ST_BATCON); } @@ -145,23 +151,23 @@ unsigned char Initialize(unsigned char inp) unsigned char BatteryControl(unsigned char inp) { unsigned char i; - + // Make sure ADC inputs are configured properly! (Will disables batteries.) if (!JumperCheck()) { return(ST_ERROR); // Error. Exit before damage is done! } - + // If neither battery is valid, flag error and go to error state if ((!BattControl[0].Enabled) && (!BattControl[1].Enabled)) { SetErrorFlag(ERR_NO_BATTERIES_ENABLED); - + return(ST_ERROR); } // Get ADC-readings, try to read EPROM, and start prequalification // of any uncharged battery. for (i = 0; i < 2; i++) { - if (BattControl[i].Enabled) { + if (BattControl[i].Enabled) { EnableBattery(i); ADC_Wait(); @@ -169,7 +175,7 @@ unsigned char BatteryControl(unsigned char inp) if (!BattData.Charged) { BatteryDataRefresh(); - return(ST_PREQUAL); + return(ST_PREQUAL); } } } @@ -221,43 +227,73 @@ unsigned char Sleep(unsigned char inp) return(ST_BATCON); } } - + DisableBatteries(); // Disable both batteries before Doze()! } while (TRUE); } +/*! \brief Watchdog interrupt-service-routine + * + * Called on watchdog timeout, added by Martin Thomas + */ +#pragma vector = WDT_vect +__interrupt void WDT_ISR(void) +{ + WatchdogFlag = 1; +} + /*! \brief Doze off for approx. 8 seconds (Vcc = 5 V) * * Waits for ADC-cycles to complete, disables the ADC, then sleeps for * approx. 8 seconds (Vcc = 5 V) using the watchdog timer. * On wakeup, ADC is re-enabled. + * Modification by Martin Thomas: + * Do not enter standby mode if PB6 is low (MASTER_INT on BC100) + * so SPI communication with the master-controller is still possible + * during "doze". */ void Doze(void) { + uint8_t sreg_saved; // Wait for this ADC cycle to complete, then halt after the next one. ADC_Wait(); ADCS.Halt = TRUE; ADCS.Flag = FALSE; - + do { - } while (ADCS.Flag == FALSE); - + } while (ADCS.Flag == FALSE); + WDTCR = (1<