X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=BaseTinyFirmware%2FGCC%2Fstatefunc.c;h=e0e265901ee3cb064da476f907b735a96c16e890;hb=65612c4c7df34cdae10f9427ace6fd9e9e430d05;hp=9df2b5c93ea086c8c0d2611d1f8cec4141e571d5;hpb=5b95e754a4af80c7389486ee874ac07c166a0867;p=avr_bc100.git diff --git a/BaseTinyFirmware/GCC/statefunc.c b/BaseTinyFirmware/GCC/statefunc.c index 9df2b5c..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 ); + } } } @@ -113,18 +119,21 @@ unsigned char Initialize(unsigned char inp) ADC_Init(); Time_Init(); + // Enable interrupts + 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); } @@ -150,23 +159,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 (!(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++) { - if (eeprom_read_byte(&BattControl[i]) & BIT_BATTERY_ENABLED) { + for (i = 0; i < BATCONN; i++) { + if (eeprom_read_byte(&BattControl[i]) & BIT_BATTERY_ENABLED) { EnableBattery(i); ADC_Wait(); @@ -174,7 +183,7 @@ unsigned char BatteryControl(unsigned char inp) if (!BattData.Charged) { BatteryDataRefresh(); - return(ST_PREQUAL); + return(ST_PREQUAL); } } } @@ -219,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); - + } while (ADCS.Flag == FALSE); + WDTCR = (1< check load and jumper J405 and J406. - if (abs(ADCS.IBAT) > 100) { + if (abs(adcs_IBAT_tmp) > 100) { PWM_Stop(); return(FALSE); } @@ -414,11 +467,11 @@ unsigned char JumperCheck(void) PWM_Stop(); return(FALSE); } - - // Wait for ADC conversions to complete + + // Wait for ADC conversions to complete ADC_Wait(); } while (Time_Left(TIMER_GEN)); - + // If we end up here, the measurements took too long. PWM_Stop();