Non-Blocking WS2812 LED Library and Teensy 4.0

Status
Not open for further replies.

JupiterMoll

Member
Hallo,

I want to use Teensy 4.0 with the Non-Blocking WS2812 LED Library that doesn’t interfere with interrupts in combination with the Audio Library.
On compiling i got this Error Message:

Code:
Arduino: 1.8.9 (Linux), TD: 1.48, Board: "Teensy 4.0, Serial, 600 MHz, Faster, US English"

/home/alphi/Downloads/arduino-1.8.9-linux64/arduino-1.8.9/hardware/teensy/avr/libraries/WS2812Serial/WS2812Serial.cpp: In member function 'bool WS2812Serial::begin()':
/home/alphi/Downloads/arduino-1.8.9-linux64/arduino-1.8.9/hardware/teensy/avr/libraries/WS2812Serial/WS2812Serial.cpp:29:2: error: 'KINETISK_UART_t' was not declared in this scope
  KINETISK_UART_t *uart;
  ^
/home/alphi/Downloads/arduino-1.8.9-linux64/arduino-1.8.9/hardware/teensy/avr/libraries/WS2812Serial/WS2812Serial.cpp:29:19: error: 'uart' was not declared in this scope
  KINETISK_UART_t *uart;
                   ^
/home/alphi/Downloads/arduino-1.8.9-linux64/arduino-1.8.9/hardware/teensy/avr/libraries/WS2812Serial/WS2812Serial.cpp:136:13: error: 'UART_C2_TE' was not declared in this scope
  uart->C2 = UART_C2_TE | UART_C2_TIE;
             ^
/home/alphi/Downloads/arduino-1.8.9-linux64/arduino-1.8.9/hardware/teensy/avr/libraries/WS2812Serial/WS2812Serial.cpp:136:26: error: 'UART_C2_TIE' was not declared in this scope
  uart->C2 = UART_C2_TE | UART_C2_TIE;
                          ^
/home/alphi/Downloads/arduino-1.8.9-linux64/arduino-1.8.9/hardware/teensy/avr/libraries/WS2812Serial/WS2812Serial.cpp:137:13: error: 'UART_C3_TXINV' was not declared in this scope
Multiple libraries were found for "WS2812Serial.h"
 Used: /home/alphi/Downloads/arduino-1.8.9-linux64/arduino-1.8.9/hardware/teensy/avr/libraries/WS2812Serial
 Not used: /home/alphi/Arduino/libraries/WS2812Serial-master
  uart->C3 = UART_C3_TXINV;
             ^
/home/alphi/Downloads/arduino-1.8.9-linux64/arduino-1.8.9/hardware/teensy/avr/libraries/WS2812Serial/WS2812Serial.cpp:138:13: error: 'UART_C5_TDMAS' was not declared in this scope
  uart->C5 = UART_C5_TDMAS;
             ^
/home/alphi/Downloads/arduino-1.8.9-linux64/arduino-1.8.9/hardware/teensy/avr/libraries/WS2812Serial/WS2812Serial.cpp:28:11: warning: unused variable 'divisor' [-Wunused-variable]
  uint32_t divisor, portconfig, hwtrigger;
           ^
Error compiling for board Teensy 4.0.


Is it possible that Non-Blocking WS2812 LED Library is not compatible with Teensy 4.0 ???
I"m asking because i want to use WS2812 Led in combination with the Audio Libray so they don't interfere with interrupts.

Thx.
 
Last edited:
This is sort of a two part answer.

The simple answer is, as far as I know, this library has not been adapted to allow it to work on a T4.
The first place I look in questions like this is in the Beta thread for the T4. There are a couple of postings near the top of this monster thread, with sort of summary of states of different libraries and testing. In this case the last place this library was mentioned in this thread was posting #6

The next place I look is at the sources, this is one of @PaulStoffregen's libraries: https://github.com/PaulStoffregen/WS2812Serial
And I don't see any recent commits nor any outstanding Pull Requests that mention T4, so my guess is it is not done yet.

The second half answer is, is there any reason that the library could not be adapted to T4? I am sort of surprised others have not done this one yet. The simple question might be, what is the minimal setup one needs to test this one out?

I might take a quick look and get an idea of difficult it would be. The first look did not look to hard. I might play for a little time and see if I can get anything out an IO pin, which I will probably only test using Logic Analyzer, otherwise I might need to rig up some simple level shifter...
 
Hi KurtE,

i'm not a low level programmer. My strength lies in the field of creative coding with electronic hardware.
If i can help in some way let me know.
 
