1 /* This file has been prepared for Doxygen automatic documentation generation.*/
\r
2 /*! \file *********************************************************************
\r
7 * Contains the functions related to the states defined in menu.h.\n
\r
8 * Also contains related functions, i.e. for checking jumpers, setting
\r
9 * error flags and "dozing".
\r
11 * \note The state function Charge() is in a separate file since it
\r
12 * should easily be changed with regard to battery type.
\r
14 * \par Application note:
\r
15 * AVR458: Charging Li-Ion Batteries with BC100 \n
\r
16 * AVR463: Charging NiMH Batteries with BC100
\r
18 * \par Documentation
\r
19 * For comprehensive code documentation, supported compilers, compiler
\r
20 * settings and supported devices see readme.html
\r
23 * Atmel Corporation: http://www.atmel.com \n
\r
24 * Support email: avr@atmel.com
\r
30 * $URL: http://svn.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/tag/20070904_release_1.0/code/IAR/statefunc.c $
\r
31 * $Date: 2007-08-23 12:55:51 +0200 (to, 23 aug 2007) $\n
\r
32 ******************************************************************************/
\r
35 #include <avr/interrupt.h>
\r
36 #include <avr/wdt.h>
\r
37 #include <avr/wdt.h>
\r
38 #include <avr/sleep.h>
\r
39 #include <avr/eeprom.h>
\r
43 #include "structs.h"
\r
47 #include "statefunc.h"
\r
48 #include "battery.h"
\r
58 //******************************************************************************
\r
60 //******************************************************************************
\r
61 unsigned char ErrorFlags; //!< \brief Holds error flags.
\r
62 //!< \note See statefunc.h for definitions of flags.
\r
64 //! \brief Holds the state in which latest error flag was set.
\r
65 //! \note See menu.h for definitions of states.
\r
66 unsigned char ErrorState;
\r
69 //******************************************************************************
\r
71 //******************************************************************************
\r
72 /*! \brief Initialization
\r
74 * - Sets the system clock prescaler to 1 (run at 8 MHz)
\r
75 * - Initializes the one-wire interface
\r
76 * - Clears on-chip EEPROM
\r
77 * - Sets battery enable pins as outputs, then disables batteries
\r
78 * - Initializes SPI according to \ref SPIMODE
\r
80 * - Initializes timers
\r
81 * - Reads battery data from both battery inputs (via ADC)
\r
82 * - Disables batteries again
\r
83 * - Sets battery A as the current one (\ref BattActive = 0)
\r
84 * - Clears ErrorFlags
\r
86 * \param inp Not used.
\r
88 * \retval ST_BATCON Next state in the sequence.
\r
90 unsigned char Initialize(unsigned char inp)
\r
92 unsigned char i, page;
\r
94 // Disable interrupts while setting prescaler.
\r
97 CLKPR = (1<<CLKPCE); // Enable CLKPS bit modification.
\r
98 CLKPR = 0; // Set prescaler 1 => 8 MHz clock frequency.
\r
100 // Init 1-Wire(R) interface.
\r
103 // Clear on-chip EEPROM.
\r
104 for (page = 0; page < 4; page++) {
\r
105 for (i = 0; i < 32; i++) {
\r
106 eeprom_write_byte(&BattEEPROM[page][i], 0);
\r
110 DDRB = (1<<PB4) | (1<<PB5); // Set battery enable pins as outputs.
\r
111 DisableBatteries();
\r
116 // Enable interrupts
\r
119 // Attempt to get ADC-readings (also gets RID-data) from both batteries.
\r
120 for (i = 0; i < 2; i++) {
\r
123 BatteryStatusRefresh();
\r
126 DisableBatteries();
\r
128 BattActive = 0; // We have to start somewhere..
\r
131 // Init complete! Go to ST_BATCON next.
\r
136 /*! \brief Tests jumper settings and batteries, starts charging if necessary.
\r
138 * First, JumperCheck() is called. If successful, the function checks if any
\r
139 * valid batteries are connected and attempts to charge these, if necessary.\n
\r
140 * If no charging is necessary, the charger goes to ST_SLEEP next.\n
\r
141 * ST_ERROR is next if either JumperCheck() fails or there are no valid
\r
142 * batteries. In this last case, the error is also flagged.
\r
144 * \param inp Not used.
\r
146 * \retval ST_ERROR Next state if either the jumper check failed, or there are
\r
147 * no valid batteries.
\r
148 * \retval ST_PREQUAL Next state if a battery is found to enabled and not fully
\r
150 * \retval ST_SLEEP Next state if battery/batteries are enabled and fully
\r
153 unsigned char BatteryControl(unsigned char inp)
\r
157 // Make sure ADC inputs are configured properly! (Will disables batteries.)
\r
158 if (!JumperCheck()) {
\r
159 return(ST_ERROR); // Error. Exit before damage is done!
\r
162 // If neither battery is valid, flag error and go to error state
\r
163 if (!(eeprom_read_byte(&BattControl[0]) & BIT_BATTERY_ENABLED) && (!eeprom_read_byte(&BattControl[1]) & BIT_BATTERY_ENABLED)) {
\r
164 SetErrorFlag(ERR_NO_BATTERIES_ENABLED);
\r
169 // Get ADC-readings, try to read EPROM, and start prequalification
\r
170 // of any uncharged battery.
\r
171 for (i = 0; i < 2; i++) {
\r
172 if (eeprom_read_byte(&BattControl[i]) & BIT_BATTERY_ENABLED) {
\r
176 if (BatteryStatusRefresh()) {
\r
177 if (!BattData.Charged) {
\r
178 BatteryDataRefresh();
\r
180 return(ST_PREQUAL);
\r
186 // If we end up here, one or two batteries are found and fully charged.
\r
187 // Disconnect, so we don't drain them, and go to sleep.
\r
188 DisableBatteries();
\r
194 /*! \brief Start running on batteries
\r
196 * \todo Run on batteries, if battery voltage high enough.
\r
197 * \todo Jump here when mains voltage drops below threshold
\r
200 unsigned char Discharge(unsigned char inp)
\r
202 return(ST_BATCON); // Supply voltage restored, start charging
\r
206 /*! \brief Sleeps until either battery needs charging
\r
208 * Calls Doze(), then refreshes the status for both batteries on wakeup. If
\r
209 * connected batteries are both charged, the function will loop. If not, it's
\r
210 * back to ST_BATCON.
\r
212 * \param inp Not used.
\r
214 * \retval ST_BATCON Next state if a connected battery isn't fully charged.
\r
216 unsigned char Sleep(unsigned char inp)
\r
221 Doze(); // Take a nap (~8 seconds).
\r
223 // If any batteries need charging, go to ST_BATCON.
\r
224 // Otherwise, keep sleeping.
\r
225 for (i = 0; i < 2; i++) {
\r
228 if ((BatteryStatusRefresh()) && (!BattData.Charged)) {
\r
233 DisableBatteries(); // Disable both batteries before Doze()!
\r
238 /*! \brief Doze off for approx. 8 seconds (Vcc = 5 V)
\r
240 * Waits for ADC-cycles to complete, disables the ADC, then sleeps for
\r
241 * approx. 8 seconds (Vcc = 5 V) using the watchdog timer.
\r
242 * On wakeup, ADC is re-enabled.
\r
246 // Wait for this ADC cycle to complete, then halt after the next one.
\r
252 } while (ADCS.Flag == FALSE);
\r
254 WDTCR = (1<<WDP3)|(1<<WDP0); // 8.0 seconds at 5 volts VCC.
\r
255 WDTCR |= (1<<WDIF)|(1<<WDIE)|(1<<WDE); // Clear flag and enable watchdog.
\r
256 MCUCR |= (1<<SE) | (1<<SM1)|(1<<SM0); // Sleep enable, mode = standby.
\r
257 sleep_cpu(); // Go to sleep, wake up by WDT.
\r
259 wdt_reset(); // Clear watchdog reset flag.
\r
260 MCUSR &= ~(1<<WDRF);
\r
261 WDTCR |= (1<<WDCE)|(1<<WDE); // Watchdog change enable.
\r
262 WDTCR = 0; // Turn off watchdog.
\r
264 ADCS.Halt = FALSE; // Enable consecutive runs of ADC.
\r
265 ADCSRA |= (1<<ADEN)|(1<<ADSC); // Enable ADC & start conversion.
\r
267 // Wait for this cycle to complete.
\r
272 /*! \brief Handles errors
\r
274 * Stops PWM output and disables batteries. The function then goes into a loop
\r
275 * that starts with a call to Doze(), then attempts to handle each error. The
\r
276 * loop will reiterate until all flags are cleared.\n
\r
277 * The charger will reinitialize after this.
\r
279 * Jumper errors are handled by clearing the flag, then calling JumperCheck().
\r
280 * If unsuccessful, the error flag will now have been set again.\n
\r
282 * If there are no valid batteries, the loop will simply reiterate until a
\r
283 * valid battery is found. The error flag will then be cleared.\n
\r
285 * In the case of PWM controller or battery temperature errors, the error
\r
286 * flag is simply cleared. This is because the problem may have gone away during
\r
287 * Doze(), or after reinitializing.\n
\r
289 * If a battery is exhausted, we clear its exhausted-flag in \ref BattData,
\r
290 * and change batteries before clearing the error flag.
\r
292 * \param inp Not used.
\r
294 unsigned char Error(unsigned char inp)
\r
298 PWM_Stop(); // Stop charging.
\r
299 DisableBatteries(); // Disable all loads.
\r
302 Doze(); // Take a nap.
\r
304 // For each bit in ErrorFlags, starting with LSB, handle
\r
305 // associated error, if the flag is set.
\r
306 for (i = 0x01; i!=0; i<<=1) {
\r
307 if(i & ErrorFlags) {
\r
310 case ERR_JUMPER_MISMATCH:
\r
311 // Clear flag & recheck.
\r
317 case ERR_NO_BATTERIES_ENABLED:
\r
318 // Clear if any battery gets enabled.
\r
319 if ((eeprom_read_byte(&BattControl[0]) & BIT_BATTERY_ENABLED) || (eeprom_read_byte(&BattControl[1]) & BIT_BATTERY_ENABLED)) {
\r
325 case ERR_PWM_CONTROL:
\r
331 case ERR_BATTERY_TEMPERATURE:
\r
337 case ERR_BATTERY_EXHAUSTED:
\r
338 // Try the other battery.
\r
339 BattData.Exhausted = FALSE;
\r
340 BattActive = (BattActive + 1) % 2;
\r
350 } while (ErrorFlags);
\r
356 /*! \brief Sets the specified error-flag and saves the current state
\r
358 * Updates \ref ErrorFlags and \ref ErrorState.
\r
360 * \note Error flags are specified in statefunc.h.
\r
362 * \param Flag Specifies what error to flag.
\r
364 void SetErrorFlag(unsigned char Flag)
\r
366 ErrorFlags |= Flag;
\r
367 ErrorState = CurrentState;
\r
371 /*! \brief Checks on-board jumpers.
\r
373 * Checks on-board jumpers by disconnecting all loads, engaging the PWM and
\r
374 * increasing the duty cycle until conditioned output voltage equals conditioned
\r
375 * input voltage. At low PWM duty and no load buck output should be zero and,
\r
376 * when increasing PWM duty, should quickly jump to steady state output roughly
\r
377 * equal to input voltage. Will disable and leave disabled all batteries.
\r
379 * \retval FALSE If jumper or load mismatch.
\r
380 * \retval TRUE If everything OK.
\r
382 unsigned char JumperCheck(void)
\r
384 unsigned char sreg_saved;
\r
385 unsigned int adcs_rawVBAT_tmp, adcs_VIN_tmp, adcs_VBAT_tmp;
\r
386 signed int adcs_IBAT_tmp;
\r
388 DisableBatteries(); // Disconnect, or loads may be destroyed!
\r
390 PWM_Start(); // Start PWM (controls the buck charger).
\r
392 // Use general timer: shouldn't take longer than (6 x 255) / 2500 ~= 0.62s.
\r
393 Time_Set(TIMER_GEN,0,1,0);
\r
396 // If the PWM output voltage saturates the ADC, stop PWM output and
\r
397 // report a failure.
\r
400 adcs_rawVBAT_tmp = ADCS.rawVBAT;
\r
401 adcs_VBAT_tmp = ADCS.VBAT;
\r
402 adcs_VIN_tmp = ADCS.VIN;
\r
403 adcs_IBAT_tmp = ADCS.IBAT;
\r
406 if (adcs_rawVBAT_tmp == 1023) {
\r
411 // If the absolute difference between measured (VIN - VBAT) and the
\r
412 // typical value are below our set maximum, everything is OK.
\r
413 if (abs((signed int)(adcs_VIN_tmp - VIN_VBAT_DIFF_TYP - adcs_VBAT_tmp)) <
\r
414 VIN_VBAT_DIFF_MAX ) {
\r
420 // Charge current is too high -> check load and jumper J405 and J406.
\r
421 if (abs(adcs_IBAT_tmp) > 100) {
\r
426 // If the PWM output can't be increased high enough -> check jumpers
\r
427 // J400-J404, J407 and J408.
\r
428 if (!PWM_IncrementDutyCycle()) {
\r
433 // Wait for ADC conversions to complete
\r
435 } while (Time_Left(TIMER_GEN));
\r
438 // If we end up here, the measurements took too long.
\r