Acc g scaling with DSP
[inav.git] / src / main / drivers / dma.h
blob10ece99925318c7a2979b12a29c30a5ba6fca5d9
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 #pragma once
20 #include "resource.h"
22 struct dmaChannelDescriptor_s;
24 typedef uint32_t dmaTag_t; // Packed DMA adapter/channel/stream
25 typedef struct dmaChannelDescriptor_s * DMA_t;
27 #if defined(UNIT_TEST)
28 typedef uint32_t DMA_TypeDef;
29 #endif
30 // DMA Tag,contains DMA id\DMA stream\DMA channel
31 #define DMA_TAG(dma, stream, channel) ( (((dma) & 0x03) << 12) | (((stream) & 0x0F) << 8) | (((channel) & 0xFF) << 0) )
32 #define DMA_NONE (0)
34 #define DMATAG_GET_DMA(x) ( ((x) >> 12) & 0x03 )
35 #define DMATAG_GET_STREAM(x) ( ((x) >> 8) & 0x0F )
36 #define DMATAG_GET_CHANNEL(x) ( ((x) >> 0) & 0xFF )
38 typedef void (*dmaCallbackHandlerFuncPtr)(DMA_t channelDescriptor);
40 #if defined(AT32F43x)
41 #define DMA_IT_TCIF ((uint32_t)0x00000002) /*!< dma full data transfer interrupt */
42 #define DMA_IT_HTIF ((uint32_t)0x00000004) /*!< dma half data transfer interrupt */
43 #define DMA_IT_DMEIF ((uint32_t)0x00000008) /*!< dma errorr interrupt */
45 //EDMA features are available for extended use
46 typedef struct dmaChannelDescriptor_s {
47 dmaTag_t tag;
48 dma_type* dma;
49 dma_channel_type* ref;
50 dmaCallbackHandlerFuncPtr irqHandlerCallback;
51 uint32_t flagsShift;
52 IRQn_Type irqNumber;
53 uint32_t userParam;
54 resourceOwner_e owner;
55 uint8_t resourceIndex;
56 dmamux_channel_type * dmaMuxref; //dmamux flag
57 } dmaChannelDescriptor_t;
59 #else
60 #define DMA_IT_TCIF ((uint32_t)0x00000020)
61 #define DMA_IT_HTIF ((uint32_t)0x00000010)
62 #define DMA_IT_TEIF ((uint32_t)0x00000008)
63 #define DMA_IT_DMEIF ((uint32_t)0x00000004)
64 #define DMA_IT_FEIF ((uint32_t)0x00000001)
67 typedef struct dmaChannelDescriptor_s {
68 dmaTag_t tag;
69 DMA_TypeDef* dma;
70 #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7)
71 DMA_Stream_TypeDef* ref;
72 #else
73 DMA_Channel_TypeDef* ref;
74 #endif
75 dmaCallbackHandlerFuncPtr irqHandlerCallback;
76 uint32_t flagsShift;
77 IRQn_Type irqNumber;
78 uint32_t userParam;
79 resourceOwner_e owner;
80 uint8_t resourceIndex;
81 } dmaChannelDescriptor_t;
83 #endif
86 #if defined(STM32F4) || defined(STM32F7) || defined(STM32H7)
88 #define DEFINE_DMA_CHANNEL(d, s, f) { \
89 .tag = DMA_TAG(d, s, 0), \
90 .dma = DMA##d, \
91 .ref = DMA##d##_Stream##s, \
92 .irqHandlerCallback = NULL, \
93 .flagsShift = f, \
94 .irqNumber = DMA##d##_Stream##s##_IRQn, \
95 .userParam = 0 \
98 #define DEFINE_DMA_IRQ_HANDLER(d, s, i) void DMA ## d ## _Stream ## s ## _IRQHandler(void) {\
99 if (dmaDescriptors[i].irqHandlerCallback)\
100 dmaDescriptors[i].irqHandlerCallback(&dmaDescriptors[i]);\
103 #define DMA_CLEAR_FLAG(d, flag) if (d->flagsShift > 31) d->dma->HIFCR = (flag << (d->flagsShift - 32)); else d->dma->LIFCR = (flag << d->flagsShift)
104 #define DMA_GET_FLAG_STATUS(d, flag) (d->flagsShift > 31 ? d->dma->HISR & (flag << (d->flagsShift - 32)): d->dma->LISR & (flag << d->flagsShift))
108 DMA_t dmaGetByRef(const DMA_Stream_TypeDef * ref);
110 #elif defined(AT32F43x)
112 // DEFINE_DMA_CHANNEL(d, s, f) use DMA_TAG(d, s, 0)
113 #define DEFINE_DMA_CHANNEL(d, s, f) { \
114 .tag = DMA_TAG(d, s, 0), \
115 .dma = DMA##d, \
116 .ref = DMA##d##_CHANNEL##s, \
117 .irqHandlerCallback = NULL, \
118 .flagsShift = f, \
119 .irqNumber = DMA##d##_Channel##s##_IRQn, \
120 .userParam = 0, \
121 .dmaMuxref = (dmamux_channel_type *)DMA##d ## MUX_CHANNEL ##s \
125 //DMA_IRQ Naming format DMA#_Channel#_IRQHandler
126 #define DEFINE_DMA_IRQ_HANDLER(d, s, i) void DMA ## d ## _Channel ## s ## _IRQHandler(void) {\
127 if (dmaDescriptors[i].irqHandlerCallback)\
128 dmaDescriptors[i].irqHandlerCallback(&dmaDescriptors[i]);\
131 //Same as dma_flag_clear/dma_flag_get
132 #define DMA_CLEAR_FLAG(d, flag) d->dma->clr = (flag << d->flagsShift)
133 #define DMA_GET_FLAG_STATUS(d, flag) (d->dma->sts & (flag << d->flagsShift))
135 DMA_t dmaGetByRef(const dma_channel_type * ref);
137 void dmaMuxEnable(DMA_t dma, uint32_t dmaMuxid);
139 #endif
141 DMA_t dmaGetByTag(dmaTag_t tag);
142 uint32_t dmaGetChannelByTag(dmaTag_t tag);
143 resourceOwner_e dmaGetOwner(DMA_t dma);
144 void dmaInit(DMA_t dma, resourceOwner_e owner, uint8_t resourceIndex);
145 void dmaEnableClock(DMA_t dma);
146 void dmaSetHandler(DMA_t dma, dmaCallbackHandlerFuncPtr callback, uint32_t priority, uint32_t userParam);
147 void dmaCleanInterrupts(DMA_t dma);