├── breadboard-1-6-x.zip ├── AVR ISP Shield Manual.pdf ├── AVR ISP Shield Schematic.pdf ├── boards.txt ├── README.md └── OPENSMART_ISP.ino /breadboard-1-6-x.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RalphBacon/Bootloader-Shield-8MHz/HEAD/breadboard-1-6-x.zip -------------------------------------------------------------------------------- /AVR ISP Shield Manual.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RalphBacon/Bootloader-Shield-8MHz/HEAD/AVR ISP Shield Manual.pdf -------------------------------------------------------------------------------- /AVR ISP Shield Schematic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RalphBacon/Bootloader-Shield-8MHz/HEAD/AVR ISP Shield Schematic.pdf -------------------------------------------------------------------------------- /boards.txt: -------------------------------------------------------------------------------- 1 | ############################################################## 2 | 3 | atmega328bb.name=ATmega328 on a breadboard (8 MHz internal clock) 4 | 5 | atmega328bb.upload.protocol=arduino 6 | atmega328bb.upload.maximum_size=30720 7 | atmega328bb.upload.speed=57600 8 | 9 | atmega328bb.bootloader.low_fuses=0xE2 10 | atmega328bb.bootloader.high_fuses=0xDA 11 | atmega328bb.bootloader.extended_fuses=0x05 12 | 13 | atmega328bb.bootloader.file=atmega/ATmegaBOOT_168_atmega328_pro_8MHz.hex 14 | atmega328bb.bootloader.unlock_bits=0x3F 15 | atmega328bb.bootloader.lock_bits=0x0F 16 | 17 | atmega328bb.build.mcu=atmega328p 18 | atmega328bb.build.f_cpu=8000000L 19 | atmega328bb.build.core=arduino:arduino 20 | atmega328bb.build.variant=arduino:standard 21 | 22 | # Added by RSB to prevent compiler warning 23 | atmega328bb.build.board=AVR_UNO 24 | # End of addition 25 | 26 | atmega328bb.bootloader.tool=arduino:avrdude 27 | atmega328bb.upload.tool=arduino:avrdude 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bootloader Shield: run an Arduino @ 8MHz with no crystal 2 | 3 | Easily upload a new bootloader with this shield, including the 8Mhz (no crystal) version 4 | 5 | # See YouTube video #119 at https://www.youtube.com/ralphbacon 6 | (Direct Link to video: https://youtu.be/BJe-pTgfLLM) 7 | 8 | If you're into power saving with the Arduino chip, the ATMega328P, then running at 8Mhz without a crystal (or resonator) is a great step to getting the sleep power consumption down to under 20µA. 9 | 10 | You can, of course, always use another Arduino as the intelligent device to upload a bootloader to a different Arduino using Dupont cables, but this bootloader shield makes it even easier: no wires, just plug it straight in to your Arduino and run the sketch. 11 | 12 | If you want to use the 8Mhz Arduino on a breadboard bootloader then follow these simple instructions (taken from Arduino.cc). 13 | (Full details: https://www.arduino.cc/en/Tutorial/ArduinoToBreadboard - press **CTRL-click** to launch in new tab) 14 | 15 | - Create a new folder in your Arduino sketches folder, called **hardware**. If you already have this, skip this step! 16 | - Download the zipped folder called **breadboard-1-6-x.zip**, unzip it and place the folder in the new hardware folder. 17 | - Restart your Arduino IDE 18 | - Ensure you can see the new "ATmega328 on a breadboard (8 MHz internal clock)" board in your list of boards in the Tools > Board menu (probably the last one in the list). 19 | 20 | Where I got my Bootloader shield (but generally available) (press **CTRL-click** to launch in new tab) 21 | https://www.aliexpress.com/item/AVR-ISP-Shield-Burning-Bootloader-Programmer-Atmega328P-Bootloader-module-with-buzzer-and-LED-indicator-for-Arduino/32853007180.html 22 | 23 | Once you have loaded this new bootloader onto your Arduino ATmega328P chip it will run from the internal 8Mhz oscillator, _even if you subsequently plug it into an Arduino board with a 16Mhz crystal present_. So don't get the µController chips mixed up! 24 | 25 | Can you use this bootloader file for a surface mounted ATmega328P when burning a bootloader in the usual way of using a different Arduino as the ISP? Of course! It's just a description of what the chip is, defined by some special "fuse" bit settings (which, if you get them wrong, is one of the ways to ensure you never can upload another sketch without re-uploading a new bootloader!). 26 | 27 | Blank ATmega328P chips can be had for as little as £1.20 ($1.60) from the Far East (sometimes with a bootloader already loaded, which you can just overwrite). 28 | 29 | If you like this video please give it a thumbs up, share it and if you're not already subscribed please consider doing so :) 30 | 31 | My channel and blog are here: 32 | ------------------------------------------------------------------ 33 | https://www.youtube.com/RalphBacon 34 | https://ralphbacon.blog 35 | ------------------------------------------------------------------ 36 | -------------------------------------------------------------------------------- /OPENSMART_ISP.ino: -------------------------------------------------------------------------------- 1 | // ArduinoISP version 04m3 2 | // Copyright (c) 2008-2011 Randall Bohn 3 | // If you require a license, see 4 | // http://www.opensource.org/licenses/bsd-license.php 5 | // 6 | // This sketch turns the Arduino into a AVRISP 7 | // using the following arduino pins: 8 | // 9 | // pin name: not-mega: mega(1280 and 2560) 10 | // slave reset: 10: 53 11 | // MOSI: 11: 51 12 | // MISO: 12: 50 13 | // SCK: 13: 52 14 | // 15 | // Put an LED (with resistor) on the following pins: 16 | // 9: Heartbeat - shows the programmer is running 17 | // 8: Error - Lights up if something goes wrong (use red if that makes sense) 18 | // 7: Programming - In communication with the slave 19 | // 20 | // 23 July 2011 Randall Bohn 21 | // -Address Arduino issue 509 :: Portability of ArduinoISP 22 | // http://code.google.com/p/arduino/issues/detail?id=509 23 | // 24 | // October 2010 by Randall Bohn 25 | // - Write to EEPROM > 256 bytes 26 | // - Better use of LEDs: 27 | // -- Flash LED_PMODE on each flash commit 28 | // -- Flash LED_PMODE while writing EEPROM (both give visual feedback of writing progress) 29 | // - Light LED_ERR whenever we hit a STK_NOSYNC. Turn it off when back in sync. 30 | // - Use pins_arduino.h (should also work on Arduino Mega) 31 | // 32 | // October 2009 by David A. Mellis 33 | // - Added support for the read signature command 34 | // 35 | // February 2009 by Randall Bohn 36 | // - Added support for writing to EEPROM (what took so long?) 37 | // Windows users should consider WinAVR's avrdude instead of the 38 | // avrdude included with Arduino software. 39 | // 40 | // January 2008 by Randall Bohn 41 | // - Thanks to Amplificar for helping me with the STK500 protocol 42 | // - The AVRISP/STK500 (mk I) protocol is used in the arduino bootloader 43 | // - The SPI functions herein were developed for the AVR910_ARD programmer 44 | // - More information at http://code.google.com/p/mega-isp 45 | 46 | #include "pins_arduino.h" 47 | #define RESET SS 48 | 49 | #define LED_HB 9 50 | #define LED_ERR 8 51 | #define LED_PMODE 7 52 | #define BUZZER 6 53 | #define PROG_FLICKER true 54 | 55 | #define HWVER 2 56 | #define SWMAJ 1 57 | #define SWMIN 18 58 | 59 | // STK Definitions 60 | #define STK_OK 0x10 61 | #define STK_FAILED 0x11 62 | #define STK_UNKNOWN 0x12 63 | #define STK_INSYNC 0x14 64 | #define STK_NOSYNC 0x15 65 | #define CRC_EOP 0x20 //ok it is a space... 66 | 67 | void pulse(int pin, int times); 68 | 69 | void setup() { 70 | Serial.begin(19200); 71 | pinMode(LED_PMODE, OUTPUT); 72 | pulse(LED_PMODE, 2); 73 | pinMode(LED_ERR, OUTPUT); 74 | pulse(LED_ERR, 2); 75 | pinMode(LED_HB, OUTPUT); 76 | pulse(LED_HB, 2); 77 | 78 | 79 | pinMode(BUZZER, OUTPUT); 80 | } 81 | 82 | int error = 0; 83 | int pmode = 0; 84 | // address for reading and writing, set by 'U' command 85 | int here; 86 | uint8_t buff[256]; // global block storage 87 | 88 | #define beget16(addr) (*addr * 256 + *(addr+1) ) 89 | typedef struct param { 90 | uint8_t devicecode; 91 | uint8_t revision; 92 | uint8_t progtype; 93 | uint8_t parmode; 94 | uint8_t polling; 95 | uint8_t selftimed; 96 | uint8_t lockbytes; 97 | uint8_t fusebytes; 98 | int flashpoll; 99 | int eeprompoll; 100 | int pagesize; 101 | int eepromsize; 102 | int flashsize; 103 | } 104 | parameter; 105 | 106 | parameter param; 107 | 108 | // this provides a heartbeat on pin 9, so you can tell the software is running. 109 | uint8_t hbval = 128; 110 | int8_t hbdelta = 8; 111 | void heartbeat() { 112 | if (hbval > 192) hbdelta = -hbdelta; 113 | if (hbval < 32) hbdelta = -hbdelta; 114 | hbval += hbdelta; 115 | analogWrite(LED_HB, hbval); 116 | delay(20); 117 | } 118 | 119 | 120 | void loop(void) { 121 | // is pmode active? 122 | if (pmode) digitalWrite(LED_PMODE, HIGH); 123 | else digitalWrite(LED_PMODE, LOW); 124 | // is there an error? 125 | if (error) digitalWrite(LED_ERR, HIGH); 126 | else digitalWrite(LED_ERR, LOW); 127 | 128 | // light the heartbeat LED 129 | heartbeat(); 130 | if (Serial.available()) { 131 | avrisp(); 132 | } 133 | } 134 | 135 | uint8_t getch() { 136 | while (!Serial.available()); 137 | return Serial.read(); 138 | } 139 | void fill(int n) { 140 | for (int x = 0; x < n; x++) { 141 | buff[x] = getch(); 142 | } 143 | } 144 | 145 | #define PTIME 30 146 | void pulse(int pin, int times) { 147 | do { 148 | digitalWrite(pin, HIGH); 149 | delay(PTIME); 150 | digitalWrite(pin, LOW); 151 | delay(PTIME); 152 | } 153 | while (times--); 154 | } 155 | 156 | void prog_lamp(int state) { 157 | if (PROG_FLICKER) 158 | digitalWrite(LED_PMODE, state); 159 | } 160 | 161 | void spi_init() { 162 | uint8_t x; 163 | SPCR = 0x53; 164 | x = SPSR; 165 | x = SPDR; 166 | } 167 | 168 | void spi_wait() { 169 | do { 170 | } 171 | while (!(SPSR & (1 << SPIF))); 172 | } 173 | 174 | uint8_t spi_send(uint8_t b) { 175 | uint8_t reply; 176 | SPDR = b; 177 | spi_wait(); 178 | reply = SPDR; 179 | return reply; 180 | } 181 | 182 | uint8_t spi_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) { 183 | uint8_t n; 184 | spi_send(a); 185 | n = spi_send(b); 186 | //if (n != a) error = -1; 187 | n = spi_send(c); 188 | return spi_send(d); 189 | } 190 | 191 | void empty_reply() { 192 | if (CRC_EOP == getch()) { 193 | Serial.print((char)STK_INSYNC); 194 | Serial.print((char)STK_OK); 195 | } 196 | else { 197 | error++; 198 | Serial.print((char)STK_NOSYNC); 199 | } 200 | } 201 | 202 | void breply(uint8_t b) { 203 | if (CRC_EOP == getch()) { 204 | Serial.print((char)STK_INSYNC); 205 | Serial.print((char)b); 206 | Serial.print((char)STK_OK); 207 | } 208 | else { 209 | error++; 210 | Serial.print((char)STK_NOSYNC); 211 | } 212 | } 213 | 214 | void get_version(uint8_t c) { 215 | switch (c) { 216 | case 0x80: 217 | breply(HWVER); 218 | break; 219 | case 0x81: 220 | breply(SWMAJ); 221 | break; 222 | case 0x82: 223 | breply(SWMIN); 224 | break; 225 | case 0x93: 226 | breply('S'); // serial programmer 227 | break; 228 | default: 229 | breply(0); 230 | } 231 | } 232 | 233 | void set_parameters() { 234 | // call this after reading paramter packet into buff[] 235 | param.devicecode = buff[0]; 236 | param.revision = buff[1]; 237 | param.progtype = buff[2]; 238 | param.parmode = buff[3]; 239 | param.polling = buff[4]; 240 | param.selftimed = buff[5]; 241 | param.lockbytes = buff[6]; 242 | param.fusebytes = buff[7]; 243 | param.flashpoll = buff[8]; 244 | // ignore buff[9] (= buff[8]) 245 | // following are 16 bits (big endian) 246 | param.eeprompoll = beget16(&buff[10]); 247 | param.pagesize = beget16(&buff[12]); 248 | param.eepromsize = beget16(&buff[14]); 249 | 250 | // 32 bits flashsize (big endian) 251 | param.flashsize = buff[16] * 0x01000000 252 | + buff[17] * 0x00010000 253 | + buff[18] * 0x00000100 254 | + buff[19]; 255 | 256 | } 257 | 258 | void start_pmode() { 259 | spi_init(); 260 | // following delays may not work on all targets... 261 | pinMode(RESET, OUTPUT); 262 | digitalWrite(RESET, HIGH); 263 | pinMode(SCK, OUTPUT); 264 | digitalWrite(SCK, LOW); 265 | delay(50); 266 | digitalWrite(RESET, LOW); 267 | delay(50); 268 | pinMode(MISO, INPUT); 269 | pinMode(MOSI, OUTPUT); 270 | spi_transaction(0xAC, 0x53, 0x00, 0x00); 271 | pmode = 1; 272 | } 273 | 274 | void end_pmode() { 275 | pinMode(MISO, INPUT); 276 | pinMode(MOSI, INPUT); 277 | pinMode(SCK, INPUT); 278 | pinMode(RESET, INPUT); 279 | pmode = 0; 280 | } 281 | 282 | void universal() { 283 | int w; 284 | uint8_t ch; 285 | 286 | fill(4); 287 | ch = spi_transaction(buff[0], buff[1], buff[2], buff[3]); 288 | breply(ch); 289 | } 290 | 291 | void flash(uint8_t hilo, int addr, uint8_t data) { 292 | spi_transaction(0x40 + 8 * hilo, 293 | addr >> 8 & 0xFF, 294 | addr & 0xFF, 295 | data); 296 | } 297 | void commit(int addr) { 298 | if (PROG_FLICKER) prog_lamp(LOW); 299 | spi_transaction(0x4C, (addr >> 8) & 0xFF, addr & 0xFF, 0); 300 | if (PROG_FLICKER) { 301 | delay(PTIME); 302 | prog_lamp(HIGH); 303 | } 304 | } 305 | 306 | //#define _current_page(x) (here & 0xFFFFE0) 307 | int current_page(int addr) { 308 | if (param.pagesize == 32) return here & 0xFFFFFFF0; 309 | if (param.pagesize == 64) return here & 0xFFFFFFE0; 310 | if (param.pagesize == 128) return here & 0xFFFFFFC0; 311 | if (param.pagesize == 256) return here & 0xFFFFFF80; 312 | return here; 313 | } 314 | 315 | 316 | void write_flash(int length) { 317 | fill(length); 318 | if (CRC_EOP == getch()) { 319 | Serial.print((char) STK_INSYNC); 320 | Serial.print((char) write_flash_pages(length)); 321 | } 322 | else { 323 | error++; 324 | Serial.print((char) STK_NOSYNC); 325 | } 326 | } 327 | 328 | uint8_t write_flash_pages(int length) { 329 | int x = 0; 330 | int page = current_page(here); 331 | while (x < length) { 332 | if (page != current_page(here)) { 333 | commit(page); 334 | page = current_page(here); 335 | } 336 | flash(LOW, here, buff[x++]); 337 | flash(HIGH, here, buff[x++]); 338 | here++; 339 | } 340 | 341 | commit(page); 342 | 343 | return STK_OK; 344 | } 345 | 346 | #define EECHUNK (32) 347 | uint8_t write_eeprom(int length) { 348 | // here is a word address, get the byte address 349 | int start = here * 2; 350 | int remaining = length; 351 | if (length > param.eepromsize) { 352 | error++; 353 | return STK_FAILED; 354 | } 355 | while (remaining > EECHUNK) { 356 | write_eeprom_chunk(start, EECHUNK); 357 | start += EECHUNK; 358 | remaining -= EECHUNK; 359 | } 360 | write_eeprom_chunk(start, remaining); 361 | return STK_OK; 362 | } 363 | // write (length) bytes, (start) is a byte address 364 | uint8_t write_eeprom_chunk(int start, int length) { 365 | // this writes byte-by-byte, 366 | // page writing may be faster (4 bytes at a time) 367 | fill(length); 368 | prog_lamp(LOW); 369 | for (int x = 0; x < length; x++) { 370 | int addr = start + x; 371 | spi_transaction(0xC0, (addr >> 8) & 0xFF, addr & 0xFF, buff[x]); 372 | delay(45); 373 | } 374 | prog_lamp(HIGH); 375 | return STK_OK; 376 | } 377 | 378 | void program_page() { 379 | char result = (char) STK_FAILED; 380 | int length = 256 * getch(); 381 | length += getch(); 382 | char memtype = getch(); 383 | // flash memory @here, (length) bytes 384 | if (memtype == 'F') { 385 | write_flash(length); 386 | return; 387 | } 388 | if (memtype == 'E') { 389 | result = (char)write_eeprom(length); 390 | if (CRC_EOP == getch()) { 391 | Serial.print((char) STK_INSYNC); 392 | Serial.print(result); 393 | } 394 | else { 395 | error++; 396 | Serial.print((char) STK_NOSYNC); 397 | } 398 | return; 399 | } 400 | Serial.print((char)STK_FAILED); 401 | return; 402 | } 403 | 404 | uint8_t flash_read(uint8_t hilo, int addr) { 405 | return spi_transaction(0x20 + hilo * 8, 406 | (addr >> 8) & 0xFF, 407 | addr & 0xFF, 408 | 0); 409 | } 410 | 411 | char flash_read_page(int length) { 412 | for (int x = 0; x < length; x += 2) { 413 | uint8_t low = flash_read(LOW, here); 414 | Serial.print((char) low); 415 | uint8_t high = flash_read(HIGH, here); 416 | Serial.print((char) high); 417 | here++; 418 | } 419 | return STK_OK; 420 | } 421 | 422 | char eeprom_read_page(int length) { 423 | // here again we have a word address 424 | int start = here * 2; 425 | for (int x = 0; x < length; x++) { 426 | int addr = start + x; 427 | uint8_t ee = spi_transaction(0xA0, (addr >> 8) & 0xFF, addr & 0xFF, 0xFF); 428 | Serial.print((char) ee); 429 | } 430 | return STK_OK; 431 | } 432 | 433 | void read_page() { 434 | char result = (char)STK_FAILED; 435 | int length = 256 * getch(); 436 | length += getch(); 437 | char memtype = getch(); 438 | if (CRC_EOP != getch()) { 439 | error++; 440 | Serial.print((char) STK_NOSYNC); 441 | return; 442 | } 443 | Serial.print((char) STK_INSYNC); 444 | if (memtype == 'F') result = flash_read_page(length); 445 | if (memtype == 'E') result = eeprom_read_page(length); 446 | Serial.print(result); 447 | return; 448 | } 449 | 450 | void read_signature() { 451 | if (CRC_EOP != getch()) { 452 | error++; 453 | Serial.print((char) STK_NOSYNC); 454 | return; 455 | } 456 | Serial.print((char) STK_INSYNC); 457 | uint8_t high = spi_transaction(0x30, 0x00, 0x00, 0x00); 458 | Serial.print((char) high); 459 | uint8_t middle = spi_transaction(0x30, 0x00, 0x01, 0x00); 460 | Serial.print((char) middle); 461 | uint8_t low = spi_transaction(0x30, 0x00, 0x02, 0x00); 462 | 463 | Serial.print((char) low); 464 | Serial.print((char) STK_OK); 465 | } 466 | ////////////////////////////////////////// 467 | ////////////////////////////////////////// 468 | 469 | 470 | //////////////////////////////////// 471 | //////////////////////////////////// 472 | int avrisp() { 473 | uint8_t data, low, high; 474 | uint8_t ch = getch(); 475 | switch (ch) { 476 | case '0': // signon 477 | error = 0; 478 | empty_reply(); 479 | break; 480 | case '1': 481 | if (getch() == CRC_EOP) { 482 | Serial.print((char) STK_INSYNC); 483 | Serial.print("AVR ISP"); 484 | Serial.print((char) STK_OK); 485 | } 486 | break; 487 | case 'A': 488 | get_version(getch()); 489 | break; 490 | case 'B': 491 | fill(20); 492 | set_parameters(); 493 | empty_reply(); 494 | break; 495 | case 'E': // extended parameters - ignore for now 496 | fill(5); 497 | empty_reply(); 498 | break; 499 | 500 | case 'P': 501 | start_pmode(); 502 | empty_reply(); 503 | break; 504 | case 'U': // set address (word) 505 | here = getch(); 506 | here += 256 * getch(); 507 | empty_reply(); 508 | break; 509 | 510 | case 0x60: //STK_PROG_FLASH 511 | low = getch(); 512 | high = getch(); 513 | empty_reply(); 514 | break; 515 | case 0x61: //STK_PROG_DATA 516 | data = getch(); 517 | empty_reply(); 518 | break; 519 | 520 | case 0x64: //STK_PROG_PAGE 521 | program_page(); 522 | break; 523 | 524 | case 0x74: //STK_READ_PAGE 't' 525 | read_page(); 526 | break; 527 | 528 | case 'V': //0x56 529 | universal(); 530 | break; 531 | case 'Q': //0x51 532 | error = 0; 533 | end_pmode(); 534 | empty_reply(); 535 | digitalWrite(BUZZER,HIGH); 536 | delay(200); 537 | digitalWrite(BUZZER,LOW); 538 | break; 539 | 540 | case 0x75: //STK_READ_SIGN 'u' 541 | read_signature(); 542 | break; 543 | 544 | // expecting a command, not CRC_EOP 545 | // this is how we can get back in sync 546 | case CRC_EOP: 547 | error++; 548 | Serial.print((char) STK_NOSYNC); 549 | break; 550 | 551 | // anything else we will return STK_UNKNOWN 552 | default: 553 | error++; 554 | if (CRC_EOP == getch()) 555 | Serial.print((char)STK_UNKNOWN); 556 | else 557 | Serial.print((char)STK_NOSYNC); 558 | } 559 | } 560 | 561 | 562 | --------------------------------------------------------------------------------