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/>.
25 #include "common/color.h"
26 #include "light_ws2811strip.h"
33 #include "timer_stm32f4xx.h"
35 #if !defined(WS2811_PIN)
36 #define WS2811_PIN PA0
37 #define WS2811_TIMER TIM5
38 #define WS2811_DMA_HANDLER_IDENTIFER DMA1_ST2_HANDLER
39 #define WS2811_DMA_STREAM DMA1_Stream2
40 #define WS2811_DMA_CHANNEL DMA_Channel_6
41 #define WS2811_TIMER_CHANNEL TIM_Channel_1
42 #define WS2811_TIMER_GPIO_AF GPIO_AF_TIM5
45 static IO_t ws2811IO
= IO_NONE
;
46 static uint16_t timDMASource
= 0;
47 bool ws2811Initialised
= false;
49 static void WS2811_DMA_IRQHandler(dmaChannelDescriptor_t
*descriptor
)
51 if (DMA_GET_FLAG_STATUS(descriptor
, DMA_IT_TCIF
)) {
52 ws2811LedDataTransferInProgress
= 0;
53 DMA_Cmd(descriptor
->stream
, DISABLE
);
54 TIM_DMACmd(WS2811_TIMER
, timDMASource
, DISABLE
);
55 DMA_CLEAR_FLAG(descriptor
, DMA_IT_TCIF
);
59 void ws2811LedStripHardwareInit(void)
61 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure
;
62 TIM_OCInitTypeDef TIM_OCInitStructure
;
63 DMA_InitTypeDef DMA_InitStructure
;
65 uint16_t prescalerValue
;
67 RCC_ClockCmd(timerRCC(WS2811_TIMER
), ENABLE
);
69 ws2811IO
= IOGetByTag(IO_TAG(WS2811_PIN
));
70 /* GPIOA Configuration: TIM5 Channel 1 as alternate function push-pull */
71 IOInit(ws2811IO
, OWNER_LED_STRIP
, RESOURCE_OUTPUT
, 0);
72 IOConfigGPIOAF(ws2811IO
, IO_CONFIG(GPIO_Mode_AF
, GPIO_Speed_50MHz
, GPIO_OType_PP
, GPIO_PuPd_UP
), WS2811_TIMER_GPIO_AF
);
75 TIM_Cmd(WS2811_TIMER
, DISABLE
);
77 /* Compute the prescaler value */
78 prescalerValue
= (uint16_t)(SystemCoreClock
/ 2 / 84000000) - 1;
80 /* Time base configuration */
81 TIM_TimeBaseStructure
.TIM_Period
= 104; // 800kHz
82 TIM_TimeBaseStructure
.TIM_Prescaler
= prescalerValue
;
83 TIM_TimeBaseStructure
.TIM_ClockDivision
= TIM_CKD_DIV1
;
84 TIM_TimeBaseStructure
.TIM_CounterMode
= TIM_CounterMode_Up
;
85 TIM_TimeBaseInit(WS2811_TIMER
, &TIM_TimeBaseStructure
);
87 /* PWM1 Mode configuration: Channel1 */
88 TIM_OCInitStructure
.TIM_OCMode
= TIM_OCMode_PWM1
;
89 TIM_OCInitStructure
.TIM_OCIdleState
= TIM_OCIdleState_Reset
;
90 TIM_OCInitStructure
.TIM_OCNIdleState
= TIM_OCNIdleState_Set
;
91 TIM_OCInitStructure
.TIM_OCPolarity
= TIM_OCNPolarity_High
;
92 TIM_OCInitStructure
.TIM_OCNPolarity
= TIM_OCNPolarity_High
;
93 TIM_OCInitStructure
.TIM_OutputState
= TIM_OutputState_Enable
;
94 TIM_OCInitStructure
.TIM_OutputNState
= TIM_OutputNState_Disable
;
95 TIM_OCInitStructure
.TIM_Pulse
= 0;
97 timerOCInit(WS2811_TIMER
, WS2811_TIMER_CHANNEL
, &TIM_OCInitStructure
);
98 timerOCPreloadConfig(WS2811_TIMER
, WS2811_TIMER_CHANNEL
, TIM_OCPreload_Enable
);
99 timDMASource
= timerDmaSource(WS2811_TIMER_CHANNEL
);
101 TIM_CtrlPWMOutputs(WS2811_TIMER
, ENABLE
);
102 TIM_ARRPreloadConfig(WS2811_TIMER
, ENABLE
);
104 TIM_CCxCmd(WS2811_TIMER
, WS2811_TIMER_CHANNEL
, TIM_CCx_Enable
);
105 TIM_Cmd(WS2811_TIMER
, ENABLE
);
108 DMA_Cmd(WS2811_DMA_STREAM
, DISABLE
);
109 DMA_DeInit(WS2811_DMA_STREAM
);
110 DMA_StructInit(&DMA_InitStructure
);
111 DMA_InitStructure
.DMA_Channel
= WS2811_DMA_CHANNEL
;
112 DMA_InitStructure
.DMA_PeripheralBaseAddr
= (uint32_t)timerCCR(WS2811_TIMER
, WS2811_TIMER_CHANNEL
);
113 DMA_InitStructure
.DMA_Memory0BaseAddr
= (uint32_t)ledStripDMABuffer
;
114 DMA_InitStructure
.DMA_DIR
= DMA_DIR_MemoryToPeripheral
;
115 DMA_InitStructure
.DMA_BufferSize
= WS2811_DMA_BUFFER_SIZE
;
116 DMA_InitStructure
.DMA_PeripheralInc
= DMA_PeripheralInc_Disable
;
117 DMA_InitStructure
.DMA_MemoryInc
= DMA_MemoryInc_Enable
;
118 DMA_InitStructure
.DMA_PeripheralDataSize
= DMA_PeripheralDataSize_Word
;
119 DMA_InitStructure
.DMA_MemoryDataSize
= DMA_MemoryDataSize_Word
;
120 DMA_InitStructure
.DMA_Mode
= DMA_Mode_Normal
;
121 DMA_InitStructure
.DMA_Priority
= DMA_Priority_VeryHigh
;
122 DMA_InitStructure
.DMA_FIFOMode
= DMA_FIFOMode_Enable
;
123 DMA_InitStructure
.DMA_FIFOThreshold
= DMA_FIFOThreshold_1QuarterFull
;
124 DMA_InitStructure
.DMA_MemoryBurst
= DMA_MemoryBurst_Single
;
125 DMA_InitStructure
.DMA_PeripheralBurst
= DMA_PeripheralBurst_Single
;
127 DMA_Init(WS2811_DMA_STREAM
, &DMA_InitStructure
);
129 DMA_ITConfig(WS2811_DMA_STREAM
, DMA_IT_TC
, ENABLE
);
130 DMA_ClearITPendingBit(WS2811_DMA_STREAM
, dmaFlag_IT_TCIF(WS2811_DMA_STREAM
));
132 dmaSetHandler(WS2811_DMA_HANDLER_IDENTIFER
, WS2811_DMA_IRQHandler
, NVIC_PRIO_WS2811_DMA
, 0);
134 const hsvColor_t hsv_white
= { 0, 255, 255};
135 ws2811Initialised
= true;
136 setStripColor(&hsv_white
);
140 void ws2811LedStripDMAEnable(void)
142 if (!ws2811Initialised
)
145 DMA_SetCurrDataCounter(WS2811_DMA_STREAM
, WS2811_DMA_BUFFER_SIZE
); // load number of bytes to be transferred
146 TIM_SetCounter(WS2811_TIMER
, 0);
147 DMA_Cmd(WS2811_DMA_STREAM
, ENABLE
);
148 TIM_DMACmd(WS2811_TIMER
, timDMASource
, ENABLE
);