2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
23 #include "common/color.h"
24 #include "light_ws2811strip.h"
34 #if !defined(WS2811_PIN)
35 #define WS2811_PIN PA0
36 #define WS2811_TIMER TIM5
37 #define WS2811_DMA_HANDLER_IDENTIFER DMA1_ST2_HANDLER
38 #define WS2811_DMA_STREAM DMA1_Stream2
39 #define WS2811_DMA_FLAG DMA_FLAG_TCIF2
40 #define WS2811_DMA_IT DMA_IT_TCIF2
41 #define WS2811_DMA_CHANNEL DMA_Channel_6
42 #define WS2811_DMA_IRQ DMA1_Stream2_IRQn
43 #define WS2811_TIMER_CHANNEL TIM_Channel_1
46 static IO_t ws2811IO
= IO_NONE
;
47 static uint16_t timDMASource
= 0;
48 bool ws2811Initialised
= false;
50 void ws2811DMAHandler(DMA_Stream_TypeDef
*stream
)
52 if (DMA_GetFlagStatus(stream
, WS2811_DMA_FLAG
)) {
53 ws2811LedDataTransferInProgress
= 0;
54 DMA_ClearITPendingBit(stream
, WS2811_DMA_IT
);
55 DMA_Cmd(stream
, DISABLE
);
56 TIM_DMACmd(WS2811_TIMER
, timDMASource
, DISABLE
);
60 void ws2811LedStripHardwareInit(void)
62 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure
;
63 TIM_OCInitTypeDef TIM_OCInitStructure
;
64 DMA_InitTypeDef DMA_InitStructure
;
66 uint16_t prescalerValue
;
68 RCC_ClockCmd(timerRCC(WS2811_TIMER
), ENABLE
);
70 ws2811IO
= IOGetByTag(IO_TAG(WS2811_PIN
));
71 /* GPIOA Configuration: TIM5 Channel 1 as alternate function push-pull */
72 IOInit(ws2811IO
, OWNER_LED_STRIP
, RESOURCE_OUTPUT
, 0);
73 IOConfigGPIOAF(ws2811IO
, IO_CONFIG(GPIO_Mode_AF
, GPIO_Speed_50MHz
, GPIO_OType_PP
, GPIO_PuPd_UP
), timerGPIOAF(WS2811_TIMER
));
76 TIM_Cmd(WS2811_TIMER
, DISABLE
);
78 /* Compute the prescaler value */
79 prescalerValue
= (uint16_t)(SystemCoreClock
/ 2 / 84000000) - 1;
81 /* Time base configuration */
82 TIM_TimeBaseStructure
.TIM_Period
= 104; // 800kHz
83 TIM_TimeBaseStructure
.TIM_Prescaler
= prescalerValue
;
84 TIM_TimeBaseStructure
.TIM_ClockDivision
= TIM_CKD_DIV1
;
85 TIM_TimeBaseStructure
.TIM_CounterMode
= TIM_CounterMode_Up
;
86 TIM_TimeBaseInit(WS2811_TIMER
, &TIM_TimeBaseStructure
);
88 /* PWM1 Mode configuration: Channel1 */
89 TIM_OCInitStructure
.TIM_OCMode
= TIM_OCMode_PWM1
;
90 TIM_OCInitStructure
.TIM_OCIdleState
= TIM_OCIdleState_Reset
;
91 TIM_OCInitStructure
.TIM_OCNIdleState
= TIM_OCNIdleState_Set
;
92 TIM_OCInitStructure
.TIM_OCPolarity
= TIM_OCNPolarity_High
;
93 TIM_OCInitStructure
.TIM_OCNPolarity
= TIM_OCNPolarity_High
;
94 TIM_OCInitStructure
.TIM_OutputState
= TIM_OutputState_Enable
;
95 TIM_OCInitStructure
.TIM_OutputNState
= TIM_OutputNState_Disable
;
96 TIM_OCInitStructure
.TIM_Pulse
= 0;
98 uint32_t channelAddress
= 0;
99 switch (WS2811_TIMER_CHANNEL
) {
101 TIM_OC1Init(WS2811_TIMER
, &TIM_OCInitStructure
);
102 timDMASource
= TIM_DMA_CC1
;
103 channelAddress
= (uint32_t)(&WS2811_TIMER
->CCR1
);
104 TIM_OC1PreloadConfig(WS2811_TIMER
, TIM_OCPreload_Enable
);
107 TIM_OC2Init(WS2811_TIMER
, &TIM_OCInitStructure
);
108 timDMASource
= TIM_DMA_CC2
;
109 channelAddress
= (uint32_t)(&WS2811_TIMER
->CCR2
);
110 TIM_OC2PreloadConfig(WS2811_TIMER
, TIM_OCPreload_Enable
);
113 TIM_OC3Init(WS2811_TIMER
, &TIM_OCInitStructure
);
114 timDMASource
= TIM_DMA_CC3
;
115 channelAddress
= (uint32_t)(&WS2811_TIMER
->CCR3
);
116 TIM_OC3PreloadConfig(WS2811_TIMER
, TIM_OCPreload_Enable
);
119 TIM_OC4Init(WS2811_TIMER
, &TIM_OCInitStructure
);
120 timDMASource
= TIM_DMA_CC4
;
121 channelAddress
= (uint32_t)(&WS2811_TIMER
->CCR4
);
122 TIM_OC4PreloadConfig(WS2811_TIMER
, TIM_OCPreload_Enable
);
126 TIM_CtrlPWMOutputs(WS2811_TIMER
, ENABLE
);
127 TIM_ARRPreloadConfig(WS2811_TIMER
, ENABLE
);
129 TIM_CCxCmd(WS2811_TIMER
, WS2811_TIMER_CHANNEL
, TIM_CCx_Enable
);
130 TIM_Cmd(WS2811_TIMER
, ENABLE
);
132 dmaSetHandler(WS2811_DMA_HANDLER_IDENTIFER
, ws2811DMAHandler
);
135 DMA_Cmd(WS2811_DMA_STREAM
, DISABLE
);
136 DMA_DeInit(WS2811_DMA_STREAM
);
137 DMA_StructInit(&DMA_InitStructure
);
138 DMA_InitStructure
.DMA_Channel
= WS2811_DMA_CHANNEL
;
139 DMA_InitStructure
.DMA_PeripheralBaseAddr
= channelAddress
;
140 DMA_InitStructure
.DMA_Memory0BaseAddr
= (uint32_t)ledStripDMABuffer
;
141 DMA_InitStructure
.DMA_DIR
= DMA_DIR_MemoryToPeripheral
;
142 DMA_InitStructure
.DMA_BufferSize
= WS2811_DMA_BUFFER_SIZE
;
143 DMA_InitStructure
.DMA_PeripheralInc
= DMA_PeripheralInc_Disable
;
144 DMA_InitStructure
.DMA_MemoryInc
= DMA_MemoryInc_Enable
;
145 DMA_InitStructure
.DMA_PeripheralDataSize
= DMA_PeripheralDataSize_Word
;
146 DMA_InitStructure
.DMA_MemoryDataSize
= DMA_MemoryDataSize_Word
;
147 DMA_InitStructure
.DMA_Mode
= DMA_Mode_Normal
;
148 DMA_InitStructure
.DMA_Priority
= DMA_Priority_VeryHigh
;
149 DMA_InitStructure
.DMA_FIFOMode
= DMA_FIFOMode_Enable
;
150 DMA_InitStructure
.DMA_FIFOThreshold
= DMA_FIFOThreshold_1QuarterFull
;
151 DMA_InitStructure
.DMA_MemoryBurst
= DMA_MemoryBurst_Single
;
152 DMA_InitStructure
.DMA_PeripheralBurst
= DMA_PeripheralBurst_Single
;
155 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1
, ENABLE
);
156 DMA_Init(WS2811_DMA_STREAM
, &DMA_InitStructure
);
158 DMA_ITConfig(WS2811_DMA_STREAM
, DMA_IT_TC
, ENABLE
);
159 DMA_ClearITPendingBit(WS2811_DMA_STREAM
, WS2811_DMA_IT
);
161 NVIC_InitTypeDef NVIC_InitStructure
;
163 NVIC_InitStructure
.NVIC_IRQChannel
= WS2811_DMA_IRQ
;
164 NVIC_InitStructure
.NVIC_IRQChannelPreemptionPriority
= NVIC_PRIORITY_BASE(NVIC_PRIO_WS2811_DMA
);
165 NVIC_InitStructure
.NVIC_IRQChannelSubPriority
= NVIC_PRIORITY_SUB(NVIC_PRIO_WS2811_DMA
);
166 NVIC_InitStructure
.NVIC_IRQChannelCmd
= ENABLE
;
167 NVIC_Init(&NVIC_InitStructure
);
169 ws2811Initialised
= true;
170 setStripColor(&hsv_white
);
174 void ws2811LedStripDMAEnable(void)
176 if (!ws2811Initialised
)
179 DMA_SetCurrDataCounter(WS2811_DMA_STREAM
, WS2811_DMA_BUFFER_SIZE
); // load number of bytes to be transferred
180 TIM_SetCounter(WS2811_TIMER
, 0);
181 DMA_Cmd(WS2811_DMA_STREAM
, ENABLE
);
182 TIM_DMACmd(WS2811_TIMER
, timDMASource
, ENABLE
);