APM:Libraries
dma.h
Go to the documentation of this file.
1 /******************************************************************************
2  * The MIT License
3 
4 (c) 2017 night_ghost@ykoctpa.ru
5 
6 based on:
7 
8  *
9  * Copyright (c) 2010 Michael Hope.
10  *
11  * Permission is hereby granted, free of charge, to any person
12  * obtaining a copy of this software and associated documentation
13  * files (the "Software"), to deal in the Software without
14  * restriction, including without limitation the rights to use, copy,
15  * modify, merge, publish, distribute, sublicense, and/or sell copies
16  * of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29  * SOFTWARE.
30  *****************************************************************************/
31 
32 #ifndef _DMA_H_
33 #define _DMA_H_
34 
35 #include "hal_types.h"
36 
37 #ifdef __cplusplus
38 extern "C"{
39 #endif
40 
41 
42 /*
43  * Register maps
44  */
45 
50 typedef struct dma_stream_t {
51  __IO uint32_t CR;
52  __IO uint32_t NDTR;
53  __IO uint32_t PAR;
54  __IO uint32_t M0AR;
55  __IO uint32_t M1AR;
56  __IO uint32_t FCR;
57 } dma_stream_t;
58 
68 typedef enum dma_channel {
69  DMA_CH0 = 0,
70  DMA_CH1 = 1,
71  DMA_CH2 = 2,
72  DMA_CH3 = 3,
73  DMA_CH4 = 4,
74  DMA_CH5 = 5,
75  DMA_CH6 = 6,
76  DMA_CH7 = 7,
77 } dma_channel;
78 
83 typedef struct dma_reg_map {
84  __IO uint32_t LISR;
85  __IO uint32_t HISR;
86  __IO uint32_t LIFCR;
87  __IO uint32_t HIFCR;
88  dma_stream_t STREAM[8];
89 } dma_reg_map;
90 
91 
92 /*
93  * Register bit definitions
94  */
95 
96 /* Channel configuration register */
97 
98 #define DMA_CR_CH0 (0x0 << 25)
99 #define DMA_CR_CH1 (0x1 << 25)
100 #define DMA_CR_CH2 (0x2 << 25)
101 #define DMA_CR_CH3 (0x3 << 25)
102 #define DMA_CR_CH4 (0x4 << 25)
103 #define DMA_CR_CH5 (0x5 << 25)
104 #define DMA_CR_CH6 (0x6 << 25)
105 #define DMA_CR_CH7 (0x7 << 25)
106 #define DMA_CR_MBURST0 (0x0 << 23)
107 #define DMA_CR_MBURST4 (0x1 << 23)
108 #define DMA_CR_MBURST8 (0x2 << 23)
109 #define DMA_CR_MBURST16 (0x3 << 23)
110 #define DMA_CR_PBURST0 (0x0 << 21)
111 #define DMA_CR_PBURST4 (0x1 << 21)
112 #define DMA_CR_PBURST8 (0x2 << 21)
113 #define DMA_CR_PBURST16 (0x3 << 21)
114 #define DMA_CR_CT0 (0x0 << 19)
115 #define DMA_CR_CT1 (0x1 << 19)
116 #define DMA_CR_DBM (0x1 << 18)
117 
118 #define DMA_CR_PL_LOW (0x0 << 16)
119 #define DMA_CR_PL_MEDIUM (0x1 << 16)
120 #define DMA_CR_PL_HIGH (0x2 << 16)
121 #define DMA_CR_PL_VERY_HIGH (0x3 << 16)
122 #define DMA_CR_PL_MASK (0x3 << 16)
123 
124 #define DMA_CR_PINCOS (0x1 << 15)
125 
126 #define DMA_CR_MSIZE_8BITS (0x0 << 13)
127 #define DMA_CR_MSIZE_16BITS (0x1 << 13)
128 #define DMA_CR_MSIZE_32BITS (0x2 << 13)
129 
130 #define DMA_CR_PSIZE_8BITS (0x0 << 11)
131 #define DMA_CR_PSIZE_16BITS (0x1 << 11)
132 #define DMA_CR_PSIZE_32BITS (0x2 << 11)
133 
134 #define DMA_CR_MINC (0x1 << 10)
135 #define DMA_CR_PINC (0x1 << 9)
136 #define DMA_CR_CIRC (0x1 << 8)
137 #define DMA_CR_DIR_P2M (0x0 << 6)
138 #define DMA_CR_DIR_M2P (0x1 << 6)
139 #define DMA_CR_DIR_M2M (0x2 << 6)
140 
141 #define DMA_CR_PFCTRL (0x1 << 5)
142 #define DMA_CR_TCIE (0x1 << 4)
143 #define DMA_CR_HTIE (0x1 << 3)
144 #define DMA_CR_TEIE (0x1 << 2)
145 #define DMA_CR_DMEIE (0x1 << 1)
146 #define DMA_CR_EN (0x1)
147 
148 #define DMA_FLAG_FEIF ((uint32_t)0x01)
149 #define DMA_FLAG_DMEIF ((uint32_t)0x04)
150 #define DMA_FLAG_TEIF ((uint32_t)0x08)
151 #define DMA_FLAG_HTIF ((uint32_t)0x10)
152 #define DMA_FLAG_TCIF ((uint32_t)0x20)
153 
154 #define DMA_FIFOMode_Disable ((uint32_t)0x00000000)
155 #define DMA_FIFOMode_Enable ((uint32_t)0x00000004)
156 
157 #define DMA_FIFOThreshold_1QuarterFull ((uint32_t)0x00000000)
158 #define DMA_FIFOThreshold_HalfFull ((uint32_t)0x00000001)
159 #define DMA_FIFOThreshold_3QuartersFull ((uint32_t)0x00000002)
160 #define DMA_FIFOThreshold_Full ((uint32_t)0x00000003)
161 
162 #define DMA_Priority_Low ((uint32_t)0x00000000)
163 #define DMA_Priority_Medium ((uint32_t)0x00010000)
164 #define DMA_Priority_High ((uint32_t)0x00020000)
165 #define DMA_Priority_VeryHigh ((uint32_t)0x00030000)
166 
170 typedef enum Dma_stream {
179  DMA2_STREAM0 = 0x10 + 0,
180  DMA2_STREAM1 = 0x10 + 1,
181  DMA2_STREAM2 = 0x10 + 2,
182  DMA2_STREAM3 = 0x10 + 3,
183  DMA2_STREAM4 = 0x10 + 4,
184  DMA2_STREAM5 = 0x10 + 5,
185  DMA2_STREAM6 = 0x10 + 6,
186  DMA2_STREAM7 = 0x10 + 7,
188 } dma_stream;
189 
190 
191 /*
192  * Devices
193  */
194 
195 
200 typedef struct dma_dev {
202  uint32_t clk_id;
203  IRQn_Type irq_lines[8];
204  Handler *handlers; // pointer to RAM array of handlers
205 } dma_dev;
206 
207 
208 
209 //extern const dma_dev * const _DMA1;
210 //extern const dma_dev * const _DMA2;
211 #define _DMA1 (&dma1);
212 #define _DMA2 (&dma2);
213 
214 
215 /*
216  * Convenience functions
217  */
218 
219 void dma_init(dma_stream stream);
220 
222 typedef enum dma_mode_flags {
223  DMA_MEM_2_MEM = 1 << 14,
224  DMA_MINC_MODE = 1 << 7,
225  DMA_PINC_MODE = 1 << 6,
226  DMA_CIRC_MODE = 1 << 5,
227  DMA_FROM_MEM = 1 << 4,
228  DMA_TRNS_ERR = 1 << 3,
229  DMA_HALF_TRNS = 1 << 2,
230  DMA_TRNS_CMPLT = 1 << 1
232 
234 typedef enum dma_xfer_size {
238 } dma_xfer_size;
239 
240 
241 
242 void dma_setup_transfer(dma_stream stream,
243  __IO void *peripheral_address,
244  __IO void *memory_address0,
245  uint32_t flags,
246  uint32_t fifo_flags);
247 
248 // memory-memory
249 void dma_setup_transfer_mm(dma_stream stream,
250  __IO void *memory_address0,
251  __IO void *memory_address1,
252  uint32_t flags,
253  uint32_t fifo_flags);
254 
255 
256 // copied from ST lib but all flags combined
257 typedef struct
258 {
265  uint32_t DMA_BufferSize;
270  uint32_t DMA_FIFO_flags;
275  uint32_t DMA_flags; // specifies all below
276 
277 #if 0
278  uint32_t DMA_Channel;
282  uint32_t DMA_DIR;
286  uint32_t DMA_PeripheralInc;
289  uint32_t DMA_MemoryInc;
293  uint32_t DMA_PeripheralDataSize;
296  uint32_t DMA_MemoryDataSize;
299  uint32_t DMA_Mode;
304  uint32_t DMA_Priority;
308  uint32_t DMA_MemoryBurst;
313  uint32_t DMA_PeripheralBurst;
317 #endif
318 } DMA_InitType;
319 
320 
321 void dma_init_transfer(dma_stream stream, DMA_InitType *);
322 
323 void dma_set_num_transfers(dma_stream stream, uint16_t num_transfers);
324 
325 void dma_attach_interrupt(dma_stream stream, Handler handler, uint8_t flag);
326 
327 void dma_detach_interrupt(dma_stream stream);
328 
329 void dma_enable(dma_stream stream);
330 
331 void dma_disable(dma_stream stream);
332 
338 uint8_t dma_is_stream_enabled(dma_stream stream);
339 
350 uint8_t dma_get_isr_bits(dma_stream stream);
351 
358 void dma_clear_isr_bits(dma_stream stream);
359 
360 void DMA1_Stream0_IRQHandler(void);
361 void DMA1_Stream1_IRQHandler(void);
362 void DMA1_Stream2_IRQHandler(void);
363 void DMA1_Stream3_IRQHandler(void);
364 void DMA1_Stream4_IRQHandler(void);
365 void DMA1_Stream5_IRQHandler(void);
366 void DMA1_Stream6_IRQHandler(void);
367 void DMA1_Stream7_IRQHandler(void);
368 void DMA2_Stream0_IRQHandler(void);
369 void DMA2_Stream1_IRQHandler(void);
370 void DMA2_Stream2_IRQHandler(void);
371 void DMA2_Stream3_IRQHandler(void);
372 void DMA2_Stream4_IRQHandler(void);
373 void DMA2_Stream5_IRQHandler(void);
374 void DMA2_Stream6_IRQHandler(void);
375 void DMA2_Stream7_IRQHandler(void);
376 
377 
378 
379 #ifdef __cplusplus
380 } // extern "C"
381 #endif
382 
383 #endif
__IO uint32_t LIFCR
Definition: dma.h:86
Definition: dma.h:73
Definition: dma.h:76
void dma_detach_interrupt(dma_stream stream)
Detach a DMA transfer interrupt handler.
Definition: dma.c:132
struct dma_reg_map dma_reg_map
DMA register map type.
void DMA2_Stream4_IRQHandler(void)
Definition: dma.c:387
uint32_t DMA_flags
Definition: dma.h:275
void DMA1_Stream0_IRQHandler(void)
Definition: dma.c:339
Definition: dma.h:75
Definition: dma.h:69
struct dma_stream_t dma_stream_t
DMA stream type.
void dma_init_transfer(dma_stream stream, DMA_InitType *)
Definition: dma.c:144
Dma_stream
Definition: dma.h:170
void DMA1_Stream5_IRQHandler(void)
Definition: dma.c:359
__IO uint32_t M1AR
Definition: dma.h:55
uint32_t DMA_PeripheralBaseAddr
Definition: dma.h:259
void DMA2_Stream6_IRQHandler(void)
Definition: dma.c:395
void DMA2_Stream3_IRQHandler(void)
Definition: dma.c:383
void DMA1_Stream7_IRQHandler(void)
Definition: dma.c:367
__IO uint32_t PAR
Definition: dma.h:53
__IO uint32_t CR
Definition: dma.h:51
void DMA2_Stream2_IRQHandler(void)
Definition: dma.c:379
DMA register map type.
Definition: dma.h:83
dma_channel
DMA channels.
Definition: dma.h:68
void dma_setup_transfer_mm(dma_stream stream, __IO void *memory_address0, __IO void *memory_address1, uint32_t flags, uint32_t fifo_flags)
__IO uint32_t HISR
Definition: dma.h:85
__IO uint32_t FCR
Definition: dma.h:56
void DMA2_Stream0_IRQHandler(void)
Definition: dma.c:371
__IO uint32_t LISR
Definition: dma.h:84
void dma_clear_isr_bits(dma_stream stream)
Clear the ISR status bits for a given DMA stream.
Definition: dma.c:237
void DMA2_Stream5_IRQHandler(void)
Definition: dma.c:391
void dma_attach_interrupt(dma_stream stream, Handler handler, uint8_t flag)
Attach an interrupt to a DMA transfer.
Definition: dma.c:108
dma_mode_flags
Definition: dma.h:222
void DMA1_Stream2_IRQHandler(void)
Definition: dma.c:347
void DMA1_Stream6_IRQHandler(void)
Definition: dma.c:363
void DMA1_Stream1_IRQHandler(void)
Definition: dma.c:343
dma_xfer_size
Definition: dma.h:234
void dma_set_num_transfers(dma_stream stream, uint16_t num_transfers)
Definition: dma.c:206
void dma_init(dma_stream stream)
Initialize a DMA device.
Definition: dma.c:85
uint32_t DMA_FIFO_flags
Definition: dma.h:270
dma_reg_map * regs
Definition: dma.h:201
uint8_t dma_get_isr_bits(dma_stream stream)
Get the ISR status bits for a DMA stream.
Definition: dma.c:272
uint8_t dma_is_stream_enabled(dma_stream stream)
Check if a DMA stream is enabled.
Definition: dma.c:230
enum Dma_stream dma_stream
__IO uint32_t HIFCR
Definition: dma.h:87
Handler * handlers
Definition: dma.h:204
uint32_t clk_id
Definition: dma.h:202
Definition: dma.h:70
uint32_t DMA_BufferSize
Definition: dma.h:265
void dma_enable(dma_stream stream)
Definition: dma.c:213
Definition: dma.h:71
Definition: dma.h:74
uint32_t DMA_Memory0BaseAddr
Definition: dma.h:261
struct dma_dev dma_dev
void DMA2_Stream1_IRQHandler(void)
Definition: dma.c:375
void uint32_t uint32_t uint32_t flag
Definition: systick.h:80
void DMA1_Stream4_IRQHandler(void)
Definition: dma.c:355
uint64_t Handler
Definition: hal_types.h:19
__IO uint32_t NDTR
Definition: dma.h:52
void dma_setup_transfer(dma_stream stream, __IO void *peripheral_address, __IO void *memory_address0, uint32_t flags, uint32_t fifo_flags)
Definition: dma.h:72
DMA stream type.
Definition: dma.h:50
void DMA1_Stream3_IRQHandler(void)
Definition: dma.c:351
void dma_disable(dma_stream stream)
Definition: dma.c:219
void DMA2_Stream7_IRQHandler(void)
Definition: dma.c:399
__IO uint32_t M0AR
Definition: dma.h:54
Definition: dma.h:200