-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAD9833.cpp
530 lines (421 loc) · 11.9 KB
/
AD9833.cpp
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//
// FILE: AD9833.cpp
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for AD9833 function generator
// DATE: 2023-08-25
// VERSION: 0.4.3
// URL: https://github.com/RobTillaart/AD9833
#include "AD9833.h"
// FREQUENCY REGISTER BITS
#define AD9833_FREG1 0x8000
#define AD9833_FREG0 0x4000
// CONTROL REGISTER BITS
#define AD9833_B28 (1 << 13)
#define AD9833_HLB (1 << 12)
#define AD9833_FSELECT (1 << 11)
#define AD9833_PSELECT (1 << 10)
#define AD9833_RESET (1 << 8)
#define AD9833_SLEEP1 (1 << 7)
#define AD9833_SLEEP12 (1 << 6)
#define AD9833_OPBITEN (1 << 5)
#define AD9833_DIV2 (1 << 3)
#define AD9833_MODE (1 << 1)
// HARDWARE SPI
AD9833::AD9833(uint8_t slaveSelect, __SPI_CLASS__ * mySPI)
{
_selectPin = slaveSelect;
_hwSPI = true;
_mySPI = mySPI;
}
// SOFTWARE SPI
AD9833::AD9833(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock)
{
_selectPin = slaveSelect;
_dataPin = spiData;
_clockPin = spiClock;
_hwSPI = false;
_mySPI = NULL;
}
void AD9833::begin()
{
_useSelect = (_selectPin != 255);
if (_useSelect)
{
pinMode(_selectPin, OUTPUT);
digitalWrite(_selectPin, HIGH);
}
_spi_settings = SPISettings(8000000, MSBFIRST, SPI_MODE2);
if (_hwSPI)
{
// _mySPI->end();
// _mySPI->begin();
}
else // SOFTWARE SPI
{
pinMode(_dataPin, OUTPUT);
pinMode(_clockPin, OUTPUT);
digitalWrite(_dataPin, LOW);
digitalWrite(_clockPin, HIGH);
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
uint8_t _port = digitalPinToPort(_dataPin);
_dataOutRegister = portOutputRegister(_port);
_dataOutBit = digitalPinToBitMask(_dataPin);
_port = digitalPinToPort(_clockPin);
_clockRegister = portOutputRegister(_port);
_clockBit = digitalPinToBitMask(_clockPin);
#endif
}
reset();
}
void AD9833::reset()
{
hardwareReset();
_control = AD9833_B28; // implicit select sine wave.
writeControlRegister(_control);
}
void AD9833::hardwareReset()
{
writeControlRegister(_control | AD9833_RESET);
// reset all library variables to be in "sync" with hardware.
_control = 0;
_waveform = AD9833_OFF;
_freq[0] = _freq[1] = 0;
_phase[0] = _phase[1] = 0;
}
bool AD9833::setPowerMode(uint8_t mode)
{
if (mode > 3) return false;
// clear previous power bits
_control &= ~(AD9833_SLEEP1 | AD9833_SLEEP12);
_control |= (mode << 6); // set the new power bits
writeControlRegister(_control);
return true;
}
uint8_t AD9833::getPowerMode()
{
return (_control & (AD9833_SLEEP1 | AD9833_SLEEP12)) >> 6;
}
void AD9833::setWave(uint8_t waveform)
{
if (waveform > AD9833_TRIANGLE) return;
// store waveform
_waveform = waveform;
// clear bits in control register
_control &= ~(AD9833_SLEEP1 | AD9833_SLEEP12 | AD9833_OPBITEN | AD9833_MODE | AD9833_DIV2);
// set bits in control register
switch(_waveform)
{
case AD9833_OFF:
_control |= (AD9833_SLEEP1 | AD9833_SLEEP12);
break;
case AD9833_SINE:
// no bits need to set
break;
case AD9833_SQUARE1:
_control |= (AD9833_DIV2 | AD9833_OPBITEN);
break;
case AD9833_SQUARE2:
_control |= (AD9833_OPBITEN);
break;
case AD9833_TRIANGLE:
_control |= (AD9833_MODE);
break;
}
writeControlRegister(_control);
}
uint8_t AD9833::getWave()
{
return _waveform;
}
void AD9833::setUseRounding(bool flag)
{
_useRounding = flag;
}
bool AD9833::getUseRounding()
{
return _useRounding;
}
float AD9833::setFrequency(float frequency, uint8_t channel)
{
if (channel > 1) return -1;
// if (_freq[channel] == frequency) return frequency;
// local variable is faster.
float newFrequency = frequency;
if (newFrequency < 0) newFrequency = 0;
else if (newFrequency > AD9833_MAX_FREQ) newFrequency = AD9833_MAX_FREQ;
// convert to bit pattern
// freq = round(frequency * pow(2, 28) / 25 MHz)); // 25 MHz == crystal frequency.
// _crystalFreqFactor == (pow(2, 28) / crystal frequency);
// round() to minimize error / use the whole range
// however round() may cause drift with harmonics => see #19
uint32_t freq = 0;
if (_useRounding) freq = round(newFrequency * _crystalFreqFactor);
else freq = uint32_t(newFrequency * _crystalFreqFactor);
writeFrequencyRegister(channel, freq);
// cache the newFrequency;
_freq[channel] = newFrequency;
return newFrequency;
}
float AD9833::getFrequency(uint8_t channel)
{
// return round(_freq[channel] * _crystalFreqFactor) / _crystalFreqFactor;
return _freq[channel];
}
float AD9833::getMaxFrequency()
{
return AD9833_MAX_FREQ;
}
void AD9833::setFrequencyChannel(uint8_t channel)
{
if (channel > 1) return;
if (channel == 0) _control &= ~AD9833_FSELECT;
if (channel == 1) _control |= AD9833_FSELECT;
writeControlRegister(_control);
}
float AD9833::setPhase(float phase, uint8_t channel)
{
if (channel > 1) return -1;
// local variable is faster.
float newPhase = phase;
// get phase within normalized range.
while (newPhase >= AD9833_MAX_PHASE) newPhase -= AD9833_MAX_PHASE;
while (newPhase < 0) newPhase += AD9833_MAX_PHASE;
// round() to minimize error / use the whole range 0..4095
// however round() may cause drift with harmonics => see #19
uint16_t phs = 0;
if (_useRounding) phs = round(newPhase * (4095.0 / 360.0));
else phs = uint32_t(newPhase * (4095.0 / 360.0));
writePhaseRegister(channel, phs);
// cache the newPhase
_phase[channel] = newPhase;
return newPhase;
}
float AD9833::getPhase(uint8_t channel)
{
// more precise => more math;
// return round(_phase[channel] * (4095.0 / 360.0)) / (4095.0 / 360.0);
return _phase[channel];
}
// returns phase set (radians) - not optimized.
// [0 .. 2 PI>
float AD9833::setPhaseRadians(float phase, uint8_t channel)
{
return setPhase(phase * RAD_TO_DEG, channel) * DEG_TO_RAD;
}
float AD9833::getPhaseRadians(uint8_t channel)
{
return getPhase(channel) * DEG_TO_RAD;
}
float AD9833::getMaxPhase()
{
return AD9833_MAX_PHASE;
}
void AD9833::setPhaseChannel(uint8_t channel)
{
if (channel > 1) return;
if (channel == 0) _control &= ~AD9833_PSELECT;
if (channel == 1) _control |= AD9833_PSELECT;
writeControlRegister(_control);
}
void AD9833::setSPIspeed(uint32_t speed)
{
_SPIspeed = speed;
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE2);
}
uint32_t AD9833::getSPIspeed()
{
return _SPIspeed;
}
bool AD9833::usesHWSPI()
{
return _hwSPI;
}
////////////////////////////////////////////////////////////////
//
// LOW LEVEL API - Expert users only
//
void AD9833::writeControlRegister(uint16_t value)
{
uint16_t data = value & 0x3FFF; // bit 15 and 14 == 00
writeData(data);
}
void AD9833:: writeFrequencyRegister(uint8_t channel, uint32_t freq)
{
uint16_t LSB = 0;
uint16_t MSB = 0;
if (channel > 1) return;
if (channel == 0) LSB = AD9833_FREG0; // bit 15 and 14 01
if (channel == 1) LSB = AD9833_FREG1; // bit 15 and 14 10
// copy channel mask.
MSB = LSB;
// be sure B28 bit is set.
_control |= AD9833_B28;
writeControlRegister(_control);
// 28 bits in two sets of 14
LSB |= (freq & 0x3FFF);
MSB |= ((freq >> 14) & 0x3FFF);
// faster to write them in one SPI transaction
writeData28(LSB, MSB);
// first send the least significant 14 bits
// writeData(LSB);
// then send the most significant 14 bits
// writeData(MSB);
}
void AD9833::writePhaseRegister(uint8_t channel, uint16_t value)
{
uint16_t data = 0;
if (channel > 1) return;
if (channel == 0) data = 0xC000; // bit 15 and 14 and 13 110
if (channel == 1) data = 0xE000; // bit 15 and 14 and 13 111
data |= (value & 0x0FFF);
writeData(data);
}
void AD9833::setCrystalFrequency(float crystalFrequency)
{
// calculate the often used factor
// 268435456.0 == POW2TO28
_crystalFreqFactor = 268435456.0 / crystalFrequency;
}
float AD9833::getCrystalFrequency()
{
// 268435456.0 == POW2TO28
return 268435456.0 / _crystalFreqFactor;
}
///////////////////////////////////////////////////////////////////
//
// EXPERIMENTAL
//
void AD9833::writeFrequencyRegisterLSB(uint8_t channel, uint16_t LSB)
{
if (channel > 1) return;
// force 14 bit
LSB &= 0x3FFF;
if (channel == 0) LSB |= AD9833_FREG0; // bit 15 and 14 01
if (channel == 1) LSB |= AD9833_FREG1; // bit 15 and 14 10
// be sure B28 and HLB bit is cleared.
_control &= ~AD9833_B28;
_control &= ~AD9833_HLB;
writeControlRegister(_control);
// send the least significant 14 bits
writeData(LSB);
}
void AD9833::writeFrequencyRegisterMSB(uint8_t channel, uint16_t MSB)
{
if (channel > 1) return;
// force 14 bit
MSB &= 0x3FFF;
if (channel == 0) MSB |= AD9833_FREG0; // bit 15 and 14 01
if (channel == 1) MSB |= AD9833_FREG1; // bit 15 and 14 10
// be sure B28 is cleared and HLB bit is set.
_control &= ~AD9833_B28;
_control |= AD9833_HLB;
writeControlRegister(_control);
// send the most significant 14 bits
writeData(MSB);
}
///////////////////////////////////////////////////////////////////
//
// PRIVATE
//
void AD9833::writeData(uint16_t data)
{
if (_useSelect) digitalWrite(_selectPin, LOW);
if (_hwSPI)
{
_mySPI->beginTransaction(_spi_settings);
_mySPI->transfer16(data);
_mySPI->endTransaction();
}
else
{
// SPI MODE2
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
uint8_t cbmask1 = _clockBit;
uint8_t cbmask2 = ~_clockBit;
uint8_t outmask1 = _dataOutBit;
uint8_t outmask2 = ~_dataOutBit;
// MSBFIRST
for (uint16_t mask = 0x8000; mask; mask >>= 1)
{
uint8_t oldSREG = SREG;
noInterrupts();
if (data & mask) *_dataOutRegister |= outmask1;
else *_dataOutRegister &= outmask2;
*_clockRegister &= cbmask2;
*_clockRegister |= cbmask1;
SREG = oldSREG;
}
#else // REFERENCE IMPLEMENTATION
// local variables is fast.
uint8_t clk = _clockPin;
uint8_t dao = _dataPin;
// MSBFIRST
for (uint16_t mask = 0x8000; mask; mask >>= 1)
{
digitalWrite(dao, (data & mask) != 0 ? HIGH : LOW);
digitalWrite(clk, LOW);
digitalWrite(clk, HIGH);
}
#endif
}
if (_useSelect) digitalWrite(_selectPin, HIGH);
}
void AD9833::writeData28(uint16_t LSB, uint16_t MSB)
{
if (_useSelect) digitalWrite(_selectPin, LOW);
if (_hwSPI)
{
_mySPI->beginTransaction(_spi_settings);
_mySPI->transfer16(LSB);
_mySPI->transfer16(MSB);
_mySPI->endTransaction();
}
else
{
// SPI MODE2
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
uint8_t cbmask1 = _clockBit;
uint8_t cbmask2 = ~_clockBit;
uint8_t outmask1 = _dataOutBit;
uint8_t outmask2 = ~_dataOutBit;
for (uint16_t mask = 0x8000; mask; mask >>= 1)
{
uint8_t oldSREG = SREG;
noInterrupts();
if (LSB & mask) *_dataOutRegister |= outmask1;
else *_dataOutRegister &= outmask2;
*_clockRegister &= cbmask2;
*_clockRegister |= cbmask1;
SREG = oldSREG;
}
for (uint16_t mask = 0x8000; mask; mask >>= 1)
{
uint8_t oldSREG = SREG;
noInterrupts();
if (MSB & mask) *_dataOutRegister |= outmask1;
else *_dataOutRegister &= outmask2;
*_clockRegister &= cbmask2;
*_clockRegister |= cbmask1;
SREG = oldSREG;
}
#else // REFERENCE IMPLEMENTATION
// local variables is fast.
uint8_t clk = _clockPin;
uint8_t dao = _dataPin;
for (uint16_t mask = 0x8000; mask; mask >>= 1)
{
digitalWrite(dao, (LSB & mask) != 0 ? HIGH : LOW);
digitalWrite(clk, LOW);
digitalWrite(clk, HIGH);
}
for (uint16_t mask = 0x8000; mask; mask >>= 1)
{
digitalWrite(dao, (MSB & mask) != 0 ? HIGH : LOW);
digitalWrite(clk, LOW);
digitalWrite(clk, HIGH);
}
#endif
}
if (_useSelect) digitalWrite(_selectPin, HIGH);
}
// -- END OF FILE --