ws2812_pwm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. # define WS2812_CHANNELS 4
  7. #else
  8. # define WS2812_CHANNELS 3
  9. #endif
  10. #ifndef WS2812_PWM_DRIVER
  11. # define WS2812_PWM_DRIVER PWMD2 // TIMx
  12. #endif
  13. #ifndef WS2812_PWM_CHANNEL
  14. # define WS2812_PWM_CHANNEL 2 // Channel
  15. #endif
  16. #ifndef WS2812_PWM_PAL_MODE
  17. # define WS2812_PWM_PAL_MODE 2 // DI Pin's alternate function value
  18. #endif
  19. #ifndef WS2812_DMA_STREAM
  20. # define WS2812_DMA_STREAM STM32_DMA1_STREAM2 // DMA Stream for TIMx_UP
  21. #endif
  22. #ifndef WS2812_DMA_CHANNEL
  23. # define WS2812_DMA_CHANNEL 2 // DMA Channel for TIMx_UP
  24. #endif
  25. #if (STM32_DMA_SUPPORTS_DMAMUX == TRUE) && !defined(WS2812_DMAMUX_ID)
  26. # error "please consult your MCU's datasheet and specify in your config.h: #define WS2812_DMAMUX_ID STM32_DMAMUX1_TIM?_UP"
  27. #endif
  28. /* Summarize https://www.st.com/resource/en/application_note/an4013-stm32-crossseries-timer-overview-stmicroelectronics.pdf to
  29. * figure out if we are using a 32bit timer. This is needed to setup the DMA controller correctly.
  30. * Ignore STM32H7XX and STM32U5XX as they are not supported by ChibiOS.
  31. */
  32. #if !defined(STM32F1XX) && !defined(STM32L0XX) && !defined(STM32L1XX)
  33. # define WS2812_PWM_TIMER_32BIT_PWMD2 1
  34. #endif
  35. #if !defined(STM32F1XX)
  36. # define WS2812_PWM_TIMER_32BIT_PWMD5 1
  37. #endif
  38. #define WS2812_CONCAT1(a, b) a##b
  39. #define WS2812_CONCAT(a, b) WS2812_CONCAT1(a, b)
  40. #if WS2812_CONCAT(WS2812_PWM_TIMER_32BIT_, WS2812_PWM_DRIVER)
  41. # define WS2812_PWM_TIMER_32BIT
  42. #endif
  43. #ifndef WS2812_PWM_COMPLEMENTARY_OUTPUT
  44. # define WS2812_PWM_OUTPUT_MODE PWM_OUTPUT_ACTIVE_HIGH
  45. #else
  46. # if !STM32_PWM_USE_ADVANCED
  47. # error "WS2812_PWM_COMPLEMENTARY_OUTPUT requires STM32_PWM_USE_ADVANCED == TRUE"
  48. # endif
  49. # define WS2812_PWM_OUTPUT_MODE PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH
  50. #endif
  51. // Push Pull or Open Drain Configuration
  52. // Default Push Pull
  53. #ifndef WS2812_EXTERNAL_PULLUP
  54. # if defined(USE_GPIOV1)
  55. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE_PUSHPULL
  56. # else
  57. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST | PAL_PUPDR_FLOATING
  58. # endif
  59. #else
  60. # if defined(USE_GPIOV1)
  61. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE_OPENDRAIN
  62. # else
  63. # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN | PAL_OUTPUT_SPEED_HIGHEST | PAL_PUPDR_FLOATING
  64. # endif
  65. #endif
  66. #ifndef WS2812_PWM_TARGET_PERIOD
  67. //# define WS2812_PWM_TARGET_PERIOD 800000 // Original code is 800k...?
  68. # define WS2812_PWM_TARGET_PERIOD 80000 // TODO: work out why 10x less on f303/f4x1
  69. #endif
  70. /* --- PRIVATE CONSTANTS ---------------------------------------------------- */
  71. #define WS2812_PWM_FREQUENCY (CPU_CLOCK / 2) /**< Clock frequency of PWM, must be valid with respect to system clock! */
  72. #define WS2812_PWM_PERIOD (WS2812_PWM_FREQUENCY / WS2812_PWM_TARGET_PERIOD) /**< Clock period in ticks. 1 / 800kHz = 1.25 uS (as per datasheet) */
  73. /**
  74. * @brief Number of bit-periods to hold the data line low at the end of a frame
  75. *
  76. * The reset period for each frame is defined in WS2812_TRST_US.
  77. * Calculate the number of zeroes to add at the end assuming 1.25 uS/bit:
  78. */
  79. #define WS2812_COLOR_BITS (WS2812_CHANNELS * 8)
  80. #define WS2812_RESET_BIT_N (1000 * WS2812_TRST_US / WS2812_TIMING)
  81. #define WS2812_COLOR_BIT_N (WS2812_LED_COUNT * WS2812_COLOR_BITS) /**< Number of data bits */
  82. #define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N) /**< Total number of bits in a frame */
  83. /**
  84. * @brief High period for a zero, in ticks
  85. *
  86. * Per the datasheet:
  87. * WS2812:
  88. * - T0H: 200 nS to 500 nS, inclusive
  89. * - T0L: 650 nS to 950 nS, inclusive
  90. * WS2812B:
  91. * - T0H: 200 nS to 500 nS, inclusive
  92. * - T0L: 750 nS to 1050 nS, inclusive
  93. *
  94. * The duty cycle is calculated for a high period of 350 nS.
  95. */
  96. #define WS2812_DUTYCYCLE_0 (WS2812_PWM_FREQUENCY / (1000000000 / 350))
  97. #if (WS2812_DUTYCYCLE_0 > 255)
  98. # error WS2812 PWM driver: High period for a 0 is more than a byte
  99. #endif
  100. /**
  101. * @brief High period for a one, in ticks
  102. *
  103. * Per the datasheet:
  104. * WS2812:
  105. * - T1H: 550 nS to 850 nS, inclusive
  106. * - T1L: 450 nS to 750 nS, inclusive
  107. * WS2812B:
  108. * - T1H: 750 nS to 1050 nS, inclusive
  109. * - T1L: 200 nS to 500 nS, inclusive
  110. *
  111. * The duty cycle is calculated for a high period of 800 nS.
  112. * This is in the middle of the specifications of the WS2812 and WS2812B.
  113. */
  114. #define WS2812_DUTYCYCLE_1 (WS2812_PWM_FREQUENCY / (1000000000 / 800))
  115. #if (WS2812_DUTYCYCLE_1 > 255)
  116. # error WS2812 PWM driver: High period for a 1 is more than a byte
  117. #endif
  118. /* --- PRIVATE MACROS ------------------------------------------------------- */
  119. /**
  120. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given bit
  121. *
  122. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  123. * @param[in] byte: The byte number [0, 2]
  124. * @param[in] bit: The bit number [0, 7]
  125. *
  126. * @return The bit index
  127. */
  128. #define WS2812_BIT(led, byte, bit) (WS2812_COLOR_BITS * (led) + 8 * (byte) + (7 - (bit)))
  129. #if (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_GRB)
  130. /**
  131. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  132. *
  133. * @note The red byte is the middle byte in the color packet
  134. *
  135. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  136. * @param[in] bit: The bit number [0, 7]
  137. *
  138. * @return The bit index
  139. */
  140. # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  141. /**
  142. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  143. *
  144. * @note The red byte is the first byte in the color packet
  145. *
  146. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  147. * @param[in] bit: The bit number [0, 7]
  148. *
  149. * @return The bit index
  150. */
  151. # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  152. /**
  153. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  154. *
  155. * @note The red byte is the last byte in the color packet
  156. *
  157. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  158. * @param[in] bit: The bit index [0, 7]
  159. *
  160. * @return The bit index
  161. */
  162. # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  163. #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_RGB)
  164. /**
  165. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  166. *
  167. * @note The red byte is the middle byte in the color packet
  168. *
  169. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  170. * @param[in] bit: The bit number [0, 7]
  171. *
  172. * @return The bit index
  173. */
  174. # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  175. /**
  176. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  177. *
  178. * @note The red byte is the first byte in the color packet
  179. *
  180. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  181. * @param[in] bit: The bit number [0, 7]
  182. *
  183. * @return The bit index
  184. */
  185. # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  186. /**
  187. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  188. *
  189. * @note The red byte is the last byte in the color packet
  190. *
  191. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  192. * @param[in] bit: The bit index [0, 7]
  193. *
  194. * @return The bit index
  195. */
  196. # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  197. #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_BGR)
  198. /**
  199. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  200. *
  201. * @note The red byte is the middle byte in the color packet
  202. *
  203. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  204. * @param[in] bit: The bit number [0, 7]
  205. *
  206. * @return The bit index
  207. */
  208. # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  209. /**
  210. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  211. *
  212. * @note The red byte is the first byte in the color packet
  213. *
  214. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  215. * @param[in] bit: The bit number [0, 7]
  216. *
  217. * @return The bit index
  218. */
  219. # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  220. /**
  221. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  222. *
  223. * @note The red byte is the last byte in the color packet
  224. *
  225. * @param[in] led: The led index [0, @ref WS2812_LED_COUNT)
  226. * @param[in] bit: The bit index [0, 7]
  227. *
  228. * @return The bit index
  229. */
  230. # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  231. #endif
  232. #ifdef RGBW
  233. /**
  234. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given white bit
  235. *
  236. * @note The white byte is the last byte in the color packet
  237. *
  238. * @param[in] led: The led index [0, @ref WS2812_LED_N)
  239. * @param[in] bit: The bit index [0, 7]
  240. *
  241. * @return The bit index
  242. */
  243. # define WS2812_WHITE_BIT(led, bit) WS2812_BIT((led), 3, (bit))
  244. #endif
  245. /* --- PRIVATE VARIABLES ---------------------------------------------------- */
  246. // STM32F2XX, STM32F4XX and STM32F7XX do NOT zero pad DMA transfers of unequal data width. Buffer width must match TIMx CCR.
  247. // For all other STM32 DMA transfer will automatically zero pad. We only need to set the right peripheral width.
  248. #if defined(STM32F2XX) || defined(STM32F4XX) || defined(STM32F7XX)
  249. # if defined(WS2812_PWM_TIMER_32BIT)
  250. # define WS2812_DMA_MEMORY_WIDTH STM32_DMA_CR_MSIZE_WORD
  251. # define WS2812_DMA_PERIPHERAL_WIDTH STM32_DMA_CR_PSIZE_WORD
  252. typedef uint32_t ws2812_buffer_t;
  253. # else
  254. # define WS2812_DMA_MEMORY_WIDTH STM32_DMA_CR_MSIZE_HWORD
  255. # define WS2812_DMA_PERIPHERAL_WIDTH STM32_DMA_CR_PSIZE_HWORD
  256. typedef uint16_t ws2812_buffer_t;
  257. # endif
  258. #else
  259. # define WS2812_DMA_MEMORY_WIDTH STM32_DMA_CR_MSIZE_BYTE
  260. # if defined(WS2812_PWM_TIMER_32BIT)
  261. # define WS2812_DMA_PERIPHERAL_WIDTH STM32_DMA_CR_PSIZE_WORD
  262. # else
  263. # define WS2812_DMA_PERIPHERAL_WIDTH STM32_DMA_CR_PSIZE_HWORD
  264. # endif
  265. typedef uint8_t ws2812_buffer_t;
  266. #endif
  267. static ws2812_buffer_t ws2812_frame_buffer[WS2812_BIT_N + 1]; /**< Buffer for a frame */
  268. /* --- PUBLIC FUNCTIONS ----------------------------------------------------- */
  269. /*
  270. * Gedanke: Double-buffer type transactions: double buffer transfers using two memory pointers for
  271. * the memory (while the DMA is reading/writing from/to a buffer, the application can
  272. * write/read to/from the other buffer).
  273. */
  274. void ws2812_init(void) {
  275. // Initialize led frame buffer
  276. uint32_t i;
  277. for (i = 0; i < WS2812_COLOR_BIT_N; i++)
  278. ws2812_frame_buffer[i] = WS2812_DUTYCYCLE_0; // All color bits are zero duty cycle
  279. for (i = 0; i < WS2812_RESET_BIT_N; i++)
  280. ws2812_frame_buffer[i + WS2812_COLOR_BIT_N] = 0; // All reset bits are zero
  281. palSetLineMode(RGB_DI_PIN, WS2812_OUTPUT_MODE);
  282. // PWM Configuration
  283. //#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
  284. static const PWMConfig ws2812_pwm_config = {
  285. .frequency = WS2812_PWM_FREQUENCY,
  286. .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
  287. .callback = NULL,
  288. .channels =
  289. {
  290. [0 ... 3] = {.mode = PWM_OUTPUT_DISABLED, .callback = NULL}, // Channels default to disabled
  291. [WS2812_PWM_CHANNEL - 1] = {.mode = WS2812_PWM_OUTPUT_MODE, .callback = NULL}, // Turn on the channel we care about
  292. },
  293. .cr2 = 0,
  294. .dier = TIM_DIER_UDE, // DMA on update event for next period
  295. };
  296. //#pragma GCC diagnostic pop // Restore command-line warning options
  297. // Configure DMA
  298. // dmaInit(); // Joe added this
  299. #if defined(WB32F3G71xx) || defined(WB32FQ95xx)
  300. dmaStreamAlloc(WS2812_DMA_STREAM - WB32_DMA_STREAM(0), 10, NULL, NULL);
  301. dmaStreamSetSource(WS2812_DMA_STREAM, ws2812_frame_buffer);
  302. dmaStreamSetDestination(WS2812_DMA_STREAM, &(WS2812_PWM_DRIVER.tim->CCR[WS2812_PWM_CHANNEL - 1])); // Ziel ist der An-Zeit im Cap-Comp-Register
  303. dmaStreamSetMode(WS2812_DMA_STREAM, WB32_DMA_CHCFG_HWHIF(WS2812_DMA_CHANNEL) | WB32_DMA_CHCFG_DIR_M2P | WB32_DMA_CHCFG_PSIZE_WORD | WB32_DMA_CHCFG_MSIZE_WORD | WB32_DMA_CHCFG_MINC | WB32_DMA_CHCFG_CIRC | WB32_DMA_CHCFG_TCIE | WB32_DMA_CHCFG_PL(3));
  304. #else
  305. dmaStreamAlloc(WS2812_DMA_STREAM - STM32_DMA_STREAM(0), 10, NULL, NULL);
  306. dmaStreamSetPeripheral(WS2812_DMA_STREAM, &(WS2812_PWM_DRIVER.tim->CCR[WS2812_PWM_CHANNEL - 1])); // Ziel ist der An-Zeit im Cap-Comp-Register
  307. dmaStreamSetMemory0(WS2812_DMA_STREAM, ws2812_frame_buffer);
  308. dmaStreamSetMode(WS2812_DMA_STREAM, STM32_DMA_CR_CHSEL(WS2812_DMA_CHANNEL) | STM32_DMA_CR_DIR_M2P | WS2812_DMA_PERIPHERAL_WIDTH | WS2812_DMA_MEMORY_WIDTH | STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_PL(3));
  309. #endif
  310. dmaStreamSetTransactionSize(WS2812_DMA_STREAM, WS2812_BIT_N);
  311. // M2P: Memory 2 Periph; PL: Priority Level
  312. #if (STM32_DMA_SUPPORTS_DMAMUX == TRUE)
  313. // If the MCU has a DMAMUX we need to assign the correct resource
  314. dmaSetRequestSource(WS2812_DMA_STREAM, WS2812_DMAMUX_ID);
  315. #endif
  316. // Start DMA
  317. dmaStreamEnable(WS2812_DMA_STREAM);
  318. // Configure PWM
  319. // NOTE: It's required that preload be enabled on the timer channel CCR register. This is currently enabled in the
  320. // 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,
  321. // disable counting, enable the channel, and then make whatever configuration changes we need.
  322. pwmStart(&WS2812_PWM_DRIVER, &ws2812_pwm_config);
  323. 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
  324. }
  325. void ws2812_write_led(uint16_t led_number, uint8_t r, uint8_t g, uint8_t b) {
  326. // Write color to frame buffer
  327. for (uint8_t bit = 0; bit < 8; bit++) {
  328. ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  329. ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  330. ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  331. }
  332. }
  333. void ws2812_write_led_rgbw(uint16_t led_number, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
  334. // Write color to frame buffer
  335. for (uint8_t bit = 0; bit < 8; bit++) {
  336. ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  337. ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  338. ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  339. #ifdef RGBW
  340. ws2812_frame_buffer[WS2812_WHITE_BIT(led_number, bit)] = ((w >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  341. #endif
  342. }
  343. }
  344. // Setleds for standard RGB
  345. void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) {
  346. static bool s_init = false;
  347. if (!s_init) {
  348. ws2812_init();
  349. s_init = true;
  350. }
  351. for (uint16_t i = 0; i < leds; i++) {
  352. #ifdef RGBW
  353. ws2812_write_led_rgbw(i, ledarray[i].r, ledarray[i].g, ledarray[i].b, ledarray[i].w);
  354. #else
  355. ws2812_write_led(i, ledarray[i].r, ledarray[i].g, ledarray[i].b);
  356. #endif
  357. }
  358. }