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.

189 lines
9.4 KiB

  1. # WS2812 Driver
  2. This driver powers the [RGB Lighting](feature_rgblight.md) and [RGB Matrix](feature_rgb_matrix.md) features.
  3. Currently QMK supports the following addressable LEDs (however, the white LED in RGBW variants is not supported):
  4. WS2811, WS2812, WS2812B, WS2812C, etc.
  5. SK6812, SK6812MINI, SK6805
  6. These LEDs are called "addressable" because instead of using a wire per color, each LED contains a small microchip that understands a special protocol sent over a single wire. The chip passes on the remaining data to the next LED, allowing them to be chained together. In this way, you can easily control the color of the individual LEDs.
  7. ## Supported Driver Types
  8. | | AVR | ARM |
  9. | -------- | ------------------ | ------------------ |
  10. | bit bang | :heavy_check_mark: | :heavy_check_mark: |
  11. | I2C | :heavy_check_mark: | |
  12. | SPI | | :heavy_check_mark: |
  13. | PWM | | :heavy_check_mark: |
  14. | PIO | | :heavy_check_mark: |
  15. ## Driver configuration
  16. ### All drivers
  17. Different versions of the addressable LEDs have differing requirements for the T<sub>RST</sub> period between frames.
  18. The default setting is 280 ยตs, which should work for most cases, but this can be overridden in your config.h. e.g.:
  19. ```c
  20. #define WS2812_TRST_US 80
  21. ```
  22. #### Byte Order
  23. Some variants of the WS2812 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.
  24. In this case, you can change the byte order by defining `WS2812_BYTE_ORDER` as one of the following values:
  25. | Byte order | Known devices |
  26. | --------------------------------- | ----------------------------- |
  27. | `WS2812_BYTE_ORDER_GRB` (default) | Most WS2812's, SK6812, SK6805 |
  28. | `WS2812_BYTE_ORDER_RGB` | WS2812B-2020 |
  29. | `WS2812_BYTE_ORDER_BGR` | TM1812 |
  30. ### Bitbang
  31. Default driver, the absence of configuration assumes this driver. To configure it, add this to your rules.mk:
  32. ```make
  33. WS2812_DRIVER = bitbang
  34. ```
  35. !> This driver is not hardware accelerated and may not be performant on heavily loaded systems.
  36. #### Adjusting bit timings
  37. The WS2812 LED communication topology depends on a serialized timed window. Different versions of the addressable LEDs have differing requirements for the timing parameters, for instance, of the SK6812.
  38. You can tune these parameters through the definition of the following macros:
  39. | Macro | Default | AVR | ARM |
  40. | --------------- | ---------------------------- | ------------------ | ------------------ |
  41. | `WS2812_TIMING` | `1250` | :heavy_check_mark: | :heavy_check_mark: |
  42. | `WS2812_T0H` | `350` | :heavy_check_mark: | :heavy_check_mark: |
  43. | `WS2812_T0L` | `WS2812_TIMING - WS2812_T0H` | | :heavy_check_mark: |
  44. | `WS2812_T1H` | `900` | :heavy_check_mark: | :heavy_check_mark: |
  45. | `WS2812_T1L` | `WS2812_TIMING - WS2812_T1H` | | :heavy_check_mark: |
  46. ### I2C
  47. Targeting boards where WS2812 support is offloaded to a 2nd MCU. Currently the driver is limited to AVR given the known consumers are ps2avrGB/BMC. To configure it, add this to your rules.mk:
  48. ```make
  49. WS2812_DRIVER = i2c
  50. ```
  51. Configure the hardware via your config.h:
  52. ```c
  53. #define WS2812_I2C_ADDRESS 0xB0 // default: 0xB0
  54. #define WS2812_I2C_TIMEOUT 100 // default: 100
  55. ```
  56. ### SPI
  57. Targeting STM32 boards where WS2812 support is offloaded to an SPI hardware device. The advantage is that the use of DMA offloads processing of the WS2812 protocol from the MCU. `WS2812_DI_PIN` for this driver is the configured SPI MOSI pin. Due to the nature of repurposing SPI to drive the LEDs, the other SPI pins, MISO and SCK, **must** remain unused. To configure it, add this to your rules.mk:
  58. ```make
  59. WS2812_DRIVER = spi
  60. ```
  61. Configure the hardware via your config.h:
  62. ```c
  63. #define WS2812_SPI SPID1 // default: SPID1
  64. #define WS2812_SPI_MOSI_PAL_MODE 5 // MOSI pin "alternate function", see the respective datasheet for the appropriate values for your MCU. default: 5
  65. #define WS2812_SPI_SCK_PIN B3 // Required for F072, may be for others -- SCK pin, see the respective datasheet for the appropriate values for your MCU. default: unspecified
  66. #define WS2812_SPI_SCK_PAL_MODE 5 // SCK pin "alternate function", see the respective datasheet for the appropriate values for your MCU. default: 5
  67. ```
  68. You must also turn on the SPI feature in your halconf.h and mcuconf.h
  69. #### Circular Buffer Mode
  70. Some boards may flicker while in the normal buffer mode. To fix this issue, circular buffer mode may be used to rectify the issue.
  71. By default, the circular buffer mode is disabled.
  72. To enable this alternative buffer mode, place this into your `config.h` file:
  73. ```c
  74. #define WS2812_SPI_USE_CIRCULAR_BUFFER
  75. ```
  76. #### Setting baudrate with divisor
  77. To adjust the baudrate at which the SPI peripheral is configured, users will need to derive the target baudrate from the clock tree provided by STM32CubeMX.
  78. Only divisors of 2, 4, 8, 16, 32, 64, 128 and 256 are supported by hardware.
  79. | Define | Default | Description |
  80. | -------------------- | ------- | ----------------------------------- |
  81. | `WS2812_SPI_DIVISOR` | `16` | SPI source clock peripheral divisor |
  82. #### Testing Notes
  83. While not an exhaustive list, the following table provides the scenarios that have been partially validated:
  84. | | SPI1 | SPI2 | SPI3 |
  85. | ---- | ------------------------------------------- | --------------------------------------- | --------------------- |
  86. | f072 | ? | B15 :heavy_check_mark: (needs SCK: B13) | N/A |
  87. | f103 | A7 :heavy_check_mark: | B15 :heavy_check_mark: | N/A |
  88. | f303 | A7 :heavy_check_mark: B5 :heavy_check_mark: | B15 :heavy_check_mark: | B5 :heavy_check_mark: |
  89. *Other supported ChibiOS boards and/or pins may function, it will be highly chip and configuration dependent.*
  90. ### PWM
  91. Targeting STM32 boards where WS2812 support is offloaded to an PWM timer and DMA stream. The advantage is that the use of DMA offloads processing of the WS2812 protocol from the MCU. To configure it, add this to your rules.mk:
  92. ```make
  93. WS2812_DRIVER = pwm
  94. ```
  95. Configure the hardware via your config.h:
  96. ```c
  97. #define WS2812_PWM_DRIVER PWMD2 // default: PWMD2
  98. #define WS2812_PWM_CHANNEL 2 // default: 2
  99. #define WS2812_PWM_PAL_MODE 2 // Pin "alternate function", see the respective datasheet for the appropriate values for your MCU. default: 2
  100. //#define WS2812_PWM_COMPLEMENTARY_OUTPUT // Define for a complementary timer output (TIMx_CHyN); omit for a normal timer output (TIMx_CHy).
  101. #define WS2812_DMA_STREAM STM32_DMA1_STREAM2 // DMA Stream for TIMx_UP, see the respective reference manual for the appropriate values for your MCU.
  102. #define WS2812_DMA_CHANNEL 2 // DMA Channel for TIMx_UP, see the respective reference manual for the appropriate values for your MCU.
  103. #define WS2812_DMAMUX_ID STM32_DMAMUX1_TIM2_UP // DMAMUX configuration for TIMx_UP -- only required if your MCU has a DMAMUX peripheral, see the respective reference manual for the appropriate values for your MCU.
  104. ```
  105. Note that using a complementary timer output (TIMx_CHyN) is possible only for advanced-control timers (TIM1, TIM8, TIM20 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.
  106. You must also turn on the PWM feature in your halconf.h and mcuconf.h
  107. #### Testing Notes
  108. While not an exhaustive list, the following table provides the scenarios that have been partially validated:
  109. | | Status |
  110. | --------- | ------------------ |
  111. | f072 | ? |
  112. | f103 | :heavy_check_mark: |
  113. | f303 | :heavy_check_mark: |
  114. | f401/f411 | :heavy_check_mark: |
  115. *Other supported ChibiOS boards and/or pins may function, it will be highly chip and configuration dependent.*
  116. ### PIO
  117. Targeting Raspberry Pi RP2040 boards only where WS2812 support is offloaded to an dedicated PIO implementation. This offloads processing of the WS2812 protocol from the MCU to a dedicated PIO program using DMA transfers.
  118. To configure it, add this to your rules.mk:
  119. ```make
  120. WS2812_DRIVER = vendor
  121. ```
  122. You may optionally switch the PIO peripheral used with the following define in config.h:
  123. ```c
  124. #define WS2812_PIO_USE_PIO1 // Force the usage of PIO1 peripheral, by default the WS2812 implementation uses the PIO0 peripheral
  125. ```
  126. The WS2812 PIO programm uses 1 state machine, 6 instructions and one DMA interrupt handler callback. Due to the implementation the time resolution for this drivers is 50ns, any value not specified in this interval will be rounded to the next matching interval.
  127. ### Push Pull and Open Drain Configuration
  128. The default configuration is a push pull on the defined pin.
  129. This can be configured for bitbang, PWM and SPI.
  130. Note: This only applies to STM32 boards.
  131. To configure the `WS2812_DI_PIN` to open drain configuration add this to your config.h file:
  132. ```c
  133. #define WS2812_EXTERNAL_PULLUP
  134. ```