It took a little longer than an hour, with lots of hair pulling. Actually not much (as I don't have much left ;) )

I have a version up at: https://github.com/KurtE/WS2812Serial/tree/T4

However what took so long to get anything to output, was that the DMAMUX_SOURCE values in imxrt.h file were wrong...

So I edited that file, pushed up the changes to my branch: https://github.com/KurtE/cores/tree/t4_avr_registers

Note: the above branch is setup to do a Pull Request into Paul's master branch, which fixed and/or added some additional things...

So far the only testing I have done is to run the Basic_test with logic analyzer connected to Serial1 TX pin, and finally got data coming out :D

However don't know if it is the right data. Also still has some different settings, that I don't know will work correctly or not...

I did verify that my changes still compiled for T3.6 and LC, but did not run them....

Side note: @PaulStoffregen - Not sure how it compiles/works on T-LC, since it looks like it is trying to use Kinetisk registers... But I did not look very hard, could be somewhere you setup stuff for Kinetisl to emulate the kinetisk, in this regard...


But it would be would be good if someone might actually try it and see if it works. If any of it works will issue PR back to Paul...
 
Cool...KurtE..:cool:
I will...try your code ...tomorrow...
and report my my expereince .... with teensy 4.0 , AudioShield Revion D and ws2182b ....
THX!!!!!!!!!!!!!
 
But it would be would be good if someone might actually try it and see if it works. If any of it works will issue PR back to Paul...

Hi KurtE, i upload the example Code of the Library to Teensy 4.0:

Code:
/* WS2812Serial BasicTest Example */

#include <WS2812Serial.h>

const int numled = 4;
const int pin = 1;

// Usable pins:
//   Teensy LC:   1, 4, 5, 24
//   Teensy 3.2:  1, 5, 8, 10, 31   (overclock to 120 MHz for pin 8)
//   Teensy 3.5:  1, 5, 8, 10, 26, 32, 33, 48
//   Teensy 3.6:  1, 5, 8, 10, 26, 32, 33

byte drawingMemory[numled*3];         //  3 bytes per LED
DMAMEM byte displayMemory[numled*12]; // 12 bytes per LED

WS2812Serial leds(numled, displayMemory, drawingMemory, pin, WS2812_GRB);


#define WHITE  0xFFFFFF

void setup() {
  leds.begin();
}

void loop() {
 leds.setPixel(0, WHITE);
    leds.show();
}

But nothing lights up.:(
Is a specific Data Pin important ?
 
@JupiterMoll - @KurtE
Looks like available pins are any SerialX Tx pins (see WS2812Serial.cpp)
Code:
//   Teensy 4.0:  1, 8, 14, 16, 20, 24

EDIT: From what I saw the WS2812 leds are 5v so to use with a T4 you might need a level shifter on DIN/DOUT. If you don't want to use a level shifter you might want to check this out I saw on Hack-a-day: https://hackaday.com/2017/01/20/cheating-at-5v-ws2812-control-to-use-a-3-3v-data-line/ or this on level shifting https://hackaday.com/2016/12/05/tak...and-5v-logic-communicate-with-level-shifters/
 
Besides using @KurtE's WS2812Serial modified library did you remember to update your imxrt.h file with the corrections to the DMAMU_SOURCE

KurtE said:
However what took so long to get anything to output, was that the DMAMUX_SOURCE values in imxrt.h file were wrong...

So I edited that file, pushed up the changes to my branch: https://github.com/KurtE/cores/tree/t4_avr_registers
I just ordered a couple of strips from Amazon for testing.
 
Did you update the cores\teensy4\imxrt.h file? If not The DMAMUX_SOURCE values are wrong and nothing will happen...

A quick test and hack is if you edit the file WS2812Serial.cpp for example for Serial1, change line 126:
From: hwtrigger = DMAMUX_SOURCE_LPUART6_TX;
To: hwtrigger = DMAMUX_SOURCE_LPUART6_RX;

The RX and TX values were swapped in the IMXRT.h file...

As for Pins:
Code:
//   Teensy 4.0:  1, 8, 14, 16, 20, 24,[COLOR="#FF0000"] 29, 39[/COLOR]
Assuming any of this works for you, I will update the example comments and the like and do PR.

Note: I did get it to look like parts of it were working yesterday. I found a Neopixel 8, from earlier project (I know I have some more around here), and it was updating, not 100% correct, but I believe some of that was due to the level shifter (Adafruit Bidirectional) I tried, which was the first one I ran across in my bucket of parts, which I know is not the best one for this. It also showed some similar stuff as wired into a T3.5
 
Unfortunately it only works partially.

I first setup a Teensy 3.6 to run BasicTest using Serial 1, and hooked up 2 32-LED rings for 64-LEDs. It ran fine.

I then created a new directory with KurtE's WS2812Serial library and updated to use the teensy4 directory from the t4_avr_branch for the imxrt.h include file. I ran it on the T4, and after 7 passes, my T4 locked up. I could not reprogram the Teensy with blink, and pressing the program button did not work. I was able to program it by rebooting it and pressing the program button immediately. I tried holding the program button down for 15 seconds, but I didn't get the timing right.
 
Did you update the cores\teensy4\imxrt.h file? If not The DMAMUX_SOURCE values are wrong and nothing will happen...

A quick test and hack is if you edit the file WS2812Serial.cpp for example for Serial1, change line 126:
From: hwtrigger = DMAMUX_SOURCE_LPUART6_TX;
To: hwtrigger = DMAMUX_SOURCE_LPUART6_RX;

I do a test with your hack without modifying the imxrt.h file using only Serial1 >> Data Pin 1 and 4 Ws2182B Leds.
And it works .....:D
Using following test code:

Code:
/* WS2812Serial BasicTest Example */

#include <WS2812Serial.h>

const int numled = 4;
const int pin = 1;

// Usable pins:
//   Teensy LC:   1, 4, 5, 24
//   Teensy 3.2:  1, 5, 8, 10, 31   (overclock to 120 MHz for pin 8)
//   Teensy 3.5:  1, 5, 8, 10, 26, 32, 33, 48
//   Teensy 3.6:  1, 5, 8, 10, 26, 32, 33
//   Teensy 4.0:  1 , only tested with hack: hwtrigger = DMAMUX_SOURCE_LPUART6_RX 

byte drawingMemory[numled*3];         //  3 bytes per LED
DMAMEM byte displayMemory[numled*12]; // 12 bytes per LED

WS2812Serial leds(numled, displayMemory, drawingMemory, pin, WS2812_GRB);


#define WHITE  0xFFFFFF
#define BLACK  0x000000

void setup() {
  leds.begin();
}

void loop() {

 leds.setPixel(0, WHITE);
 leds.setPixel(1, WHITE);
 leds.setPixel(2, WHITE);
 leds.setPixel(3, WHITE);
 leds.show();

 delay(100);
 
 leds.setPixel(0, BLACK);
 leds.setPixel(1, BLACK);
 leds.setPixel(2, BLACK);
 leds.setPixel(3, BLACK);
 leds.show();
    
 delay(100);
}
 
If I use the stock imxrt.h from Teensy 1.48, I don't get any LEDs being flashed. If I just use the imxrt.h file and not the other files from the teensy4 directly like I did previously, it still hangs sometimes after the 7th round.
 
I started to test the other file included in example folder of Non-Blocking WS2812 LED Library : FastLED_Cylon.ino

Code:
// FastLED Cylon Example, using Non-Blocking WS2812Serial

#include <WS2812Serial.h>
#define USE_WS2812SERIAL
#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 4

// Usable pins:
//   Teensy LC:   1, 4, 5, 24
//   Teensy 3.2:  1, 5, 8, 10, 31   (overclock to 120 MHz for pin 8)
//   Teensy 3.5:  1, 5, 8, 10, 26, 32, 33, 48
//   Teensy 3.6:  1, 5, 8, 10, 26, 32, 33
#define DATA_PIN 1

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
	Serial.begin(57600);
	Serial.println("resetting");
	LEDS.addLeds<WS2812SERIAL,DATA_PIN,RGB>(leds,NUM_LEDS);
	LEDS.setBrightness(84);
}

void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }

