Finally rename flight.c/.h to pid.c/.h. Cleanup some dependencies.
[betaflight.git] / src / main / drivers / light_ws2811strip_stm32f10x.c
blob2b0de69bdb88a74e86cf6094ba6a1df8bd01c077
1 /*
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/>.
18 #include <stdbool.h>
19 #include <stdint.h>
21 #include "platform.h"
23 #include "common/color.h"
24 #include "drivers/light_ws2811strip.h"
25 #include "nvic.h"
27 void ws2811LedStripHardwareInit(void)
29 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
30 TIM_OCInitTypeDef TIM_OCInitStructure;
31 GPIO_InitTypeDef GPIO_InitStructure;
32 DMA_InitTypeDef DMA_InitStructure;
34 uint16_t prescalerValue;
36 #ifdef CC3D
37 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
38 GPIO_StructInit(&GPIO_InitStructure);
39 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
40 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
41 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
42 GPIO_Init(GPIOB, &GPIO_InitStructure);
43 #else
44 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
46 /* GPIOA Configuration: TIM3 Channel 1 as alternate function push-pull */
47 GPIO_StructInit(&GPIO_InitStructure);
48 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
49 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
50 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
51 GPIO_Init(GPIOA, &GPIO_InitStructure);
52 #endif
54 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
55 /* Compute the prescaler value */
56 prescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
57 /* Time base configuration */
58 TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
59 TIM_TimeBaseStructure.TIM_Period = 29; // 800kHz
60 TIM_TimeBaseStructure.TIM_Prescaler = prescalerValue;
61 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
62 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
63 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
65 /* PWM1 Mode configuration: Channel1 */
66 TIM_OCStructInit(&TIM_OCInitStructure);
67 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
68 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
69 TIM_OCInitStructure.TIM_Pulse = 0;
70 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
71 TIM_OC1Init(TIM3, &TIM_OCInitStructure);
72 TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
74 TIM_CtrlPWMOutputs(TIM3, ENABLE);
76 /* configure DMA */
77 /* DMA clock enable */
78 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
80 /* DMA1 Channel6 Config */
81 DMA_DeInit(DMA1_Channel6);
83 DMA_StructInit(&DMA_InitStructure);
84 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM3->CCR1;
85 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ledStripDMABuffer;
86 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
87 DMA_InitStructure.DMA_BufferSize = WS2811_DMA_BUFFER_SIZE;
88 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
89 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
90 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
91 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
92 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
93 DMA_InitStructure.DMA_Priority = DMA_Priority_High;
94 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
96 DMA_Init(DMA1_Channel6, &DMA_InitStructure);
98 /* TIM3 CC1 DMA Request enable */
99 TIM_DMACmd(TIM3, TIM_DMA_CC1, ENABLE);
101 DMA_ITConfig(DMA1_Channel6, DMA_IT_TC, ENABLE);
103 NVIC_InitTypeDef NVIC_InitStructure;
105 NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel6_IRQn;
106 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_PRIORITY_BASE(NVIC_PRIO_WS2811_DMA);
107 NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_PRIORITY_SUB(NVIC_PRIO_WS2811_DMA);
108 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
109 NVIC_Init(&NVIC_InitStructure);
111 setStripColor(&hsv_white);
112 ws2811UpdateStrip();
115 void DMA1_Channel6_IRQHandler(void)
117 if (DMA_GetFlagStatus(DMA1_FLAG_TC6)) {
118 ws2811LedDataTransferInProgress = 0;
119 DMA_Cmd(DMA1_Channel6, DISABLE); // disable DMA channel 6
120 DMA_ClearFlag(DMA1_FLAG_TC6); // clear DMA1 Channel 6 transfer complete flag
124 void ws2811LedStripDMAEnable(void)
126 DMA_SetCurrDataCounter(DMA1_Channel6, WS2811_DMA_BUFFER_SIZE); // load number of bytes to be transferred
127 TIM_SetCounter(TIM3, 0);
128 TIM_Cmd(TIM3, ENABLE);
129 DMA_Cmd(DMA1_Channel6, ENABLE);