2021-01-29 06:31:58 -05:00
|
|
|
|
//
|
|
|
|
|
// FILE: AD985X.cpp
|
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// VERSION: 0.3.4
|
2021-01-29 06:31:58 -05:00
|
|
|
|
// DATE: 2019-02-08
|
|
|
|
|
// PURPOSE: Class for AD9850 and AD9851 function generator
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// URL: https://github.com/RobTillaart/AD985X
|
2021-01-29 06:31:58 -05:00
|
|
|
|
//
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// HISTORY: see changelog.md
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "AD985X.h"
|
|
|
|
|
|
|
|
|
|
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// UNO HARDWARE SPI PINS
|
|
|
|
|
#define SPI_CLOCK 13 // not portable...
|
2021-01-29 06:31:58 -05:00
|
|
|
|
#define SPI_MISO 12
|
|
|
|
|
#define SPI_MOSI 11
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define AD985X_POWERDOWN 0x04
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
//
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// AD9850
|
2021-01-29 06:31:58 -05:00
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
AD9850::AD9850()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-06-06 13:57:20 -04:00
|
|
|
|
void AD9850::begin(uint8_t select, uint8_t resetPin, uint8_t FQUDPin, uint8_t dataOut , uint8_t clock)
|
2021-01-29 06:31:58 -05:00
|
|
|
|
{
|
2021-08-25 03:52:59 -04:00
|
|
|
|
_select = select;
|
|
|
|
|
_reset = resetPin;
|
|
|
|
|
_fqud = FQUDPin;
|
|
|
|
|
_dataOut = dataOut;
|
|
|
|
|
_clock = clock;
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// following 3 are always set.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
pinMode(_select, OUTPUT);
|
|
|
|
|
pinMode(_reset, OUTPUT);
|
|
|
|
|
pinMode(_fqud, OUTPUT);
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// device select = HIGH See - https://github.com/RobTillaart/AD985X/issues/13
|
2021-08-25 03:52:59 -04:00
|
|
|
|
digitalWrite(_select, LOW);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
digitalWrite(_reset, LOW);
|
|
|
|
|
digitalWrite(_fqud, LOW);
|
|
|
|
|
|
2021-08-25 03:52:59 -04:00
|
|
|
|
_hwSPI = ((dataOut == 0) || (clock == 0));
|
|
|
|
|
|
|
|
|
|
_spi_settings = SPISettings(2000000, LSBFIRST, SPI_MODE0);
|
|
|
|
|
|
|
|
|
|
if (_hwSPI)
|
2021-01-29 06:31:58 -05:00
|
|
|
|
{
|
2021-08-25 03:52:59 -04:00
|
|
|
|
#if defined(ESP32)
|
2022-10-25 13:47:34 -04:00
|
|
|
|
if (_useHSPI) // HSPI
|
2021-08-25 03:52:59 -04:00
|
|
|
|
{
|
|
|
|
|
mySPI = new SPIClass(HSPI);
|
|
|
|
|
mySPI->end();
|
2022-10-25 13:47:34 -04:00
|
|
|
|
mySPI->begin(14, 12, 13, select); // CLK=14 MISO=12 MOSI=13
|
2021-08-25 03:52:59 -04:00
|
|
|
|
}
|
2022-10-25 13:47:34 -04:00
|
|
|
|
else // VSPI
|
2021-08-25 03:52:59 -04:00
|
|
|
|
{
|
|
|
|
|
mySPI = new SPIClass(VSPI);
|
|
|
|
|
mySPI->end();
|
2022-10-25 13:47:34 -04:00
|
|
|
|
mySPI->begin(18, 19, 23, select); // CLK=18 MISO=19 MOSI=23
|
2021-08-25 03:52:59 -04:00
|
|
|
|
}
|
2022-10-25 13:47:34 -04:00
|
|
|
|
#else // generic hardware SPI
|
2021-08-25 03:52:59 -04:00
|
|
|
|
mySPI = &SPI;
|
|
|
|
|
mySPI->end();
|
|
|
|
|
mySPI->begin();
|
|
|
|
|
#endif
|
2021-01-29 06:31:58 -05:00
|
|
|
|
}
|
2022-10-25 13:47:34 -04:00
|
|
|
|
else // software SPI
|
2021-01-29 06:31:58 -05:00
|
|
|
|
{
|
2021-08-25 03:52:59 -04:00
|
|
|
|
pinMode(_dataOut, OUTPUT);
|
|
|
|
|
pinMode(_clock, OUTPUT);
|
|
|
|
|
digitalWrite(_dataOut, LOW);
|
|
|
|
|
digitalWrite(_clock, LOW);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-25 03:52:59 -04:00
|
|
|
|
#if defined(ESP32)
|
|
|
|
|
void AD9850::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)
|
|
|
|
|
{
|
|
|
|
|
_clock = clk;
|
|
|
|
|
_dataOut = mosi;
|
|
|
|
|
_select = select;
|
|
|
|
|
pinMode(_select, OUTPUT);
|
|
|
|
|
digitalWrite(_select, LOW);
|
|
|
|
|
|
|
|
|
|
mySPI->end(); // disable SPI
|
|
|
|
|
mySPI->begin(clk, miso, mosi, select);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
void AD9850::reset()
|
|
|
|
|
{
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// be sure to select the correct device
|
2021-08-25 03:52:59 -04:00
|
|
|
|
digitalWrite(_select, HIGH);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
pulsePin(_reset);
|
2021-12-10 10:56:06 -05:00
|
|
|
|
if (_hwSPI)
|
|
|
|
|
{
|
|
|
|
|
#if defined(ESP32)
|
2022-10-25 13:47:34 -04:00
|
|
|
|
if (_useHSPI) pulsePin(14); // HSPI magic number clock
|
|
|
|
|
else pulsePin(18); // VSPI magic number clock
|
2021-12-10 10:56:06 -05:00
|
|
|
|
#else
|
|
|
|
|
// UNO hardware SPI
|
|
|
|
|
pulsePin(SPI_CLOCK);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2021-01-29 06:31:58 -05:00
|
|
|
|
else pulsePin(_clock);
|
2021-06-06 13:57:20 -04:00
|
|
|
|
digitalWrite(_select, LOW);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
2022-10-25 13:47:34 -04:00
|
|
|
|
_config = 0; // 0 phase no power down
|
2021-01-29 06:31:58 -05:00
|
|
|
|
_freq = 0;
|
|
|
|
|
_factor = 0;
|
|
|
|
|
_offset = 0;
|
|
|
|
|
_autoUpdate = true;
|
|
|
|
|
writeData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AD9850::powerDown()
|
|
|
|
|
{
|
2022-10-25 13:47:34 -04:00
|
|
|
|
_config |= AD985X_POWERDOWN; // keep phase and REFCLK as is.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
writeData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AD9850::powerUp()
|
|
|
|
|
{
|
|
|
|
|
_config &= ~AD985X_POWERDOWN;
|
|
|
|
|
writeData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AD9850::setPhase(uint8_t phase)
|
|
|
|
|
{
|
|
|
|
|
if (phase > 31) return;
|
|
|
|
|
_config &= 0x07;
|
|
|
|
|
_config |= (phase << 3);
|
|
|
|
|
writeData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AD9850::pulsePin(uint8_t pin)
|
|
|
|
|
{
|
|
|
|
|
digitalWrite(pin, HIGH);
|
|
|
|
|
digitalWrite(pin, LOW);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-25 03:52:59 -04:00
|
|
|
|
void AD9850::setSPIspeed(uint32_t speed)
|
|
|
|
|
{
|
|
|
|
|
_SPIspeed = speed;
|
|
|
|
|
_spi_settings = SPISettings(_SPIspeed, LSBFIRST, SPI_MODE0);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
void AD9850::writeData()
|
|
|
|
|
{
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// Serial.println(_factor, HEX);
|
|
|
|
|
// Serial.println(_config, HEX);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
uint32_t data = _factor;
|
|
|
|
|
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// used for multi device configuration only - https://github.com/RobTillaart/AD985X/issues/13
|
2021-01-29 06:31:58 -05:00
|
|
|
|
digitalWrite(_select, HIGH);
|
2021-08-25 03:52:59 -04:00
|
|
|
|
if (_hwSPI)
|
2021-01-29 06:31:58 -05:00
|
|
|
|
{
|
2021-08-25 03:52:59 -04:00
|
|
|
|
mySPI->beginTransaction(_spi_settings);
|
|
|
|
|
mySPI->transfer(data & 0xFF);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
data >>= 8;
|
2021-08-25 03:52:59 -04:00
|
|
|
|
mySPI->transfer(data & 0xFF);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
data >>= 8;
|
2021-08-25 03:52:59 -04:00
|
|
|
|
mySPI->transfer(data & 0xFF);
|
|
|
|
|
mySPI->transfer(data >> 8);
|
2022-10-25 13:47:34 -04:00
|
|
|
|
mySPI->transfer(_config & 0xFC); // mask factory test bit
|
2021-08-25 03:52:59 -04:00
|
|
|
|
mySPI->endTransaction();
|
2021-01-29 06:31:58 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
swSPI_transfer(data & 0xFF);
|
|
|
|
|
data >>= 8;
|
|
|
|
|
swSPI_transfer(data & 0xFF);
|
|
|
|
|
data >>= 8;
|
|
|
|
|
swSPI_transfer(data & 0xFF);
|
|
|
|
|
swSPI_transfer(data >> 8);
|
2022-10-25 13:47:34 -04:00
|
|
|
|
swSPI_transfer(_config & 0xFC); // mask factory test bit
|
2021-01-29 06:31:58 -05:00
|
|
|
|
}
|
|
|
|
|
digitalWrite(_select, LOW);
|
|
|
|
|
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// update frequency + phase + control bits.
|
|
|
|
|
// should at least be 4 ns delay - P14 datasheet
|
2021-01-29 06:31:58 -05:00
|
|
|
|
if (_autoUpdate) update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// simple one mode version
|
2021-08-25 03:52:59 -04:00
|
|
|
|
void AD9850::swSPI_transfer(uint8_t val)
|
2021-01-29 06:31:58 -05:00
|
|
|
|
{
|
2021-08-25 03:52:59 -04:00
|
|
|
|
uint8_t clk = _clock;
|
|
|
|
|
uint8_t dao = _dataOut;
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// for (uint8_t mask = 0x80; mask; mask >>= 1) // MSBFIRST
|
|
|
|
|
for (uint8_t mask = 0x01; mask; mask <<= 1) // LSBFIRST
|
2021-01-29 06:31:58 -05:00
|
|
|
|
{
|
2021-08-25 03:52:59 -04:00
|
|
|
|
digitalWrite(dao, (val & mask));
|
|
|
|
|
digitalWrite(clk, HIGH);
|
|
|
|
|
digitalWrite(clk, LOW);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AD9850::setFrequency(uint32_t freq)
|
|
|
|
|
{
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// freq OUT = (Δ Phase × CLKIN)/2^32
|
|
|
|
|
// 64 bit math to keep precision to the max
|
2021-01-29 06:31:58 -05:00
|
|
|
|
if (freq > AD9850_MAX_FREQ) freq = AD9850_MAX_FREQ;
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// _factor = round(freq * 34.359738368); // 4294967296 / 125000000
|
2021-01-29 06:31:58 -05:00
|
|
|
|
_factor = (147573952589ULL * freq) >> 32;
|
|
|
|
|
_freq = freq;
|
|
|
|
|
_factor += _offset;
|
|
|
|
|
writeData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// especially for lower frequencies (with decimals)
|
2021-01-29 06:31:58 -05:00
|
|
|
|
void AD9850::setFrequencyF(float freq)
|
|
|
|
|
{
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// freq OUT = (Δ Phase × CLKIN)/2^32
|
|
|
|
|
// 64 bit math to keep precision to the max
|
2021-01-29 06:31:58 -05:00
|
|
|
|
if (freq > AD9850_MAX_FREQ) freq = AD9850_MAX_FREQ;
|
2022-10-25 13:47:34 -04:00
|
|
|
|
_factor = round(freq * 34.359738368); // 4294967296 / 125000000
|
2021-01-29 06:31:58 -05:00
|
|
|
|
_freq = freq;
|
|
|
|
|
_factor += _offset;
|
|
|
|
|
writeData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AD9850::update()
|
|
|
|
|
{
|
|
|
|
|
digitalWrite(_select, HIGH);
|
|
|
|
|
pulsePin(_fqud);
|
|
|
|
|
digitalWrite(_select, LOW);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
//
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// AD9851
|
2021-01-29 06:31:58 -05:00
|
|
|
|
//
|
|
|
|
|
|
2022-10-25 13:47:34 -04:00
|
|
|
|
#define AD9851_REFCLK 0x01 // bit is a 6x multiplier bit P.14 datasheet
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
void AD9851::setFrequency(uint32_t freq)
|
|
|
|
|
{
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// PREVENT OVERFLOW
|
2021-01-29 06:31:58 -05:00
|
|
|
|
if (freq > AD9851_MAX_FREQ) freq = AD9851_MAX_FREQ;
|
|
|
|
|
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// AUTO SWITCH REFERENCE FREQUENCY
|
2021-01-29 06:31:58 -05:00
|
|
|
|
if (_autoRefClock)
|
|
|
|
|
{
|
|
|
|
|
if (freq > _ARCCutOffFreq)
|
|
|
|
|
{
|
|
|
|
|
_config |= AD9851_REFCLK;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_config &= ~AD9851_REFCLK;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 13:47:34 -04:00
|
|
|
|
if (_config & AD9851_REFCLK) // 6x 30 = 180 MHz
|
2021-01-29 06:31:58 -05:00
|
|
|
|
{
|
|
|
|
|
_factor = (102481911520ULL * freq) >> 32; // (1 << 64) / 180000000
|
|
|
|
|
}
|
2022-10-25 13:47:34 -04:00
|
|
|
|
else // 1x 30 = 30 MHz
|
2021-01-29 06:31:58 -05:00
|
|
|
|
{
|
|
|
|
|
_factor = (614891469123ULL * freq) >> 32; // (1 << 64) / 30000000
|
|
|
|
|
}
|
|
|
|
|
_freq = freq;
|
|
|
|
|
_factor += _offset;
|
|
|
|
|
writeData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// especially for lower frequencies (with decimals)
|
2021-01-29 06:31:58 -05:00
|
|
|
|
void AD9851::setFrequencyF(float freq)
|
|
|
|
|
{
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// PREVENT OVERFLOW
|
2021-01-29 06:31:58 -05:00
|
|
|
|
if (freq > AD9851_MAX_FREQ) freq = AD9851_MAX_FREQ;
|
|
|
|
|
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// AUTO SWITCH REFERENCE FREQUENCY
|
2021-01-29 06:31:58 -05:00
|
|
|
|
if (_autoRefClock)
|
|
|
|
|
{
|
|
|
|
|
if (freq > _ARCCutOffFreq)
|
|
|
|
|
{
|
|
|
|
|
_config |= AD9851_REFCLK;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_config &= ~AD9851_REFCLK;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 13:47:34 -04:00
|
|
|
|
if (_config & AD9851_REFCLK) // 6x 30 = 180 MHz
|
2021-01-29 06:31:58 -05:00
|
|
|
|
{
|
|
|
|
|
_factor = uint64_t(102481911520ULL * freq) >> 32; // (1 << 64) / 180000000
|
|
|
|
|
}
|
2022-10-25 13:47:34 -04:00
|
|
|
|
else // 1x 30 = 30 MHz
|
2021-01-29 06:31:58 -05:00
|
|
|
|
{
|
|
|
|
|
_factor = (6148914691ULL * uint64_t (100 * freq)) >> 32;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_freq = freq;
|
|
|
|
|
_factor += _offset;
|
|
|
|
|
writeData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
//
|
2022-10-25 13:47:34 -04:00
|
|
|
|
// AD9851 - AUTO REFERENCE CLOCK
|
2021-01-29 06:31:58 -05:00
|
|
|
|
//
|
|
|
|
|
void AD9851::setAutoRefClock(bool arc)
|
|
|
|
|
{
|
|
|
|
|
_autoRefClock = arc;
|
|
|
|
|
setFrequency(_freq);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AD9851::setRefClockHigh()
|
|
|
|
|
{
|
|
|
|
|
_config |= AD9851_REFCLK;
|
|
|
|
|
setFrequency(_freq);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AD9851::setRefClockLow()
|
|
|
|
|
{
|
|
|
|
|
_config &= ~AD9851_REFCLK;
|
|
|
|
|
setFrequency(_freq);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t AD9851::getRefClock()
|
|
|
|
|
{
|
|
|
|
|
return (_config & AD9851_REFCLK) ? 180 : 30;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AD9851::setARCCutOffFreq(uint32_t Hz)
|
|
|
|
|
{
|
|
|
|
|
if (Hz > 30000000UL) Hz = 30000000;
|
|
|
|
|
_ARCCutOffFreq = Hz;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
2022-10-25 13:47:34 -04:00
|
|
|
|
|