diff options
Diffstat (limited to 'platforms/chibios/drivers/ws2812_pwm.c')
| -rw-r--r-- | platforms/chibios/drivers/ws2812_pwm.c | 311 |
1 files changed, 311 insertions, 0 deletions
diff --git a/platforms/chibios/drivers/ws2812_pwm.c b/platforms/chibios/drivers/ws2812_pwm.c new file mode 100644 index 000000000..e6af55b6b --- /dev/null +++ b/platforms/chibios/drivers/ws2812_pwm.c | |||
| @@ -0,0 +1,311 @@ | |||
| 1 | #include "ws2812.h" | ||
| 2 | #include "quantum.h" | ||
| 3 | #include <hal.h> | ||
| 4 | |||
| 5 | /* Adapted from https://github.com/joewa/WS2812-LED-Driver_ChibiOS/ */ | ||
| 6 | |||
| 7 | #ifdef RGBW | ||
| 8 | # error "RGBW not supported" | ||
| 9 | #endif | ||
| 10 | |||
| 11 | #ifndef WS2812_PWM_DRIVER | ||
| 12 | # define WS2812_PWM_DRIVER PWMD2 // TIMx | ||
| 13 | #endif | ||
| 14 | #ifndef WS2812_PWM_CHANNEL | ||
| 15 | # define WS2812_PWM_CHANNEL 2 // Channel | ||
| 16 | #endif | ||
| 17 | #ifndef WS2812_PWM_PAL_MODE | ||
| 18 | # define WS2812_PWM_PAL_MODE 2 // DI Pin's alternate function value | ||
| 19 | #endif | ||
| 20 | #ifndef WS2812_DMA_STREAM | ||
| 21 | # define WS2812_DMA_STREAM STM32_DMA1_STREAM2 // DMA Stream for TIMx_UP | ||
| 22 | #endif | ||
| 23 | #ifndef WS2812_DMA_CHANNEL | ||
| 24 | # define WS2812_DMA_CHANNEL 2 // DMA Channel for TIMx_UP | ||
| 25 | #endif | ||
| 26 | #if (STM32_DMA_SUPPORTS_DMAMUX == TRUE) && !defined(WS2812_DMAMUX_ID) | ||
| 27 | # error "please consult your MCU's datasheet and specify in your config.h: #define WS2812_DMAMUX_ID STM32_DMAMUX1_TIM?_UP" | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #ifndef WS2812_PWM_COMPLEMENTARY_OUTPUT | ||
| 31 | # define WS2812_PWM_OUTPUT_MODE PWM_OUTPUT_ACTIVE_HIGH | ||
| 32 | #else | ||
| 33 | # if !STM32_PWM_USE_ADVANCED | ||
| 34 | # error "WS2812_PWM_COMPLEMENTARY_OUTPUT requires STM32_PWM_USE_ADVANCED == TRUE" | ||
| 35 | # endif | ||
| 36 | # define WS2812_PWM_OUTPUT_MODE PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH | ||
| 37 | #endif | ||
| 38 | |||
| 39 | // Push Pull or Open Drain Configuration | ||
| 40 | // Default Push Pull | ||
| 41 | #ifndef WS2812_EXTERNAL_PULLUP | ||
| 42 | # if defined(USE_GPIOV1) | ||
| 43 | # define WS2812_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL | ||
| 44 | # else | ||
| 45 | # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUPDR_FLOATING | ||
| 46 | # endif | ||
| 47 | #else | ||
| 48 | # if defined(USE_GPIOV1) | ||
| 49 | # define WS2812_OUTPUT_MODE PAL_MODE_STM32_ALTERNATE_OPENDRAIN | ||
| 50 | # else | ||
| 51 | # define WS2812_OUTPUT_MODE PAL_MODE_ALTERNATE(WS2812_PWM_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUPDR_FLOATING | ||
| 52 | # endif | ||
| 53 | #endif | ||
| 54 | |||
| 55 | #ifndef WS2812_PWM_TARGET_PERIOD | ||
| 56 | //# define WS2812_PWM_TARGET_PERIOD 800000 // Original code is 800k...? | ||
| 57 | # define WS2812_PWM_TARGET_PERIOD 80000 // TODO: work out why 10x less on f303/f4x1 | ||
| 58 | #endif | ||
| 59 | |||
| 60 | /* --- PRIVATE CONSTANTS ---------------------------------------------------- */ | ||
| 61 | |||
| 62 | #define WS2812_PWM_FREQUENCY (STM32_SYSCLK / 2) /**< Clock frequency of PWM, must be valid with respect to system clock! */ | ||
| 63 | #define WS2812_PWM_PERIOD (WS2812_PWM_FREQUENCY / WS2812_PWM_TARGET_PERIOD) /**< Clock period in ticks. 1 / 800kHz = 1.25 uS (as per datasheet) */ | ||
| 64 | |||
| 65 | /** | ||
| 66 | * @brief Number of bit-periods to hold the data line low at the end of a frame | ||
| 67 | * | ||
| 68 | * The reset period for each frame is defined in WS2812_TRST_US. | ||
| 69 | * Calculate the number of zeroes to add at the end assuming 1.25 uS/bit: | ||
| 70 | */ | ||
| 71 | #define WS2812_RESET_BIT_N (1000 * WS2812_TRST_US / 1250) | ||
| 72 | #define WS2812_COLOR_BIT_N (RGBLED_NUM * 24) /**< Number of data bits */ | ||
| 73 | #define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N) /**< Total number of bits in a frame */ | ||
| 74 | |||
| 75 | /** | ||
| 76 | * @brief High period for a zero, in ticks | ||
| 77 | * | ||
| 78 | * Per the datasheet: | ||
| 79 | * WS2812: | ||
| 80 | * - T0H: 200 nS to 500 nS, inclusive | ||
| 81 | * - T0L: 650 nS to 950 nS, inclusive | ||
| 82 | * WS2812B: | ||
| 83 | * - T0H: 200 nS to 500 nS, inclusive | ||
| 84 | * - T0L: 750 nS to 1050 nS, inclusive | ||
| 85 | * | ||
| 86 | * The duty cycle is calculated for a high period of 350 nS. | ||
| 87 | */ | ||
| 88 | #define WS2812_DUTYCYCLE_0 (WS2812_PWM_FREQUENCY / (1000000000 / 350)) | ||
| 89 | |||
| 90 | /** | ||
| 91 | * @brief High period for a one, in ticks | ||
| 92 | * | ||
| 93 | * Per the datasheet: | ||
| 94 | * WS2812: | ||
| 95 | * - T1H: 550 nS to 850 nS, inclusive | ||
| 96 | * - T1L: 450 nS to 750 nS, inclusive | ||
| 97 | * WS2812B: | ||
| 98 | * - T1H: 750 nS to 1050 nS, inclusive | ||
| 99 | * - T1L: 200 nS to 500 nS, inclusive | ||
| 100 | * | ||
| 101 | * The duty cycle is calculated for a high period of 800 nS. | ||
| 102 | * This is in the middle of the specifications of the WS2812 and WS2812B. | ||
| 103 | */ | ||
| 104 | #define WS2812_DUTYCYCLE_1 (WS2812_PWM_FREQUENCY / (1000000000 / 800)) | ||
| 105 | |||
| 106 | /* --- PRIVATE MACROS ------------------------------------------------------- */ | ||
| 107 | |||
| 108 | /** | ||
| 109 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given bit | ||
| 110 | * | ||
| 111 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 112 | * @param[in] byte: The byte number [0, 2] | ||
| 113 | * @param[in] bit: The bit number [0, 7] | ||
| 114 | * | ||
| 115 | * @return The bit index | ||
| 116 | */ | ||
| 117 | #define WS2812_BIT(led, byte, bit) (24 * (led) + 8 * (byte) + (7 - (bit))) | ||
| 118 | |||
| 119 | #if (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_GRB) | ||
| 120 | /** | ||
| 121 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit | ||
| 122 | * | ||
| 123 | * @note The red byte is the middle 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_RED_BIT(led, bit) WS2812_BIT((led), 1, (bit)) | ||
| 131 | |||
| 132 | /** | ||
| 133 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit | ||
| 134 | * | ||
| 135 | * @note The red byte is the first 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_GREEN_BIT(led, bit) WS2812_BIT((led), 0, (bit)) | ||
| 143 | |||
| 144 | /** | ||
| 145 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit | ||
| 146 | * | ||
| 147 | * @note The red byte is the last byte in the color packet | ||
| 148 | * | ||
| 149 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 150 | * @param[in] bit: The bit index [0, 7] | ||
| 151 | * | ||
| 152 | * @return The bit index | ||
| 153 | */ | ||
| 154 | # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit)) | ||
| 155 | |||
| 156 | #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_RGB) | ||
| 157 | /** | ||
| 158 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit | ||
| 159 | * | ||
| 160 | * @note The red byte is the middle byte in the color packet | ||
| 161 | * | ||
| 162 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 163 | * @param[in] bit: The bit number [0, 7] | ||
| 164 | * | ||
| 165 | * @return The bit index | ||
| 166 | */ | ||
| 167 | # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 0, (bit)) | ||
| 168 | |||
| 169 | /** | ||
| 170 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit | ||
| 171 | * | ||
| 172 | * @note The red byte is the first byte in the color packet | ||
| 173 | * | ||
| 174 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 175 | * @param[in] bit: The bit number [0, 7] | ||
| 176 | * | ||
| 177 | * @return The bit index | ||
| 178 | */ | ||
| 179 | # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit)) | ||
| 180 | |||
| 181 | /** | ||
| 182 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit | ||
| 183 | * | ||
| 184 | * @note The red byte is the last byte in the color packet | ||
| 185 | * | ||
| 186 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 187 | * @param[in] bit: The bit index [0, 7] | ||
| 188 | * | ||
| 189 | * @return The bit index | ||
| 190 | */ | ||
| 191 | # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit)) | ||
| 192 | |||
| 193 | #elif (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_BGR) | ||
| 194 | /** | ||
| 195 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit | ||
| 196 | * | ||
| 197 | * @note The red byte is the middle byte in the color packet | ||
| 198 | * | ||
| 199 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 200 | * @param[in] bit: The bit number [0, 7] | ||
| 201 | * | ||
| 202 | * @return The bit index | ||
| 203 | */ | ||
| 204 | # define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 2, (bit)) | ||
| 205 | |||
| 206 | /** | ||
| 207 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit | ||
| 208 | * | ||
| 209 | * @note The red byte is the first byte in the color packet | ||
| 210 | * | ||
| 211 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 212 | * @param[in] bit: The bit number [0, 7] | ||
| 213 | * | ||
| 214 | * @return The bit index | ||
| 215 | */ | ||
| 216 | # define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 1, (bit)) | ||
| 217 | |||
| 218 | /** | ||
| 219 | * @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit | ||
| 220 | * | ||
| 221 | * @note The red byte is the last byte in the color packet | ||
| 222 | * | ||
| 223 | * @param[in] led: The led index [0, @ref RGBLED_NUM) | ||
| 224 | * @param[in] bit: The bit index [0, 7] | ||
| 225 | * | ||
| 226 | * @return The bit index | ||
| 227 | */ | ||
| 228 | # define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 0, (bit)) | ||
| 229 | #endif | ||
| 230 | |||
| 231 | /* --- PRIVATE VARIABLES ---------------------------------------------------- */ | ||
| 232 | |||
| 233 | static uint32_t ws2812_frame_buffer[WS2812_BIT_N + 1]; /**< Buffer for a frame */ | ||
| 234 | |||
| 235 | /* --- PUBLIC FUNCTIONS ----------------------------------------------------- */ | ||
| 236 | /* | ||
| 237 | * Gedanke: Double-buffer type transactions: double buffer transfers using two memory pointers for | ||
| 238 | the memory (while the DMA is reading/writing from/to a buffer, the application can | ||
| 239 | write/read to/from the other buffer). | ||
| 240 | */ | ||
| 241 | |||
| 242 | void ws2812_init(void) { | ||
| 243 | // Initialize led frame buffer | ||
| 244 | uint32_t i; | ||
| 245 | for (i = 0; i < WS2812_COLOR_BIT_N; i++) ws2812_frame_buffer[i] = WS2812_DUTYCYCLE_0; // All color bits are zero duty cycle | ||
| 246 | for (i = 0; i < WS2812_RESET_BIT_N; i++) ws2812_frame_buffer[i + WS2812_COLOR_BIT_N] = 0; // All reset bits are zero | ||
| 247 | |||
| 248 | palSetLineMode(RGB_DI_PIN, WS2812_OUTPUT_MODE); | ||
| 249 | |||
| 250 | // PWM Configuration | ||
| 251 | //#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 | ||
| 252 | static const PWMConfig ws2812_pwm_config = { | ||
| 253 | .frequency = WS2812_PWM_FREQUENCY, | ||
| 254 | .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 | ||
| 255 | .callback = NULL, | ||
| 256 | .channels = | ||
| 257 | { | ||
| 258 | [0 ... 3] = {.mode = PWM_OUTPUT_DISABLED, .callback = NULL}, // Channels default to disabled | ||
| 259 | [WS2812_PWM_CHANNEL - 1] = {.mode = WS2812_PWM_OUTPUT_MODE, .callback = NULL}, // Turn on the channel we care about | ||
| 260 | }, | ||
| 261 | .cr2 = 0, | ||
| 262 | .dier = TIM_DIER_UDE, // DMA on update event for next period | ||
| 263 | }; | ||
| 264 | //#pragma GCC diagnostic pop // Restore command-line warning options | ||
| 265 | |||
| 266 | // Configure DMA | ||
| 267 | // dmaInit(); // Joe added this | ||
| 268 | dmaStreamAlloc(WS2812_DMA_STREAM - STM32_DMA_STREAM(0), 10, NULL, NULL); | ||
| 269 | dmaStreamSetPeripheral(WS2812_DMA_STREAM, &(WS2812_PWM_DRIVER.tim->CCR[WS2812_PWM_CHANNEL - 1])); // Ziel ist der An-Zeit im Cap-Comp-Register | ||
| 270 | dmaStreamSetMemory0(WS2812_DMA_STREAM, ws2812_frame_buffer); | ||
| 271 | dmaStreamSetTransactionSize(WS2812_DMA_STREAM, WS2812_BIT_N); | ||
| 272 | 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)); | ||
| 273 | // M2P: Memory 2 Periph; PL: Priority Level | ||
| 274 | |||
| 275 | #if (STM32_DMA_SUPPORTS_DMAMUX == TRUE) | ||
| 276 | // If the MCU has a DMAMUX we need to assign the correct resource | ||
| 277 | dmaSetRequestSource(WS2812_DMA_STREAM, WS2812_DMAMUX_ID); | ||
| 278 | #endif | ||
| 279 | |||
| 280 | // Start DMA | ||
| 281 | dmaStreamEnable(WS2812_DMA_STREAM); | ||
| 282 | |||
| 283 | // Configure PWM | ||
| 284 | // NOTE: It's required that preload be enabled on the timer channel CCR register. This is currently enabled in the | ||
| 285 | // 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, | ||
| 286 | // disable counting, enable the channel, and then make whatever configuration changes we need. | ||
| 287 | pwmStart(&WS2812_PWM_DRIVER, &ws2812_pwm_config); | ||
| 288 | 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 | ||
| 289 | } | ||
| 290 | |||
| 291 | void ws2812_write_led(uint16_t led_number, uint8_t r, uint8_t g, uint8_t b) { | ||
| 292 | // Write color to frame buffer | ||
| 293 | for (uint8_t bit = 0; bit < 8; bit++) { | ||
| 294 | ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0; | ||
| 295 | ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0; | ||
| 296 | ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | // Setleds for standard RGB | ||
| 301 | void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) { | ||
| 302 | static bool s_init = false; | ||
| 303 | if (!s_init) { | ||
| 304 | ws2812_init(); | ||
| 305 | s_init = true; | ||
| 306 | } | ||
| 307 | |||
| 308 | for (uint16_t i = 0; i < leds; i++) { | ||
| 309 | ws2812_write_led(i, ledarray[i].r, ledarray[i].g, ledarray[i].b); | ||
| 310 | } | ||
| 311 | } | ||
