-
Notifications
You must be signed in to change notification settings - Fork 2
/
dma.c
51 lines (39 loc) · 1.52 KB
/
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
#include <stdint.h>
#include <stddef.h>
#include "dma.h"
#define DMA_BASE 0x20007000
#define DMA_ENABLE 0x20007FF0
#define DMA_CS_OFFSET 0x00
#define DMA_CONBLK_AD_OFFSET 0x01
#define DMA_PTR(channel, offset) (((volatile uint32_t*)DMA_BASE) + ((channel) << 6) + (offset))
struct DMAControlBlock
{
uint32_t transferInfo;
uint32_t srcAddress;
uint32_t dstAddress;
uint32_t transferLen;
uint32_t stride;
struct DMAControlBlock *next;
};
static void DMALaunchRequest(uint8_t channel, const struct DMAControlBlock *controlBlock)
{
*(uint32_t*)DMA_ENABLE = 1; // Enable DMA channel 0
// Wait for channel being inactive
while((*DMA_PTR(channel, DMA_CS_OFFSET)) & 1);
*DMA_PTR(channel, DMA_CONBLK_AD_OFFSET) = 0x40000000 | (uint32_t) controlBlock;
*DMA_PTR(channel, DMA_CS_OFFSET) = 7; // Enables DMA transfer
}
void DMACopy32(void *dst, void *src, size_t sizeBytes)
{
static struct DMAControlBlock __attribute__((aligned(0x100))) controlBlock;
controlBlock.transferInfo = (0 << 9) // 32 bit source read
| (1 << 8) // Increment source
| (0 << 5) // 32 bit destination write
| (1 << 4); // Increment destination
controlBlock.srcAddress = 0x40000000 | (uint32_t) src;
controlBlock.dstAddress = 0x40000000 | (uint32_t) dst;
controlBlock.transferLen = sizeBytes;
controlBlock.stride = 0;
controlBlock.next = 0; // No chained operation
DMALaunchRequest(0, &controlBlock);
}