avr_boot icon indicating copy to clipboard operation
avr_boot copied to clipboard

update software of atmega2560 using sd card

Open shehabhassan opened this issue 7 months ago • 0 comments

I am currently working on an OTA update project for the ATmega2560. After researching different options, I found the avr_boot bootloader, which is designed to flash firmware from an SD card automatically.

The bootloader works correctly only if no application firmware is present on the ATmega2560 flash memory. If I flash only the bootloader (no application code), it correctly loads the firmware from the SD card on reset. However, if an application sketch is already present, the bootloader does not work correctly — it skips loading the firmware from the SD card even after multiple resets.

#define SERIAL_RX_BUFFER_SIZE 512
#define SERIAL_TX_BUFFER_SIZE 28

#define DEBUG true
#define DEBUG_PRINT(x)    if (DEBUG) Serial.print(x)
#define DEBUG_PRINTLN(x)  if (DEBUG) Serial.println(x)

#include <SD.h>
#include <SPI.h>

void setup() {
  Serial.begin(9600);
  delay(200);
  DEBUG_PRINTLN("Running application...");
  
  if (SD.begin(53)) {
    if (SD.exists("FIRMWARE.BIN")) {
      Serial.println(" Firmware ready, resetting...");
      delay(1000);  // Give user time to see message
      softwareReset();
    }
  }

}

void loop() {
  DEBUG_PRINTLN("Normal application running...");
  delay(1000);
}

void softwareReset() {
  Serial.println("Triggering software reset...");
  delay(100);
  cli(); // Disable interrupts to ensure reset
  wdt_enable(WDTO_30MS);
  while (1) {}
}

My Question:

Is this behavior expected from avr_boot?

Should I modify something in the main.c file of the bootloader to force checking for new firmware every reset even if application code exists?

Or should I handle this entirely inside the Arduino sketch (application) instead?

Any suggestions or recommended best practices to solve this issue would be highly appreciated.

shehabhassan avatar Apr 29 '25 14:04 shehabhassan