2008-03-14 Martin Thomas <mthomas@rhrk.uni-kl.de>
[avr_bc100.git] / BaseTinyFirmware / GCC / main.c
1 /* This file has been prepared for Doxygen automatic documentation generation.*/\r
2 /*! \file *********************************************************************\r
3  *\r
4  * \brief\r
5  *      Main program file\r
6  *\r
7  *      Contains the main program, which is a basic state machine.\r
8  *\r
9  * \par Application note:\r
10  *      AVR458: Charging Li-Ion Batteries with BC100 \n\r
11  *      AVR463: Charging NiMH 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: 2302 $\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/main.c $\r
26  * $Date: 2007-08-23 14:57:36 +0200 (to, 23 aug 2007) $\n\r
27  ******************************************************************************/\r
28 \r
29 /*! \page License\r
30  * Copyright (c) 2007, Atmel Corporation All rights reserved.\r
31  *\r
32  * Redistribution and use in source and binary forms, with or without\r
33  * modification, are permitted provided that the following conditions are met:\r
34  *\r
35  * 1. Redistributions of source code must retain the above copyright notice,\r
36  * this list of conditions and the following disclaimer.\r
37  *\r
38  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
39  * this list of conditions and the following disclaimer in the documentation\r
40  * and/or other materials provided with the distribution.\r
41  *\r
42  * 3. The name of ATMEL may not be used to endorse or promote products derived\r
43  * from this software without specific prior written permission.\r
44  *\r
45  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED\r
46  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
47  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND\r
48  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,\r
49  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
50  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
51  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
53  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
54  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
55  */\r
56 \r
57 #include <avr/io.h>\r
58 #include <avr/pgmspace.h>\r
59 \r
60 #include <stdlib.h>\r
61 \r
62 #include "structs.h"\r
63 \r
64 #include "main.h"\r
65 #include "ADC.h"\r
66 #include "statefunc.h"\r
67 #include "battery.h"\r
68 #include "menu.h"\r
69 #include "OWI.h"\r
70 #include "PWM.h"\r
71 #include "time.h"\r
72 #include "USI.h"\r
73 \r
74 \r
75 \r
76 //******************************************************************************\r
77 // Globals\r
78 //******************************************************************************\r
79 unsigned char CurrentState;     //!< \brief Global that indicates current state\r
80                                 //!<\r
81                                 //!< Updated by main().\r
82                                 //!< \note See menu.h for definition of states.\r
83 \r
84 \r
85 //******************************************************************************\r
86 // Functions\r
87 //******************************************************************************\r
88 /*! \brief Main program\r
89  *\r
90  * The main function goes into an infinite loop, keeping track of the current\r
91  * state and the next one. If the next state is different from the current, it\r
92  * looks up the address to the next state function, in \ref menu_state[], and \r
93  * updates \ref CurrentState. The state function is then called and will\r
94  * eventually return a new state, and so the loop reiterates.\r
95  *\r
96  * \todo The variable inp is passed to all state functions, but is not used\r
97  * for anything yet. Remove?\r
98  */\r
99 int main( void )\r
100 {\r
101         unsigned char nextstate, inp, i;\r
102         unsigned char (*pStateFunc)(unsigned char);   // Function pointer.\r
103         \r
104         // Initialize local state variables.\r
105         inp = ZERO;\r
106         CurrentState = nextstate = ST_INIT;\r
107         pStateFunc = NULL;\r
108         \r
109         // Look for function associated with current state, get its address.\r
110         for (i = 0; pgm_read_byte(&menu_state[i].state) != 0; i++) {\r
111                 if (pgm_read_byte(&menu_state[i].state) == CurrentState) {\r
112                         pStateFunc = (PGM_VOID_P) pgm_read_word(&menu_state[i].pFunc);\r
113                 }\r
114         }\r
115 \r
116         while (TRUE) {\r
117                 // Run function associated with current state, get next state in return.\r
118                 if (pStateFunc != NULL){\r
119                         nextstate = pStateFunc(inp);\r
120                 }\r
121 \r
122                 // Look up function for next state, if it differs from the current.\r
123                 if (nextstate != CurrentState) {\r
124                         CurrentState = nextstate;\r
125                         for ( i = 0; pgm_read_byte(&menu_state[i].state) != 0; i++) {\r
126                                 if (pgm_read_byte(&menu_state[i].state) == CurrentState) {\r
127                                         pStateFunc = (PGM_VOID_P) pgm_read_word(&menu_state[i].pFunc);\r
128                                 }\r
129                         }\r
130                 }\r
131         }\r
132 }\r
133 \r
134 \r
135 /* Doxygen documentation mainpage ********************************************/\r
136 /*! \mainpage\r
137  * \section intro Introduction\r
138  * This documents the software for application note AVR458. This is a charger\r
139  * for Li-Ion batteries, based on ATAVRBC100.\r
140  *\r
141  * \section compinfo Compilation Info\r
142  * This software was compiled with IAR Embedded Workbench, 4.30. To use GCC \r
143  * the source have to be modified.\n\r
144  * \n\r
145  * To make project on IAR EWAVR:\n\r
146  * Add the .c files to project (ADC.c, battery.c, main.c, menu.c,\r
147  * OWI.c, PWM.c, time.c and USI.c). Add either LIIONcharge.c or NIMHcharge.c,\r
148  * and update LIIONspecs.h or NIMHspecs.h, and battery.h with the appropriate\r
149  * battery data if needed.\n\r
150  * \n\r
151  * Use device --cpu=tiny861, enable bit definitions in I/O include files,\r
152  * optimization low for debug target and high for release, output format: ubrof8\r
153  * for Debug and intel_extended for Release. \n\r
154  *\r
155  * \section deviceinfo Device Info\r
156  * This application is based on the ATtiny 861, but it is possible to migrate \r
157  * the design to other AVR microcontrollers, such as pin-compatible devices\r
158  * ATtiny 261/461. Low pin count devices such as ATtiny 25/45/85 may also be\r
159  * used, but with reduced functionality.\r
160  * \r
161  * Required fuse bit settings:\r
162  *    <pre>\r
163  *    FUSE BIT  | SETTING\r
164  *    ----------+--------------------\r
165  *    CKDIV8    | 1 (unprogrammed)\r
166  *    CKSEL3..0 | 0010 (internal osc)\r
167  *    </pre>\r
168  *\r
169  *\r
170  * \section todo To Do-list\r
171  * \todo\r
172  * - Finalize master-slave communication protocol\r
173  * - Implement discharge mode\r
174  *\r
175  * \section contactinfo Contact Info\r
176  * For more info about Atmel AVR visit http://www.atmel.com/products/AVR/ \n\r
177  * For application notes visit \r
178  * http://www.atmel.com/dyn/products/app_notes.asp?family_id=607 \n\r
179  * Support mail: avr@atmel.com\r
180  */\r
181 \r
182 \r
183 /*! \page misra MISRA C rule violations\r
184  *\r
185  * \par Rule 1\r
186  * <i>"All code shall conform to ISO 9899 standard C, with no extensions\r
187  * permitted."</i>\r
188  *\r
189  * Extensions are necessary because ISO C has no way of specifying that a\r
190  * function should be an interrupt service routine, or that we would like data\r
191  * members to be stored in f.ex. EEPROM.\r
192  *\r
193  *\r
194  * \par Rule 37\r
195  * <i>"Bitwise operations shall not be performed on signed integer types."</i>\r
196  *\r
197  * The compiler assumes all the 1's we shift around to make bitmasks are signed\r
198  * integers. Specifying them all to be (unsigned int), either directly or via a\r
199  * definition, would fix the rule violations, but also reduce code readability.\r
200  *\r
201  *\r
202  * \par Rule 45\r
203  * <i>"Type casting from any type to or from pointers shall not be used."</i>\r
204  *\r
205  * Assigning macro NULL, defined in stdlib.h, to a pointer causes this..\r
206  *\r
207  *\r
208  * \par Rule 96\r
209  * <i>"In the definition of a function-like macro the whole definition, and each\r
210  * instance of a parameter, shall be enclosed in parentheses."</i>\r
211  * \r
212  * It is difficult to use parentheses with void function-like macros.\r
213  */\r