ws2812_pwm.c 16 KB

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