|
1 | 1 | # Agent Instructions |
2 | 2 |
|
| 3 | +This file provides guidance to AI agents when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +CDC/DFU/UF2 bootloader for Nordic nRF52 microcontrollers (nRF52832, nRF52833, nRF52840). Supports DFU over Serial, BLE OTA, and USB mass storage (UF2). Runs on 60+ boards. |
| 8 | + |
3 | 9 | ## Style |
4 | | -- Follow the repo `.clang-format` when making changes. |
| 10 | + |
| 11 | +Follow the repo `.clang-format` when making changes. |
5 | 12 |
|
6 | 13 | ## Build / Verify |
7 | | -- Prefer CMake for testing/verification. |
8 | | -- Always build both boards during verification: |
9 | | - - `feather_nrf52832` |
10 | | - - `feather_nrf52840_express` |
| 14 | + |
| 15 | +Default board: `feather_nrf52840_express`. Always verify changes against both boards: |
| 16 | +- `feather_nrf52840_express` (nRF52840) |
| 17 | +- `feather_nrf52832` (nRF52832) |
11 | 18 |
|
12 | 19 | ### CMake (preferred) |
13 | 20 | ```bash |
14 | | -cmake -S . -B cmake-build-feather_nrf52832 -DBOARD=feather_nrf52832 |
15 | | -cmake --build cmake-build-feather_nrf52832 |
16 | | - |
17 | 21 | cmake -S . -B cmake-build-feather_nrf52840_express -DBOARD=feather_nrf52840_express |
18 | 22 | cmake --build cmake-build-feather_nrf52840_express |
| 23 | + |
| 24 | +cmake -S . -B cmake-build-feather_nrf52832 -DBOARD=feather_nrf52832 |
| 25 | +cmake --build cmake-build-feather_nrf52832 |
19 | 26 | ``` |
20 | 27 |
|
21 | 28 | ### Make (alternate) |
22 | 29 | ```bash |
23 | | -make BOARD=feather_nrf52832 all |
24 | 30 | make BOARD=feather_nrf52840_express all |
| 31 | +make BOARD=feather_nrf52832 all |
| 32 | +``` |
| 33 | + |
| 34 | +### Flashing |
| 35 | +```bash |
| 36 | +make BOARD={board} flash # Flash via JLink |
| 37 | +make BOARD={board} flash-dfu # Flash via Serial/CDC DFU |
| 38 | +make BOARD={board} flash-sd # Flash SoftDevice only |
25 | 39 | ``` |
| 40 | + |
| 41 | +### Build all boards |
| 42 | +```bash |
| 43 | +python3 tools/build_all.py |
| 44 | +``` |
| 45 | + |
| 46 | +## Architecture |
| 47 | + |
| 48 | +### MCU Variants and SoftDevices |
| 49 | +- **nrf52** (nRF52832): UART-only bootloader, default SoftDevice s132 v6.1.1 |
| 50 | +- **nrf52833**: USB support, default SoftDevice s140 v7.3.0 |
| 51 | +- **nrf52840**: Full USB + OTA, default SoftDevice s140 v6.1.1 |
| 52 | +- Boards may override `SD_NAME`/`SD_VERSION` in their `board.mk` |
| 53 | + |
| 54 | +### Key Source Structure |
| 55 | +- `src/main.c` — Bootloader entry point, DFU mode detection, LED/button init |
| 56 | +- `src/dfu_init.c` — DFU packet validation, CRC/signature verification |
| 57 | +- `src/dfu_ble_svc.c` — BLE DFU service |
| 58 | +- `src/flash_nrf5x.c` — Flash memory operations |
| 59 | +- `src/boards/boards.c` — Board abstraction (LED control, buttons, timing) |
| 60 | +- `src/usb/` — USB stack (nRF52833/nRF52840): CDC serial, MSC storage, UF2 handler |
| 61 | +- `src/usb/uf2/ghostfat.c` — Virtual FAT filesystem for UF2 drag-and-drop |
| 62 | + |
| 63 | +### Board Definition System |
| 64 | + |
| 65 | +Each board lives in `src/boards/{board_name}/` with: |
| 66 | +- `board.h` — Pin definitions, LED/button assignments, USB VID/PID, UF2 metadata |
| 67 | +- `board.mk` — Makefile variable `MCU_SUB_VARIANT` (nrf52, nrf52833, or nrf52840) |
| 68 | +- `board.cmake` — CMake variable `MCU_VARIANT` |
| 69 | +- `pinconfig.c` — CF2 bootloader configuration (flash/RAM size, UF2 family ID) |
| 70 | + |
| 71 | +### Memory Layout (linker scripts in `linker/`) |
| 72 | +- Bootloader occupies ~38KB near end of flash (e.g., 0xF4000–0xFD800 on nRF52840) |
| 73 | +- No heap (`__HEAP_SIZE=0`), static allocation only |
| 74 | +- Special sections: double-reset detection word, bond info for OTA, MBR params, bootloader settings |
| 75 | + |
| 76 | +### Submodules (`lib/`) |
| 77 | +- `tinyusb` — USB device stack |
| 78 | +- `nrfx` — Nordic HAL drivers |
| 79 | +- `uf2` — UF2 format tools |
| 80 | +- `tinycrypt` — Crypto (only when `SIGNED_FW=1`) |
| 81 | +- `sdk/`, `sdk11/` — Nordic SDK libraries |
| 82 | +- `softdevice/` — Precompiled Bluetooth stack binaries |
| 83 | + |
| 84 | +### Compile-Time Feature Flags |
| 85 | +- `SIGNED_FW` — Require signed firmware (disables UF2 unless `FORCE_UF2=1`) |
| 86 | +- `DUALBANK_FW` — Dual-bank updates |
| 87 | +- `DEFAULT_TO_OTA_DFU` — Default to BLE OTA instead of serial DFU |
| 88 | +- `DEBUG` — Enable RTT debugging, larger bootloader region |
| 89 | + |
| 90 | +## CI |
| 91 | + |
| 92 | +GitHub Actions (`.github/workflows/githubci.yml`) builds all boards in parallel using a matrix generated from `src/boards/` directory names. On release, artifacts (zip, hex, uf2) are uploaded as release assets. |
| 93 | + |
| 94 | +## Required Toolchain |
| 95 | + |
| 96 | +- `arm-none-eabi-gcc` (tested with 12.3.1) |
| 97 | +- Python 3 with: `adafruit-nrfutil`, `intelhex` |
| 98 | +- `nrfjprog` (for JLink flashing) |
0 commit comments