-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathudma.c
448 lines (391 loc) · 16.8 KB
/
udma.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/******************************************************************************
* Filename: udma.c
* Revised: 2017-04-26 18:27:45 +0200 (Wed, 26 Apr 2017)
* Revision: 48852
*
* Description: Driver for the uDMA controller
*
* Copyright (c) 2015 - 2017, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3) Neither the name of the ORGANIZATION nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
#include "udma.h"
//*****************************************************************************
//
// Handle support for DriverLib in ROM:
// This section will undo prototype renaming made in the header file
//
//*****************************************************************************
#if !defined(DOXYGEN)
#undef uDMAChannelAttributeEnable
#define uDMAChannelAttributeEnable NOROM_uDMAChannelAttributeEnable
#undef uDMAChannelAttributeDisable
#define uDMAChannelAttributeDisable NOROM_uDMAChannelAttributeDisable
#undef uDMAChannelAttributeGet
#define uDMAChannelAttributeGet NOROM_uDMAChannelAttributeGet
#undef uDMAChannelControlSet
#define uDMAChannelControlSet NOROM_uDMAChannelControlSet
#undef uDMAChannelTransferSet
#define uDMAChannelTransferSet NOROM_uDMAChannelTransferSet
#undef uDMAChannelScatterGatherSet
#define uDMAChannelScatterGatherSet NOROM_uDMAChannelScatterGatherSet
#undef uDMAChannelSizeGet
#define uDMAChannelSizeGet NOROM_uDMAChannelSizeGet
#undef uDMAChannelModeGet
#define uDMAChannelModeGet NOROM_uDMAChannelModeGet
#endif
//*****************************************************************************
//
// Enables attributes of a uDMA channel
//
//*****************************************************************************
void
uDMAChannelAttributeEnable(uint32_t ui32Base, uint32_t ui32ChannelNum,
uint32_t ui32Attr)
{
// Check the arguments.
ASSERT(uDMABaseValid(ui32Base));
ASSERT(ui32ChannelNum < UDMA_NUM_CHANNELS);
ASSERT((ui32Attr & ~(UDMA_ATTR_USEBURST | UDMA_ATTR_ALTSELECT |
UDMA_ATTR_HIGH_PRIORITY | UDMA_ATTR_REQMASK)) == 0);
// Set the useburst bit for this channel if set in ui32Attr.
if(ui32Attr & UDMA_ATTR_USEBURST)
{
HWREG(ui32Base + UDMA_O_SETBURST) = 1 << ui32ChannelNum;
}
// Set the alternate control select bit for this channel,
// if set in ui32Attr.
if(ui32Attr & UDMA_ATTR_ALTSELECT)
{
HWREG(ui32Base + UDMA_O_SETCHNLPRIALT) = 1 << ui32ChannelNum;
}
// Set the high priority bit for this channel, if set in ui32Attr.
if(ui32Attr & UDMA_ATTR_HIGH_PRIORITY)
{
HWREG(ui32Base + UDMA_O_SETCHNLPRIORITY) = 1 << ui32ChannelNum;
}
// Set the request mask bit for this channel, if set in ui32Attr.
if(ui32Attr & UDMA_ATTR_REQMASK)
{
HWREG(ui32Base + UDMA_O_SETREQMASK) = 1 << ui32ChannelNum;
}
}
//*****************************************************************************
//
// Disables attributes of an uDMA channel
//
//*****************************************************************************
void
uDMAChannelAttributeDisable(uint32_t ui32Base, uint32_t ui32ChannelNum,
uint32_t ui32Attr)
{
// Check the arguments.
ASSERT(uDMABaseValid(ui32Base));
ASSERT(ui32ChannelNum < UDMA_NUM_CHANNELS);
ASSERT((ui32Attr & ~(UDMA_ATTR_USEBURST | UDMA_ATTR_ALTSELECT |
UDMA_ATTR_HIGH_PRIORITY | UDMA_ATTR_REQMASK)) == 0);
// Clear the useburst bit for this channel if set in ui32Attr.
if(ui32Attr & UDMA_ATTR_USEBURST)
{
HWREG(ui32Base + UDMA_O_CLEARBURST) = 1 << ui32ChannelNum;
}
// Clear the alternate control select bit for this channel, if set in
// ululAttr.
if(ui32Attr & UDMA_ATTR_ALTSELECT)
{
HWREG(ui32Base + UDMA_O_CLEARCHNLPRIALT) = 1 << ui32ChannelNum;
}
// Clear the high priority bit for this channel, if set in ui32Attr.
if(ui32Attr & UDMA_ATTR_HIGH_PRIORITY)
{
HWREG(ui32Base + UDMA_O_CLEARCHNLPRIORITY) = 1 << ui32ChannelNum;
}
// Clear the request mask bit for this channel, if set in ui32Attr.
if(ui32Attr & UDMA_ATTR_REQMASK)
{
HWREG(ui32Base + UDMA_O_CLEARREQMASK) = 1 << ui32ChannelNum;
}
}
//*****************************************************************************
//
// Gets the enabled attributes of a uDMA channel
//
//*****************************************************************************
uint32_t
uDMAChannelAttributeGet(uint32_t ui32Base, uint32_t ui32ChannelNum)
{
uint32_t ui32Attr = 0;
// Check the arguments.
ASSERT(uDMABaseValid(ui32Base));
ASSERT(ui32ChannelNum < UDMA_NUM_CHANNELS);
// Check to see if useburst bit is set for this channel.
if(HWREG(ui32Base + UDMA_O_SETBURST) & (1 << ui32ChannelNum))
{
ui32Attr |= UDMA_ATTR_USEBURST;
}
// Check to see if the alternate control bit is set for this channel.
if(HWREG(ui32Base + UDMA_O_SETCHNLPRIALT) & (1 << ui32ChannelNum))
{
ui32Attr |= UDMA_ATTR_ALTSELECT;
}
// Check to see if the high priority bit is set for this channel.
if(HWREG(ui32Base + UDMA_O_SETCHNLPRIORITY) & (1 << ui32ChannelNum))
{
ui32Attr |= UDMA_ATTR_HIGH_PRIORITY;
}
// Check to see if the request mask bit is set for this channel.
if(HWREG(ui32Base + UDMA_O_SETREQMASK) & (1 << ui32ChannelNum))
{
ui32Attr |= UDMA_ATTR_REQMASK;
}
// Return the configuration flags.
return(ui32Attr);
}
//*****************************************************************************
//
// Sets the control parameters for a uDMA channel control structure
//
//*****************************************************************************
void
uDMAChannelControlSet(uint32_t ui32Base, uint32_t ui32ChannelStructIndex,
uint32_t ui32Control)
{
tDMAControlTable *pControlTable;
// Check the arguments.
ASSERT(uDMABaseValid(ui32Base));
ASSERT(ui32ChannelStructIndex < (UDMA_NUM_CHANNELS * 2));
ASSERT(HWREG(ui32Base + UDMA_O_CTRL) != 0);
// Get the base address of the control table.
pControlTable = (tDMAControlTable *)HWREG(ui32Base + UDMA_O_CTRL);
// Get the current control word value and mask off the fields to be
// changed, then OR in the new settings.
pControlTable[ui32ChannelStructIndex].ui32Control =
((pControlTable[ui32ChannelStructIndex].ui32Control &
~(UDMA_DST_INC_M |
UDMA_SRC_INC_M |
UDMA_SIZE_M |
UDMA_ARB_M |
UDMA_NEXT_USEBURST)) |
ui32Control);
}
//*****************************************************************************
//
// Sets the transfer parameters for a uDMA channel control structure
//
//*****************************************************************************
void
uDMAChannelTransferSet(uint32_t ui32Base, uint32_t ui32ChannelStructIndex,
uint32_t ui32Mode, void *pvSrcAddr, void *pvDstAddr,
uint32_t ui32TransferSize)
{
tDMAControlTable *pControlTable;
uint32_t ui32Control;
uint32_t ui32Inc;
uint32_t ui32BufferBytes;
// Check the arguments.
ASSERT(uDMABaseValid(ui32Base));
ASSERT(ui32ChannelStructIndex < (UDMA_NUM_CHANNELS * 2));
ASSERT(HWREG(ui32Base + UDMA_O_CTRL) != 0);
ASSERT(ui32Mode <= UDMA_MODE_PER_SCATTER_GATHER);
ASSERT((uint32_t)pvSrcAddr >= SRAM_BASE);
ASSERT((uint32_t)pvDstAddr >= SRAM_BASE);
ASSERT((ui32TransferSize != 0) && (ui32TransferSize <= UDMA_XFER_SIZE_MAX));
// Get the base address of the control table.
pControlTable = (tDMAControlTable *)HWREG(ui32Base + UDMA_O_CTRL);
// Get the current control word value and mask off the mode and size
// fields.
ui32Control = (pControlTable[ui32ChannelStructIndex].ui32Control &
~(UDMA_XFER_SIZE_M | UDMA_MODE_M));
// Adjust the mode if the alt control structure is selected.
if(ui32ChannelStructIndex & UDMA_ALT_SELECT)
{
if((ui32Mode == UDMA_MODE_MEM_SCATTER_GATHER) ||
(ui32Mode == UDMA_MODE_PER_SCATTER_GATHER))
{
ui32Mode |= UDMA_MODE_ALT_SELECT;
}
}
// Set the transfer size and mode in the control word (but don't write the
// control word yet as it could kick off a transfer).
ui32Control |= ui32Mode | ((ui32TransferSize - 1) << UDMA_XFER_SIZE_S);
// Get the address increment value for the source, from the control word.
ui32Inc = (ui32Control & UDMA_SRC_INC_M);
// Compute the ending source address of the transfer. If the source
// increment is set to none, then the ending address is the same as the
// beginning.
if(ui32Inc != UDMA_SRC_INC_NONE)
{
ui32Inc = ui32Inc >> UDMA_SRC_INC_S;
ui32BufferBytes = ui32TransferSize << ui32Inc;
pvSrcAddr = (void *)((uint32_t)pvSrcAddr + ui32BufferBytes - (1 << ui32Inc));
}
// Load the source ending address into the control block.
pControlTable[ui32ChannelStructIndex].pvSrcEndAddr = pvSrcAddr;
// Get the address increment value for the destination, from the control
// word.
ui32Inc = ui32Control & UDMA_DST_INC_M;
// Compute the ending destination address of the transfer. If the
// destination increment is set to none, then the ending address is the
// same as the beginning.
if(ui32Inc != UDMA_DST_INC_NONE)
{
// There is a special case if this is setting up a scatter-gather
// transfer. The destination pointer needs to point to the end of
// the alternate structure for this channel instead of calculating
// the end of the buffer in the normal way.
if((ui32Mode == UDMA_MODE_MEM_SCATTER_GATHER) ||
(ui32Mode == UDMA_MODE_PER_SCATTER_GATHER))
{
pvDstAddr =
(void *)&pControlTable[ui32ChannelStructIndex |
UDMA_ALT_SELECT].ui32Spare;
}
// Not a scatter-gather transfer, calculate end pointer normally.
else
{
ui32Inc = ui32Inc >> UDMA_DST_INC_S;
ui32BufferBytes = ui32TransferSize << ui32Inc;
pvDstAddr = (void *)((uint32_t)pvDstAddr + ui32BufferBytes - 1);
}
}
// Load the destination ending address into the control block.
pControlTable[ui32ChannelStructIndex].pvDstEndAddr = pvDstAddr;
// Write the new control word value.
pControlTable[ui32ChannelStructIndex].ui32Control = ui32Control;
}
//*****************************************************************************
//
// Configures a uDMA channel for scatter-gather mode
//
//*****************************************************************************
void
uDMAChannelScatterGatherSet(uint32_t ui32Base, uint32_t ui32ChannelNum,
uint32_t ui32TaskCount, void *pvTaskList,
uint32_t ui32IsPeriphSG)
{
tDMAControlTable *pControlTable;
tDMAControlTable *pTaskTable;
// Check the parameters.
ASSERT(uDMABaseValid(ui32Base));
ASSERT(ui32ChannelNum < UDMA_NUM_CHANNELS);
ASSERT(HWREG(ui32Base + UDMA_O_CTRL) != 0);
ASSERT(pvTaskList != 0);
ASSERT(ui32TaskCount <= UDMA_XFER_SIZE_MAX);
ASSERT(ui32TaskCount != 0);
// Get the base address of the control table.
pControlTable = (tDMAControlTable *)HWREG(ui32Base + UDMA_O_CTRL);
// Get a handy pointer to the task list.
pTaskTable = (tDMAControlTable *)pvTaskList;
// Compute the ending address for the source pointer. This will be the
// last element of the last task in the task table.
pControlTable[ui32ChannelNum].pvSrcEndAddr =
&pTaskTable[ui32TaskCount - 1].ui32Spare;
// Compute the ending address for the destination pointer. This will be
// the end of the alternate structure for this channel.
pControlTable[ui32ChannelNum].pvDstEndAddr =
&pControlTable[ui32ChannelNum | UDMA_ALT_SELECT].ui32Spare;
// Compute the control word. Most configurable items are fixed for
// scatter-gather. Item and increment sizes are all 32-bit and arb
// size must be 4. The count is the number of items in the task list
// times 4 (4 words per task).
pControlTable[ui32ChannelNum].ui32Control =
(UDMA_DST_INC_32 | UDMA_SRC_INC_32 |
UDMA_SIZE_32 | UDMA_ARB_4 |
(((ui32TaskCount * 4) - 1) << UDMA_XFER_SIZE_S) |
(ui32IsPeriphSG ? UDMA_MODE_PER_SCATTER_GATHER :
UDMA_MODE_MEM_SCATTER_GATHER));
// Scatter-gather operations can leave the alt bit set. So if doing
// back to back scatter-gather transfers, the second attempt may not
// work correctly because the alt bit is set. Therefore, clear the
// alt bit here to ensure that it is always cleared before a new SG
// transfer is started.
HWREG(ui32Base + UDMA_O_CLEARCHNLPRIALT) = 1 << ui32ChannelNum;
}
//*****************************************************************************
//
// Gets the current transfer size for a uDMA channel control structure
//
//*****************************************************************************
uint32_t
uDMAChannelSizeGet(uint32_t ui32Base, uint32_t ui32ChannelStructIndex)
{
tDMAControlTable *pControlTable;
uint32_t ui32Control;
// Check the arguments.
ASSERT(uDMABaseValid(ui32Base));
ASSERT(ui32ChannelStructIndex < (UDMA_NUM_CHANNELS * 2));
ASSERT(HWREG(ui32Base + UDMA_O_CTRL) != 0);
// Get the base address of the control table.
pControlTable = (tDMAControlTable *)HWREG(ui32Base + UDMA_O_CTRL);
// Get the current control word value and mask off all but the size field
// and the mode field.
ui32Control = (pControlTable[ui32ChannelStructIndex].ui32Control &
(UDMA_XFER_SIZE_M | UDMA_MODE_M));
// If the size field and mode field are 0 then the transfer is finished
// and there are no more items to transfer.
if(ui32Control == 0)
{
return(0);
}
// Otherwise, if either the size field or more field is non-zero, then
// not all the items have been transferred.
else
{
// Shift the size field and add one, then return to user.
return((ui32Control >> UDMA_XFER_SIZE_S) + 1);
}
}
//*****************************************************************************
//
// Gets the transfer mode for a uDMA channel control structure
//
//*****************************************************************************
uint32_t
uDMAChannelModeGet(uint32_t ui32Base, uint32_t ui32ChannelStructIndex)
{
tDMAControlTable *pControlTable;
uint32_t ui32Control;
// Check the arguments.
ASSERT(uDMABaseValid(ui32Base));
ASSERT(ui32ChannelStructIndex < (UDMA_NUM_CHANNELS * 2));
ASSERT(HWREG(ui32Base + UDMA_O_CTRL) != 0);
// Get the base address of the control table.
pControlTable = (tDMAControlTable *)HWREG(ui32Base + UDMA_O_CTRL);
// Get the current control word value and mask off all but the mode field.
ui32Control = (pControlTable[ui32ChannelStructIndex].ui32Control &
UDMA_MODE_M);
// Check if scatter/gather mode, and if so, mask off the alt bit.
if(((ui32Control & ~UDMA_MODE_ALT_SELECT) == UDMA_MODE_MEM_SCATTER_GATHER) ||
((ui32Control & ~UDMA_MODE_ALT_SELECT) == UDMA_MODE_PER_SCATTER_GATHER))
{
ui32Control &= ~UDMA_MODE_ALT_SELECT;
}
// Return the mode to the caller.
return(ui32Control);
}