ws2812_spi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #ifndef WS2812_SPI_SCK_PAL_MODE
  15. # define WS2812_SPI_SCK_PAL_MODE 5
  16. #endif
  17. // Push Pull or Open Drain Configuration
  18. // Default Push Pull
  19. #ifndef WS2812_EXTERNAL_PULLUP
  20. # if defined(USE_GPIOV1)
  21. # define WS2812_MOSI_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL
  22. # else
  23. # define WS2812_MOSI_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_SPI_MOSI_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL
  24. # endif
  25. #else
  26. # if defined(USE_GPIOV1)
  27. # define WS2812_MOSI_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN
  28. # else
  29. # define WS2812_MOSI_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_SPI_MOSI_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN
  30. # endif
  31. #endif
  32. // Define SPI config speed
  33. // baudrate should target 3.2MHz
  34. // F072 fpclk = 48MHz
  35. // 48/16 = 3Mhz
  36. #if WS2812_SPI_DIVISOR == 2
  37. # define WS2812_SPI_DIVISOR (0)
  38. #elif WS2812_SPI_DIVISOR == 4
  39. # define WS2812_SPI_DIVISOR (SPI_CR1_BR_0)
  40. #elif WS2812_SPI_DIVISOR == 8
  41. # define WS2812_SPI_DIVISOR (SPI_CR1_BR_1)
  42. #elif WS2812_SPI_DIVISOR == 16 // same as default
  43. # define WS2812_SPI_DIVISOR (SPI_CR1_BR_1 | SPI_CR1_BR_0)
  44. #elif WS2812_SPI_DIVISOR == 32
  45. # define WS2812_SPI_DIVISOR (SPI_CR1_BR_2)
  46. #elif WS2812_SPI_DIVISOR == 64
  47. # define WS2812_SPI_DIVISOR (SPI_CR1_BR_2 | SPI_CR1_BR_0)
  48. #elif WS2812_SPI_DIVISOR == 128
  49. # define WS2812_SPI_DIVISOR (SPI_CR1_BR_2 | SPI_CR1_BR_1)
  50. #elif WS2812_SPI_DIVISOR == 256
  51. # define WS2812_SPI_DIVISOR (SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0)
  52. #else
  53. # define WS2812_SPI_DIVISOR (SPI_CR1_BR_1 | SPI_CR1_BR_0) // default
  54. #endif
  55. // Use SPI circular buffer
  56. #ifdef WS2812_SPI_USE_CIRCULAR_BUFFER
  57. # define WS2812_SPI_BUFFER_MODE 1 // circular buffer
  58. #else
  59. # define WS2812_SPI_BUFFER_MODE 0 // normal buffer
  60. #endif
  61. #if defined(USE_GPIOV1)
  62. # define WS2812_SCK_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL
  63. #else
  64. # define WS2812_SCK_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_SPI_SCK_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL
  65. #endif
  66. #define BYTES_FOR_LED_BYTE 4
  67. #define NB_COLORS 3
  68. #define BYTES_FOR_LED (BYTES_FOR_LED_BYTE * NB_COLORS)
  69. #define DATA_SIZE (BYTES_FOR_LED * RGBLED_NUM)
  70. #define RESET_SIZE (1000 * WS2812_TRST_US / (2 * 1250))
  71. #define PREAMBLE_SIZE 4
  72. static uint8_t txbuf[PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE] = {0};
  73. /*
  74. * As the trick here is to use the SPI to send a huge pattern of 0 and 1 to
  75. * the ws2812b protocol, we use this helper function to translate bytes into
  76. * 0s and 1s for the LED (with the appropriate timing).
  77. */
  78. static uint8_t get_protocol_eq(uint8_t data, int pos) {
  79. uint8_t eq = 0;
  80. if (data & (1 << (2 * (3 - pos))))
  81. eq = 0b1110;
  82. else
  83. eq = 0b1000;
  84. if (data & (2 << (2 * (3 - pos))))
  85. eq += 0b11100000;
  86. else
  87. eq += 0b10000000;
  88. return eq;
  89. }
  90. static void set_led_color_rgb(LED_TYPE color, int pos) {
  91. uint8_t* tx_start = &txbuf[PREAMBLE_SIZE];
  92. #if (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_GRB)
  93. for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + j] = get_protocol_eq(color.g, j);
  94. for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + BYTES_FOR_LED_BYTE + j] = get_protocol_eq(color.r, j);
  95. 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);
  96. #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_RGB)
  97. for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + j] = get_protocol_eq(color.r, j);
  98. for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + BYTES_FOR_LED_BYTE + j] = get_protocol_eq(color.g, j);
  99. 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);
  100. #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_BGR)
  101. for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + j] = get_protocol_eq(color.b, j);
  102. for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + BYTES_FOR_LED_BYTE + j] = get_protocol_eq(color.g, j);
  103. for (int j = 0; j < 4; j++) tx_start[BYTES_FOR_LED * pos + BYTES_FOR_LED_BYTE * 2 + j] = get_protocol_eq(color.r, j);
  104. #endif
  105. }
  106. void ws2812_init(void) {
  107. palSetLineMode(RGB_DI_PIN, WS2812_MOSI_OUTPUT_MODE);
  108. #ifdef WS2812_SPI_SCK_PIN
  109. palSetLineMode(WS2812_SPI_SCK_PIN, WS2812_SCK_OUTPUT_MODE);
  110. #endif // WS2812_SPI_SCK_PIN
  111. // TODO: more dynamic baudrate
  112. static const SPIConfig spicfg = {WS2812_SPI_BUFFER_MODE, NULL, PAL_PORT(RGB_DI_PIN), PAL_PAD(RGB_DI_PIN), WS2812_SPI_DIVISOR};
  113. spiAcquireBus(&WS2812_SPI); /* Acquire ownership of the bus. */
  114. spiStart(&WS2812_SPI, &spicfg); /* Setup transfer parameters. */
  115. spiSelect(&WS2812_SPI); /* Slave Select assertion. */
  116. #ifdef WS2812_SPI_USE_CIRCULAR_BUFFER
  117. spiStartSend(&WS2812_SPI, sizeof(txbuf) / sizeof(txbuf[0]), txbuf);
  118. #endif
  119. }
  120. void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) {
  121. static bool s_init = false;
  122. if (!s_init) {
  123. ws2812_init();
  124. s_init = true;
  125. }
  126. for (uint8_t i = 0; i < leds; i++) {
  127. set_led_color_rgb(ledarray[i], i);
  128. }
  129. // Send async - each led takes ~0.03ms, 50 leds ~1.5ms, animations flushing faster than send will cause issues.
  130. // Instead spiSend can be used to send synchronously (or the thread logic can be added back).
  131. #ifndef WS2812_SPI_USE_CIRCULAR_BUFFER
  132. # ifdef WS2812_SPI_SYNC
  133. spiSend(&WS2812_SPI, sizeof(txbuf) / sizeof(txbuf[0]), txbuf);
  134. # else
  135. spiStartSend(&WS2812_SPI, sizeof(txbuf) / sizeof(txbuf[0]), txbuf);
  136. # endif
  137. #endif
  138. }