X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=BaseTinyFirmware%2FGCC%2Fstatefunc.c;h=e0e265901ee3cb064da476f907b735a96c16e890;hb=65612c4c7df34cdae10f9427ace6fd9e9e430d05;hp=ef1464dcab714766f0addf4d6fb0261e04cec53b;hpb=edd0e551e6041f7596c880cdaef13dfa67eff6b5;p=avr_bc100.git diff --git a/BaseTinyFirmware/GCC/statefunc.c b/BaseTinyFirmware/GCC/statefunc.c index ef1464d..e0e2659 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 ); + } } } @@ -117,17 +123,17 @@ unsigned char Initialize(unsigned char inp) sei(); // Attempt to get ADC-readings (also gets RID-data) from both batteries. - for (i = 0; i < 2; i++) { + for (i = 0; i < BATCONN; i++) { EnableBattery(i); ADC_Wait(); BatteryStatusRefresh(); } DisableBatteries(); - + BattActive = 0; // We have to start somewhere.. ErrorFlags = 0; - + // Init complete! Go to ST_BATCON next. return(ST_BATCON); } @@ -153,22 +159,22 @@ 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); } // Get ADC-readings, try to read EPROM, and start prequalification // of any uncharged battery. - for (i = 0; i < 2; i++) { + for (i = 0; i < BATCONN; i++) { if (eeprom_read_byte(&BattControl[i]) & BIT_BATTERY_ENABLED) { EnableBattery(i); ADC_Wait(); @@ -222,50 +228,82 @@ unsigned char Sleep(unsigned char inp) // If any batteries need charging, go to ST_BATCON. // Otherwise, keep sleeping. - for (i = 0; i < 2; i++) { + for (i = 0; i < BATCONN; i++) { EnableBattery(i); ADC_Wait(); - if ((BatteryStatusRefresh()) && (!BattData.Charged)) { - return(ST_BATCON); + if ( BatteryStatusRefresh() ) { + if ( !BattData.Charged ) { + 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<