| 1 | /**********************************************************/ |
| 2 | /* Optiboot bootloader for Arduino */ |
| 3 | /* */ |
| 4 | /* http://optiboot.googlecode.com */ |
| 5 | /* */ |
| 6 | /* Arduino-maintained version : See README.TXT */ |
| 7 | /* http://code.google.com/p/arduino/ */ |
| 8 | /* It is the intent that changes not relevant to the */ |
| 9 | /* Arduino production envionment get moved from the */ |
| 10 | /* optiboot project to the arduino project in "lumps." */ |
| 11 | /* */ |
| 12 | /* Heavily optimised bootloader that is faster and */ |
| 13 | /* smaller than the Arduino standard bootloader */ |
| 14 | /* */ |
| 15 | /* */ |
| 16 | /* Enhancements for supporting KuttyPy */ |
| 17 | /* Author : Jithin B.P. <[email protected]> */ |
| 18 | /* Supports the kuttypy protocol for Atmega32 but */ |
| 19 | /* retains the original arduino bootloader */ |
| 20 | /* Also marks pins PC0 and PC1 as SCL pins */ |
| 21 | /* Watchdog timer of 1 Second set inside Kpy loop. */ |
| 22 | /* */ |
| 23 | /* */ |
| 24 | /* */ |
| 25 | /* Enhancements: */ |
| 26 | /* Fits in 512 bytes, saving 1.5K of code space */ |
| 27 | /* Background page erasing speeds up programming */ |
| 28 | /* Higher baud rate speeds up programming */ |
| 29 | /* Written almost entirely in C */ |
| 30 | /* Customisable timeout with accurate timeconstant */ |
| 31 | /* Optional virtual UART. No hardware UART required. */ |
| 32 | /* Optional virtual boot partition for devices without. */ |
| 33 | /* */ |
| 34 | /* What you lose: */ |
| 35 | /* Implements a skeleton STK500 protocol which is */ |
| 36 | /* missing several features including EEPROM */ |
| 37 | /* programming and non-page-aligned writes */ |
| 38 | /* High baud rate breaks compatibility with standard */ |
| 39 | /* Arduino flash settings */ |
| 40 | /* */ |
| 41 | /* Fully supported: */ |
| 42 | /* ATmega168 based devices (Diecimila etc) */ |
| 43 | /* ATmega328P based devices (Duemilanove etc) */ |
| 44 | /* */ |
| 45 | /* Beta test (believed working.) */ |
| 46 | /* ATmega8 based devices (Arduino legacy) */ |
| 47 | /* ATmega328 non-picopower devices */ |
| 48 | /* ATmega644P based devices (Sanguino) */ |
| 49 | /* ATmega1284P based devices */ |
| 50 | /* ATmega1280 based devices (Arduino Mega) */ |
| 51 | /* */ |
| 52 | /* Alpha test */ |
| 53 | /* ATmega32 */ |
| 54 | /* */ |
| 55 | /* Work in progress: */ |
| 56 | /* ATtiny84 based devices (Luminet) */ |
| 57 | /* */ |
| 58 | /* Does not support: */ |
| 59 | /* USB based devices (eg. Teensy, Leonardo) */ |
| 60 | /* */ |
| 61 | /* Assumptions: */ |
| 62 | /* The code makes several assumptions that reduce the */ |
| 63 | /* code size. They are all true after a hardware reset, */ |
| 64 | /* but may not be true if the bootloader is called by */ |
| 65 | /* other means or on other hardware. */ |
| 66 | /* No interrupts can occur */ |
| 67 | /* UART and Timer 1 are set to their reset state */ |
| 68 | /* SP points to RAMEND */ |
| 69 | /* */ |
| 70 | /* Code builds on code, libraries and optimisations from: */ |
| 71 | /* stk500boot.c by Jason P. Kyle */ |
| 72 | /* Arduino bootloader http://arduino.cc */ |
| 73 | /* Spiff's 1K bootloader http://spiffie.org/know/arduino_1k_bootloader/bootloader.shtml */ |
| 74 | /* avr-libc project http://nongnu.org/avr-libc */ |
| 75 | /* Adaboot http://www.ladyada.net/library/arduino/bootloader.html */ |
| 76 | /* AVR305 Atmel Application Note */ |
| 77 | /* */ |
| 78 | /* This program is free software; you can redistribute it */ |
| 79 | /* and/or modify it under the terms of the GNU General */ |
| 80 | /* Public License as published by the Free Software */ |
| 81 | /* Foundation; either version 2 of the License, or */ |
| 82 | /* (at your option) any later version. */ |
| 83 | /* */ |
| 84 | /* This program is distributed in the hope that it will */ |
| 85 | /* be useful, but WITHOUT ANY WARRANTY; without even the */ |
| 86 | /* implied warranty of MERCHANTABILITY or FITNESS FOR A */ |
| 87 | /* PARTICULAR PURPOSE. See the GNU General Public */ |
| 88 | /* License for more details. */ |
| 89 | /* */ |
| 90 | /* You should have received a copy of the GNU General */ |
| 91 | /* Public License along with this program; if not, write */ |
| 92 | /* to the Free Software Foundation, Inc., */ |
| 93 | /* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ |
| 94 | /* */ |
| 95 | /* Licence can be viewed at */ |
| 96 | /* http://www.fsf.org/licenses/gpl.txt */ |
| 97 | /* */ |
| 98 | /**********************************************************/ |
| 99 | |
| 100 | |
| 101 | /**********************************************************/ |
| 102 | /* */ |
| 103 | /* Optional defines: */ |
| 104 | /* */ |
| 105 | /**********************************************************/ |
| 106 | /* */ |
| 107 | /* BIG_BOOT: */ |
| 108 | /* Build a 1k bootloader, not 512 bytes. This turns on */ |
| 109 | /* extra functionality. */ |
| 110 | /* */ |
| 111 | /* BAUD_RATE: */ |
| 112 | /* Set bootloader baud rate. */ |
| 113 | /* */ |
| 114 | /* LUDICROUS_SPEED: */ |
| 115 | /* 230400 baud :-) */ |
| 116 | /* */ |
| 117 | /* SUPPORT_EEPROM: */ |
| 118 | /* Support reading and writing from EEPROM. This is not */ |
| 119 | /* used by Arduino, so off by default. */ |
| 120 | /* */ |
| 121 | /* TIMEOUT_MS: */ |
| 122 | /* Bootloader timeout period, in milliseconds. */ |
| 123 | /* 500,1000,2000,4000,8000 supported. */ |
| 124 | /* */ |
| 125 | /* UART: */ |
| 126 | /* UART number (0..n) for devices with more than */ |
| 127 | /* one hardware uart (644P, 1284P, etc) */ |
| 128 | /* */ |
| 129 | /**********************************************************/ |
| 130 | |
| 131 | /**********************************************************/ |
| 132 | /* Version Numbers! */ |
| 133 | /* */ |
| 134 | /* Arduino Optiboot now includes this Version number in */ |
| 135 | /* the source and object code. */ |
| 136 | /* */ |
| 137 | /* Version 3 was released as zip from the optiboot */ |
| 138 | /* repository and was distributed with Arduino 0022. */ |
| 139 | /* Version 4 starts with the arduino repository commit */ |
| 140 | /* that brought the arduino repository up-to-date with */ |
| 141 | /* the optiboot source tree changes since v3. */ |
| 142 | /* Version 5 was created at the time of the new Makefile */ |
| 143 | /* structure (Mar, 2013), even though no binaries changed*/ |
| 144 | /* It would be good if versions implemented outside the */ |
| 145 | /* official repository used an out-of-seqeunce version */ |
| 146 | /* number (like 104.6 if based on based on 4.5) to */ |
| 147 | /* prevent collisions. */ |
| 148 | /* */ |
| 149 | /**********************************************************/ |
| 150 | |
| 151 | /**********************************************************/ |
| 152 | /* Edit History: */ |
| 153 | /* */ |
| 154 | /* Mar 2013 */ |
| 155 | /* 5.0 WestfW: Major Makefile restructuring. */ |
| 156 | /* See Makefile and pin_defs.h */ |
| 157 | /* (no binary changes) */ |
| 158 | /* */ |
| 159 | /* 4.6 WestfW/Pito: Add ATmega32 support */ |
| 160 | /* Jan 2013 */ |
| 161 | /* 4.6 WestfW/dkinzer: use autoincrement lpm for read */ |
| 162 | /* 4.6 WestfW/dkinzer: pass reset cause to app in R2 */ |
| 163 | /* Mar 2012 */ |
| 164 | /* 4.5 WestfW: add infrastructure for non-zero UARTS. */ |
| 165 | /* 4.5 WestfW: fix SIGNATURE_2 for m644 (bad in avr-libc) */ |
| 166 | /* Jan 2012: */ |
| 167 | /* 4.5 WestfW: fix NRWW value for m1284. */ |
| 168 | /* 4.4 WestfW: use attribute OS_main instead of naked for */ |
| 169 | /* main(). This allows optimizations that we */ |
| 170 | /* count on, which are prohibited in naked */ |
| 171 | /* functions due to PR42240. (keeps us less */ |
| 172 | /* than 512 bytes when compiler is gcc4.5 */ |
| 173 | /* (code from 4.3.2 remains the same.) */ |
| 174 | /* 4.4 WestfW and Maniacbug: Add m1284 support. This */ |
| 175 | /* does not change the 328 binary, so the */ |
| 176 | /* version number didn't change either. (?) */ |
| 177 | /* June 2011: */ |
| 178 | /* 4.4 WestfW: remove automatic soft_uart detect (didn't */ |
| 179 | /* know what it was doing or why.) Added a */ |
| 180 | /* check of the calculated BRG value instead. */ |
| 181 | /* Version stays 4.4; existing binaries are */ |
| 182 | /* not changed. */ |
| 183 | /* 4.4 WestfW: add initialization of address to keep */ |
| 184 | /* the compiler happy. Change SC'ed targets. */ |
| 185 | /* Return the SW version via READ PARAM */ |
| 186 | /* 4.3 WestfW: catch framing errors in getch(), so that */ |
| 187 | /* AVRISP works without HW kludges. */ |
| 188 | /* http://code.google.com/p/arduino/issues/detail?id=368n*/ |
| 189 | /* 4.2 WestfW: reduce code size, fix timeouts, change */ |
| 190 | /* verifySpace to use WDT instead of appstart */ |
| 191 | /* 4.1 WestfW: put version number in binary. */ |
| 192 | /**********************************************************/ |
| 193 | |
| 194 | #define OPTIBOOT_MAJVER 5 |
| 195 | #define OPTIBOOT_MINVER 0 |
| 196 | |
| 197 | #define MAKESTR(a) #a |
| 198 | #define MAKEVER(a, b) MAKESTR(a*256+b) |
| 199 | |
| 200 | asm(" .section .version\n" |
| 201 | "optiboot_version: .word " MAKEVER(OPTIBOOT_MAJVER, OPTIBOOT_MINVER) "\n" |
| 202 | " .section .text\n"); |
| 203 | |
| 204 | #include <inttypes.h> |
| 205 | #include <avr/io.h> |
| 206 | #include <avr/pgmspace.h> |
| 207 | |
| 208 | // <avr/boot.h> uses sts instructions, but this version uses out instructions |
| 209 | // This saves cycles and program memory. |
| 210 | #include "boot.h" |
| 211 | |
| 212 | |
| 213 | // We don't use <avr/wdt.h> as those routines have interrupt overhead we don't need. |
| 214 | |
| 215 | #include "pin_defs.h" |
| 216 | #include "stk500.h" |
| 217 | |
| 218 | |
| 219 | #ifdef LUDICROUS_SPEED |
| 220 | #define BAUD_RATE 230400L |
| 221 | #endif |
| 222 | |
| 223 | /* set the UART baud rate defaults */ |
| 224 | #ifndef BAUD_RATE |
| 225 | #if F_CPU >= 8000000L |
| 226 | #define BAUD_RATE 115200L // Highest rate Avrdude win32 will support |
| 227 | #elsif F_CPU >= 1000000L |
| 228 | #define BAUD_RATE 9600L // 19200 also supported, but with significant error |
| 229 | #elsif F_CPU >= 128000L |
| 230 | #define BAUD_RATE 4800L // Good for 128kHz internal RC |
| 231 | #else |
| 232 | #define BAUD_RATE 1200L // Good even at 32768Hz |
| 233 | #endif |
| 234 | #endif |
| 235 | |
| 236 | #ifndef UART |
| 237 | #define UART 0 |
| 238 | #endif |
| 239 | |
| 240 | #define BAUD_SETTING (( (F_CPU + BAUD_RATE * 4L) / ((BAUD_RATE * 8L))) - 1 ) |
| 241 | #define BAUD_ACTUAL (F_CPU/(8 * ((BAUD_SETTING)+1))) |
| 242 | #define BAUD_ERROR (( 100*(BAUD_RATE - BAUD_ACTUAL) ) / BAUD_RATE) |
| 243 | |
| 244 | #if BAUD_ERROR >= 5 |
| 245 | #error BAUD_RATE error greater than 5% |
| 246 | #elif BAUD_ERROR <= -5 |
| 247 | #error BAUD_RATE error greater than -5% |
| 248 | #elif BAUD_ERROR >= 2 |
| 249 | #warning BAUD_RATE error greater than 2% |
| 250 | #elif BAUD_ERROR <= -2 |
| 251 | #warning BAUD_RATE error greater than -2% |
| 252 | #endif |
| 253 | |
| 254 | #if 0 |
| 255 | /* Switch in soft UART for hard baud rates */ |
| 256 | /* |
| 257 | * I don't understand what this was supposed to accomplish, where the |
| 258 | * constant "280" came from, or why automatically (and perhaps unexpectedly) |
| 259 | * switching to a soft uart is a good thing, so I'm undoing this in favor |
| 260 | * of a range check using the same calc used to config the BRG... |
| 261 | */ |
| 262 | #if (F_CPU/BAUD_RATE) > 280 // > 57600 for 16MHz |
| 263 | #ifndef SOFT_UART |
| 264 | #define SOFT_UART |
| 265 | #endif |
| 266 | #endif |
| 267 | #else // 0 |
| 268 | #if (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 > 250 |
| 269 | #error Unachievable baud rate (too slow) BAUD_RATE |
| 270 | #endif // baud rate slow check |
| 271 | #if (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 < 3 |
| 272 | #error Unachievable baud rate (too fast) BAUD_RATE |
| 273 | #endif // baud rate fastn check |
| 274 | #endif |
| 275 | |
| 276 | /* Watchdog settings */ |
| 277 | #define WATCHDOG_OFF (0) |
| 278 | #define WATCHDOG_16MS (_BV(WDE)) |
| 279 | #define WATCHDOG_32MS (_BV(WDP0) | _BV(WDE)) |
| 280 | #define WATCHDOG_64MS (_BV(WDP1) | _BV(WDE)) |
| 281 | #define WATCHDOG_125MS (_BV(WDP1) | _BV(WDP0) | _BV(WDE)) |
| 282 | #define WATCHDOG_250MS (_BV(WDP2) | _BV(WDE)) |
| 283 | #define WATCHDOG_500MS (_BV(WDP2) | _BV(WDP0) | _BV(WDE)) |
| 284 | #define WATCHDOG_1S (_BV(WDP2) | _BV(WDP1) | _BV(WDE)) |
| 285 | #define WATCHDOG_2S (_BV(WDP2) | _BV(WDP1) | _BV(WDP0) | _BV(WDE)) |
| 286 | #ifndef __AVR_ATmega8__ |
| 287 | #define WATCHDOG_4S (_BV(WDP3) | _BV(WDE)) |
| 288 | #define WATCHDOG_8S (_BV(WDP3) | _BV(WDP0) | _BV(WDE)) |
| 289 | #endif |
| 290 | |
| 291 | /* Function Prototypes */ |
| 292 | /* The main function is in init9, which removes the interrupt vector table */ |
| 293 | /* we don't need. It is also 'naked', which means the compiler does not */ |
| 294 | /* generate any entry or exit code itself. */ |
| 295 | int main(void) __attribute__ ((OS_main)) __attribute__ ((section (".init9"))); |
| 296 | void putch(char); |
| 297 | uint8_t getch(void); |
| 298 | static inline void getNch(uint8_t); /* "static inline" is a compiler hint to reduce code size */ |
| 299 | void verifySpace(); |
| 300 | uint8_t getLen(); |
| 301 | static inline void watchdogReset(); |
| 302 | void watchdogConfig(uint8_t x); |
| 303 | #ifdef SOFT_UART |
| 304 | void uartDelay() __attribute__ ((naked)); |
| 305 | #endif |
| 306 | void appStart(uint8_t rstFlags) __attribute__ ((naked)); |
| 307 | |
| 308 | void (*app_start)(void) = 0x0000; |
| 309 | |
| 310 | /* |
| 311 | * NRWW memory |
| 312 | * Addresses below NRWW (Non-Read-While-Write) can be programmed while |
| 313 | * continuing to run code from flash, slightly speeding up programming |
| 314 | * time. Beware that Atmel data sheets specify this as a WORD address, |
| 315 | * while optiboot will be comparing against a 16-bit byte address. This |
| 316 | * means that on a part with 128kB of memory, the upper part of the lower |
| 317 | * 64k will get NRWW processing as well, even though it doesn't need it. |
| 318 | * That's OK. In fact, you can disable the overlapping processing for |
| 319 | * a part entirely by setting NRWWSTART to zero. This reduces code |
| 320 | * space a bit, at the expense of being slightly slower, overall. |
| 321 | * |
| 322 | * RAMSTART should be self-explanatory. It's bigger on parts with a |
| 323 | * lot of peripheral registers. |
| 324 | */ |
| 325 | #if defined(__AVR_ATmega168__) |
| 326 | #define RAMSTART (0x100) |
| 327 | #define NRWWSTART (0x3800) |
| 328 | #elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega32__) |
| 329 | #define RAMSTART (0x100) |
| 330 | #define NRWWSTART (0x7000) |
| 331 | #elif defined (__AVR_ATmega644P__) |
| 332 | #define RAMSTART (0x100) |
| 333 | #define NRWWSTART (0xE000) |
| 334 | // correct for a bug in avr-libc |
| 335 | #undef SIGNATURE_2 |
| 336 | #define SIGNATURE_2 0x0A |
| 337 | #elif defined (__AVR_ATmega1284P__) |
| 338 | #define RAMSTART (0x100) |
| 339 | #define NRWWSTART (0xE000) |
| 340 | #elif defined(__AVR_ATtiny84__) |
| 341 | #define RAMSTART (0x100) |
| 342 | #define NRWWSTART (0x0000) |
| 343 | #elif defined(__AVR_ATmega1280__) |
| 344 | #define RAMSTART (0x200) |
| 345 | #define NRWWSTART (0xE000) |
| 346 | #elif defined(__AVR_ATmega8__) || defined(__AVR_ATmega88__) |
| 347 | #define RAMSTART (0x100) |
| 348 | #define NRWWSTART (0x1800) |
| 349 | #endif |
| 350 | |
| 351 | /* C zero initialises all global variables. However, that requires */ |
| 352 | /* These definitions are NOT zero initialised, but that doesn't matter */ |
| 353 | /* This allows us to drop the zero init code, saving us memory */ |
| 354 | #define buff ((uint8_t*)(RAMSTART)) |
| 355 | #ifdef VIRTUAL_BOOT_PARTITION |
| 356 | #define rstVect (*(uint16_t*)(RAMSTART+SPM_PAGESIZE*2+4)) |
| 357 | #define wdtVect (*(uint16_t*)(RAMSTART+SPM_PAGESIZE*2+6)) |
| 358 | #endif |
| 359 | |
| 360 | /* |
| 361 | * Handle devices with up to 4 uarts (eg m1280.) Rather inelegantly. |
| 362 | * Note that mega8/m32 still needs special handling, because ubrr is handled |
| 363 | * differently. |
| 364 | */ |
| 365 | #if UART == 0 |
| 366 | # define UART_SRA UCSR0A |
| 367 | # define UART_SRB UCSR0B |
| 368 | # define UART_SRC UCSR0C |
| 369 | # define UART_SRL UBRR0L |
| 370 | # define UART_UDR UDR0 |
| 371 | #elif UART == 1 |
| 372 | #if !defined(UDR1) |
| 373 | #error UART == 1, but no UART1 on device |
| 374 | #endif |
| 375 | # define UART_SRA UCSR1A |
| 376 | # define UART_SRB UCSR1B |
| 377 | # define UART_SRC UCSR1C |
| 378 | # define UART_SRL UBRR1L |
| 379 | # define UART_UDR UDR1 |
| 380 | #elif UART == 2 |
| 381 | #if !defined(UDR2) |
| 382 | #error UART == 2, but no UART2 on device |
| 383 | #endif |
| 384 | # define UART_SRA UCSR2A |
| 385 | # define UART_SRB UCSR2B |
| 386 | # define UART_SRC UCSR2C |
| 387 | # define UART_SRL UBRR2L |
| 388 | # define UART_UDR UDR2 |
| 389 | #elif UART == 3 |
| 390 | #if !defined(UDR1) |
| 391 | #error UART == 3, but no UART3 on device |
| 392 | #endif |
| 393 | # define UART_SRA UCSR3A |
| 394 | # define UART_SRB UCSR3B |
| 395 | # define UART_SRC UCSR3C |
| 396 | # define UART_SRL UBRR3L |
| 397 | # define UART_UDR UDR3 |
| 398 | #endif |
| 399 | |
| 400 | /* main program starts here */ |
| 401 | int main(void) { |
| 402 | uint8_t ch; |
| 403 | |
| 404 | /* |
| 405 | * Making these local and in registers prevents the need for initializing |
| 406 | * them, and also saves space because code no longer stores to memory. |
| 407 | * (initializing address keeps the compiler happy, but isn't really |
| 408 | * necessary, and uses 4 bytes of flash.) |
| 409 | */ |
| 410 | register uint16_t address = 0; |
| 411 | register uint8_t length; |
| 412 | |
| 413 | // After the zero init loop, this is the first code to run. |
| 414 | // |
| 415 | // This code makes the following assumptions: |
| 416 | // No interrupts will execute |
| 417 | // SP points to RAMEND |
| 418 | // r1 contains zero |
| 419 | // |
| 420 | // If not, uncomment the following instructions: |
| 421 | // cli(); |
| 422 | asm volatile ("clr __zero_reg__"); |
| 423 | #if defined(__AVR_ATmega8__) || defined (__AVR_ATmega32__) |
| 424 | SP=RAMEND; // This is done by hardware reset |
| 425 | #endif |
| 426 | |
| 427 | // Adaboot no-wait mod |
| 428 | ch = MCUSR; |
| 429 | MCUSR = 0; |
| 430 | if (!(ch & _BV(EXTRF))) appStart(ch); |
| 431 | |
| 432 | #if defined(__AVR_ATmega8__) || defined (__AVR_ATmega32__) |
| 433 | UCSRA = _BV(U2X); //Double speed mode USART |
| 434 | UCSRB = _BV(RXEN) | _BV(TXEN); // enable Rx & Tx |
| 435 | UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0); // config USART; 8N1 |
| 436 | UBRRL = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 ); |
| 437 | #else |
| 438 | UART_SRA = _BV(U2X0); //Double speed mode USART0 |
| 439 | UART_SRB = _BV(RXEN0) | _BV(TXEN0); |
| 440 | UART_SRC = _BV(UCSZ00) | _BV(UCSZ01); |
| 441 | UART_SRL = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 ); |
| 442 | #endif |
| 443 | |
| 444 | // Set up watchdog to trigger after 500ms |
| 445 | watchdogConfig(WATCHDOG_1S); |
| 446 | |
| 447 | /* Forever loop */ |
| 448 | for (;;) { |
| 449 | /* get character from UART */ |
| 450 | ch = getch(); |
| 451 | /* check for kuttypy requests [version/read/write/i2c] */ |
| 452 | if(ch>=1 && ch<=6 ) { |
| 453 | uint8_t bts=0,data=0, *port; |
| 454 | uint16_t timeout; |
| 455 | TWSR=0x00; TWBR=0x46 ; TWCR=0x04; //Init I2C |
| 456 | PORTC = 3; //Enable SCL/SDA Pull up |
| 457 | |
| 458 | /* turn on Watchdog */ |
| 459 | WDTCR = (1<<WDE)|(1<<WDP2)|(1<<WDP1); // 1 second watchdog for kuttypy |
| 460 | |
| 461 | /* test for valid commands */ |
| 462 | do { |
| 463 | if(ch == 1) |
| 464 | { |
| 465 | UDR = KUTTYPY_TYPE; |
| 466 | } |
| 467 | else if(ch == KUTTYPY_READ) |
| 468 | { |
| 469 | while ( !(UCSRA & (1<<RXC)) ) ; // wait |
| 470 | port = UDR; // get the port address |
| 471 | UDR = *port; |
| 472 | }else if(ch == KUTTYPY_WRITE) |
| 473 | { |
| 474 | while ( !(UCSRA & (1<<RXC)) ) ; // wait |
| 475 | port = UDR; // get the register address |
| 476 | while ( !(UCSRA & (1<<RXC)) ) ; // wait |
| 477 | data = UDR; // get the data |
| 478 | *port = data; // write data to the port address |
| 479 | }else if(ch == KUTTYPY_I2C_WRITE) |
| 480 | { |
| 481 | timeout = 10000; |
| 482 | while ( !(UCSRA & (1<<RXC)) ) ; // wait |
| 483 | data = UDR; // get the I2C address |
| 484 | TWCR = 0xA4; // send a start bit on i2c bus |
| 485 | while(!(TWCR & 0x80) && timeout)timeout--; // wait for confirmation of transmit |
| 486 | TWDR = data<<1; // load address of i2c device |
| 487 | TWCR = 0x84; // transmit |
| 488 | while(!(TWCR & 0x80) && timeout)timeout--; // wait for confirmation of transmit |
| 489 | |
| 490 | while ( !(UCSRA & (1<<RXC)) ) ; // wait |
| 491 | bts = UDR; // get the total bytes to send |
| 492 | while(bts){ // receive bts bytes from uart, and fwd on I2C |
| 493 | while ( !(UCSRA & (1<<RXC)) ) ; // wait |
| 494 | TWDR = UDR; |
| 495 | TWCR = 0x84; // transmit |
| 496 | while(!(TWCR & 0x80) && timeout)timeout--; // wait for confirmation of transmit |
| 497 | asm("WDR"); |
| 498 | bts--; |
| 499 | } |
| 500 | TWCR = 0x94; // stop bit |
| 501 | while ( !(UCSRA & (1<<UDRE) ) ); //Wait until transmit buffer empty |
| 502 | if(timeout)UDR = 1; // send timeout status |
| 503 | else UDR=0; |
| 504 | }else if(ch == KUTTYPY_I2C_READ) |
| 505 | { |
| 506 | timeout = 10000; |
| 507 | while ( !(UCSRA & (1<<RXC)) ) ; // wait |
| 508 | data = UDR; // get the I2C address |
| 509 | TWCR = 0xA4; // send a start bit on i2c bus |
| 510 | while(!(TWCR & 0x80) && timeout)timeout--; // wait for confirmation of transmit |
| 511 | TWDR = data<<1; // load address of i2c device |
| 512 | TWCR = 0x84; // transmit |
| 513 | while(!(TWCR & 0x80) && timeout)timeout--; // wait for confirmation of transmit |
| 514 | |
| 515 | while ( !(UCSRA & (1<<RXC)) ) ; // wait |
| 516 | TWDR = UDR; // write the register to read from. |
| 517 | TWCR = 0x84; // transmit |
| 518 | while(!(TWCR & 0x80) && timeout)timeout--; // wait for confirmation of transmit |
| 519 | asm("WDR"); |
| 520 | |
| 521 | while ( !(UCSRA & (1<<RXC)) ) ; // wait |
| 522 | bts = UDR; // get the total bytes to read |
| 523 | |
| 524 | TWCR = 0xA4; // send a repeated start bit on i2c bus |
| 525 | while(!(TWCR & 0x80) && timeout)timeout--; // wait for confirmation of transmit |
| 526 | TWDR = data<<1|1; // load address of i2c device [ READ MODE ] |
| 527 | TWCR = 0xC4; // transmit |
| 528 | while(!(TWCR & 0x80) && timeout)timeout--; // wait for confirmation of transmit |
| 529 | |
| 530 | while(bts-1){ |
| 531 | TWCR = 0xC4; // transmit, ACK (byte request) |
| 532 | while(!(TWCR & 0x80) && timeout)timeout--; // wait for confirmation of transmit |
| 533 | |
| 534 | while ( !(UCSRA & (1<<UDRE) ) ); //Wait until transmit buffer empty |
| 535 | UDR = TWDR; // and grab the target data |
| 536 | asm("WDR"); |
| 537 | bts--; |
| 538 | } |
| 539 | TWCR = 0x84; // transmit, NACK (last byte request) |
| 540 | while(!(TWCR & 0x80) && timeout)timeout--; // wait for confirmation of transmit |
| 541 | while ( !(UCSRA & (1<<UDRE) ) ); //Wait until transmit buffer empty |
| 542 | UDR = TWDR; // and grab the target data |
| 543 | TWCR = 0x94; // stop bit |
| 544 | while ( !(UCSRA & (1<<UDRE) ) ); //Wait until transmit buffer empty |
| 545 | if(timeout)UDR = 1; // send timeout status |
| 546 | else UDR=0; |
| 547 | |
| 548 | }else if(ch == KUTTYPY_I2C_SCAN) |
| 549 | { |
| 550 | timeout=11000; |
| 551 | DDRC=1 ;// SCL as output |
| 552 | PORTC=0; while(timeout--); PORTC=3; |
| 553 | DDRC=0 ;// SCL as input |
| 554 | PORTC = 3; //Enable SCL/SDA Pull up |
| 555 | |
| 556 | timeout=50000; |
| 557 | for(data=1;data<=127;data++){ |
| 558 | TWCR = 0xA4; // send a start bit on i2c bus |
| 559 | while(!(TWCR & 0x80) && timeout)timeout--; // wait for confirmation of transmit |
| 560 | TWDR = data<<1; // load address of i2c device |
| 561 | TWCR = 0x84; // transmit |
| 562 | while(!(TWCR & 0x80) && timeout)timeout--; // wait for confirmation of transmit |
| 563 | asm("WDR"); |
| 564 | if(TWSR&0xFC == 0x18){ // SLA+W has been transmitted; ACK has been received |
| 565 | while ( !(UCSRA & (1<<UDRE) ) ); //Wait until transmit buffer empty |
| 566 | UDR = data; // send the address that responded |
| 567 | } |
| 568 | } |
| 569 | TWCR = 0x94; // stop bit |
| 570 | while ( !(UCSRA & (1<<UDRE) ) ); //Wait until transmit buffer empty |
| 571 | if(timeout){UDR = 255;} // termination character. |
| 572 | else {UDR=254;} |
| 573 | |
| 574 | }else if(ch == 'j') { |
| 575 | //appStart(0); |
| 576 | //watchdogConfig(WATCHDOG_OFF); |
| 577 | watchdogConfig(WATCHDOG_16MS); |
| 578 | while(1); |
| 579 | //app_start(); |
| 580 | } |
| 581 | while ( !(UCSRA & (1<<RXC)) ){asm("WDR");} // wait for command from PC |
| 582 | ch = UDR; // Store the received byte |
| 583 | |
| 584 | }while(1); /* kuttypy functions over */ |
| 585 | |
| 586 | } |
| 587 | /* end of kuttypy segment */ |
| 588 | |
| 589 | |
| 590 | |
| 591 | else if(ch == STK_GET_PARAMETER) { |
| 592 | unsigned char which = getch(); |
| 593 | verifySpace(); |
| 594 | if (which == 0x82) { |
| 595 | /* |
| 596 | * Send optiboot version as "minor SW version" |
| 597 | */ |
| 598 | putch(OPTIBOOT_MINVER); |
| 599 | } else if (which == 0x81) { |
| 600 | putch(OPTIBOOT_MAJVER); |
| 601 | } else { |
| 602 | /* |
| 603 | * GET PARAMETER returns a generic 0x03 reply for |
| 604 | * other parameters - enough to keep Avrdude happy |
| 605 | */ |
| 606 | putch(0x03); |
| 607 | } |
| 608 | } |
| 609 | else if(ch == STK_SET_DEVICE) { |
| 610 | // SET DEVICE is ignored |
| 611 | getNch(20); |
| 612 | } |
| 613 | else if(ch == STK_SET_DEVICE_EXT) { |
| 614 | // SET DEVICE EXT is ignored |
| 615 | getNch(5); |
| 616 | } |
| 617 | else if(ch == STK_LOAD_ADDRESS) { |
| 618 | // LOAD ADDRESS |
| 619 | uint16_t newAddress; |
| 620 | newAddress = getch(); |
| 621 | newAddress = (newAddress & 0xff) | (getch() << 8); |
| 622 | #ifdef RAMPZ |
| 623 | // Transfer top bit to RAMPZ |
| 624 | RAMPZ = (newAddress & 0x8000) ? 1 : 0; |
| 625 | #endif |
| 626 | newAddress += newAddress; // Convert from word address to byte address |
| 627 | address = newAddress; |
| 628 | verifySpace(); |
| 629 | } |
| 630 | else if(ch == STK_UNIVERSAL) { |
| 631 | // UNIVERSAL command is ignored |
| 632 | getNch(4); |
| 633 | putch(0x00); |
| 634 | } |
| 635 | /* Write memory, length is big endian and is in bytes */ |
| 636 | else if(ch == STK_PROG_PAGE) { |
| 637 | // PROGRAM PAGE - we support flash programming only, not EEPROM |
| 638 | uint8_t *bufPtr; |
| 639 | uint16_t addrPtr; |
| 640 | |
| 641 | getch(); /* getlen() */ |
| 642 | length = getch(); |
| 643 | getch(); |
| 644 | |
| 645 | // If we are in RWW section, immediately start page erase |
| 646 | if (address < NRWWSTART) __boot_page_erase_short((uint16_t)(void*)address); |
| 647 | |
| 648 | // While that is going on, read in page contents |
| 649 | bufPtr = buff; |
| 650 | do *bufPtr++ = getch(); |
| 651 | while (--length); |
| 652 | |
| 653 | // If we are in NRWW section, page erase has to be delayed until now. |
| 654 | // Todo: Take RAMPZ into account (not doing so just means that we will |
| 655 | // treat the top of both "pages" of flash as NRWW, for a slight speed |
| 656 | // decrease, so fixing this is not urgent.) |
| 657 | if (address >= NRWWSTART) __boot_page_erase_short((uint16_t)(void*)address); |
| 658 | |
| 659 | // Read command terminator, start reply |
| 660 | verifySpace(); |
| 661 | |
| 662 | // If only a partial page is to be programmed, the erase might not be complete. |
| 663 | // So check that here |
| 664 | boot_spm_busy_wait(); |
| 665 | |
| 666 | #ifdef VIRTUAL_BOOT_PARTITION |
| 667 | if ((uint16_t)(void*)address == 0) { |
| 668 | // This is the reset vector page. We need to live-patch the code so the |
| 669 | // bootloader runs. |
| 670 | // |
| 671 | // Move RESET vector to WDT vector |
| 672 | uint16_t vect = buff[0] | (buff[1]<<8); |
| 673 | rstVect = vect; |
| 674 | wdtVect = buff[8] | (buff[9]<<8); |
| 675 | vect -= 4; // Instruction is a relative jump (rjmp), so recalculate. |
| 676 | buff[8] = vect & 0xff; |
| 677 | buff[9] = vect >> 8; |
| 678 | |
| 679 | // Add jump to bootloader at RESET vector |
| 680 | buff[0] = 0x7f; |
| 681 | buff[1] = 0xce; // rjmp 0x1d00 instruction |
| 682 | } |
| 683 | #endif |
| 684 | |
| 685 | // Copy buffer into programming buffer |
| 686 | bufPtr = buff; |
| 687 | addrPtr = (uint16_t)(void*)address; |
| 688 | ch = SPM_PAGESIZE / 2; |
| 689 | do { |
| 690 | uint16_t a; |
| 691 | a = *bufPtr++; |
| 692 | a |= (*bufPtr++) << 8; |
| 693 | __boot_page_fill_short((uint16_t)(void*)addrPtr,a); |
| 694 | addrPtr += 2; |
| 695 | } while (--ch); |
| 696 | |
| 697 | // Write from programming buffer |
| 698 | __boot_page_write_short((uint16_t)(void*)address); |
| 699 | boot_spm_busy_wait(); |
| 700 | |
| 701 | #if defined(RWWSRE) |
| 702 | // Reenable read access to flash |
| 703 | boot_rww_enable(); |
| 704 | #endif |
| 705 | |
| 706 | } |
| 707 | /* Read memory block mode, length is big endian. */ |
| 708 | else if(ch == STK_READ_PAGE) { |
| 709 | // READ PAGE - we only read flash |
| 710 | getch(); /* getlen() */ |
| 711 | length = getch(); |
| 712 | getch(); |
| 713 | |
| 714 | verifySpace(); |
| 715 | do { |
| 716 | #ifdef VIRTUAL_BOOT_PARTITION |
| 717 | // Undo vector patch in bottom page so verify passes |
| 718 | if (address == 0) ch=rstVect & 0xff; |
| 719 | else if (address == 1) ch=rstVect >> 8; |
| 720 | else if (address == 8) ch=wdtVect & 0xff; |
| 721 | else if (address == 9) ch=wdtVect >> 8; |
| 722 | else ch = pgm_read_byte_near(address); |
| 723 | address++; |
| 724 | #elif defined(RAMPZ) |
| 725 | // Since RAMPZ should already be set, we need to use EPLM directly. |
| 726 | // Also, we can use the autoincrement version of lpm to update "address" |
| 727 | // do putch(pgm_read_byte_near(address++)); |
| 728 | // while (--length); |
| 729 | // read a Flash and increment the address (may increment RAMPZ) |
| 730 | __asm__ ("elpm %0,Z+\n" : "=r" (ch), "=z" (address): "1" (address)); |
| 731 | #else |
| 732 | // read a Flash byte and increment the address |
| 733 | __asm__ ("lpm %0,Z+\n" : "=r" (ch), "=z" (address): "1" (address)); |
| 734 | #endif |
| 735 | putch(ch); |
| 736 | } while (--length); |
| 737 | } |
| 738 | |
| 739 | /* Get device signature bytes */ |
| 740 | else if(ch == STK_READ_SIGN) { |
| 741 | // READ SIGN - return what Avrdude wants to hear |
| 742 | verifySpace(); |
| 743 | putch(SIGNATURE_0); |
| 744 | putch(SIGNATURE_1); |
| 745 | putch(SIGNATURE_2); |
| 746 | } |
| 747 | else if (ch == STK_LEAVE_PROGMODE) { /* 'Q' */ |
| 748 | verifySpace(); |
| 749 | //watchdogConfig(WATCHDOG_16MS);//while (1); |
| 750 | } |
| 751 | else { |
| 752 | // This covers the response to commands like STK_ENTER_PROGMODE |
| 753 | verifySpace(); |
| 754 | } |
| 755 | putch(STK_OK); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | void putch(char ch) { |
| 760 | while (!(UART_SRA & _BV(UDRE0))); |
| 761 | UART_UDR = ch; |
| 762 | } |
| 763 | |
| 764 | uint8_t getch(void) { |
| 765 | uint8_t ch; |
| 766 | |
| 767 | |
| 768 | while(!(UART_SRA & _BV(RXC0))) |
| 769 | ; |
| 770 | if (!(UART_SRA & _BV(FE0))) { |
| 771 | /* |
| 772 | * A Framing Error indicates (probably) that something is talking |
| 773 | * to us at the wrong bit rate. Assume that this is because it |
| 774 | * expects to be talking to the application, and DON'T reset the |
| 775 | * watchdog. This should cause the bootloader to abort and run |
| 776 | * the application "soon", if it keeps happening. (Note that we |
| 777 | * don't care that an invalid char is returned...) |
| 778 | */ |
| 779 | watchdogReset(); |
| 780 | } |
| 781 | ch = UART_UDR; |
| 782 | return ch; |
| 783 | } |
| 784 | |
| 785 | void getNch(uint8_t count) { |
| 786 | do getch(); while (--count); |
| 787 | verifySpace(); |
| 788 | } |
| 789 | |
| 790 | void verifySpace() { |
| 791 | if (getch() != CRC_EOP) { |
| 792 | watchdogConfig(WATCHDOG_16MS); // shorten WD timeout |
| 793 | while (1) // and busy-loop so that WD causes |
| 794 | ; // a reset and app start. |
| 795 | } |
| 796 | putch(STK_INSYNC); |
| 797 | } |
| 798 | |
| 799 | // Watchdog functions. These are only safe with interrupts turned off. |
| 800 | void watchdogReset() { |
| 801 | __asm__ __volatile__ ( |
| 802 | "wdr\n" |
| 803 | ); |
| 804 | } |
| 805 | |
| 806 | void watchdogConfig(uint8_t x) { |
| 807 | WDTCSR = _BV(WDCE) | _BV(WDE); |
| 808 | WDTCSR = x; |
| 809 | } |
| 810 | |
| 811 | void appStart(uint8_t rstFlags) { |
| 812 | // save the reset flags in the designated register |
| 813 | // This can be saved in a main program by putting code in .init0 (which |
| 814 | // executes before normal c init code) to save R2 to a global variable. |
| 815 | __asm__ __volatile__ ("mov r2, %0\n" :: "r" (rstFlags)); |
| 816 | |
| 817 | watchdogConfig(WATCHDOG_OFF); |
| 818 | __asm__ __volatile__ ( |
| 819 | #ifdef VIRTUAL_BOOT_PARTITION |
| 820 | // Jump to WDT vector |
| 821 | "ldi r30,4\n" |
| 822 | "clr r31\n" |
| 823 | #else |
| 824 | // Jump to RST vector |
| 825 | "clr r30\n" |
| 826 | "clr r31\n" |
| 827 | #endif |
| 828 | "ijmp\n" |
| 829 | ); |
| 830 | } |