Programming MightyHat

Since MigthyHat is primarily a power controller, a power path latch ensures the Raspberry Pi is kept running whenever MightyHat is restarted/reprogrammed. This is an important feature which means that the Raspberry Pi can run undisturbed while we make any changes to the MightyHat firmware.

There are 3 ways to program MightyHat. Let’s review each one:

  1. Programming via FTDI header
    This can be done at any time, but the Pi must be turned off. This is because the serial port of the MightyHat (atmega328p) is shared with the Pi. So after the Pi is off, plug your FTDI Adapter as usual and program it as any other Moteino – ie pick Moteino (if you installed the Moteino core) or Arduino UNO from Tools>Boards in your ArduinoIDE.
    The FTDI connector can only power and program the ATmega328, it cannot power the whole board. You must connect power through uUSB and/or LiPo battery to power your Pi.
  2. Wireless Programming
    The MightyHats have an atmega328 and 4mbit FLASH MEM chip, and come with the standard DualOptiboot bootloader, which allows easy over the air programming. The sample sketch has the wireless programming token check which is required for wireless programming (just uncomment this line: #define ENABLE_WIRELESS_PROGRAMMING), see this guide on how to achieve wireless programming on MightyHat or any Moteino.
    Wireless programming is convenient when you do not have physical access to your MightyHat or RaspberryPi.
  3. Programming directly from the Pi via avrdude
    The RST pin of the atmega328p is connected to the Raspberry Pi pin 22 (GPIO 25). This gives the Pi the ability to reset the MightyHat/atmega328p, which is required to put the atmega328p in bootloader mode. This issue is covered in more detail in this article, so please read that if you want more technical details of what this really does. You will need to install avrdude and patch it so that uploads to the board can be made directly via avrdude. There is a more detailed guide here for how to install this on the MightyHat.
    For this to work the GPIO serial must be completely free and unused by any other process. Also the DTR pin has to be setup to reset the atmega328 before programming (this is what the avrdude patch does).

    Note: that a MightyHat compatible image is published for the “Moteino Framework Gateway” already has this patch, so you don’t need to do this again. Otherwise if you are installing MightyHat on your own run this command to install the patched avrdude:

    git clone ~/avrdude-rpi && ~/avrdude-rpi/install

    After avrdude is patched you can upload a new sketch to the MightyHat using a command such as this:

    avrdude -c arduino -p atmega328p -P /dev/ttyAMA0 -b 115200 -U flash:w:pihat.hex

    Here’s a complete avrdude programming sequence. Note the sudo stop gateway and sudo start gateway before and after the programming (this is required to free the serial port used by the gateway app, or otherwise you have to free up the serial port from any other application using it, or avrdude will fail):