📄 main.lst
字号:
##############################################################################
# #
# IAR Atmel AVR C/C++ Compiler V4.11A/W32 17/Jan/2006 19:30:50 #
# Copyright 1996-2005 IAR Systems. All rights reserved. #
# #
# Source file = C:\Documents and Settings\andeaper\My #
# Documents\QVCS\Appnotes\AVR109 Self Programming\Source #
# Code\IAR\ATmega2560\main.c #
# Command line = "C:\Documents and Settings\andeaper\My #
# Documents\QVCS\Appnotes\AVR109 Self Programming\Source #
# Code\IAR\ATmega2560\main.c" --cpu=m2560 -ms -o #
# "C:\Documents and Settings\andeaper\My #
# Documents\QVCS\Appnotes\AVR109 Self Programming\Source #
# Code\IAR\ATmega2560\Release\Obj\" -lCN "C:\Documents #
# and Settings\andeaper\My Documents\QVCS\Appnotes\AVR109 #
# Self Programming\Source Code\IAR\ATmega2560\Release\Li #
# st\" -y --initializers_in_flash -z6 --no_inline #
# --no_cross_call --no_tbaa --cross_call_passes=4 #
# --debug -DENABLE_BIT_DEFINITIONS --do_cross_call -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 4096 #
# List file = C:\Documents and Settings\andeaper\My #
# Documents\QVCS\Appnotes\AVR109 Self Programming\Source #
# Code\IAR\ATmega2560\Release\List\main.lst #
# Object file = C:\Documents and Settings\andeaper\My #
# Documents\QVCS\Appnotes\AVR109 Self Programming\Source #
# Code\IAR\ATmega2560\Release\Obj\main.r90 #
# #
# #
##############################################################################
C:\Documents and Settings\andeaper\My Documents\QVCS\Appnotes\AVR109 Self Programming\Source Code\IAR\ATmega2560\main.c
1 /*****************************************************************************
2 *
3 * Atmel Corporation
4 *
5 * File : main.c
6 * Compiler : IAR C 3.10C Kickstart
7 * Revision : $Revision: 2.0 $
8 * Date : $Date: Tuesday, November 29, $
9 * Updated by : $Author: raapeland $
10 *
11 * Support mail : avr@atmel.com
12 *
13 * Target platform : All AVRs with bootloader support
14 *
15 * AppNote : AVR109 - Self-programming
16 *
17 * Description : This Program allows an AVR with bootloader capabilities to
18 * Read/write its own Flash/EEprom. To enter Programming mode
19 * an input pin is checked. If this pin is pulled low, programming mode
20 * is entered. If not, normal execution is done from $0000
21 * "reset" vector in Application area.
22 *
23 * Preparations : Use the preprocessor.xls file for obtaining a customized
24 * defines.h file and linker-file code-segment definition for
25 * the device you are compiling for.
26 ****************************************************************************/
27 #include <inavr.h>
28 #include "defines.h"
\ In segment ABSOLUTE, at 0x57
\ union <unnamed> volatile __io _A_SPMCSR
\ _A_SPMCSR:
\ 00000000 DS 1
\ In segment ABSOLUTE, at 0x41
\ union <unnamed> volatile __io _A_EEAR
\ _A_EEAR:
\ 00000000 DS 2
\ In segment ABSOLUTE, at 0x40
\ union <unnamed> volatile __io _A_EEDR
\ _A_EEDR:
\ 00000000 DS 1
\ In segment ABSOLUTE, at 0x3f
\ union <unnamed> volatile __io _A_EECR
\ _A_EECR:
\ 00000000 DS 1
\ In segment ABSOLUTE, at 0x2b
\ union <unnamed> volatile __io _A_PORTD
\ _A_PORTD:
\ 00000000 DS 1
\ In segment ABSOLUTE, at 0x29
\ union <unnamed> volatile __io _A_PIND
\ _A_PIND:
\ 00000000 DS 1
29 #include "serial.h"
30 #include "flash.h"
31
32
33 #ifndef EEWE
34 #define EEWE EEPE
35 #endif
36 #ifndef EEMWE
37 #define EEMWE EEMPE
38 #endif
39
40 /*** WARNING: The AVRprog application in AVR Studio does not support block mode,
41 but it will try to use it anyway. Therefore, block mode must be disabled below
42 if AVRprog is going to be used. ***/
43
44
45 /* Uncomment the following to save code space */
46 #define REMOVE_AVRPROG_SUPPORT
47 //#define REMOVE_FUSE_AND_LOCK_BIT_SUPPORT
48 //#define REMOVE_BLOCK_SUPPORT
49 //#define REMOVE_EEPROM_BYTE_SUPPORT
50 //#define REMOVE_FLASH_BYTE_SUPPORT
51
52
53
54 #ifndef REMOVE_BLOCK_SUPPORT
55 unsigned char BlockLoad(unsigned int size, unsigned char mem, long *address);
56 void BlockRead(unsigned int size, unsigned char mem, long *address);
57
58 /* BLOCKSIZE should be chosen so that the following holds: BLOCKSIZE*n = PAGESIZE, where n=1,2,3... */
59 #define BLOCKSIZE PAGESIZE
60
61 #endif
62
63
64
\ In segment FARCODE, align 2, keep-with-next
65 __C_task void main(void)
\ main:
66 {
\ 00000000 9724 SBIW R29:R28, 4
67 long address;
68 unsigned int temp_int;
69 unsigned char val;
70
71
72 /* Initialization */
73 void (*funcptr)( void ) = 0x0000; // Set up function pointer to RESET vector.
74 PROGPORT |= (1<<PROG_NO); // Enable pull-up on PROG_NO line on PROGPORT.
\ 00000002 9A5C SBI 0x0B, 0x04
75 initbootuart(); // Initialize UART.
\ 00000004 ........ CALL initbootuart
76
77 /* Branch to bootloader or application code? */
78 if( !(PROGPIN & (1<<PROG_NO)) ) // If PROGPIN is pulled low, enter programmingmode.
\ 00000008 9B4C SBIS 0x09, 0x04
\ 0000000A C011 RJMP ??main_0
79 {
80 /* Main loop */
81 for(;;)
82 {
83 val=recchar(); // Wait for command character.
84
85 // Check autoincrement status.
86 if(val=='a')
87 {
88 sendchar('Y'); // Yes, we do autoincrement.
89 }
90
91
92 // Set address.
93 else if(val=='A') // Set address...
94 { // NOTE: Flash addresses are given in words, not bytes.
95 unsigned char high = recchar();
96 unsigned char low = recchar();
97 address = ((long)high<<8) | (long)low;
98 sendchar('\r'); // Send OK back.
99 }
100
101 // Set 24-bit address.
102 else if(val=='H')
103 { // NOTE: Flash addresses are given in words, not bytes.
104 unsigned char highest = recchar();
105 unsigned char high = recchar();
106 unsigned char low = recchar();
107 address = ((long)highest << 16) | ((long)high << 8) | (long)low;
108 sendchar('\r'); // Send OK back.
109 }
110
111 // Chip erase.
112 else if(val=='e')
113 {
114 for(address = 0; address < APP_END;address += PAGESIZE)
115 { // NOTE: Here we use address as a byte-address, not word-address, for convenience.
116 _WAIT_FOR_SPM();
117 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
118 _PAGE_ERASE( address );
119 #pragma diag_default=Pe1053 // Back to default.
120 }
121
122 sendchar('\r'); // Send OK back.
123 }
124
125 #ifndef REMOVE_BLOCK_SUPPORT
126 // Check block load support.
127 else if(val=='b')
128 {
129 sendchar('Y'); // Report block load supported.
130 sendchar((BLOCKSIZE>>8) & 0xFF); // MSB first.
131 sendchar(BLOCKSIZE&0xFF); // Report BLOCKSIZE (bytes).
132 }
133
134
135 // Start block load.
136 else if(val=='B')
137 {
138 temp_int = (recchar()<<8) | recchar(); // Get block size.
139 val = recchar(); // Get memtype.
140 sendchar( BlockLoad(temp_int,val,&address) ); // Block load.
141 }
142
143
144 // Start block read.
145 else if(val=='g')
146 {
147 temp_int = (recchar()<<8) | recchar(); // Get block size.
148 val = recchar(); // Get memtype
149 BlockRead(temp_int,val,&address); // Block read
150 }
151 #endif
152
153 #ifndef REMOVE_FLASH_BYTE_SUPPORT
154 // Read program memory.
155 else if(val=='R')
156 {
157 // Send high byte, then low byte of flash word.
158 _WAIT_FOR_SPM();
159 _ENABLE_RWW_SECTION();
160 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
161 sendchar( _LOAD_PROGRAM_MEMORY( (address << 1)+1 ) );
162 sendchar( _LOAD_PROGRAM_MEMORY( (address << 1)+0 ) );
163 #pragma diag_default=Pe1053 // Back to default.
164
165 address++; // Auto-advance to next Flash word.
166 }
167
168
169 // Write program memory, low byte.
170 else if(val=='c')
171 { // NOTE: Always use this command before sending high byte.
172 temp_int=recchar(); // Get low byte for later _FILL_TEMP_WORD.
173 sendchar('\r'); // Send OK back.
174 }
175
176
177 // Write program memory, high byte.
178 else if(val=='C')
179 {
180 temp_int |= (recchar()<<8); // Get and insert high byte.
181 _WAIT_FOR_SPM();
182 #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
183 _FILL_TEMP_WORD( (address << 1), temp_int ); // Convert word-address to byte-address and fill.
184 #pragma diag_default=Pe1053 // Back to default.
185 address++; // Auto-advance to next Flash word.
186 sendchar('\r'); // Send OK back.
187 }
188
189
190 // Write page.
191 else if(val== 'm')
192 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -