Tuesday, August 2, 2016

Self Flashing Planck

Handwired Planck with Arduino Pro Micro loaded with LUFA MassStorage bootloader

How does this work?

Holding down LOWER while plugging in the USB cable will launch the bootloader in Mass Storage mode.

While in Mass Storage mode the Atmega32u4 will appear as a drive to the computer. It will show two files: EEPROM.bin and FLASH.bin. Replacing either of these with an appropriate hex file converted to a .bin file will write the file to flash memory. Delete the existing file before copying the new one. Unmount the drive and unplug and reconnect it without pressing LOWER and the new firmware will run instead of the bootloader.

While the keyboard is functioning holding LOWER + RAISE + Q will launch the bootloader. You must continue to hold LOWER until the bootloader goes into Mass Storage mode. This is assuming you have the default keymap. If not, it will be wherever the RESET code is in your keymap.

LOWER is the key in the bottom row 5th column from the left. The bottom row is connected to C6, the 5th column is connected to B6

By holding down LOWER you are making the equivalent of this connection, through the keyboard matrix.

How do you do this?

Handwire your Planck

Wire up your Planck like this. If you do it some other way you will need to adjust the pins the bootloader uses to trigger Mass Storage mode.

Compile your Planck firmware

You need a working build environment to compile QMK. QMK documentation is here.

Modify the Planck files for the handwired matrix.

qmk_firmware/keyboards/planck/config.h changes:
#define MATRIX_ROW_PINS { D1, D0, D4, C6 }
#define MATRIX_COL_PINS { D7, E6, B4, B5, B6, B2, B3, B1, F7, F6, F5, F4 }

qmk_firmware/keyboards/planck/Makefile. Size must be 4096
# Boot Section Size in *bytes*
#   Teensy halfKay   512
#   Teensy++ halfKay 1024
#   Atmel DFU loader 4096
#   LUFA bootloader  4096
#   USBaspLoader     2048

Compile your firmware and then convert the .hex file to .bin with the following command:
objcopy -Iihex -Obinary thenameofthecompiledfile.hex FLASH.bin

You will end up with a smaller FLASH.bin file. This is the file you will copy to the mass storage drive.

Compile the LUFA MassStorage Bootloader

Required reading:

Get the latest LUFA source code:

Make these changes before compiling:

lufa-LUFA-151115/Bootloaders/MassStorage/makefile changes
MCU          = atmega32u4
ARCH         = AVR8
F_CPU        = 16000000

FLASH_SIZE_KB         = 32

lufa-LUFA-151115/Bootloaders/MassStorage/BootloaderMassStorage.c changes
        /* Enable pull-up on the IO13 pin so we can use it to select the mode */
        PORTB |= (1 << 6); //Set pin B6 as input with internal pullup
        DDRC  |= (1 << 6); //Set pin C6 as low (equivalent of ground)
        PORTC &= ~(1 << 6);

        /* If IO13 is not jumpered to ground, start the user application instead */
        JumpToApplication = ((PINB & (1 << 6)) != 0);

        /* Disable pull-up after the check has completed */
        PORTB &= ~(1 << 6);

Compile the bootloader. You will end up with BootloaderMassStorage.hex

Flashing bootloader using Arduino as ISP

Instructions on using an Arduino as an ISP programmer

The pins to connect to on the Pro Micro:

Pro Micro connected to a Arduino Duemilanove clone.

Commands to write bootloader using Arduino as ISP. Replace comXXX with the correct COM port
avrdude -p m32u4 -c avrisp -P comXXX -b 19200 -e -v
avrdude -p m32u4 -c avrisp -P comXXX -b 19200 -U lfuse:w:0xff:m -U hfuse:w:0xd8:m -U efuse:w:0xc9:m -v
avrdude -p m32u4 -c avrisp -P comXXX -b 19200 -B 4 -U flash:w:"BootloaderMassStorage.hex" -v

The first time the Pro Micro with the new bootloader is plugged in it will go into Mass Storage mode since flashing the bootloader erased the rest of the flash. You can now copy the FLASH.bin file onto the drive.

Should you do this?

Probably only worth the effort if you were going to give this keyboard to someone else who does not have the skills or resources to compile firmware themselves. You would be able to compile new firmware for them and email it to them. They would need no additional software to flash it.

This bootloader does occupy 6KB of flash space. This is 2KB more than the original bootloader. This leaves 26KB free for the firmware. QMK with default keymap is ~25KB. Depending on the features you use in your keymap you may need the 2KB.

An alternative to this is the LUFA DFU bootloader. It only takes up 4KB. It does require a DFU compatible utility like Atmel FLIP. You would make the same changes to: