Initial import
[avr_bc100.git] / BaseTinyFirmware / IAR / 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 <ioavr.h>\r
58 #include <inavr.h>\r
59 #include <stdlib.h>\r
60 \r
61 #include "structs.h"\r
62 \r
63 #include "main.h"\r
64 #include "ADC.h"\r
65 #include "statefunc.h"\r
66 #include "battery.h"\r
67 #include "menu.h"\r
68 #include "OWI.h"\r
69 #include "PWM.h"\r
70 #include "time.h"\r
71 #include "USI.h"\r
72 \r
73 \r
74 \r
75 //******************************************************************************\r
76 // Globals\r
77 //******************************************************************************\r
78 unsigned char CurrentState;     //!< \brief Global that indicates current state\r
79                                 //!<\r
80                                 //!< Updated by main().\r
81                                 //!< \note See menu.h for definition of states.\r
82 \r
83 \r
84 //******************************************************************************\r
85 // Functions\r
86 //******************************************************************************\r
87 /*! \brief Main program\r
88  *\r
89  * The main function goes into an infinite loop, keeping track of the current\r
90  * state and the next one. If the next state is different from the current, it\r
91  * looks up the address to the next state function, in \ref menu_state[], and \r
92  * updates \ref CurrentState. The state function is then called and will\r
93  * eventually return a new state, and so the loop reiterates.\r
94  *\r
95  * \todo The variable inp is passed to all state functions, but is not used\r
96  * for anything yet. Remove?\r
97  */\r
98 int main( void )\r
99 {\r
100         unsigned char nextstate, inp, i;\r
101         unsigned char (*pStateFunc)(unsigned char);   // Function pointer.\r
102         \r
103         // Initialize local state variables.\r
104         inp = ZERO;\r
105         CurrentState = nextstate = ST_INIT;\r
106         pStateFunc = NULL;\r
107         \r
108         // Look for function associated with current state, get its address.\r
109         for (i = 0; menu_state[i].state != 0; i++) {\r
110                 if (menu_state[i].state == CurrentState) {\r
111                         pStateFunc = menu_state[i].pFunc;\r
112                 }\r
113         }\r
114 \r
115         while (TRUE) {\r
116                 // Run function associated with current state, get next state in return.\r
117                 if (pStateFunc != NULL){\r
118                         nextstate = pStateFunc(inp);\r
119                 }\r
120 \r
121                 // Look up function for next state, if it differs from the current.\r
122                 if (nextstate != CurrentState) {\r
123                         CurrentState = nextstate;\r
124                         for ( i = 0; menu_state[i].state != 0; i++) {\r
125                                 if (menu_state[i].state == CurrentState) {\r
126                                         pStateFunc = menu_state[i].pFunc;\r
127                                 }\r
128                         }\r
129                 }\r
130         }\r
131 }\r
132 \r
133 \r
134 /* Doxygen documentation mainpage ********************************************/\r
135 /*! \mainpage\r
136  * \section intro Introduction\r
137  * This documents the software for application note AVR458. This is a charger\r
138  * for Li-Ion batteries, based on ATAVRBC100.\r
139  *\r
140  * \section compinfo Compilation Info\r
141  * This software was compiled with IAR Embedded Workbench, 4.30. To use GCC \r
142  * the source have to be modified.\n\r
143  * \n\r
144  * To make project on IAR EWAVR:\n\r
145  * Add the .c files to project (ADC.c, battery.c, main.c, menu.c,\r
146  * OWI.c, PWM.c, time.c and USI.c). Add either LIIONcharge.c or NIMHcharge.c,\r
147  * and update LIIONspecs.h or NIMHspecs.h, and battery.h with the appropriate\r
148  * battery data if needed.\n\r
149  * \n\r
150  * Use device --cpu=tiny861, enable bit definitions in I/O include files,\r
151  * optimization low for debug target and high for release, output format: ubrof8\r
152  * for Debug and intel_extended for Release. \n\r
153  *\r
154  * \section deviceinfo Device Info\r
155  * This application is based on the ATtiny 861, but it is possible to migrate \r
156  * the design to other AVR microcontrollers, such as pin-compatible devices\r
157  * ATtiny 261/461. Low pin count devices such as ATtiny 25/45/85 may also be\r
158  * used, but with reduced functionality.\r
159  * \r
160  * Required fuse bit settings:\r
161  *    <pre>\r
162  *    FUSE BIT  | SETTING\r
163  *    ----------+--------------------\r
164  *    CKDIV8    | 1 (unprogrammed)\r
165  *    CKSEL3..0 | 0010 (internal osc)\r
166  *    </pre>\r
167  *\r
168  *\r
169  * \section todo To Do-list\r
170  * \todo\r
171  * - Finalize master-slave communication protocol\r
172  * - Implement discharge mode\r
173  *\r
174  * \section contactinfo Contact Info\r
175  * For more info about Atmel AVR visit http://www.atmel.com/products/AVR/ \n\r
176  * For application notes visit \r
177  * http://www.atmel.com/dyn/products/app_notes.asp?family_id=607 \n\r
178  * Support mail: avr@atmel.com\r
179  */\r
180 \r
181 \r
182 /*! \page misra MISRA C rule violations\r
183  *\r
184  * \par Rule 1\r
185  * <i>"All code shall conform to ISO 9899 standard C, with no extensions\r
186  * permitted."</i>\r
187  *\r
188  * Extensions are necessary because ISO C has no way of specifying that a\r
189  * function should be an interrupt service routine, or that we would like data\r
190  * members to be stored in f.ex. EEPROM.\r
191  *\r
192  *\r
193  * \par Rule 37\r
194  * <i>"Bitwise operations shall not be performed on signed integer types."</i>\r
195  *\r
196  * The compiler assumes all the 1's we shift around to make bitmasks are signed\r
197  * integers. Specifying them all to be (unsigned int), either directly or via a\r
198  * definition, would fix the rule violations, but also reduce code readability.\r
199  *\r
200  *\r
201  * \par Rule 45\r
202  * <i>"Type casting from any type to or from pointers shall not be used."</i>\r
203  *\r
204  * Assigning macro NULL, defined in stdlib.h, to a pointer causes this..\r
205  *\r
206  *\r
207  * \par Rule 96\r
208  * <i>"In the definition of a function-like macro the whole definition, and each\r
209  * instance of a parameter, shall be enclosed in parentheses."</i>\r
210  * \r
211  * It is difficult to use parentheses with void function-like macros.\r
212  */\r