1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-25 17:25:20 +03:00

Extract STM32F10x specific WS2811 code into separate file, ready for

STM32F30x port.
This commit is contained in:
Dominic Clifton 2014-06-08 13:24:12 +01:00
parent 0861310537
commit 578585db74
4 changed files with 140 additions and 93 deletions

View file

@ -182,6 +182,8 @@ NAZE_SRC = startup_stm32f10x_md_gcc.S \
drivers/compass_hmc5883l.c \
drivers/gpio_stm32f10x.c \
drivers/light_led_stm32f10x.c \
drivers/light_ws2811strip.c \
drivers/light_ws2811strip_stm32f10x.c \
drivers/sonar_hcsr04.c \
drivers/pwm_mapping.c \
drivers/pwm_output.c \
@ -205,6 +207,7 @@ OLIMEXINO_SRC = startup_stm32f10x_md_gcc.S \
drivers/gpio_stm32f10x.c \
drivers/light_led_stm32f10x.c \
drivers/light_ws2811strip.c \
drivers/light_ws2811strip_stm32f10x.c \
drivers/pwm_mapping.c \
drivers/pwm_output.c \
drivers/pwm_rssi.c \
@ -249,7 +252,8 @@ NAZE32PRO_SRC = $(STM32F30x_COMMON_SRC) \
STM32F3DISCOVERY_COMMON_SRC = $(STM32F30x_COMMON_SRC) \
drivers/accgyro_l3gd20.c \
drivers/accgyro_l3gd20.c \
drivers/accgyro_lsm303dlhc.c
drivers/accgyro_lsm303dlhc.c \
drivers/light_ws2811strip.c
STM32F3DISCOVERY_SRC = $(STM32F3DISCOVERY_COMMON_SRC) \
drivers/accgyro_adxl345.c \

View file

