Skip to content

Add SAMD21 support (example configs, SERCOM console and NVMCTRL fixes) - #960

Merged
mattia-moffa merged 4 commits into
wolfSSL:masterfrom
dgarske:samd21
Nov 19, 2026
Merged

mattia-moffa merged 4 commits into
wolfSSL:masterfrom
dgarske:samd21

Conversation

@dgarske

@dgarske dgarske commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Description

hal/samr21.c is a SAM D21 HAL, clocked for a SAMD21G18A at 48MHz, but nothing could reach it: no example config, no docs, no CI build.

Adds config/examples/samd21.config and samd21_j18.config for the 128KB/16KB and 256KB/32KB parts, a SERCOM console, a docs/Targets.md section, CI jobs, and wolfBoot_success() in the test app so an A/B update confirms instead of rolling back. WOLFBOOT_RAM_SIZE is now a linker substitution, as the script hardcoded 16KB and capped the 32KB parts.

Both layouts use a 32KB bootloader region, because the NVMCTRL BOOTPROT fuse protects 8KB, 16KB or 32KB and nothing in between: any other boundary either leaves the bootloader writable or pushes the protected region into the boot partition, where it would block the swap.

HAL fixes:

  • hal_init() and hal_prepare_boot() cleared APBBMASK.NVMCTRL, set out of reset, gating the flash controller's clock off before any write or erase.
  • PAC_WP_NVMCTL was the DSU bit; NVMCTRL is peripheral 34 vs PAC1's base 32, so bit 2.
  • hal_flash_write() issued one page-write per transfer; now sets CTRLB.MANW and commits each page.
  • Write and erase always returned success; they now report NVMCTRL.STATUS errors.

Testing

Not validated on hardware; the console pinout is from the Xplained Pro schematic. Both configs build, and unit-samr21-erase-advance and unit-flash-write-samr21 cover the erase stride, a multi-page write and the error returns.

@dgarske dgarske self-assigned this Oct 28, 2026
@dgarske
dgarske force-pushed the samd21 branch 3 times, most recently from 6cc2361 to 5f280ce Compare October 29, 2026 04:39
@dgarske dgarske changed the title Add SAMD21 support (example configs, SERCOM console and NVMCTRL fixes) Add SAMD21 support, and fix hybrid LMS/XMSS verification Oct 29, 2026
@dgarske dgarske changed the title Add SAMD21 support, and fix hybrid LMS/XMSS verification Add SAMD21 support and fix hybrid LMS/XMSS verification Oct 29, 2026
@dgarske
dgarske force-pushed the samd21 branch 2 times, most recently from 8771890 to 7b02878 Compare October 29, 2026 05:19
@dgarske dgarske changed the title Add SAMD21 support and fix hybrid LMS/XMSS verification Add SAMD21 support (example configs, SERCOM console and NVMCTRL fixes) Oct 29, 2026
@dgarske dgarske assigned mattia-moffa and wolfSSL-Bot and unassigned dgarske Nov 18, 2026
@dgarske
dgarske requested a review from mattia-moffa November 18, 2026 04:49
Comment thread hal/samr21.c Resolved
Comment thread hal/samr21.c Resolved
mattia-moffa
mattia-moffa previously approved these changes Nov 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The UART implementation adds a second parameter to uart_rx, conflicting with the public one-argument declaration and its caller, creating incompatible function types across translation units.

1 open issue
What changed in this PR

This PR makes the existing SAM D21 / SAM R21 HAL usable through example configurations, console support, and build integration. It also improves flash programming and protection handling. Hardware validation remains outstanding.

Changes:

  • Add configurations and CI builds for 128KB and 256KB SAMD21 parts, with configurable linker RAM size.
  • Add a SERCOM console and firmware confirmation in the test application.
  • Fix NVMCTRL clocking, page commits, error reporting, and protection handling, with expanded host tests and documentation.
File Description
tools/​unit-tests/​unit-samr21-flash-protect.c Test lock-region calculations, PAC handling, and errors.
tools/​unit-tests/​unit-flash-write-samr21.c Add multi-page data-path and controller-error tests.
tools/​unit-tests/​Makefile Register protection tests and extend HAL extraction rules.
tools/​config.mk Preserve WOLFBOOT_RAM_SIZE during configuration generation.
test-app/​app_samr21.c Add LED status and confirm updated firmware.
Makefile Substitute RAM size into linker scripts.
hal/​uart/​uart_drv_samr21.c Add a configurable SERCOM USART driver.
hal/​samr21.ld Replace fixed RAM length with a configuration value.
hal/​samr21.c Correct flash operations and add region protection.
docs/​Targets.md Document configurations, memory layouts, console, and validation status.
config/​examples/​samd21.config Add a 128KB flash / 16KB SRAM configuration.
config/​examples/​samd21_j18.config Add a 256KB flash / 32KB SRAM configuration.
arch.mk Default SAMR21-target RAM size to 16KB.
.gitignore Ignore generated protection-test files and executable.
.github/​workflows/​test-configs.yml Build both SAMD21 configurations in CI.

Review effort: Balanced

Comment thread hal/uart/uart_drv_samr21.c Resolved
@dgarske
dgarske requested a review from mattia-moffa November 19, 2026 16:41
@mattia-moffa
mattia-moffa merged commit 0625155 into wolfSSL:master Nov 19, 2026
558 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants