Crazy-Geeks / STM32-ARGB-DMA

STM32 Library for Addressable LEDs: WS2812(b), WS2811, SK6812, etc. Supports RGB & RGBW LEDs.
MIT License
84 stars 25 forks source link
argb cubemx dma led peripheral pwm rgb sk6812 stm32 stm32f103 tim timer timer-channel ws2811 ws2812 ws2812b ws2813

STM32-ARGB-DMA

Fastest and simplest library for ARGB LEDs: WS28xx and SK68xx Series RGB or RGBW for STM32 Series.
Uses DMA Interrupts and PWM to control LED Strip

Banner

RU Description

RU Habr

Features:

Limitations

Lib settings

#define WS2812       // Family: {WS2811S, WS2811F, WS2812, SK6812}
// WS2811S — RGB, 400kHz;
// WS2811F — RGB, 800kHz;
// WS2812  — GRB, 800kHz;
// SK6812  — RGBW, 800kHz

#define NUM_PIXELS 5 // Pixel quantity

#define USE_GAMMA_CORRECTION 1 // Gamma-correction should fix red&green, try for yourself

#define TIM_NUM    2  // Timer number
#define TIM_CH     TIM_CHANNEL_2  // Timer's PWM channel
#define DMA_HANDLE hdma_tim2_ch2_ch4  // DMA Channel
#define DMA_SIZE_WORD     // DMA Memory Data Width: {.._BYTE, .._HWORD, .._WORD}
// DMA channel can be found in main.c / tim.c

Function reference (from .h file):

// API enum status
typedef enum ARGB_STATE {
    ARGB_BUSY = 0,      // DMA Transfer in progress
    ARGB_READY = 1,     // DMA Ready to transfer
    ARGB_OK = 2,        // Function execution success
    ARGB_PARAM_ERR = 3, // Error in input parameters
} ARGB_STATE;

void ARGB_Init(void);   // Initialization
void ARGB_Clear(void);  // Clear strip

void ARGB_SetBrightness(u8_t br); // Set global brightness

void ARGB_SetRGB(u16_t i, u8_t r, u8_t g, u8_t b);  // Set single LED by RGB
void ARGB_SetHSV(u16_t i, u8_t hue, u8_t sat, u8_t val); // Set single LED by HSV
void ARGB_SetWhite(u16_t i, u8_t w); // Set white component in LED (RGBW)

void ARGB_FillRGB(u8_t r, u8_t g, u8_t b); // Fill all strip with RGB color
void ARGB_FillHSV(u8_t hue, u8_t sat, u8_t val); // Fill all strip with HSV color
void ARGB_FillWhite(u8_t w); // Fill all strip's white component (RGBW)

ARGB_STATE ARGB_Ready(void); // Get DMA Ready state
ARGB_STATE ARGB_Show(void); // Push data to the strip

Connection

Connection

Instructions for use:

Also available in PDF (RU/EN)

  • Use CubeMX to configure clocks and peripheral.
  • Enable PWM Generation for your preferred timer channel.
  • PWM Mode 1, OC Preload: Enable, Fast Mode: Disable, CH Polarity: High
  • Enable DMA for your timer channel with "Memory To Peripheral" direction.
  • Set DMA mode to Circular, Data Width to Word/Byte, Increment Address checkbox only for Memory.
  • Set GPIO Speed to the Maximum, use Open Drain or Push Pull Mode - details in Troubleshooting.
  • Save CubeMX .ioc file and generate code.
  • Add library to your source destination and add #include in your code.
  • In main.c file search for your DMA Handler
    /* Private variables */
    TIM_HandleTypeDef htim2;
    DMA_HandleTypeDef hdma_tim2_ch2_ch4;  <-- THIS
  • Add this handler in ARGB.h file in DMA HANDLE define.
  • Set other defines with your settings.
  • Now we're ready to go!

TROUBLESHOOTING

Suggestions

Special thanks

NarodStream, VFD

Donate options