1 ///////////////////////////////////////////////////////////////////////////////
\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
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
22 ///////////////////////////////////////////////////////////////////////////////
\r
26 RSEG CSTACK:DATA:NOROOT(0)
\r
27 RSEG RSTACK:DATA:NOROOT(0)
\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
34 EXTERN ?need_segment_init
\r
36 PUBWEAK `?<Segment init: NEAR_Z>`
\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
51 // 5 * Main program file
\r
53 // 7 * Contains the main program, which is a basic state machine.
\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
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
64 // 18 * Atmel Corporation: http://www.atmel.com \n
\r
65 // 19 * Support email: avr@atmel.com
\r
69 // 23 * $Revision: 2302 $
\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
75 // 29 /*! \page License
\r
76 // 30 * Copyright (c) 2007, Atmel Corporation All rights reserved.
\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
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
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
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
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
103 // 57 #include <ioavr.h>
\r
104 // 58 #include <inavr.h>
\r
105 // 59 #include <stdlib.h>
\r
107 // 61 #include "structs.h"
\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
121 // 75 //******************************************************************************
\r
123 // 77 //******************************************************************************
\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
131 // 80 //!< Updated by main().
\r
132 // 81 //!< \note See menu.h for definition of states.
\r
135 // 84 //******************************************************************************
\r
137 // 86 //******************************************************************************
\r
138 // 87 /*! \brief Main program
\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
146 // 95 * \todo The variable inp is passed to all state functions, but is not used
\r
147 // 96 * for anything yet. Remove?
\r
150 RSEG CODE:CODE:NOROOT(1)
\r
151 // 98 int main( void )
\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
161 // 103 // Initialize local state variables.
\r
164 // 105 CurrentState = nextstate = ST_INIT;
\r
167 STS CurrentState, R16
\r
168 // 106 pStateFunc = NULL;
\r
171 MOVW R5:R4, R17:R16
\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
182 LDI R30, LOW(menu_state)
\r
183 LDI R31, (menu_state) >> 8
\r
189 // 110 if (menu_state[i].state == CurrentState) {
\r
195 LDI R30, LOW(menu_state)
\r
196 LDI R31, (menu_state) >> 8
\r
200 LDS R17, CurrentState
\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
213 MOVW R31:R30, R27:R26
\r
217 MOVW R5:R4, R17:R16
\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
233 // 118 nextstate = pStateFunc(inp);
\r
235 MOVW R31:R30, R5:R4
\r
240 // 121 // Look up function for next state, if it differs from the current.
\r
241 // 122 if (nextstate != CurrentState) {
\r
243 LDS R16, CurrentState
\r
246 // 123 CurrentState = nextstate;
\r
247 STS CurrentState, R25
\r
248 // 124 for ( i = 0; menu_state[i].state != 0; i++) {
\r
256 LDI R30, LOW(menu_state)
\r
257 LDI R31, (menu_state) >> 8
\r
263 // 125 if (menu_state[i].state == CurrentState) {
\r
269 LDI R30, LOW(menu_state)
\r
270 LDI R31, (menu_state) >> 8
\r
274 LDS R17, CurrentState
\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
287 MOVW R31:R30, R27:R26
\r
291 MOVW R5:R4, R17:R16
\r
301 ASEGN ABSOLUTE:DATA:NOROOT,01cH
\r
304 ASEGN ABSOLUTE:DATA:NOROOT,01dH
\r
307 ASEGN ABSOLUTE:DATA:NOROOT,01eH
\r
310 ASEGN ABSOLUTE:DATA:NOROOT,01fH
\r
313 RSEG INITTAB:CODE:NOROOT(0)
\r
314 `?<Segment init: NEAR_Z>`:
\r
315 DW SFE(NEAR_Z) - SFB(NEAR_Z)
\r
318 REQUIRE ?need_segment_init
\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
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
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
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
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
349 // 160 * Required fuse bit settings:
\r
351 // 162 * FUSE BIT | SETTING
\r
352 // 163 * ----------+--------------------
\r
353 // 164 * CKDIV8 | 1 (unprogrammed)
\r
354 // 165 * CKSEL3..0 | 0010 (internal osc)
\r
358 // 169 * \section todo To Do-list
\r
360 // 171 * - Finalize master-slave communication protocol
\r
361 // 172 * - Implement discharge mode
\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
371 // 182 /*! \page misra MISRA C rule violations
\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
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
382 // 193 * \par Rule 37
\r
383 // 194 * <i>"Bitwise operations shall not be performed on signed integer types."</i>
\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
390 // 201 * \par Rule 45
\r
391 // 202 * <i>"Type casting from any type to or from pointers shall not be used."</i>
\r
393 // 204 * Assigning macro NULL, defined in stdlib.h, to a pointer causes this..
\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
400 // 211 * It is difficult to use parentheses with void function-like macros.
\r
403 // 220 bytes in segment CODE
\r
404 // 6 bytes in segment INITTAB
\r
405 // 1 byte in segment NEAR_Z
\r
407 // 220 bytes of CODE memory (+ 6 bytes shared)
\r
408 // 1 byte of DATA memory
\r