1 |
580 |
jeremybenn |
/* ----------------------------------------------------------------------------
|
2 |
|
|
* ATMEL Microcontroller Software Support
|
3 |
|
|
* ----------------------------------------------------------------------------
|
4 |
|
|
* Copyright (c) 2008, Atmel Corporation
|
5 |
|
|
*
|
6 |
|
|
* All rights reserved.
|
7 |
|
|
*
|
8 |
|
|
* Redistribution and use in source and binary forms, with or without
|
9 |
|
|
* modification, are permitted provided that the following conditions are met:
|
10 |
|
|
*
|
11 |
|
|
* - Redistributions of source code must retain the above copyright notice,
|
12 |
|
|
* this list of conditions and the disclaimer below.
|
13 |
|
|
*
|
14 |
|
|
* Atmel's name may not be used to endorse or promote products derived from
|
15 |
|
|
* this software without specific prior written permission.
|
16 |
|
|
*
|
17 |
|
|
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
18 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
19 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
20 |
|
|
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
23 |
|
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24 |
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
26 |
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
|
|
* ----------------------------------------------------------------------------
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
//------------------------------------------------------------------------------
|
31 |
|
|
// Headers
|
32 |
|
|
//------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
#include "dma.h"
|
35 |
|
|
#include <utility/assert.h>
|
36 |
|
|
#include <utility/trace.h>
|
37 |
|
|
|
38 |
|
|
//------------------------------------------------------------------------------
|
39 |
|
|
// Global functions
|
40 |
|
|
//------------------------------------------------------------------------------
|
41 |
|
|
|
42 |
|
|
//------------------------------------------------------------------------------
|
43 |
|
|
/// Configure a DMAC peripheral
|
44 |
|
|
//------------------------------------------------------------------------------
|
45 |
|
|
void DMA_Config(unsigned int flag)
|
46 |
|
|
{
|
47 |
|
|
AT91C_BASE_HDMA->HDMA_GCFG = flag;
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
//------------------------------------------------------------------------------
|
51 |
|
|
/// Enables a DMAC peripheral
|
52 |
|
|
//------------------------------------------------------------------------------
|
53 |
|
|
void DMA_Enable(void)
|
54 |
|
|
{
|
55 |
|
|
AT91C_BASE_HDMA->HDMA_EN = AT91C_HDMA_ENABLE;
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
//------------------------------------------------------------------------------
|
59 |
|
|
/// Disables DMAC peripheral
|
60 |
|
|
//------------------------------------------------------------------------------
|
61 |
|
|
void DMA_Disable(void)
|
62 |
|
|
{
|
63 |
|
|
AT91C_BASE_HDMA->HDMA_EN = ~(AT91C_HDMA_ENABLE);
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
//-----------------------------------------------------------------------------
|
67 |
|
|
/// Enable DMA interrupt
|
68 |
|
|
/// \param flag IT to be enabled
|
69 |
|
|
//-----------------------------------------------------------------------------
|
70 |
|
|
void DMA_EnableIt (unsigned int flag)
|
71 |
|
|
{
|
72 |
|
|
AT91C_BASE_HDMA->HDMA_EBCIER = flag;
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
//-----------------------------------------------------------------------------
|
76 |
|
|
/// Disable DMA interrupt
|
77 |
|
|
/// \param flag IT to be enabled
|
78 |
|
|
//-----------------------------------------------------------------------------
|
79 |
|
|
void DMA_DisableIt (unsigned int flag)
|
80 |
|
|
{
|
81 |
|
|
AT91C_BASE_HDMA->HDMA_EBCIDR = flag;
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
//-----------------------------------------------------------------------------
|
85 |
|
|
/// Return DMA Interrupt Status
|
86 |
|
|
//-----------------------------------------------------------------------------
|
87 |
|
|
unsigned int DMA_GetStatus(void)
|
88 |
|
|
{
|
89 |
|
|
return (AT91C_BASE_HDMA->HDMA_EBCISR);
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
//-----------------------------------------------------------------------------
|
93 |
|
|
/// Return DMA Interrupt Mask Status
|
94 |
|
|
//-----------------------------------------------------------------------------
|
95 |
|
|
unsigned int DMA_GetInterruptMask(void)
|
96 |
|
|
{
|
97 |
|
|
return (AT91C_BASE_HDMA->HDMA_EBCIMR);
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
//-----------------------------------------------------------------------------
|
101 |
|
|
/// Returns the current status register of the given DMA peripheral, but
|
102 |
|
|
/// masking interrupt sources which are not currently enabled.
|
103 |
|
|
//-----------------------------------------------------------------------------
|
104 |
|
|
unsigned int DMA_GetMaskedStatus(void)
|
105 |
|
|
{
|
106 |
|
|
unsigned int status;
|
107 |
|
|
status = AT91C_BASE_HDMA->HDMA_EBCISR;
|
108 |
|
|
status &= AT91C_BASE_HDMA->HDMA_EBCIMR;
|
109 |
|
|
return status;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
//------------------------------------------------------------------------------
|
113 |
|
|
/// Enables DMAC channel
|
114 |
|
|
/// \param channel Particular channel number.
|
115 |
|
|
//------------------------------------------------------------------------------
|
116 |
|
|
void DMA_EnableChannel(unsigned int channel)
|
117 |
|
|
{
|
118 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
119 |
|
|
AT91C_BASE_HDMA->HDMA_CHER |= DMA_ENA << channel;
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
//------------------------------------------------------------------------------
|
123 |
|
|
/// Disables a DMAC channel
|
124 |
|
|
/// \param channel Particular channel number.
|
125 |
|
|
//------------------------------------------------------------------------------
|
126 |
|
|
void DMA_DisableChannel(unsigned int channel)
|
127 |
|
|
{
|
128 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
129 |
|
|
AT91C_BASE_HDMA->HDMA_CHDR |= DMA_DIS << channel;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
//------------------------------------------------------------------------------
|
133 |
|
|
/// Resume DMAC channel from an stall state.
|
134 |
|
|
/// \param channel Particular channel number.
|
135 |
|
|
//------------------------------------------------------------------------------
|
136 |
|
|
void DMA_KeeponChannel(unsigned int channel)
|
137 |
|
|
{
|
138 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
139 |
|
|
AT91C_BASE_HDMA->HDMA_CHER |= DMA_KEEPON << channel;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
//------------------------------------------------------------------------------
|
143 |
|
|
/// Clear automatic mode for multi-buffer transfer.
|
144 |
|
|
/// \param channel Particular channel number.
|
145 |
|
|
//------------------------------------------------------------------------------
|
146 |
|
|
void DMA_ClearAutoMode(unsigned int channel)
|
147 |
|
|
{
|
148 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
149 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CTRLB &= 0x7FFFFFFF;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
//------------------------------------------------------------------------------
|
153 |
|
|
/// Return DMAC channel status
|
154 |
|
|
//------------------------------------------------------------------------------
|
155 |
|
|
unsigned int DMA_GetChannelStatus(void)
|
156 |
|
|
{
|
157 |
|
|
return( AT91C_BASE_HDMA->HDMA_CHSR);
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
//-----------------------------------------------------------------------------
|
161 |
|
|
/// Set DMA source address used by a HDMA channel.
|
162 |
|
|
/// \param channel Particular channel number.
|
163 |
|
|
/// \param sources sources address.
|
164 |
|
|
//-----------------------------------------------------------------------------
|
165 |
|
|
void DMA_SetSourceAddr(unsigned char channel, unsigned int address)
|
166 |
|
|
{
|
167 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
168 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_SADDR = address;
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
//-----------------------------------------------------------------------------
|
172 |
|
|
/// Set DMA destination address used by a HDMA channel.
|
173 |
|
|
/// \param channel Particular channel number.
|
174 |
|
|
/// \param sources destination address.
|
175 |
|
|
//-----------------------------------------------------------------------------
|
176 |
|
|
void DMA_SetDestinationAddr(unsigned char channel, unsigned int address)
|
177 |
|
|
{
|
178 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
179 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_DADDR = address;
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
//-----------------------------------------------------------------------------
|
183 |
|
|
/// Set DMA descriptor address used by a HDMA channel.
|
184 |
|
|
/// \param channel Particular channel number.
|
185 |
|
|
/// \param sources destination address.
|
186 |
|
|
//-----------------------------------------------------------------------------
|
187 |
|
|
void DMA_SetDescriptorAddr(unsigned char channel, unsigned int address)
|
188 |
|
|
{
|
189 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
190 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_DSCR = address ;
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
//-----------------------------------------------------------------------------
|
195 |
|
|
/// Set DMA control A register used by a HDMA channel.
|
196 |
|
|
/// \param channel Particular channel number.
|
197 |
|
|
/// \param size Dma transfer size in byte.
|
198 |
|
|
/// \param sourceWidth Single transfer width for source.
|
199 |
|
|
/// \param destWidth Single transfer width for destination.
|
200 |
|
|
/// \param done Transfer done field.
|
201 |
|
|
//-----------------------------------------------------------------------------
|
202 |
|
|
void DMA_SetSourceBufferSize(unsigned char channel,
|
203 |
|
|
unsigned int size,
|
204 |
|
|
unsigned char sourceWidth,
|
205 |
|
|
unsigned char destWidth,
|
206 |
|
|
unsigned char done)
|
207 |
|
|
{
|
208 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
209 |
|
|
ASSERT(sourceWidth < 4, "width does not support");
|
210 |
|
|
ASSERT(destWidth < 4, "width does not support");
|
211 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CTRLA = (size |
|
212 |
|
|
sourceWidth << 24 |
|
213 |
|
|
destWidth << 28 |
|
214 |
|
|
done << 31);
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
//-----------------------------------------------------------------------------
|
218 |
|
|
/// Set DMA transfer mode for source used by a HDMA channel.
|
219 |
|
|
/// \param channel Particular channel number.
|
220 |
|
|
/// \param transferMode Transfer buffer mode (single, LLI, reload or contiguous)
|
221 |
|
|
/// \param addressingType Type of addrassing mode
|
222 |
|
|
/// 0 : incrementing, 1: decrementing, 2: fixed.
|
223 |
|
|
//-----------------------------------------------------------------------------
|
224 |
|
|
void DMA_SetSourceBufferMode(unsigned char channel,
|
225 |
|
|
unsigned char transferMode,
|
226 |
|
|
unsigned char addressingType)
|
227 |
|
|
{
|
228 |
|
|
unsigned int value;
|
229 |
|
|
|
230 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
231 |
|
|
|
232 |
|
|
value = AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CTRLB;
|
233 |
|
|
value &= ~ (AT91C_SRC_DSCR | AT91C_SRC_INCR | 1<<31);
|
234 |
|
|
switch(transferMode){
|
235 |
|
|
case DMA_TRANSFER_SINGLE:
|
236 |
|
|
value |= AT91C_SRC_DSCR | addressingType << 24;
|
237 |
|
|
break;
|
238 |
|
|
case DMA_TRANSFER_LLI:
|
239 |
|
|
value |= addressingType << 24;
|
240 |
|
|
break;
|
241 |
|
|
case DMA_TRANSFER_RELOAD:
|
242 |
|
|
case DMA_TRANSFER_CONTIGUOUS:
|
243 |
|
|
value |= AT91C_SRC_DSCR | addressingType << 24 | 1<<31;
|
244 |
|
|
break;
|
245 |
|
|
}
|
246 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CTRLB = value;
|
247 |
|
|
|
248 |
|
|
if(transferMode == DMA_TRANSFER_RELOAD || transferMode == DMA_TRANSFER_CONTIGUOUS){
|
249 |
|
|
value = AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CFG;
|
250 |
|
|
value &= ~ (AT91C_SRC_REP);
|
251 |
|
|
// When automatic mode is activated, the source address and the control register are reloaded from previous transfer.
|
252 |
|
|
if(transferMode == DMA_TRANSFER_RELOAD) {
|
253 |
|
|
value |= AT91C_SRC_REP;
|
254 |
|
|
}
|
255 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CFG = value;
|
256 |
|
|
}
|
257 |
|
|
else {
|
258 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CFG = 0;
|
259 |
|
|
}
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
//-----------------------------------------------------------------------------
|
263 |
|
|
/// Set DMA transfer mode for destination used by a HDMA channel.
|
264 |
|
|
/// \param channel Particular channel number.
|
265 |
|
|
/// \param transferMode Transfer buffer mode (single, LLI, reload or contiguous)
|
266 |
|
|
/// \param addressingType Type of addrassing mode
|
267 |
|
|
/// 0 : incrementing, 1: decrementing, 2: fixed.
|
268 |
|
|
//-----------------------------------------------------------------------------
|
269 |
|
|
void DMA_SetDestBufferMode(unsigned char channel,
|
270 |
|
|
unsigned char transferMode,
|
271 |
|
|
unsigned char addressingType)
|
272 |
|
|
{
|
273 |
|
|
unsigned int value;
|
274 |
|
|
|
275 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
276 |
|
|
|
277 |
|
|
value = AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CTRLB;
|
278 |
|
|
value &= ~ (AT91C_DST_DSCR | AT91C_DST_INCR);
|
279 |
|
|
|
280 |
|
|
switch(transferMode){
|
281 |
|
|
case DMA_TRANSFER_SINGLE:
|
282 |
|
|
case DMA_TRANSFER_RELOAD:
|
283 |
|
|
case DMA_TRANSFER_CONTIGUOUS:
|
284 |
|
|
value |= AT91C_DST_DSCR | addressingType << 24;
|
285 |
|
|
break;
|
286 |
|
|
case DMA_TRANSFER_LLI:
|
287 |
|
|
value |= addressingType << 24;
|
288 |
|
|
break;
|
289 |
|
|
}
|
290 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CTRLB = value;
|
291 |
|
|
if(transferMode == DMA_TRANSFER_RELOAD || transferMode == DMA_TRANSFER_CONTIGUOUS){
|
292 |
|
|
value = AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CFG;
|
293 |
|
|
value &= ~ (AT91C_DST_REP);
|
294 |
|
|
// When automatic mode is activated, the source address and the control register are reloaded from previous transfer.
|
295 |
|
|
if(transferMode == DMA_TRANSFER_RELOAD) {
|
296 |
|
|
value |= AT91C_DST_REP;
|
297 |
|
|
}
|
298 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CFG = value;
|
299 |
|
|
}
|
300 |
|
|
else {
|
301 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CFG = 0;
|
302 |
|
|
}
|
303 |
|
|
}
|
304 |
|
|
|
305 |
|
|
//------------------------------------------------------------------------------
|
306 |
|
|
/// Set DMA configuration registe used by a HDMA channel.
|
307 |
|
|
/// \param channel Particular channel number.
|
308 |
|
|
/// \param value configuration value.
|
309 |
|
|
//------------------------------------------------------------------------------
|
310 |
|
|
void DMA_SetConfiguration(unsigned char channel, unsigned int value)
|
311 |
|
|
{
|
312 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
313 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CFG = value;
|
314 |
|
|
}
|
315 |
|
|
|
316 |
|
|
//------------------------------------------------------------------------------
|
317 |
|
|
/// Set DMA source PIP configuration used by a HDMA channel.
|
318 |
|
|
/// \param channel Particular channel number.
|
319 |
|
|
/// \param pipHole stop on done mode.
|
320 |
|
|
/// \param pipBoundary lock mode.
|
321 |
|
|
//------------------------------------------------------------------------------
|
322 |
|
|
void DMA_SPIPconfiguration(unsigned char channel,
|
323 |
|
|
unsigned int pipHole,
|
324 |
|
|
unsigned int pipBoundary)
|
325 |
|
|
|
326 |
|
|
{
|
327 |
|
|
unsigned int value;
|
328 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
329 |
|
|
value = AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CTRLB;
|
330 |
|
|
value &= ~ (AT91C_SRC_PIP);
|
331 |
|
|
value |= AT91C_SRC_PIP;
|
332 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CTRLB = value;
|
333 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_SPIP = (pipHole + 1) | pipBoundary <<16;
|
334 |
|
|
}
|
335 |
|
|
|
336 |
|
|
//------------------------------------------------------------------------------
|
337 |
|
|
/// Set DMA destination PIP configuration used by a HDMA channel.
|
338 |
|
|
/// \param channel Particular channel number.
|
339 |
|
|
/// \param pipHole stop on done mode.
|
340 |
|
|
/// \param pipBoundary lock mode.
|
341 |
|
|
//------------------------------------------------------------------------------
|
342 |
|
|
void DMA_DPIPconfiguration(unsigned char channel,
|
343 |
|
|
unsigned int pipHole,
|
344 |
|
|
unsigned int pipBoundary)
|
345 |
|
|
|
346 |
|
|
{
|
347 |
|
|
unsigned int value;
|
348 |
|
|
ASSERT(channel < DMA_CHANNEL_NUM, "this channel does not exist");
|
349 |
|
|
value = AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CTRLB;
|
350 |
|
|
value &= ~ (AT91C_DST_PIP);
|
351 |
|
|
value |= AT91C_DST_PIP;
|
352 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_CTRLB = value;
|
353 |
|
|
AT91C_BASE_HDMA->HDMA_CH[channel].HDMA_DPIP = (pipHole + 1) | pipBoundary <<16;
|
354 |
|
|
}
|
355 |
|
|
|