Initial import
[avr_bc100.git] / BaseTinyFirmware / IAR / Release / List / main.s90
1 ///////////////////////////////////////////////////////////////////////////////\r
2 //                                                                            /\r
3 // IAR Atmel AVR C/C++ Compiler V4.30F/W32              13/Mar/2008  04:52:00 /\r
4 // Copyright 1996-2007 IAR Systems. All rights reserved.                      /\r
5 //                                                                            /\r
6 //    Source file           =  C:\home\kevin\pub\src\bc100\IAR\main.c         /\r
7 //    Command line          =  C:\home\kevin\pub\src\bc100\IAR\main.c         /\r
8 //                             --cpu=tiny861 -ms -o C:\home\kevin\pub\src\bc1 /\r
9 //                             00\IAR\Release\Obj\ -D NDEBUG -lCN             /\r
10 //                             C:\home\kevin\pub\src\bc100\IAR\Release\List\  /\r
11 //                             -lB C:\home\kevin\pub\src\bc100\IAR\Release\Li /\r
12 //                             st\ --initializers_in_flash -s9                /\r
13 //                             --no_cross_call --no_tbaa                      /\r
14 //                             -DENABLE_BIT_DEFINITIONS -e -I "C:\Program     /\r
15 //                             Files\IAR Systems\Embedded Workbench           /\r
16 //                             4.0\avr\INC\" -I "C:\Program Files\IAR         /\r
17 //                             Systems\Embedded Workbench 4.0\avr\INC\CLIB\"  /\r
18 //                             --eeprom_size 512 --misrac=5-9,11-12,14,16-17, /\r
19 //                             19-21,24-26,29-32,34-35,38-39,42-43,46,50,     /\r
20 //                             52-54,56-59,61-62,64-65,68-80,83-84,87-91,     /\r
21 //                             94-95,98-100,103-110,112-126                   /\r
22 //    Enabled MISRA C rules =  5-9,11-12,14,16-17,19-21,24-26,29-32,34-35,    /\r
23 //                             38-39,42-43,46,50,52-54,56-59,61-62,64-65,     /\r
24 //                             68-80,83-84,87-91,94-95,98-100,103-110,112-126 /\r
25 //      Checked             =  5,7-9,11-12,14,17,19-21,24,29-32,34-35,38-39,  /\r
26 //                             42,46,50,52-54,56-59,61-62,64,68-69,71-80,     /\r
27 //                             83-84,87-89,91,94-95,98,100,104-105,108-109,   /\r
28 //                             112-115,118-126                                /\r
29 //      Not checked         =  6,16,25-26,43,65,70,90,99,103,106-107,110,     /\r
30 //                             116-117                                        /\r
31 //    List file             =  C:\home\kevin\pub\src\bc100\IAR\Release\List\m /\r
32 //                             ain.s90                                        /\r
33 //                                                                            /\r
34 //                                                                            /\r
35 ///////////////////////////////////////////////////////////////////////////////\r
36 \r
37         NAME main\r
38 \r
39         RSEG CSTACK:DATA:NOROOT(0)\r
40         RSEG RSTACK:DATA:NOROOT(0)\r
41 \r
42         EXTERN ?need_segment_init\r
43 \r
44         PUBWEAK `?<Segment init: NEAR_Z>`\r
45         PUBLIC CurrentState\r
46         PUBWEAK __?EEARH\r
47         PUBWEAK __?EEARL\r
48         PUBWEAK __?EECR\r
49         PUBWEAK __?EEDR\r
50         PUBLIC main\r
51 \r
52         EXTERN menu_state\r
53 \r
54 // C:\home\kevin\pub\src\bc100\IAR\main.c\r
55 //    1 /* This file has been prepared for Doxygen automatic documentation generation.*/\r
56 //    2 /*! \file *********************************************************************\r
57 //    3  *\r
58 //    4  * \brief\r
59 //    5  *      Main program file\r
60 //    6  *\r
61 //    7  *      Contains the main program, which is a basic state machine.\r
62 //    8  *\r
63 //    9  * \par Application note:\r
64 //   10  *      AVR458: Charging Li-Ion Batteries with BC100 \n\r
65 //   11  *      AVR463: Charging NiMH Batteries with BC100\r
66 //   12  *\r
67 //   13  * \par Documentation\r
68 //   14  *      For comprehensive code documentation, supported compilers, compiler \r
69 //   15  *      settings and supported devices see readme.html\r
70 //   16  *\r
71 //   17  * \author\r
72 //   18  *      Atmel Corporation: http://www.atmel.com \n\r
73 //   19  *      Support email: avr@atmel.com\r
74 //   20  *\r
75 //   21  * \r
76 //   22  * $Name$\r
77 //   23  * $Revision: 2302 $\r
78 //   24  * $RCSfile$\r
79 //   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
80 //   26  * $Date: 2007-08-23 14:57:36 +0200 (to, 23 aug 2007) $\n\r
81 //   27  ******************************************************************************/\r
82 //   28 \r
83 //   29 /*! \page License\r
84 //   30  * Copyright (c) 2007, Atmel Corporation All rights reserved.\r
85 //   31  *\r
86 //   32  * Redistribution and use in source and binary forms, with or without\r
87 //   33  * modification, are permitted provided that the following conditions are met:\r
88 //   34  *\r
89 //   35  * 1. Redistributions of source code must retain the above copyright notice,\r
90 //   36  * this list of conditions and the following disclaimer.\r
91 //   37  *\r
92 //   38  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
93 //   39  * this list of conditions and the following disclaimer in the documentation\r
94 //   40  * and/or other materials provided with the distribution.\r
95 //   41  *\r
96 //   42  * 3. The name of ATMEL may not be used to endorse or promote products derived\r
97 //   43  * from this software without specific prior written permission.\r
98 //   44  *\r
99 //   45  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED\r
100 //   46  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
101 //   47  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND\r
102 //   48  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,\r
103 //   49  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
104 //   50  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
105 //   51  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
106 //   52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
107 //   53  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
108 //   54  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
109 //   55  */\r
110 //   56 \r
111 //   57 #include <ioavr.h>\r
112 //   58 #include <inavr.h>\r
113 //   59 #include <stdlib.h>\r
114 //   60 \r
115 //   61 #include "structs.h"\r
116 //   62 \r
117 //   63 #include "main.h"\r
118 //   64 #include "ADC.h"\r
119 //   65 #include "statefunc.h"\r
120 //   66 #include "battery.h"\r
121 //   67 #include "menu.h"\r
122 //   68 #include "OWI.h"\r
123 //   69 #include "PWM.h"\r
124 //   70 #include "time.h"\r
125 //   71 #include "USI.h"\r
126 //   72 \r
127 //   73 \r
128 //   74 \r
129 //   75 //******************************************************************************\r
130 //   76 // Globals\r
131 //   77 //******************************************************************************\r
132 \r
133         RSEG NEAR_Z:DATA:NOROOT(0)\r
134         REQUIRE `?<Segment init: NEAR_Z>`\r
135 //   78 unsigned char CurrentState;     //!< \brief Global that indicates current state\r
136 CurrentState:\r
137         DS 1\r
138 //   79                                 //!<\r
139 //   80                                 //!< Updated by main().\r
140 //   81                                 //!< \note See menu.h for definition of states.\r
141 //   82 \r
142 //   83 \r
143 //   84 //******************************************************************************\r
144 //   85 // Functions\r
145 //   86 //******************************************************************************\r
146 //   87 /*! \brief Main program\r
147 //   88  *\r
148 //   89  * The main function goes into an infinite loop, keeping track of the current\r
149 //   90  * state and the next one. If the next state is different from the current, it\r
150 //   91  * looks up the address to the next state function, in \ref menu_state[], and \r
151 //   92  * updates \ref CurrentState. The state function is then called and will\r
152 //   93  * eventually return a new state, and so the loop reiterates.\r
153 //   94  *\r
154 //   95  * \todo The variable inp is passed to all state functions, but is not used\r
155 //   96  * for anything yet. Remove?\r
156 //   97  */\r
157 \r
158         RSEG CODE:CODE:NOROOT(1)\r
159 //   98 int main( void )\r
160 main:\r
161 //   99 {\r
162         ST      -Y, R25\r
163         ST      -Y, R24\r
164 //  100         unsigned char nextstate, inp, i;\r
165 //  101         unsigned char (*pStateFunc)(unsigned char);   // Function pointer.\r
166 //  102         \r
167 //  103         // Initialize local state variables.\r
168 //  104         inp = ZERO;\r
169 //  105         CurrentState = nextstate = ST_INIT;\r
170         LDI     R18, 10\r
171         STS     CurrentState, R18\r
172 //  106         pStateFunc = NULL;\r
173         LDI     R24, 0\r
174         LDI     R25, 0\r
175 //  107         \r
176 //  108         // Look for function associated with current state, get its address.\r
177 //  109         for (i = 0; menu_state[i].state != 0; i++) {\r
178         LDI     R16, 0\r
179         RJMP    ??main_0\r
180 //  110                 if (menu_state[i].state == CurrentState) {\r
181 ??main_1:\r
182         CPI     R17, 10\r
183         BRNE    ??main_2\r
184 //  111                         pStateFunc = menu_state[i].pFunc;\r
185         ADIW    R31:R30, 1\r
186         LPM     R24, Z+\r
187         LPM     R25, Z\r
188 //  112                 }\r
189 //  113         }\r
190 ??main_2:\r
191         INC     R16\r
192 ??main_0:\r
193         MOV     R20, R16\r
194         LDI     R21, 0\r
195         MOVW    R23:R22, R21:R20\r
196         LSL     R20\r
197         ROL     R21\r
198         ADD     R20, R22\r
199         ADC     R21, R23\r
200         LDI     R30, LOW(menu_state)\r
201         LDI     R31, (menu_state) >> 8\r
202         ADD     R30, R20\r
203         ADC     R31, R21\r
204         LPM     R17, Z\r
205         TST     R17\r
206         BRNE    ??main_1\r
207 //  114 \r
208 //  115         while (TRUE) {\r
209 //  116                 // Run function associated with current state, get next state in return.\r
210 //  117                 if (pStateFunc != NULL){\r
211 ??main_3:\r
212         LDI     R16, 0\r
213         CPI     R24, 0\r
214         CPC     R25, R16\r
215         BREQ    ??main_4\r
216 //  118                         nextstate = pStateFunc(inp);\r
217         MOVW    R31:R30, R25:R24\r
218         ICALL\r
219         MOV     R18, R16\r
220 //  119                 }\r
221 //  120 \r
222 //  121                 // Look up function for next state, if it differs from the current.\r
223 //  122                 if (nextstate != CurrentState) {\r
224 ??main_4:\r
225         LDS     R16, CurrentState\r
226         CP      R18, R16\r
227         BREQ    ??main_3\r
228 //  123                         CurrentState = nextstate;\r
229         STS     CurrentState, R18\r
230 //  124                         for ( i = 0; menu_state[i].state != 0; i++) {\r
231         LDI     R16, 0\r
232 ??main_5:\r
233         MOV     R20, R16\r
234         LDI     R21, 0\r
235         MOVW    R23:R22, R21:R20\r
236         LSL     R20\r
237         ROL     R21\r
238         ADD     R20, R22\r
239         ADC     R21, R23\r
240         LDI     R30, LOW(menu_state)\r
241         LDI     R31, (menu_state) >> 8\r
242         ADD     R30, R20\r
243         ADC     R31, R21\r
244         LPM     R17, Z\r
245         TST     R17\r
246         BREQ    ??main_3\r
247 //  125                                 if (menu_state[i].state == CurrentState) {\r
248         LDS     R19, CurrentState\r
249         CP      R17, R19\r
250         BRNE    ??main_6\r
251 //  126                                         pStateFunc = menu_state[i].pFunc;\r
252         ADIW    R31:R30, 1\r
253         LPM     R24, Z+\r
254         LPM     R25, Z\r
255 //  127                                 }\r
256 //  128                         }\r
257 ??main_6:\r
258         INC     R16\r
259         RJMP    ??main_5\r
260 //  129                 }\r
261 //  130         }\r
262 //  131 }\r
263 \r
264         ASEGN ABSOLUTE:DATA:NOROOT,01cH\r
265 __?EECR:\r
266 \r
267         ASEGN ABSOLUTE:DATA:NOROOT,01dH\r
268 __?EEDR:\r
269 \r
270         ASEGN ABSOLUTE:DATA:NOROOT,01eH\r
271 __?EEARL:\r
272 \r
273         ASEGN ABSOLUTE:DATA:NOROOT,01fH\r
274 __?EEARH:\r
275 \r
276         RSEG INITTAB:CODE:NOROOT(0)\r
277 `?<Segment init: NEAR_Z>`:\r
278         DW      SFE(NEAR_Z) - SFB(NEAR_Z)\r
279         DW      SFB(NEAR_Z)\r
280         DW      0\r
281         REQUIRE ?need_segment_init\r
282 \r
283         END\r
284 //  132 \r
285 //  133 \r
286 //  134 /* Doxygen documentation mainpage ********************************************/\r
287 //  135 /*! \mainpage\r
288 //  136  * \section intro Introduction\r
289 //  137  * This documents the software for application note AVR458. This is a charger\r
290 //  138  * for Li-Ion batteries, based on ATAVRBC100.\r
291 //  139  *\r
292 //  140  * \section compinfo Compilation Info\r
293 //  141  * This software was compiled with IAR Embedded Workbench, 4.30. To use GCC \r
294 //  142  * the source have to be modified.\n\r
295 //  143  * \n\r
296 //  144  * To make project on IAR EWAVR:\n\r
297 //  145  * Add the .c files to project (ADC.c, battery.c, main.c, menu.c,\r
298 //  146  * OWI.c, PWM.c, time.c and USI.c). Add either LIIONcharge.c or NIMHcharge.c,\r
299 //  147  * and update LIIONspecs.h or NIMHspecs.h, and battery.h with the appropriate\r
300 //  148  * battery data if needed.\n\r
301 //  149  * \n\r
302 //  150  * Use device --cpu=tiny861, enable bit definitions in I/O include files,\r
303 //  151  * optimization low for debug target and high for release, output format: ubrof8\r
304 //  152  * for Debug and intel_extended for Release. \n\r
305 //  153  *\r
306 //  154  * \section deviceinfo Device Info\r
307 //  155  * This application is based on the ATtiny 861, but it is possible to migrate \r
308 //  156  * the design to other AVR microcontrollers, such as pin-compatible devices\r
309 //  157  * ATtiny 261/461. Low pin count devices such as ATtiny 25/45/85 may also be\r
310 //  158  * used, but with reduced functionality.\r
311 //  159  * \r
312 //  160  * Required fuse bit settings:\r
313 //  161  *    <pre>\r
314 //  162  *    FUSE BIT  | SETTING\r
315 //  163  *    ----------+--------------------\r
316 //  164  *    CKDIV8    | 1 (unprogrammed)\r
317 //  165  *    CKSEL3..0 | 0010 (internal osc)\r
318 //  166  *    </pre>\r
319 //  167  *\r
320 //  168  *\r
321 //  169  * \section todo To Do-list\r
322 //  170  * \todo\r
323 //  171  * - Finalize master-slave communication protocol\r
324 //  172  * - Implement discharge mode\r
325 //  173  *\r
326 //  174  * \section contactinfo Contact Info\r
327 //  175  * For more info about Atmel AVR visit http://www.atmel.com/products/AVR/ \n\r
328 //  176  * For application notes visit \r
329 //  177  * http://www.atmel.com/dyn/products/app_notes.asp?family_id=607 \n\r
330 //  178  * Support mail: avr@atmel.com\r
331 //  179  */\r
332 //  180 \r
333 //  181 \r
334 //  182 /*! \page misra MISRA C rule violations\r
335 //  183  *\r
336 //  184  * \par Rule 1\r
337 //  185  * <i>"All code shall conform to ISO 9899 standard C, with no extensions\r
338 //  186  * permitted."</i>\r
339 //  187  *\r
340 //  188  * Extensions are necessary because ISO C has no way of specifying that a\r
341 //  189  * function should be an interrupt service routine, or that we would like data\r
342 //  190  * members to be stored in f.ex. EEPROM.\r
343 //  191  *\r
344 //  192  *\r
345 //  193  * \par Rule 37\r
346 //  194  * <i>"Bitwise operations shall not be performed on signed integer types."</i>\r
347 //  195  *\r
348 //  196  * The compiler assumes all the 1's we shift around to make bitmasks are signed\r
349 //  197  * integers. Specifying them all to be (unsigned int), either directly or via a\r
350 //  198  * definition, would fix the rule violations, but also reduce code readability.\r
351 //  199  *\r
352 //  200  *\r
353 //  201  * \par Rule 45\r
354 //  202  * <i>"Type casting from any type to or from pointers shall not be used."</i>\r
355 //  203  *\r
356 //  204  * Assigning macro NULL, defined in stdlib.h, to a pointer causes this..\r
357 //  205  *\r
358 //  206  *\r
359 //  207  * \par Rule 96\r
360 //  208  * <i>"In the definition of a function-like macro the whole definition, and each\r
361 //  209  * instance of a parameter, shall be enclosed in parentheses."</i>\r
362 //  210  * \r
363 //  211  * It is difficult to use parentheses with void function-like macros.\r
364 //  212  */\r
365 // \r
366 // 132 bytes in segment CODE\r
367 //   6 bytes in segment INITTAB\r
368 //   1 byte  in segment NEAR_Z\r
369 // \r
370 // 132 bytes of CODE memory (+ 6 bytes shared)\r
371 //   1 byte  of DATA memory\r
372 //\r
373 //Errors: none\r
374 //Warnings: none\r