Fix function brace style
[betaflight.git] / src / main / drivers / dma.c
blob73f2626816c42cf7233c5cf0d1a7c687628d7ad0
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 <string.h>
23 #include <stdint.h>
25 #include "platform.h"
27 #ifdef USE_DMA
29 #include "drivers/nvic.h"
30 #include "dma.h"
33 * DMA descriptors.
35 dmaChannelDescriptor_t dmaDescriptors[DMA_LAST_HANDLER] = {
36 DEFINE_DMA_CHANNEL(DMA1, 1, 0),
37 DEFINE_DMA_CHANNEL(DMA1, 2, 4),
38 DEFINE_DMA_CHANNEL(DMA1, 3, 8),
39 DEFINE_DMA_CHANNEL(DMA1, 4, 12),
40 DEFINE_DMA_CHANNEL(DMA1, 5, 16),
41 DEFINE_DMA_CHANNEL(DMA1, 6, 20),
42 DEFINE_DMA_CHANNEL(DMA1, 7, 24),
46 * DMA IRQ Handlers
49 DEFINE_DMA_IRQ_HANDLER(1, 1, DMA1_CH1_HANDLER)
50 DEFINE_DMA_IRQ_HANDLER(1, 2, DMA1_CH2_HANDLER)
51 DEFINE_DMA_IRQ_HANDLER(1, 3, DMA1_CH3_HANDLER)
52 DEFINE_DMA_IRQ_HANDLER(1, 4, DMA1_CH4_HANDLER)
53 DEFINE_DMA_IRQ_HANDLER(1, 5, DMA1_CH5_HANDLER)
54 DEFINE_DMA_IRQ_HANDLER(1, 6, DMA1_CH6_HANDLER)
55 DEFINE_DMA_IRQ_HANDLER(1, 7, DMA1_CH7_HANDLER)
57 #define RETURN_TCIF_FLAG(s, d, n) if (s == DMA ## d ## _Channel ## n) return DMA ## d ## _FLAG_TC ## n
59 uint32_t dmaFlag_IT_TCIF(const dmaResource_t *channel)
61 RETURN_TCIF_FLAG((DMA_ARCH_TYPE *)channel, 1, 1);
62 RETURN_TCIF_FLAG((DMA_ARCH_TYPE *)channel, 1, 2);
63 RETURN_TCIF_FLAG((DMA_ARCH_TYPE *)channel, 1, 3);
64 RETURN_TCIF_FLAG((DMA_ARCH_TYPE *)channel, 1, 4);
65 RETURN_TCIF_FLAG((DMA_ARCH_TYPE *)channel, 1, 5);
66 RETURN_TCIF_FLAG((DMA_ARCH_TYPE *)channel, 1, 6);
67 RETURN_TCIF_FLAG((DMA_ARCH_TYPE *)channel, 1, 7);
68 RETURN_TCIF_FLAG((DMA_ARCH_TYPE *)channel, 2, 1);
69 RETURN_TCIF_FLAG((DMA_ARCH_TYPE *)channel, 2, 2);
70 RETURN_TCIF_FLAG((DMA_ARCH_TYPE *)channel, 2, 3);
71 RETURN_TCIF_FLAG((DMA_ARCH_TYPE *)channel, 2, 4);
72 RETURN_TCIF_FLAG((DMA_ARCH_TYPE *)channel, 2, 5);
73 return 0;
76 #define DMA_RCC(x) ((x) == DMA1 ? RCC_AHBPeriph_DMA1 : RCC_AHBPeriph_DMA2)
77 void dmaEnable(dmaIdentifier_e identifier)
79 const int index = DMA_IDENTIFIER_TO_INDEX(identifier);
81 RCC_AHBPeriphClockCmd(DMA_RCC(dmaDescriptors[index].dma), ENABLE);
84 void dmaSetHandler(dmaIdentifier_e identifier, dmaCallbackHandlerFuncPtr callback, uint32_t priority, uint32_t userParam)
86 NVIC_InitTypeDef NVIC_InitStructure;
88 const int index = DMA_IDENTIFIER_TO_INDEX(identifier);
89 /* TODO: remove this - enforce the init */
90 RCC_AHBPeriphClockCmd(DMA_RCC(dmaDescriptors[index].dma), ENABLE);
91 dmaDescriptors[index].irqHandlerCallback = callback;
92 dmaDescriptors[index].userParam = userParam;
93 dmaDescriptors[index].completeFlag = dmaFlag_IT_TCIF(dmaDescriptors[index].ref);
95 NVIC_InitStructure.NVIC_IRQChannel = dmaDescriptors[index].irqN;
96 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_PRIORITY_BASE(priority);
97 NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_PRIORITY_SUB(priority);
98 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
99 NVIC_Init(&NVIC_InitStructure);
101 #endif