23
23
#include "nu_bitutil.h"
24
24
#include "dma.h"
25
25
26
+ #define NU_PDMA_CH_MAX PDMA_CH_MAX /* Specify maximum channels of PDMA */
27
+ #define NU_PDMA_CH_Pos 0 /* Specify first channel number of PDMA */
28
+ #define NU_PDMA_CH_Msk (((1 << NU_PDMA_CH_MAX) - 1) << NU_PDMA_CH_Pos)
29
+
26
30
struct nu_dma_chn_s {
27
31
void (* handler )(uint32_t , uint32_t );
28
32
uint32_t id ;
@@ -31,7 +35,7 @@ struct nu_dma_chn_s {
31
35
32
36
static int dma_inited = 0 ;
33
37
static uint32_t dma_chn_mask = 0 ;
34
- static struct nu_dma_chn_s dma_chn_arr [PDMA_CH_MAX ];
38
+ static struct nu_dma_chn_s dma_chn_arr [NU_PDMA_CH_MAX ];
35
39
36
40
static void pdma_vec (void );
37
41
static const struct nu_modinit_s dma_modinit = {DMA_0 , PDMA_MODULE , 0 , 0 , PDMA_RST , PDMA_IRQn , (void * ) pdma_vec };
@@ -44,7 +48,7 @@ void dma_init(void)
44
48
}
45
49
46
50
dma_inited = 1 ;
47
- dma_chn_mask = 0 ;
51
+ dma_chn_mask = ~ NU_PDMA_CH_Msk ;
48
52
memset (dma_chn_arr , 0x00 , sizeof (dma_chn_arr ));
49
53
50
54
// Reset this module
@@ -65,25 +69,12 @@ int dma_channel_allocate(uint32_t capabilities)
65
69
dma_init ();
66
70
}
67
71
68
- #if 1
69
72
int i = nu_cto (dma_chn_mask );
70
73
if (i != 32 ) {
71
74
dma_chn_mask |= 1 << i ;
72
- memset (dma_chn_arr + i , 0x00 , sizeof (struct nu_dma_chn_s ));
75
+ memset (dma_chn_arr + i - NU_PDMA_CH_Pos , 0x00 , sizeof (struct nu_dma_chn_s ));
73
76
return i ;
74
77
}
75
- #else
76
- int i ;
77
-
78
- for (i = 0 ; i < PDMA_CH_MAX ; i ++ ) {
79
- if ((dma_chn_mask & (1 << i )) == 0 ) {
80
- // Channel available
81
- dma_chn_mask |= 1 << i ;
82
- memset (dma_chn_arr + i , 0x00 , sizeof (struct nu_dma_chn_s ));
83
- return i ;
84
- }
85
- }
86
- #endif
87
78
88
79
// No channel available
89
80
return DMA_ERROR_OUT_OF_CHANNELS ;
@@ -102,9 +93,9 @@ void dma_set_handler(int channelid, uint32_t handler, uint32_t id, uint32_t even
102
93
{
103
94
MBED_ASSERT (dma_chn_mask & (1 << channelid ));
104
95
105
- dma_chn_arr [channelid ].handler = (void (* )(uint32_t , uint32_t )) handler ;
106
- dma_chn_arr [channelid ].id = id ;
107
- dma_chn_arr [channelid ].event = event ;
96
+ dma_chn_arr [channelid - NU_PDMA_CH_Pos ].handler = (void (* )(uint32_t , uint32_t )) handler ;
97
+ dma_chn_arr [channelid - NU_PDMA_CH_Pos ].id = id ;
98
+ dma_chn_arr [channelid - NU_PDMA_CH_Pos ].event = event ;
108
99
109
100
// Set interrupt vector if someone has removed it.
110
101
NVIC_SetVector (dma_modinit .irq_n , (uint32_t ) dma_modinit .var );
@@ -127,14 +118,14 @@ static void pdma_vec(void)
127
118
PDMA_CLR_ABORT_FLAG (abtsts );
128
119
129
120
while (abtsts ) {
130
- int chn_id = nu_ctz (abtsts );
121
+ int chn_id = nu_ctz (abtsts ) - PDMA_ABTSTS_ABTIFn_Pos + NU_PDMA_CH_Pos ;
131
122
if (dma_chn_mask & (1 << chn_id )) {
132
- struct nu_dma_chn_s * dma_chn = dma_chn_arr + chn_id ;
123
+ struct nu_dma_chn_s * dma_chn = dma_chn_arr + chn_id - NU_PDMA_CH_Pos ;
133
124
if (dma_chn -> handler && (dma_chn -> event & DMA_EVENT_ABORT )) {
134
125
dma_chn -> handler (dma_chn -> id , DMA_EVENT_ABORT );
135
126
}
136
127
}
137
- abtsts &= ~(1 << chn_id );
128
+ abtsts &= ~(1 << ( chn_id - NU_PDMA_CH_Pos + PDMA_ABTSTS_ABTIFn_Pos ) );
138
129
}
139
130
}
140
131
@@ -145,14 +136,14 @@ static void pdma_vec(void)
145
136
PDMA_CLR_TD_FLAG (tdsts );
146
137
147
138
while (tdsts ) {
148
- int chn_id = nu_ctz (tdsts );
139
+ int chn_id = nu_ctz (tdsts ) - PDMA_TDSTS_TDIFn_Pos + NU_PDMA_CH_Pos ;
149
140
if (dma_chn_mask & (1 << chn_id )) {
150
- struct nu_dma_chn_s * dma_chn = dma_chn_arr + chn_id ;
141
+ struct nu_dma_chn_s * dma_chn = dma_chn_arr + chn_id - NU_PDMA_CH_Pos ;
151
142
if (dma_chn -> handler && (dma_chn -> event & DMA_EVENT_TRANSFER_DONE )) {
152
143
dma_chn -> handler (dma_chn -> id , DMA_EVENT_TRANSFER_DONE );
153
144
}
154
145
}
155
- tdsts &= ~(1 << chn_id );
146
+ tdsts &= ~(1 << ( chn_id - NU_PDMA_CH_Pos + PDMA_TDSTS_TDIFn_Pos ) );
156
147
}
157
148
}
158
149
@@ -170,14 +161,14 @@ static void pdma_vec(void)
170
161
PDMA -> INTSTS = reqto ;
171
162
172
163
while (reqto ) {
173
- int chn_id = nu_ctz (reqto ) - PDMA_INTSTS_REQTOFn_Pos ;
164
+ int chn_id = nu_ctz (reqto ) - PDMA_INTSTS_REQTOFn_Pos + NU_PDMA_CH_Pos ;
174
165
if (dma_chn_mask & (1 << chn_id )) {
175
- struct nu_dma_chn_s * dma_chn = dma_chn_arr + chn_id ;
166
+ struct nu_dma_chn_s * dma_chn = dma_chn_arr + chn_id - NU_PDMA_CH_Pos ;
176
167
if (dma_chn -> handler && (dma_chn -> event & DMA_EVENT_TIMEOUT )) {
177
168
dma_chn -> handler (dma_chn -> id , DMA_EVENT_TIMEOUT );
178
169
}
179
170
}
180
- reqto &= ~(1 << (chn_id + PDMA_INTSTS_REQTOFn_Pos ));
171
+ reqto &= ~(1 << (chn_id - NU_PDMA_CH_Pos + PDMA_INTSTS_REQTOFn_Pos ));
181
172
}
182
173
}
183
174
}
0 commit comments