Skip to content

Commit 6337083

Browse files
committed
Merge branch 'Lorilandly-samd51-port'
2 parents 8f275db + 51a8dbd commit 6337083

20 files changed

Lines changed: 852 additions & 6 deletions

‎.github/workflows/build_arduino_examples_matrix.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ jobs:
8282
- atmega2560
8383
- atmega32u4
8484
- atmelsam
85+
- samd51
8586
- rpipico
8687
- rpipico2
8788

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
pre-1.2.8:
2+
- samd51: New platform port (Microchip SAMD51, Arduino SAMD51 core) (#317)
3+
- TCC PWM pulse driver: one TCC instance per stepper, hardware-timed pulse edges via buffered `PERBUF`/`CCBUF` registers
4+
- 16 MHz tick timebase from DFLL48M divided by 3 on a dedicated GCLK generator
5+
- ~4 ms ramp tick driven by a plain TC instance (TC3 default, compile-time override)
6+
- Workaround for silicon erratum DS80000748 (SYNCBUSY/buffer-latch race on abort)
7+
- Up to 3 steppers on SAMD51G, up to 5 on SAMD51J/N/P; SAMD21 not supported
8+
- ADR 0001/0002 document the port design decisions
29
- esp32 RMT: Ensure each filled RMT part ends with a pause symbol so DIR toggle cannot race the last step pulse (#370)
310

411
1.2.7:

‎README.md‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Arduino core 3.1.0 will support ESP-IDF V5.3.0 (based on RC1)
2121
## Overview
2222

2323
This is a high speed alternative for the [AccelStepper library](http://www.airspayce.com/mikem/arduino/AccelStepper/).
24-
Supported are avr (ATmega 168/328/P, ATmega2560, ATmega32u4), atmelsam due, esp32, esp32s2, esp32s3, esp32c3, esp32c6, esp32p4, Raspberry pi pico and pico2.
24+
Supported are avr (ATmega 168/328/P, ATmega2560, ATmega32u4), atmelsam due, Microchip SAMD51, esp32, esp32s2, esp32s3, esp32c3, esp32c6, esp32p4, Raspberry pi pico and pico2.
2525

2626
For memory footprint information across supported architectures, see [Memory Report](https://github.com/gin66/FastAccelStepper/blob/master/extras/doc/memory_report.md).
2727

@@ -36,6 +36,7 @@ The stepper motors should be connected via a driver IC (like A4988) with a 1, 2
3636
* I2S Direct mode: 1-3 additional steppers using I2S controllers directly (IDF ≥5.3)
3737
- pico: Any GPIO up to 31
3838
- atmel sam due: This can be one of each group of pins: 34/67/74/35, 17/36/72/37/42, 40/64/69/41, 9, 8/44, 7/45, 6
39+
- samd51: Any pin with a TCC waveform output in the board's variant table (checked at `stepperConnectToPin()`), one stepper per TCC instance
3940
- Step should be done on transition Low to High. High time will be only a few us.
4041
On esp32 the high time is for slow speed fixed to ~2ms and high speed to 50% duty cycle.
4142
For pico direction delay is recommended
@@ -55,7 +56,7 @@ FastAccelStepper offers the following features:
5556
* 1-pin operation for e.g. peristaltic pump => only positive move
5657
* 2-pin operation for e.g. axis control
5758
* 3-pin operation to reduce power dissipation of driver/stepper
58-
* Lower limit of 260s per step @ 16MHz aka one step every four minute (esp32/avr), 198s for sam due
59+
* Lower limit of 260s per step @ 16MHz aka one step every four minute (esp32/avr/samd51), 198s for sam due
5960
* fully interrupt/task driven - no periodic function to be called from application loop
6061
* supports acceleration and deceleration with per stepper max speed/acceleration
6162
* Allows the motor to continuously run in the current direction until stopMove() is called.
@@ -94,6 +95,7 @@ The `src/` directory is organized into the following subdirectories:
9495
| `pd_esp32/` | ESP32 pulse driver: RMT, MCPWM/PCNT, and I2S-based stepper control |
9596
| `pd_pico/` | RP2040/RP2350 pulse driver: PIO-based stepper control |
9697
| `pd_sam/` | SAM Due pulse driver: timer-based stepper control |
98+
| `pd_samd/` | SAMD51 pulse driver: TCC PWM-based stepper control |
9799
| `pd_test/` | Test platform pulse driver: PC-based testing simulation |
98100
| `fas_queue/` | Queue implementation: command queue management |
99101
| `fas_ramp/` | Ramp calculation: acceleration/deceleration curves |
@@ -276,6 +278,15 @@ This test application was AI crafted....
276278

277279
Tested with max two stepper motors with 50 kHz step rate by clazarowitz
278280

281+
### Microchip SAMD51
282+
283+
* one stepper per TCC instance: 3 steppers on SAMD51G, up to 5 on SAMD51J/N/P
284+
* step pin must have a TCC waveform output in the board's variant table; direction/enable can be any output pin
285+
* one overflow interrupt per step; pulse edges are hardware-timed, so step timing is exact regardless of interrupt latency
286+
* Steppers' command queue depth: 32
287+
* SAMD21 is not supported (compile error)
288+
* requires an Arduino SAMD51 core (uses the variant pin table); tested with the Adafruit SAMD core
289+
279290
## Usage
280291

281292
The library is in use with A4988, but other driver ICs should work, too.
@@ -492,6 +503,30 @@ This is supported by clazarowitz
492503

493504
**Note:** The SAM platform cannot be tested in CI. An audit against the SAM3X8E datasheet has identified [6 open issues](https://github.com/gin66/FastAccelStepper/blob/master/extras/doc/ai_improvements/05_sam_platform_audit.md) (4 critical, 2 minor), including invalid pin mappings and a wrong timer register value. Any changes to the SAM code must be cross-checked against that document.
494505

506+
### Microchip SAMD51
507+
508+
The step pin's TCC runs in normal PWM mode with buffered registers: `PERBUF` holds the next
509+
step interval and `CCBUF` the pulse width, so pulse edges are generated by hardware and are
510+
tick-exact regardless of interrupt latency. One overflow interrupt per step decrements the
511+
current queue entry and stages the next command at command boundaries — the same design as
512+
the AVR port, but with the timer's double buffering removing the edge-timing race.
513+
514+
The timebase is exactly 16 MHz (DFLL48M divided by 3) on a dedicated generic clock
515+
generator, matching the AVR/ESP32/Pico tick rate so the precomputed ramp constants apply.
516+
The ~4 ms ramp tick (`manageSteppers()`) runs from a plain TC instance. Defaults can be
517+
overridden at compile time if your project claims these peripherals:
518+
519+
```
520+
-DFAS_SAMD_GCLK_GEN=6 // generic clock generator for the 16 MHz timebase
521+
-DFAS_SAMD_RAMP_TC=3 // TC instance for the ramp tick (TC0 is used by tone())
522+
-DFAS_SAMD_PULSE_WIDTH_US=2 // step pulse high time
523+
```
524+
525+
The implementation contains a workaround for silicon erratum DS80000748 (2.20.1): clearing
526+
`STATUS.PERBUFV`/`CCBUFV` releases SYNCBUSY before the buffer registers are actually
527+
restored, so the flags are cleared twice with SYNCBUSY waits — otherwise a write to `PER`
528+
right after an aborted buffered update can wedge the synchronization bridge permanently.
529+
495530
### ALL
496531

497532
The used formula is just s = 1/2 * a * t² = v² / (2 a) with s = steps, a = acceleration, v = speed and t = time. In order to determine the speed for a given step, the calculation is v = sqrt(2 * a * s). The performed square root is an 8 bit table lookup using log2/pow2. Sufficient exact for this purpose.
@@ -656,4 +691,5 @@ As mentioned by kthod861 in [Issue #110](https://github.com/gin66/FastAccelStepp
656691
- Thanks Magnus for pico sdk support and code improvement hints (#334)
657692
- Thanks Sam W for pull request (https://github.com/gin66/FastAccelStepper/pull/351) samx3-due: Fix null result from `tryAllocateQueue()`
658693
- Thanks drayde for pull request (https://github.com/gin66/FastAccelStepper/pull/366) Fix ESP-IDF 6 RMT header include
694+
- Thanks Loriland for the Microchip SAMD51 port (https://github.com/gin66/FastAccelStepper/issues/317)
659695

‎examples/RawAccessTest/RawAccessTest.ino‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const uint8_t dirPinStepper = 18;
2222
const uint8_t stepPinStepper = 1; // only defined to satisfy compiler
2323
const uint8_t enablePinStepper = 2;
2424
const uint8_t dirPinStepper = 3;
25+
#elif defined(ARDUINO_ARCH_SAMD)
26+
const uint8_t stepPinStepper = 10; // TCC0 output on Feather M4
27+
const uint8_t enablePinStepper = 21;
28+
const uint8_t dirPinStepper = 23;
2529
#elif defined(PICO_RP2040) || defined(PICO_RP2350)
2630
const uint8_t stepPinStepper = 15; // only defined to satisfy compiler
2731
const uint8_t enablePinStepper = 13;

‎examples/StepperDemo/StepperDemo.ino‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#endif
2626
#elif defined(ARDUINO_ARCH_SAM)
2727
#include "StepperPins_sam.h"
28+
#elif defined(ARDUINO_ARCH_SAMD)
29+
#include "StepperPins_samd.h"
2830
#elif defined(PICO_RP2040) || defined(PICO_RP2350)
2931
#include "StepperPins_pico.h"
3032
#endif
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#ifndef STEPPER_PINS_SAMD_H
2+
#define STEPPER_PINS_SAMD_H
3+
4+
#include "StepperConfig.h"
5+
6+
// Hardware configuration for the Adafruit Feather M4 (SAMD51J19A).
7+
// Step pins need a TCC waveform output in the variant pin table; the
8+
// Feather M4 breaks out TCC0 (pins 0/1/10/11/12/13) and TCC1 (pins 5/6/9).
9+
// Only the first two entries below connect; the remaining ones are placeholders
10+
// for boards that expose more TCC outputs (stepperConnectToPin() returns NULL).
11+
const uint8_t led_pin = PIN_LED_13;
12+
const struct stepper_config_s stepper_config[MAX_STEPPER] = {
13+
{
14+
step : 10, // TCC0/WO[0]
15+
enable_low_active : 21,
16+
enable_high_active : PIN_UNDEFINED,
17+
direction : 23,
18+
dir_change_delay : 0,
19+
direction_high_count_up : true,
20+
auto_enable : true,
21+
on_delay_us : 50,
22+
off_delay_ms : 1000
23+
},
24+
{
25+
step : 5, // TCC1/WO[0]
26+
enable_low_active : 22,
27+
enable_high_active : PIN_UNDEFINED,
28+
direction : 24,
29+
dir_change_delay : 0,
30+
direction_high_count_up : true,
31+
auto_enable : true,
32+
on_delay_us : 500,
33+
off_delay_ms : 1000
34+
},
35+
{
36+
step : 6,
37+
enable_low_active : PIN_UNDEFINED,
38+
enable_high_active : PIN_UNDEFINED,
39+
direction : 12,
40+
dir_change_delay : 0,
41+
direction_high_count_up : true,
42+
auto_enable : false,
43+
on_delay_us : 0,
44+
off_delay_ms : 0
45+
},
46+
{
47+
step : 9,
48+
enable_low_active : PIN_UNDEFINED,
49+
enable_high_active : PIN_UNDEFINED,
50+
direction : 11,
51+
dir_change_delay : 0,
52+
direction_high_count_up : true,
53+
auto_enable : false,
54+
on_delay_us : 0,
55+
off_delay_ms : 0
56+
},
57+
{
58+
step : 4,
59+
enable_low_active : PIN_UNDEFINED,
60+
enable_high_active : PIN_UNDEFINED,
61+
direction : 23,
62+
dir_change_delay : 0,
63+
direction_high_count_up : true,
64+
auto_enable : false,
65+
on_delay_us : 0,
66+
off_delay_ms : 0
67+
}};
68+
69+
#endif

‎examples/StepperDemo/generic.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#if defined(ARDUINO_ARCH_AVR)
99
#define get_char(x) pgm_read_byte(x)
1010
#define MSG_TYPE PGM_P
11-
#elif defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAM)
11+
#elif defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAM) || \
12+
defined(ARDUINO_ARCH_SAMD)
1213
#define get_char(x) *x
1314
#define MSG_TYPE const char*
1415
#elif defined(ESP_PLATFORM)

‎extras/ci/build_matrix.yaml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ templates:
147147
build_flags: ["-Wall", "-Wno-deprecated-declarations"]
148148
lib_extra_dirs: .
149149

150+
samd51:
151+
platform: atmelsam
152+
board: adafruit_feather_m4
153+
framework: arduino
154+
build_flags: ["-Wall", "-Wno-deprecated-declarations"]
155+
lib_extra_dirs: .
156+
150157
rp2040:
151158
platform: https://github.com/maxgerhardt/platform-raspberrypi.git
152159
framework: arduino
@@ -224,6 +231,9 @@ environments:
224231
atmelsam:
225232
template: sam
226233

234+
samd51:
235+
template: samd51
236+
227237
pico:
228238
template: rp2040
229239
board: pico
@@ -391,6 +401,7 @@ workflows:
391401
- atmega2560
392402
- atmega32u4
393403
- atmelsam
404+
- samd51
394405
- rpipico
395406
- rpipico2
396407
script: build-platformio.sh

‎extras/ci/platformio.ini‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ framework = arduino
108108
build_flags = -Wall -Wno-deprecated-declarations
109109
lib_extra_dirs = .
110110

111+
[env:samd51]
112+
platform = atmelsam
113+
board = adafruit_feather_m4
114+
framework = arduino
115+
build_flags = -Wall -Wno-deprecated-declarations
116+
lib_extra_dirs = .
117+
111118
[env:pico]
112119
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
113120
board = pico
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SAMD51 pulse driver: TCC normal PWM with one overflow interrupt per step
2+
3+
The SAMD51 port generates step pulses with the step pin's TCC in normal PWM (buffered
4+
`PERBUF` = next step interval, `CCBUF` = pulse width), taking one overflow interrupt per
5+
step that decrements the current queue entry and, at command boundaries, stages the next
6+
entry's interval and direction. Pulse-edge timing is therefore hardware-exact regardless of
7+
interrupt latency; the ISR only has to finish within one step interval, giving ~50 µs of
8+
slack at typical speeds and a ~200 ksteps/s ceiling — far above stepper physics.
9+
10+
## Considered options
11+
12+
- **TCC free-running + Event System into a TC event counter (one interrupt per command,
13+
ESP32 MCPWM/PCNT-style).** Zero per-step CPU load, but the command-boundary overrun race
14+
(counter reaches N while the TCC is already emitting the next pulse at the old period)
15+
is the hardest part of the ESP32 port and would be re-invented on new silicon. Costs a
16+
TC + EVSYS channel per stepper. Rejected for v1; the peripheral setup is a superset of
17+
the chosen design, so it remains an upgrade path if multi-stepper high-rate support is
18+
ever needed.
19+
- **DMA-fed per-step period updates (RMT-style).** Requires expanding queue commands into
20+
per-step period buffers — an extra layer no other port needs — to eliminate ISR load
21+
that is already negligible (~0.2% CPU for one motor). Rejected.
22+
- **The SAM Due port's design (pin-change interrupt per step + PWM peripheral).** Known
23+
problematic upstream (audit lists 4 critical issues; needs sprinkled busy-waits).
24+
Explicitly not used as the template; the AVR port is.

0 commit comments

Comments
 (0)