|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +This file provides guidance to AI coding agents when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Adafruit's fork of the Arduino Core for SAMD21 (Cortex-M0+) and SAMD51 (Cortex-M4) microcontrollers. Provides the board support package (BSP) installed via Arduino Board Manager for Adafruit SAMD boards. |
| 8 | + |
| 9 | +## Build & Test Commands |
| 10 | + |
| 11 | +This is an Arduino BSP — there is no standalone build. Code is compiled via Arduino CLI. |
| 12 | + |
| 13 | +**Default/preferred boards**: Use **metro_m0** (SAMD21/M0) and **metro_m4** (SAMD51/M4) as the reference boards for development and testing. |
| 14 | + |
| 15 | +```bash |
| 16 | +# Install Arduino CLI and set up BSP (CI does this automatically) |
| 17 | +# Build all examples for a specific board: |
| 18 | +python3 tools/build_all.py adafruit_metro_m0 # SAMD21/M0 |
| 19 | +python3 tools/build_all.py adafruit_metro_m4 # SAMD51/M4 |
| 20 | + |
| 21 | +# FQBN format: adafruit:samd:adafruit_<board_id> |
| 22 | + |
| 23 | +# Build a single sketch: |
| 24 | +arduino-cli compile --fqbn adafruit:samd:adafruit_metro_m0 path/to/sketch # SAMD21 |
| 25 | +arduino-cli compile --fqbn adafruit:samd:adafruit_metro_m4 path/to/sketch # SAMD51 |
| 26 | + |
| 27 | +# Upload: |
| 28 | +arduino-cli upload --fqbn adafruit:samd:adafruit_metro_m0 -p /dev/ttyACM0 path/to/sketch |
| 29 | +arduino-cli upload --fqbn adafruit:samd:adafruit_metro_m4 -p /dev/ttyACM0 path/to/sketch |
| 30 | +``` |
| 31 | + |
| 32 | +CI runs `tools/build_all.py` across a matrix of boards to compile all library examples. When testing changes, always verify against both metro_m0 (M0/SAMD21) and metro_m4 (M4/SAMD51) to cover both chip families. |
| 33 | + |
| 34 | +## Architecture |
| 35 | + |
| 36 | +### Core Build System Files |
| 37 | +- **`platform.txt`** — Defines compiler toolchain (arm-none-eabi-gcc), flags, upload tools (bossac, openocd), and build recipes. This is the central build configuration. |
| 38 | +- **`boards.txt`** — Board definitions (36+ boards): MCU type, clock speed, memory layout, USB VID/PID, upload protocol, menu options (optimization level, USB stack selection). |
| 39 | +- **`programmers.txt`** — J-Link and Atmel-ICE debugger configurations. |
| 40 | + |
| 41 | +### `cores/arduino/` — Core Runtime |
| 42 | +The Arduino API implementation for SAMD. Key subsystems: |
| 43 | +- **`startup.c`** — Clock initialization, oscillator config, peripheral clocks (differs significantly between SAMD21 and SAMD51 via `#ifdef __SAMD51__`) |
| 44 | +- **`SERCOM.cpp/h`** — Abstraction over SAMD SERCOM peripherals. SERCOMs are flexible hardware blocks that can be configured as UART, SPI, or I2C. This is the foundation for Wire, SPI, and Serial libraries. |
| 45 | +- **`wiring_analog.c`** — ADC/DAC operations with SAMD21/SAMD51 divergence |
| 46 | +- **`cortex_handlers.c`** — ARM exception/interrupt vector table and handlers |
| 47 | +- **`USB/`** — Native Arduino USB stack (CDC, HID, PluggableUSB) |
| 48 | + |
| 49 | +### `variants/` — Board Pin Definitions (49 variants) |
| 50 | +Each variant has: |
| 51 | +- **`variant.h`** — Pin count, pin-to-peripheral mappings, LED pins, SERCOM assignments |
| 52 | +- **`variant.cpp`** — `g_APinDescription[]` array mapping Arduino pin numbers to physical port/pin/function/PWM/ADC |
| 53 | +- **`linker_scripts/`** — Memory layout (`flash_with_bootloader.ld` is the typical one) |
| 54 | + |
| 55 | +When adding a new board: create a variant directory, add entries to `boards.txt`, and provide a bootloader binary. |
| 56 | + |
| 57 | +### `libraries/` — Bundled Libraries |
| 58 | +- **SPI, Wire** — SERCOM-based, support multiple instances via different SERCOM peripherals |
| 59 | +- **Adafruit_TinyUSB_Arduino** — Alternative USB stack (submodule), supports mass storage, HID, MIDI, etc. |
| 60 | +- **Adafruit_ZeroDMA** — DMA controller library (submodule) |
| 61 | +- **Servo, I2S, HID, SDU** — Standard peripheral libraries |
| 62 | + |
| 63 | +### `bootloaders/` — Prebuilt UF2 Bootloader Binaries |
| 64 | +One binary per board. These enable USB drag-and-drop and bossac uploads. |
| 65 | + |
| 66 | +## Key Patterns |
| 67 | + |
| 68 | +- **SAMD21 vs SAMD51 divergence**: Most low-level code uses `#ifdef __SAMD51__` (or `_SAMD21_`) to handle register-level differences. The two chips have different clock trees, peripheral registers, and interrupt structures. |
| 69 | +- **SERCOM multiplexing**: Pin functions are routed through SERCOM peripherals. A board's variant files define which SERCOMs are assigned to SPI, I2C, and UART. |
| 70 | +- **Upload via bossac**: Default upload uses the SAM-BA bootloader protocol. Board enters bootloader mode via double-tap reset or 1200-baud touch. |
| 71 | +- **USB stack choice**: Boards can select between Arduino's native USB stack and TinyUSB via the `build.usbstack` menu option in `boards.txt`. |
| 72 | +- **Submodules**: `Adafruit_TinyUSB_Arduino` and `Adafruit_ZeroDMA` are git submodules — remember to `git submodule update --init` after cloning. |
0 commit comments