void loop() {
	static uint8_t hue = 0;
	Serial.print("x");
	// First slide the led in one direction
	for(int i = 0; i < NUM_LEDS; i++) {
		// Set the i'th led to red
		leds[i] = CHSV(hue++, 255, 255);
		// Show the leds
		FastLED.show();
		// now that we've shown the leds, reset the i'th led to black
		// leds[i] = CRGB::Black;
		fadeall();
		// Wait a little bit before we loop around and do it again
		delay(20);
	}
	Serial.print("x");

	// Now go in the other direction.
	for(int i = (NUM_LEDS)-1; i >= 0; i--) {
		// Set the i'th led to red
		leds[i] = CHSV(hue++, 255, 255);
		// Show the leds
		FastLED.show();
		// now that we've shown the leds, reset the i'th led to black
		// leds[i] = CRGB::Black;
		fadeall();
		// Wait a little bit before we loop around and do it again
		delay(20);
	}
}

Ant i got following compiling error:

Code:
Arduino: 1.8.9 (Linux), TD: 1.48, Board: "Teensy 4.0, Serial, 600 MHz, Faster, US English"

In file included from /home/alphi/Arduino/libraries/WS2812Serial-T4/examples/FastLED_Cylon/FastLED_Cylon.ino:5:0:
/home/alphi/Arduino/libraries/FastLED/FastLED.h:14:21: note: #pragma message: FastLED version 3.003.002
 #    pragma message "FastLED version 3.003.002"
                     ^
