forked from Xilinx/PYNQ
/
pmod_dac.c
executable file
·404 lines (369 loc) · 12.8 KB
/
pmod_dac.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
/******************************************************************************
* Copyright (c) 2016, Xilinx, Inc.
* SPDX-License-Identifier: BSD-3-Clause
*
*****************************************************************************/
/******************************************************************************
*
*
* @file pmod_dac.c
*
* IOP code (MicroBlaze) for Pmod DA4
* Pmod DA4 is write only, and has SPI interface.
* Switch configuration is done within this program, Pmod can be plugged
* into upper row or lower row of the connector.
* The Pmod DA4 is an 8 channel 12-bit digital-to-analog converter run via
* the analog devices AD5628.
* http://store.digilentinc.com/pmodda4-eight-12-bit-d-a-outputs/
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver Who Date Changes
* ----- --- ------- -----------------------------------------------
* 1.00a cmc 03/29/16 release
* 1.00b pp 05/27/16 fix pmod_init()
*
* </pre>
*
*****************************************************************************/
#include "xparameters.h"
#include "circular_buffer.h"
#include "spi.h"
#include "timer.h"
static spi device;
/*
* Passed parameters in MAILBOX_WRITE_CMD
* bits 31:20 => delay in microsecond if wave generation mode is selected
* bits 31:20 => 12-bit value to be output if fixed value mode is selected
* bits 19:16 => 4-bit value indicates channels to be used
* bits 15:8 => number of cycles to be produced in wave generation mode,
* if it is set to 0, then generate waveforms forever
* bit 7 => not used
* bit 6 => random wave generation - pattern is written in MAILBOX_DATA area
* with MAILBOX_DATA(0) having number of samples per cycle, followed
* by 12-bit values in each word
* bit 5 => Sine wave generation mode
* bit 4 => Triangle waveform generation mode
* bit 3 => Sawtooth generation mode
* bit 2 => Square wave generation mode
* bit 1 => Fixed Value mode
* bit 0 => 1 command issued, 0 command completed
*
* Channels table
* bits [19:16]
* 0000 => Channel A
* 0001 => Channel B
* 0010 => Channel C
* 0011 => Channel D
* 0100 => Channel E
* 0101 => Channel F
* 0110 => Channel G
* 1111 => All channels
*/
#define BUFFER_SIZE 6
typedef u8 DataBuffer[BUFFER_SIZE];
u8 WriteBuffer[BUFFER_SIZE];
static int sinetable[256] = {
0x80, 0x83, 0x86, 0x89, 0x8C, 0x90, 0x93, 0x96,
0x99, 0x9C, 0x9F, 0xA2, 0xA5, 0xA8, 0xAB, 0xAE,
0xB1, 0xB3, 0xB6, 0xB9, 0xBC, 0xBF, 0xC1, 0xC4,
0xC7, 0xC9, 0xCC, 0xCE, 0xD1, 0xD3, 0xD5, 0xD8,
0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE8,
0xEA,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0x26, 0x28, 0x2B, 0x2D, 0x2F, 0x32, 0x34, 0x37,
0x39, 0x3C, 0x3F, 0x41, 0x44, 0x47, 0x4A, 0x4D,
0x4F, 0x52, 0x55, 0x58, 0x5B, 0x5E, 0x61, 0x64,
0x67, 0x6A, 0x6D, 0x70, 0x74, 0x77, 0x7A, 0x7D
};
void RefOn(void) {
// Turn ON internal reference voltage
WriteBuffer[3]=0x01;
// This byte must be 0
WriteBuffer[2]=0x00;
// channel number (0) | least significant 4-bits data
WriteBuffer[1]=0x00;
// write to and update requested channel
WriteBuffer[0]=0x08;
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
}
void RefOFF(void) {
// Turn OFF internal reference voltage
WriteBuffer[3]=0x00;
// This byte must be 0
WriteBuffer[2]=0x00;
// channel number (0) | least significant 4-bits data
WriteBuffer[1]=0x00;
// write to and update requested channel
WriteBuffer[0]=0x08;
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
}
void FixedGen(u8 channels, u16 fixedvalue) {
// 0x55 - this is the dummy data for 12-bit dac
WriteBuffer[3] = 0x55;
// least significant 8-bits data
WriteBuffer[2] = fixedvalue & 0xff;
// channel number (0) | most significant 4-bits data
WriteBuffer[1] = (channels << 4) | (fixedvalue >> 8);
// 4 most significant bits don't care | write and update DAC command
WriteBuffer[0] = 0x03;
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
}
void SqWaveGen(u8 channels, u8 numofcycles, u16 delay) {
int i, j;
WriteBuffer[3] = 0x55;
WriteBuffer[0] = 0x03;
if(numofcycles==0){
for(;;){
WriteBuffer[2] = 0xff;
WriteBuffer[1] = (channels << 4) | 0x0f;
for(j=0; j< 4096; j++) {
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay>2)
delay_us(delay/2);
}
WriteBuffer[2] = 0x00;
WriteBuffer[1] = (channels << 4) | 0x00;
for(j=0; j< 4096; j++) {
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay>2)
delay_us(delay/2);
}
}
}
else {
for(i=0; i<numofcycles ; i++) {
WriteBuffer[2] = 0xff;
WriteBuffer[1] = (channels << 4) | 0x0f;
for(j=0; j< 4096; j++) {
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay>2)
delay_us(delay/2);
}
WriteBuffer[2] = 0x00;
WriteBuffer[1] = (channels << 4) | 0x00;
for(j=0; j< 4096; j++) {
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay>2)
delay_us(delay/2);
}
}
}
}
void SawToothWaveGen(u8 channels, u8 numofcycles, u16 delay) {
int i, j;
WriteBuffer[3] = 0x55;
WriteBuffer[0] = 0x03;
if(numofcycles==0){
for(;;){
for(j=0; j< 4096; j++) {
WriteBuffer[2] = j & 0xff;
WriteBuffer[1] = (channels << 4) | ((j >> 8 ) & 0x0f);
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay)
delay_us(delay);
}
WriteBuffer[2] = 0x00;
WriteBuffer[1] = (channels << 4) | 0x00;
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay)
delay_us(delay);
}
}
else {
for(i=0; i<numofcycles ; i++) {
for(j=0; j< 4096; j++) {
WriteBuffer[2] = j & 0xff;
WriteBuffer[1] = (channels << 4) | ((j >> 8 ) & 0x0f);
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay)
delay_us(delay);
}
WriteBuffer[2] = 0x00;
WriteBuffer[1] = (channels << 4) | 0x00;
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay)
delay_us(delay);
}
}
}
void TriangleWaveGen(u8 channels, u8 numofcycles, u16 delay) {
int i, j;
WriteBuffer[3] = 0x55;
WriteBuffer[0] = 0x03;
if(numofcycles==0){
for(;;){
for(j=0; j< 4096; j++) {
WriteBuffer[2] = j & 0xff;
WriteBuffer[1] = (channels << 4) | ((j >> 8 ) & 0x0f);
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay)
delay_us(delay);
}
for(j=4095; j>=0; j--) {
WriteBuffer[2] = j & 0xff;
WriteBuffer[1] = (channels << 4) | ((j >> 8 ) & 0x0f);
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay)
delay_us(delay);
}
}
}
else {
for(i=0; i<numofcycles ; i++) {
for(j=0; j< 4096; j++) {
WriteBuffer[2] = j & 0xff;
WriteBuffer[1] = (channels << 4) | ((j >> 8 ) & 0x0f);
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay)
delay_us(delay);
}
for(j=4095; j>=0; j--) {
WriteBuffer[2] = j & 0xff;
WriteBuffer[1] = (channels << 4) | ((j >> 8 ) & 0x0f);
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay)
delay_us(delay);
}
}
}
}
void SineWaveGen(u8 channels, u8 numofcycles, u16 delay) {
int i, j;
int num;
WriteBuffer[3] = 0x55;
WriteBuffer[0] = 0x03;
if(numofcycles==0){
for(;;){
for(j=0; j< 4096; j++) {
num = (sinetable[j%256] << 4);
WriteBuffer[2] = num & 0xff;
WriteBuffer[1] = (channels << 4) | ((num >> 8 ) & 0x0f);
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay)
delay_us(delay);
}
}
}
else {
for(i=0; i<numofcycles ; i++) {
for(j=0; j< 4096; j++) {
num = (sinetable[j%256] << 4);
WriteBuffer[2] = num & 0xff;
WriteBuffer[1] = (channels << 4) | ((num >> 8 ) & 0x0f);
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay)
delay_us(delay);
}
}
}
}
void RandomWaveGen(u8 channels, u8 numofcycles, u16 delay) {
int i, j;
int num, numofsamples;
WriteBuffer[3] = 0x55;
WriteBuffer[0] = 0x03;
numofsamples=MAILBOX_DATA(0);
if(numofcycles==0){
for(;;){
for(j=0; j< numofsamples; j++) {
num = MAILBOX_DATA((j%256)+1);
WriteBuffer[2] = num & 0xff;
WriteBuffer[1] = (channels << 4) | ((num >> 8 ) & 0x0f);
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay)
delay_us(delay);
}
}
}
else {
for(i=0; i<numofcycles ; i++) {
for(j=0; j< numofsamples; j++) {
num = MAILBOX_DATA((j%256)+1);
WriteBuffer[2] = num & 0xff;
WriteBuffer[1] = (channels << 4) | ((num >> 8 ) & 0x0f);
spi_transfer(device, (char*)WriteBuffer, NULL, 4);
if(delay)
delay_us(delay);
}
}
}
}
void dac(u8 channels, u8 mode, u16 fixedvalue, u8 numofcycles, u16 delay)
{
switch(mode) {
case 1:FixedGen(channels,fixedvalue); break;
case 2:SqWaveGen(channels, numofcycles, delay); break;
case 3:SawToothWaveGen(channels, numofcycles, delay); break;
case 4:TriangleWaveGen(channels, numofcycles, delay); break;
case 5:SineWaveGen(channels, numofcycles, delay); break;
case 6:RandomWaveGen(channels, numofcycles, delay); break;
}
}
int main(void)
{
u16 delay;
u32 cmd;
u8 numofcycles;
u16 fixedvalue;
u8 mode, channels;
device = spi_open(3, 2, 1, 0);
device = spi_configure(device, 0, 1);
RefOn();
while(1){
while((MAILBOX_CMD_ADDR & 0x01)==0);
cmd = MAILBOX_CMD_ADDR;
mode=0;
if((cmd >> 1) & 0x01)
mode=1;
else if((cmd >> 2) & 0x01)
mode=2;
else if((cmd >> 3) & 0x01)
mode=3;
else if((cmd >> 4) & 0x01)
mode=4;
else if((cmd >> 5) & 0x01)
mode=5;
else if((cmd >> 6) & 0x01)
mode=6;
channels = (cmd >> 16) & 0x0f;
fixedvalue = (cmd >> 20) & 0x0fff;
if(((cmd >> 20) & 0x0fff)==0)
// set to 1 second if the field is set to 0
delay = 1000;
else
// multiple of approximate milliseconds
delay=(cmd >> 20) & 0xfff;
if(((cmd >> 8) & 0x000ff)==0)
// indicate infinite number of samples
numofcycles = 0;
else
// set to number of samples
numofcycles=(cmd >> 8) & 0x0ff;
dac(channels,mode,fixedvalue,numofcycles,delay);
MAILBOX_CMD_ADDR = 0x0;
}
return 0;
}