You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
12 KiB

  1. # WS2812 Driver :id=ws2812-driver
  2. This driver provides support for WorldSemi addressable RGB(W) LEDs, and compatible equivalents:
  3. * WS2811, WS2812, WS2812B, WS2812C, etc.
  4. * SK6812, SK6812MINI, SK6805
  5. These LEDs are often called "addressable" because instead of using a wire per color (and per LED), each LED contains a small microchip that understands a special protocol sent over a single wire.
  6. The LEDs can be chained together, and the remaining data is passed on to the next. In this way, you can easily control the color of many LEDs using a single GPIO.
  7. ## Usage :id=usage
  8. In most cases, the WS2812 driver code is automatically included if you are using either the [RGBLight](feature_rgblight.md) or [RGB Matrix](feature_rgb_matrix.md) feature with the `ws2812` driver set, and you would use those APIs instead.
  9. However, if you need to use the driver standalone, add the following to your `rules.mk`:
  10. ```make
  11. WS2812_DRIVER_REQUIRED = yes
  12. ```
  13. You can then call the WS2812 API by including `ws2812.h` in your code.
  14. ## Basic Configuration :id=basic-configuration
  15. Add the following to your `config.h`:
  16. |Define |Default |Description |
  17. |-------------------|-----------------------|------------------------------------------------------------------------------------------------|
  18. |`WS2812_DI_PIN` |*Not defined* |The GPIO pin connected to the DI pin of the first LED in the chain |
  19. |`WS2812_LED_COUNT` |*Not defined* |Number of LEDs in the WS2812 chain - automatically set when RGBLight or RGB Matrix is configured|
  20. |`WS2812_TIMING` |`1250` |The total length of a bit (TH+TL) in nanoseconds |
  21. |`WS2812_T1H` |`900` |The length of a "1" bit's high phase in nanoseconds |
  22. |`WS2812_T0H` |`350` |The length of a "0" bit's high phase in nanoseconds |
  23. |`WS2812_TRST_US` |`280` |The length of the reset phase in microseconds |
  24. |`WS2812_BYTE_ORDER`|`WS2812_BYTE_ORDER_GRB`|The byte order of the RGB data |
  25. ### Timing Adjustment :id=timing-adjustment
  26. The WS2812 LED communication protocol works by encoding a "1" bit with a long high pulse (T<sub>1</sub>H), and a "0" bit with a shorter pulse (T<sub>0</sub>H). The total cycle length of a bit is the same.
  27. The "reset" pulse (T<sub>RST</sub>) latches the sent RGB data to all of the LEDs and denotes a completed "frame".
  28. Some WS2812 variants have slightly different timing parameter requirements, which can be accounted for if necessary using the above `#define`s in your `config.h`.
  29. ### Byte Order :id=byte-order
  30. Some WS2812 variants may have their color components in a different physical or logical order. For example, the WS2812B-2020 has physically swapped red and green LEDs, which causes the wrong color to be displayed, because the default order of the bytes sent over the wire is defined as GRB.
  31. If you find your LED colors are consistently swapped, you may need to change the byte order by adding the following to your `config.h`:
  32. ```c
  33. #define WS2812_BYTE_ORDER WS2812_BYTE_ORDER_GRB
  34. ```
  35. Where the byte order may be one of:
  36. |Byte Order|Known Devices |
  37. |----------|----------------------------|
  38. |`GRB` |Most WS2812s, SK6812, SK6805|
  39. |`RGB` |WS2812B-2020 |
  40. |`BGR` |TM1812 |
  41. ## Driver Configuration :id=driver-configuration
  42. Driver selection can be configured in `rules.mk` as `WS2812_DRIVER`, or in `info.json` as `ws2812.driver`. Valid values are `bitbang` (default), `i2c`, `spi`, `pwm`, `vendor`, or `custom`. See below for information on individual drivers.
  43. ### Bitbang Driver :id=bitbang-driver
  44. This is the default WS2812 driver. It operates by "bit-banging" ie. directly toggling the GPIO.
  45. Please note that on AVR devices, due to the tight timing requirements longer chains and/or heavy CPU loads may cause visible lag. Unfortunately this driver is usually the only option for AVR.
  46. ```make
  47. WS2812_DRIVER = bitbang
  48. ```
  49. ### I2C Driver :id=i2c-driver
  50. A specialized driver mainly used for PS2AVRGB (Bootmapper Client) boards, which possess an ATtiny85 that handles the WS2812 LEDs.
  51. ```make
  52. WS2812_DRIVER = i2c
  53. ```
  54. The following `#define`s apply only to the `i2c` driver:
  55. |Define |Default|Description |
  56. |--------------------|-------|---------------------------------|
  57. |`WS2812_I2C_ADDRESS`|`0xB0` |The I2C address of the ATtiny85. |
  58. |`WS2812_I2C_TIMEOUT`|`100` |The I2C timeout, in milliseconds.|
  59. ### PIO Driver :id=pio-driver
  60. This driver is RP2040-only, and leverages the onboard PIO (programmable I/O) system and DMA to offload processing from the CPU.
  61. The WS2812 PIO program uses one state machine, six instructions and one DMA interrupt handler callback. Due to the implementation the time resolution for this driver is 50 ns - any value not specified in this interval will be rounded to the next matching interval.
  62. ```make
  63. WS2812_DRIVER = vendor
  64. ```
  65. ### PWM Driver :id=pwm-driver
  66. This driver is ARM-only, and leverages the onboard PWM peripheral and DMA to offload processing from the CPU.
  67. ```make
  68. WS2812_DRIVER = pwm
  69. ```
  70. ### SPI Driver :id=spi-driver
  71. This driver is ARM-only, and leverages the onboard SPI peripheral and DMA to offload processing from the CPU. The DI pin **must** be connected to the MOSI pin on the MCU, and all other SPI pins **must** be left unused. This is also very dependent on your MCU's SPI peripheral clock speed, and may or may not be possible depending on the MCU selected.
  72. ```make
  73. WS2812_DRIVER = spi
  74. ```
  75. ## ChibiOS/ARM Configuration :id=arm-configuration
  76. The following defines apply only to ARM devices:
  77. |Define |Default |Description |
  78. |------------|------------------------------|---------------------------------------------------------------------------------|
  79. |`WS2812_T1L`|`(WS2812_TIMING - WS2812_T1H)`|The length of a "1" bit's low phase in nanoseconds (bitbang and PIO drivers only)|
  80. |`WS2812_T0L`|`(WS2812_TIMING - WS2812_T0H)`|The length of a "0" bit's low phase in nanoseconds (bitbang and PIO drivers only)|
  81. ### Push-Pull and Open Drain :id=push-pull-open-drain
  82. By default, the GPIO used for data transmission is configured as a *push-pull* output, meaning the pin is effectively always driven either to VCC or to ground.
  83. For situations where the logic level voltage is lower than the power supply voltage, however, this can pose an issue. The solution is to configure the pin for *open drain* mode instead, and use a pullup resistor between the DI pin and VCC. In this mode, the MCU can only pull the GPIO *low*, or leave it floating. The pullup resistor is then responsible for pulling the line high, when the MCU is not driving the GPIO.
  84. To configure the DI pin for open drain configuration, add the following to your `config.h`:
  85. ```c
  86. #define WS2812_EXTERNAL_PULLUP
  87. ```
  88. ### SPI Driver :id=arm-spi-driver
  89. Depending on the ChibiOS board configuration, you may need to enable SPI at the keyboard level. For STM32, this would look like:
  90. `halconf.h`:
  91. ```c
  92. #define HAL_USE_SPI TRUE
  93. ```
  94. `mcuconf.h`:
  95. ```c
  96. #undef STM32_SPI_USE_SPI1
  97. #define STM32_SPI_USE_SPI1 TRUE
  98. ```
  99. The following `define`s apply only to the `spi` driver:
  100. |Define |Default |Description |
  101. |--------------------------------|-------------|-------------------------------------------------------------------------------|
  102. |`WS2812_SPI_DRIVER` |`SPID1` |The SPI driver to use |
  103. |`WS2812_SPI_MOSI_PAL_MODE` |`5` |The MOSI pin alternative function to use |
  104. |`WS2812_SPI_SCK_PIN` |*Not defined*|The SCK pin - required for F072 and possibly others |
  105. |`WS2812_SPI_SCK_PAL_MODE` |`5` |The SCK pin alternative function to use - required for F072 and possibly others|
  106. |`WS2812_SPI_DIVISOR` |`16` |The divisor used to adjust the baudrate |
  107. |`WS2812_SPI_USE_CIRCULAR_BUFFER`|*Not defined*|Enable a circular buffer for improved rendering |
  108. #### Setting the Baudrate :id=arm-spi-baudrate
  109. To adjust the SPI baudrate, you will need to derive the target baudrate from the clock tree provided by STM32CubeMX, and add the following to your `config.h`:
  110. ```c
  111. #define WS2812_SPI_DIVISOR 16
  112. ```
  113. Only divisors of 2, 4, 8, 16, 32, 64, 128 and 256 are supported on STM32 devices. Other MCUs may have similar constraints -- check the reference manual for your respective MCU for specifics.
  114. #### Circular Buffer :id=arm-spi-circular-buffer
  115. A circular buffer can be enabled if you experience flickering.
  116. To enable the circular buffer, add the following to your `config.h`:
  117. ```c
  118. #define WS2812_SPI_USE_CIRCULAR_BUFFER
  119. ```
  120. ### PIO Driver :id=arm-pio-driver
  121. The following `#define`s apply only to the PIO driver:
  122. |Define |Default |Description |
  123. |---------------------|-------------|---------------------------------------|
  124. |`WS2812_PIO_USE_PIO1`|*Not defined*|Use the PIO1 peripheral instead of PIO0|
  125. ### PWM Driver :id=arm-pwm-driver
  126. Depending on the ChibiOS board configuration, you may need to enable PWM at the keyboard level. For STM32, this would look like:
  127. `halconf.h`:
  128. ```c
  129. #define HAL_USE_PWM TRUE
  130. ```
  131. `mcuconf.h`:
  132. ```c
  133. #undef STM32_PWM_USE_TIM2
  134. #define STM32_PWM_USE_TIM2 TRUE
  135. ```
  136. The following `#define`s apply only to the `pwm` driver:
  137. |Define |Default |Description |
  138. |---------------------------------|--------------------|------------------------------------------------------------------------------------------|
  139. |`WS2812_PWM_DRIVER` |`PWMD2` |The PWM driver to use |
  140. |`WS2812_PWM_CHANNEL` |`2` |The PWM channel to use |
  141. |`WS2812_PWM_PAL_MODE` |`2` |The pin alternative function to use |
  142. |`WS2812_DMA_STREAM` |`STM32_DMA1_STREAM2`|The DMA Stream for `TIMx_UP` |
  143. |`WS2812_DMA_CHANNEL` |`2` |The DMA Channel for `TIMx_UP` |
  144. |`WS2812_DMAMUX_ID` |*Not defined* |The DMAMUX configuration for `TIMx_UP` - only required if your MCU has a DMAMUX peripheral|
  145. |`WS2812_PWM_COMPLEMENTARY_OUTPUT`|*Not defined* |Whether the PWM output is complementary (`TIMx_CHyN`) |
  146. ?> Using a complementary timer output (`TIMx_CHyN`) is possible only for advanced-control timers (1, 8 and 20 on STM32), and the `STM32_PWM_USE_ADVANCED` option in `mcuconf.h` must be set to `TRUE`. Complementary outputs of general-purpose timers are not supported due to ChibiOS limitations.
  147. ## API :id=api
  148. ### `void ws2812_setleds(rgb_led_t *ledarray, uint16_t number_of_leds)` :id=api-ws2812-setleds
  149. Send RGB data to the WS2812 LED chain.
  150. #### Arguments :id=api-ws2812-setleds-arguments
  151. - `rgb_led_t *ledarray`
  152. A pointer to the LED array.
  153. - `uint16_t number_of_leds`
  154. The length of the LED array.