In file included from /home/alphi/Arduino/libraries/WS2812Serial-T4/examples/FastLED_Cylon/FastLED_Cylon.ino:5:0:
/home/alphi/Arduino/libraries/FastLED/FastLED.h: In static member function 'static CLEDController& CFastLED::addLeds(CRGB*, int, int)':
/home/alphi/Arduino/libraries/FastLED/FastLED.h:393:10: error: 'CWS2812SerialController' does not name a type
   static CWS2812SerialController<DATA_PIN,RGB_ORDER> controller;
          ^
/home/alphi/Arduino/libraries/FastLED/FastLED.h:394:19: error: 'controller' was not declared in this scope
   return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset);
                   ^
Using library WS2812Serial-T4 in folder: /home/alphi/Arduino/libraries/WS2812Serial-T4 (legacy)
Using library FastLED at version 3.3.2 in folder: /home/alphi/Arduino/libraries/FastLED 
Using library SPI at version 1.0 in folder: /home/alphi/Downloads/arduino-1.8.9-linux64/arduino-1.8.9/hardware/teensy/avr/libraries/SPI 
Error compiling for board Teensy 4.0.
 
@ MichaelMeissner not sure what happened.

@all.

I have better case now for level shifter using propshield... Working better. I have updated the two examples plus readme.md file, fixed Pin 17...
Tried on the valid pins on standard outer pins as using breadboard. Will try others later.

I went ahead and issued PR: https://github.com/PaulStoffregen/WS2812Serial/pull/8

IMG_0988.jpg
 
Note, I didn't bother with the level shifter, I just powered the two neopixels from 3.3v as these rings seem to be ok with 3.3v. Maybe it tripped the fuse. I need to get off to work. Tonight, I'll break out a level shifter and use 5v tonight. But the Teensy 3.6 ran with the same setup past where the Teensy 4.0 hangs.
 
Thanks @MichaelMeissner.

I moved the setup to the FRDM (with USB) board, which is plugged into breadboard and tested the other pins, and they appear to work as well.
This is including 39 which is an alternate pin in Serial5. The only alternate Serial TX pin...
 
I have updated the two examples plus readme.md file, fixed Pin 17...

Do you test the FastLED_Cylon.ino example with your new update in github ?

I still got compiling errors after downloading your https://github.com/KurtE/WS2812Serial/tree/T4.

Code:
In file included from /tmp/arduino_modified_sketch_372441/FastLED_Cylon.ino:5:0:
/home/alphi/Arduino/libraries/FastLED/FastLED.h:14:21: note: #pragma message: FastLED version 3.003.002
 #    pragma message "FastLED version 3.003.002"
                     ^
In file included from /tmp/arduino_modified_sketch_372441/FastLED_Cylon.ino:5:0:
/home/alphi/Arduino/libraries/FastLED/FastLED.h: In static member function 'static CLEDController& CFastLED::addLeds(CRGB*, int, int)':
/home/alphi/Arduino/libraries/FastLED/FastLED.h:393:10: error: 'CWS2812SerialController' does not name a type
   static CWS2812SerialController<DATA_PIN,RGB_ORDER> controller;
          ^
/home/alphi/Arduino/libraries/FastLED/FastLED.h:394:19: error: 'controller' was not declared in this scope
   return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset);
 
Ok... i will check it out....

Right now I find the line of code in FastLED.h that causes compile error for the example FastLED_Cylon.ino in the Non-Blocking WS2812 LED Library in combination with Teensy 4.0.

Code:
In file included from /tmp/arduino_modified_sketch_232718/FastLED_Cylon.ino:5:0:
/home/alphi/Arduino/libraries/FastLED/FastLED.h: In static member function 'static CLEDController& CFastLED::addLeds(CRGB*, int, int)':
/home/alphi/Arduino/libraries/FastLED/FastLED.h:393:10: error: 'CWS2812SerialController' does not name a type
   static CWS2812SerialController<DATA_PIN,RGB_ORDER> controller;
          ^
