-
Notifications
You must be signed in to change notification settings - Fork 32
/
tm_stm32_dma.c
364 lines (322 loc) · 10.9 KB
/
tm_stm32_dma.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* |----------------------------------------------------------------------
* | Copyright (c) 2020 Tilen MAJERLE
* |
* | Permission is hereby granted, free of charge, to any person
* | obtaining a copy of this software and associated documentation
* | files (the "Software"), to deal in the Software without restriction,
* | including without limitation the rights to use, copy, modify, merge,
* | publish, distribute, sublicense, and/or sell copies of the Software,
* | and to permit persons to whom the Software is furnished to do so,
* | subject to the following conditions:
* |
* | The above copyright notice and this permission notice shall be
* | included in all copies or substantial portions of the Software.
* |
* | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* | AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* | OTHER DEALINGS IN THE SOFTWARE.
* |----------------------------------------------------------------------
*/
#include "tm_stm32_dma.h"
/* Private defines for stream numbers */
#define GET_STREAM_NUMBER_DMA1(stream) (((uint32_t)(stream) - (uint32_t)DMA1_Stream0) / (0x18))
#define GET_STREAM_NUMBER_DMA2(stream) (((uint32_t)(stream) - (uint32_t)DMA2_Stream0) / (0x18))
/* Offsets for bits */
static const uint8_t DMA_Flags_Bit_Pos[4] = {
0, 6, 16, 22
};
static const IRQn_Type DMA_IRQs[2][8] = {
{
DMA1_Stream0_IRQn, DMA1_Stream1_IRQn, DMA1_Stream2_IRQn, DMA1_Stream3_IRQn,
DMA1_Stream4_IRQn, DMA1_Stream5_IRQn, DMA1_Stream6_IRQn, DMA1_Stream7_IRQn
},
{
DMA2_Stream0_IRQn, DMA2_Stream1_IRQn, DMA2_Stream2_IRQn, DMA2_Stream3_IRQn,
DMA2_Stream4_IRQn, DMA2_Stream5_IRQn, DMA2_Stream6_IRQn, DMA2_Stream7_IRQn
}
};
void TM_DMA_ClearFlags(DMA_Stream_TypeDef* DMA_Stream) {
/* Clear all flags */
TM_DMA_ClearFlag(DMA_Stream, DMA_FLAG_ALL);
}
void TM_DMA_ClearFlag(DMA_Stream_TypeDef* DMA_Stream, uint32_t flag) {
uint32_t location;
uint32_t stream_number;
/* Check stream value */
if (DMA_Stream < DMA2_Stream0) {
location = (uint32_t)&DMA1->LIFCR;
stream_number = GET_STREAM_NUMBER_DMA1(DMA_Stream);
} else {
location = (uint32_t)&DMA2->LIFCR;
stream_number = GET_STREAM_NUMBER_DMA2(DMA_Stream);
}
/* Get register offset */
if (stream_number >= 4) {
/* High registers for DMA clear */
location += 4;
/* Do offset for high DMA registers */
stream_number -= 4;
}
/* Clear flags */
*(__IO uint32_t *)location = (flag & DMA_FLAG_ALL) << DMA_Flags_Bit_Pos[stream_number];
}
uint32_t TM_DMA_GetFlags(DMA_Stream_TypeDef* DMA_Stream, uint32_t flag) {
uint32_t stream_number = 0;
uint32_t location = 0;
uint32_t flags = 0;
/* Check stream value */
if (DMA_Stream < DMA2_Stream0) {
location = (uint32_t)&DMA1->LISR;
stream_number = GET_STREAM_NUMBER_DMA1(DMA_Stream);
} else {
location = (uint32_t)&DMA2->LISR;
stream_number = GET_STREAM_NUMBER_DMA2(DMA_Stream);
}
/* Get register offset */
if (stream_number >= 4) {
/* High registers for DMA clear */
location += 4;
/* Do offset for high DMA registers */
stream_number -= 4;
}
/* Get register value */
flags = *(__IO uint32_t *)location;
flags >>= DMA_Flags_Bit_Pos[stream_number];
flags &= DMA_FLAG_ALL;
/* Return value */
return flags;
}
void TM_DMA_EnableInterrupts(DMA_Stream_TypeDef* DMA_Stream) {
uint32_t stream_number;
IRQn_Type type;
uint32_t preemption;
/* Clear flags first */
TM_DMA_ClearFlag(DMA_Stream, DMA_FLAG_ALL);
/* Check stream value */
if (DMA_Stream < DMA2_Stream0) {
stream_number = GET_STREAM_NUMBER_DMA1(DMA_Stream);
preemption = DMA1_NVIC_PREEMPTION_PRIORITY;
type = DMA_IRQs[0][stream_number];
} else {
stream_number = GET_STREAM_NUMBER_DMA2(DMA_Stream);
preemption = DMA2_NVIC_PREEMPTION_PRIORITY;
type = DMA_IRQs[1][stream_number];
}
/* Disable IRQ */
HAL_NVIC_DisableIRQ(type);
/* Set priorities */
HAL_NVIC_SetPriority(type, preemption, stream_number);
/* Enable IRQ */
HAL_NVIC_EnableIRQ(type);
/* Enable DMA stream interrupts */
DMA_Stream->CR |= DMA_SxCR_TCIE | DMA_SxCR_HTIE | DMA_SxCR_TEIE | DMA_SxCR_DMEIE;
DMA_Stream->FCR |= DMA_SxFCR_FEIE;
}
void TM_DMA_DisableInterrupts(DMA_Stream_TypeDef* DMA_Stream) {
IRQn_Type IRQValue;
/* Clear flags first */
TM_DMA_ClearFlag(DMA_Stream, DMA_FLAG_ALL);
/* Check stream value */
if (DMA_Stream < DMA2_Stream0) {
IRQValue = DMA_IRQs[0][GET_STREAM_NUMBER_DMA1(DMA_Stream)];
} else {
IRQValue = DMA_IRQs[0][GET_STREAM_NUMBER_DMA2(DMA_Stream)];
}
/* Disable NVIC */
HAL_NVIC_DisableIRQ(IRQValue);
/* Disable DMA stream interrupts */
DMA_Stream->CR &= ~(DMA_SxCR_TCIE | DMA_SxCR_HTIE | DMA_SxCR_TEIE | DMA_SxCR_DMEIE);
DMA_Stream->FCR &= DMA_SxFCR_FEIE;
}
void TM_DMA_Init(DMA_Stream_TypeDef* Stream, DMA_HandleTypeDef* HDMA) {
/* Init DMA stream */
if (HDMA) {
/* Unlock DMA */
__HAL_UNLOCK(HDMA);
/* Init DMA */
HDMA->Instance = Stream;
HAL_DMA_Init(HDMA);
} else {
/* Enable DMA clock */
if (Stream >= DMA2_Stream0) {
/* Enable DMA2 clock */
RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN;
} else {
/* Enable DMA1 clock */
RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN;
}
}
}
void TM_DMA_DeInit(DMA_Stream_TypeDef* Stream) {
DMA_HandleTypeDef DMA_InitStruct;
/* Fill data and deinit DMA stream */
DMA_InitStruct.Instance = Stream;
HAL_DMA_DeInit(&DMA_InitStruct);
}
void TM_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t Source, uint32_t Destination, uint16_t Length) {
/* Unlock DMA */
__HAL_UNLOCK(hdma);
/* Start DMA transfer */
HAL_DMA_Start(hdma, Source, Destination, Length);
}
/*****************************************************************/
/* DMA INTERRUPT USER CALLBACKS */
/*****************************************************************/
__weak void TM_DMA_TransferCompleteHandler(DMA_Stream_TypeDef* DMA_Stream) {
/* NOTE: This function should not be modified, when the callback is needed,
the TM_DMA_TransferCompleteHandler should be implemented in the user file
*/
}
__weak void TM_DMA_HalfTransferCompleteHandler(DMA_Stream_TypeDef* DMA_Stream) {
/* NOTE: This function should not be modified, when the callback is needed,
the TM_DMA_HalfTransferCompleteHandler should be implemented in the user file
*/
}
__weak void TM_DMA_TransferErrorHandler(DMA_Stream_TypeDef* DMA_Stream) {
/* NOTE: This function should not be modified, when the callback is needed,
the TM_DMA_TransferErrorHandler should be implemented in the user file
*/
}
__weak void TM_DMA_DirectModeErrorHandler(DMA_Stream_TypeDef* DMA_Stream) {
/* NOTE: This function should not be modified, when the callback is needed,
the TM_DMA_DirectModeErrorHandler should be implemented in the user file
*/
}
__weak void TM_DMA_FIFOErrorHandler(DMA_Stream_TypeDef* DMA_Stream) {
/* NOTE: This function should not be modified, when the callback is needed,
the TM_DMA_FIFOErrorHandler should be implemented in the user file
*/
}
/*****************************************************************/
/* DMA INTERNAL FUNCTIONS */
/*****************************************************************/
static void TM_DMA_INT_ProcessInterrupt(DMA_Stream_TypeDef* DMA_Stream) {
/* Get DMA interrupt status flags */
uint16_t flags = TM_DMA_GetFlags(DMA_Stream, DMA_FLAG_ALL);
/* Clear flags */
TM_DMA_ClearFlag(DMA_Stream, DMA_FLAG_ALL);
/* Call user callback function */
/* Check transfer complete flag */
if ((flags & DMA_FLAG_TCIF) && (DMA_Stream->CR & DMA_SxCR_TCIE)) {
TM_DMA_TransferCompleteHandler(DMA_Stream);
}
/* Check half-transfer complete flag */
if ((flags & DMA_FLAG_HTIF) && (DMA_Stream->CR & DMA_SxCR_HTIE)) {
TM_DMA_HalfTransferCompleteHandler(DMA_Stream);
}
/* Check transfer error flag */
if ((flags & DMA_FLAG_TEIF) && (DMA_Stream->CR & DMA_SxCR_TEIE)) {
TM_DMA_TransferErrorHandler(DMA_Stream);
}
/* Check direct error flag */
if ((flags & DMA_FLAG_DMEIF) && (DMA_Stream->CR & DMA_SxCR_DMEIE)) {
TM_DMA_DirectModeErrorHandler(DMA_Stream);
}
/* Check FIFO error flag */
if ((flags & DMA_FLAG_FEIF) && (DMA_Stream->FCR & DMA_SxFCR_FEIE)) {
TM_DMA_FIFOErrorHandler(DMA_Stream);
}
}
/* Handle all DMA interrupt handlers possible */
#ifndef DMA1_STREAM0_DISABLE_IRQHANDLER
void DMA1_Stream0_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA1_Stream0);
}
#endif
#ifndef DMA1_STREAM1_DISABLE_IRQHANDLER
void DMA1_Stream1_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA1_Stream1);
}
#endif
#ifndef DMA1_STREAM2_DISABLE_IRQHANDLER
void DMA1_Stream2_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA1_Stream2);
}
#endif
#ifndef DMA1_STREAM3_DISABLE_IRQHANDLER
void DMA1_Stream3_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA1_Stream3);
}
#endif
#ifndef DMA1_STREAM4_DISABLE_IRQHANDLER
void DMA1_Stream4_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA1_Stream4);
}
#endif
#ifndef DMA1_STREAM5_DISABLE_IRQHANDLER
void DMA1_Stream5_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA1_Stream5);
}
#endif
#ifndef DMA1_STREAM6_DISABLE_IRQHANDLER
void DMA1_Stream6_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA1_Stream6);
}
#endif
#ifndef DMA1_STREAM7_DISABLE_IRQHANDLER
void DMA1_Stream7_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA1_Stream7);
}
#endif
#ifndef DMA2_STREAM0_DISABLE_IRQHANDLER
void DMA2_Stream0_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA2_Stream0);
}
#endif
#ifndef DMA2_STREAM1_DISABLE_IRQHANDLER
void DMA2_Stream1_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA2_Stream1);
}
#endif
#ifndef DMA2_STREAM2_DISABLE_IRQHANDLER
void DMA2_Stream2_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA2_Stream2);
}
#endif
#ifndef DMA2_STREAM3_DISABLE_IRQHANDLER
void DMA2_Stream3_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA2_Stream3);
}
#endif
#ifndef DMA2_STREAM4_DISABLE_IRQHANDLER
void DMA2_Stream4_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA2_Stream4);
}
#endif
#ifndef DMA2_STREAM5_DISABLE_IRQHANDLER
void DMA2_Stream5_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA2_Stream5);
}
#endif
#ifndef DMA2_STREAM6_DISABLE_IRQHANDLER
void DMA2_Stream6_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA2_Stream6);
}
#endif
#ifndef DMA2_STREAM7_DISABLE_IRQHANDLER
void DMA2_Stream7_IRQHandler(void) {
/* Call user function */
TM_DMA_INT_ProcessInterrupt(DMA2_Stream7);
}
#endif