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\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
31 // List file = C:\home\kevin\pub\src\bc100\IAR\Release\List\m /
\r
35 ///////////////////////////////////////////////////////////////////////////////
\r
39 RSEG CSTACK:DATA:NOROOT(0)
\r
40 RSEG RSTACK:DATA:NOROOT(0)
\r
42 EXTERN ?need_segment_init
\r
44 PUBWEAK `?<Segment init: NEAR_Z>`
\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
59 // 5 * Main program file
\r
61 // 7 * Contains the main program, which is a basic state machine.
\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
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
72 // 18 * Atmel Corporation: http://www.atmel.com \n
\r
73 // 19 * Support email: avr@atmel.com
\r
77 // 23 * $Revision: 2302 $
\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
83 // 29 /*! \page License
\r
84 // 30 * Copyright (c) 2007, Atmel Corporation All rights reserved.
\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
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
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
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
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
111 // 57 #include <ioavr.h>
\r
112 // 58 #include <inavr.h>
\r
113 // 59 #include <stdlib.h>
\r
115 // 61 #include "structs.h"
\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
129 // 75 //******************************************************************************
\r
131 // 77 //******************************************************************************
\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
139 // 80 //!< Updated by main().
\r
140 // 81 //!< \note See menu.h for definition of states.
\r
143 // 84 //******************************************************************************
\r
145 // 86 //******************************************************************************
\r
146 // 87 /*! \brief Main program
\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
154 // 95 * \todo The variable inp is passed to all state functions, but is not used
\r
155 // 96 * for anything yet. Remove?
\r
158 RSEG CODE:CODE:NOROOT(1)
\r
159 // 98 int main( void )
\r
164 // 100 unsigned char nextstate, inp, i;
\r
165 // 101 unsigned char (*pStateFunc)(unsigned char); // Function pointer.
\r
167 // 103 // Initialize local state variables.
\r
169 // 105 CurrentState = nextstate = ST_INIT;
\r
171 STS CurrentState, R18
\r
172 // 106 pStateFunc = NULL;
\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
180 // 110 if (menu_state[i].state == CurrentState) {
\r
184 // 111 pStateFunc = menu_state[i].pFunc;
\r
195 MOVW R23:R22, R21:R20
\r
200 LDI R30, LOW(menu_state)
\r
201 LDI R31, (menu_state) >> 8
\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
216 // 118 nextstate = pStateFunc(inp);
\r
217 MOVW R31:R30, R25:R24
\r
222 // 121 // Look up function for next state, if it differs from the current.
\r
223 // 122 if (nextstate != CurrentState) {
\r
225 LDS R16, CurrentState
\r
228 // 123 CurrentState = nextstate;
\r
229 STS CurrentState, R18
\r
230 // 124 for ( i = 0; menu_state[i].state != 0; i++) {
\r
235 MOVW R23:R22, R21:R20
\r
240 LDI R30, LOW(menu_state)
\r
241 LDI R31, (menu_state) >> 8
\r
247 // 125 if (menu_state[i].state == CurrentState) {
\r
248 LDS R19, CurrentState
\r
251 // 126 pStateFunc = menu_state[i].pFunc;
\r
264 ASEGN ABSOLUTE:DATA:NOROOT,01cH
\r
267 ASEGN ABSOLUTE:DATA:NOROOT,01dH
\r
270 ASEGN ABSOLUTE:DATA:NOROOT,01eH
\r
273 ASEGN ABSOLUTE:DATA:NOROOT,01fH
\r
276 RSEG INITTAB:CODE:NOROOT(0)
\r
277 `?<Segment init: NEAR_Z>`:
\r
278 DW SFE(NEAR_Z) - SFB(NEAR_Z)
\r
281 REQUIRE ?need_segment_init
\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
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
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
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
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
312 // 160 * Required fuse bit settings:
\r
314 // 162 * FUSE BIT | SETTING
\r
315 // 163 * ----------+--------------------
\r
316 // 164 * CKDIV8 | 1 (unprogrammed)
\r
317 // 165 * CKSEL3..0 | 0010 (internal osc)
\r
321 // 169 * \section todo To Do-list
\r
323 // 171 * - Finalize master-slave communication protocol
\r
324 // 172 * - Implement discharge mode
\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
334 // 182 /*! \page misra MISRA C rule violations
\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
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
345 // 193 * \par Rule 37
\r
346 // 194 * <i>"Bitwise operations shall not be performed on signed integer types."</i>
\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
353 // 201 * \par Rule 45
\r
354 // 202 * <i>"Type casting from any type to or from pointers shall not be used."</i>
\r
356 // 204 * Assigning macro NULL, defined in stdlib.h, to a pointer causes this..
\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
363 // 211 * It is difficult to use parentheses with void function-like macros.
\r
366 // 132 bytes in segment CODE
\r
367 // 6 bytes in segment INITTAB
\r
368 // 1 byte in segment NEAR_Z
\r
370 // 132 bytes of CODE memory (+ 6 bytes shared)
\r
371 // 1 byte of DATA memory
\r