sanguino/bootloaders/atmega/ATmegaBOOT.c

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
permissions
-rw-r--r--

added missing sanguino files

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1 /**********************************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2 /* Serial Bootloader for Atmel megaAVR Controllers */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4 /* tested with ATmega8, ATmega128 and ATmega168 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 /* should work with other mega's, see code for details */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7 /* ATmegaBOOT.c */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 /* 20090308: integrated Mega changes into main bootloader */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11 /* source by D. Mellis */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12 /* 20080930: hacked for Arduino Mega (with the 1280 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 /* processor, backwards compatible) */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14 /* by D. Cuartielles */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 /* 20070626: hacked for Arduino Diecimila (which auto- */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16 /* resets when a USB connection is made to it) */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17 /* by D. Mellis */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18 /* 20060802: hacked for Arduino by D. Cuartielles */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 /* based on a previous hack by D. Mellis */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20 /* and D. Cuartielles */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22 /* Monitor and debug functions were added to the original */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23 /* code by Dr. Erik Lins, chip45.com. (See below) */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25 /* Thanks to Karl Pitrich for fixing a bootloader pin */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26 /* problem and more informative LED blinking! */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28 /* For the latest version see: */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29 /* http://www.chip45.com/ */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31 /* ------------------------------------------------------ */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33 /* based on stk500boot.c */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 /* Copyright (c) 2003, Jason P. Kyle */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35 /* All rights reserved. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36 /* see avr1.org for original file and information */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38 /* This program is free software; you can redistribute it */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39 /* and/or modify it under the terms of the GNU General */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40 /* Public License as published by the Free Software */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41 /* Foundation; either version 2 of the License, or */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 /* (at your option) any later version. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44 /* This program is distributed in the hope that it will */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 /* be useful, but WITHOUT ANY WARRANTY; without even the */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46 /* implied warranty of MERCHANTABILITY or FITNESS FOR A */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 /* PARTICULAR PURPOSE. See the GNU General Public */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 /* License for more details. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 /* You should have received a copy of the GNU General */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51 /* Public License along with this program; if not, write */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52 /* to the Free Software Foundation, Inc., */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53 /* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 /* Licence can be viewed at */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 /* http://www.fsf.org/licenses/gpl.txt */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58 /* Target = Atmel AVR m128,m64,m32,m16,m8,m162,m163,m169, */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 /* m8515,m8535. ATmega161 has a very small boot block so */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 /* isn't supported. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61 /* */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62 /* Tested with m168 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 /**********************************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 /* $Id$ */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68 /* some includes */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69 #include <inttypes.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70 #include <avr/io.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71 #include <avr/pgmspace.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72 #include <avr/interrupt.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73 #include <avr/wdt.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74 #include <util/delay.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76 /* the current avr-libc eeprom functions do not support the ATmega168 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77 /* own eeprom write/read functions are used instead */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78 #if !defined(__AVR_ATmega168__) || !defined(__AVR_ATmega328P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79 #include <avr/eeprom.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82 /* Use the F_CPU defined in Makefile */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84 /* 20060803: hacked by DojoCorp */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85 /* 20070626: hacked by David A. Mellis to decrease waiting time for auto-reset */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86 /* set the waiting time for the bootloader */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87 /* get this from the Makefile instead */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88 /* #define MAX_TIME_COUNT (F_CPU>>4) */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90 /* 20070707: hacked by David A. Mellis - after this many errors give up and launch application */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91 #define MAX_ERROR_COUNT 5
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92 #define NUM_LED_FLASHES 3
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93 /* set the UART baud rate */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94 /* 20060803: hacked by DojoCorp */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95 //#define BAUD_RATE 115200
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 #ifndef BAUD_RATE
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
97 #define BAUD_RATE 19200
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
98 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
99
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
100
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
101 /* SW_MAJOR and MINOR needs to be updated from time to time to avoid warning message from AVR Studio */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
102 /* never allow AVR Studio to do an update !!!! */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
103 #define HW_VER 0x02
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
104 #define SW_MAJOR 0x01
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
105 #define SW_MINOR 0x10
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
106
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
107
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
108 /* Adjust to suit whatever pin your hardware uses to enter the bootloader */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
109 /* ATmega128 has two UARTS so two pins are used to enter bootloader and select UART */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
110 /* ATmega1280 has four UARTS, but for Arduino Mega, we will only use RXD0 to get code */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
111 /* BL0... means UART0, BL1... means UART1 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
112 #ifdef __AVR_ATmega128__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
113 #define BL_DDR DDRF
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
114 #define BL_PORT PORTF
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
115 #define BL_PIN PINF
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
116 #define BL0 PINF7
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
117 #define BL1 PINF6
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
118 #elif defined __AVR_ATmega1280__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
119 /* we just don't do anything for the MEGA and enter bootloader on reset anyway*/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
120 #elif defined __AVR_ATmega1284P__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
121
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
122 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
123 /* other ATmegas have only one UART, so only one pin is defined to enter bootloader */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
124 #define BL_DDR DDRD
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
125 #define BL_PORT PORTD
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
126 #define BL_PIN PIND
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
127 #define BL PIND6
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
128 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
129
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
130
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
131 /* onboard LED is used to indicate, that the bootloader was entered (3x flashing) */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
132 /* if monitor functions are included, LED goes on after monitor was entered */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
133 #if defined __AVR_ATmega128__ || defined __AVR_ATmega1280__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
134 /* Onboard LED is connected to pin PB7 (e.g. Crumb128, PROBOmega128, Savvy128, Arduino Mega) */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
135 #define LED_DDR DDRB
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
136 #define LED_PORT PORTB
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
137 #define LED_PIN PINB
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
138 #define LED PINB7
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
139 #elif defined __AVR_ATmega1284P__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
140 #define LED_DDR DDRB
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
141 #define LED_PORT PORTB
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
142 #define LED_PIN PINB
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
143 #define LED PINB0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
144 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
145 /* Onboard LED is connected to pin PB5 in Arduino NG, Diecimila, and Duomilanuove */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
146 /* other boards like e.g. Crumb8, Crumb168 are using PB2 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
147 #define LED_DDR DDRB
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
148 #define LED_PORT PORTB
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
149 #define LED_PIN PINB
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
150 #define LED PINB5
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
151 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
152
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
153
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
154 /* monitor functions will only be compiled when using ATmega128, due to bootblock size constraints */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
155 #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
156 #define MONITOR 1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
157 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
158
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
159
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
160 /* define various device id's */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
161 /* manufacturer byte is always the same */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
162 #define SIG1 0x1E // Yep, Atmel is the only manufacturer of AVR micros. Single source :(
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
163
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
164 #if defined __AVR_ATmega1280__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
165 #define SIG2 0x97
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
166 #define SIG3 0x03
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
167 #define PAGE_SIZE 0x80U //128 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
168
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
169 #elif defined __AVR_ATmega1284P__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
170 #define SIG2 0x97
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
171 #define SIG3 0x05
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
172 #define PAGE_SIZE 0x080U //128 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
173
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
174 #elif defined __AVR_ATmega1281__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
175 #define SIG2 0x97
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
176 #define SIG3 0x04
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
177 #define PAGE_SIZE 0x80U //128 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
178
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
179 #elif defined __AVR_ATmega128__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
180 #define SIG2 0x97
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
181 #define SIG3 0x02
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
182 #define PAGE_SIZE 0x80U //128 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
183
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
184 #elif defined __AVR_ATmega64__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
185 #define SIG2 0x96
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
186 #define SIG3 0x02
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
187 #define PAGE_SIZE 0x80U //128 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
188
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
189 #elif defined __AVR_ATmega32__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
190 #define SIG2 0x95
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
191 #define SIG3 0x02
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
192 #define PAGE_SIZE 0x40U //64 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
193
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
194 #elif defined __AVR_ATmega16__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
195 #define SIG2 0x94
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
196 #define SIG3 0x03
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
197 #define PAGE_SIZE 0x40U //64 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
198
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
199 #elif defined __AVR_ATmega8__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
200 #define SIG2 0x93
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
201 #define SIG3 0x07
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
202 #define PAGE_SIZE 0x20U //32 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
203
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
204 #elif defined __AVR_ATmega88__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
205 #define SIG2 0x93
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
206 #define SIG3 0x0a
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
207 #define PAGE_SIZE 0x20U //32 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
208
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
209 #elif defined __AVR_ATmega168__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
210 #define SIG2 0x94
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
211 #define SIG3 0x06
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
212 #define PAGE_SIZE 0x40U //64 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
213
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
214 #elif defined __AVR_ATmega328P__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
215 #define SIG2 0x95
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
216 #define SIG3 0x0F
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
217 #define PAGE_SIZE 0x40U //64 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
218
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
219 #elif defined __AVR_ATmega162__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
220 #define SIG2 0x94
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
221 #define SIG3 0x04
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
222 #define PAGE_SIZE 0x40U //64 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
223
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
224 #elif defined __AVR_ATmega163__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
225 #define SIG2 0x94
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
226 #define SIG3 0x02
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
227 #define PAGE_SIZE 0x40U //64 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
228
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
229 #elif defined __AVR_ATmega169__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
230 #define SIG2 0x94
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
231 #define SIG3 0x05
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
232 #define PAGE_SIZE 0x40U //64 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
233
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
234 #elif defined __AVR_ATmega8515__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
235 #define SIG2 0x93
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
236 #define SIG3 0x06
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
237 #define PAGE_SIZE 0x20U //32 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
238
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
239 #elif defined __AVR_ATmega8535__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
240 #define SIG2 0x93
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
241 #define SIG3 0x08
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
242 #define PAGE_SIZE 0x20U //32 words
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
243 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
244
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
245
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
246 /* function prototypes */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
247 void putch(char);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
248 char getch(void);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
249 void getNch(uint8_t);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
250 void byte_response(uint8_t);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
251 void nothing_response(void);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
252 char gethex(void);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
253 void puthex(char);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
254 void flash_led(uint8_t);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
255
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
256 /* some variables */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
257 union address_union
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
258 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
259 uint16_t word;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
260 uint8_t byte[2];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
261 } address;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
262
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
263 union length_union
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
264 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
265 uint16_t word;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
266 uint8_t byte[2];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
267 } length;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
268
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
269 struct flags_struct
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
270 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
271 unsigned eeprom : 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
272 unsigned rampz : 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
273 } flags;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
274
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
275 uint8_t buff[256];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
276 uint8_t address_high;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
277
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
278 uint8_t pagesz=0x80;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
279
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
280 uint8_t i;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
281 uint8_t bootuart = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
282
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
283 uint8_t error_count = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
284
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
285 void (*app_start)(void) = 0x0000;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
286
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
287
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
288 /* main program starts here */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
289 int main(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
290 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
291 uint8_t ch,ch2;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
292 uint16_t w;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
293 #ifdef WATCHDOG_MODS
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
294 ch = MCUSR;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
295 MCUSR = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
296 WDTCSR |= _BV(WDCE) | _BV(WDE);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
297 WDTCSR = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
298 // Check if the WDT was used to reset, in which case we dont bootload and skip straight to the code. woot.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
299 if (! (ch & _BV(EXTRF))) // if its a not an external reset...
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
300 app_start(); // skip bootloader
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
301 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
302 asm volatile("nop\n\t");
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
303 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
304 /* set pin direction for bootloader pin and enable pullup */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
305 /* for ATmega128, two pins need to be initialized */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
306 #ifdef __AVR_ATmega128__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
307 BL_DDR &= ~_BV(BL0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
308 BL_DDR &= ~_BV(BL1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
309 BL_PORT |= _BV(BL0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
310 BL_PORT |= _BV(BL1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
311 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
312 /* We run the bootloader regardless of the state of this pin. Thus, don't
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
313 put it in a different state than the other pins. --DAM, 070709
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
314 This also applies to Arduino Mega -- DC, 080930
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
315 BL_DDR &= ~_BV(BL);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
316 BL_PORT |= _BV(BL);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
317 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
318 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
319 #ifdef __AVR_ATmega128__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
320 /* check which UART should be used for booting */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
321 if(bit_is_clear(BL_PIN, BL0))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
322 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
323 bootuart = 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
324 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
325 else if(bit_is_clear(BL_PIN, BL1))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
326 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
327 bootuart = 2;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
328 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
329 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
330 #if defined __AVR_ATmega1280__ || defined __AVR_ATmega1284P__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
331 /* the mega1280 chip has four serial ports ... we could eventually use any of them, or not? */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
332 /* however, we don't wanna confuse people, to avoid making a mess, we will stick to RXD0, TXD0 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
333 bootuart = 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
334 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
335 /* check if flash is programmed already, if not start bootloader anyway */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
336 if(pgm_read_byte_near(0x0000) != 0xFF)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
337 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
338 #ifdef __AVR_ATmega128__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
339 /* no UART was selected, start application */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
340 if(!bootuart)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
341 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
342 app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
343 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
344 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
345 /* check if bootloader pin is set low */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
346 /* we don't start this part neither for the m8, nor m168 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
347 //if(bit_is_set(BL_PIN, BL)) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
348 // app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
349 // }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
350 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
351 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
352 #ifdef __AVR_ATmega128__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
353 /* no bootuart was selected, default to uart 0 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
354 if(!bootuart)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
355 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
356 bootuart = 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
357 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
358 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
359 /* initialize UART(s) depending on CPU defined */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
360 #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
361 if(bootuart == 1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
362 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
363 UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
364 UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
365 UCSR0A = 0x00;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
366 UCSR0C = 0x06;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
367 UCSR0B = _BV(TXEN0)|_BV(RXEN0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
368 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
369 if(bootuart == 2)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
370 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
371 UBRR1L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
372 UBRR1H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
373 UCSR1A = 0x00;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
374 UCSR1C = 0x06;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
375 UCSR1B = _BV(TXEN1)|_BV(RXEN1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
376 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
377 #elif defined __AVR_ATmega163__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
378 UBRR = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
379 UBRRHI = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
380 UCSRA = 0x00;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
381 UCSRB = _BV(TXEN)|_BV(RXEN);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
382 #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
383 #ifdef DOUBLE_SPEED
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
384 UCSR0A = (1<<U2X0); //Double speed mode USART0
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
385 UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*8L)-1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
386 UBRR0H = (F_CPU/(BAUD_RATE*8L)-1) >> 8;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
387 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
388 UBRR0L = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
389 UBRR0H = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
390 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
391 UCSR0B = (1<<RXEN0) | (1<<TXEN0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
392 UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
393 /* Enable internal pull-up resistor on pin D0 (RX), in order
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
394 to supress line noise that prevents the bootloader from
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
395 timing out (DAM: 20070509) */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
396 DDRD &= ~_BV(PIND0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
397 PORTD |= _BV(PIND0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
398 #elif defined __AVR_ATmega8__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
399 /* m8 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
400 UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
401 UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
402 UCSRB = (1<<RXEN)|(1<<TXEN); // enable Rx & Tx
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
403 UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // config USART; 8N1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
404 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
405 /* m16,m32,m169,m8515,m8535 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
406 UBRRL = (uint8_t)(F_CPU/(BAUD_RATE*16L)-1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
407 UBRRH = (F_CPU/(BAUD_RATE*16L)-1) >> 8;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
408 UCSRA = 0x00;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
409 UCSRC = 0x06;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
410 UCSRB = _BV(TXEN)|_BV(RXEN);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
411 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
412 #if defined __AVR_ATmega1280__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
413 /* Enable internal pull-up resistor on pin D0 (RX), in order
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
414 to supress line noise that prevents the bootloader from
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
415 timing out (DAM: 20070509) */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
416 /* feature added to the Arduino Mega --DC: 080930 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
417 DDRE &= ~_BV(PINE0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
418 PORTE |= _BV(PINE0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
419 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
420 /* set LED pin as output */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
421 LED_DDR |= _BV(LED);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
422 /* flash onboard LED to signal entering of bootloader */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
423 #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
424 // 4x for UART0, 5x for UART1
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
425 flash_led(NUM_LED_FLASHES + bootuart);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
426 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
427 flash_led(NUM_LED_FLASHES);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
428 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
429 /* 20050803: by DojoCorp, this is one of the parts provoking the
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
430 system to stop listening, cancelled from the original */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
431 //putch('\0');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
432 /* forever loop */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
433 for (;;)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
434 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
435 /* get character from UART */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
436 ch = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
437 /* A bunch of if...else if... gives smaller code than switch...case ! */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
438 /* Hello is anyone home ? */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
439 if(ch=='0')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
440 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
441 nothing_response();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
442 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
443 /* Request programmer ID */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
444 /* Not using PROGMEM string due to boot block in m128 being beyond 64kB boundry */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
445 /* Would need to selectively manipulate RAMPZ, and it's only 9 characters anyway so who cares. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
446 else if(ch=='1')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
447 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
448 if (getch() == ' ')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
449 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
450 putch(0x14);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
451 putch('A');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
452 putch('V');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
453 putch('R');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
454 putch(' ');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
455 putch('I');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
456 putch('S');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
457 putch('P');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
458 putch(0x10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
459 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
460 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
461 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
462 if (++error_count == MAX_ERROR_COUNT)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
463 app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
464 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
465 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
466 /* AVR ISP/STK500 board commands DON'T CARE so default nothing_response */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
467 else if(ch=='@')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
468 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
469 ch2 = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
470 if (ch2>0x85) getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
471 nothing_response();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
472 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
473 /* AVR ISP/STK500 board requests */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
474 else if(ch=='A')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
475 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
476 ch2 = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
477 if(ch2==0x80) byte_response(HW_VER); // Hardware version
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
478 else if(ch2==0x81) byte_response(SW_MAJOR); // Software major version
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
479 else if(ch2==0x82) byte_response(SW_MINOR); // Software minor version
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
480 else if(ch2==0x98) byte_response(0x03); // Unknown but seems to be required by avr studio 3.56
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
481 else byte_response(0x00); // Covers various unnecessary responses we don't care about
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
482 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
483 /* Device Parameters DON'T CARE, DEVICE IS FIXED */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
484 else if(ch=='B')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
485 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
486 getNch(20);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
487 nothing_response();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
488 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
489 /* Parallel programming stuff DON'T CARE */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
490 else if(ch=='E')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
491 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
492 getNch(5);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
493 nothing_response();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
494 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
495 /* P: Enter programming mode */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
496 /* R: Erase device, don't care as we will erase one page at a time anyway. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
497 else if(ch=='P' || ch=='R')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
498 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
499 nothing_response();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
500 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
501 /* Leave programming mode */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
502 else if(ch=='Q')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
503 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
504 nothing_response();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
505 #ifdef WATCHDOG_MODS
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
506 // autoreset via watchdog (sneaky!)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
507 WDTCSR = _BV(WDE);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
508 while (1); // 16 ms
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
509 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
510 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
511 /* Set address, little endian. EEPROM in bytes, FLASH in words */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
512 /* Perhaps extra address bytes may be added in future to support > 128kB FLASH. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
513 /* This might explain why little endian was used here, big endian used everywhere else. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
514 else if(ch=='U')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
515 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
516 address.byte[0] = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
517 address.byte[1] = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
518 nothing_response();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
519 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
520 /* Universal SPI programming command, disabled. Would be used for fuses and lock bits. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
521 else if(ch=='V')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
522 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
523 if (getch() == 0x30)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
524 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
525 getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
526 ch = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
527 getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
528 if (ch == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
529 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
530 byte_response(SIG1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
531 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
532 else if (ch == 1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
533 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
534 byte_response(SIG2);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
535 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
536 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
537 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
538 byte_response(SIG3);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
539 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
540 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
541 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
542 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
543 getNch(3);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
544 byte_response(0x00);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
545 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
546 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
547 /* Write memory, length is big endian and is in bytes */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
548 else if(ch=='d')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
549 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
550 length.byte[1] = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
551 length.byte[0] = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
552 flags.eeprom = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
553 if (getch() == 'E') flags.eeprom = 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
554 for (w=0; w<length.word; w++)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
555 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
556 buff[w] = getch(); // Store data in buffer, can't keep up with serial data stream whilst programming pages
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
557 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
558 if (getch() == ' ')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
559 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
560 if (flags.eeprom) //Write to EEPROM one byte at a time
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
561 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
562 address.word <<= 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
563 for(w=0; w<length.word; w++)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
564 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
565 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
566 while(EECR & (1<<EEPE));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
567 EEAR = (uint16_t)(void *)address.word;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
568 EEDR = buff[w];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
569 EECR |= (1<<EEMPE);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
570 EECR |= (1<<EEPE);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
571 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
572 eeprom_write_byte((void *)address.word,buff[w]);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
573 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
574 address.word++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
575 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
576 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
577 else //Write to FLASH one page at a time
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
578 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
579 if (address.byte[1]>127) address_high = 0x01; //Only possible with m128, m256 will need 3rd address byte. FIXME
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
580 else address_high = 0x00;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
581 #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
582 RAMPZ = address_high;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
583 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
584 address.word = address.word << 1; //address * 2 -> byte location
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
585 /* if ((length.byte[0] & 0x01) == 0x01) length.word++; //Even up an odd number of bytes */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
586 if ((length.byte[0] & 0x01)) length.word++; //Even up an odd number of bytes
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
587 cli(); //Disable interrupts, just to be sure
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
588 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega1284P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
589 while(bit_is_set(EECR,EEPE)); //Wait for previous EEPROM writes to complete
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
590 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
591 while(bit_is_set(EECR,EEWE)); //Wait for previous EEPROM writes to complete
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
592 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
593 asm volatile(
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
594 "clr r17 \n\t" //page_word_count
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
595 "lds r30,address \n\t" //Address of FLASH location (in bytes)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
596 "lds r31,address+1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
597 "ldi r28,lo8(buff) \n\t" //Start of buffer array in RAM
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
598 "ldi r29,hi8(buff) \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
599 "lds r24,length \n\t" //Length of data to be written (in bytes)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
600 "lds r25,length+1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
601 "length_loop: \n\t" //Main loop, repeat for number of words in block
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
602 "cpi r17,0x00 \n\t" //If page_word_count=0 then erase page
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
603 "brne no_page_erase \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
604 "wait_spm1: \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
605 "lds r16,%0 \n\t" //Wait for previous spm to complete
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
606 "andi r16,1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
607 "cpi r16,1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
608 "breq wait_spm1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
609 "ldi r16,0x03 \n\t" //Erase page pointed to by Z
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
610 "sts %0,r16 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
611 "spm \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
612 #ifdef __AVR_ATmega163__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
613 ".word 0xFFFF \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
614 "nop \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
615 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
616 "wait_spm2: \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
617 "lds r16,%0 \n\t" //Wait for previous spm to complete
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
618 "andi r16,1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
619 "cpi r16,1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
620 "breq wait_spm2 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
621 "ldi r16,0x11 \n\t" //Re-enable RWW section
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
622 "sts %0,r16 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
623 "spm \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
624 #ifdef __AVR_ATmega163__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
625 ".word 0xFFFF \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
626 "nop \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
627 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
628 "no_page_erase: \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
629 "ld r0,Y+ \n\t" //Write 2 bytes into page buffer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
630 "ld r1,Y+ \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
631 "wait_spm3: \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
632 "lds r16,%0 \n\t" //Wait for previous spm to complete
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
633 "andi r16,1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
634 "cpi r16,1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
635 "breq wait_spm3 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
636 "ldi r16,0x01 \n\t" //Load r0,r1 into FLASH page buffer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
637 "sts %0,r16 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
638 "spm \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
639 "inc r17 \n\t" //page_word_count++
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
640 "cpi r17,%1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
641 "brlo same_page \n\t" //Still same page in FLASH
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
642 "write_page: \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
643 "clr r17 \n\t" //New page, write current one first
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
644 "wait_spm4: \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
645 "lds r16,%0 \n\t" //Wait for previous spm to complete
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
646 "andi r16,1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
647 "cpi r16,1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
648 "breq wait_spm4 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
649 #ifdef __AVR_ATmega163__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
650 "andi r30,0x80 \n\t" // m163 requires Z6:Z1 to be zero during page write
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
651 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
652 "ldi r16,0x05 \n\t" //Write page pointed to by Z
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
653 "sts %0,r16 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
654 "spm \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
655 #ifdef __AVR_ATmega163__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
656 ".word 0xFFFF \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
657 "nop \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
658 "ori r30,0x7E \n\t" // recover Z6:Z1 state after page write (had to be zero during write)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
659 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
660 "wait_spm5: \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
661 "lds r16,%0 \n\t" //Wait for previous spm to complete
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
662 "andi r16,1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
663 "cpi r16,1 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
664 "breq wait_spm5 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
665 "ldi r16,0x11 \n\t" //Re-enable RWW section
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
666 "sts %0,r16 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
667 "spm \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
668 #ifdef __AVR_ATmega163__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
669 ".word 0xFFFF \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
670 "nop \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
671 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
672 "same_page: \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
673 "adiw r30,2 \n\t" //Next word in FLASH
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
674 "sbiw r24,2 \n\t" //length-2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
675 "breq final_write \n\t" //Finished
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
676 "rjmp length_loop \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
677 "final_write: \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
678 "cpi r17,0 \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
679 "breq block_done \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
680 "adiw r24,2 \n\t" //length+2, fool above check on length after short page write
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
681 "rjmp write_page \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
682 "block_done: \n\t"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
683 "clr __zero_reg__ \n\t" //restore zero register
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
684 #if defined __AVR_ATmega168__ || __AVR_ATmega328P__ || __AVR_ATmega128__ || __AVR_ATmega1280__ || __AVR_ATmega1281__ || __AVR_ATmega1284P__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
685 : "=m" (SPMCSR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
686 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
687 : "=m" (SPMCR) : "M" (PAGE_SIZE) : "r0","r16","r17","r24","r25","r28","r29","r30","r31"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
688 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
689 );
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
690 /* Should really add a wait for RWW section to be enabled, don't actually need it since we never */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
691 /* exit the bootloader without a power cycle anyhow */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
692 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
693 putch(0x14);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
694 putch(0x10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
695 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
696 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
697 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
698 if (++error_count == MAX_ERROR_COUNT)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
699 app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
700 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
701 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
702 /* Read memory block mode, length is big endian. */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
703 else if(ch=='t')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
704 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
705 length.byte[1] = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
706 length.byte[0] = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
707 #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
708 if (address.word>0x7FFF) flags.rampz = 1; // No go with m256, FIXME
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
709 else flags.rampz = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
710 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
711 address.word = address.word << 1; // address * 2 -> byte location
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
712 if (getch() == 'E') flags.eeprom = 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
713 else flags.eeprom = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
714 if (getch() == ' ') // Command terminator
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
715 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
716 putch(0x14);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
717 for (w=0; w < length.word; w++) // Can handle odd and even lengths okay
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
718 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
719 if (flags.eeprom) // Byte access EEPROM read
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
720 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
721 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
722 while(EECR & (1<<EEPE));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
723 EEAR = (uint16_t)(void *)address.word;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
724 EECR |= (1<<EERE);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
725 putch(EEDR);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
726 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
727 putch(eeprom_read_byte((void *)address.word));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
728 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
729 address.word++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
730 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
731 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
732 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
733 if (!flags.rampz) putch(pgm_read_byte_near(address.word));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
734 #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
735 else putch(pgm_read_byte_far(address.word + 0x10000));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
736 // Hmmmm, yuck FIXME when m256 arrvies
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
737 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
738 address.word++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
739 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
740 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
741 putch(0x10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
742 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
743 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
744 /* Get device signature bytes */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
745 else if(ch=='u')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
746 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
747 if (getch() == ' ')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
748 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
749 putch(0x14);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
750 putch(SIG1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
751 putch(SIG2);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
752 putch(SIG3);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
753 putch(0x10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
754 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
755 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
756 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
757 if (++error_count == MAX_ERROR_COUNT)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
758 app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
759 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
760 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
761 /* Read oscillator calibration byte */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
762 else if(ch=='v')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
763 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
764 byte_response(0x00);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
765 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
766 #if defined MONITOR
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
767 /* here come the extended monitor commands by Erik Lins */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
768 /* check for three times exclamation mark pressed */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
769 else if(ch=='!')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
770 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
771 ch = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
772 if(ch=='!')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
773 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
774 ch = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
775 if(ch=='!')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
776 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
777 PGM_P welcome = "";
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
778 #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
779 uint16_t extaddr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
780 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
781 uint8_t addrl, addrh;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
782 #ifdef CRUMB128
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
783 welcome = "ATmegaBOOT / Crumb128 - (C) J.P.Kyle, E.Lins - 050815\n\r";
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
784 #elif defined PROBOMEGA128
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
785 welcome = "ATmegaBOOT / PROBOmega128 - (C) J.P.Kyle, E.Lins - 050815\n\r";
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
786 #elif defined SAVVY128
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
787 welcome = "ATmegaBOOT / Savvy128 - (C) J.P.Kyle, E.Lins - 050815\n\r";
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
788 #elif defined __AVR_ATmega1280__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
789 welcome = "ATmegaBOOT / Arduino Mega - (C) Arduino LLC - 090930\n\r";
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
790 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
791 /* turn on LED */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
792 LED_DDR |= _BV(LED);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
793 LED_PORT &= ~_BV(LED);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
794 /* print a welcome message and command overview */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
795 for(i=0; welcome[i] != '\0'; ++i)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
796 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
797 putch(welcome[i]);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
798 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
799 /* test for valid commands */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
800 for(;;)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
801 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
802 putch('\n');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
803 putch('\r');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
804 putch(':');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
805 putch(' ');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
806 ch = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
807 putch(ch);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
808 /* toggle LED */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
809 if(ch == 't')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
810 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
811 if(bit_is_set(LED_PIN,LED))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
812 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
813 LED_PORT &= ~_BV(LED);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
814 putch('1');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
815 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
816 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
817 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
818 LED_PORT |= _BV(LED);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
819 putch('0');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
820 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
821 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
822 /* read byte from address */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
823 else if(ch == 'r')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
824 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
825 ch = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
826 putch(ch);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
827 addrh = gethex();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
828 addrl = gethex();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
829 putch('=');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
830 ch = *(uint8_t *)((addrh << 8) + addrl);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
831 puthex(ch);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
832 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
833 /* write a byte to address */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
834 else if(ch == 'w')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
835 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
836 ch = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
837 putch(ch);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
838 addrh = gethex();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
839 addrl = gethex();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
840 ch = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
841 putch(ch);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
842 ch = gethex();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
843 *(uint8_t *)((addrh << 8) + addrl) = ch;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
844 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
845 /* read from uart and echo back */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
846 else if(ch == 'u')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
847 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
848 for(;;)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
849 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
850 putch(getch());
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
851 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
852 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
853 #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
854 /* external bus loop */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
855 else if(ch == 'b')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
856 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
857 putch('b');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
858 putch('u');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
859 putch('s');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
860 MCUCR = 0x80;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
861 XMCRA = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
862 XMCRB = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
863 extaddr = 0x1100;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
864 for(;;)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
865 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
866 ch = *(volatile uint8_t *)extaddr;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
867 if(++extaddr == 0)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
868 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
869 extaddr = 0x1100;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
870 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
871 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
872 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
873 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
874 else if(ch == 'j')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
875 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
876 app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
877 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
878 } /* end of monitor functions */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
879 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
880 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
881 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
882 /* end of monitor */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
883 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
884 else if (++error_count == MAX_ERROR_COUNT)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
885 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
886 app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
887 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
888 } /* end of forever loop */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
889 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
890
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
891
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
892 char gethexnib(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
893 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
894 char a;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
895 a = getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
896 putch(a);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
897 if(a >= 'a')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
898 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
899 return (a - 'a' + 0x0a);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
900 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
901 else if(a >= '0')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
902 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
903 return(a - '0');
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
904 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
905 return a;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
906 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
907
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
908
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
909 char gethex(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
910 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
911 return (gethexnib() << 4) + gethexnib();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
912 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
913
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
914
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
915 void puthex(char ch)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
916 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
917 char ah;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
918 ah = ch >> 4;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
919 if(ah >= 0x0a)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
920 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
921 ah = ah - 0x0a + 'a';
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
922 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
923 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
924 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
925 ah += '0';
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
926 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
927 ch &= 0x0f;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
928 if(ch >= 0x0a)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
929 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
930 ch = ch - 0x0a + 'a';
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
931 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
932 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
933 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
934 ch += '0';
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
935 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
936 putch(ah);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
937 putch(ch);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
938 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
939
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
940
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
941 void putch(char ch)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
942 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
943 #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
944 if(bootuart == 1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
945 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
946 while (!(UCSR0A & _BV(UDRE0)));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
947 UDR0 = ch;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
948 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
949 else if (bootuart == 2)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
950 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
951 while (!(UCSR1A & _BV(UDRE1)));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
952 UDR1 = ch;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
953 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
954 #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
955 while (!(UCSR0A & _BV(UDRE0)));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
956 UDR0 = ch;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
957 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
958 /* m8,16,32,169,8515,8535,163 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
959 while (!(UCSRA & _BV(UDRE)));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
960 UDR = ch;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
961 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
962 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
963
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
964
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
965 char getch(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
966 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
967 #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
968 uint32_t count = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
969 if(bootuart == 1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
970 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
971 while(!(UCSR0A & _BV(RXC0)))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
972 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
973 /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
974 /* HACKME:: here is a good place to count times*/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
975 count++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
976 if (count > MAX_TIME_COUNT)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
977 app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
978 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
979 return UDR0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
980 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
981 else if(bootuart == 2)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
982 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
983 while(!(UCSR1A & _BV(RXC1)))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
984 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
985 /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
986 /* HACKME:: here is a good place to count times*/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
987 count++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
988 if (count > MAX_TIME_COUNT)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
989 app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
990 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
991 return UDR1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
992 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
993 return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
994 #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
995 uint32_t count = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
996 while(!(UCSR0A & _BV(RXC0)))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
997 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
998 /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
999 /* HACKME:: here is a good place to count times*/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1000 count++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1001 if (count > MAX_TIME_COUNT)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1002 app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1003 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1004 return UDR0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1005 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1006 /* m8,16,32,169,8515,8535,163 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1007 uint32_t count = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1008 while(!(UCSRA & _BV(RXC)))
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1009 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1010 /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1011 /* HACKME:: here is a good place to count times*/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1012 count++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1013 if (count > MAX_TIME_COUNT)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1014 app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1015 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1016 return UDR;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1017 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1018 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1019
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1020
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1021 void getNch(uint8_t count)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1022 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1023 while(count--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1024 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1025 #if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1284P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1026 if(bootuart == 1)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1027 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1028 while(!(UCSR0A & _BV(RXC0)));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1029 UDR0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1030 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1031 else if(bootuart == 2)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1032 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1033 while(!(UCSR1A & _BV(RXC1)));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1034 UDR1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1035 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1036 #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1037 getch();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1038 #else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1039 /* m8,16,32,169,8515,8535,163 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1040 /* 20060803 DojoCorp:: Addon coming from the previous Bootloader*/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1041 //while(!(UCSRA & _BV(RXC)));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1042 //UDR;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1043 getch(); // need to handle time out
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1044 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1045 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1046 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1047
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1048
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1049 void byte_response(uint8_t val)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1050 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1051 if (getch() == ' ')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1052 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1053 putch(0x14);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1054 putch(val);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1055 putch(0x10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1056 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1057 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1058 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1059 if (++error_count == MAX_ERROR_COUNT)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1060 app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1061 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1062 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1063
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1064
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1065 void nothing_response(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1066 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1067 if (getch() == ' ')
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1068 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1069 putch(0x14);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1070 putch(0x10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1071 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1072 else
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1073 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1074 if (++error_count == MAX_ERROR_COUNT)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1075 app_start();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1076 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1077 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1078
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1079 void flash_led(uint8_t count)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1080 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1081 while (count--)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1082 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1083 LED_PORT |= _BV(LED);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1084 _delay_ms(100);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1085 LED_PORT &= ~_BV(LED);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1086 _delay_ms(100);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1087 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1088 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1089
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1090
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1091 /* end of file ATmegaBOOT.c */

mercurial