X-Git-Url: http://git.kpe.io/?p=avr_bc100.git;a=blobdiff_plain;f=BaseTinyFirmware%2FGCC%2Fstatefunc.c;fp=BaseTinyFirmware%2FGCC%2Fstatefunc.c;h=9fc0ae5ae08669aecee4f6b6dfa13b1d2c4117c3;hp=ef1464dcab714766f0addf4d6fb0261e04cec53b;hb=89173ec9cd6f33843bf27c45be0f2f9be3f5f0ce;hpb=7997f136b12114035265bd082c854bff95fd73dc diff --git a/BaseTinyFirmware/GCC/statefunc.c b/BaseTinyFirmware/GCC/statefunc.c index ef1464d..9fc0ae5 100644 --- a/BaseTinyFirmware/GCC/statefunc.c +++ b/BaseTinyFirmware/GCC/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$ @@ -65,6 +65,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 @@ -93,17 +96,20 @@ unsigned char Initialize(unsigned char inp) // Disable interrupts while setting prescaler. cli(); - + 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++) { - eeprom_write_byte(&BattEEPROM[page][i], 0); + // mthomas: avoid unneeded writes + if ( eeprom_read_byte( &BattEEPROM[page][i] ) != 0 ) { + eeprom_write_byte( &BattEEPROM[page][i], 0 ); + } } } @@ -124,10 +130,10 @@ unsigned char Initialize(unsigned char inp) } DisableBatteries(); - + BattActive = 0; // We have to start somewhere.. ErrorFlags = 0; - + // Init complete! Go to ST_BATCON next. return(ST_BATCON); } @@ -153,16 +159,16 @@ 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 (!(eeprom_read_byte(&BattControl[0]) & BIT_BATTERY_ENABLED) && (!eeprom_read_byte(&BattControl[1]) & BIT_BATTERY_ENABLED)) { SetErrorFlag(ERR_NO_BATTERIES_ENABLED); - + return(ST_ERROR); } @@ -229,43 +235,72 @@ 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 + */ +ISR(WDT_vect) +{ + 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); - + WDTCR = (1<