0.3.4 AD985X

This commit is contained in:
rob tillaart 2022-10-25 19:47:34 +02:00
parent 5fa5018b5f
commit f38eb9bcbc
20 changed files with 151 additions and 111 deletions

View File

@ -1,3 +1,18 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
@ -7,5 +22,6 @@ compile:
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560
- esp8266
# - mega2560
- rpipico

View File

@ -1,35 +1,19 @@
//
// FILE: AD985X.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.3
// VERSION: 0.3.4
// DATE: 2019-02-08
// PURPOSE: Class for AD9850 and AD9851 function generator
// URL: https://github.com/RobTillaart/AD985X
//
// HISTORY:
// 0.1.0 2019-03-19 initial version
// 0.1.1 2020-12-09 add Arduino-CI
// 0.1.2 2020-12-27 add setAutoMode() + offset
// 0.2.0 2020-12-28 major refactor class hierarchy + float frequency
// 0.2.1 2021-01-10 add get- and setARCCutOffFreq()
// 0.2.2 2021-01-24 add manual updating frequency
// get- setManualFQ_UD(), update()
// inverted SELECT line as preparation for multi-device.
// 0.3.0 2021-06-06 fix factory bit mask + new examples + some refactor
// added multi device document
// 0.3.1 2021-08-25 VSPI / HSPI support for ESP32
// faster software SPI
// minor optimizations / refactor
// 0.3.2 2021-10-16 update Arduino-CI
// 0.3.3 2021-12-10 update library.json, license, readme.md
// fix reset() for ESP32 hardware SPI
// HISTORY: see changelog.md
#include "AD985X.h"
// UNO HARDWARE SPI PINS
#define SPI_CLOCK 13 // not portable...
// UNO HARDWARE SPI PINS
#define SPI_CLOCK 13 // not portable...
#define SPI_MISO 12
#define SPI_MOSI 11
@ -39,7 +23,7 @@
////////////////////////////////////////////////////////
//
// AD9850
// AD9850
//
AD9850::AD9850()
@ -54,11 +38,11 @@ void AD9850::begin(uint8_t select, uint8_t resetPin, uint8_t FQUDPin, uint8_t da
_fqud = FQUDPin;
_dataOut = dataOut;
_clock = clock;
// following 3 are always set.
// following 3 are always set.
pinMode(_select, OUTPUT);
pinMode(_reset, OUTPUT);
pinMode(_fqud, OUTPUT);
// device select = HIGH See - https://github.com/RobTillaart/AD985X/issues/13
// device select = HIGH See - https://github.com/RobTillaart/AD985X/issues/13
digitalWrite(_select, LOW);
digitalWrite(_reset, LOW);
digitalWrite(_fqud, LOW);
@ -70,25 +54,25 @@ void AD9850::begin(uint8_t select, uint8_t resetPin, uint8_t FQUDPin, uint8_t da
if (_hwSPI)
{
#if defined(ESP32)
if (_useHSPI) // HSPI
if (_useHSPI) // HSPI
{
mySPI = new SPIClass(HSPI);
mySPI->end();
mySPI->begin(14, 12, 13, select); // CLK=14 MISO=12 MOSI=13
mySPI->begin(14, 12, 13, select); // CLK=14 MISO=12 MOSI=13
}
else // VSPI
else // VSPI
{
mySPI = new SPIClass(VSPI);
mySPI->end();
mySPI->begin(18, 19, 23, select); // CLK=18 MISO=19 MOSI=23
mySPI->begin(18, 19, 23, select); // CLK=18 MISO=19 MOSI=23
}
#else // generic hardware SPI
#else // generic hardware SPI
mySPI = &SPI;
mySPI->end();
mySPI->begin();
#endif
}
else // software SPI
else // software SPI
{
pinMode(_dataOut, OUTPUT);
pinMode(_clock, OUTPUT);
@ -117,14 +101,14 @@ void AD9850::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select
void AD9850::reset()
{
// be sure to select the correct device
// be sure to select the correct device
digitalWrite(_select, HIGH);
pulsePin(_reset);
if (_hwSPI)
{
#if defined(ESP32)
if (_useHSPI) pulsePin(14); // HSPI magic number clock
else pulsePin(18); // VSPI magic number clock
if (_useHSPI) pulsePin(14); // HSPI magic number clock
else pulsePin(18); // VSPI magic number clock
#else
// UNO hardware SPI
pulsePin(SPI_CLOCK);
@ -133,7 +117,7 @@ void AD9850::reset()
else pulsePin(_clock);
digitalWrite(_select, LOW);
_config = 0; // 0 phase no power down
_config = 0; // 0 phase no power down
_freq = 0;
_factor = 0;
_offset = 0;
@ -144,7 +128,7 @@ void AD9850::reset()
void AD9850::powerDown()
{
_config |= AD985X_POWERDOWN; // keep phase and REFCLK as is.
_config |= AD985X_POWERDOWN; // keep phase and REFCLK as is.
writeData();
}
@ -181,11 +165,11 @@ void AD9850::setSPIspeed(uint32_t speed)
void AD9850::writeData()
{
// Serial.println(_factor, HEX);
// Serial.println(_config, HEX);
// Serial.println(_factor, HEX);
// Serial.println(_config, HEX);
uint32_t data = _factor;
// used for multi device configuration only - https://github.com/RobTillaart/AD985X/issues/13
// used for multi device configuration only - https://github.com/RobTillaart/AD985X/issues/13
digitalWrite(_select, HIGH);
if (_hwSPI)
{
@ -196,7 +180,7 @@ void AD9850::writeData()
data >>= 8;
mySPI->transfer(data & 0xFF);
mySPI->transfer(data >> 8);
mySPI->transfer(_config & 0xFC); // mask factory test bit
mySPI->transfer(_config & 0xFC); // mask factory test bit
mySPI->endTransaction();
}
else
@ -207,23 +191,23 @@ void AD9850::writeData()
data >>= 8;
swSPI_transfer(data & 0xFF);
swSPI_transfer(data >> 8);
swSPI_transfer(_config & 0xFC); // mask factory test bit
swSPI_transfer(_config & 0xFC); // mask factory test bit
}
digitalWrite(_select, LOW);
// update frequency + phase + control bits.
// should at least be 4 ns delay - P14 datasheet
// update frequency + phase + control bits.
// should at least be 4 ns delay - P14 datasheet
if (_autoUpdate) update();
}
// simple one mode version
// simple one mode version
void AD9850::swSPI_transfer(uint8_t val)
{
uint8_t clk = _clock;
uint8_t dao = _dataOut;
// for (uint8_t mask = 0x80; mask; mask >>= 1) // MSBFIRST
for (uint8_t mask = 0x01; mask; mask <<= 1) // LSBFIRST
// for (uint8_t mask = 0x80; mask; mask >>= 1) // MSBFIRST
for (uint8_t mask = 0x01; mask; mask <<= 1) // LSBFIRST
{
digitalWrite(dao, (val & mask));
digitalWrite(clk, HIGH);
@ -234,10 +218,10 @@ void AD9850::swSPI_transfer(uint8_t val)
void AD9850::setFrequency(uint32_t freq)
{
// freq OUT = (Δ Phase × CLKIN)/2^32
// 64 bit math to keep precision to the max
// freq OUT = (Δ Phase × CLKIN)/2^32
// 64 bit math to keep precision to the max
if (freq > AD9850_MAX_FREQ) freq = AD9850_MAX_FREQ;
// _factor = round(freq * 34.359738368); // 4294967296 / 125000000
// _factor = round(freq * 34.359738368); // 4294967296 / 125000000
_factor = (147573952589ULL * freq) >> 32;
_freq = freq;
_factor += _offset;
@ -245,13 +229,13 @@ void AD9850::setFrequency(uint32_t freq)
}
// especially for lower frequencies (with decimals)
// especially for lower frequencies (with decimals)
void AD9850::setFrequencyF(float freq)
{
// freq OUT = (Δ Phase × CLKIN)/2^32
// 64 bit math to keep precision to the max
// freq OUT = (Δ Phase × CLKIN)/2^32
// 64 bit math to keep precision to the max
if (freq > AD9850_MAX_FREQ) freq = AD9850_MAX_FREQ;
_factor = round(freq * 34.359738368); // 4294967296 / 125000000
_factor = round(freq * 34.359738368); // 4294967296 / 125000000
_freq = freq;
_factor += _offset;
writeData();
@ -268,17 +252,17 @@ void AD9850::update()
////////////////////////////////////////////////////////
//
// AD9851
// AD9851
//
#define AD9851_REFCLK 0x01 // bit is a 6x multiplier bit P.14 datasheet
#define AD9851_REFCLK 0x01 // bit is a 6x multiplier bit P.14 datasheet
void AD9851::setFrequency(uint32_t freq)
{
// PREVENT OVERFLOW
// PREVENT OVERFLOW
if (freq > AD9851_MAX_FREQ) freq = AD9851_MAX_FREQ;
// AUTO SWITCH REFERENCE FREQUENCY
// AUTO SWITCH REFERENCE FREQUENCY
if (_autoRefClock)
{
if (freq > _ARCCutOffFreq)
@ -291,11 +275,11 @@ void AD9851::setFrequency(uint32_t freq)
}
}
if (_config & AD9851_REFCLK) // 6x 30 = 180 MHz
if (_config & AD9851_REFCLK) // 6x 30 = 180 MHz
{
_factor = (102481911520ULL * freq) >> 32; // (1 << 64) / 180000000
}
else // 1x 30 = 30 MHz
else // 1x 30 = 30 MHz
{
_factor = (614891469123ULL * freq) >> 32; // (1 << 64) / 30000000
}
@ -305,13 +289,13 @@ void AD9851::setFrequency(uint32_t freq)
}
// especially for lower frequencies (with decimals)
// especially for lower frequencies (with decimals)
void AD9851::setFrequencyF(float freq)
{
// PREVENT OVERFLOW
// PREVENT OVERFLOW
if (freq > AD9851_MAX_FREQ) freq = AD9851_MAX_FREQ;
// AUTO SWITCH REFERENCE FREQUENCY
// AUTO SWITCH REFERENCE FREQUENCY
if (_autoRefClock)
{
if (freq > _ARCCutOffFreq)
@ -324,11 +308,11 @@ void AD9851::setFrequencyF(float freq)
}
}
if (_config & AD9851_REFCLK) // 6x 30 = 180 MHz
if (_config & AD9851_REFCLK) // 6x 30 = 180 MHz
{
_factor = uint64_t(102481911520ULL * freq) >> 32; // (1 << 64) / 180000000
}
else // 1x 30 = 30 MHz
else // 1x 30 = 30 MHz
{
_factor = (6148914691ULL * uint64_t (100 * freq)) >> 32;
}
@ -341,7 +325,7 @@ void AD9851::setFrequencyF(float freq)
////////////////////////////////////////////////////////
//
// AD9851 - AUTO REFERENCE CLOCK
// AD9851 - AUTO REFERENCE CLOCK
//
void AD9851::setAutoRefClock(bool arc)
{
@ -377,3 +361,4 @@ void AD9851::setARCCutOffFreq(uint32_t Hz)
// -- END OF FILE --

View File

@ -2,10 +2,9 @@
//
// FILE: AD985X.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.3
// VERSION: 0.3.4
// DATE: 2019-02-08
// PURPOSE: Class for AD9850 and AD9851 function generator
//
// URL: https://github.com/RobTillaart/AD985X
//
@ -14,7 +13,7 @@
#include "SPI.h"
#define AD985X_LIB_VERSION (F("0.3.3"))
#define AD985X_LIB_VERSION (F("0.3.4"))
#define AD9850_MAX_FREQ (40UL * 1000UL * 1000UL)
@ -26,30 +25,30 @@ class AD9850
public:
AD9850();
// for HW SPI only use lower 3 parameters.
// for HW SPI only use lower 3 parameters.
void begin(uint8_t select, uint8_t resetPin, uint8_t FQUDPin, uint8_t dataOut = 0, uint8_t clock = 0);
void reset();
void powerDown();
void powerUp();
void setFrequency(uint32_t freq); // 0..AD9850_MAX_FREQ
void setFrequencyF(float freq); // works best for lower frequencies.
void setFrequency(uint32_t freq); // 0..AD9850_MAX_FREQ
void setFrequencyF(float freq); // works best for lower frequencies.
float getFrequency() { return _freq; };
uint32_t getMaxFrequency() { return AD9850_MAX_FREQ; };
// 0 .. 31 steps of 11.25 degrees
// 0 .. 31 steps of 11.25 degrees
void setPhase(uint8_t phase = 0);
uint8_t getPhase() { return (_config >> 3); };
// offset to calibrate the frequency (internal counter)
// offset must be stored by the user.
// offset to calibrate the frequency (internal counter)
// offset must be stored by the user.
void setCalibration(int32_t offset = 0) { _offset = offset; };
int32_t getCalibration() { return _offset; };
// internal chip factor used for frequency. (debugging only)
// internal chip factor used for frequency. (debugging only)
uint32_t getFactor() { return _factor; };
// autoUpdate is default true;
void setAutoUpdate(bool update) { _autoUpdate = update; };
// autoUpdate is default true;
void setAutoUpdate(bool update = true) { _autoUpdate = update; };
bool getAutoUpdate() { return _autoUpdate; };
void update();
@ -57,17 +56,17 @@ public:
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed() { return _SPIspeed; };
// debugging
// debugging
bool usesHWSPI() { return _hwSPI; };
// ESP32 specific
// ESP32 specific
#if defined(ESP32)
void selectHSPI() { _useHSPI = true; };
void selectVSPI() { _useHSPI = false; };
bool usesHSPI() { return _useHSPI; };
bool usesVSPI() { return !_useHSPI; };
// to overrule ESP32 default hardware pins
// to overrule ESP32 default hardware pins
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
#endif
@ -87,14 +86,14 @@ protected:
bool _useHSPI = true;
#endif
// PINS
// PINS
uint8_t _dataOut = 0;
uint8_t _clock = 0;
uint8_t _select = 0;
uint8_t _reset = 0;
uint8_t _fqud = 0; // frequency update
uint8_t _fqud = 0; // frequency update
// SIGNAL
// SIGNAL
float _freq = 1;
uint32_t _factor = 0;
uint8_t _config = 0;
@ -106,19 +105,19 @@ protected:
class AD9851 : public AD9850
{
public:
void setFrequency(uint32_t freq); // 0..AD9851_MAX_FREQ
void setFrequency(uint32_t freq); // 0..AD9851_MAX_FREQ
void setFrequencyF(float freq);
uint32_t getMaxFrequency() { return AD9851_MAX_FREQ; };
void setRefClockHigh(); // 180 MHz
void setRefClockLow(); // 30 MHz
void setRefClockHigh(); // 180 MHz
void setRefClockLow(); // 30 MHz
uint8_t getRefClock();
void setAutoRefClock(bool arc);
bool getAutoRefClock() { return _autoRefClock; };
// 10 MHz is default, set in Hz.
// will be kept <= 30 MHz as that is the freq of LOW mode.
// 10 MHz is default, set in Hz.
// will be kept <= 30 MHz as that is the freq of LOW mode.
void setARCCutOffFreq(uint32_t Hz = 10000000UL );
uint32_t getARCCutOffFreq() { return _ARCCutOffFreq; };
@ -128,4 +127,6 @@ protected:
};
// -- END OF FILE --

View File

@ -14,10 +14,22 @@ Arduino library for AD9850 and AD9851 function generators.
## Description
Library for the AD9850 and AD9851 function generators.
These devices can produce a square and a sine wave
| type | max frequency | phase (stepsize) |
|:---------|---------------:|-------------------:|
| AD9850 | 40 MHz | 0..31 x 11.25° |
| AD9851 | 70 MHz | 0..31 x 11.25° |
Note that at the max frequency the devices do not give a nice sine anymore.
You need to check what is acceptable for your project.
The library has a AD9850 as base class that implements the commonalities.
The AD9851 is derived and has its own **setFrequency()** methods.
Furthermore the AD9851 also has function to select the reference clock,
a feature the AD9850 does not have.
a feature the AD9850 does not have.
This feature improves the tuning for both low and high frequencies.
**Warning**
The library is not suitable for AD9852 as that is a function generator with
@ -28,6 +40,7 @@ Note: mainly tested on Arduino UNO. Tweaking for other platforms is expected.
## Connection
Schema break-out
```
TOP VIEW
@ -54,9 +67,10 @@ Note: mainly tested on Arduino UNO. Tweaking for other platforms is expected.
```
### Multi device
See Multi_AD985X_devices.pdf
See **Multi_AD985X_devices.pdf**
Discussion leading to the document see - https://github.com/RobTillaart/AD985X/issues/13
@ -292,4 +306,5 @@ The user is also responsible to store it e.g. in EEPROM to make it persistent.
- examples for ESP32 HWSPI interface
- do tests on ESP32
- performance measurements
- move code to .cpp

View File

@ -1,8 +1,8 @@
//
// FILE: AD9850_demo.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.2.0
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AD985X
#include "AD985X.h"

View File

@ -1,8 +1,8 @@
//
// FILE: AD9850_demo_float.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.2.0
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AD985X
#include "AD985X.h"

View File

@ -1,9 +1,8 @@
//
// FILE: AD985X_multi.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo multi device
// DATE: 2021-06-04
// URL: https://github.com/RobTillaart/AD985X
/*

View File

@ -1,9 +1,8 @@
//
// FILE: AD9850_multi_sync.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo multi device
// DATE: 2021-06-06
// URL: https://github.com/RobTillaart/AD985X
// measure speed difference when updating multiple devices
// - sequentially

View File

@ -1,9 +1,8 @@
//
// FILE: AD9851_demo.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.2.0
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AD985X
#include "AD985X.h"

View File

@ -1,8 +1,8 @@
//
// FILE: AD9851_demo_2.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AD985X
#include "AD985X.h"

View File

@ -1,9 +1,8 @@
//
// FILE: AD9851_demo_float.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.2.0
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AD985X
#include "AD985X.h"

View File

@ -1,8 +1,8 @@
//
// FILE: AD9851_manual update.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AD985X
#include "AD985X.h"

View File

@ -0,0 +1,27 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560
# - rpipico

View File

@ -1,8 +1,8 @@
//
// FILE: AD9851_six_potmeter.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AD985X
#include "AD985X.h"

View File

@ -1,8 +1,8 @@
//
// FILE: AD9851_sweeper.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.2.0
// PURPOSE: demo
// URL: https://github.com/RobTillaart/AD985X
#include "AD985X.h"

View File

@ -1,9 +1,8 @@
//
// FILE: AD985X_array.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo multi device
// DATE: 2021-06-04
// URL: https://github.com/RobTillaart/AD985X
/*

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/AD985X.git"
},
"version": "0.3.3",
"version": "0.3.4",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=AD985X
version=0.3.3
version=0.3.4
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for AD9850 and AD9851 function generators. Supports both hardware SPI as software SPI.

View File

@ -27,8 +27,10 @@
unittest_setup()
{
fprintf(stderr, "VERSION: %s\n", AD985X_LIB_VERSION);
}
unittest_teardown()
{
}
@ -36,7 +38,6 @@ unittest_teardown()
unittest(test_constructor)
{
fprintf(stderr, "VERSION: %s\n", AD985X_LIB_VERSION);
AD9850 funcgen0;
AD9851 funcgen1;
@ -47,7 +48,6 @@ unittest(test_constructor)
unittest(test_auto_update)
{
fprintf(stderr, "VERSION: %s\n", AD985X_LIB_VERSION);
AD9850 funcgen0;
AD9851 funcgen1;
funcgen0.begin(4, 5, 6);
@ -191,6 +191,7 @@ unittest(test_ad9851_offset)
assertEqual(0, funcgen.getCalibration());
}
unittest(test_ad9851_float_freq)
{
AD9851 funcgen;