ws2812_pwm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #if (STM32_DMA_SUPPORTS_DMAMUX == TRUE) && !defined(WS2812_DMAMUX_ID)
  24. # error "please consult your MCU's datasheet and specify in your config.h: #define WS2812_DMAMUX_ID STM32_DMAMUX1_TIM?_UP"
  25. #endif
  26. #ifndef WS2812_PWM_COMPLEMENTARY_OUTPUT
  27. # define WS2812_PWM_OUTPUT_MODE PWM_OUTPUT_ACTIVE_HIGH
  28. #else
  29. # if !STM32_PWM_USE_ADVANCED
  30. # error "WS2812_PWM_COMPLEMENTARY_OUTPUT requires STM32_PWM_USE_ADVANCED == TRUE"
  31. # endif
  32. # define WS2812_PWM_OUTPUT_MODE PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH
  33. #endif
  34. // Push Pull or Open Drain Configuration
  35. // Default Push Pull
  36. #ifndef WS2812_EXTERNAL_PULLUP
  37. # if defined(USE_GPIOV1)
  38. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE_PUSHPULL
  39. # else
  40. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST | PAL_PUPDR_FLOATING
  41. # endif
  42. #else
  43. # if defined(USE_GPIOV1)
  44. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE_PUSHPULL
  45. # else
  46. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN | PAL_OUTPUT_SPEED_HIGHEST | PAL_PUPDR_FLOATING
  47. # endif
  48. #endif
  49. #ifndef WS2812_PWM_TARGET_PERIOD
  50. //# define WS2812_PWM_TARGET_PERIOD 800000 // Original code is 800k...?
  51. # define WS2812_PWM_TARGET_PERIOD 80000 // TODO: work out why 10x less on f303/f4x1
  52. #endif
  53. /* --- PRIVATE CONSTANTS ---------------------------------------------------- */
  54. #define WS2812_PWM_FREQUENCY (CPU_CLOCK / 2) /**< Clock frequency of PWM, must be valid with respect to system clock! */
  55. #define WS2812_PWM_PERIOD (WS2812_PWM_FREQUENCY / WS2812_PWM_TARGET_PERIOD) /**< Clock period in ticks. 1 / 800kHz = 1.25 uS (as per datasheet) */
  56. /**
  57. * @brief Number of bit-periods to hold the data line low at the end of a frame
  58. *
  59. * The reset period for each frame is defined in WS2812_TRST_US.
  60. * Calculate the number of zeroes to add at the end assuming 1.25 uS/bit:
  61. */
  62. #define WS2812_RESET_BIT_N (1000 * WS2812_TRST_US / 1250)
  63. #define WS2812_COLOR_BIT_N (RGBLED_NUM * 24) /**< Number of data bits */
  64. #define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N) /**< Total number of bits in a frame */
  65. /**
  66. * @brief High period for a zero, in ticks
  67. *
  68. * Per the datasheet:
  69. * WS2812:
  70. * - T0H: 200 nS to 500 nS, inclusive
  71. * - T0L: 650 nS to 950 nS, inclusive
  72. * WS2812B:
  73. * - T0H: 200 nS to 500 nS, inclusive
  74. * - T0L: 750 nS to 1050 nS, inclusive
  75. *
  76. * The duty cycle is calculated for a high period of 350 nS.
  77. */
  78. #define WS2812_DUTYCYCLE_0 (WS2812_PWM_FREQUENCY / (1000000000 / 350))
  79. /**
  80. * @brief High period for a one, in ticks
  81. *
  82. * Per the datasheet:
  83. * WS2812:
  84. * - T1H: 550 nS to 850 nS, inclusive
  85. * - T1L: 450 nS to 750 nS, inclusive
  86. * WS2812B:
  87. * - T1H: 750 nS to 1050 nS, inclusive
  88. * - T1L: 200 nS to 500 nS, inclusive
  89. *
  90. * The duty cycle is calculated for a high period of 800 nS.
  91. * This is in the middle of the specifications of the WS2812 and WS2812B.
  92. */
  93. #define WS2812_DUTYCYCLE_1 (WS2812_PWM_FREQUENCY / (1000000000 / 800))
  94. /* --- PRIVATE MACROS ------------------------------------------------------- */
  95. /**
  96. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given bit
  97. *
  98. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  99. * @param[in] byte: The byte number [0, 2]
  100. * @param[in] bit: The bit number [0, 7]
  101. *
  102. * @return The bit index
  103. */
  104. #define WS2812_BIT(led, byte, bit) (24 * (led) + 8 * (byte) + (7 - (bit)))
  105. #if (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_GRB)
  106. /**
  107. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  108. *
  109. * @note The red byte is the middle byte in the color packet
  110. *
  111. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  112. * @param[in] bit: The bit number [0, 7]
  113. *
  114. * @return The bit index
  115. */
  116. # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  117. /**
  118. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  119. *
  120. * @note The red byte is the first byte in the color packet
  121. *
  122. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  123. * @param[in] bit: The bit number [0, 7]
  124. *
  125. * @return The bit index
  126. */
  127. # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  128. /**
  129. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  130. *
  131. * @note The red byte is the last byte in the color packet
  132. *
  133. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  134. * @param[in] bit: The bit index [0, 7]
  135. *
  136. * @return The bit index
  137. */
  138. # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  139. #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_RGB)
  140. /**
  141. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  142. *
  143. * @note The red byte is the middle byte in the color packet
  144. *
  145. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  146. * @param[in] bit: The bit number [0, 7]
  147. *
  148. * @return The bit index
  149. */
  150. # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  151. /**
  152. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  153. *
  154. * @note The red byte is the first byte in the color packet
  155. *
  156. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  157. * @param[in] bit: The bit number [0, 7]
  158. *
  159. * @return The bit index
  160. */
  161. # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  162. /**
  163. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  164. *
  165. * @note The red byte is the last byte in the color packet
  166. *
  167. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  168. * @param[in] bit: The bit index [0, 7]
  169. *
  170. * @return The bit index
  171. */
  172. # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  173. #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_BGR)
  174. /**
  175. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  176. *
  177. * @note The red byte is the middle byte in the color packet
  178. *
  179. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  180. * @param[in] bit: The bit number [0, 7]
  181. *
  182. * @return The bit index
  183. */
  184. # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  185. /**
  186. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  187. *
  188. * @note The red byte is the first byte in the color packet
  189. *
  190. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  191. * @param[in] bit: The bit number [0, 7]
  192. *
  193. * @return The bit index
  194. */
  195. # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  196. /**
  197. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  198. *
  199. * @note The red byte is the last byte in the color packet
  200. *
  201. * @param[in] led: The led index [0, @ref RGBLED_NUM)
  202. * @param[in] bit: The bit index [0, 7]
  203. *
  204. * @return The bit index
  205. */
  206. # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  207. #endif
  208. /* --- PRIVATE VARIABLES ---------------------------------------------------- */
  209. static uint32_t ws2812_frame_buffer[WS2812_BIT_N + 1]; /**< Buffer for a frame */
  210. /* --- PUBLIC FUNCTIONS ----------------------------------------------------- */
  211. /*
  212. * Gedanke: Double-buffer type transactions: double buffer transfers using two memory pointers for
  213. the memory (while the DMA is reading/writing from/to a buffer, the application can
  214. write/read to/from the other buffer).
  215. */
  216. void ws2812_init(void) {
  217. // Initialize led frame buffer
  218. uint32_t i;
  219. for (i = 0; i < WS2812_COLOR_BIT_N; i++) ws2812_frame_buffer[i] = WS2812_DUTYCYCLE_0; // All color bits are zero duty cycle
  220. for (i = 0; i < WS2812_RESET_BIT_N; i++) ws2812_frame_buffer[i + WS2812_COLOR_BIT_N] = 0; // All reset bits are zero
  221. palSetLineMode(RGB_DI_PIN, WS2812_OUTPUT_MODE);
  222. // PWM Configuration
  223. //#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
  224. static const PWMConfig ws2812_pwm_config = {
  225. .frequency = WS2812_PWM_FREQUENCY,
  226. .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
  227. .callback = NULL,
  228. .channels =
  229. {
  230. [0 ... 3] = {.mode = PWM_OUTPUT_DISABLED, .callback = NULL}, // Channels default to disabled
  231. [WS2812_PWM_CHANNEL - 1] = {.mode = WS2812_PWM_OUTPUT_MODE, .callback = NULL}, // Turn on the channel we care about
  232. },
  233. .cr2 = 0,
  234. .dier = TIM_DIER_UDE, // DMA on update event for next period
  235. };
  236. //#pragma GCC diagnostic pop // Restore command-line warning options
  237. // Configure DMA
  238. // dmaInit(); // Joe added this
  239. dmaStreamAlloc(WS2812_DMA_STREAM - STM32_DMA_STREAM(0), 10, NULL, NULL);
  240. dmaStreamSetPeripheral(WS2812_DMA_STREAM, &(WS2812_PWM_DRIVER.tim->CCR[WS2812_PWM_CHANNEL - 1])); // Ziel ist der An-Zeit im Cap-Comp-Register
  241. dmaStreamSetMemory0(WS2812_DMA_STREAM, ws2812_frame_buffer);
  242. dmaStreamSetTransactionSize(WS2812_DMA_STREAM, WS2812_BIT_N);
  243. 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));
  244. // M2P: Memory 2 Periph; PL: Priority Level
  245. #if (STM32_DMA_SUPPORTS_DMAMUX == TRUE)
  246. // If the MCU has a DMAMUX we need to assign the correct resource
  247. dmaSetRequestSource(WS2812_DMA_STREAM, WS2812_DMAMUX_ID);
  248. #endif
  249. // Start DMA
  250. dmaStreamEnable(WS2812_DMA_STREAM);
  251. // Configure PWM
  252. // NOTE: It's required that preload be enabled on the timer channel CCR register. This is currently enabled in the
  253. // 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,
  254. // disable counting, enable the channel, and then make whatever configuration changes we need.
  255. pwmStart(&WS2812_PWM_DRIVER, &ws2812_pwm_config);
  256. 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
  257. }
  258. void ws2812_write_led(uint16_t led_number, uint8_t r, uint8_t g, uint8_t b) {
  259. // Write color to frame buffer
  260. for (uint8_t bit = 0; bit < 8; bit++) {
  261. ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  262. ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  263. ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  264. }
  265. }
  266. // Setleds for standard RGB
  267. void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) {
  268. static bool s_init = false;
  269. if (!s_init) {
  270. ws2812_init();
  271. s_init = true;
  272. }
  273. for (uint16_t i = 0; i < leds; i++) {
  274. ws2812_write_led(i, ledarray[i].r, ledarray[i].g, ledarray[i].b);
  275. }
  276. }