ws2812.c 9.9 KB

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