fix case name, improve syntax for linker option
[avr_bc100.git] / BaseTinyFirmware / GCC / LIIONcharge.c
1 /* This file has been prepared for Doxygen automatic documentation generation.*/\r
2 /*! \file *********************************************************************\r
3  *\r
4  * \brief\r
5  *      Charge state function for Li-Ion batteries\r
6  *\r
7  *      Contains the charge state function, in which the Li-Ion charging\r
8  *      algorithm is, plus the associated functions.\r
9  *\r
10  * \par Application note:\r
11  *      AVR458: Charging Li-Ion Batteries with BC100\r
12  *\r
13  * \par Documentation\r
14  *      For comprehensive code documentation, supported compilers, compiler \r
15  *      settings and supported devices see readme.html\r
16  *\r
17  * \author\r
18  *      Atmel Corporation: http://www.atmel.com \n\r
19  *      Support email: avr@atmel.com\r
20  *\r
21  * \r
22  * $Name$\r
23  * $Revision: 2261 $\r
24  * $RCSfile$\r
25  * $URL: http://svn.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/tag/20070904_release_1.0/code/IAR/LIIONcharge.c $\r
26  * $Date: 2007-08-10 09:28:35 +0200 (fr, 10 aug 2007) $\n\r
27  ******************************************************************************/\r
28 \r
29 #include <avr/io.h>\r
30 \r
31 #include "enums.h"\r
32 #include "structs.h"\r
33 \r
34 #include "battery.h"\r
35 #include "charge.h"\r
36 #include "chargefunc.h"\r
37 #include "main.h"\r
38 #include "menu.h"\r
39 #include "LIIONspecs.h"\r
40 #include "PWM.h"\r
41 #include "time.h"\r
42 \r
43 #ifndef LIION\r
44 #error LIION not defined in main.h!\r
45 #endif // LIION\r
46 \r
47 \r
48 //******************************************************************************\r
49 // Functions\r
50 //******************************************************************************\r
51 /*! \brief Controls the charging.\r
52  *\r
53  * This function contains the charging algorithm itself, divided into stages.\n\r
54  * For each stage the PWM may be started/stopped, and the timer, \r
55  * halt-requirements and charge parameters may be set.\n\r
56  * The charging functions return whatever state is next, and as long as no\r
57  * errors occur this is the next charging stage.\r
58  *\r
59  * \note If more stages are needed simply define more states in menu.h, include\r
60  * them in \ref menu_state[] in menu.c, then add the cases to this function.\r
61  *\r
62  * \note This algorithm is for Li-Ion batteries.\r
63  */\r
64 unsigned char Charge(unsigned char inp)\r
65 {\r
66         unsigned char NextState;\r
67 \r
68         switch (CurrentState)   {\r
69         case ST_PREQUAL:  // First step is prequalification.\r
70                 \r
71                 // Charge with the defined prequalifiction-current, and if no errors\r
72                 // occur return ST_CCURRENT as the next state.\r
73                 ChargeParameters.Current = BAT_CURRENT_PREQUAL;\r
74                 ChargeParameters.NextState = ST_CCURRENT;\r
75                 \r
76                 // We want charging to halt if voltage reaches a limit or time runs out.\r
77                 // In case of timeout the battery will be flagged as exhausted, and an\r
78                 // error will be flagged.\r
79                 HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME | \r
80                                             HALT_FLAG_EXHAUSTION);\r
81                 \r
82                 // Set the maximum temperature and charge voltage limit.\r
83                 HaltParameters.TemperatureMax = BAT_TEMPERATURE_MAX;\r
84                 HaltParameters.TemperatureMin = BAT_TEMPERATURE_MIN;\r
85                 HaltParameters.VoltageMax = BAT_VOLTAGE_PREQUAL;\r
86 \r
87                 // Start PWM output and charging timer first.\r
88                 PWM_Start();\r
89                 Time_Set(TIMER_CHG, BAT_TIME_PREQUAL, 0, 0);\r
90 \r
91                 // Call the constant current charging-function.\r
92                 // If all goes well, we will get ST_CCURRENT in return.\r
93                 NextState = ConstantCurrent();\r
94         break;\r
95         \r
96 \r
97         case ST_CCURRENT:  // Second step is constant current charging.\r
98                 \r
99                 // Set charging timer to the battery's maximum charge time.\r
100                 Time_Set(TIMER_CHG,BattData.MaxTime,0,0);\r
101 \r
102                 // Charge at the battery's maximum current, go to ST_CVOLTAGE next.\r
103                 ChargeParameters.Current = BattData.MaxCurrent;\r
104                 ChargeParameters.NextState = ST_CVOLTAGE;\r
105 \r
106 \r
107                 // Charge until the defined BatChargeVoltage is reached.\r
108                 HaltParameters.VoltageMax = BAT_VOLTAGE_MAX;\r
109                 \r
110                 // Start charging using constant current.\r
111                 NextState = ConstantCurrent();\r
112         break;\r
113 \r
114 \r
115         case ST_CVOLTAGE:  // Third step is constant voltage charging.\r
116                 \r
117                 // Charge with the defined charge-voltage, go to ST_ENDCHARGE next.\r
118                 ChargeParameters.Voltage = BAT_VOLTAGE_MAX;\r
119                 ChargeParameters.NextState = ST_ENDCHARGE;\r
120                 \r
121                 // We want charging to halt if temperature rises too high, if current\r
122                 // sinks below limit, or time runs out. Also, flag error if temperature\r
123                 // limit is reached. Timeout doesn't mean anything is wrong at this point.\r
124                 HaltParameters.HaltFlags = (HALT_CURRENT_MIN | HALT_TIME);\r
125                 \r
126                 HaltParameters.CurrentMin = BattData.MinCurrent;\r
127                 \r
128                 // Start charging using constant voltage. We will continue on the \r
129                 // timer started in ST_CCURRENT.\r
130                 NextState = ConstantVoltage(); \r
131         break;\r
132 \r
133 \r
134         case ST_ENDCHARGE:  // Charging is done!\r
135 \r
136                 PWM_Stop();\r
137                 BattData.Charged = TRUE;\r
138                 \r
139                 // If the other battery is enabled go to ST_BATCON, otherwise\r
140                 // go to ST_SLEEP.\r
141                 if (eeprom_read_byte(&BattControl[(BattActive+1)%2]) & BIT_BATTERY_ENABLED) {\r
142                         NextState = ST_BATCON;\r
143                 } else {\r
144                         NextState = ST_SLEEP;\r
145                 }               \r
146         break;\r
147 \r
148 \r
149         default:  // Shouldn't end up here. Reinitialize for safety.\r
150                 NextState = ST_INIT;\r
151                 break;\r
152         }\r
153 \r
154         // Return the next state.\r
155         return(NextState);\r
156 }\r