ws2812_pwm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "ws2812.h"
  2. #include "quantum.h"
  3. #include "hal.h"
  4. /* Adapted from https://github.com/joewa/WS2812-LED-Driver_ChibiOS/ */
  5. #ifdef RGBW
  6. # error "RGBW not supported"
  7. #endif
  8. #ifndef WS2812_PWM_DRIVER
  9. # define WS2812_PWM_DRIVER PWMD2 // TIMx
  10. #endif
  11. #ifndef WS2812_PWM_CHANNEL
  12. # define WS2812_PWM_CHANNEL 2 // Channel
  13. #endif
  14. #ifndef WS2812_PWM_PAL_MODE
  15. # define WS2812_PWM_PAL_MODE 2 // DI Pin's alternate function value
  16. #endif
  17. #ifndef WS2812_DMA_STREAM
  18. # define WS2812_DMA_STREAM STM32_DMA1_STREAM2 // DMA Stream for TIMx_UP
  19. #endif
  20. #ifndef WS2812_DMA_CHANNEL
  21. # define WS2812_DMA_CHANNEL 2 // DMA Channel for TIMx_UP
  22. #endif
  23. #ifndef WS2812_PWM_TARGET_PERIOD
  24. //# define WS2812_PWM_TARGET_PERIOD 800000 // Original code is 800k...?
  25. # define WS2812_PWM_TARGET_PERIOD 80000 // TODO: work out why 10x less on f303/f4x1
  26. #endif
  27. /* --- PRIVATE CONSTANTS ---------------------------------------------------- */
  28. #define WS2812_PWM_FREQUENCY (STM32_SYSCLK / 2) /**< Clock frequency of PWM, must be valid with respect to system clock! */
  29. #define WS2812_PWM_PERIOD (WS2812_PWM_FREQUENCY / WS2812_PWM_TARGET_PERIOD) /**< Clock period in ticks. 1 / 800kHz = 1.25 uS (as per datasheet) */
  30. /**
  31. * @brief Number of bit-periods to hold the data line low at the end of a frame
  32. *
  33. * The reset period for each frame must be at least 50 uS; so we add in 50 bit-times
  34. * of zeroes at the end. (50 bits)*(1.25 uS/bit) = 62.5 uS, which gives us some
  35. * slack in the timing requirements
  36. */
  37. #define WS2812_RESET_BIT_N (50)
  38. #define WS2812_COLOR_BIT_N (RGBLED_NUM * 24) /**< Number of data bits */
  39. #define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N) /**< Total number of bits in a frame */
  40. /**
  41. * @brief High period for a zero, in ticks
  42. *
  43. * Per the datasheet:
  44. * WS2812:
  45. * - T0H: 200 nS to 500 nS, inclusive
  46. * - T0L: 650 nS to 950 nS, inclusive
  47. * WS2812B:
  48. * - T0H: 200 nS to 500 nS, inclusive
  49. * - T0L: 750 nS to 1050 nS, inclusive
  50. *
  51. * The duty cycle is calculated for a high period of 350 nS.
  52. */
  53. #define WS2812_DUTYCYCLE_0 (WS2812_PWM_FREQUENCY / (1000000000 / 350))
  54. /**
  55. * @brief High period for a one, in ticks
  56. *
  57. * Per the datasheet:
  58. * WS2812:
  59. * - T1H: 550 nS to 850 nS, inclusive
  60. * - T1L: 450 nS to 750 nS, inclusive
  61. * WS2812B:
  62. * - T1H: 750 nS to 1050 nS, inclusive
  63. * - T1L: 200 nS to 500 nS, inclusive
  64. *
  65. * The duty cycle is calculated for a high period of 800 nS.
  66. * This is in the middle of the specifications of the WS2812 and WS2812B.
  67. */
  68. #define WS2812_DUTYCYCLE_1 (WS2812_PWM_FREQUENCY / (1000000000 / 800))
  69. /* --- PRIVATE MACROS ------------------------------------------------------- */
  70. /**
  71. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given bit
  72. *
  73. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  74. * @param[in] byte: The byte number [0, 2]
  75. * @param[in] bit: The bit number [0, 7]
  76. *
  77. * @return The bit index
  78. */
  79. #define WS2812_BIT(led, byte, bit) (24 * (led) + 8 * (byte) + (7 - (bit)))
  80. /**
  81. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  82. *
  83. * @note The red byte is the middle byte in the color packet
  84. *
  85. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  86. * @param[in] bit: The bit number [0, 7]
  87. *
  88. * @return The bit index
  89. */
  90. #define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  91. /**
  92. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  93. *
  94. * @note The red byte is the first byte in the color packet
  95. *
  96. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  97. * @param[in] bit: The bit number [0, 7]
  98. *
  99. * @return The bit index
  100. */
  101. #define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  102. /**
  103. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  104. *
  105. * @note The red byte is the last byte in the color packet
  106. *
  107. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  108. * @param[in] bit: The bit index [0, 7]
  109. *
  110. * @return The bit index
  111. */
  112. #define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  113. /* --- PRIVATE VARIABLES ---------------------------------------------------- */
  114. static uint32_t ws2812_frame_buffer[WS2812_BIT_N + 1]; /**< Buffer for a frame */
  115. /* --- PUBLIC FUNCTIONS ----------------------------------------------------- */
  116. /*
  117. * Gedanke: Double-buffer type transactions: double buffer transfers using two memory pointers for
  118. the memory (while the DMA is reading/writing from/to a buffer, the application can
  119. write/read to/from the other buffer).
  120. */
  121. void ws2812_init(void) {
  122. // Initialize led frame buffer
  123. uint32_t i;
  124. for (i = 0; i < WS2812_COLOR_BIT_N; i++) ws2812_frame_buffer[i] = WS2812_DUTYCYCLE_0; // All color bits are zero duty cycle
  125. for (i = 0; i < WS2812_RESET_BIT_N; i++) ws2812_frame_buffer[i + WS2812_COLOR_BIT_N] = 0; // All reset bits are zero
  126. #if defined(USE_GPIOV1)
  127. palSetLineMode(RGB_DI_PIN, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  128. #else
  129. palSetLineMode(RGB_DI_PIN, PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUPDR_FLOATING);
  130. #endif
  131. // PWM Configuration
  132. //#pragma GCC diagnostic ignored "-Woverride-init" // Turn off override-init warning for this struct. We use the overriding ability to set a "default" channel config
  133. static const PWMConfig ws2812_pwm_config = {
  134. .frequency = WS2812_PWM_FREQUENCY,
  135. .period = WS2812_PWM_PERIOD, // Mit dieser Periode wird UDE-Event erzeugt und ein neuer Wert (Länge WS2812_BIT_N) vom DMA ins CCR geschrieben
  136. .callback = NULL,
  137. .channels =
  138. {
  139. [0 ... 3] = {.mode = PWM_OUTPUT_DISABLED, .callback = NULL}, // Channels default to disabled
  140. [WS2812_PWM_CHANNEL - 1] = {.mode = PWM_OUTPUT_ACTIVE_HIGH, .callback = NULL}, // Turn on the channel we care about
  141. },
  142. .cr2 = 0,
  143. .dier = TIM_DIER_UDE, // DMA on update event for next period
  144. };
  145. //#pragma GCC diagnostic pop // Restore command-line warning options
  146. // Configure DMA
  147. // dmaInit(); // Joe added this
  148. dmaStreamAlloc(WS2812_DMA_STREAM - STM32_DMA1_STREAM1, 10, NULL, NULL);
  149. dmaStreamSetPeripheral(WS2812_DMA_STREAM, &(WS2812_PWM_DRIVER.tim->CCR[WS2812_PWM_CHANNEL - 1])); // Ziel ist der An-Zeit im Cap-Comp-Register
  150. dmaStreamSetMemory0(WS2812_DMA_STREAM, ws2812_frame_buffer);
  151. dmaStreamSetTransactionSize(WS2812_DMA_STREAM, WS2812_BIT_N);
  152. dmaStreamSetMode(WS2812_DMA_STREAM, STM32_DMA_CR_CHSEL(WS2812_DMA_CHANNEL) | STM32_DMA_CR_DIR_M2P | STM32_DMA_CR_PSIZE_WORD | STM32_DMA_CR_MSIZE_WORD | STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_PL(3));
  153. // M2P: Memory 2 Periph; PL: Priority Level
  154. // Start DMA
  155. dmaStreamEnable(WS2812_DMA_STREAM);
  156. // Configure PWM
  157. // NOTE: It's required that preload be enabled on the timer channel CCR register. This is currently enabled in the
  158. // ChibiOS driver code, so we don't have to do anything special to the timer. If we did, we'd have to start the timer,
  159. // disable counting, enable the channel, and then make whatever configuration changes we need.
  160. pwmStart(&WS2812_PWM_DRIVER, &ws2812_pwm_config);
  161. pwmEnableChannel(&WS2812_PWM_DRIVER, WS2812_PWM_CHANNEL - 1, 0); // Initial period is 0; output will be low until first duty cycle is DMA'd in
  162. }
  163. void ws2812_write_led(uint16_t led_number, uint8_t r, uint8_t g, uint8_t b) {
  164. // Write color to frame buffer
  165. for (uint8_t bit = 0; bit < 8; bit++) {
  166. ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  167. ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  168. ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  169. }
  170. }
  171. // Setleds for standard RGB
  172. void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) {
  173. static bool s_init = false;
  174. if (!s_init) {
  175. ws2812_init();
  176. s_init = true;
  177. }
  178. for (uint16_t i = 0; i < leds; i++) {
  179. ws2812_write_led(i, ledarray[i].r, ledarray[i].g, ledarray[i].b);
  180. }
  181. }