fix case name, improve syntax for linker option
authorKevin Rosenberg <kevin@rosenberg.net>
Sun, 30 Mar 2008 15:08:18 +0000 (09:08 -0600)
committerKevin Rosenberg <kevin@rosenberg.net>
Sun, 30 Mar 2008 15:08:18 +0000 (09:08 -0600)
BaseTinyFirmware/GCC/LIIONcharge.c [new file with mode: 0644]
BaseTinyFirmware/GCC/NIMHcharge.c [new file with mode: 0644]
BaseTinyFirmware/GCC/avr458/Makefile
BaseTinyFirmware/GCC/avr463/Makefile
BaseTinyFirmware/GCC/liioncharge.c [deleted file]
BaseTinyFirmware/GCC/nimhcharge.c [deleted file]

diff --git a/BaseTinyFirmware/GCC/LIIONcharge.c b/BaseTinyFirmware/GCC/LIIONcharge.c
new file mode 100644 (file)
index 0000000..0ce5427
--- /dev/null
@@ -0,0 +1,156 @@
+/* This file has been prepared for Doxygen automatic documentation generation.*/\r
+/*! \file *********************************************************************\r
+ *\r
+ * \brief\r
+ *      Charge state function for Li-Ion batteries\r
+ *\r
+ *      Contains the charge state function, in which the Li-Ion charging\r
+ *      algorithm is, plus the associated functions.\r
+ *\r
+ * \par Application note:\r
+ *      AVR458: Charging Li-Ion Batteries with BC100\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: 2261 $\r
+ * $RCSfile$\r
+ * $URL: http://svn.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/tag/20070904_release_1.0/code/IAR/LIIONcharge.c $\r
+ * $Date: 2007-08-10 09:28:35 +0200 (fr, 10 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 "LIIONspecs.h"\r
+#include "PWM.h"\r
+#include "time.h"\r
+\r
+#ifndef LIION\r
+#error LIION not defined in main.h!\r
+#endif // LIION\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 Li-Ion batteries.\r
+ */\r
+unsigned char Charge(unsigned char inp)\r
+{\r
+       unsigned char NextState;\r
+\r
+       switch (CurrentState)   {\r
+       case ST_PREQUAL:  // First step is prequalification.\r
+               \r
+               // Charge with the defined prequalifiction-current, and if no errors\r
+               // occur return ST_CCURRENT as the next state.\r
+               ChargeParameters.Current = BAT_CURRENT_PREQUAL;\r
+               ChargeParameters.NextState = ST_CCURRENT;\r
+               \r
+               // We want charging to halt if voltage reaches a limit or time runs out.\r
+               // In case of timeout the battery will be flagged as exhausted, and an\r
+               // error will be flagged.\r
+               HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME | \r
+                                           HALT_FLAG_EXHAUSTION);\r
+               \r
+               // Set the maximum temperature and charge voltage limit.\r
+               HaltParameters.TemperatureMax = BAT_TEMPERATURE_MAX;\r
+               HaltParameters.TemperatureMin = BAT_TEMPERATURE_MIN;\r
+               HaltParameters.VoltageMax = BAT_VOLTAGE_PREQUAL;\r
+\r
+               // Start PWM output and charging timer first.\r
+               PWM_Start();\r
+               Time_Set(TIMER_CHG, BAT_TIME_PREQUAL, 0, 0);\r
+\r
+               // Call the constant current charging-function.\r
+               // If all goes well, we will get ST_CCURRENT in return.\r
+               NextState = ConstantCurrent();\r
+       break;\r
+       \r
+\r
+       case ST_CCURRENT:  // Second step is constant current charging.\r
+               \r
+               // Set charging timer to the battery's maximum charge time.\r
+               Time_Set(TIMER_CHG,BattData.MaxTime,0,0);\r
+\r
+               // Charge at the battery's maximum current, go to ST_CVOLTAGE next.\r
+               ChargeParameters.Current = BattData.MaxCurrent;\r
+               ChargeParameters.NextState = ST_CVOLTAGE;\r
+\r
+\r
+               // Charge until the defined BatChargeVoltage is reached.\r
+               HaltParameters.VoltageMax = BAT_VOLTAGE_MAX;\r
+               \r
+               // Start charging using constant current.\r
+               NextState = ConstantCurrent();\r
+       break;\r
+\r
+\r
+       case ST_CVOLTAGE:  // Third step is constant voltage charging.\r
+               \r
+               // Charge with the defined charge-voltage, go to ST_ENDCHARGE next.\r
+               ChargeParameters.Voltage = BAT_VOLTAGE_MAX;\r
+               ChargeParameters.NextState = ST_ENDCHARGE;\r
+               \r
+               // We want charging to halt if temperature rises too high, if current\r
+               // sinks below limit, or time runs out. Also, flag error if temperature\r
+               // limit is reached. Timeout doesn't mean anything is wrong at this point.\r
+               HaltParameters.HaltFlags = (HALT_CURRENT_MIN | HALT_TIME);\r
+               \r
+               HaltParameters.CurrentMin = BattData.MinCurrent;\r
+               \r
+               // Start charging using constant voltage. We will continue on the \r
+               // timer started in ST_CCURRENT.\r
+               NextState = ConstantVoltage(); \r
+       break;\r
+\r
+\r
+       case ST_ENDCHARGE:  // Charging is done!\r
+\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.\r
+       return(NextState);\r
+}\r
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
index d74f9d4b2745d0690867f24d0d3744fdaf887f90..e457082b52c3424001b54871b5117337e657277f 100644 (file)
@@ -6,7 +6,7 @@
 PROJECT = avr458\r
 MCU = attiny861\r
 TARGET = $(PROJECT).elf\r
