ws2812.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * @file ws2812.c
  3. * @author Austin Glaser <austin.glaser@gmail.com>
  4. * @brief WS2812 LED driver
  5. *
  6. * Copyright (C) 2016 Austin Glaser
  7. *
  8. * This software may be modified and distributed under the terms
  9. * of the MIT license. See the LICENSE file for details.
  10. *
  11. * @todo Put in names and descriptions of variables which need to be defined to use this file
  12. *
  13. * @addtogroup WS2812
  14. * @{
  15. */
  16. /* --- PRIVATE DEPENDENCIES ------------------------------------------------- */
  17. // This Driver
  18. #include "ws2812.h"
  19. // Standard
  20. #include <stdint.h>
  21. // ChibiOS
  22. #include "ch.h"
  23. #include "hal.h"
  24. // Application
  25. #include "board.h"
  26. #include "util.h"
  27. /* --- CONFIGURATION CHECK -------------------------------------------------- */
  28. #if !defined(WS2812_LED_N)
  29. #error WS2812 LED chain length not specified
  30. #elif WS2812_LED_N <= 0
  31. #error WS2812 LED chain length set to invalid value
  32. #endif
  33. #if !defined(WS2812_TIM_N)
  34. #error WS2812 timer not specified
  35. #elif WS2812_TIM_N == 1
  36. #define WS2812_DMA_STREAM STM32_DMA1_STREAM5
  37. #elif WS2812_TIM_N == 2
  38. #define WS2812_DMA_STREAM STM32_DMA1_STREAM2
  39. #elif WS2812_TIM_N == 3
  40. #define WS2812_DMA_STREAM STM32_DMA1_STREAM3
  41. #elif WS2812_TIM_N == 4
  42. #define WS2812_DMA_STREAM STM32_DMA1_STREAM7
  43. #else
  44. #error WS2812 timer set to invalid value
  45. #endif
  46. #if !defined(WS2812_TIM_CH)
  47. #error WS2812 timer channel not specified
  48. #elif WS2812_TIM_CH >= 4
  49. #error WS2812 timer channel set to invalid value
  50. #endif
  51. /* --- PRIVATE CONSTANTS ---------------------------------------------------- */
  52. #define WS2812_PWM_FREQUENCY (72000000) /**< Clock frequency of PWM */
  53. #define WS2812_PWM_PERIOD (90) /**< Clock period in ticks. 90/(72 MHz) = 1.25 uS (as per datasheet) */
  54. /**
  55. * @brief Number of bit-periods to hold the data line low at the end of a frame
  56. *
  57. * The reset period for each frame must be at least 50 uS; so we add in 50 bit-times
  58. * of zeroes at the end. (50 bits)*(1.25 uS/bit) = 62.5 uS, which gives us some
  59. * slack in the timing requirements
  60. */
  61. #define WS2812_RESET_BIT_N (50)
  62. #define WS2812_COLOR_BIT_N (WS2812_LED_N*24) /**< Number of data bits */
  63. #define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N) /**< Total number of bits in a frame */
  64. /**
  65. * @brief High period for a zero, in ticks
  66. *
  67. * Per the datasheet:
  68. * - T0H: 0.200 uS to 0.500 uS, inclusive
  69. * - T0L: 0.650 uS to 0.950 uS, inclusive
  70. *
  71. * With a duty cycle of 22 ticks, we have a high period of 22/(72 MHz) = 3.06 uS, and
  72. * a low period of (90 - 22)/(72 MHz) = 9.44 uS. These values are within the allowable
  73. * bounds, and intentionally skewed as far to the low duty-cycle side as possible
  74. */
  75. #define WS2812_DUTYCYCLE_0 (22)
  76. /**
  77. * @brief High period for a one, in ticks
  78. *
  79. * Per the datasheet:
  80. * - T0H: 0.550 uS to 0.850 uS, inclusive
  81. * - T0L: 0.450 uS to 0.750 uS, inclusive
  82. *
  83. * With a duty cycle of 56 ticks, we have a high period of 56/(72 MHz) = 7.68 uS, and
  84. * a low period of (90 - 56)/(72 MHz) = 4.72 uS. These values are within the allowable
  85. * bounds, and intentionally skewed as far to the high duty-cycle side as possible
  86. */
  87. #define WS2812_DUTYCYCLE_1 (56)
  88. /* --- PRIVATE MACROS ------------------------------------------------------- */
  89. /**
  90. * @brief Generates a reference to a numbered PWM driver
  91. *
  92. * @param[in] n: The driver (timer) number
  93. *
  94. * @return A reference to the driver
  95. */
  96. #define PWMD(n) CONCAT_EXPANDED_SYMBOLS(PWMD, n)
  97. #define WS2812_PWMD PWMD(WS2812_TIM_N) /**< The PWM driver to use for the LED chain */
  98. /**
  99. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given bit
  100. *
  101. * @param[in] led: The led index [0, @ref WS2812_LED_N)
  102. * @param[in] byte: The byte number [0, 2]
  103. * @param[in] bit: The bit number [0, 7]
  104. *
  105. * @return The bit index
  106. */
  107. #define WS2812_BIT(led, byte, bit) (24*(led) + 8*(byte) + (7 - (bit)))
  108. /**
  109. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  110. *
  111. * @note The red byte is the middle byte in the color packet
  112. *
  113. * @param[in] led: The led index [0, @ref WS2812_LED_N)
  114. * @param[in] bit: The bit number [0, 7]
  115. *
  116. * @return The bit index
  117. */
  118. #define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  119. /**
  120. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  121. *
  122. * @note The red byte is the first byte in the color packet
  123. *
  124. * @param[in] led: The led index [0, @ref WS2812_LED_N)
  125. * @param[in] bit: The bit number [0, 7]
  126. *
  127. * @return The bit index
  128. */
  129. #define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  130. /**
  131. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  132. *
  133. * @note The red byte is the last byte in the color packet
  134. *
  135. * @param[in] led: The led index [0, @ref WS2812_LED_N)
  136. * @param[in] bit: The bit index [0, 7]
  137. *
  138. * @return The bit index
  139. */
  140. #define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  141. /* --- PRIVATE VARIABLES ---------------------------------------------------- */
  142. static uint8_t ws2812_frame_buffer[WS2812_BIT_N]; /**< Buffer for a frame */
  143. /* --- PUBLIC FUNCTIONS ----------------------------------------------------- */
  144. void ws2812_init(void)
  145. {
  146. // Initialize led frame buffer
  147. uint32_t i;
  148. for (i = 0; i < WS2812_COLOR_BIT_N; i++) ws2812_frame_buffer[i] = WS2812_DUTYCYCLE_0; // All color bits are zero duty cycle
  149. for (i = 0; i < WS2812_RESET_BIT_N; i++) ws2812_frame_buffer[i + WS2812_COLOR_BIT_N] = 0; // All reset bits are zero
  150. // Configure PA0 as AF output
  151. palSetPadMode(GPIOA, 1, PAL_MODE_ALTERNATE(1));
  152. // PWM Configuration
  153. #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
  154. static const PWMConfig ws2812_pwm_config = {
  155. .frequency = WS2812_PWM_FREQUENCY,
  156. .period = WS2812_PWM_PERIOD,
  157. .callback = NULL,
  158. .channels = {
  159. [0 ... 3] = {.mode = PWM_OUTPUT_DISABLED, .callback = NULL}, // Channels default to disabled
  160. [WS2812_TIM_CH] = {.mode = PWM_OUTPUT_ACTIVE_HIGH, .callback = NULL}, // Turn on the channel we care about
  161. },
  162. .cr2 = 0,
  163. .dier = TIM_DIER_UDE, // DMA on update event for next period
  164. };
  165. #pragma GCC diagnostic pop // Restore command-line warning options
  166. // Configure DMA
  167. dmaStreamAllocate(WS2812_DMA_STREAM, 10, NULL, NULL);
  168. dmaStreamSetPeripheral(WS2812_DMA_STREAM, &(WS2812_PWMD.tim->CCR[WS2812_TIM_CH]));
  169. dmaStreamSetMemory0(WS2812_DMA_STREAM, ws2812_frame_buffer);
  170. dmaStreamSetTransactionSize(WS2812_DMA_STREAM, WS2812_BIT_N);
  171. dmaStreamSetMode(WS2812_DMA_STREAM,
  172. STM32_DMA_CR_DIR_M2P | STM32_DMA_CR_PSIZE_WORD | STM32_DMA_CR_MSIZE_BYTE |
  173. STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_PL(3));
  174. // Start DMA
  175. dmaStreamEnable(WS2812_DMA_STREAM);
  176. // Configure PWM
  177. // NOTE: It's required that preload be enabled on the timer channel CCR register. This is currently enabled in the
  178. // 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,
  179. // disable counting, enable the channel, and then make whatever configuration changes we need.
  180. pwmStart(&WS2812_PWMD, &ws2812_pwm_config);
  181. pwmEnableChannel(&WS2812_PWMD, WS2812_TIM_CH, 0); // Initial period is 0; output will be low until first duty cycle is DMA'd in
  182. }
  183. ws2812_err_t ws2812_write_led(uint32_t led_number, uint8_t r, uint8_t g, uint8_t b)
  184. {
  185. // Check for valid LED
  186. if (led_number >= WS2812_LED_N) return WS2812_LED_INVALID;
  187. // Write color to frame buffer
  188. uint32_t bit;
  189. for (bit = 0; bit < 8; bit++) {
  190. ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  191. ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  192. ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  193. }
  194. // Success
  195. return WS2812_SUCCESS;
  196. }
  197. /** @} addtogroup WS2812 */
  198. void ws2812_setleds(LED_TYPE *ledarray, uint16_t number_of_leds) {
  199. ws2812_init();
  200. uint8_t i = 0;
  201. while (i < number_of_leds) {
  202. ws2812_write_led(i, ledarray[i].r, ledarray[i].g, ledarray[i].b);
  203. i++;
  204. }
  205. }
  206. void ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t number_of_leds) {
  207. }