gin66 / FastAccelStepper

A high speed stepper library for Atmega 168/328p (nano), Atmega32u4, Atmega 2560, ESP32, ESP32S2, ESP32S3, ESP32C3, ESP32C6 and Atmel SAM Due
MIT License
301 stars 69 forks source link

Seeeduino XIAO (SAMD21G18 Cortex-M0+) #78

Open courtens opened 3 years ago

courtens commented 3 years ago

Is this library compatible with my Seeeduino XIAO? It is a SAMD21G18 Cortex-M0+ chip.

This is what I get trying to run UsageExample.ino:

WARNING: library FastAccelStepper claims to run on avr, esp32 architecture(s) and may be incompatible with your current board which runs on samd architecture(s).
In file included from c:\Users\Nathaniel\Documents\Arduino\libraries\FastAccelStepper\src/FastAccelStepper.h:10:0,
                 from c:\Users\Nathaniel\Documents\Arduino\FilmFix\FilmFix.ino:52:
c:\users\nathaniel\documents\arduino\libraries\fastaccelstepper\tests\pc_based\stubs.h:5:0: warning: "pgm_read_byte_near" redefined
 #define pgm_read_byte_near(x) (*(x))

In file included from C:\Users\Nathaniel\AppData\Local\Arduino15\packages\Seeeduino\hardware\samd\1.8.1\cores\arduino/Arduino.h:36:0,
                 from C:\Users\NATHAN~1\AppData\Local\Temp\arduino-sketch-4B354A760F649884E833B790D5F4E104\sketch\FilmFix.ino.cpp:1:
C:\Users\Nathaniel\AppData\Local\Arduino15\packages\Seeeduino\hardware\samd\1.8.1\cores\arduino/avr/pgmspace.h:110:0: note: this is the location of the previous definition
 #define pgm_read_byte_near(addr) pgm_read_byte(addr)

In file included from c:\Users\Nathaniel\Documents\Arduino\libraries\FastAccelStepper\src/FastAccelStepper.h:10:0,
                 from c:\Users\Nathaniel\Documents\Arduino\FilmFix\FilmFix.ino:52:
c:\users\nathaniel\documents\arduino\libraries\fastaccelstepper\tests\pc_based\stubs.h:19:0: warning: "abs" redefined
 #define abs(x) ((x) > 0 ? (x) : -(x))

In file included from C:\Users\NATHAN~1\AppData\Local\Temp\arduino-sketch-4B354A760F649884E833B790D5F4E104\sketch\FilmFix.ino.cpp:1:0:
C:\Users\Nathaniel\AppData\Local\Arduino15\packages\Seeeduino\hardware\samd\1.8.1\cores\arduino/Arduino.h:135:0: note: this is the location of the previous definition
 #define abs(x) ((x)>0?(x):-(x))

C:\Users\NATHAN~1\AppData\Local\Temp\arduino-sketch-4B354A760F649884E833B790D5F4E104\libraries\FastAccelStepper\FastAccelStepper.cpp.o: In function `FastAccelStepperEngine::stepperConnectToPin(unsigned char)':
c:\Users\Nathaniel\Documents\Arduino\libraries\FastAccelStepper\src/FastAccelStepper.cpp:82: undefined reference to `StepperQueue::isValidStepPin(unsigned char)'
c:\Users\Nathaniel\Documents\Arduino\libraries\FastAccelStepper\src/FastAccelStepper.cpp:116: undefined reference to `StepperQueue::queueNumForStepPin(unsigned char)'
C:\Users\NATHAN~1\AppData\Local\Temp\arduino-sketch-4B354A760F649884E833B790D5F4E104\libraries\FastAccelStepper\FastAccelStepper.cpp.o: In function `FastAccelStepper::setDirectionPin(unsigned char, bool)':
c:\Users\Nathaniel\Documents\Arduino\libraries\FastAccelStepper\src/FastAccelStepper.cpp:476: undefined reference to `fas_queue'
C:\Users\NATHAN~1\AppData\Local\Temp\arduino-sketch-4B354A760F649884E833B790D5F4E104\libraries\FastAccelStepper\FastAccelStepper.cpp.o: In function `FastAccelStepper::move(long)':
c:\Users\Nathaniel\Documents\Arduino\libraries\FastAccelStepper\src/FastAccelStepper.cpp:545: undefined reference to `fas_queue'
C:\Users\NATHAN~1\AppData\Local\Temp\arduino-sketch-4B354A760F649884E833B790D5F4E104\libraries\FastAccelStepper\RampGenerator.cpp.o: In function `RampGenerator::_startMove(long, long)':
c:\Users\Nathaniel\Documents\Arduino\libraries\FastAccelStepper\src/RampGenerator.cpp:139: undefined reference to `noInterrupts()'
c:\Users\Nathaniel\Documents\Arduino\libraries\FastAccelStepper\src/RampGenerator.cpp:147: undefined reference to `interrupts()'
collect2.exe: error: ld returned 1 exit status
Compilation error: Error: 2 UNKNOWN: exit status 1
courtens commented 3 years ago