-CC = avr-gcc.exe\r
+CC = avr-gcc\r
 \r
 ## Options common to compile, link and assembly rules\r
 COMMON = -mmcu=$(MCU)\r
@@ -23,7 +23,7 @@ ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2
 \r
 ## Linker flags\r
 LDFLAGS = $(COMMON)\r
-LDFLAGS +=  -Wl,-Map=$(PROJECT).map -Wl,-relax\r
+LDFLAGS +=  -Wl,-Map=$(PROJECT).map -Wl,--relax\r
 \r
 \r
 ## Intel Hex file production flags\r
index 826b34552aaca3dd8b62852a644d1af88bd738b9..736b3f8d9746a6c12bff449c98382d93ba8ea437 100644 (file)
@@ -6,7 +6,7 @@
 PROJECT = avr463\r
 MCU = attiny861\r
 TARGET = $(PROJECT).elf\r
-CC = avr-gcc.exe\r
+CC = avr-gcc\r
 \r
 ## Options common to compile, link and assembly rules\r
 COMMON = -mmcu=$(MCU)\r
@@ -23,7 +23,7 @@ ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2
 \r
 ## Linker flags\r
 LDFLAGS = $(COMMON)\r
-LDFLAGS +=  -Wl,-Map=$(PROJECT).map -Wl,-relax\r
+LDFLAGS +=  -Wl,-Map=$(PROJECT).map -Wl,--relax\r
 \r
 \r
 ## Intel Hex file production flags\r
