1 ###############################################################################
\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
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\bc10 #
\r
9 # 0\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\Lis #
\r
12 # t\ --initializers_in_flash -s9 --no_cross_call #
\r
13 # --no_tbaa -DENABLE_BIT_DEFINITIONS -e -I #
\r
14 # "C:\Program Files\IAR Systems\Embedded #
\r
15 # Workbench 4.0\avr\INC\" -I "C:\Program #
\r
16 # Files\IAR Systems\Embedded Workbench #
\r
17 # 4.0\avr\INC\CLIB\" --eeprom_size 512 #
\r
18 # --misrac=5-9,11-12,14,16-17,19-21,24-26,29-32, #
\r
19 # 34-35,38-39,42-43,46,50,52-54,56-59,61-62, #
\r
20 # 64-65,68-80,83-84,87-91,94-95,98-100,103-110, #
\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
29 # Not checked = 6,16,25-26,43,65,70,90,99,103,106-107,110, #
\r
31 # List file = C:\home\kevin\pub\src\bc100\IAR\Release\List\ma #
\r
33 # Object file = C:\home\kevin\pub\src\bc100\IAR\Release\Obj\mai #
\r
37 ###############################################################################
\r
39 C:\home\kevin\pub\src\bc100\IAR\main.c
\r
40 1 /* This file has been prepared for Doxygen automatic documentation generation.*/
\r
41 2 /*! \file *********************************************************************
\r
44 5 * Main program file
\r
46 7 * Contains the main program, which is a basic state machine.
\r
48 9 * \par Application note:
\r
49 10 * AVR458: Charging Li-Ion Batteries with BC100 \n
\r
50 11 * AVR463: Charging NiMH Batteries with BC100
\r
52 13 * \par Documentation
\r
53 14 * For comprehensive code documentation, supported compilers, compiler
\r
54 15 * settings and supported devices see readme.html
\r
57 18 * Atmel Corporation: http://www.atmel.com \n
\r
58 19 * Support email: avr@atmel.com
\r
62 23 * $Revision: 2302 $
\r
64 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
65 26 * $Date: 2007-08-23 14:57:36 +0200 (to, 23 aug 2007) $\n
\r
66 27 ******************************************************************************/
\r
68 29 /*! \page License
\r
69 30 * Copyright (c) 2007, Atmel Corporation All rights reserved.
\r
71 32 * Redistribution and use in source and binary forms, with or without
\r
72 33 * modification, are permitted provided that the following conditions are met:
\r
74 35 * 1. Redistributions of source code must retain the above copyright notice,
\r
75 36 * this list of conditions and the following disclaimer.
\r
77 38 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
78 39 * this list of conditions and the following disclaimer in the documentation
\r
79 40 * and/or other materials provided with the distribution.
\r
81 42 * 3. The name of ATMEL may not be used to endorse or promote products derived
\r
82 43 * from this software without specific prior written permission.
\r
84 45 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
\r
85 46 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
86 47 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
\r
87 48 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
\r
88 49 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
\r
89 50 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
90 51 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
\r
91 52 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
92 53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
93 54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
96 57 #include <ioavr.h>
\r
97 58 #include <inavr.h>
\r
98 59 #include <stdlib.h>
\r
100 61 #include "structs.h"
\r
102 63 #include "main.h"
\r
103 64 #include "ADC.h"
\r
104 65 #include "statefunc.h"
\r
105 66 #include "battery.h"
\r
106 67 #include "menu.h"
\r
107 68 #include "OWI.h"
\r
108 69 #include "PWM.h"
\r
109 70 #include "time.h"
\r
110 71 #include "USI.h"
\r
114 75 //******************************************************************************
\r
116 77 //******************************************************************************
\r
118 \ In segment NEAR_Z, align 1, keep-with-next
\r
119 \ 00000000 REQUIRE `?<Segment init: NEAR_Z>`
\r
120 78 unsigned char CurrentState; //!< \brief Global that indicates current state
\r
124 80 //!< Updated by main().
\r
125 81 //!< \note See menu.h for definition of states.
\r
128 84 //******************************************************************************
\r
130 86 //******************************************************************************
\r
131 87 /*! \brief Main program
\r
133 89 * The main function goes into an infinite loop, keeping track of the current
\r
134 90 * state and the next one. If the next state is different from the current, it
\r
135 91 * looks up the address to the next state function, in \ref menu_state[], and
\r
136 92 * updates \ref CurrentState. The state function is then called and will
\r
137 93 * eventually return a new state, and so the loop reiterates.
\r
139 95 * \todo The variable inp is passed to all state functions, but is not used
\r
140 96 * for anything yet. Remove?
\r
143 \ In segment CODE, align 2, keep-with-next
\r
144 98 int main( void )
\r
147 \ 00000000 939A ST -Y, R25
\r
148 \ 00000002 938A ST -Y, R24
\r
149 100 unsigned char nextstate, inp, i;
\r
150 101 unsigned char (*pStateFunc)(unsigned char); // Function pointer.
\r
152 103 // Initialize local state variables.
\r
154 105 CurrentState = nextstate = ST_INIT;
\r
155 \ 00000004 E02A LDI R18, 10
\r
156 \ 00000006 9320.... STS CurrentState, R18
\r
157 106 pStateFunc = NULL;
\r
158 \ 0000000A E080 LDI R24, 0
\r
159 \ 0000000C E090 LDI R25, 0
\r
161 108 // Look for function associated with current state, get its address.
\r
162 109 for (i = 0; menu_state[i].state != 0; i++) {
\r
163 \ 0000000E E000 LDI R16, 0
\r
164 \ 00000010 C006 RJMP ??main_0
\r
165 110 if (menu_state[i].state == CurrentState) {
\r
167 \ 00000012 301A CPI R17, 10
\r
168 \ 00000014 F419 BRNE ??main_2
\r
169 111 pStateFunc = menu_state[i].pFunc;
\r
170 \ 00000016 9631 ADIW R31:R30, 1
\r
171 \ 00000018 9185 LPM R24, Z+
\r
172 \ 0000001A 9194 LPM R25, Z
\r
176 \ 0000001C 9503 INC R16
\r
178 \ 0000001E 2F40 MOV R20, R16
\r
179 \ 00000020 E050 LDI R21, 0
\r
180 \ 00000022 01BA MOVW R23:R22, R21:R20
\r
181 \ 00000024 0F44 LSL R20
\r
182 \ 00000026 1F55 ROL R21
\r
183 \ 00000028 0F46 ADD R20, R22
\r
184 \ 0000002A 1F57 ADC R21, R23
\r
185 \ 0000002C .... LDI R30, LOW(menu_state)
\r
186 \ 0000002E .... LDI R31, (menu_state) >> 8
\r
187 \ 00000030 0FE4 ADD R30, R20
\r
188 \ 00000032 1FF5 ADC R31, R21
\r
189 \ 00000034 9114 LPM R17, Z
\r
190 \ 00000036 2311 TST R17
\r
191 \ 00000038 F761 BRNE ??main_1
\r
194 116 // Run function associated with current state, get next state in return.
\r
195 117 if (pStateFunc != NULL){
\r
197 \ 0000003A E000 LDI R16, 0
\r
198 \ 0000003C 3080 CPI R24, 0
\r
199 \ 0000003E 0790 CPC R25, R16
\r
200 \ 00000040 F019 BREQ ??main_4
\r
201 118 nextstate = pStateFunc(inp);
\r
202 \ 00000042 01FC MOVW R31:R30, R25:R24
\r
203 \ 00000044 9509 ICALL
\r
204 \ 00000046 2F20 MOV R18, R16
\r
207 121 // Look up function for next state, if it differs from the current.
\r
208 122 if (nextstate != CurrentState) {
\r
210 \ 00000048 9100.... LDS R16, CurrentState
\r
211 \ 0000004C 1720 CP R18, R16
\r
212 \ 0000004E F3A9 BREQ ??main_3
\r
213 123 CurrentState = nextstate;
\r
214 \ 00000050 9320.... STS CurrentState, R18
\r
215 124 for ( i = 0; menu_state[i].state != 0; i++) {
\r
216 \ 00000054 E000 LDI R16, 0
\r
218 \ 00000056 2F40 MOV R20, R16
\r
219 \ 00000058 E050 LDI R21, 0
\r
220 \ 0000005A 01BA MOVW R23:R22, R21:R20
\r
221 \ 0000005C 0F44 LSL R20
\r
222 \ 0000005E 1F55 ROL R21
\r
223 \ 00000060 0F46 ADD R20, R22
\r
224 \ 00000062 1F57 ADC R21, R23
\r
225 \ 00000064 .... LDI R30, LOW(menu_state)
\r
226 \ 00000066 .... LDI R31, (menu_state) >> 8
\r
227 \ 00000068 0FE4 ADD R30, R20
\r
228 \ 0000006A 1FF5 ADC R31, R21
\r
229 \ 0000006C 9114 LPM R17, Z
\r
230 \ 0000006E 2311 TST R17
\r
231 \ 00000070 F321 BREQ ??main_3
\r
232 125 if (menu_state[i].state == CurrentState) {
\r
233 \ 00000072 9130.... LDS R19, CurrentState
\r
234 \ 00000076 1713 CP R17, R19
\r
235 \ 00000078 F419 BRNE ??main_6
\r
236 126 pStateFunc = menu_state[i].pFunc;
\r
237 \ 0000007A 9631 ADIW R31:R30, 1
\r
238 \ 0000007C 9185 LPM R24, Z+
\r
239 \ 0000007E 9194 LPM R25, Z
\r
243 \ 00000080 9503 INC R16
\r
244 \ 00000082 CFE9 RJMP ??main_5
\r
250 134 /* Doxygen documentation mainpage ********************************************/
\r
252 136 * \section intro Introduction
\r
253 137 * This documents the software for application note AVR458. This is a charger
\r
254 138 * for Li-Ion batteries, based on ATAVRBC100.
\r
256 140 * \section compinfo Compilation Info
\r
257 141 * This software was compiled with IAR Embedded Workbench, 4.30. To use GCC
\r
258 142 * the source have to be modified.\n
\r
260 144 * To make project on IAR EWAVR:\n
\r
261 145 * Add the .c files to project (ADC.c, battery.c, main.c, menu.c,
\r
262 146 * OWI.c, PWM.c, time.c and USI.c). Add either LIIONcharge.c or NIMHcharge.c,
\r
263 147 * and update LIIONspecs.h or NIMHspecs.h, and battery.h with the appropriate
\r
264 148 * battery data if needed.\n
\r
266 150 * Use device --cpu=tiny861, enable bit definitions in I/O include files,
\r
267 151 * optimization low for debug target and high for release, output format: ubrof8
\r
268 152 * for Debug and intel_extended for Release. \n
\r
270 154 * \section deviceinfo Device Info
\r
271 155 * This application is based on the ATtiny 861, but it is possible to migrate
\r
272 156 * the design to other AVR microcontrollers, such as pin-compatible devices
\r
273 157 * ATtiny 261/461. Low pin count devices such as ATtiny 25/45/85 may also be
\r
274 158 * used, but with reduced functionality.
\r
276 160 * Required fuse bit settings:
\r
278 162 * FUSE BIT | SETTING
\r
279 163 * ----------+--------------------
\r
280 164 * CKDIV8 | 1 (unprogrammed)
\r
281 165 * CKSEL3..0 | 0010 (internal osc)
\r
285 169 * \section todo To Do-list
\r
287 171 * - Finalize master-slave communication protocol
\r
288 172 * - Implement discharge mode
\r
290 174 * \section contactinfo Contact Info
\r
291 175 * For more info about Atmel AVR visit http://www.atmel.com/products/AVR/ \n
\r
292 176 * For application notes visit
\r
293 177 * http://www.atmel.com/dyn/products/app_notes.asp?family_id=607 \n
\r
294 178 * Support mail: avr@atmel.com
\r
298 182 /*! \page misra MISRA C rule violations
\r
301 185 * <i>"All code shall conform to ISO 9899 standard C, with no extensions
\r
302 186 * permitted."</i>
\r
304 188 * Extensions are necessary because ISO C has no way of specifying that a
\r
305 189 * function should be an interrupt service routine, or that we would like data
\r
306 190 * members to be stored in f.ex. EEPROM.
\r
310 194 * <i>"Bitwise operations shall not be performed on signed integer types."</i>
\r
312 196 * The compiler assumes all the 1's we shift around to make bitmasks are signed
\r
313 197 * integers. Specifying them all to be (unsigned int), either directly or via a
\r
314 198 * definition, would fix the rule violations, but also reduce code readability.
\r
318 202 * <i>"Type casting from any type to or from pointers shall not be used."</i>
\r
320 204 * Assigning macro NULL, defined in stdlib.h, to a pointer causes this..
\r
324 208 * <i>"In the definition of a function-like macro the whole definition, and each
\r
325 209 * instance of a parameter, shall be enclosed in parentheses."</i>
\r
327 211 * It is difficult to use parentheses with void function-like macros.
\r
330 Maximum stack usage in bytes:
\r
332 Function CSTACK RSTACK
\r
333 -------- ------ ------
\r
335 -> Indirect call 2 2
\r
338 Segment part sizes:
\r
340 Function/Label Bytes
\r
341 -------------- -----
\r
347 132 bytes in segment CODE
\r
348 6 bytes in segment INITTAB
\r
349 1 byte in segment NEAR_Z
\r
351 132 bytes of CODE memory (+ 6 bytes shared)
\r
352 1 byte of DATA memory
\r