Code Monkey home page Code Monkey logo

stm32-ws2811-ws2812-ws2812b-ws281x-tim-pwm-dma-timer's Introduction

WS2811 and WS2812B driver for STM32 with TIM, PWM and DMA

This application note aims to explain architecture and understanding how to develop driver for addressable LEDs, such as WS2811, WS2812, WS2812B or any other matching protocol specifications.

Table of Contents

Github supports ToC by default. It is available in the top-left corner of this document.

Abbreviations

  • DMA: Direct Memory Access controller in STM32
  • TIM: Timer peripheral, general or advanced, w/ or w/o external channel (output compare or input capture)
  • PWM: Pulse-Width-Modulation generated by TIM peripheral
  • HT: Half-Transfer Complete DMA event/flag
  • TC: Transfer Complete DMA event/flag
  • IRQ: Interrupt

Understanding LED protocol

WS2811 and WS2812 protocol is specific one and has defined values:

  • Transfer rate is 800 kHz, or 1.25us pulse length for each bit
  • Transfer length is 24 pulses for each led, that's 30us for one LED
  • Each logical bit (1 or 0) consists of high and low part, with different length
  • Reset pulse is needed prior updating led strip, to synchronize sequence

WS2811 & WS2812 & WS2812B LED protocol

Minimum reset pulse length depends on WS281x device. Check datasheet for your particular unit. WS2812B says > 50us, while WS2811 says > 280us.

STM32 TIM and PWM

Several STM32 timers (TIM) have support for Capture/Compare channels, connect to output pins, and being able to generate PWM pulses. According to protocol (also explained above), timer should be able to generate 3 type of pulses:

  • All-time zero to emulate reset pulse
  • Logical bit 1 with ~3/4 of time being high and low for the rest of the time
  • Logical bit 0 with ~1/4 of time being high and low for the rest of the time

Furthemore, timer should be configured to generate update event for 800 kHz, with auto-reload register (TIMx->ARR) set to TIMx->ARR = timer_kernel_clock / 800000 - 1 and prescaler = 0 for maximum resolution.

Timer does not support a look-up table to know if logical bit 1 or 0 is next, hence we will utilize DMA feature to implement this task. Timer channel allows DMA requests to load next value to channel compare register, and effectively implement look-up table with data transfer from memory to timer peripheral.

STM32 Timer Cookbook is a great starting point to understand how timers work in STM32s.

STM32 DMA

DMA controllers in STM32s support various operations, one of them being super handy for our WS LED driver, called circular operation mode. Circular mode will continuously transmit data from memory to peripheral (or, in general, can also go opposite direction) and periodically send transfer-complete or half-transfer-complete interrupts to the application.

STM32 DMA circular mode

We will use HT and TC events extensively, as they will be use to prepare data for next operations to transfer all bits for all leds. More explained in the later sections.

List of some useful STM32 DMA application notes

Memory requirement

Memory requirement for one LED strip is split to:

  • 3 * leds_count to store read, green & blue colors for each led
  • 1 working buffer with size of 2 leds (48 elements), each element of 16/32 bits, depending on used TIM peripheral

Putting it all together

To put all together, application developer must:

  • Decide to use one timer with PWM generation support with one channel
  • Select one DMA channel/stream/dmamux (depends on STM32) for selected timer and channel
  • Put DMA in circular mode and enable HT and TC interrupts
  • Use HT and TC events to prepare data for next operation
    • HT event is used to prepare data at the beginning of memory (part of memory just completed with transmision)
    • TC event is used to prepare data for second part of memory

Examples are extensively commented and should provide necessary understanding for application development

Examples

Examples can be used as reference code to implement your own LED driver with your own STM32.

  • Developed in STM32CubeIDE for easier evaluation on STM32 boards
  • Supports CMake and VSCode development
  • Uses LL or HAL drivers
  • Demos for various STM32
STM32 family Board name TIM & CH GPIO DMA settings
STM32G0xx NUCLEO-G0B1RE TIM2 CH4 PA3 DMA2, Channel 5

stm32-ws2811-ws2812-ws2812b-ws281x-tim-pwm-dma-timer's People

Contributors

enrico1036 avatar majerle avatar matstm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

stm32-ws2811-ws2812-ws2812b-ws281x-tim-pwm-dma-timer's Issues

Reset Pulse for newer WS2812B

Hi,

I just implemented a control for the WS2812B LEDs based on Your tutorial (thanks btw. it was very helpful :)). At first I could not make the WS2812B LEDs work and therefore did a bit of research. It turned out that the specification changed for newer models of the LEDs. Especially the reset pulse is now required to take at least 280 us. Check out: http://www.world-semi.com/Certifications/WS2812B.html for the newest datasheet. With an adjustment of the reset pulse duration the LEDs light up now. Hope that helps anyone.

Stm32u5 port.

Hello,
I tried to port the library to a stm32u5 and I haven't managed to get the library to control the LEDs at the moment.
I prepared a branch: stm32u5 (WIP)

  • Timer 2, channel 4
  • GPDMA channel 0

I have no signal on the output. DMA interrupts are occurring as expected.
Anyone have any clues as to what's wrong or missing for this family?
Regards

St32f072c8t - usart

Iโ€™m on St32f072c8t.
I can use only one of these 2 pin to control a ws2812 led strip:
PA14 (Alternate functions Usart2_tx and swclk)
or PA12 (Alternate functions can_tx, usart_rts, tim1_etr, comp2_out, tsc_g4_io4, eventout, usb_dp)
Can I use this library? I suppose no, but Iโ€™m a newbie ๐Ÿ˜
Any hint for a beginner?

LED order wrong for second and thrid LED

Hi!

Unfortunately I haven't had time to test the code in real hardware, but it seems, there is an error in the data loading for the third LED. As I understand the code, when the first reset pulse is done, the first half of the buffer is set (main.c:407), the current_led is incremented, the second half is also filled later and the DMA is initailzed (main.c:421).

After the first HC interrupt the current led is incremented again to the third LED (current_led == 2, after line 389), but the data filled in the second half of the buffer (line 409) because this is only a HC interrupt.

Is there something wrong with my logic? I'll test the code and as soon as I can to confirm it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.