Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Identify which boot loader is loaded in the ATmega328
// Detect which type of boot loader is present, using a fixed built-in table
// 2012-03-06 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
#include <avr/pgmspace.h>
#include <util/crc16.h>
#define VERSION "2"
// list of all known boot loaders with their unique signatures
struct { word crc; const char* desc; } signatures[] = {
0x489C, "Duemilanove",
0xF1A0, "Nanode (Duemilanove mod)",
0xFD70, "OptiBoot 4.4",
0, 0
};
static word CalculateChecksum (word addr, word size) {
word crc = ~0;
prog_uint8_t* p = (prog_uint8_t*) addr;
for (word i = 0; i < size; ++i)
crc = _crc16_update(crc, pgm_read_byte(p++));
Serial.print(" CRC ");
Serial.print(size);
Serial.print("b @ 0x");
Serial.print(addr, HEX);
Serial.print(" = ");
Serial.println(crc, HEX);
return crc;
}
static const char* IdentifyBootLoader (word addr, word size) {
word crc = CalculateChecksum(addr, size);
for (byte i = 0; signatures[i].desc != 0; ++i)
if (signatures[i].crc == crc)
return signatures[i].desc;
return 0;
}
void setup () {
Serial.begin(57600);
Serial.println("\n[bootCheck." VERSION "]");
const char* message = IdentifyBootLoader(0x7800, 2048);
if (message == 0)
message = IdentifyBootLoader(0x7E00, 512);
if (message == 0)
message = "(UNKNOWN)";
Serial.print("Boot loader: ");
Serial.println(message);
}
void loop () {}
@thomasaw

This comment has been minimized.

Copy link

thomasaw commented Jun 19, 2013

Like Denisj on your blog, I needed to change the serial speed to 9600 to get this to work.

@pjblittle

This comment has been minimized.

Copy link

pjblittle commented May 15, 2015

Thank you for this. I had to change line 19 because prog_uint8_t is deprecated (it is May 2015, and I'm using Arduino 1.6.1). So this is what I have now:
uint8_t* p = (uint8_t*) addr;
and it ran OK so I assume my change didn't cause the results to be in error.
Apparently the bootloader on my Seeeduino Stalker board is a Duemilanove.

@FPSUsername

This comment has been minimized.

Copy link

FPSUsername commented Jul 17, 2015

BootCheck.ino: In function 'word CalculateChecksum(word, word)':
BootCheck.ino:19:3: error: 'prog_uint8_t' was not declared in this scope
BootCheck.ino:19:17: error: 'p' was not declared in this scope
BootCheck.ino:19:35: error: expected primary-expression before ')' token
BootCheck.ino:19:37: error: expected ';' before 'addr'
Error compiling.

This is when I want to upload it to my arduino uno.

@GearsAndSuch

This comment has been minimized.

Copy link

GearsAndSuch commented Apr 14, 2016

JCW,

This no longer compiles in arduino environments (tested on Arduino 1.6.8), as the prog_uint8_t type is deprecated. The compiler gives the following error:
BootLoaderID:20: error: expected ';' before 'addr'
prog_uint8_t* p = (prog_uint8_t*) addr;
^
exit status 1
prog_uint8_t' was not declared in this scope

Based on this discussion, I suggest adding
typedef uint8_t PROGMEM prog_uint8_t;
just before
prog_uint8_t* p = (prog_uint8_t*) addr; (Line 19, at the time of writing).

@GearsAndSuch

This comment has been minimized.

Copy link

GearsAndSuch commented Apr 15, 2016

Another Interesting tidbit: One Arduino Leonardo (from adafruit) says:
[bootCheck.2]
CRC 2048b @ 0x7800 = 6AF7
CRC 512b @ 0x7E00 = 5E1E
Boot loader: (UNKNOWN)

while another (source unknown) says:

[bootCheck.2]
CRC 2048b @ 0x7800 = D557
CRC 512b @ 0x7E00 = 1F47
Boot loader: (UNKNOWN)

@Edison517

This comment has been minimized.

Copy link

Edison517 commented Jul 15, 2016

I'm pretty sure I uploaded OptiBoot 6.2 on one of my ATMega328P chips and got this:
[bootCheck.2]
CRC 2048b @ 0x7800 = 7165
CRC 512b @ 0x7E00 = 4165
Boot loader: (UNKNOWN)

@Edison517

This comment has been minimized.

Copy link

Edison517 commented Jul 18, 2016

Ignore my previous comment (leaving it there for search purposes). I used the optiLoader program to burn the bootloader in my previous comment. I don't know what version of the OptiBoot bootloader it loaded.

I just used an Uno R3 with ArduinoISP sketch to burn a bootloader on a new 328 and got the following (using the latest 6.2 OptiBoot)
[bootCheck.2]
CRC 2048b @ 0x7800 = 5813
CRC 512b @ 0x7E00 = 6813
Boot loader: (UNKNOWN)

@kgamecarter

This comment has been minimized.

Copy link

kgamecarter commented Jun 19, 2019

add
0xB6F1, "OptiBoot 8.0",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.