diff --git a/BaseTinyFirmware/GCC/liioncharge.c b/BaseTinyFirmware/GCC/liioncharge.c
deleted file mode 100644 (file)
index 0ce5427..0000000
+++ /dev/null
@@ -1,156 +0,0 @@
-/* This file has been prepared for Doxygen automatic documentation generation.*/\r
-/*! \file *********************************************************************\r
- *\r
- * \brief\r
- *      Charge state function for Li-Ion batteries\r
- *\r
- *      Contains the charge state function, in which the Li-Ion charging\r
- *      algorithm is, plus the associated functions.\r
- *\r
- * \par Application note:\r
- *      AVR458: Charging Li-Ion Batteries with BC100\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: 2261 $\r
- * $RCSfile$\r
- * $URL: http://svn.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/tag/20070904_release_1.0/code/IAR/LIIONcharge.c $\r
- * $Date: 2007-08-10 09:28:35 +0200 (fr, 10 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 "LIIONspecs.h"\r
-#include "PWM.h"\r
-#include "time.h"\r
-\r
-#ifndef LIION\r
-#error LIION not defined in main.h!\r
-#endif // LIION\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 Li-Ion batteries.\r
- */\r
-unsigned char Charge(unsigned char inp)\r
-{\r
-       unsigned char NextState;\r
-\r
-       switch (CurrentState)   {\r
-       case ST_PREQUAL:  // First step is prequalification.\r
-               \r
-               // Charge with the defined prequalifiction-current, and if no errors\r
-               // occur return ST_CCURRENT as the next state.\r
-               ChargeParameters.Current = BAT_CURRENT_PREQUAL;\r
-               ChargeParameters.NextState = ST_CCURRENT;\r
-               \r
-               // We want charging to halt if voltage reaches a limit or time runs out.\r
-               // In case of timeout the battery will be flagged as exhausted, and an\r
-               // error will be flagged.\r
-               HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME | \r
-                                           HALT_FLAG_EXHAUSTION);\r
-               \r
-               // Set the maximum temperature and charge voltage limit.\r
-               HaltParameters.TemperatureMax = BAT_TEMPERATURE_MAX;\r
-               HaltParameters.TemperatureMin = BAT_TEMPERATURE_MIN;\r
-               HaltParameters.VoltageMax = BAT_VOLTAGE_PREQUAL;\r
-\r
-               // Start PWM output and charging timer first.\r
-               PWM_Start();\r
-               Time_Set(TIMER_CHG, BAT_TIME_PREQUAL, 0, 0);\r
-\r
-               // Call the constant current charging-function.\r
-               // If all goes well, we will get ST_CCURRENT in return.\r
-               NextState = ConstantCurrent();\r
-       break;\r
-       \r
-\r
-       case ST_CCURRENT:  // Second step is constant current charging.\r
-               \r
-               // Set charging timer to the battery's maximum charge time.\r
-               Time_Set(TIMER_CHG,BattData.MaxTime,0,0);\r
-\r
-               // Charge at the battery's maximum current, go to ST_CVOLTAGE next.\r
-               ChargeParameters.Current = BattData.MaxCurrent;\r
-               ChargeParameters.NextState = ST_CVOLTAGE;\r
-\r
-\r
-               // Charge until the defined BatChargeVoltage is reached.\r
-               HaltParameters.VoltageMax = BAT_VOLTAGE_MAX;\r
-               \r
-               // Start charging using constant current.\r
-               NextState = ConstantCurrent();\r
-       break;\r
-\r
-\r
-       case ST_CVOLTAGE:  // Third step is constant voltage charging.\r
-               \r
-               // Charge with the defined charge-voltage, go to ST_ENDCHARGE next.\r
-               ChargeParameters.Voltage = BAT_VOLTAGE_MAX;\r
-               ChargeParameters.NextState = ST_ENDCHARGE;\r
-               \r
-               // We want charging to halt if temperature rises too high, if current\r
-               // sinks below limit, or time runs out. Also, flag error if temperature\r
-               // limit is reached. Timeout doesn't mean anything is wrong at this point.\r
-               HaltParameters.HaltFlags = (HALT_CURRENT_MIN | HALT_TIME);\r
-               \r
-               HaltParameters.CurrentMin = BattData.MinCurrent;\r
-               \r
-               // Start charging using constant voltage. We will continue on the \r
-               // timer started in ST_CCURRENT.\r
-               NextState = ConstantVoltage(); \r
-       break;\r
-\r
-\r
-       case ST_ENDCHARGE:  // Charging is done!\r
-\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.\r
-       return(NextState);\r
-}\r
diff --git a/BaseTinyFirmware/GCC/nimhcharge.c b/BaseTinyFirmware/GCC/nimhcharge.c
deleted file mode 100644 (file)
index 1a436cd..0000000
+++ /dev/null
@@ -1,179 +0,0 @@
-/* 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