Driving WS2812 RGB LEDs on MCXN947 Using SCTimer and eDMA
1. Introduction to MCXN947
The MCXN947 is a high-performance MCU in the NXP MCX N series. It features a dual-core Arm® Cortex®-M33 architecture running at up to 150 MHz and integrates a rich set of connectivity and control peripherals. It is well suited for applications such as industrial control, human-machine interfaces (HMI), smart home devices, edge AI, building automation, and motor control.
In addition to commonly used peripherals such as UART, SPI, I2C, ADC, and FlexCAN, the MCXN947 also integrates several advanced modules, including:
eDMA
SCTimer
LCD controller
USB High-Speed
Ethernet
CAN FD
I3C
SmartDMA
Among these peripherals, SCTimer is particularly useful for applications that require flexible and precise timing generation.
Unlike a conventional PWM timer, SCTimer uses an event-state architecture, which makes it suitable for implementing:
High-precision PWM
Multiple waveform outputs
Hardware state machines
DMA triggering
Protocol timing generation
For timing-sensitive protocols such as WS2812, DShot, and infrared control protocols, SCTimer can provide a more flexible and efficient solution than a conventional timer.
This article demonstrates how to combine the SCTimer and eDMA on the MCXN947 to implement a hardware-driven WS2812 RGB LED solution with minimal CPU involvement during data transmission.
2. Project Background
The WS2812, or WS2812B, is an addressable RGB LED with an integrated driver IC. Data is transmitted through a single-wire serial protocol.
Each LED receives 24 bits of color data in the following order:
G7 ... G0
R7 ... R0
B7 ... B0
After receiving its own 24-bit color value, each LED passes the remaining data to the next LED in the chain. This makes it possible to control a large number of RGB LEDs using only a single MCU output pin.
Because WS2812 communication has relatively strict pulse-width requirements, MCU implementations commonly use one of the following approaches:
GPIO bit-banging
Timer + interrupt
SPI waveform encoding
Timer + DMA
For long LED strips or applications with strict real-time requirements, GPIO bit-banging and interrupt-based implementations can consume significant CPU resources.
In this project, we use:
SCTimer + eDMA
to implement a hardware-driven WS2812 interface.
The main advantages are:
No bit-level interrupts
No software delay for individual bits
No CPU intervention during bitstream transmission
Accurate and stable timing
Good scalability for longer LED chains
3. Why SCTimer Instead of FlexPWM, CTIMER, or SPI?
There are several ways to implement a WS2812 interface on the MCXN947. Let's briefly compare them.
SPI Approach
A common technique is to encode each WS2812 bit into multiple SPI bits.
For example:
WS2812 bit 0 → 100
WS2812 bit 1 → 110
The encoded data is then transmitted using SPI.
Advantages
Relatively simple implementation
Disadvantages
Data size increases by approximately 3x or more
An additional encoding buffer is required
SPI clock selection must closely match the required WS2812 timing
Less flexible waveform control
For example, for 100 LEDs:
100 LEDs
↓
2400 WS2812 bits
↓
7200 SPI bits
The extra encoded data increases memory usage, especially as the LED chain becomes longer.
CTIMER Approach
CTIMER can also generate PWM waveforms suitable for WS2812.
However, the match value needs to be updated for every bit.
This usually requires:
CTIMER + DMA
or:
CTIMER + Interrupt
While this is certainly possible, CTIMER does not provide the same flexible event-based architecture as SCTimer.
FlexPWM Approach
FlexPWM is a powerful PWM module primarily designed for applications such as motor control.
Although it can also be used to generate WS2812 waveforms, it has several considerations:
More complex configuration
PWM resources may already be used by motor-control functions
DMA-based duty-cycle updates can be more complicated
For a single-wire LED protocol, FlexPWM may provide more functionality than is actually required.
SCTimer Approach
SCTimer provides several features that map naturally to the WS2812 protocol.
MATCH/MATCHREL Double Buffering
eDMA can update:
MATCHREL
while SCTimer uses:
MATCH
for the current waveform.
At the appropriate reload point, the MATCHREL value is transferred to MATCH automatically, allowing the next pulse width to be prepared without stopping the counter.
Event Mechanism
In this implementation:
Event0 → Bit-period control
Event1 → Duty-cycle control
+ DMA trigger
This maps very naturally to the WS2812 waveform.
Flexible Output Control
SCTimer events can control the output using actions such as:
SET
CLEAR
TOGGLE
Therefore, waveform edges can be generated entirely by hardware.
DMA Integration
An SCTimer event can generate a DMA request:
SCTimer Event
↓
DMA Request
↓
eDMA
↓
MATCHREL Update
No CPU intervention is required for each transmitted bit.
For these reasons, SCTimer + eDMA provides an efficient and scalable approach to implementing WS2812 communication on the MCXN947.
4. System Architecture
The overall data flow is shown below:
RGB Color Buffer
│
▼
WS2812_PrepareBits()
│
▼
g_bitValues[]
(53 or 135 ticks)
│
▼
eDMA
│
▼
SCT0->MATCHREL[]
│
▼
SCTimer
│
┌─────────────────────┐
│ Event0: Bit Period │
│ Event1: Duty Cycle │
└─────────────────────┘
│
▼
OUT4
│
▼
WS2812
The responsibilities of each component are clearly separated:
CPU
│
└─→ Prepares/encodes LED data
eDMA
│
└─→ Transfers pulse-width values
SCTimer
│
└─→ Generates the output waveform
Once transmission starts, SCTimer and eDMA handle the bitstream generation without requiring CPU intervention for each bit.
5. WS2812 Timing Design
In this implementation, the timing parameters are defined as:
#define WS2812_BIT_PERIOD 188U
#define WS2812_T0H 53U
#define WS2812_T1H 135U
The SCTimer clock is:
150 MHz
Therefore, one timer tick is approximately:
1 / 150 MHz ≈ 6.67 ns
Bit Period
188 × 6.67 ns
≈ 1.253 µs
≈ 798 kHz
This is close to the nominal 800 kHz WS2812 data rate.
Logic 0
The high-level duration is:
53 × 6.67 ns
≈ 353 ns
Therefore:
T0H ≈ 353 ns
T0L ≈ 900 ns
Logic 1
The high-level duration is:
135 × 6.67 ns
≈ 900 ns
Therefore:
T1H ≈ 900 ns
T1L ≈ 353 ns
In simplified form:
Logic 0:
┌───┐
│ │
───┘ └────────────────
353 ns
Logic 1:
┌────────────┐
│ │
───┘ └────────
900 ns
The total bit period remains approximately 1.253 µs for both logic 0 and logic 1.
6. SCTimer Event Design
Only two SCTimer events are required in this implementation.
Event0: Bit-Period Event
Event0 is responsible for:
Counter Limit
+
Set Output High
For example:
SCTIMER_CreateAndScheduleEvent(
SCT0,
kSCTIMER_MatchEventOnly,
WS2812_BIT_PERIOD - 1U,
...
);
This event defines the approximately 1.25 µs bit period.
At the beginning of each new bit period, the output is driven high.
Event1: Duty-Cycle Event
Event1 performs two important functions:
Clear Output
+
Trigger DMA
This is configured using SCTimer functions such as:
SCTIMER_SetupOutputClearAction();
SCTIMER_SetupDmaTriggerAction();
The timing of Event1 is dynamically controlled using either:
53 ticks
or:
135 ticks
depending on whether the current WS2812 bit is logic 0 or logic 1.
7. MATCH/MATCHREL Double-Buffering Mechanism
This is one of the key concepts in this implementation.
SCTimer provides the following match-related registers:
MATCH
MATCHREL
While the current bit is being transmitted:
MATCH
controls the timing of the current waveform.
At the same time, eDMA updates:
MATCHREL
with the timing value required for the next bit.
At the next reload point:
MATCHREL
│
▼
Automatic Reload
│
▼
MATCH
This creates a hardware pipeline:
Output current bit
│
▼
DMA prepares next bit
│
▼
MATCHREL is reloaded
│
▼
Output next bit
This mechanism allows the pulse width for the next bit to be prepared while the current bit is still being transmitted.
Most importantly, there is no need for the CPU to update the duty cycle on a bit-by-bit basis.
8. Data Encoding
The LED color data is stored in:
g_ledData[][]
using GRB byte order, as required by the WS2812 protocol.
Before transmission, the following function is called:
WS2812_PrepareBits();
It converts the LED color data into an array of SCTimer match values:
g_bitValues[]
Each WS2812 bit is represented by one timing value:
53 → Logic 0
135 → Logic 1
For example, an RGB/GRB data pattern might be converted into a sequence such as:
53
53
53
135
135
...
These values are then written by eDMA directly to the corresponding SCTimer MATCHREL register.
In other words, g_bitValues[] is not the serial waveform itself. Instead, it contains the high-pulse duration for each WS2812 bit.
9. eDMA Configuration
The DMA transfer path is:
g_bitValues[]
│
▼
SCT0->MATCHREL[]
The source address is incremented after each transfer so that eDMA reads the next encoded pulse-width value.
Source:
g_bitValues[0]
g_bitValues[1]
g_bitValues[2]
...
The destination address remains fixed:
Destination:
SCT0->MATCHREL[...]
Each DMA transfer moves:
2 bytes
corresponding to one match value used to determine the high time of one WS2812 bit.
This means every DMA request prepares the pulse width required for the following bit.
10. Why Does DMA Start from Bit 1?
An important implementation detail is that the DMA source starts from:
&g_bitValues[1]
rather than:
&g_bitValues[0]
This is intentional.
The timing for Bit 0 is preloaded by the CPU before SCTimer starts.
The sequence is therefore:
Preload Bit 0
│
▼
Transmit Bit 0
│
▼
DMA prepares Bit 1
│
▼
Transmit Bit 1
│
▼
DMA prepares Bit 2
│
▼
Transmit Bit 2
│
▼
...
This creates a continuous hardware pipeline.
By the time the current bit completes, the timing value needed for the next bit has already been written to MATCHREL.
11. Data Transmission Flow
The complete transmission flow is:
WS2812_PrepareBits()
│
▼
Preload Bit 0
│
▼
Start eDMA
│
▼
Start SCTimer
│
▼
Output Bit 0
│
▼
DMA loads Bit 1
│
▼
Output Bit 1
│
▼
DMA loads Bit 2
│
▼
...
│
▼
DMA transfer complete
│
▼
Output remains LOW
│
▼
WS2812 Reset/Latch
During bitstream transmission:
No bit-level interrupt is required.
The CPU does not update the duty cycle for individual bits.
SCTimer generates the waveform.
eDMA provides the next pulse-width value.
This allows the bitstream itself to be generated entirely by hardware.
12. DMA Completion Handling
After the DMA major loop completes, the DMA callback is executed:
WS2812_DmaCallback()
One of its main tasks is to disable the Event0 SET action.
The sequence is:
Last bit completed
│
▼
Disable further SET action
│
▼
Output remains LOW
This puts the WS2812 bus into the low-level state required for the reset/latch interval.
It is worth noting that the DMA completion callback occurs once per complete transfer, not once per WS2812 bit, so the CPU is not involved in bit-level waveform generation.
13. WS2812 Reset Pulse
After the complete data frame is transmitted, the WS2812 requires the data line to remain low for a sufficient period so that the new color values can be latched.
In this implementation, a simple delay loop is used:
for (...)
{
__NOP();
}
This generates approximately:
320 µs
of low-level hold time.
The duration is comfortably longer than the reset/latch interval required by the WS2812 implementation used in this example, ensuring that the LED data is properly latched before the next frame is transmitted.
For applications where CPU blocking must also be avoided between frames, this delay could be replaced with an additional hardware timer or SCTimer-based mechanism.
14. Experimental Results
The output waveform was captured using a logic analyzer.
The measured bit timing is approximately:
Logic 0
High: 353 ns
Low: 900 ns
Logic 1
High: 900 ns
Low: 353 ns
Bit Period
≈ 1.25 µs
The measured waveform therefore matches the timing values configured in the SCTimer implementation and provides the required waveform for WS2812 communication.
More importantly, during the entire bitstream transmission, the CPU does not perform any bit-level timing or duty-cycle update.
15. Conclusion
This project demonstrates an efficient method of driving WS2812 RGB LEDs on the MCXN947 by combining SCTimer and eDMA.
The core concept can be summarized as follows:
SCTimer Event0 defines the fixed WS2812 bit period.
SCTimer Event1 determines the high pulse width and generates the DMA request.
eDMA updates MATCHREL with the pulse-width value for the next bit.
MATCHREL is automatically transferred to MATCH at the appropriate reload point.
SCTimer and eDMA cooperate to generate the complete WS2812 bitstream with no CPU involvement at the bit level.
Compared with traditional GPIO bit-banging, interrupt-driven PWM, or SPI encoding approaches, this solution provides several advantages:
✅ Accurate and stable timing
✅ Very low CPU overhead during transmission
✅ Good scalability for longer LED chains
✅ No bit-level interrupt processing
✅ Efficient use of MCXN947 SCTimer capabilities
For applications involving RGB lighting effects, status indicators, HMI lighting, or smart lighting, the combination of SCTimer + eDMA provides a clean and efficient way to drive WS2812 LEDs on the MCXN947.
View full article