This file provides guidance to AI coding agents when working with code in this repository.
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.
This is an Arduino BSP — there is no standalone build. Code is compiled via Arduino CLI.
Default/preferred boards: Use metro_m0 (SAMD21/M0) and metro_m4 (SAMD51/M4) as the reference boards for development and testing.
# Install Arduino CLI and set up BSP (CI does this automatically)
# Build all examples for a specific board:
python3 tools/build_all.py adafruit_metro_m0 # SAMD21/M0
python3 tools/build_all.py adafruit_metro_m4 # SAMD51/M4
# FQBN format: adafruit:samd:adafruit_<board_id>
# Build a single sketch:
arduino-cli compile --fqbn adafruit:samd:adafruit_metro_m0 path/to/sketch # SAMD21
arduino-cli compile --fqbn adafruit:samd:adafruit_metro_m4 path/to/sketch # SAMD51
# Upload:
arduino-cli upload --fqbn adafruit:samd:adafruit_metro_m0 -p /dev/ttyACM0 path/to/sketch
arduino-cli upload --fqbn adafruit:samd:adafruit_metro_m4 -p /dev/ttyACM0 path/to/sketchCI 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.
platform.txt— Defines compiler toolchain (arm-none-eabi-gcc), flags, upload tools (bossac, openocd), and build recipes. This is the central build configuration.boards.txt— Board definitions (36+ boards): MCU type, clock speed, memory layout, USB VID/PID, upload protocol, menu options (optimization level, USB stack selection).programmers.txt— J-Link and Atmel-ICE debugger configurations.
The Arduino API implementation for SAMD. Key subsystems:
startup.c— Clock initialization, oscillator config, peripheral clocks (differs significantly between SAMD21 and SAMD51 via#ifdef __SAMD51__)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.wiring_analog.c— ADC/DAC operations with SAMD21/SAMD51 divergencecortex_handlers.c— ARM exception/interrupt vector table and handlersUSB/— Native Arduino USB stack (CDC, HID, PluggableUSB)
Each variant has:
variant.h— Pin count, pin-to-peripheral mappings, LED pins, SERCOM assignmentsvariant.cpp—g_APinDescription[]array mapping Arduino pin numbers to physical port/pin/function/PWM/ADClinker_scripts/— Memory layout (flash_with_bootloader.ldis the typical one)
When adding a new board: create a variant directory, add entries to boards.txt, and provide a bootloader binary.
- SPI, Wire — SERCOM-based, support multiple instances via different SERCOM peripherals
- Adafruit_TinyUSB_Arduino — Alternative USB stack (submodule), supports mass storage, HID, MIDI, etc.
- Adafruit_ZeroDMA — DMA controller library (submodule)
- Servo, I2S, HID, SDU — Standard peripheral libraries
One binary per board. These enable USB drag-and-drop and bossac uploads.
- 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. - SERCOM multiplexing: Pin functions are routed through SERCOM peripherals. A board's variant files define which SERCOMs are assigned to SPI, I2C, and UART.
- Upload via bossac: Default upload uses the SAM-BA bootloader protocol. Board enters bootloader mode via double-tap reset or 1200-baud touch.
- USB stack choice: Boards can select between Arduino's native USB stack and TinyUSB via the
build.usbstackmenu option inboards.txt. - Submodules:
Adafruit_TinyUSB_ArduinoandAdafruit_ZeroDMAare git submodules — remember togit submodule update --initafter cloning.