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.

102 lines
3.3 KiB

  1. #include "quantum.h"
  2. #include "ws2812.h"
  3. /* Adapted from https://github.com/gamazeps/ws2812b-chibios-SPIDMA/ */
  4. #ifdef RGBW
  5. # error "RGBW not supported"
  6. #endif
  7. // Define the spi your LEDs are plugged to here
  8. #ifndef WS2812_SPI
  9. # define WS2812_SPI SPID1
  10. #endif
  11. #ifndef WS2812_SPI_MOSI_PAL_MODE
  12. # define WS2812_SPI_MOSI_PAL_MODE 5
  13. #endif
  14. // Push Pull or Open Drain Configuration
  15. // Default Push Pull
  16. #ifndef WS2812_EXTERNAL_PULLUP
  17. # if defined(USE_GPIOV1)
  18. # define WS2812_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL
  19. # else
  20. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_SPI_MOSI_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL
  21. # endif
  22. #else
  23. # if defined(USE_GPIOV1)
  24. # define WS2812_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN
  25. # else
  26. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_SPI_MOSI_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN
  27. # endif
  28. #endif
  29. #define BYTES_FOR_LED_BYTE 4
  30. #define NB_COLORS 3
  31. #define BYTES_FOR_LED (BYTES_FOR_LED_BYTE * NB_COLORS)
  32. #define DATA_SIZE (BYTES_FOR_LED * RGBLED_NUM)
  33. #define RESET_SIZE (1000 * WS2812_TRST_US / (2 * 1250))
  34. #define PREAMBLE_SIZE 4
  35. static uint8_t txbuf[PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE] = {0};
  36. /*
  37. * As the trick here is to use the SPI to send a huge pattern of 0 and 1 to
  38. * the ws2812b protocol, we use this helper function to translate bytes into
  39. * 0s and 1s for the LED (with the appropriate timing).
  40. */
  41. static uint8_t get_protocol_eq(uint8_t data, int pos) {
  42. uint8_t eq = 0;
  43. if (data & (1 << (2 * (3 - pos))))
  44. eq = 0b1110;
  45. else
  46. eq = 0b1000;
  47. if (data & (2 << (2 * (3 - pos))))
  48. eq += 0b11100000;
  49. else
  50. eq += 0b10000000;
  51. return eq;
  52. }
  53. static void set_led_color_rgb(LED_TYPE color, int pos) {
  54. uint8_t* tx_start = &txbuf[PREAMBLE_SIZE];
  55. for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + j] = get_protocol_eq(color.g, j);
  56. for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + BYTES_FOR_LED_BYTE + j] = get_protocol_eq(color.r, j);
  57. for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + BYTES_FOR_LED_BYTE * 2 + j] = get_protocol_eq(color.b, j);
  58. }
  59. void ws2812_init(void) {
  60. palSetLineMode(RGB_DI_PIN, WS2812_OUTPUT_MODE);
  61. // TODO: more dynamic baudrate
  62. static const SPIConfig spicfg = {
  63. 0, NULL, PAL_PORT(RGB_DI_PIN), PAL_PAD(RGB_DI_PIN),
  64. SPI_CR1_BR_1 | SPI_CR1_BR_0 // baudrate : fpclk / 8 => 1tick is 0.32us (2.25 MHz)
  65. };
  66. spiAcquireBus(&WS2812_SPI); /* Acquire ownership of the bus. */
  67. spiStart(&WS2812_SPI, &spicfg); /* Setup transfer parameters. */
  68. spiSelect(&WS2812_SPI); /* Slave Select assertion. */
  69. }
  70. void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) {
  71. static bool s_init = false;
  72. if (!s_init) {
  73. ws2812_init();
  74. s_init = true;
  75. }
  76. for (uint8_t i = 0; i < leds; i++) {
  77. set_led_color_rgb(ledarray[i], i);
  78. }
  79. // Send async - each led takes ~0.03ms, 50 leds ~1.5ms, animations flushing faster than send will cause issues.
  80. // Instead spiSend can be used to send synchronously (or the thread logic can be added back).
  81. #ifdef WS2812_SPI_SYNC
  82. spiSend(&WS2812_SPI, sizeof(txbuf) / sizeof(txbuf[0]), txbuf);
  83. #else
  84. spiStartSend(&WS2812_SPI, sizeof(txbuf) / sizeof(txbuf[0]), txbuf);
  85. #endif
  86. }