/
DMASpi_example1.ino
254 lines (219 loc) · 6.74 KB
/
DMASpi_example1.ino
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
//#include <WProgram.h>
#include <SPI.h>
#include <DmaSpi.h>
/** Important
The sketch waits for user input through Serial (USB) before it starts the setup.
After any key was pressed, it should begin to work. It will ask for a second keypress later.
This example cannot simply be modified to use SPI1 or SPI2 instead of SPI0 because ActiveLowChipSelect is
hardcoded to use SPI (SPI0).
If you want to use SPI1: see DMASpi_example2.
If you want to use SPI2: adapt example and create a new chip select class.
**/
/** Hardware setup:
Teensy 3.1, 3.2: DOUT (11) connected to DIN (12)
Teensy LC: DOUT (11) connected to DIN (12)
Teensy 3.5, 3.6: DOUT (11) connected to DIN (12)
Pin 0 is used as a chip select pin, don't connect anything there.
**/
/** buffers to send from and to receive to **/
#define DMASIZE 100
uint8_t src[DMASIZE];
volatile uint8_t dest[DMASIZE];
volatile uint8_t dest1[DMASIZE];
/** Wait for and consume a keypress over USB **/
void waitForKeyPress()
{
Serial.println("\nPress a key to continue\n");
while(!Serial.available());
while(Serial.available())
{
Serial.read();
}
}
void dumpBuffer(const volatile uint8_t* buf, const char* prefix)
{
Serial.print(prefix);
for (size_t i = 0; i < DMASIZE; i++)
{
Serial.printf("0x%02x ", buf[i]);
}
Serial.print('\n');
}
/** Compare the buffers and print the destination contents if there's a mismatch **/
void compareBuffers(const uint8_t* src_, const uint8_t* dest_)
{
int n = memcmp((const void*)src_, (const void*)dest_, DMASIZE);
if (n == 0)
{
Serial.println("src and dest match");
}
else
{
Serial.println("src and dest don't match");
dumpBuffer(src_, " src: " );
dumpBuffer(dest_, "dest: ");
}
}
void setSrc()
{
for (size_t i = 0; i < DMASIZE; i++)
{
src[i] = i;
}
}
void clrDest(uint8_t* dest_)
{
memset((void*)dest_, 0x00, DMASIZE);
}
void setup()
{
waitForKeyPress();
Serial.println("Hi!");
/** Prepare source and destination **/
setSrc();
clrDest((uint8_t*)dest);
Serial.println("Buffers are prepared");Serial.flush();
/** set up SPI **/
SPISettings spiSettings;
SPI.begin();
// transmit 10 bytes and measure time to get a feel of how long that takes
SPI.beginTransaction(spiSettings);
elapsedMicros us;
for (size_t i = 0; i < DMASIZE; i++)
{
dest[i] = SPI.transfer(src[i]);
}
uint32_t t = us;
Serial.print("Time for non-DMA transfer: ");Serial.print(t);Serial.println("us");
SPI.endTransaction();
compareBuffers(src, (const uint8_t*)dest);
waitForKeyPress();
DMASPI0.begin();
DMASPI0.start();
DmaSpi::Transfer trx(nullptr, 0, nullptr);
Serial.println("Testing src -> dest, single transfer");
Serial.println("--------------------------------------------------");
trx = DmaSpi::Transfer(src, DMASIZE, dest);
clrDest((uint8_t*)dest);
DMASPI0.registerTransfer(trx);
while(trx.busy())
{
}
Serial.println("Finished DMA transfer");
compareBuffers(src, (const uint8_t*)dest);
Serial.println("==================================================\n\n");
Serial.println("Testing src -> discard, single transfer");
Serial.println("--------------------------------------------------");
trx = DmaSpi::Transfer(src, DMASIZE, nullptr);
DMASPI0.registerTransfer(trx);
while(trx.busy())
{
}
Serial.println("Finished DMA transfer");
Serial.printf("last discarded value is 0x%02x\n", DMASPI0.devNull());
if (DMASPI0.devNull() == src[DMASIZE-1])
{
Serial.println("That appears to be correct");
}
else
{
Serial.printf("That appears to be wrong, it should be src[DMASIZE-1] which is 0x%02x\n", src[DMASIZE-1]);
}
Serial.println("==================================================\n\n");
Serial.println("Testing 0xFF dummy data -> dest, single transfer");
Serial.println("--------------------------------------------------");
trx = DmaSpi::Transfer(nullptr, DMASIZE, dest, 0xFF);
memset((void*)src, 0xFF, DMASIZE); // we need this for checking the dest buffer
clrDest((uint8_t*)dest);
DMASPI0.registerTransfer(trx);
while(trx.busy())
{
}
Serial.println("Finished DMA transfer");
compareBuffers(src, (const uint8_t*)dest);
Serial.println("==================================================\n\n");
Serial.println("Testing multiple queued transfers");
Serial.println("--------------------------------------------------");
trx = DmaSpi::Transfer(src, DMASIZE, dest, 0xFF);
setSrc();
clrDest((uint8_t*)dest);
clrDest((uint8_t*)dest1);
DmaSpi::Transfer trx1(src, DMASIZE, dest1);
DMASPI0.registerTransfer(trx);
DMASPI0.registerTransfer(trx1);
while(trx.busy());
Serial.println("Finished DMA transfer");
while(trx1.busy());
Serial.println("Finished DMA transfer1");
compareBuffers(src, (const uint8_t*)dest);
compareBuffers(src, (const uint8_t*)dest1);
Serial.println("==================================================\n\n");
Serial.println("Testing pause and restart");
Serial.println("--------------------------------------------------");
clrDest((uint8_t*)dest);
clrDest((uint8_t*)dest1);
DMASPI0.registerTransfer(trx);
DMASPI0.registerTransfer(trx1);
DMASPI0.stop();
us = elapsedMicros();
while(!DMASPI0.stopped());
t = us;
while(trx.busy());
Serial.printf("Time until stopped: %lu us\n", t);
Serial.println("Finished DMA transfer");
if (DMASPI0.stopped())
{
Serial.println("DMA SPI appears to have stopped (this is good)\nrestarting");
}
else
{
Serial.println("DMA SPI does not report stopped state, but it should. (this is bad)");
}
DMASPI0.start();
while(trx1.busy());
Serial.println("Finished DMA transfer1");
compareBuffers(src, (const uint8_t*)dest);
compareBuffers(src, (const uint8_t*)dest1);
Serial.println("==================================================\n\n");
Serial.println("Testing src -> dest, with chip select object");
Serial.println("--------------------------------------------------");
ActiveLowChipSelect cs(0, SPISettings());
// DebugChipSelect cs;
trx = DmaSpi::Transfer(src, DMASIZE, dest, 0, &cs);
clrDest((uint8_t*)dest);
DMASPI0.registerTransfer(trx);
while(trx.busy())
{
}
Serial.println("Finished DMA transfer");
compareBuffers(src, (const uint8_t*)dest);
Serial.println("==================================================\n\n");
DmaSpi::Transfer(src, DMASIZE, dest, 0, &cs);
if (DMASPI0.stopped())
{
Serial.println("DMA SPI stopped.");
}
else
{
Serial.println("DMA SPI is still running");
}
DMASPI0.stop();
if (DMASPI0.stopped())
{
Serial.println("DMA SPI stopped.");
}
else
{
Serial.println("DMA SPI is still running");
}
DMASPI0.end();
SPI.end();
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
digitalWriteFast(LED_BUILTIN, true);
delay(500);
digitalWriteFast(LED_BUILTIN, false);
delay(500);
}