You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
13 KiB

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