Browse Source

pwm ws driver (not working)

pull/2666/head
Jack Humbert 6 years ago
parent
commit
7c19e9fa04
11 changed files with 359 additions and 638 deletions
  1. +224
    -535
      drivers/arm/ws2812.c
  2. +102
    -94
      drivers/arm/ws2812.h
  3. +2
    -0
      keyboards/planck/config.h
  4. +5
    -0
      keyboards/planck/rev6/config.h
  5. +1
    -1
      keyboards/planck/rev6/halconf.h
  6. +1
    -1
      keyboards/planck/rev6/mcuconf.h
  7. +4
    -0
      keyboards/planck/rev6/rev6.c
  8. +1
    -0
      keyboards/planck/rev6/rules.mk
  9. +14
    -6
      quantum/rgblight.c
  10. +3
    -1
      quantum/rgblight_types.h
  11. +2
    -0
      tmk_core/chibios.mk

+ 224
- 535
drivers/arm/ws2812.c View File

@ -1,556 +1,245 @@
/*
WS2812B CPU and memory efficient library
Date: 28.9.2016
Author: Martin Hubacek
http://www.martinhubacek.cz
@hubmartin
Licence: MIT License
*/
#include <string.h>
#include "stm32f3xx_hal.h"
/**
* @file ws2812.c
* @author Austin Glaser <austin.glaser@gmail.com>
* @brief WS2812 LED driver
*
* Copyright (C) 2016 Austin Glaser
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*
* @todo Put in names and descriptions of variables which need to be defined to use this file
*
* @addtogroup WS2812
* @{
*/
/* --- PRIVATE DEPENDENCIES ------------------------------------------------- */
// This Driver
#include "ws2812.h"
extern WS2812_Struct ws2812b;
// Define source arrays for my DMAs
uint32_t WS2812_IO_High[] = { WS2812B_PINS };
uint32_t WS2812_IO_Low[] = {WS2812B_PINS << 16};
// WS2812 framebuffer - buffer for 2 LEDs - two times 24 bits
uint16_t ws2812bDmaBitBuffer[24 * 2];
// Gamma correction table
const uint8_t gammaTable[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
static void ws2812b_gpio_init(void)
{
// WS2812B outputs
WS2812B_GPIO_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = WS2812B_PINS;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(WS2812B_PORT, &GPIO_InitStruct);
// Enable output pins for debuging to see DMA Full and Half transfer interrupts
#if defined(LED4_PORT) && defined(LED5_PORT)
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = LED4_PIN;
HAL_GPIO_Init(LED4_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LED5_PIN;
HAL_GPIO_Init(LED5_PORT, &GPIO_InitStruct);
#endif
}
TIM_HandleTypeDef Tim2Handle;
TIM_OC_InitTypeDef tim2OC1;
TIM_OC_InitTypeDef tim2OC2;
uint32_t tim_period;
static void TIM2_init(void)
{
// TIM2 Periph clock enable
__HAL_RCC_TIM2_CLK_ENABLE();
// This computation of pulse length should work ok,
// at some slower core speeds it needs some tuning.
tim_period = SystemCoreClock / 800000; // 0,125us period (10 times lower the 1,25us period to have fixed math below)
uint32_t cc1 = (10 * tim_period) / 36;
uint32_t cc2 = (10 * tim_period) / 15;
Tim2Handle.Instance = TIM2;
Tim2Handle.Init.Period = tim_period;
Tim2Handle.Init.RepetitionCounter = 0;
Tim2Handle.Init.Prescaler = 0;
Tim2Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
Tim2Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_PWM_Init(&Tim2Handle);
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
tim2OC1.OCMode = TIM_OCMODE_PWM1;
tim2OC1.OCPolarity = TIM_OCPOLARITY_HIGH;
tim2OC1.Pulse = cc1;
tim2OC1.OCNPolarity = TIM_OCNPOLARITY_HIGH;
tim2OC1.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&Tim2Handle, &tim2OC1, TIM_CHANNEL_1);
tim2OC2.OCMode = TIM_OCMODE_PWM1;
tim2OC2.OCPolarity = TIM_OCPOLARITY_HIGH;
tim2OC2.Pulse = cc2;
tim2OC2.OCNPolarity = TIM_OCNPOLARITY_HIGH;
tim2OC2.OCFastMode = TIM_OCFAST_DISABLE;
tim2OC2.OCIdleState = TIM_OCIDLESTATE_RESET;
tim2OC2.OCNIdleState = TIM_OCNIDLESTATE_RESET;
HAL_TIM_PWM_ConfigChannel(&Tim2Handle, &tim2OC2, TIM_CHANNEL_2);
HAL_TIM_Base_Start(&Tim2Handle);
HAL_TIM_PWM_Start(&Tim2Handle, TIM_CHANNEL_1);
}
DMA_HandleTypeDef dmaUpdate;
DMA_HandleTypeDef dmaCC1;
DMA_HandleTypeDef dmaCC2;
#define BUFFER_SIZE (sizeof(ws2812bDmaBitBuffer)/sizeof(uint16_t))
static void DMA_init(void)
{
// TIM2 Update event
__HAL_RCC_DMA1_CLK_ENABLE();
dmaUpdate.Init.Direction = DMA_MEMORY_TO_PERIPH;
dmaUpdate.Init.PeriphInc = DMA_PINC_DISABLE;
dmaUpdate.Init.MemInc = DMA_MINC_DISABLE;
dmaUpdate.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
dmaUpdate.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
dmaUpdate.Init.Mode = DMA_CIRCULAR;
dmaUpdate.Init.Priority = DMA_PRIORITY_VERY_HIGH;
dmaUpdate.Instance = DMA1_Channel2;
//dmaUpdate.XferCpltCallback = TransferComplete;
//dmaUpdate.XferErrorCallback = TransferError;
HAL_DMA_Init(&dmaUpdate);
//HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0);
//HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);
HAL_DMA_Start(&dmaUpdate, (uint32_t)WS2812_IO_High, (uint32_t)&WS2812B_PORT->BSRR, BUFFER_SIZE);
// TIM2 CC1 event
dmaCC1.Init.Direction = DMA_MEMORY_TO_PERIPH;
dmaCC1.Init.PeriphInc = DMA_PINC_DISABLE;
dmaCC1.Init.MemInc = DMA_MINC_ENABLE;
dmaCC1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
dmaCC1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
dmaCC1.Init.Mode = DMA_CIRCULAR;
dmaCC1.Init.Priority = DMA_PRIORITY_VERY_HIGH;
dmaCC1.Instance = DMA1_Channel5;
//dmaUpdate.XferCpltCallback = TransferComplete;
//dmaUpdate.XferErrorCallback = TransferError;
//dmaUpdate.XferHalfCpltCallback = TransferHalf;
//HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
//HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
HAL_DMA_Init(&dmaCC1);
HAL_DMA_Start(&dmaCC1, (uint32_t)ws2812bDmaBitBuffer, (uint32_t)&WS2812B_PORT->BRR, BUFFER_SIZE);
// TIM2 CC2 event
dmaCC2.Init.Direction = DMA_MEMORY_TO_PERIPH;
dmaCC2.Init.PeriphInc = DMA_PINC_DISABLE;
dmaCC2.Init.MemInc = DMA_MINC_DISABLE;
dmaCC2.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
dmaCC2.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
dmaCC2.Init.Mode = DMA_CIRCULAR;
dmaCC2.Init.Priority = DMA_PRIORITY_VERY_HIGH;
dmaCC2.Instance = DMA1_Channel7;
dmaCC2.XferCpltCallback = DMA_TransferCompleteHandler;
dmaCC2.XferHalfCpltCallback = DMA_TransferHalfHandler;
//dmaUpdate.XferErrorCallback = TransferError;
HAL_DMA_Init(&dmaCC2);
HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn);
HAL_DMA_Start_IT(&dmaCC2, (uint32_t)WS2812_IO_Low, (uint32_t)&WS2812B_PORT->BSRR, BUFFER_SIZE);
}
/*
void DMA1_Channel2_IRQHandler(void)
{
// Check the interrupt and clear flag
HAL_DMA_IRQHandler(&dmaUpdate);
}
void DMA1_Channel5_IRQHandler(void)
{
// Check the interrupt and clear flag
HAL_DMA_IRQHandler(&dmaCC1);
}*/
void DMA1_Channel7_IRQHandler(void)
{
// Check the interrupt and clear flag
HAL_DMA_IRQHandler(&dmaCC2);
}
static void loadNextFramebufferData(WS2812_BufferItem *bItem, uint32_t row)
{
uint32_t r = bItem->frameBufferPointer[bItem->frameBufferCounter++];
uint32_t g = bItem->frameBufferPointer[bItem->frameBufferCounter++];
uint32_t b = bItem->frameBufferPointer[bItem->frameBufferCounter++];
if(bItem->frameBufferCounter == bItem->frameBufferSize)
bItem->frameBufferCounter = 0;
ws2812b_set_pixel(bItem->channel, row, r, g, b);
}
// Transmit the framebuffer
static void WS2812_sendbuf()
{
// transmission complete flag
ws2812b.transferComplete = 0;
uint32_t i;
for( i = 0; i < WS2812_BUFFER_COUNT; i++ )
{
ws2812b.item[i].frameBufferCounter = 0;
// Standard
#include <stdint.h>
loadNextFramebufferData(&ws2812b.item[i], 0); // ROW 0
loadNextFramebufferData(&ws2812b.item[i], 1); // ROW 0
}
// ChibiOS
#include "ch.h"
#include "hal.h"
// clear all DMA flags
__HAL_DMA_CLEAR_FLAG(&dmaUpdate, DMA_FLAG_TC2 | DMA_FLAG_HT2 | DMA_FLAG_TE2);
__HAL_DMA_CLEAR_FLAG(&dmaCC1, DMA_FLAG_TC5 | DMA_FLAG_HT5 | DMA_FLAG_TE5);
__HAL_DMA_CLEAR_FLAG(&dmaCC2, DMA_FLAG_TC7 | DMA_FLAG_HT7 | DMA_FLAG_TE7);
// Application
#include "board.h"
#include "util.h"
// configure the number of bytes to be transferred by the DMA controller
dmaUpdate.Instance->CNDTR = BUFFER_SIZE;
dmaCC1.Instance->CNDTR = BUFFER_SIZE;
dmaCC2.Instance->CNDTR = BUFFER_SIZE;
/* --- CONFIGURATION CHECK -------------------------------------------------- */
// clear all TIM2 flags
__HAL_TIM_CLEAR_FLAG(&Tim2Handle, TIM_FLAG_UPDATE | TIM_FLAG_CC1 | TIM_FLAG_CC2 | TIM_FLAG_CC3 | TIM_FLAG_CC4);
// enable DMA channels
__HAL_DMA_ENABLE(&dmaUpdate);
__HAL_DMA_ENABLE(&dmaCC1);
__HAL_DMA_ENABLE(&dmaCC2);
// IMPORTANT: enable the TIM2 DMA requests AFTER enabling the DMA channels!
__HAL_TIM_ENABLE_DMA(&Tim2Handle, TIM_DMA_UPDATE);
__HAL_TIM_ENABLE_DMA(&Tim2Handle, TIM_DMA_CC1);
__HAL_TIM_ENABLE_DMA(&Tim2Handle, TIM_DMA_CC2);
TIM2->CNT = tim_period-1;
// start TIM2
__HAL_TIM_ENABLE(&Tim2Handle);
}
void DMA_TransferHalfHandler(DMA_HandleTypeDef *DmaHandle)
{
#if defined(LED4_PORT)
LED4_PORT->BSRR = LED4_PIN;
#endif
// Is this the last LED?
if(ws2812b.repeatCounter != (WS2812B_NUMBER_OF_LEDS / 2 - 1))
{
uint32_t i;
for( i = 0; i < WS2812_BUFFER_COUNT; i++ )
{
loadNextFramebufferData(&ws2812b.item[i], 0);
}
} else {
// If this is the last pixel, set the next pixel value to zeros, because
// the DMA would not stop exactly at the last bit.
ws2812b_set_pixel(0, 0, 0, 0, 0);
}
#if defined(LED4_PORT)
LED4_PORT->BRR = LED4_PIN;
#endif
}
#if !defined(WS2812_LED_N)
#error WS2812 LED chain length not specified
#elif WS2812_LED_N <= 0
#error WS2812 LED chain length set to invalid value
#endif
void DMA_TransferCompleteHandler(DMA_HandleTypeDef *DmaHandle)
{
#if defined(LED5_PORT)
LED5_PORT->BSRR = LED5_PIN;
#endif
ws2812b.repeatCounter++;
if(ws2812b.repeatCounter == WS2812B_NUMBER_OF_LEDS / 2)
{
// Transfer of all LEDs is done, disable DMA but enable tiemr update IRQ to stop the 50us pulse
ws2812b.repeatCounter = 0;
// Enable TIM2 Update interrupt for 50us Treset signal
__HAL_TIM_ENABLE_IT(&Tim2Handle, TIM_IT_UPDATE);
// Disable DMA
__HAL_DMA_DISABLE(&dmaUpdate);
__HAL_DMA_DISABLE(&dmaCC1);
__HAL_DMA_DISABLE(&dmaCC2);
// Disable the DMA requests
__HAL_TIM_DISABLE_DMA(&Tim2Handle, TIM_DMA_UPDATE);
__HAL_TIM_DISABLE_DMA(&Tim2Handle, TIM_DMA_CC1);
__HAL_TIM_DISABLE_DMA(&Tim2Handle, TIM_DMA_CC2);
// Manually set outputs to low to generate 50us reset impulse
WS2812B_PORT->BSRR = WS2812_IO_Low[0];
} else {
// Load bitbuffer with next RGB LED values
uint32_t i;
for( i = 0; i < WS2812_BUFFER_COUNT; i++ )
{
loadNextFramebufferData(&ws2812b.item[i], 1);
}
}
#if defined(LED5_PORT)
LED5_PORT->BRR = LED5_PIN;
#endif
}
#if !defined(WS2812_TIM_N)
#error WS2812 timer not specified
#elif WS2812_TIM_N == 1
#define WS2812_DMA_STREAM STM32_DMA1_STREAM5
#elif WS2812_TIM_N == 2
#define WS2812_DMA_STREAM STM32_DMA1_STREAM2
#elif WS2812_TIM_N == 3
#define WS2812_DMA_STREAM STM32_DMA1_STREAM3
#elif WS2812_TIM_N == 4
#define WS2812_DMA_STREAM STM32_DMA1_STREAM7
#else
#error WS2812 timer set to invalid value
#endif
#if !defined(WS2812_TIM_CH)
#error WS2812 timer channel not specified
#elif WS2812_TIM_CH >= 4
#error WS2812 timer channel set to invalid value
#endif
void TIM2_IRQHandler(void)
/* --- PRIVATE CONSTANTS ---------------------------------------------------- */
#define WS2812_PWM_FREQUENCY (72000000) /**< Clock frequency of PWM */
#define WS2812_PWM_PERIOD (90) /**< Clock period in ticks. 90/(72 MHz) = 1.25 uS (as per datasheet) */
/**
* @brief Number of bit-periods to hold the data line low at the end of a frame
*
* The reset period for each frame must be at least 50 uS; so we add in 50 bit-times
* of zeroes at the end. (50 bits)*(1.25 uS/bit) = 62.5 uS, which gives us some
* slack in the timing requirements
*/
#define WS2812_RESET_BIT_N (50)
#define WS2812_COLOR_BIT_N (WS2812_LED_N*24) /**< Number of data bits */
#define WS2812_BIT_N (WS2812_COLOR_BIT_N + WS2812_RESET_BIT_N) /**< Total number of bits in a frame */
/**
* @brief High period for a zero, in ticks
*
* Per the datasheet:
* - T0H: 0.200 uS to 0.500 uS, inclusive
* - T0L: 0.650 uS to 0.950 uS, inclusive
*
* With a duty cycle of 22 ticks, we have a high period of 22/(72 MHz) = 3.06 uS, and
* a low period of (90 - 22)/(72 MHz) = 9.44 uS. These values are within the allowable
* bounds, and intentionally skewed as far to the low duty-cycle side as possible
*/
#define WS2812_DUTYCYCLE_0 (22)
/**
* @brief High period for a one, in ticks
*
* Per the datasheet:
* - T0H: 0.550 uS to 0.850 uS, inclusive
* - T0L: 0.450 uS to 0.750 uS, inclusive
*
* With a duty cycle of 56 ticks, we have a high period of 56/(72 MHz) = 7.68 uS, and
* a low period of (90 - 56)/(72 MHz) = 4.72 uS. These values are within the allowable
* bounds, and intentionally skewed as far to the high duty-cycle side as possible
*/
#define WS2812_DUTYCYCLE_1 (56)
/* --- PRIVATE MACROS ------------------------------------------------------- */
/**
* @brief Generates a reference to a numbered PWM driver
*
* @param[in] n: The driver (timer) number
*
* @return A reference to the driver
*/
#define PWMD(n) CONCAT_EXPANDED_SYMBOLS(PWMD, n)
#define WS2812_PWMD PWMD(WS2812_TIM_N) /**< The PWM driver to use for the LED chain */
/**
* @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given bit
*
* @param[in] led: The led index [0, @ref WS2812_LED_N)
* @param[in] byte: The byte number [0, 2]
* @param[in] bit: The bit number [0, 7]
*
* @return The bit index
*/
#define WS2812_BIT(led, byte, bit) (24*(led) + 8*(byte) + (7 - (bit)))
/**
* @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given red bit
*
* @note The red byte is the middle byte in the color packet
*
* @param[in] led: The led index [0, @ref WS2812_LED_N)
* @param[in] bit: The bit number [0, 7]
*
* @return The bit index
*/
#define WS2812_RED_BIT(led, bit) WS2812_BIT((led), 1, (bit))
/**
* @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given green bit
*
* @note The red byte is the first byte in the color packet
*
* @param[in] led: The led index [0, @ref WS2812_LED_N)
* @param[in] bit: The bit number [0, 7]
*
* @return The bit index
*/
#define WS2812_GREEN_BIT(led, bit) WS2812_BIT((led), 0, (bit))
/**
* @brief Determine the index in @ref ws2812_frame_buffer "the frame buffer" of a given blue bit
*
* @note The red byte is the last byte in the color packet
*
* @param[in] led: The led index [0, @ref WS2812_LED_N)
* @param[in] bit: The bit index [0, 7]
*
* @return The bit index
*/
#define WS2812_BLUE_BIT(led, bit) WS2812_BIT((led), 2, (bit))
/* --- PRIVATE VARIABLES ---------------------------------------------------- */
static uint8_t ws2812_frame_buffer[WS2812_BIT_N]; /**< Buffer for a frame */
/* --- PUBLIC FUNCTIONS ----------------------------------------------------- */
void ws2812_init(void)
{
HAL_TIM_IRQHandler(&Tim2Handle);
// Initialize led frame buffer
uint32_t i;
for (i = 0; i < WS2812_COLOR_BIT_N; i++) ws2812_frame_buffer[i] = WS2812_DUTYCYCLE_0; // All color bits are zero duty cycle
for (i = 0; i < WS2812_RESET_BIT_N; i++) ws2812_frame_buffer[i + WS2812_COLOR_BIT_N] = 0; // All reset bits are zero
// Configure PA0 as AF output
palSetPadMode(GPIOA, 1, PAL_MODE_ALTERNATE(1));
// PWM Configuration
#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
static const PWMConfig ws2812_pwm_config = {
.frequency = WS2812_PWM_FREQUENCY,
.period = WS2812_PWM_PERIOD,
.callback = NULL,
.channels = {
[0 ... 3] = {.mode = PWM_OUTPUT_DISABLED, .callback = NULL}, // Channels default to disabled
[WS2812_TIM_CH] = {.mode = PWM_OUTPUT_ACTIVE_HIGH, .callback = NULL}, // Turn on the channel we care about
},
.cr2 = 0,
.dier = TIM_DIER_UDE, // DMA on update event for next period
};
#pragma GCC diagnostic pop // Restore command-line warning options
// Configure DMA
dmaStreamAllocate(WS2812_DMA_STREAM, 10, NULL, NULL);
dmaStreamSetPeripheral(WS2812_DMA_STREAM, &(WS2812_PWMD.tim->CCR[WS2812_TIM_CH]));
dmaStreamSetMemory0(WS2812_DMA_STREAM, ws2812_frame_buffer);
dmaStreamSetTransactionSize(WS2812_DMA_STREAM, WS2812_BIT_N);
dmaStreamSetMode(WS2812_DMA_STREAM,
STM32_DMA_CR_DIR_M2P | STM32_DMA_CR_PSIZE_WORD | STM32_DMA_CR_MSIZE_BYTE |
STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_PL(3));
// Start DMA
dmaStreamEnable(WS2812_DMA_STREAM);
// Configure PWM
// NOTE: It's required that preload be enabled on the timer channel CCR register. This is currently enabled in the
// 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,
// disable counting, enable the channel, and then make whatever configuration changes we need.
pwmStart(&WS2812_PWMD, &ws2812_pwm_config);
pwmEnableChannel(&WS2812_PWMD, WS2812_TIM_CH, 0); // Initial period is 0; output will be low until first duty cycle is DMA'd in
}
// TIM2 Interrupt Handler gets executed on every TIM2 Update if enabled
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
ws2812_err_t ws2812_write_led(uint32_t led_number, uint8_t r, uint8_t g, uint8_t b)
{
// I have to wait 50us to generate Treset signal
if (ws2812b.timerPeriodCounter < (uint8_t)WS2812_RESET_PERIOD)
{
// count the number of timer periods
ws2812b.timerPeriodCounter++;
}
else
{
ws2812b.timerPeriodCounter = 0;
__HAL_TIM_DISABLE(&Tim2Handle);
TIM2->CR1 = 0; // disable timer
// disable the TIM2 Update
__HAL_TIM_DISABLE_IT(&Tim2Handle, TIM_IT_UPDATE);
// set TransferComplete flag
ws2812b.transferComplete = 1;
}
// Check for valid LED
if (led_number >= WS2812_LED_N) return WS2812_LED_INVALID;
// Write color to frame buffer
uint32_t bit;
for (bit = 0; bit < 8; bit++) {
ws2812_frame_buffer[WS2812_RED_BIT(led_number, bit)] = ((r >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
ws2812_frame_buffer[WS2812_GREEN_BIT(led_number, bit)] = ((g >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
ws2812_frame_buffer[WS2812_BLUE_BIT(led_number, bit)] = ((b >> bit) & 0x01) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
}
// Success
return WS2812_SUCCESS;
}
/** @} addtogroup WS2812 */
static void ws2812b_set_pixel(uint8_t row, uint16_t column, uint8_t red, uint8_t green, uint8_t blue)
{
// Apply gamma
red = gammaTable[red];
green = gammaTable[green];
blue = gammaTable[blue];
uint32_t calcCol = (column*24);
uint32_t invRed = ~red;
uint32_t invGreen = ~green;
uint32_t invBlue = ~blue;
#if defined(SETPIX_1)
uint8_t i;
uint32_t calcClearRow = ~(0x01<<row);
for (i = 0; i < 8; i++)
{
// clear the data for pixel
ws2812bDmaBitBuffer[(calcCol+i)] &= calcClearRow;
ws2812bDmaBitBuffer[(calcCol+8+i)] &= calcClearRow;
ws2812bDmaBitBuffer[(calcCol+16+i)] &= calcClearRow;
// write new data for pixel
ws2812bDmaBitBuffer[(calcCol+i)] |= (((((invGreen)<<i) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+8+i)] |= (((((invRed)<<i) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+16+i)] |= (((((invBlue)<<i) & 0x80)>>7)<<row);
}
#elif defined(SETPIX_2)
uint8_t i;
for (i = 0; i < 8; i++)
{
// Set or clear the data for the pixel
if(((invGreen)<<i) & 0x80)
varSetBit(ws2812bDmaBitBuffer[(calcCol+i)], row);
else
varResetBit(ws2812bDmaBitBuffer[(calcCol+i)], row);
if(((invRed)<<i) & 0x80)
varSetBit(ws2812bDmaBitBuffer[(calcCol+8+i)], row);
else
varResetBit(ws2812bDmaBitBuffer[(calcCol+8+i)], row);
if(((invBlue)<<i) & 0x80)
varSetBit(ws2812bDmaBitBuffer[(calcCol+16+i)], row);
else
varResetBit(ws2812bDmaBitBuffer[(calcCol+16+i)], row);
}
#elif defined(SETPIX_3)
ws2812bDmaBitBuffer[(calcCol+0)] |= (((((invGreen)<<0) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+8+0)] |= (((((invRed)<<0) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+16+0)] |= (((((invBlue)<<0) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+1)] |= (((((invGreen)<<1) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+8+1)] |= (((((invRed)<<1) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+16+1)] |= (((((invBlue)<<1) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+2)] |= (((((invGreen)<<2) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+8+2)] |= (((((invRed)<<2) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+16+2)] |= (((((invBlue)<<2) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+3)] |= (((((invGreen)<<3) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+8+3)] |= (((((invRed)<<3) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+16+3)] |= (((((invBlue)<<3) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+4)] |= (((((invGreen)<<4) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+8+4)] |= (((((invRed)<<4) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+16+4)] |= (((((invBlue)<<4) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+5)] |= (((((invGreen)<<5) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+8+5)] |= (((((invRed)<<5) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+16+5)] |= (((((invBlue)<<5) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+6)] |= (((((invGreen)<<6) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+8+6)] |= (((((invRed)<<6) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+16+6)] |= (((((invBlue)<<6) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+7)] |= (((((invGreen)<<7) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+8+7)] |= (((((invRed)<<7) & 0x80)>>7)<<row);
ws2812bDmaBitBuffer[(calcCol+16+7)] |= (((((invBlue)<<7) & 0x80)>>7)<<row);
#elif defined(SETPIX_4)
// Bitband optimizations with pure increments, 5us interrupts
uint32_t *bitBand = BITBAND_SRAM(&ws2812bDmaBitBuffer[(calcCol)], row);
*bitBand = (invGreen >> 7);
bitBand+=16;
*bitBand = (invGreen >> 6);
bitBand+=16;
*bitBand = (invGreen >> 5);
bitBand+=16;
*bitBand = (invGreen >> 4);
bitBand+=16;
*bitBand = (invGreen >> 3);
bitBand+=16;
*bitBand = (invGreen >> 2);
bitBand+=16;
*bitBand = (invGreen >> 1);
bitBand+=16;
*bitBand = (invGreen >> 0);
bitBand+=16;
// RED
*bitBand = (invRed >> 7);
bitBand+=16;
*bitBand = (invRed >> 6);
bitBand+=16;
*bitBand = (invRed >> 5);
bitBand+=16;
*bitBand = (invRed >> 4);
bitBand+=16;
*bitBand = (invRed >> 3);
bitBand+=16;
*bitBand = (invRed >> 2);
bitBand+=16;
*bitBand = (invRed >> 1);
bitBand+=16;
*bitBand = (invRed >> 0);
bitBand+=16;
// BLUE
*bitBand = (invBlue >> 7);
bitBand+=16;
*bitBand = (invBlue >> 6);
bitBand+=16;
*bitBand = (invBlue >> 5);
bitBand+=16;
*bitBand = (invBlue >> 4);
bitBand+=16;
*bitBand = (invBlue >> 3);
bitBand+=16;
*bitBand = (invBlue >> 2);
bitBand+=16;
*bitBand = (invBlue >> 1);
bitBand+=16;
*bitBand = (invBlue >> 0);
bitBand+=16;
#endif
void ws2812_setleds(LED_TYPE *ledarray, uint16_t number_of_leds) {
ws2812_init();
uint8_t i = 0;
while (i < number_of_leds) {
ws2812_write_led(i, ledarray[i].r, ledarray[i].g, ledarray[i].b);
i++;
}
}
void ws2812b_init()
{
ws2812b_gpio_init();
DMA_init();
TIM2_init();
void ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t number_of_leds) {
// Need to start the first transfer
ws2812b.transferComplete = 1;
}
void ws2812b_handle()
{
if(ws2812b.startTransfer) {
ws2812b.startTransfer = 0;
WS2812_sendbuf();
}
}

+ 102
- 94
drivers/arm/ws2812.h View File

@ -1,94 +1,102 @@
/*
WS2812B CPU and memory efficient library
Date: 28.9.2016
Author: Martin Hubacek
http://www.martinhubacek.cz
@hubmartin
Licence: MIT License
*/
#ifndef WS2812B_H_
#define WS2812B_H_
#include "ws2812.h"
// GPIO enable command
#define WS2812B_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
// LED output port
#define WS2812B_PORT GPIOC
// LED output pins
#define WS2812B_PINS (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3)
// How many LEDs are in the series
#define WS2812B_NUMBER_OF_LEDS 60
// Number of output LED strips. Each has its own buffer.
#define WS2812_BUFFER_COUNT 2
// Choose one of the bit-juggling setpixel implementation
// *******************************************************
//#define SETPIX_1 // For loop, works everywhere, slow
//#define SETPIX_2 // Bit band in a loop
//#define SETPIX_3 // Like SETPIX_1 but with unrolled loop
#define SETPIX_4 // Fastest copying using bit-banding
// DEBUG OUTPUT
// ********************
#define LED4_PORT GPIOC
#define LED4_PIN GPIO_PIN_10
#define LED5_PORT GPIOC
#define LED5_PIN GPIO_PIN_10
// Public functions
// ****************
void ws2812b_init();
void ws2812b_handle();
// Library structures
// ******************
// This value sets number of periods to generate 50uS Treset signal
#define WS2812_RESET_PERIOD 12
typedef struct WS2812_BufferItem {
uint8_t* frameBufferPointer;
uint32_t frameBufferSize;
uint32_t frameBufferCounter;
uint8_t channel; // digital output pin/channel
} WS2812_BufferItem;
typedef struct WS2812_Struct
{
WS2812_BufferItem item[WS2812_BUFFER_COUNT];
uint8_t transferComplete;
uint8_t startTransfer;
uint32_t timerPeriodCounter;
uint32_t repeatCounter;
} WS2812_Struct;
WS2812_Struct ws2812b;
// Bit band stuff
#define RAM_BASE 0x20000000
#define RAM_BB_BASE 0x22000000
#define Var_ResetBit_BB(VarAddr, BitNumber) (*(volatile uint32_t *) (RAM_BB_BASE | ((VarAddr - RAM_BASE) << 5) | ((BitNumber) << 2)) = 0)
#define Var_SetBit_BB(VarAddr, BitNumber) (*(volatile uint32_t *) (RAM_BB_BASE | ((VarAddr - RAM_BASE) << 5) | ((BitNumber) << 2)) = 1)
#define Var_GetBit_BB(VarAddr, BitNumber) (*(volatile uint32_t *) (RAM_BB_BASE | ((VarAddr - RAM_BASE) << 5) | ((BitNumber) << 2)))
#define BITBAND_SRAM(address, bit) ( (__IO uint32_t *) (RAM_BB_BASE + (((uint32_t)address) - RAM_BASE) * 32 + (bit) * 4))
#define varSetBit(var,bit) (Var_SetBit_BB((uint32_t)&var,bit))
#define varResetBit(var,bit) (Var_ResetBit_BB((uint32_t)&var,bit))
#define varGetBit(var,bit) (Var_GetBit_BB((uint32_t)&var,bit))
static void ws2812b_set_pixel(uint8_t row, uint16_t column, uint8_t red, uint8_t green, uint8_t blue);
void DMA_TransferCompleteHandler(DMA_HandleTypeDef *DmaHandle);
void DMA_TransferHalfHandler(DMA_HandleTypeDef *DmaHandle);
#endif /* WS2812B_H_ */
/**
* @file ws2812.h
* @author Austin Glaser <austin.glaser@gmail.com>
* @brief Interface to WS2812 LED driver
*
* Copyright (C) 2016 Austin Glaser
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*
* @todo Put in names and descriptions of variables which need to be defined to use this file
*/
#ifndef WS2812_H_
#define WS2812_H_
/**
* @defgroup WS2812 WS2812 Driver
* @{
*
* @brief DMA-based WS2812 LED driver
*
* A driver for WS2812 LEDs
*/
/* --- PUBLIC DEPENDENCIES -------------------------------------------------- */
// Standard
#include <stdint.h>
#include "rgblight_types.h"
/* --- PUBLIC CONSTANTS ----------------------------------------------------- */
/**
* @brief Return codes from ws2812 interface functions
*/
typedef enum {
WS2812_SUCCESS = 0x00, /**< Operation completeed successfully */
WS2812_LED_INVALID, /**< Attempted to index an invalid LED (@ref WS2812_N_LEDS) */
MAX_WS2812_ERR, /**< Total number of possible error codes */
WS2812_ERR_INVALID /**< Invalid error value */
} ws2812_err_t;
/* --- PUBLIC FUNCTIONS ----------------------------------------------------- */
/**
* @brief Initialize the driver
*
* After this function is called, all necessary background tasks will be started.
* The frame is initially dark.
*/
void ws2812_init(void);
/**
* @brief Write the value of a single LED in the chain
*
* The color value is written to a frame buffer, and will not
* be updated until the next frame is written. Frames are written
* at the maximum possible speed -- the longest latency between a
* call to this function and the value being displayed is
* 1.25uS*(24*@ref WS2812_LED_N + 50)
*
* @param[in] led_number: The index of the LED to be written. Must be strictly less than
* @ref WS2812_N_LEDS
* @param[in] r: The red level of the LED
* @param[in] g: The green level of the LED
* @param[in] b: The blue level of the LED
*
* @retval WS2812_SUCCESS: The write was successful
* @retval WS2812_LED_INVALID: The write was to an invalid LED index
*/
ws2812_err_t ws2812_write_led(uint32_t led_number, uint8_t r, uint8_t g, uint8_t b);
/** @} defgroup WS2812 */
void ws2812_setleds(LED_TYPE *ledarray, uint16_t number_of_leds);
void ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t number_of_leds);
/**
* @brief Concatenates two symbols s1 and s2 exactly, without expanding either
*
* @param[in] s1: The first symbol to concatenate
* @param[in] s2: The second symbol to concatenate
*
* @return A single symbol containing s1 and s2 concatenated without expansion
*/
#define CONCAT_SYMBOLS(s1, s2) s1##s2
/**
* @brief Concatenate the symbols s1 and s2, expanding both of them
*
* This is important because simply applying s1##s2 doesn't expand them if they're
* preprocessor tokens themselves
*
* @param[in] s1: The first symbol to concatenate
* @param[in] s2: The second symbol to concatenate
*
* @return A single symbol containing s1 expanded followed by s2 expanded
*/
#define CONCAT_EXPANDED_SYMBOLS(s1, s2) CONCAT_SYMBOLS(s1, s2)
#endif /* WS2812_H_ */

+ 2
- 0
keyboards/planck/config.h View File

@ -18,7 +18,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef CONFIG_H
#define CONFIG_H
#ifdef __AVR__
#include "config_common.h"
#endif
/* USB Device descriptor parameter */
#define VENDOR_ID 0xFEED


+ 5
- 0
keyboards/planck/rev6/config.h View File

@ -125,4 +125,9 @@
/* override number of MIDI tone keycodes (each octave adds 12 keycodes and allocates 12 bytes) */
//#define MIDI_TONE_KEYCODE_OCTAVES 1
#define WS2812_LED_N 1
#define RGBLED_NUM WS2812_LED_N
#define WS2812_TIM_N 1
#define WS2812_TIM_CH 1
#endif

+ 1
- 1
keyboards/planck/rev6/halconf.h View File

@ -111,7 +111,7 @@
* @brief Enables the PWM subsystem.
*/
#if !defined(HAL_USE_PWM) || defined(__DOXYGEN__)
#define HAL_USE_PWM FALSE
#define HAL_USE_PWM TRUE
#endif
/**


+ 1
- 1
keyboards/planck/rev6/mcuconf.h View File

@ -182,7 +182,7 @@
* PWM driver system settings.
*/
#define STM32_PWM_USE_ADVANCED FALSE
#define STM32_PWM_USE_TIM1 FALSE
#define STM32_PWM_USE_TIM1 TRUE
#define STM32_PWM_USE_TIM2 FALSE
#define STM32_PWM_USE_TIM3 FALSE
#define STM32_PWM_USE_TIM4 FALSE


+ 4
- 0
keyboards/planck/rev6/rev6.c View File

@ -14,8 +14,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rev6.h"
#include "rgblight.h"
void matrix_init_kb(void) {
rgblight_enable();
rgblight_mode(1);
rgblight_setrgb(0xFF, 0xFF, 0xFF);
matrix_init_user();
}


+ 1
- 0
keyboards/planck/rev6/rules.mk View File

@ -52,4 +52,5 @@ COMMAND_ENABLE = yes # Commands for debug and configuration
NKRO_ENABLE = yes # USB Nkey Rollover
CUSTOM_MATRIX = yes # Custom matrix file
AUDIO_ENABLE = yes
RGBLIGHT_ENABLE = yes
# SERIAL_LINK_ENABLE = yes

+ 14
- 6
quantum/rgblight.c View File

@ -14,9 +14,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#ifdef __AVR__
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#endif
#include "wait.h"
#include "progmem.h"
#include "timer.h"
#include "rgblight.h"
@ -113,10 +115,16 @@ void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
uint32_t eeconfig_read_rgblight(void) {
return eeprom_read_dword(EECONFIG_RGBLIGHT);
#ifdef __AVR__
return eeprom_read_dword(EECONFIG_RGBLIGHT);
#else
return 0;
#endif
}
void eeconfig_update_rgblight(uint32_t val) {
eeprom_update_dword(EECONFIG_RGBLIGHT, val);
#ifdef __AVR__
eeprom_update_dword(EECONFIG_RGBLIGHT, val);
#endif
}
void eeconfig_update_rgblight_default(void) {
dprintf("eeconfig_update_rgblight_default\n");
@ -281,7 +289,7 @@ void rgblight_disable(void) {
#ifdef RGBLIGHT_ANIMATIONS
rgblight_timer_disable();
#endif
_delay_ms(50);
wait_ms(50);
rgblight_set();
}


+ 3
- 1
quantum/rgblight_types.h View File

@ -23,7 +23,9 @@
#ifndef RGBLIGHT_TYPES
#define RGBLIGHT_TYPES
#include <avr/io.h>
#ifdef __AVR__
#include <avr/io.h>
#endif
#ifdef RGBW
#define LED_TYPE struct cRGBW


+ 2
- 0
tmk_core/chibios.mk View File

@ -145,6 +145,8 @@ HEX = $(OBJCOPY) -O $(FORMAT)
EEP =
BIN = $(OBJCOPY) -O binary
COMMON_VPATH += $(DRIVER_PATH)/arm
THUMBFLAGS = -DTHUMB_PRESENT -mno-thumb-interwork -DTHUMB_NO_INTERWORKING -mthumb -DTHUMB
COMPILEFLAGS += -fomit-frame-pointer


Loading…
Cancel
Save