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