-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmem2mem.pde
151 lines (137 loc) · 4.05 KB
/
mem2mem.pde
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
/**********************************************************************
teensy only 16KB RAM
use DMA for memcpy memset ch 21 pg 387
thanks to Paul
new paul addition memcpy32p()
*/
#define BYTES 4096
uint8_t src[BYTES] __attribute__ ((aligned (16)));
uint8_t dst[BYTES] __attribute__ ((aligned (16)));
//int32_t DMAMEM _dma_Buffer_A[WORDS];
//int32_t DMAMEM _dma_Buffer_B[WORDS];
void prmbs(char *lbl,unsigned long us,int bytes) {
float mbs = 8.*bytes/us;
Serial.print(mbs,2); Serial.print(" mbs ");
Serial.print(us); Serial.print(" us ");
Serial.println(lbl);
}
void memcpy32(void *dest, const void *src, unsigned int bytes)
{
DMA_TCD1_SADDR = src;
DMA_TCD1_SOFF = 4;
DMA_TCD1_ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2); //32bit
DMA_TCD1_NBYTES_MLNO = bytes;
DMA_TCD1_SLAST = 0;
DMA_TCD1_DADDR = dest;
DMA_TCD1_DOFF = 4;
DMA_TCD1_CITER_ELINKNO = 1;
DMA_TCD1_DLASTSGA = 0;
DMA_TCD1_BITER_ELINKNO = 1;
DMA_TCD1_CSR = DMA_TCD_CSR_START;
while (!(DMA_TCD1_CSR & DMA_TCD_CSR_DONE)) /* wait */ ;
}
void memcpy32p(void *dest, const void *src, unsigned int bytes)
{
// buffer needs to be aligned 16 and multiple of 16
DMA_TCD1_SADDR = src;
DMA_TCD1_SOFF = 16;
DMA_TCD1_ATTR = DMA_TCD_ATTR_SSIZE(4) | DMA_TCD_ATTR_DSIZE(4);
DMA_TCD1_NBYTES_MLNO = bytes;
DMA_TCD1_SLAST = 0;
DMA_TCD1_DADDR = dest;
DMA_TCD1_DOFF = 16;
DMA_TCD1_CITER_ELINKNO = 1;
DMA_TCD1_DLASTSGA = 0;
DMA_TCD1_BITER_ELINKNO = 1;
DMA_TCD1_CSR = DMA_TCD_CSR_START;
while (!(DMA_TCD1_CSR & DMA_TCD_CSR_DONE)) /* wait */ ;
}
void memset32(void *dest, int val, unsigned int bytes)
{
DMA_TCD1_SADDR = &val;
DMA_TCD1_SOFF = 0;
DMA_TCD1_ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
DMA_TCD1_NBYTES_MLNO = bytes;
DMA_TCD1_SLAST = 0;
DMA_TCD1_DADDR = dest;
DMA_TCD1_DOFF = 4;
DMA_TCD1_CITER_ELINKNO = 1;
DMA_TCD1_DLASTSGA = 0;
DMA_TCD1_BITER_ELINKNO = 1;
DMA_TCD1_CSR = DMA_TCD_CSR_START;
while (!(DMA_TCD1_CSR & DMA_TCD_CSR_DONE)) /* wait */ ;
}
void gpio2mem(void *dest, void * src, unsigned int bytes)
{
DMA_TCD1_SADDR = src;
DMA_TCD1_SOFF = 0;
DMA_TCD1_ATTR = DMA_TCD_ATTR_SSIZE(2) | DMA_TCD_ATTR_DSIZE(2);
DMA_TCD1_NBYTES_MLNO = bytes;
DMA_TCD1_SLAST = 0;
DMA_TCD1_DADDR = dest;
DMA_TCD1_DOFF = 4;
DMA_TCD1_CITER_ELINKNO = 1;
DMA_TCD1_DLASTSGA = 0;
DMA_TCD1_BITER_ELINKNO = 1;
DMA_TCD1_CSR = DMA_TCD_CSR_START;
while (!(DMA_TCD1_CSR & DMA_TCD_CSR_DONE)) /* wait */ ;
}
void setup(){
Serial.begin(9600);
while(!Serial);
}
void loop(){
int i;
unsigned long t1,t2;
for (i=0;i<BYTES;i++){
dst[i]=0;
src[i]=i;
}
memcpy32(dst,src,BYTES);
Serial.println(dst[3],DEC);
for (i=0;i<BYTES;i++){
dst[i]=0;
src[i]=i;
}
memcpy32p(dst,src,BYTES);
Serial.println(dst[3],DEC);
memset32(dst,45,BYTES);
Serial.println(dst[4],DEC);
t1=micros();
memcpy32(dst,src,BYTES);
t2 = micros() - t1;
prmbs("memcpy32",t2,BYTES);
t1=micros();
memcpy32p(dst,src,BYTES);
t2 = micros() - t1;
prmbs("memcpy32p",t2,BYTES);
t1=micros();
memset32(dst,66,BYTES);
t2 = micros() - t1;
prmbs("memset32",t2,BYTES);
t1=micros();
gpio2mem(dst,(void *)&GPIOA_PDIR,BYTES);
t2 = micros() - t1;
prmbs("gpio2mem",t2,BYTES);
t1=micros();
for(i=0;i<BYTES;i++) dst[i] = src[i];
t2 = micros() - t1;
prmbs("loop copy",t2,BYTES);
dst[3]=99;
t1=micros();
memcpy(dst,src,BYTES);
t2 = micros() - t1;
prmbs("memcpy",t2,BYTES);
Serial.println(dst[3],DEC);
t1=micros();
memset(dst,66,BYTES);
t2 = micros() - t1;
prmbs("memset",t2,BYTES);
Serial.println(dst[3],HEX);
t1=micros();
for(i=0;i<BYTES;i++) dst[i] = 66;
t2 = micros() - t1;
prmbs("set loop",t2,BYTES);
Serial.println();
delay(3000);
}