How and why I compile the QMK keyboard firmware in Alpine Linux | Hund


How and why I compile the QMK keyboard firmware in Alpine Linux

Monday, May 17, 2021

I haven’t touched the firmware on my keyboard for a couple of years now, but the other day I decided it was time to make a few minor tweaks to it.

Compiling the QMK firmware in Gentoo means that I have to setup a development toolchain for the Atmel AVR microprocessor, which is what my keyboard uses. While it’s not rocket science to cross compile things in Gentoo, we can’t ignore the fact that I’m lazy.. So. I found it a lot easier and quicker to just set it up on my secondary computer with Alpine Linux instead.

All I had to do was to install the packages avr-gcc and avr-libc, I could then simply compile the source code. Flashing the firmware is still done on Gentoo with the tool dfu-programmer, simply because that’s the machine my keyboard is connected to.

To make things a lot more simpler I wrote a Bash script that lets me edit, compile and flash the firmware:

  1. The edit option simply opens the keymap.c file using Neovim.
  2. The make option syncs the changes of the source code to my other computer using rsync, it then compiles the new version on my other computer, makes a backup of the old firmware and lastly copies the new firmware back to my desktop computer.
  3. The third options is called flash, and run as root because I can’t use the keyboard when it’s put into the bootloader mode and the commands has to be run with privileges. The third option simply erases the storage on the keyboard, flashes the firmware and then boots the keyboard back up.

This is what the script looks like:

#!/bin/bash -e


case $1 in
        nvim $DIR/qmk_firmware/keyboards/kb/keymaps/default/keymap.c
        rsync -aAXv -e "ssh" $DIR/qmk_firmware/ billy:/home/johan/qmk_firmware/
        ssh johan@billy -t "cd /home/johan/qmk_firmware && make kb:default"
        if test -f "$DIR/$FILE"; then
            mv "$DIR/$FILE" "$DIR/$FILE-$(date +%Y-%m-%d--%H%M%S)"
        rsync -aAXv -e "ssh" billy:/home/johan/qmk_firmware/kb_default.hex "$DIR/$FILE"
        echo "Don't forget to put your keyboard into the bootloader mode!"
        echo "5..." ; sleep 1
        echo "4..." ; sleep 1
        echo "3..." ; sleep 1
        echo "2..." ; sleep 1
        echo "1..." ; sleep 1
        echo "Flashing keyboard..."
        dfu-programmer atmega32u4 erase --force
        dfu-programmer atmega32u4 flash "$DIR/$FILE"
        dfu-programmer atmega32u4 launch

It’s nothing fancy, but it gets the job done. Improvements are always appreciated!