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.

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