/////////////////////////////////////////////////////////////////////////////// // / // IAR Atmel AVR C/C++ Compiler V4.30F/W32 13/Mar/2008 04:52:00 / // Copyright 1996-2007 IAR Systems. All rights reserved. / // / // Source file = C:\home\kevin\pub\src\bc100\IAR\main.c / // Command line = C:\home\kevin\pub\src\bc100\IAR\main.c / // --cpu=tiny861 -ms -o C:\home\kevin\pub\src\bc1 / // 00\IAR\Release\Obj\ -D NDEBUG -lCN / // C:\home\kevin\pub\src\bc100\IAR\Release\List\ / // -lB C:\home\kevin\pub\src\bc100\IAR\Release\Li / // st\ --initializers_in_flash -s9 / // --no_cross_call --no_tbaa / // -DENABLE_BIT_DEFINITIONS -e -I "C:\Program / // Files\IAR Systems\Embedded Workbench / // 4.0\avr\INC\" -I "C:\Program Files\IAR / // Systems\Embedded Workbench 4.0\avr\INC\CLIB\" / // --eeprom_size 512 --misrac=5-9,11-12,14,16-17, / // 19-21,24-26,29-32,34-35,38-39,42-43,46,50, / // 52-54,56-59,61-62,64-65,68-80,83-84,87-91, / // 94-95,98-100,103-110,112-126 / // Enabled MISRA C rules = 5-9,11-12,14,16-17,19-21,24-26,29-32,34-35, / // 38-39,42-43,46,50,52-54,56-59,61-62,64-65, / // 68-80,83-84,87-91,94-95,98-100,103-110,112-126 / // Checked = 5,7-9,11-12,14,17,19-21,24,29-32,34-35,38-39, / // 42,46,50,52-54,56-59,61-62,64,68-69,71-80, / // 83-84,87-89,91,94-95,98,100,104-105,108-109, / // 112-115,118-126 / // Not checked = 6,16,25-26,43,65,70,90,99,103,106-107,110, / // 116-117 / // List file = C:\home\kevin\pub\src\bc100\IAR\Release\List\m / // ain.s90 / // / // / /////////////////////////////////////////////////////////////////////////////// NAME main RSEG CSTACK:DATA:NOROOT(0) RSEG RSTACK:DATA:NOROOT(0) EXTERN ?need_segment_init PUBWEAK `?` PUBLIC CurrentState PUBWEAK __?EEARH PUBWEAK __?EEARL PUBWEAK __?EECR PUBWEAK __?EEDR PUBLIC main EXTERN menu_state // C:\home\kevin\pub\src\bc100\IAR\main.c // 1 /* This file has been prepared for Doxygen automatic documentation generation.*/ // 2 /*! \file ********************************************************************* // 3 * // 4 * \brief // 5 * Main program file // 6 * // 7 * Contains the main program, which is a basic state machine. // 8 * // 9 * \par Application note: // 10 * AVR458: Charging Li-Ion Batteries with BC100 \n // 11 * AVR463: Charging NiMH Batteries with BC100 // 12 * // 13 * \par Documentation // 14 * For comprehensive code documentation, supported compilers, compiler // 15 * settings and supported devices see readme.html // 16 * // 17 * \author // 18 * Atmel Corporation: http://www.atmel.com \n // 19 * Support email: avr@atmel.com // 20 * // 21 * // 22 * $Name$ // 23 * $Revision: 2302 $ // 24 * $RCSfile$ // 25 * $URL: http://svn.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/tag/20070904_release_1.0/code/IAR/main.c $ // 26 * $Date: 2007-08-23 14:57:36 +0200 (to, 23 aug 2007) $\n // 27 ******************************************************************************/ // 28 // 29 /*! \page License // 30 * Copyright (c) 2007, Atmel Corporation All rights reserved. // 31 * // 32 * Redistribution and use in source and binary forms, with or without // 33 * modification, are permitted provided that the following conditions are met: // 34 * // 35 * 1. Redistributions of source code must retain the above copyright notice, // 36 * this list of conditions and the following disclaimer. // 37 * // 38 * 2. Redistributions in binary form must reproduce the above copyright notice, // 39 * this list of conditions and the following disclaimer in the documentation // 40 * and/or other materials provided with the distribution. // 41 * // 42 * 3. The name of ATMEL may not be used to endorse or promote products derived // 43 * from this software without specific prior written permission. // 44 * // 45 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED // 46 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // 47 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND // 48 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, // 49 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // 50 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // 51 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // 52 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // 53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // 54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // 55 */ // 56 // 57 #include // 58 #include // 59 #include // 60 // 61 #include "structs.h" // 62 // 63 #include "main.h" // 64 #include "ADC.h" // 65 #include "statefunc.h" // 66 #include "battery.h" // 67 #include "menu.h" // 68 #include "OWI.h" // 69 #include "PWM.h" // 70 #include "time.h" // 71 #include "USI.h" // 72 // 73 // 74 // 75 //****************************************************************************** // 76 // Globals // 77 //****************************************************************************** RSEG NEAR_Z:DATA:NOROOT(0) REQUIRE `?` // 78 unsigned char CurrentState; //!< \brief Global that indicates current state CurrentState: DS 1 // 79 //!< // 80 //!< Updated by main(). // 81 //!< \note See menu.h for definition of states. // 82 // 83 // 84 //****************************************************************************** // 85 // Functions // 86 //****************************************************************************** // 87 /*! \brief Main program // 88 * // 89 * The main function goes into an infinite loop, keeping track of the current // 90 * state and the next one. If the next state is different from the current, it // 91 * looks up the address to the next state function, in \ref menu_state[], and // 92 * updates \ref CurrentState. The state function is then called and will // 93 * eventually return a new state, and so the loop reiterates. // 94 * // 95 * \todo The variable inp is passed to all state functions, but is not used // 96 * for anything yet. Remove? // 97 */ RSEG CODE:CODE:NOROOT(1) // 98 int main( void ) main: // 99 { ST -Y, R25 ST -Y, R24 // 100 unsigned char nextstate, inp, i; // 101 unsigned char (*pStateFunc)(unsigned char); // Function pointer. // 102 // 103 // Initialize local state variables. // 104 inp = ZERO; // 105 CurrentState = nextstate = ST_INIT; LDI R18, 10 STS CurrentState, R18 // 106 pStateFunc = NULL; LDI R24, 0 LDI R25, 0 // 107 // 108 // Look for function associated with current state, get its address. // 109 for (i = 0; menu_state[i].state != 0; i++) { LDI R16, 0 RJMP ??main_0 // 110 if (menu_state[i].state == CurrentState) { ??main_1: CPI R17, 10 BRNE ??main_2 // 111 pStateFunc = menu_state[i].pFunc; ADIW R31:R30, 1 LPM R24, Z+ LPM R25, Z // 112 } // 113 } ??main_2: INC R16 ??main_0: MOV R20, R16 LDI R21, 0 MOVW R23:R22, R21:R20 LSL R20 ROL R21 ADD R20, R22 ADC R21, R23 LDI R30, LOW(menu_state) LDI R31, (menu_state) >> 8 ADD R30, R20 ADC R31, R21 LPM R17, Z TST R17 BRNE ??main_1 // 114 // 115 while (TRUE) { // 116 // Run function associated with current state, get next state in return. // 117 if (pStateFunc != NULL){ ??main_3: LDI R16, 0 CPI R24, 0 CPC R25, R16 BREQ ??main_4 // 118 nextstate = pStateFunc(inp); MOVW R31:R30, R25:R24 ICALL MOV R18, R16 // 119 } // 120 // 121 // Look up function for next state, if it differs from the current. // 122 if (nextstate != CurrentState) { ??main_4: LDS R16, CurrentState CP R18, R16 BREQ ??main_3 // 123 CurrentState = nextstate; STS CurrentState, R18 // 124 for ( i = 0; menu_state[i].state != 0; i++) { LDI R16, 0 ??main_5: MOV R20, R16 LDI R21, 0 MOVW R23:R22, R21:R20 LSL R20 ROL R21 ADD R20, R22 ADC R21, R23 LDI R30, LOW(menu_state) LDI R31, (menu_state) >> 8 ADD R30, R20 ADC R31, R21 LPM R17, Z TST R17 BREQ ??main_3 // 125 if (menu_state[i].state == CurrentState) { LDS R19, CurrentState CP R17, R19 BRNE ??main_6 // 126 pStateFunc = menu_state[i].pFunc; ADIW R31:R30, 1 LPM R24, Z+ LPM R25, Z // 127 } // 128 } ??main_6: INC R16 RJMP ??main_5 // 129 } // 130 } // 131 } ASEGN ABSOLUTE:DATA:NOROOT,01cH __?EECR: ASEGN ABSOLUTE:DATA:NOROOT,01dH __?EEDR: ASEGN ABSOLUTE:DATA:NOROOT,01eH __?EEARL: ASEGN ABSOLUTE:DATA:NOROOT,01fH __?EEARH: RSEG INITTAB:CODE:NOROOT(0) `?`: DW SFE(NEAR_Z) - SFB(NEAR_Z) DW SFB(NEAR_Z) DW 0 REQUIRE ?need_segment_init END // 132 // 133 // 134 /* Doxygen documentation mainpage ********************************************/ // 135 /*! \mainpage // 136 * \section intro Introduction // 137 * This documents the software for application note AVR458. This is a charger // 138 * for Li-Ion batteries, based on ATAVRBC100. // 139 * // 140 * \section compinfo Compilation Info // 141 * This software was compiled with IAR Embedded Workbench, 4.30. To use GCC // 142 * the source have to be modified.\n // 143 * \n // 144 * To make project on IAR EWAVR:\n // 145 * Add the .c files to project (ADC.c, battery.c, main.c, menu.c, // 146 * OWI.c, PWM.c, time.c and USI.c). Add either LIIONcharge.c or NIMHcharge.c, // 147 * and update LIIONspecs.h or NIMHspecs.h, and battery.h with the appropriate // 148 * battery data if needed.\n // 149 * \n // 150 * Use device --cpu=tiny861, enable bit definitions in I/O include files, // 151 * optimization low for debug target and high for release, output format: ubrof8 // 152 * for Debug and intel_extended for Release. \n // 153 * // 154 * \section deviceinfo Device Info // 155 * This application is based on the ATtiny 861, but it is possible to migrate // 156 * the design to other AVR microcontrollers, such as pin-compatible devices // 157 * ATtiny 261/461. Low pin count devices such as ATtiny 25/45/85 may also be // 158 * used, but with reduced functionality. // 159 * // 160 * Required fuse bit settings: // 161 *
//  162  *    FUSE BIT  | SETTING
//  163  *    ----------+--------------------
//  164  *    CKDIV8    | 1 (unprogrammed)
//  165  *    CKSEL3..0 | 0010 (internal osc)
//  166  *    
// 167 * // 168 * // 169 * \section todo To Do-list // 170 * \todo // 171 * - Finalize master-slave communication protocol // 172 * - Implement discharge mode // 173 * // 174 * \section contactinfo Contact Info // 175 * For more info about Atmel AVR visit http://www.atmel.com/products/AVR/ \n // 176 * For application notes visit // 177 * http://www.atmel.com/dyn/products/app_notes.asp?family_id=607 \n // 178 * Support mail: avr@atmel.com // 179 */ // 180 // 181 // 182 /*! \page misra MISRA C rule violations // 183 * // 184 * \par Rule 1 // 185 * "All code shall conform to ISO 9899 standard C, with no extensions // 186 * permitted." // 187 * // 188 * Extensions are necessary because ISO C has no way of specifying that a // 189 * function should be an interrupt service routine, or that we would like data // 190 * members to be stored in f.ex. EEPROM. // 191 * // 192 * // 193 * \par Rule 37 // 194 * "Bitwise operations shall not be performed on signed integer types." // 195 * // 196 * The compiler assumes all the 1's we shift around to make bitmasks are signed // 197 * integers. Specifying them all to be (unsigned int), either directly or via a // 198 * definition, would fix the rule violations, but also reduce code readability. // 199 * // 200 * // 201 * \par Rule 45 // 202 * "Type casting from any type to or from pointers shall not be used." // 203 * // 204 * Assigning macro NULL, defined in stdlib.h, to a pointer causes this.. // 205 * // 206 * // 207 * \par Rule 96 // 208 * "In the definition of a function-like macro the whole definition, and each // 209 * instance of a parameter, shall be enclosed in parentheses." // 210 * // 211 * It is difficult to use parentheses with void function-like macros. // 212 */ // // 132 bytes in segment CODE // 6 bytes in segment INITTAB // 1 byte in segment NEAR_Z // // 132 bytes of CODE memory (+ 6 bytes shared) // 1 byte of DATA memory // //Errors: none //Warnings: none