# Keyboard firmware

Written 2014 - 2026 by Maarten Tromp <maarten@geekabit.nl>

Article: <https://www.geekabit.nl/projects/building-a-keyboard-from-scratch/>

Firmware for the split, column-staggered keyboard (version 1.1): it scans four MCP23017 i/o expanders over I2C and presents itself to the host as a USB boot keyboard, using the software USB stack V-USB.


## Hardware

- ATmega328P at 12 MHz (crystal), powered from USB
- Four MCP23017 i/o expanders (addresses 0x20 - 0x23, two per half), 16 keys each, one switch per input: no matrix, so no ghosting
- I2C at 100 kHz, which is plenty for scanning 64 keys; a debug LED on PB0
- USB D+ on PD2 (INT0), D- on PD3, as V-USB wants it

Key position `n` (0 - 63) is bit `n` of the 64-bit state: bytes 0 - 1 are the first expander's GPIOA and GPIOB, 2 - 3 the second's, and so on. The keymaps, in `src/core/keyboard.c`, are listed by position.


## Layers

Two fn keys, one per half. While one is down, the keys of the fn layer are used: arrows, function keys, page up and down, brackets and so on. Symbols that need shift on a US keyboard (underscore, plus, curly braces, tilde, pipe, double quote) are sent with a virtual shift for as long as that key is down. The host decides what a key code becomes (the keyboard layout is the host's), so a different layout on the host gives different symbols.

A key puts its key code in the report when it goes down and takes it out when it goes up, whatever the layers are doing by then: fn can be let go before the key.


A change of a key is only believed when it lasts: 3 ms for a press (so a scan that read garbage is ignored), 8 ms for a release (the contacts can open and close a few times), and after a release the same key can not go down again for 50 ms. The times are in `src/core/debounce.h`; the main loop gets its milliseconds from timer 0.


## Directory layout

| Path | Contents |
|------|----------|
| `src/core/` | Key scanning logic, debounce, keymaps, the report queue and the report descriptor: no hardware (also used by the tests) |
| `src/hw/` | ATmega328P specific code: `main.c`, the I2C driver, `usbconfig.h` |
| `usbdrv/` | V-USB, as released |
| `tests/` | Unit tests, integration tests and a fuzzer, run on the build machine |
| `unity/` | The unit test framework Unity, as released |
| `tools/` | Checks and scripts |


## Building

```
make            # build/avr-gcc-release/firmware.hex
make debug      # with V-USB's debug output on
make prog-release   # flash with avrdude and a usbasp programmer; the fuses are not touched
```

Needs avr-gcc and avr-libc.


## Testing

```
make test       # unit and integration tests, with ASan and UBSan
make coverage   # coverage of src/core/
make fuzz       # libFuzzer, clang
tools/check.sh  # everything that runs without hardware
```

`tools/check.sh` builds the firmware release and debug with warnings as errors, checks the worst-case stack depth, runs the tests with gcc and clang sanitizers, coverage, clang-tidy, cppcheck, gcc's static analyser, a spell check and the fuzzer. Nothing in it needs the keyboard.


## License

My own files are released under CC0 1.0 Universal, see `LICENSE`.

`usbdrv/` is V-USB by Objective Development, version 2009-08-22, under the GNU GPL version 2 or 3 (your choice) or a commercial licence, see `usbdrv/License.txt`. The firmware is linked with it, so **the firmware as built is covered by the GPL**, not by CC0: whoever gets a binary must be able to get this source. Publishing the source together with the binary, as the article's downloads do, takes care of that.

`unity/` is Unity by ThrowTheSwitch.org, under the MIT licence, see `unity/LICENSE.txt`. It is only used by the tests.

`src/core/hid.h` started as the key code list of Sebastiaan (2011).

USB IDs: vendor 0x16c0 and product 0x27db, Objective Development's shared pair for keyboards (`usbdrv/USB-IDs-for-free.txt`), with `geekabit.nl` as the vendor name. Only the OS's own HID driver is used, which is what that licence asks for. The pair is for keyboards only.
