You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible to port to the nRF52840, at least for SPI LED types?
Although the nRF52xxx is similar to the nRF51xxx (which is supported), if I understand correctly, portions of the nRF52 core run at high interrupt level (for BLE), and they cannot be disabled by user application code. Obviously, this could impact any code depending on tight execution timing. Based on this, would some LED control types need to be explicitly excluded (e.g., NeoPixel)?
Take a look at my fork here. I have tested with the nrf52832 and it should work exactly the same for the nrf52840. I have only tested APA102 but I'd guess other SPI chipsets should work.
After lots of tinkering, compiling, and adding compilation asserts / pragma messages, I've started to understand some aspects of this port. I think it will be possible to port this, which is great. Even better, we may be able rely upon the availability of Nordic's nrfx repo (part of at least Adafruit's BSP for Feather Express 52840. Work now underway....
Sorry for the late reply, I did not see a notification that anyone had responded. Q1a. I don't think setting MISO to pin 1 was intentional. It probably should have been set to 0xFFFFFFFF. As far as I can tell from the documentation it is perfectly fine to not specify a pin for MISO if you aren't using it.
Q1b. Agreed. I think I might have explicitly told PSEL.CSN to be 0xFFFFFFFF at some point but probably removed it for some unknown reason.
Q2. The comment is wrong. I took code from the NRF51 and Teensy platforms and it is likely something that originated elsewhere.
What features of the nrfx library are you thinking will be useful here?
I have not had much luck running this on my nrf52840. I have not been able to get the Adafruit BSP to play nicely with the Particle Xenon board I have, which is mostly used with the Nordic SDK anyway, so all of my testing has been with my feather52832.
Thanks for answers, it helps me sanity-check my understanding, and keep my sanity. :)
I'll update to indicate MISO is not needed, and explicitly set CSN for the 52840 + SPIM3 only (no other board/SPIM appears to support this).
Why use nrfx?
a hardware-abstraction layer (HAL), useful for switching between SPIM instances
same HAL allows common code for nRF51 and nRF52 (and other) boards
built-in workarounds for chip errata, owned and maintained by Nordic (i.e., less really migraine-inducing, head-scratching hardware debugging)
I've already gotten the simplest of items showing on a dotstar strip (RGB ordering verification, simple chaser). I'll push a branch "real soon now".
Do you have any thoughts on how to test/validate the nRF52840 support is "complete"?
Also, what specific problems or types of problems have you experienced with the Particle Xenon?
@henrygab I'm interested to see what improvements you've made to my hatchet job of a port. I honestly got it to a point of "hey pretty lights" one afternoon and did not explore it much further than that. I did try to understand how to get neopixels working and ran out of patience.
In regards to nrfx, I wonder if that isn't going to add a bunch of complexity that many users won't want to deal with, especially in Arduino land. Might make the possibility of porting to the Nordic SDK a little simpler though.
As far as validating that support is complete, I'd imagine that'll take some community testing with various led chipsets. This is probably something @focalintent should comment on.
For some reason Particle Xenon isn't playing nicely with the nordic QSPI libraries in the Adafruit BSP. I've tried removing everything I could find having to do with the QSPI memory and still have problems with most sketches compiling. Since I got the Xenon primarily for working with the Nordic SDK anyway I have not put too much time towards figuring out why I am having the problems I am. Other than that the Xenon is a great dev board and they were a steal at $9 during pre-order.
No it shouldn’t - that’s used for an optimization in some places where I precompute the full value for the port so that setting the pin levels just becomes a single write vs a read, or/and, then write. (1 memory access in one instruction vs 2 memory accesses and 3 instructions (or, in the case of combining clock/data pin setting, 4 memory accesses and 6 instructions))
@pcaddict -- found one bug that may affect use of certain boards / pins, and thus might fix your Xenon issue. I just need to confirm it compiles, then I will push the first major fix. ETA: a couple days, maybe sooner if the Arduino IDE plays nice.
Ok. Frustrated with Arduino. Getting closer, but extending ETA. Don't want to push stuff that doesn't work. Wish there were better documentation on the implied contracts that the fastpin / fastspi functions must follow to work properly with FastLED….
@pcaddict -- Found the following note in the nRF52840 product specification, at the end of the SPIM chapter:
[32] High bit rates may require GPIOs to be set as High Drive, see GPIO chapter for more details.
Based on the above, could your problems with data corruption when trying to use >4MHz be solved by simply reconfiguring the pins to high drive mode for both 0 and 1 (H0H1)?
If you have something that works at 4MHz, try using the above instead of the following lines in Init() in fastspi_arm_nrf52.h:
For the nRF52840 at least, this would be something such as:
//// pins must be set to input/output before enabling the SPIM instance//FastPin<_DATA_PIN>::setOutput();//FastPin<_CLOCK_PIN>::setOutput();// set drive mode to H0H1PIN_CNF[_DATA_PIN ] = ( PIN_CNF[_DATA_PIN ] & (~0x00030000u) ) | 0x3;
PIN_CNF[_CLOCK_PIN] = ( PIN_CNF[_CLOCK_PIN] & (~0x00030000u) ) | 0x3;
If that solves your high-speed clocking for SPI, I'll be glad.
Getting closer... The nRF52840 has more than 32 pins, and thus has two ports. This requires the _ARMPIN template to be … slightly more complex. Throw in GCC's / C++'s restrictions on template parameter types, and it's just a matter of finding an expression that doesn't cause compile-time errors....
@pcaddict - g_ADigitalPinMap[pin] is not a constexpr, and is not resolved at compile-time. Therefore, it creates some limitations for use as a template parameter.
a non-type, non-template template-parameter must be a pointer to
a constexpr that designates the address of an object with
static storage duration AND external or internal linkage --OR--
a function with external or internal linkage.
So, the template receives a PIN, which is the Arduino definition and does NOT match the underlying chip PORT/PIN. Thus, the template must have a static _init() function that converts the passed-in PIN into the PORT/PIN that actually matters. For nRF52840, also need to save pointer to the (NRF_GPIO_Type*) for the PORT, because there are two distinct ports (yes, 48 pins... so either NRF_P0 or NRF_P1 is used).
Or at least, this is my working theory at this point. Loading the .elf in Ozone, my calls to FastPin<PIN_LED1>::toggle() is being optimized away, even with -O0, so I'm working through figuring that out at the moment....
In that case, I'd take a PR to a branch - and then pull the nrf52 over once I get a chance to actually get the clockless support in. Given that the clockless chipsets are by far more widely used, it'd be kind of important to get it in before pulling the platform in to master.
Do I generate the branch in your depot as part of the PR, or do you set it up? (either way, may I suggest 'nrf52' as the branch?)
Although I've added pin definitions for a large set of nrf52-based boards, I only have one of those to test with. Do you want the PR to have the following line for the untested boards? #error "Untested board -- remove this line, test, and submit results via https://github.com/FastLED/FastLED/issues"
Is there a "how to test your port" documentation anywhere? If not, do you have recommendations of various levels or types of testing for any given board port?
With that, I think I'm ready to provide at least the FastPIN implementation for initial code review.
I think you can target a new branch in the PR - if not I should be able to edit it
Use #warning instead of #error (#error will break/stop compilation)
The testing of a port involves running it against a variety of led types to make sure all the variations on things work : ) (my workshop has a lot of test strips for when I'm actively working on testing a board/port - I'll also rig stuff up on breadboards so I can rapidly try testing across the full set of ports, to be sure - which is a habit I got into after the Arduino mega - which had different cycle times for writing different pins!)
I have validated most of the individual ports work on the nRF52840, connected to individual pins. I ran the code under the Ozone debugger with a J-Link EDU. This helped me realize how terrible inlining all code is for debugging.
I have validated basic functionality for 16-count APA102, using software SPI.
However, I couldn't create a new target branch with my PR, so I created against master.
@focalintent OK, there's now a new pull request, with both FastPIN and FastSPI implemented. If someone pre-builds the buffers they want to use (e.g., more likely for POV displays), the FastSPI implementation supports a single DMA-enabled SPI transaction.
@henrygab I do not have time to dig much into clockless support but if you take a look in the examples of the latest Nordic Thread and Zigbee SDK they have added a driver for WS2812 LEDs that might get you pointed in the right direction.
The sample code is not directly relevant to clockless.
The sample sets a PWM output to the right clock rate, pre-generates the full LED string's output, and then just starts a NRFX-based playback of that pre-generated output.
Positives:
Very simple to comprehend
Low CPU overhead while updating the LED strings
Easy to adapt to re-use the same LED update buffer (e.g., static or limited set of static outputs, such as used for POV)
Easy to adapt to allow single PWM to control multiple LED strings with single PWM instance, so long as the protocol clock rate is the same
Negatives:
Very RAM-hungry... 48 bytes extra RAM per pixel(!) … Is this reasonable for FastLED?
In contrast, the FastLED clockless implementations I've looked at appear to translate the RGB data 'on the fly' / 'just in time' to adjust the GPIO pins, and thus avoid this extra RAM overhead. In exchange, these methods re-compute the output for each LED string every time show() is called, even when no data changes for that string.
While I could code something like this up, I am concerned [the 48-bytes per pixel overhead] doesn't fit the "flavor" of FastLED.
I'm also presuming async callbacks are supported in the Arduino environment (haven't used them before). Worst, the main benefit (re-use of the buffer for refresh) is not usable with the FastLED architecture.
@focalintent, will you accept a PR with clockless implemented, even if the it uses more RAM than you're used to?
I believe I can design it to have an upper-bound memory overhead of about <2.5k RAM, even with BLE enabled.
Click on each item below to expand details....
Why reliable clockless is hard on nRF52
The `softdevice` can cause any in-progress clockless output to fail, if it relies on CPU. The interrupt processing time is on the order of hundreds of microseconds, while the clockless protocols can fail with deviations as small as 50ns.
Radio (BLE, ANT, Zigbee, etc.) is software-defined (called `soft device`)
Interrupts for the softdevice cannot be disabled
Softdevice may hard-lock if app uses interrupt priorities of 0 or 1
BLE specifically interferes with timing of clockless, due to both long latency from the BLE interrupt processing and potentially rapid timing between instances of those interrupts.
Note: Freq is the minimum time between such interrupts occurring.
These timings only cover softdevice processing that occurs at interrupt priority levels 0 and 1 … the delays at priority level 4 (deferred BLE processing) can substantially increase these values.
PWM peripheral based prototype
I have a working prototype. It's not templated, it uses the PWM controller, and it takes 48 bytes of RAM per pixel. The output clocking is rock-solid, once it starts. The PWM sequence buffer is compile-time allocated based on the length of the longest LED string, but only one such buffer exists, regardless of the number of output ports.
(e.g. 144 LEDs would take just under 7k RAM for PWM sequence data)
Memory Usage vs. Timing
Starting with the following:
SoftDevice may pre-empt any application code for 400μs
A single bit from a 800KHz protocol ~= 1.25μs
400μs / 1.25μs == up to 320 protocol bits sent during a SoftDevice interrupt
PWM peripheral uses two bytes per protocol bit
14 pixels is 336 protocol bits (672 bytes for PWM peripheral)
The PWM peripheral can chain / loop up to two sequences, and it must be setup
so that it will have enough data if pre-empted at any time to be able to push
out another 14 pixels of data, AND be able to give the peripheral additional data
before it's fully drained.
Since pre-emption can happen with zero bits left in the current sequence,
each sequence must have at least 14 pixels of data. A three-buffer approach,
each having ~16 pixels of data (768 bytes per sequence buffer, 2304 bytes total)
would likely be possible without loss of reliability in all but extreme cases, and
even there would be detectable.
Anyways, please let me know if this PR (high-RAM but reliable) would be of interest.
I get the following error when trying to compile the DemoReel100 example with FastLED 3.5.0 & Arduino 1.8.19 on MacOS 11.6:
This platform isn't recognized by FastLED... yet. See comments in FastLED/led_sysdefs.h for options.
I've never worked on adding support for a board. It looks like the following are defined by the board definition: ARDUINO_ARCH_NRF52840, ARDUINO_SEEED_XIAO_NRF52840_SENSE, ARDUINO_ARCH_NRF52, ARDUINO_ARCH_MBED
If I manually add #define NRF52_SERIES to the sketch before #include <FastLED.h>, I instead get the following errors:
Full error
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/fastpin.h: In member function 'void Pin::_init()':
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/fastpin.h:41:14: error: 'digitalPinToBitMask' was not declared in this scope
mPinMask = digitalPinToBitMask(mPin);
^~~~~~~~~~~~~~~~~~~
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/fastpin.h:41:14: note: suggested alternative: 'digitalPinToPinName'
mPinMask = digitalPinToBitMask(mPin);
^~~~~~~~~~~~~~~~~~~
digitalPinToPinName
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/fastpin.h:42:28: error: 'portOutputRegister' was not declared in this scope
mPort = (volatile RwReg*)portOutputRegister(digitalPinToPort(mPin));
^~~~~~~~~~~~~~~~~~
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/fastpin.h:43:30: error: 'portInputRegister' was not declared in this scope
mInPort = (volatile RoReg*)portInputRegister(digitalPinToPort(mPin));
^~~~~~~~~~~~~~~~~
In file included from /Users/jcoon/Documents/Arduino/libraries/FastLED/src/platforms/arm/nrf52/fastled_arm_nrf52.h:8:0,
from /Users/jcoon/Documents/Arduino/libraries/FastLED/src/platforms.h:11,
from /Users/jcoon/Documents/Arduino/libraries/FastLED/src/FastLED.h:54,
from /var/folders/5z/9mpthc4n4jl904ywb78k3w7c0000gn/T/arduino_modified_sketch_601815/DemoReel100.ino:3:
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/platforms/arm/nrf52/clockless_arm_nrf52.h: In static member function 'static void ClocklessController<_DATA_PIN, _T1, _T2, _T3, _RGB_ORDER, _XTRA0, _FLIP, _WAIT_TIME_MICROSECONDS>::startPwmPlayback_EnableInterruptsAndShortcuts(NRF_PWM_Type*)':
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/platforms/arm/nrf52/clockless_arm_nrf52.h:99:44: error: 'configMAX_SYSCALL_INTERRUPT_PRIORITY' was not declared in this scope
result = sd_nvic_SetPriority(irqn, configMAX_SYSCALL_INTERRUPT_PRIORITY);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /Users/jcoon/Documents/Arduino/libraries/FastLED/src/FastLED.h:50:0,
from /var/folders/5z/9mpthc4n4jl904ywb78k3w7c0000gn/T/arduino_modified_sketch_601815/DemoReel100.ino:3:
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/fastpin.h: In instantiation of 'class FastPin<3>':
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/platforms/arm/nrf52/clockless_arm_nrf52.h:52:38: required from 'static void ClocklessController<_DATA_PIN, _T1, _T2, _T3, _RGB_ORDER, _XTRA0, _FLIP, _WAIT_TIME_MICROSECONDS>::startPwmPlayback_InitializePinState() [with unsigned char _DATA_PIN = 3; int _T1 = 6; int _T2 = 8; int _T3 = 6; EOrder _RGB_ORDER = (EOrder)66; int _XTRA0 = 0; bool _FLIP = false; int _WAIT_TIME_MICROSECONDS = 10]'
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/platforms/arm/nrf52/clockless_arm_nrf52.h:174:44: required from 'void ClocklessController<_DATA_PIN, _T1, _T2, _T3, _RGB_ORDER, _XTRA0, _FLIP, _WAIT_TIME_MICROSECONDS>::init() [with unsigned char _DATA_PIN = 3; int _T1 = 6; int _T2 = 8; int _T3 = 6; EOrder _RGB_ORDER = (EOrder)66; int _XTRA0 = 0; bool _FLIP = false; int _WAIT_TIME_MICROSECONDS = 10]'
/var/folders/5z/9mpthc4n4jl904ywb78k3w7c0000gn/T/arduino_modified_sketch_601815/DemoReel100.ino:124:1: required from here
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/fastpin.h:210:2: error: static assertion failed: Invalid pin specified
static_assert(validpin(), "Invalid pin specified");
^~~~~~~~~~~~~
In file included from /Users/jcoon/Documents/Arduino/libraries/FastLED/src/platforms/arm/nrf52/fastled_arm_nrf52.h:8:0,
from /Users/jcoon/Documents/Arduino/libraries/FastLED/src/platforms.h:11,
from /Users/jcoon/Documents/Arduino/libraries/FastLED/src/FastLED.h:54,
from /var/folders/5z/9mpthc4n4jl904ywb78k3w7c0000gn/T/arduino_modified_sketch_601815/DemoReel100.ino:3:
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/platforms/arm/nrf52/clockless_arm_nrf52.h: In instantiation of 'static void ClocklessController<_DATA_PIN, _T1, _T2, _T3, _RGB_ORDER, _XTRA0, _FLIP, _WAIT_TIME_MICROSECONDS>::startPwmPlayback_InitializePwmInstance(NRF_PWM_Type*) [with unsigned char _DATA_PIN = 3; int _T1 = 6; int _T2 = 8; int _T3 = 6; EOrder _RGB_ORDER = (EOrder)66; int _XTRA0 = 0; bool _FLIP = false; int _WAIT_TIME_MICROSECONDS = 10]':
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/platforms/arm/nrf52/clockless_arm_nrf52.h:268:47: required from 'static void ClocklessController<_DATA_PIN, _T1, _T2, _T3, _RGB_ORDER, _XTRA0, _FLIP, _WAIT_TIME_MICROSECONDS>::startPwmPlayback(uint16_t) [with unsigned char _DATA_PIN = 3; int _T1 = 6; int _T2 = 8; int _T3 = 6; EOrder _RGB_ORDER = (EOrder)66; int _XTRA0 = 0; bool _FLIP = false; int _WAIT_TIME_MICROSECONDS = 10; uint16_t = short unsigned int]'
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/platforms/arm/nrf52/clockless_arm_nrf52.h:186:25: required from 'void ClocklessController<_DATA_PIN, _T1, _T2, _T3, _RGB_ORDER, _XTRA0, _FLIP, _WAIT_TIME_MICROSECONDS>::showPixels(PixelController<_RGB_ORDER>&) [with unsigned char _DATA_PIN = 3; int _T1 = 6; int _T2 = 8; int _T3 = 6; EOrder _RGB_ORDER = (EOrder)66; int _XTRA0 = 0; bool _FLIP = false; int _WAIT_TIME_MICROSECONDS = 10]'
/var/folders/5z/9mpthc4n4jl904ywb78k3w7c0000gn/T/arduino_modified_sketch_601815/DemoReel100.ino:124:1: required from here
/Users/jcoon/Documents/Arduino/libraries/FastLED/src/platforms/arm/nrf52/clockless_arm_nrf52.h:62:55: error: 'nrf_pin' is not a member of 'FastPin<3>'
pwm->PSEL.OUT[0] = FastPin<_DATA_PIN>::nrf_pin();
~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
Using library FastLED at version 3.5.0 in folder: /Users/jcoon/Documents/Arduino/libraries/FastLED
exit status 1
Error compiling for board Seeed XIAO nRF52840 Sense.
I was able to fix Invalid pin specified by adding the following to fastpin_arm_nrf52_variants.h:
Pin definition
#if defined (ARDUINO_SEEED_XIAO_NRF52840_SENSE)
#if defined(__FASTPIN_ARM_NRF52_VARIANT_FOUND)
#error "Cannot define more than one board at a time"
#else#define__FASTPIN_ARM_NRF52_VARIANT_FOUND#endif_FL_DEFPIN( 10, 15, 1); // D10 is P1.15 -- PIN_SPI_MOSI#endif// defined (ARDUINO_SEEED_XIAO_NRF52840_SENSE)
But I'm not sure how to fix the other errors.
If I choose Adafruit nRF52 Boards > Nordic nRF52840DK it compiles fine, but errors trying to upload.
@henrygab or anyone else have any ideas? Any help would be greatly appreciated. Thanks in advance!
Yes, I have ideas. Here's a relevant post for a different board (ItsyBitsy).
Caveats
There was no testing on MBED OS. There may be significant work required to get this working on MBED OS.
nRF52840 is currently hard-coded to maximum 144 pixels. Currently, you have to edit the header to change this.
There are design considerations that make this board very tough to get reliable outputs on, at least when BLE is enabled. The solution trades high RAM usage in order to provide strict reliability via 100% DMA output.
Critically, test each pin that was added. I think something along the lines of FastPin<ARDUINO_PIN_NUMBER>::toggle() in loop() (with delay of course) would let you test each pin number matches. I've found errors in board documentation, so this is a critical step.
If you submit a PR, would you mind also changing the currently hard-coded pixel limit, so that it's instead a hard-coded default? Changing this to be user-definable will also help minimize RAM usage.
Just change:
#defineFASTLED_NRF52_MAXIMUM_PIXELS_PER_STRING144// TODO: Figure out how to safely let this be calller-defined....
And... I found the results of my prior look at enabling MBED OS support.
Short answer:
It's non-trivial to add support for nRF52 boards that use MBED OS, because the code presumes the use of FreeRTOS. I don't have the time (or hardware) to look at that currently.
If you can get the Adafruit nRF52 bootloader working, and adapt the Adafruit nRF52 BSP for this board (likely just adding some variant headers), you might be able to run FreeRTOS on the board, which will allow the FastLED code to work. That's likely much less work than re-implementing and re-testing the FastLED code.
Activity
focalintent commentedon Mar 4, 2019
I haven’t seen anyone supply even a partial port - and I won’t be able to get to it for a while, yet.
pcaddict commentedon Mar 15, 2019
Take a look at my fork here. I have tested with the nrf52832 and it should work exactly the same for the nrf52840. I have only tested APA102 but I'd guess other SPI chipsets should work.
Video of an APA102 string being driven by the nrf52832: https://www.youtube.com/watch?v=VYZ5e_oi5cw
henrygab commentedon Mar 19, 2019
Wow, looks great, @pcaddict
Would you be able to help me understand a couple items?
Line https://github.com/pcaddict/FastLED/blob/master/platforms/arm/nrf52/fastspi_arm_nrf52.h#L59 appears to set MISO to port0.pin1.
Q# 1a. Is this intentional?
Q# 1b.
PSEL.CSNis not explicitly set, and thus appears to be presumed to be the reset-default value of0xFFFFFFFF. I think this is OK, as no device select is typically necessary. Do you agree?Q# 1c. Is it legal to leave MISO disabled while using SPIM, if you never care about input values? (and if so, should this be set to
0xFFFFFFFF?)Line https://github.com/pcaddict/FastLED/blob/master/platforms/arm/nrf52/fastspi_arm_nrf52.h#L87 appears to state that the function returns immediately after setting the register. However, it appears at line https://github.com/pcaddict/FastLED/blob/master/platforms/arm/nrf52/fastspi_arm_nrf52.h#L95 to wait for the end-of-transmission event.
Q#2. Is the comment wrong, the code wrong, or my understanding wrong?
Thanks!
henrygab commentedon Mar 23, 2019
After lots of tinkering, compiling, and adding compilation asserts / pragma messages, I've started to understand some aspects of this port. I think it will be possible to port this, which is great. Even better, we may be able rely upon the availability of Nordic's nrfx repo (part of at least Adafruit's BSP for Feather Express 52840. Work now underway....
pcaddict commentedon Mar 23, 2019
Sorry for the late reply, I did not see a notification that anyone had responded.
Q1a. I don't think setting MISO to pin 1 was intentional. It probably should have been set to 0xFFFFFFFF. As far as I can tell from the documentation it is perfectly fine to not specify a pin for MISO if you aren't using it.
Q1b. Agreed. I think I might have explicitly told PSEL.CSN to be 0xFFFFFFFF at some point but probably removed it for some unknown reason.
Q2. The comment is wrong. I took code from the NRF51 and Teensy platforms and it is likely something that originated elsewhere.
What features of the nrfx library are you thinking will be useful here?
I have not had much luck running this on my nrf52840. I have not been able to get the Adafruit BSP to play nicely with the Particle Xenon board I have, which is mostly used with the Nordic SDK anyway, so all of my testing has been with my feather52832.
henrygab commentedon Mar 24, 2019
Thanks for answers, it helps me sanity-check my understanding, and keep my sanity. :)
I'll update to indicate MISO is not needed, and explicitly set CSN for the 52840 + SPIM3 only (no other board/SPIM appears to support this).
Why use nrfx?
I've already gotten the simplest of items showing on a dotstar strip (RGB ordering verification, simple chaser). I'll push a branch "real soon now".
Do you have any thoughts on how to test/validate the nRF52840 support is "complete"?
Also, what specific problems or types of problems have you experienced with the Particle Xenon?
pcaddict commentedon Mar 24, 2019
@henrygab I'm interested to see what improvements you've made to my hatchet job of a port. I honestly got it to a point of "hey pretty lights" one afternoon and did not explore it much further than that. I did try to understand how to get neopixels working and ran out of patience.
In regards to nrfx, I wonder if that isn't going to add a bunch of complexity that many users won't want to deal with, especially in Arduino land. Might make the possibility of porting to the Nordic SDK a little simpler though.
As far as validating that support is complete, I'd imagine that'll take some community testing with various led chipsets. This is probably something @focalintent should comment on.
For some reason Particle Xenon isn't playing nicely with the nordic QSPI libraries in the Adafruit BSP. I've tried removing everything I could find having to do with the QSPI memory and still have problems with most sketches compiling. Since I got the Xenon primarily for working with the Nordic SDK anyway I have not put too much time towards figuring out why I am having the problems I am. Other than that the Xenon is a great dev board and they were a steal at $9 during pre-order.
henrygab commentedon Mar 27, 2019
@pcaddict Ok, will share "real soon now"... as soon as I have anything that I believe is any improvement.
Another question... am I misunderstanding something here?
https://github.com/pcaddict/FastLED/blob/master/platforms/arm/nrf52/fastpin_arm_nrf52.h#L83
This appears to allow the pin's register to be set to literally any value, without regard to the value of _MASK. Doesn't this break the encapsulation / purpose of _MASK?
In other words, shouldn't this instead be something more like:
NRF_P0->OUT = val & _MASK?focalintent commentedon Mar 27, 2019
No it shouldn’t - that’s used for an optimization in some places where I precompute the full value for the port so that setting the pin levels just becomes a single write vs a read, or/and, then write. (1 memory access in one instruction vs 2 memory accesses and 3 instructions (or, in the case of combining clock/data pin setting, 4 memory accesses and 6 instructions))
henrygab commentedon Mar 28, 2019
@pcaddict -- found one bug that may affect use of certain boards / pins, and thus might fix your Xenon issue. I just need to confirm it compiles, then I will push the first major fix. ETA: a couple days, maybe sooner if the Arduino IDE plays nice.
henrygab commentedon Mar 30, 2019
Ok. Frustrated with Arduino. Getting closer, but extending ETA. Don't want to push stuff that doesn't work. Wish there were better documentation on the implied contracts that the fastpin / fastspi functions must follow to work properly with FastLED….
henrygab commentedon Apr 1, 2019
@pcaddict -- Found the following note in the nRF52840 product specification, at the end of the SPIM chapter:
Based on the above, could your problems with data corruption when trying to use >4MHz be solved by simply reconfiguring the pins to high drive mode for both 0 and 1 (H0H1)?
If you have something that works at 4MHz, try using the above instead of the following lines in Init() in fastspi_arm_nrf52.h:
For the nRF52840 at least, this would be something such as:
If that solves your high-speed clocking for SPI, I'll be glad.
henrygab commentedon Apr 10, 2019
Getting closer... The nRF52840 has more than 32 pins, and thus has two ports. This requires the _ARMPIN template to be … slightly more complex. Throw in GCC's / C++'s restrictions on template parameter types, and it's just a matter of finding an expression that doesn't cause compile-time errors....
pcaddict commentedon Apr 10, 2019
Would it not make sense to just pass the result of g_ADigitalPinMap[pin] as the parameter for each pin?
henrygab commentedon Apr 10, 2019
@pcaddict - g_ADigitalPinMap[pin] is not a constexpr, and is not resolved at compile-time. Therefore, it creates some limitations for use as a template parameter.
So, the template receives a PIN, which is the Arduino definition and does NOT match the underlying chip PORT/PIN. Thus, the template must have a static _init() function that converts the passed-in PIN into the PORT/PIN that actually matters. For nRF52840, also need to save pointer to the (NRF_GPIO_Type*) for the PORT, because there are two distinct ports (yes, 48 pins... so either NRF_P0 or NRF_P1 is used).
Or at least, this is my working theory at this point. Loading the .elf in Ozone, my calls to
FastPin<PIN_LED1>::toggle()is being optimized away, even with -O0, so I'm working through figuring that out at the moment....10 remaining items
focalintent commentedon Apr 14, 2019
In that case, I'd take a PR to a branch - and then pull the nrf52 over once I get a chance to actually get the clockless support in. Given that the clockless chipsets are by far more widely used, it'd be kind of important to get it in before pulling the platform in to master.
henrygab commentedon Apr 14, 2019
Ok. A few related questions:
Do I generate the branch in your depot as part of the PR, or do you set it up? (either way, may I suggest 'nrf52' as the branch?)
Although I've added pin definitions for a large set of nrf52-based boards, I only have one of those to test with. Do you want the PR to have the following line for the untested boards?
#error "Untested board -- remove this line, test, and submit results via https://github.com/FastLED/FastLED/issues"Is there a "how to test your port" documentation anywhere? If not, do you have recommendations of various levels or types of testing for any given board port?
With that, I think I'm ready to provide at least the FastPIN implementation for initial code review.
List of board variants prepped:
#defineARDUINO_NRF52840_FEATHERARDUINO_NRF52832_FEATHERARDUINO_NRF52840_METROARDUINO_NRF52840_PCA10056ARDUINO_ELECTRONUT_BLUEYARDUINO_ELECTRONUT_HACKABLEARDUINO_ELECTRONUT_hackaBLE_v2ARDUINO_RB_BLEND_2ARDUINO_RB_BLE_NANO_2ARDUINO_NRF52_DKARDUINO_STCT_NRF52_minidevfocalintent commentedon Apr 14, 2019
I think you can target a new branch in the PR - if not I should be able to edit it
Use #warning instead of #error (#error will break/stop compilation)
The testing of a port involves running it against a variety of led types to make sure all the variations on things work : ) (my workshop has a lot of test strips for when I'm actively working on testing a board/port - I'll also rig stuff up on breadboards so I can rapidly try testing across the full set of ports, to be sure - which is a habit I got into after the Arduino mega - which had different cycle times for writing different pins!)
henrygab commentedon Apr 16, 2019
@focalintent Wow! that's quite a lot.
I have validated most of the individual ports work on the nRF52840, connected to individual pins. I ran the code under the Ozone debugger with a J-Link EDU. This helped me realize how terrible inlining all code is for debugging.
I have validated basic functionality for 16-count APA102, using software SPI.
However, I couldn't create a new target branch with my PR, so I created against master.
henrygab commentedon Apr 25, 2019
@focalintent OK, there's now a new pull request, with both FastPIN and FastSPI implemented. If someone pre-builds the buffers they want to use (e.g., more likely for POV displays), the FastSPI implementation supports a single DMA-enabled SPI transaction.
pcaddict commentedon Apr 25, 2019
@henrygab I do not have time to dig much into clockless support but if you take a look in the examples of the latest Nordic Thread and Zigbee SDK they have added a driver for WS2812 LEDs that might get you pointed in the right direction.
henrygab commentedon Apr 27, 2019
Edit: actually, I am going to use this method
The sample code is not directly relevant to clockless.
The sample sets a PWM output to the right clock rate, pre-generates the full LED string's output, and then just starts a NRFX-based playback of that pre-generated output.
Positives:
Negatives:
In contrast, the FastLED clockless implementations I've looked at appear to translate the RGB data 'on the fly' / 'just in time' to adjust the GPIO pins, and thus avoid this extra RAM overhead. In exchange, these methods re-compute the output for each LED string every time show() is called, even when no data changes for that string.
While I could code something like this up, I am concerned [the 48-bytes per pixel overhead] doesn't fit the "flavor" of FastLED.
I'm also presuming async callbacks are supported in the Arduino environment (haven't used them before). Worst, the main benefit (re-use of the buffer for refresh) is not usable with the FastLED architecture.
henrygab commentedon May 15, 2019
@focalintent, will you accept a PR with clockless implemented, even if the it uses more RAM than you're used to?
I believe I can design it to have an upper-bound memory overhead of about <2.5k RAM, even with BLE enabled.
Click on each item below to expand details....
Why reliable clockless is hard on nRF52
The `softdevice` can cause any in-progress clockless output to fail, if it relies on CPU. The interrupt processing time is on the order of hundreds of microseconds, while the clockless protocols can fail with deviations as small as 50ns.
Example BLE Interrupt Timing for SoftDevice v1.40
Note:
Freqis the minimum time between such interrupts occurring.These timings only cover softdevice processing that occurs at interrupt priority levels 0 and 1 … the delays at priority level 4 (deferred BLE processing) can substantially increase these values.
PWM peripheral based prototype
I have a working prototype. It's not templated, it uses the PWM controller, and it takes 48 bytes of RAM per pixel. The output clocking is rock-solid, once it starts. The PWM sequence buffer is compile-time allocated based on the length of the longest LED string, but only one such buffer exists, regardless of the number of output ports. (e.g. 144 LEDs would take just under 7k RAM for PWM sequence data)
Memory Usage vs. Timing
Starting with the following:
The PWM peripheral can chain / loop up to two sequences, and it must be setup
so that it will have enough data if pre-empted at any time to be able to push
out another 14 pixels of data, AND be able to give the peripheral additional data
before it's fully drained.
Since pre-emption can happen with zero bits left in the current sequence,
each sequence must have at least 14 pixels of data. A three-buffer approach,
each having ~16 pixels of data (768 bytes per sequence buffer, 2304 bytes total)
would likely be possible without loss of reliability in all but extreme cases, and
even there would be detectable.
Anyways, please let me know if this PR (high-RAM but reliable) would be of interest.
henrygab commentedon May 24, 2019
PR is now ready via #802, supporting compile-time determined fixed maximum length of clockless.,
jasoncoon commentedon Jan 17, 2022
I just received a XIAO BLE from Seeed, which has an nRF52840 M4F@64MHz: https://wiki.seeedstudio.com/XIAO_BLE
I get the following error when trying to compile the
DemoReel100example with FastLED 3.5.0 & Arduino 1.8.19 on MacOS 11.6:This platform isn't recognized by FastLED... yet. See comments in FastLED/led_sysdefs.h for options.I've never worked on adding support for a board. It looks like the following are defined by the board definition: ARDUINO_ARCH_NRF52840, ARDUINO_SEEED_XIAO_NRF52840_SENSE, ARDUINO_ARCH_NRF52, ARDUINO_ARCH_MBED
If I manually add
#define NRF52_SERIESto the sketch before#include <FastLED.h>, I instead get the following errors:Full error
I was able to fix
Invalid pin specifiedby adding the following tofastpin_arm_nrf52_variants.h:Pin definition
But I'm not sure how to fix the other errors.
If I choose
Adafruit nRF52 Boards>Nordic nRF52840DKit compiles fine, but errors trying to upload.@henrygab or anyone else have any ideas? Any help would be greatly appreciated. Thanks in advance!
henrygab commentedon Jan 18, 2022
Hi Jason,
Funny how small a world it is. 😄
Yes, I have ideas. Here's a relevant post for a different board (ItsyBitsy).
Caveats
There was no testing on MBED OS. There may be significant work required to get this working on MBED OS.
nRF52840 is currently hard-coded to maximum 144 pixels. Currently, you have to edit the header to change this.
There are design considerations that make this board very tough to get reliable outputs on, at least when BLE is enabled. The solution trades high RAM usage in order to provide strict reliability via 100% DMA output.
Generally, how to enable a new nRF52840 board
Start by reviewing PR Enable Circuit playground Bluefruit #1027, which enabled the Circuit Playground Bluefruit board.
Add a new section to platforms/arm/nrf52/fastpin_arm_nrf52_variants.h, based on whether
ARDUINO_SEEED_XIAO_NRF52840_SENSEis defined. (See example)Determine the Arduino pin number <--> nRF52840 Port/Pin mapping for each of the exposed / usable pins on the new board.
For each exposed / usable pins, add a new line in that new section you've added in step 2. Use the following form:
Critically, test each pin that was added. I think something along the lines of
FastPin<ARDUINO_PIN_NUMBER>::toggle()in loop() (with delay of course) would let you test each pin number matches. I've found errors in board documentation, so this is a critical step.Test each of those added pins as a clockless data pin driving 100 pixels. _If this step is not performed_, then please add a warning that the board is currently untested.
Optionally, test at least a couple pins driving some APA102 (SPI ... data + clock) pixels.
henrygab commentedon Jan 18, 2022
Using https://wiki.seeedstudio.com/XIAO_BLE/ as a starting reference point, here's a strawman of what might work:
henrygab commentedon Jan 18, 2022
If you submit a PR, would you mind also changing the currently hard-coded pixel limit, so that it's instead a hard-coded default? Changing this to be user-definable will also help minimize RAM usage.
Just change:
Into:
henrygab commentedon Jan 18, 2022
And... I found the results of my prior look at enabling MBED OS support.
Short answer:
It's non-trivial to add support for nRF52 boards that use MBED OS, because the code presumes the use of FreeRTOS. I don't have the time (or hardware) to look at that currently.
If you can get the Adafruit nRF52 bootloader working, and adapt the Adafruit nRF52 BSP for this board (likely just adding some variant headers), you might be able to run FreeRTOS on the board, which will allow the FastLED code to work. That's likely much less work than re-implementing and re-testing the FastLED code.
Sorry!