@ -33,16 +33,10 @@
#include "drivers/light_ws2811strip.h"
#define LED_STRIP_LENGTH 10
#define BITS_PER_LED 24
#define DELAY_BUFFER_LENGTH 42 // for 50us delay
uint8_t ledStripDMABuffer[WS2811_DMA_BUFFER_SIZE];
volatile uint8_t ws2811LedDataTransferInProgress = 0;
#define DMA_BUFFER_SIZE (BITS_PER_LED * LED_STRIP_LENGTH + DELAY_BUFFER_LENGTH) // number of bytes needed is #LEDs * 24 bytes + 42 trailing bytes)
static uint8_t ledStripDMABuffer[DMA_BUFFER_SIZE];
static volatile uint8_t ws2811LedDataTransferInProgress = 0;
static rgbColor24bpp_t ledColorBuffer[LED_STRIP_LENGTH];
static rgbColor24bpp_t ledColorBuffer[WS2811_LED_STRIP_LENGTH];
const rgbColor24bpp_t black = { {0, 0, 0} };
const rgbColor24bpp_t orange = { {255, 128, 0} };
@ -56,7 +50,7 @@ void setLedColor(uint16_t index, const rgbColor24bpp_t *color)
void setStripColor(const rgbColor24bpp_t *color)
{
uint16_t index;
for (index = 0; index < LED_STRIP_LENGTH; index++) {
for (index = 0; index < WS2811_LED_STRIP_LENGTH; index++) {
setLedColor(index, color);
}
}
@ -64,91 +58,19 @@ void setStripColor(const rgbColor24bpp_t *color)
void setStripColors(const rgbColor24bpp_t *colors)
{
uint16_t index;
for (index = 0; index < LED_STRIP_LENGTH; index++) {
for (index = 0; index < WS2811_LED_STRIP_LENGTH; index++) {
setLedColor(index, colors++);
}
}
void ws2811LedStripInit(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
uint16_t prescalerValue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* GPIOA Configuration: TIM3 Channel 1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Compute the prescaler value */
prescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 29; // 800kHz
TIM_TimeBaseStructure.TIM_Prescaler = prescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
/* configure DMA */
/* DMA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA1 Channel6 Config */
DMA_DeInit(DMA1_Channel6);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM3->CCR1;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ledStripDMABuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = DMA_BUFFER_SIZE;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
/* TIM3 CC1 DMA Request enable */
TIM_DMACmd(TIM3, TIM_DMA_CC1, ENABLE);
DMA_ITConfig(DMA1_Channel6, DMA_IT_TC, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
ws2811LedStripHardwareInit();
setStripColor(&white);
ws2811UpdateStrip();
}
void DMA1_Channel6_IRQHandler(void)
{
if (DMA_GetFlagStatus(DMA1_FLAG_TC6)) {
ws2811LedDataTransferInProgress = 0;
DMA_Cmd(DMA1_Channel6, DISABLE); // disable DMA channel 6
DMA_ClearFlag(DMA1_FLAG_TC6); // clear DMA1 Channel 6 transfer complete flag
}
}
bool isWS2811LedStripReady(void)
{
return !ws2811LedDataTransferInProgress;
@ -192,7 +114,7 @@ void ws2811UpdateStrip(void)
// fill transmit buffer with correct compare values to achieve
// correct pulse widths according to color values
while (ledIndex < LED_STRIP_LENGTH)
while (ledIndex < WS2811_LED_STRIP_LENGTH)
{
updateLEDDMABuffer(ledColorBuffer[ledIndex].rgb.g);
updateLEDDMABuffer(ledColorBuffer[ledIndex].rgb.r);
@ -202,18 +124,13 @@ void ws2811UpdateStrip(void)
}
// add needed delay at end of byte cycle, pulsewidth = 0
while(dmaBufferOffset < DMA_BUFFER_SIZE)
while(dmaBufferOffset < WS2811_DMA_BUFFER_SIZE)
{
ledStripDMABuffer[dmaBufferOffset] = 0;
dmaBufferOffset++;
}
ws2811LedDataTransferInProgress = 1;
DMA_SetCurrDataCounter(DMA1_Channel6, DMA_BUFFER_SIZE); // load number of bytes to be transferred
TIM_SetCounter(TIM3, 0);
TIM_Cmd(TIM3, ENABLE);
DMA_Cmd(DMA1_Channel6, ENABLE);
ws2811LedStripDMAEnable();
}

View file

@ -17,6 +17,11 @@
#pragma once
#define WS2811_LED_STRIP_LENGTH 10
#define WS2811_BITS_PER_LED 24
#define WS2811_DELAY_BUFFER_LENGTH 42 // for 50us delay
#define WS2811_DMA_BUFFER_SIZE (WS2811_BITS_PER_LED * WS2811_LED_STRIP_LENGTH + WS2811_DELAY_BUFFER_LENGTH) // number of bytes needed is #LEDs * 24 bytes + 42 trailing bytes)
typedef enum {
CC_RED = 0,
@ -39,6 +44,9 @@ typedef union {
void ws2811LedStripInit(void);
void ws2811LedStripHardwareInit(void);
void ws2811LedStripDMAEnable(void);
void ws2811UpdateStrip(void);
void setLedColor(uint16_t index, const rgbColor24bpp_t *color);
void setStripColor(const rgbColor24bpp_t *color);
@ -46,6 +54,9 @@ void setStripColors(const rgbColor24bpp_t *colors);
bool isWS2811LedStripReady(void);
extern uint8_t ledStripDMABuffer[WS2811_DMA_BUFFER_SIZE];
extern volatile uint8_t ws2811LedDataTransferInProgress;
extern const rgbColor24bpp_t black;
extern const rgbColor24bpp_t white;
extern const rgbColor24bpp_t orange;

View file

@ -0,0 +1,115 @@
/*
* This file is part of Cleanflight.
*
* Cleanflight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cleanflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdint.h>
#include "platform.h"
#include "drivers/light_ws2811strip.h"
void ws2811LedStripHardwareInit(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
uint16_t prescalerValue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* GPIOA Configuration: TIM3 Channel 1 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Compute the prescaler value */
prescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 29; // 800kHz
TIM_TimeBaseStructure.TIM_Prescaler = prescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
/* configure DMA */
/* DMA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* DMA1 Channel6 Config */
DMA_DeInit(DMA1_Channel6);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM3->CCR1;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ledStripDMABuffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = WS2811_DMA_BUFFER_SIZE;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
/* TIM3 CC1 DMA Request enable */
TIM_DMACmd(TIM3, TIM_DMA_CC1, ENABLE);
DMA_ITConfig(DMA1_Channel6, DMA_IT_TC, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
setStripColor(&white);
ws2811UpdateStrip();
}
void DMA1_Channel6_IRQHandler(void)
{
if (DMA_GetFlagStatus(DMA1_FLAG_TC6)) {
ws2811LedDataTransferInProgress = 0;
DMA_Cmd(DMA1_Channel6, DISABLE); // disable DMA channel 6
DMA_ClearFlag(DMA1_FLAG_TC6); // clear DMA1 Channel 6 transfer complete flag
}
}
void ws2811LedStripDMAEnable(void)
{
DMA_SetCurrDataCounter(DMA1_Channel6, WS2811_DMA_BUFFER_SIZE); // load number of bytes to be transferred
TIM_SetCounter(TIM3, 0);
TIM_Cmd(TIM3, ENABLE);
DMA_Cmd(DMA1_Channel6, ENABLE);
}