/home/alphi/Arduino/libraries/FastLED/FastLED.h:394:19: error: 'controller' was not declared in this scope
   return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset);

FastLED.h:

Code:
#ifdef USE_WS2812SERIAL
	template<SWS2812 CHIPSET, int DATA_PIN, EOrder RGB_ORDER>
	static CLEDController &addLeds(struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset = 0)
	{
		static CWS2812SerialController<DATA_PIN,RGB_ORDER> controller;
		return addLeds(&controller, data, nLedsOrOffset, nLedsIfOffset);
	}
#endif

Sadly i don't know where to start to find a solution...
 
I got it to build, not sure if it will work.

But if you look where your FastLED library downloaded and look for the file:
Code:
..FastLED\platforms\arm\mxrt1062\fastled_arm_mxrt1062.h

And try changing the contents to:


Code:
#ifndef __INC_FASTLED_ARM_MXRT1062_H
#define __INC_FASTLED_ARM_MXRT1062_H

#include "fastpin_arm_mxrt1062.h"
#include "fastspi_arm_mxrt1062.h"
#include "../k20/octows2811_controller.h"
#include "../k20/ws2812serial_controller.h"
#include "../k20/smartmatrix_t3.h"
#include "clockless_arm_mxrt1062.h"
#include "block_clockless_arm_mxrt1062.h"

#endif
 
As I suspected earlier, when I saw the lock up, with the pixelss all plugged into 3.3v, it was just about to display white (0xff, 0xff, and 0xff) to all 64 pixels (i.e. all white at maximum intensity). That is what caused the Teensy 4.0 to lock up. Presumably, this was due to the fuse being triggered. I attached a SN74HCT125N and used the 5v directly from the USB to power the LEDs, it worked fine.

I was curious, and I put a USB voltmeter/ammeter on the USB connection, it read about 1.2 amps (at 5v) when it was displaying all white at maximum intensity. This matches up with the specs that say the pixels will consume ~18-20 mA per pixel at maximum brightness.

I forgot about the large power draw, since I always limit the max for R + G + B to be no more than 30 (i.e. white would be 10 + 10 + 10). This allows me to run a pair of goggles using the 2 32-pixel rings at 75 mA. But in this case, I was taking the example from the WS2812Serial library.
 
I got it to build, not sure if it will work.

But if you look where your FastLED library downloaded and look for the file:
Code:
..FastLED\platforms\arm\mxrt1062\fastled_arm_mxrt1062.h

And try changing the contents to:


Code:
#ifndef __INC_FASTLED_ARM_MXRT1062_H
#define __INC_FASTLED_ARM_MXRT1062_H

#include "fastpin_arm_mxrt1062.h"
#include "fastspi_arm_mxrt1062.h"
#include "../k20/octows2811_controller.h"
#include "../k20/ws2812serial_controller.h"
#include "../k20/smartmatrix_t3.h"
#include "clockless_arm_mxrt1062.h"
#include "block_clockless_arm_mxrt1062.h"

#endif

I tested it with the Teensy 4.and it works....;)
Thank you so much for your rapid help KurtE!!!!!!!!!!!!!!
 
I tested it with the Teensy 4.and it works....;)
Thank you so much for your rapid help KurtE!!!!!!!!!!!!!!

You are welcome: I just issued a Pull Request back to FastLED project: https://github.com/FastLED/FastLED/pull/922

@PaulStoffregen - Not sure where you are pulling in the copy of FastLED into releases, but your fork is a quite a bit behind the FastLED main fork...

@all - Again Not sure what is going to happen to the ownership of the FastLED library, as the owner died in a terrible accident.
https://forum.pjrc.com/threads/57502-FastLED-Sad-news?highlight=garcia
 
Last edited:
You are welcome: I just issued a Pull Request back to FastLED project: https://github.com/FastLED/FastLED/pull/922

@PaulStoffregen - Not sure where you are pulling in the copy of FastLED into releases, but your fork is a quite a bit behind the FastLED main fork...

@all - Again Not sure what is going to happen to the ownership of the FastLED library, as the owner died in a terrible accident.
https://forum.pjrc.com/threads/57502-FastLED-Sad-news?highlight=garcia

Hi KurtE et al. Sadly, we did lose Dan Garcia in September, and for personal reasons, I also took a little break from maintaining FastLED. However, I'm back again now, and I've added a couple of new maintainers to the project as well; FastLED will continue.

I'm looking at the PR now, and will merge it in asap. Thank you for your help and support!
-Mark
 
Status
Not open for further replies.
Back
Top