1 /* This file has been prepared for Doxygen automatic documentation generation.*/
\r
2 /*! \file *********************************************************************
\r
7 * Contains the main program, which is a basic state machine.
\r
9 * \par Application note:
\r
10 * AVR458: Charging Li-Ion Batteries with BC100 \n
\r
11 * AVR463: Charging NiMH Batteries with BC100
\r
13 * \par Documentation
\r
14 * For comprehensive code documentation, supported compilers, compiler
\r
15 * settings and supported devices see readme.html
\r
18 * Atmel Corporation: http://www.atmel.com \n
\r
19 * Support email: avr@atmel.com
\r
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
26 * $Date: 2007-08-23 14:57:36 +0200 (to, 23 aug 2007) $\n
\r
27 ******************************************************************************/
\r
30 * Copyright (c) 2007, Atmel Corporation All rights reserved.
\r
32 * Redistribution and use in source and binary forms, with or without
\r
33 * modification, are permitted provided that the following conditions are met:
\r
35 * 1. Redistributions of source code must retain the above copyright notice,
\r
36 * this list of conditions and the following disclaimer.
\r
38 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
39 * this list of conditions and the following disclaimer in the documentation
\r
40 * and/or other materials provided with the distribution.
\r
42 * 3. The name of ATMEL may not be used to endorse or promote products derived
\r
43 * from this software without specific prior written permission.
\r
45 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
\r
46 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
47 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
\r
48 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
\r
49 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
\r
50 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
51 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
\r
52 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
61 #include "structs.h"
\r
65 #include "statefunc.h"
\r
66 #include "battery.h"
\r
75 //******************************************************************************
\r
77 //******************************************************************************
\r
78 unsigned char CurrentState; //!< \brief Global that indicates current state
\r
80 //!< Updated by main().
\r
81 //!< \note See menu.h for definition of states.
\r
84 //******************************************************************************
\r
86 //******************************************************************************
\r
87 /*! \brief Main program
\r
89 * The main function goes into an infinite loop, keeping track of the current
\r
90 * state and the next one. If the next state is different from the current, it
\r
91 * looks up the address to the next state function, in \ref menu_state[], and
\r
92 * updates \ref CurrentState. The state function is then called and will
\r
93 * eventually return a new state, and so the loop reiterates.
\r
95 * \todo The variable inp is passed to all state functions, but is not used
\r
96 * for anything yet. Remove?
\r
100 unsigned char nextstate, inp, i;
\r
101 unsigned char (*pStateFunc)(unsigned char); // Function pointer.
\r
103 // Initialize local state variables.
\r
105 CurrentState = nextstate = ST_INIT;
\r
108 // Look for function associated with current state, get its address.
\r
109 for (i = 0; menu_state[i].state != 0; i++) {
\r
110 if (menu_state[i].state == CurrentState) {
\r
111 pStateFunc = menu_state[i].pFunc;
\r
116 // Run function associated with current state, get next state in return.
\r
117 if (pStateFunc != NULL){
\r
118 nextstate = pStateFunc(inp);
\r
121 // Look up function for next state, if it differs from the current.
\r
122 if (nextstate != CurrentState) {
\r
123 CurrentState = nextstate;
\r
124 for ( i = 0; menu_state[i].state != 0; i++) {
\r
125 if (menu_state[i].state == CurrentState) {
\r
126 pStateFunc = menu_state[i].pFunc;
\r
134 /* Doxygen documentation mainpage ********************************************/
\r
136 * \section intro Introduction
\r
137 * This documents the software for application note AVR458. This is a charger
\r
138 * for Li-Ion batteries, based on ATAVRBC100.
\r
140 * \section compinfo Compilation Info
\r
141 * This software was compiled with IAR Embedded Workbench, 4.30. To use GCC
\r
142 * the source have to be modified.\n
\r
144 * To make project on IAR EWAVR:\n
\r
145 * Add the .c files to project (ADC.c, battery.c, main.c, menu.c,
\r
146 * OWI.c, PWM.c, time.c and USI.c). Add either LIIONcharge.c or NIMHcharge.c,
\r
147 * and update LIIONspecs.h or NIMHspecs.h, and battery.h with the appropriate
\r
148 * battery data if needed.\n
\r
150 * Use device --cpu=tiny861, enable bit definitions in I/O include files,
\r
151 * optimization low for debug target and high for release, output format: ubrof8
\r
152 * for Debug and intel_extended for Release. \n
\r
154 * \section deviceinfo Device Info
\r
155 * This application is based on the ATtiny 861, but it is possible to migrate
\r
156 * the design to other AVR microcontrollers, such as pin-compatible devices
\r
157 * ATtiny 261/461. Low pin count devices such as ATtiny 25/45/85 may also be
\r
158 * used, but with reduced functionality.
\r
160 * Required fuse bit settings:
\r
162 * FUSE BIT | SETTING
\r
163 * ----------+--------------------
\r
164 * CKDIV8 | 1 (unprogrammed)
\r
165 * CKSEL3..0 | 0010 (internal osc)
\r
169 * \section todo To Do-list
\r
171 * - Finalize master-slave communication protocol
\r
172 * - Implement discharge mode
\r
174 * \section contactinfo Contact Info
\r
175 * For more info about Atmel AVR visit http://www.atmel.com/products/AVR/ \n
\r
176 * For application notes visit
\r
177 * http://www.atmel.com/dyn/products/app_notes.asp?family_id=607 \n
\r
178 * Support mail: avr@atmel.com
\r
182 /*! \page misra MISRA C rule violations
\r
185 * <i>"All code shall conform to ISO 9899 standard C, with no extensions
\r
188 * Extensions are necessary because ISO C has no way of specifying that a
\r
189 * function should be an interrupt service routine, or that we would like data
\r
190 * members to be stored in f.ex. EEPROM.
\r
194 * <i>"Bitwise operations shall not be performed on signed integer types."</i>
\r
196 * The compiler assumes all the 1's we shift around to make bitmasks are signed
\r
197 * integers. Specifying them all to be (unsigned int), either directly or via a
\r
198 * definition, would fix the rule violations, but also reduce code readability.
\r
202 * <i>"Type casting from any type to or from pointers shall not be used."</i>
\r
204 * Assigning macro NULL, defined in stdlib.h, to a pointer causes this..
\r
208 * <i>"In the definition of a function-like macro the whole definition, and each
\r
209 * instance of a parameter, shall be enclosed in parentheses."</i>
\r
211 * It is difficult to use parentheses with void function-like macros.
\r