fix case name, improve syntax for linker option
[avr_bc100.git] / BaseTinyFirmware / GCC / NIMHcharge.c
diff --git a/BaseTinyFirmware/GCC/NIMHcharge.c b/BaseTinyFirmware/GCC/NIMHcharge.c
new file mode 100644 (file)
index 0000000..1a436cd
--- /dev/null
@@ -0,0 +1,179 @@
+/* This file has been prepared for Doxygen automatic documentation generation.*/\r
+/*! \file *********************************************************************\r
+ *\r
+ * \brief\r
+ *      Charge state function for NiMH batteries\r
+ *\r
+ *      Contains the charge state function, in which the NiMH charging\r
+ *      algorithm is, plus the associated functions.\r
+ *\r
+ * \par Application note:\r
+ *      AVR463: Charging NiMH Batteries with BC100 \n\r
+ *\r
+ * \par Documentation\r
+ *      For comprehensive code documentation, supported compilers, compiler \r
+ *      settings and supported devices see readme.html\r
+ *\r
+ * \author\r
+ *      Atmel Corporation: http://www.atmel.com \n\r
+ *      Support email: avr@atmel.com\r
+ *\r
+ * \r
+ * $Name$\r
+ * $Revision: 2255 $\r
+ * $RCSfile$\r
+ * $URL: http://svn.norway.atmel.com/AppsAVR8/avr463_Charging_NiMH_Batteries_with_BC100/trunk/code/IAR/NIMHcharge.c $\r
+ * $Date: 2007-08-09 14:47:58 +0200 (to, 09 aug 2007) $\n\r
+ ******************************************************************************/\r
+\r
+#include <avr/io.h>\r
+\r
+#include "enums.h"\r
+#include "structs.h"\r
+\r
+#include "battery.h"\r
+#include "charge.h"\r
+#include "chargefunc.h"\r
+#include "main.h"\r
+#include "menu.h"\r
+#include "NIMHspecs.h"\r
+#include "PWM.h"\r
+#include "time.h"\r
+\r
+#ifndef NIMH\r
+#error NIMH not defined in main.h!\r
+#endif // NIMH\r
+\r
+\r
+//******************************************************************************\r
+// Functions\r
+//******************************************************************************\r
+/*! \brief Controls the charging.\r
+ *\r
+ * This function contains the charging algorithm itself, divided into stages.\n\r
+ * For each stage the PWM may be started/stopped, and the timer, \r
+ * halt-requirements and charge parameters may be set.\n\r
+ * The charging functions return whatever state is next, and as long as no\r
+ * errors occur this is the next charging stage.\r
+ *\r
+ * \note If more stages are needed simply define more states in menu.h, include\r
+ * them in \ref menu_state[] in menu.c, then add the cases to this function.\r
+ *\r
+ * \note This algorithm is for NiMH batteries.\r
+ */\r
+unsigned char Charge(unsigned char inp)\r
+{\r
+       unsigned char NextState;\r
+\r
+       switch (CurrentState) {\r
+       // First stage is a prequalification. Attempt to charge battery to 1 V,\r
+       // using a 0.1 C current, within 2 minutes.\r
+       // If this fails, the battery is likely damaged.\r
+       // If it succeeds, start a fast charge.\r
+       case ST_PREQUAL:\r
+               \r
+               // Set up charge current and next state.\r
+               ChargeParameters.Current = BattData.Capacity / 10;\r
+               ChargeParameters.NextState = ST_FASTCHARGE;\r
+               \r
+               // Halt charge on voltage limit or timeout.\r
+               // Timeout means battery exhaustion.\r
+               HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME |\r
+                                           HALT_FLAG_EXHAUSTION);\r
+               \r
+               // Set up voltage limit and temperature limits.\r
+               HaltParameters.VoltageMax = BAT_VOLTAGE_PREQUAL;\r
+               HaltParameters.TemperatureMin = BAT_TEMPERATURE_MIN;\r
+               HaltParameters.TemperatureMax = 35;\r
+               \r
+               // Reset temperature measurement for HaltNow().\r
+               HaltParameters.LastNTC = 0;\r
+               \r
+               // Start PWM and charge timer before calling the charge function.\r
+               PWM_Start();\r
+               Time_Set(TIMER_CHG, BAT_TIME_PREQUAL, 0, 0);\r
+               \r
+               // Call charge function, get next state.\r
+               NextState = ConstantCurrent();\r
+       break;\r
+\r
+\r
+       // Second stage is a fast charge. Charge at 1.0 C for at most 1.5 hours,\r
+       // until either rate of temperature increase or voltage reaches limit, or\r
+       // the voltage drops sufficiently.\r
+       // Timeout doesn't mean battery exhaustion now.\r
+       case ST_FASTCHARGE:\r
+\r
+               // Set up charge current and next state.\r
+               ChargeParameters.Current = BattData.Capacity;\r
+               ChargeParameters.NextState = ST_LOWRATECHARGE;\r
+               \r
+               // Halt charge on voltage limit, timeout, voltage drop or rate of \r
+               // temperature increase.\r
+               HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME |\r
+                                           HALT_VOLTAGE_DROP | HALT_TEMPERATURE_RISE);\r
+               \r
+               // Set up limits for voltage, voltage drop, temperature and rate of \r
+               // temperature increase (1 degree C per minute).\r
+               HaltParameters.VoltageMax = BAT_VOLTAGE_MAX;\r
+               HaltParameters.VoltageDrop = BAT_VOLTAGE_DROP;\r
+               HaltParameters.TemperatureMax = BAT_TEMPERATURE_MAX;\r
+               HaltParameters.TemperatureRise = 1;\r
+               \r
+               // Reset maximum voltage measurement for HaltNow().\r
+               HaltParameters.VBATMax = 0;\r
+\r
+               // Start timer, PWM should still be running.\r
+               Time_Set(TIMER_CHG, BattData.MaxTime, 0, 0);\r
+\r
+               // Call charge function, get next state.\r
+               NextState = ConstantCurrent();\r
+       break;\r
+\r
+\r
+       // Last stage is a trickle charge. Charge at 0.1 C for at most 30 minutes,\r
+       // until either rate of temperature increase or voltage reaches limit.\r
+       case ST_LOWRATECHARGE:\r
+               \r
+               // Set up charge current and next state.\r
+               ChargeParameters.Current = BattData.Capacity / 10;\r
+               ChargeParameters.NextState = ST_ENDCHARGE;\r
+               \r
+               // Halt charge on voltage limit, timeout or temperature rise.\r
+               // Use the same requirements as during the last stage (ST_FASTCHARGE).\r
+               HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME |\r
+                                           HALT_TEMPERATURE_RISE);\r
+\r
+               // Start timer, 30 minutes.\r
+               Time_Set(TIMER_CHG, 30, 0, 0);\r
+               \r
+               // Call charge function, get next state.\r
+               NextState = ConstantCurrent();\r
+       break;\r
+\r
+\r
+       // Charging is done!\r
+       case ST_ENDCHARGE:\r
+\r
+               // Stop the PWM output and flag battery as charged.\r
+               PWM_Stop();\r
+               BattData.Charged = TRUE;\r
+               \r
+               // If the other battery is enabled go to ST_BATCON, otherwise\r
+               // go to ST_SLEEP.\r
+               if (eeprom_read_byte(&BattControl[(BattActive+1)%2]) & BIT_BATTERY_ENABLED) {\r
+                       NextState = ST_BATCON;\r
+               } else {\r
+                       NextState = ST_SLEEP;\r
+               }               \r
+       break;\r
+\r
+\r
+       default:  // Shouldn't end up here. Reinitialize for safety.\r
+               NextState = ST_INIT;\r
+               break;\r
+       }\r
+\r
+       // Return the next state to main().\r
+       return(NextState);\r
+}\r