I replaced defined(ARDUINO_ARCH_AVR) with defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAMD).

Now I am getting:

error: 'MAX_STEPPER' was not declared in this scope
   FastAccelStepper* _stepper[MAX_STEPPER];

MAX_STEPPER is defined in FastAccelStepper\src\RampGenerator.h as:

#if defined(TEST)
#define MAX_STEPPER 2
#define TICKS_PER_S 16000000L
#elif defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAMD) 
#if defined(__AVR_ATmega328P__)
#define MAX_STEPPER 2
#elif defined(__AVR_ATmega2560__)
#define MAX_STEPPER 3
#endif
#define TICKS_PER_S F_CPU
#elif defined(ARDUINO_ARCH_ESP32)
#define MAX_STEPPER 6
#define TICKS_PER_S 16000000L
#else
#define MAX_STEPPER 6
#define TICKS_PER_S 16000000L
#endif

so I also I replaced defined(__AVR_ATmega2560__) with defined(__AVR_ATmega2560__) || defined(ARDUINO_ARCH_SAMD). and also tried defined(__AVR_ATmega328P__) with defined(__AVR_ATmega328P__) || defined(ARDUINO_ARCH_SAMD) instead --- but got stuck. I don't know enough about Timers.

The Seeeduino runs at 48MHz

gin66 commented 3 years ago

From the datasheet the Seeduino Xiao could drive three steppers:

Three 24-bit Timer/Counters for Control (TCC), with extended functions: • Up to four compare channels with optional complementary output • Generation of synchronized pulse width modulation (PWM) pattern across port pins

But it is quite an effort to implement. If there are a couple of comments wanting this chip to be supported, then I will look into it. So I keep it open, for others to register their wish. A patch (MIT licensed) would be welcomed, too

courtens commented 3 years ago

Thank you -- very much appreciated!

I would need to drive just one stepper (... if that helps.)

I am still new to the github concept and I am not sure what this would entail: "A patch (MIT licensed) would be welcomed, too"; I plan on using it privately, and found FlexyStepper to do the job for now. It would be nice if I could run it faster so I can use 1/8 or 1/16 steps.

clazarowitz commented 3 years ago

You are in so much luck....I'm not sure what the seeduino xiao is, (duh, its in your title) but I'm still not sure how much it is like the DUE/SAM8x3e. Might have to change the ifdefs a bit to exclude certain ARDUINO_ARCH_SAM boards..., but I'm pretty close to forking/committing/pull-requesting Due support. You're addition of ARDUINO_ARCH_SAM makes me wonder.

I didn't look too closely at the full line. My code is using the PWM generator peripheral, not synchronous, to generate up to 8 channels...but not all pins are easily accessible, and some share functions with other pins you're likely to want. I arbitrarily capped it at 6. gin66 is correct, this was a non-trivial effort. It probably would have been easier for him, and I suspect he will need to "fix" some of my code (or tell me what to fix).

I chose NOT to use the timers because I need them to read servo PWM signals. It looks like those timers have a rise time capture, as well as a fall time capture, so no ISR delay on pulses that are synchronous and end within a few microseconds of one another. I'm working pretty hard to get this committed tonight....as evident by the fact I just registered a github account (I havent had need/want to contribute/collaborate until very recently). Anyhow, off to fork...

clazarowitz commented 3 years ago

You can give my fork a shot and see if it works...I didn't look at the datasheet for the SAMD21G18, so I honestly have no idea. https://github.com/clazarowitz/FastAccelStepper

clazarowitz commented 3 years ago

So, I did just have a look at the data sheet for the SAMD21G18. Sorry, my fork will definatly not work. The M0 and M3 from atmel are just too different. Looks like my #defines will need to be updated. ARDUINO_ARCH_SAM is far too broad.