ws2812_f4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * @file ws2812.c
  3. * @author Austin Glaser <austin.glaser@gmail.com>, Joerg Wangemann <joerg.wangemann@gmail.com>
  4. * @brief WS2812 LED driver
  5. *
  6. * Copyright (C) 2016 Austin Glaser, 2017 Joerg Wangemann
  7. *
  8. * This software may be modified and distributed under the terms
  9. * of the MIT license. See the LICENSE file for details.
  10. *
  11. * @todo Put in names and descriptions of variables which need to be defined to use this file
  12. *
  13. * @addtogroup WS2812
  14. * @{
  15. */
  16. /* --- PRIVATE DEPENDENCIES ------------------------------------------------- */
  17. // This Driver
  18. #include "ws2812_f4.h"
  19. // Standard
  20. #include <stdint.h>
  21. // ChibiOS
  22. #include "ch.h"
  23. #include "hal.h"
  24. #include "wait.h"
  25. // Application
  26. //#include "board.h"
  27. // TODO: Add these #define's to the headers of your project.
  28. // Pin, timer and dma are all connected, check them all if you change one.
  29. // Tested with STM32F4, working at 144 or 168 MHz.
  30. //#define WS2812_LED_N 2 // Number of LEDs
  31. //#define PORT_WS2812 GPIOB
  32. //#define PIN_WS2812 9
  33. //#define WS2812_TIM_N 4 // timer, 1-11
  34. //#define WS2812_TIM_CH 3 // timer channel, 0-3
  35. //#define WS2812_DMA_STREAM STM32_DMA1_STREAM2 // DMA stream for TIMx_UP (look up in reference manual under DMA Channel selection)
  36. //#define WS2812_DMA_CHANNEL 6 // DMA channel for TIMx_UP
  37. // The WS2812 expects 5V signal level (or at least 0.7 * VDD). Sometimes it works
  38. // with a 3V signal level, otherwise the easiest way to get the signal level to 5V
  39. // is to add an external pullup resistor from the DI pin to 5V (10k will do) and
  40. // configure the pin as open drain.
  41. // (An SMD resistor is easily solders on the connections of a light strip)
  42. // Uncomment the next line if an external pullup resistor is used.
  43. //#define WS2812_EXTERNAL_PULLUP
  44. /* --- CONFIGURATION CHECK -------------------------------------------------- */
  45. #if !defined(WS2812_LED_N)
  46. #error WS2812 LED chain length not specified
  47. #elif WS2812_LED_N <= 0
  48. #error WS2812 LED chain length set to invalid value
  49. #endif
  50. #if !defined(WS2812_TIM_N)
  51. #error WS2812 timer not specified
  52. #endif
  53. #if defined(STM32F2XX) || defined(STM32F4XX) || defined(STM32F7XX)
  54. #if WS2812_TIM_N <= 2
  55. #define WS2812_AF 1
  56. #elif WS2812_TIM_N <= 5
  57. #define WS2812_AF 2
  58. #elif WS2812_TIM_N <= 11
  59. #define WS2812_AF 3
  60. #endif
  61. #elif !defined(WS2812_AF)
  62. #error WS2812_AF timer alternate function not specified
  63. #endif
  64. #if !defined(WS2812_TIM_CH)
  65. #error WS2812 timer channel not specified
  66. #elif WS2812_TIM_CH >= 4
  67. #error WS2812 timer channel set to invalid value
  68. #endif
  69. /* --- PRIVATE CONSTANTS ---------------------------------------------------- */
  70. #define WS2812_PWM_FREQUENCY (STM32_SYSCLK/2) /**< Clock frequency of PWM, must be valid with respect to system clock! */
  71. #define WS2812_PWM_PERIOD (WS2812_PWM_FREQUENCY/800000) /**< Clock period in ticks. 1 / 800kHz = 1.25 uS (as per datasheet) */
  72. /**
  73. * @brief Number of bit-periods to hold the data line low at the end of a frame
  74. *
  75. * The reset period for each frame must be at least 50 uS; so we add in 50 bit-times
  76. * of zeroes at the end. (50 bits)*(1.25 uS/bit) = 62.5 uS, which gives us some
  77. * slack in the timing requirements
  78. */
  79. #define WS2812_RESET_BIT_N (50)
  80. #define WS2812_COLOR_BIT_N (WS2812_LED_N*24) /**< Number of data bits */
  81. #define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N) /**< Total number of bits in a frame */
  82. /**
  83. * @brief High period for a zero, in ticks
  84. *
  85. * Per the datasheet:
  86. * WS2812:
  87. * - T0H: 200 nS to 500 nS, inclusive
  88. * - T0L: 650 nS to 950 nS, inclusive
  89. * WS2812B:
  90. * - T0H: 200 nS to 500 nS, inclusive
  91. * - T0L: 750 nS to 1050 nS, inclusive
  92. *
  93. * The duty cycle is calculated for a high period of 350 nS.
  94. */
  95. #define WS2812_DUTYCYCLE_0 (WS2812_PWM_FREQUENCY/(1000000000/450))
  96. /**
  97. * @brief High period for a one, in ticks
  98. *
  99. * Per the datasheet:
  100. * WS2812:
  101. * - T1H: 550 nS to 850 nS, inclusive
  102. * - T1L: 450 nS to 750 nS, inclusive
  103. * WS2812B:
  104. * - T1H: 750 nS to 1050 nS, inclusive
  105. * - T1L: 200 nS to 500 nS, inclusive
  106. *
  107. * The duty cycle is calculated for a high period of 800 nS.
  108. * This is in the middle of the specifications of the WS2812 and WS2812B.
  109. */
  110. #define WS2812_DUTYCYCLE_1 (WS2812_PWM_FREQUENCY/(1000000000/900))
  111. /* --- PRIVATE MACROS ------------------------------------------------------- */
  112. /**
  113. * @brief Generates a reference to a numbered PWM driver
  114. *
  115. * @param[in] n: The driver (timer) number
  116. *
  117. * @return A reference to the driver
  118. */
  119. #define PWMD(n) CONCAT_EXPANDED_SYMBOLS(PWMD, n)
  120. #define WS2812_PWMD PWMD(WS2812_TIM_N) /**< The PWM driver to use for the LED chain */
  121. /**
  122. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given bit
  123. *
  124. * @param[in] led: The led index [0, @ref WS2812_LED_N)
  125. * @param[in] byte: The byte number [0, 2]
  126. * @param[in] bit: The bit number [0, 7]
  127. *
  128. * @return The bit index
  129. */
  130. #define WS2812_BIT(led, byte, bit) (24*(led) + 8*(byte) + (7 - (bit)))
  131. /**
  132. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
  133. *
  134. * @note The red byte is the middle byte in the color packet
  135. *
  136. * @param[in] led: The led index [0, @ref WS2812_LED_N)
  137. * @param[in] bit: The bit number [0, 7]
  138. *
  139. * @return The bit index
  140. */
  141. #define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 1, (bit))
  142. /**
  143. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
  144. *
  145. * @note The green byte is the first byte in the color packet
  146. *
  147. * @param[in] led: The led index [0, @ref WS2812_LED_N)
  148. * @param[in] bit: The bit number [0, 7]
  149. *
  150. * @return The bit index
  151. */
  152. #define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 0, (bit))
  153. /**
  154. * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
  155. *
  156. * @note The blue byte is the last byte in the color packet
  157. *
  158. * @param[in] led: The led index [0, @ref WS2812_LED_N)
  159. * @param[in] bit: The bit index [0, 7]
  160. *
  161. * @return The bit index
  162. */
  163. #define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
  164. /* --- PRIVATE VARIABLES ---------------------------------------------------- */
  165. static uint32_t ws2812_frame_buffer[WS2812_BIT_N + 1]; /**< Buffer for a frame */
  166. /* --- PUBLIC FUNCTIONS ----------------------------------------------------- */
  167. /*
  168. * Gedanke: Double-buffer type transactions: double buffer transfers using two memory pointers for
  169. the memory (while the DMA is reading/writing from/to a buffer, the application can
  170. write/read to/from the other buffer).
  171. */
  172. void ws2812_init(void)
  173. {
  174. // Initialize led frame buffer
  175. uint32_t i;
  176. for (i = 0; i < WS2812_COLOR_BIT_N; i++) ws2812_frame_buffer[i] = WS2812_DUTYCYCLE_0; // All color bits are zero duty cycle
  177. for (i = 0; i < WS2812_RESET_BIT_N; i++) ws2812_frame_buffer[i + WS2812_COLOR_BIT_N] = 0; // All reset bits are zero
  178. // Configure pin as AF output. If there's an external pull up resistor the signal level is brought to 5V using open drain mode.
  179. #ifdef WS2812_EXTERNAL_PULLUP
  180. palSetPadMode(PORT_WS2812, PIN_WS2812, PAL_MODE_ALTERNATE(WS2812_AF) | PAL_STM32_OTYPE_OPENDRAIN);
  181. #else
  182. palSetPadMode(PORT_WS2812, PIN_WS2812, PAL_MODE_ALTERNATE(WS2812_AF) | PAL_STM32_OTYPE_PUSHPULL); //PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  183. #endif
  184. //palClearPad(PORT_WS2812, PIN_WS2812);
  185. //wait_ms(1);
  186. // PWM Configuration
  187. #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
  188. static const PWMConfig ws2812_pwm_config = {
  189. .frequency = WS2812_PWM_FREQUENCY,
  190. .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
  191. .callback = NULL,
  192. .channels = {
  193. [0 ... 3] = {.mode = PWM_OUTPUT_DISABLED, .callback = NULL}, // Channels default to disabled
  194. [WS2812_TIM_CH] = {.mode = PWM_OUTPUT_ACTIVE_HIGH, .callback = NULL}, // Turn on the channel we care about
  195. },
  196. .cr2 = 0,
  197. .dier = TIM_DIER_UDE, // DMA on update event for next period
  198. };
  199. #pragma GCC diagnostic pop // Restore command-line warning options
  200. // Configure DMA
  201. //dmaInit(); // Joe added this
  202. dmaStreamAllocate(WS2812_DMA_STREAM, 10, NULL, NULL);
  203. dmaStreamSetPeripheral(WS2812_DMA_STREAM, &(WS2812_PWMD.tim->CCR[WS2812_TIM_CH])); // Ziel ist der An-Zeit im Cap-Comp-Register
  204. dmaStreamSetMemory0(WS2812_DMA_STREAM, ws2812_frame_buffer);
  205. dmaStreamSetTransactionSize(WS2812_DMA_STREAM, WS2812_BIT_N);
  206. dmaStreamSetMode(WS2812_DMA_STREAM,
  207. STM32_DMA_CR_CHSEL(WS2812_DMA_CHANNEL) | STM32_DMA_CR_DIR_M2P | STM32_DMA_CR_PSIZE_WORD | STM32_DMA_CR_MSIZE_WORD |
  208. STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_PL(3));
  209. // M2P: Memory 2 Periph; PL: Priority Level
  210. // Start DMA
  211. dmaStreamEnable(WS2812_DMA_STREAM);
  212. // Configure PWM
  213. // NOTE: It's required that preload be enabled on the timer channel CCR register. This is currently enabled in the
  214. // 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,
  215. // disable counting, enable the channel, and then make whatever configuration changes we need.
  216. pwmStart(&WS2812_PWMD, &ws2812_pwm_config);
  217. pwmEnableChannel(&WS2812_PWMD, WS2812_TIM_CH, 0); // Initial period is 0; output will be low until first duty cycle is DMA'd in
  218. }
  219. ws2812_err_t ws2812_write_led(uint32_t led_number, uint8_t r, uint8_t g, uint8_t b)
  220. {
  221. // Check for valid LED
  222. if (led_number > WS2812_LED_N) return WS2812_LED_INVALID;
  223. // Write color to frame buffer
  224. for (uint32_t bit = 0; bit < 8; bit++) {
  225. ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  226. ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  227. ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
  228. }
  229. // Success
  230. return WS2812_SUCCESS;
  231. }
  232. void ws2812_setleds(LED_TYPE *ledarray, uint16_t number_of_leds)
  233. {
  234. for(int i = 0; i < number_of_leds; i++) {
  235. ws2812_write_led(i, ledarray[i].r, ledarray[i].g, ledarray[i].b);
  236. }
  237. }
  238. /** @} addtogroup WS2812 */