Bootloader

How to flash the Bootloader to the ATmega328

Using Raspberry Pi for flashing

  • Connection, use a resistor 2k2…5k6 in series for every signal:

    RPi  -> Target
    IO25 -> Reset
    IO11 -> SCK
    IO10 -> MOSI
    IO9  -> MISO
    GND  -> GND
    
  • Install AVRdude:

    $ sudo apt-get install avrdude
    
  • Edit the file avrdude.conf and add the following lines under the section PROGRAMMER DEFINITIONS:

    $ sudo nano /usr/local/etc/avrdude.conf
    
    programmer
      id    = "pi_isp";
      desc  = "GPIO bitbang";
      type  = "linuxgpio";
      reset = 25;
      sck   = 11;
      mosi  = 10;
      miso  = 9;
    ;
    
  • Download bootloader:

    $ wget https://github.com/watterott/RPi-UNO-HAT/raw/master/software/bloader.hex
    
  • Flash bootloader:

    $ avrdude -c pi_isp -p m328p -e -U flash:w:bloader.hex:i -U lfuse:w:0xFF:m -U hfuse:w:0xD6:m -U efuse:w:0x05:m -U lock:w:0x0F:m
    

    If the programmer is not found, then the cause could be multiple avrdude versions. In this case add the config file as parameter: -C /usr/local/etc/avrdude.conf

    A mismatch warning 0xFD != 0x05 on the eFuse can be ignored (choose no). Both values are the same setting, but with a different writing of unused bits.

    If the device signature shows 1E 95 14 then use m328 as device name (not m328p).

Using Arduino Uno for flashing

  • Connection (Arduino Uno connected to RPi via USB):

    Uno -> Target
    D10 -> Reset
    D13 -> SCK
    D11 -> MOSI
    D12 -> MISO
    GND -> GND
    
  • Install AVRdude:

    $ sudo apt-get install avrdude
    
  • Download bootloader and ISP firmware:

    $ wget https://github.com/watterott/RPi-UNO-HAT/raw/master/software/bloader.hex
    $ wget https://github.com/watterott/RPi-UNO-HAT/raw/master/software/isp.hex
    
  • Flash ISP firmware to Arduino Uno (USB device ttyACM0 or ttyUSB0):

    $ avrdude -c arduino -P /dev/ttyUSB0 -b 115200 -p m328p -e -U flash:w:isp.hex:i
    
  • Flash bootloader with Arduino Uno (USB device ttyACM0 or ttyUSB0):

    $ avrdude -c stk500v1 -P /dev/ttyUSB0 -b 19200 -p m328p -e -U flash:w:bloader.hex:i -U lfuse:w:0xFF:m -U hfuse:w:0xD6:m -U efuse:w:0x05:m -U lock:w:0x0F:m
    

    A mismatch warning 0xFD != 0x05 on the eFuse can be ignored (choose no). Both values are the same setting, but with a different writing of unused bits.

    If the device signature shows 1E 95 14 then use m328 as device name (not m328p).

Test Bootloader

Auto-Reset for AVRdude needed, see here.

  • Pi 3

    $ sudo avrdude -c arduino -P /dev/ttyS0 -b 57600 -p m328p -v
    
  • all other Pi models

    $ sudo avrdude -c arduino -P /dev/ttyAMA0 -b 57600 -p m328p -v