Code re-organisation: src/platform/xxx for the MCU type (#13955)
[betaflight.git] / src / platform / APM32 / light_ws2811strip_apm32.c
bloba96d3a9bc4654c3ae40f996745981721edde534b
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
26 #ifdef USE_LED_STRIP
28 #include "common/color.h"
30 #include "drivers/dma.h"
31 #include "drivers/dma_reqmap.h"
32 #include "drivers/io.h"
33 #include "drivers/nvic.h"
34 #include "drivers/rcc.h"
35 #include "drivers/system.h"
36 #include "drivers/timer.h"
38 #include "drivers/light_ws2811strip.h"
40 static IO_t ws2811IO = IO_NONE;
42 static TMR_HandleTypeDef TmrHandle;
43 static uint16_t timerChannel = 0;
45 FAST_IRQ_HANDLER void WS2811_DMA_IRQHandler(dmaChannelDescriptor_t* descriptor)
47 DAL_DMA_IRQHandler(TmrHandle.hdma[descriptor->userParam]);
48 TIM_DMACmd(&TmrHandle, timerChannel, DISABLE);
49 ws2811LedDataTransferInProgress = false;
52 bool ws2811LedStripHardwareInit(ioTag_t ioTag)
54 if (!ioTag) {
55 return false;
58 const timerHardware_t *timerHardware = timerAllocate(ioTag, OWNER_LED_STRIP, 0);
60 if (timerHardware == NULL) {
61 return false;
64 TMR_TypeDef *timer = timerHardware->tim;
65 timerChannel = timerHardware->channel;
67 dmaResource_t *dmaRef;
69 #if defined(USE_DMA_SPEC)
70 const dmaChannelSpec_t *dmaSpec = dmaGetChannelSpecByTimer(timerHardware);
72 if (dmaSpec == NULL) {
73 return false;
76 dmaRef = dmaSpec->ref;
77 uint32_t dmaChannel = dmaSpec->channel;
78 #else
79 dmaRef = timerHardware->dmaRef;
80 uint32_t dmaChannel = timerHardware->dmaChannel;
81 #endif
83 if (dmaRef == NULL || !dmaAllocate(dmaGetIdentifier(dmaRef), OWNER_LED_STRIP, 0)) {
84 return false;
86 TmrHandle.Instance = timer;
88 /* Compute the prescaler value */
89 uint16_t prescaler = timerGetPrescalerByDesiredMhz(timer, WS2811_TIMER_MHZ);
90 uint16_t period = timerGetPeriodByPrescaler(timer, prescaler, WS2811_CARRIER_HZ);
92 BIT_COMPARE_1 = period / 3 * 2;
93 BIT_COMPARE_0 = period / 3;
95 TmrHandle.Init.Prescaler = prescaler;
96 TmrHandle.Init.Period = period; // 800kHz
97 TmrHandle.Init.ClockDivision = TMR_CLOCKDIVISION_DIV1;
98 TmrHandle.Init.CounterMode = TMR_COUNTERMODE_UP;
99 TmrHandle.Init.AutoReloadPreload = TMR_AUTORELOAD_PRELOAD_ENABLE;
100 if (DAL_TMR_PWM_Init(&TmrHandle) != DAL_OK) {
101 /* Initialization Error */
102 return false;
105 static DMA_HandleTypeDef hdma_tim;
107 ws2811IO = IOGetByTag(ioTag);
108 IOInit(ws2811IO, OWNER_LED_STRIP, 0);
109 IOConfigGPIOAF(ws2811IO, IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_PULLDOWN), timerHardware->alternateFunction);
111 __DAL_RCM_DMA1_CLK_ENABLE();
112 __DAL_RCM_DMA2_CLK_ENABLE();
114 /* Set the parameters to be configured */
115 hdma_tim.Init.Channel = dmaChannel;
116 hdma_tim.Init.Direction = DMA_MEMORY_TO_PERIPH;
117 hdma_tim.Init.PeriphInc = DMA_PINC_DISABLE;
118 hdma_tim.Init.MemInc = DMA_MINC_ENABLE;
119 hdma_tim.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
120 hdma_tim.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
121 hdma_tim.Init.Mode = DMA_NORMAL;
122 hdma_tim.Init.Priority = DMA_PRIORITY_HIGH;
123 hdma_tim.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
124 hdma_tim.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
125 hdma_tim.Init.MemBurst = DMA_MBURST_SINGLE;
126 hdma_tim.Init.PeriphBurst = DMA_PBURST_SINGLE;
128 /* Set hdma_tim instance */
129 hdma_tim.Instance = (DMA_ARCH_TYPE *)dmaRef;
131 uint16_t dmaIndex = timerDmaIndex(timerChannel);
133 /* Link hdma_tim to hdma[x] (channelx) */
134 __DAL_LINKDMA(&TmrHandle, hdma[dmaIndex], hdma_tim);
136 dmaEnable(dmaGetIdentifier(dmaRef));
137 dmaSetHandler(dmaGetIdentifier(dmaRef), WS2811_DMA_IRQHandler, NVIC_PRIO_WS2811_DMA, dmaIndex);
139 /* Initialize TIMx DMA handle */
140 if (DAL_DMA_Init(TmrHandle.hdma[dmaIndex]) != DAL_OK) {
141 /* Initialization Error */
142 return false;
145 TMR_OC_InitTypeDef TMR_OCInitStructure;
147 /* PWM1 Mode configuration: Channel1 */
148 TMR_OCInitStructure.OCMode = TMR_OCMODE_PWM1;
149 TMR_OCInitStructure.OCIdleState = TMR_OCIDLESTATE_SET;
150 TMR_OCInitStructure.OCPolarity = (timerHardware->output & TIMER_OUTPUT_INVERTED) ? TMR_OCPOLARITY_LOW : TMR_OCPOLARITY_HIGH;
151 TMR_OCInitStructure.OCNIdleState = TMR_OCNIDLESTATE_RESET;
152 TMR_OCInitStructure.OCNPolarity = (timerHardware->output & TIMER_OUTPUT_INVERTED) ? TMR_OCNPOLARITY_LOW : TMR_OCNPOLARITY_HIGH;
153 TMR_OCInitStructure.Pulse = 0;
154 TMR_OCInitStructure.OCFastMode = TMR_OCFAST_DISABLE;
155 if (DAL_TMR_PWM_ConfigChannel(&TmrHandle, &TMR_OCInitStructure, timerChannel) != DAL_OK) {
156 /* Configuration Error */
157 return false;
159 if (timerHardware->output & TIMER_OUTPUT_N_CHANNEL) {
160 if (DAL_TMREx_PWMN_Start(&TmrHandle, timerChannel) != DAL_OK) {
161 /* Starting PWM generation Error */
162 return false;
164 } else {
165 if (DAL_TMR_PWM_Start(&TmrHandle, timerChannel) != DAL_OK) {
166 /* Starting PWM generation Error */
167 return false;
171 return true;
174 void ws2811LedStripDMAEnable(void)
176 if (DMA_SetCurrDataCounter(&TmrHandle, timerChannel, ledStripDMABuffer, WS2811_DMA_BUFFER_SIZE) != DAL_OK) {
177 /* DMA set error */
178 ws2811LedDataTransferInProgress = false;
179 return;
181 /* Reset timer counter */
182 __DAL_TMR_SET_COUNTER(&TmrHandle,0);
183 /* Enable channel DMA requests */
184 TIM_DMACmd(&TmrHandle,timerChannel,ENABLE);
187 #endif // USE_LED_STRIP