diff --git a/AD568X.cpp b/AD568X.cpp index 9a6c238..e249bb4 100644 --- a/AD568X.cpp +++ b/AD568X.cpp @@ -1,7 +1,7 @@ // // FILE: AD568X.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 +// VERSION: 0.2.0 // DATE: 2023-09-18 // PURPOSE: Arduino library for AD568X series Digital Analog Convertor. @@ -17,20 +17,24 @@ #define AD568X_REG_CONTROL 0x40 -AD568X::AD568X(uint8_t slaveSelect) + +// HARDWARE SPI +AD568X::AD568X(uint8_t slaveSelect, __SPI_CLASS__ * mySPI) { - _hwSPI = true; _select = slaveSelect; + _hwSPI = true; + _mySPI = mySPI; _value = 0; } - -AD568X::AD568X(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect) +// SOFTWARE SPI +AD568X::AD568X(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock) { + _select = slaveSelect; _hwSPI = false; + _mySPI = NULL; _dataOut = spiData; _clock = spiClock; - _select = slaveSelect; _value = 0; } @@ -46,27 +50,11 @@ void AD568X::begin() if(_hwSPI) { - #if defined(ESP32) - if (_useHSPI) // HSPI - { - mySPI = new SPIClass(HSPI); - mySPI->end(); - mySPI->begin(14, 12, 13, _select); // CLK=14 MISO=12 MOSI=13 - } - else // VSPI - { - mySPI = new SPIClass(VSPI); - mySPI->end(); - mySPI->begin(18, 19, 23, _select); // CLK=18 MISO=19 MOSI=23 - } - #else // generic hardware SPI - mySPI = &SPI; - mySPI->end(); - mySPI->begin(); - #endif + _mySPI->end(); + _mySPI->begin(); delay(1); } - else // software SPI + else // SOFTWARE SPI { pinMode(_dataOut, OUTPUT); pinMode(_clock, OUTPUT); @@ -239,48 +227,6 @@ bool AD568X::usesHWSPI() } -// ESP32 specific -#if defined(ESP32) - -void AD568X::selectHSPI() -{ - _useHSPI = true; -} - - -void AD568X::selectVSPI() -{ - _useHSPI = false; -} - - -bool AD568X::usesHSPI() -{ - return _useHSPI; -} - - -bool AD568X::usesVSPI() -{ - return !_useHSPI; -} - - -void AD568X::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select) -{ - _clock = clk; - _dataOut = mosi; - _select = select; - pinMode(_select, OUTPUT); - digitalWrite(_select, HIGH); - - mySPI->end(); // disable SPI - mySPI->begin(clk, miso, mosi, select); // enable SPI -} - -#endif - - ////////////////////////////////////////////////////////////////// // // PRIVATE @@ -302,11 +248,11 @@ void AD568X::updateDevice(uint8_t a, uint8_t b, uint8_t c) digitalWrite(_select, LOW); if (_hwSPI) { - mySPI->beginTransaction(_spi_settings); - mySPI->transfer(a); - mySPI->transfer(b); - mySPI->transfer(c); - mySPI->endTransaction(); + _mySPI->beginTransaction(_spi_settings); + _mySPI->transfer(a); + _mySPI->transfer(b); + _mySPI->transfer(c); + _mySPI->endTransaction(); } else // Software SPI { @@ -354,17 +300,15 @@ void AD568X::swSPI_transfer(uint8_t value) // // DERIVED AD5681 // -AD5681R::AD5681R(uint8_t slaveSelect) : AD568X(slaveSelect) +AD5681R::AD5681R(uint8_t slaveSelect, __SPI_CLASS__ * mySPI) : AD568X(slaveSelect, mySPI) { _type = 12; - _value = 0; } -AD5681R::AD5681R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect) - : AD568X(spiData, spiClock, slaveSelect) +AD5681R::AD5681R(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock) + : AD568X(slaveSelect, spiData, spiClock) { _type = 12; - _value = 0; } @@ -372,17 +316,15 @@ AD5681R::AD5681R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect) // // DERIVED AD5682 // -AD5682R::AD5682R(uint8_t slaveSelect) : AD568X(slaveSelect) +AD5682R::AD5682R(uint8_t slaveSelect, __SPI_CLASS__ * mySPI) : AD568X(slaveSelect, mySPI) { _type = 14; - _value = 0; } -AD5682R::AD5682R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect) - : AD568X(spiData, spiClock, slaveSelect) +AD5682R::AD5682R(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock) + : AD568X(slaveSelect, spiData, spiClock) { _type = 14; - _value = 0; } @@ -390,17 +332,15 @@ AD5682R::AD5682R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect) // // DERIVED AD5683 // -AD5683R::AD5683R(uint8_t slaveSelect) : AD568X(slaveSelect) +AD5683R::AD5683R(uint8_t slaveSelect, __SPI_CLASS__ * mySPI) : AD568X(slaveSelect, mySPI) { _type = 16; - _value = 0; } -AD5683R::AD5683R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect) - : AD568X(spiData, spiClock, slaveSelect) +AD5683R::AD5683R(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock) + : AD568X(slaveSelect, spiData, spiClock) { _type = 16; - _value = 0; } @@ -408,17 +348,15 @@ AD5683R::AD5683R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect) // // DERIVED AD5683 // -AD5683::AD5683(uint8_t slaveSelect) : AD568X(slaveSelect) +AD5683::AD5683(uint8_t slaveSelect, __SPI_CLASS__ * mySPI) : AD568X(slaveSelect, mySPI) { _type = 16; - _value = 0; } -AD5683::AD5683(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect) - : AD568X(spiData, spiClock, slaveSelect) +AD5683::AD5683(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock) + : AD568X(slaveSelect, spiData, spiClock) { _type = 16; - _value = 0; } diff --git a/AD568X.h b/AD568X.h index 5740015..948004d 100644 --- a/AD568X.h +++ b/AD568X.h @@ -2,7 +2,7 @@ // // FILE: AD568X.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 +// VERSION: 0.2.0 // DATE: 2023-09-18 // PURPOSE: Arduino library for AD568X series Digital Analog Convertor. @@ -10,7 +10,14 @@ #include "Arduino.h" #include "SPI.h" -#define AD568X_LIB_VERSION (F("0.1.0")) +#define AD568X_LIB_VERSION (F("0.2.0")) + + +#if defined(ARDUINO_ARCH_RP2040) +#define __SPI_CLASS__ SPIClassRP2040 +#else +#define __SPI_CLASS__ SPIClass +#endif #define AD568X_PWR_NORMAL 0x00 @@ -22,8 +29,11 @@ class AD568X { public: - AD568X(uint8_t slaveSelect); - AD568X(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect); + // HARDWARE SPI + AD568X(uint8_t slaveSelect, __SPI_CLASS__ * mySPI = &SPI); + // SOFTWARE SPI + AD568X(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock); + void begin(); uint8_t getType(); @@ -71,17 +81,6 @@ class AD568X uint32_t getSPIspeed(); bool usesHWSPI(); - // ESP32 specific - #if defined(ESP32) - void selectHSPI(); - void selectVSPI(); - bool usesHSPI(); - bool usesVSPI(); - - // to overrule ESP32 default hardware pins - void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select); - #endif - protected: uint8_t _type = 0; // # bits @@ -100,8 +99,8 @@ class AD568X void updateDevice(uint8_t a, uint8_t b, uint8_t c); void swSPI_transfer(uint8_t value); - SPIClass * mySPI; - SPISettings _spi_settings; + __SPI_CLASS__ * _mySPI; + SPISettings _spi_settings; #if defined(ESP32) bool _useHSPI = true; @@ -124,36 +123,44 @@ class AD568X class AD5681R : public AD568X { public: - AD5681R(uint8_t slaveSelect); - AD5681R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect); - - // SHORT WRITE ? + // HARDWARE SPI + AD5681R(uint8_t slaveSelect, __SPI_CLASS__ * mySPI = &SPI); + // SOFTWARE SPI + AD5681R(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock); }; class AD5682R : public AD568X { public: - AD5682R(uint8_t slaveSelect); - AD5682R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect); + // HARDWARE SPI + AD5682R(uint8_t slaveSelect, __SPI_CLASS__ * mySPI = &SPI); + // SOFTWARE SPI + AD5682R(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock); }; class AD5683R : public AD568X { public: - AD5683R(uint8_t slaveSelect); - AD5683R(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect); + // HARDWARE SPI + AD5683R(uint8_t slaveSelect, __SPI_CLASS__ * mySPI = &SPI); + // SOFTWARE SPI + AD5683R(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock); + }; class AD5683 : public AD568X { public: - AD5683(uint8_t slaveSelect); - AD5683(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect); + // HARDWARE SPI + AD5683(uint8_t slaveSelect, __SPI_CLASS__ * mySPI = &SPI); + // SOFTWARE SPI + AD5683(uint8_t slaveSelect, uint8_t spiData, uint8_t spiClock); + }; +// -- END OF FILE -- -// -- END OF FILE -- diff --git a/CHANGELOG.md b/CHANGELOG.md index 503de8d..b2bc895 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.2.0] - 2023-11-26 +- refactor constructor interface - breaking changes. + - minimize conditional code. -- create SPI_CLASS macro to solve it. +- reordered parameters software SPI constructor. +- update readme.md +- update examples + +---- + ## [0.1.0] - 2023-09-18 - initial version diff --git a/README.md b/README.md index 0ca318b..f7999fc 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,16 @@ Feedback, issues, improvements are welcome. Please file an issue on GitHub. +#### 0.2.0 breaking change + +The version 0.2.0 has breaking changes in the interface. +The essence is removal of ESP32 specific code from the library. +This makes it possible to support the ESP32-S3 and other processors in the future. +Also it makes the library a bit simpler to maintain. + +Note the order of the parameters of the software SPI constructor has changed in 0.2.0. + + ## Related - https://github.com/RobTillaart/AD56x8 (multi channel DAC's) @@ -63,23 +73,24 @@ Please file an issue on GitHub. Should not be used to instantiate a device as the derived types have set the correct number of bits. -- **AD568X(uint8_t slaveSelect)** constructor base class, sets HW SPI. -Sets internal values to zero. +- **AD568X(uint8_t slaveSelect, SPIClassRP2040 \* mySPI = &SPI)** constructor HW SPI (RP2040 specific). Sets internal value to zero. +- **AD568X(uint8_t slaveSelect, SPIClass \* mySPI = &SPI)** constructor HW SPI. +Sets internal value to zero. - **AD568X(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)** constructor, sets SW SPI. Sets internal values to zero. - **begin()** initializes the SPI and sets internal state. - +- **uint8_t getType()** returns bit depth (see below). #### Derived classes (preferred use) The parameters for the specific constructors are identical to the base class. One should use these, as these set the bit resolution! -- **AD5681R(..)** constructor, 12 bit. -- **AD5682R(..)** constructor, 14 bit. -- **AD5683R(..)** constructor, 16 bit. -- **AD5683(..)** constructor, 16 bit. +- **AD5681R(uint8_t slaveSelect, ..)** constructor, 12 bit. +- **AD5682R(uint8_t slaveSelect, ..)** constructor, 14 bit. +- **AD5683R(uint8_t slaveSelect, ..)** constructor, 16 bit. +- **AD5683(uint8_t slaveSelect, ..)** constructor, 16 bit. ### LDAC @@ -162,20 +173,6 @@ please read datasheet of the ADC first to get optimal speed. #### SPI ESP32 specific -("inherited" from MPC_DAC library) - -- **void selectHSPI()** in case hardware SPI, the ESP32 has two options HSPI and VSPI. -- **void selectVSPI()** see above. -- **bool usesHSPI()** returns true if HSPI is used. -- **bool usesVSPI()** returns true if VSPI is used. - -The **selectVSPI()** or the **selectHSPI()** needs to be called -BEFORE the **begin()** function. - -(experimental) -- **void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)** -overrule GPIO pins of ESP32 for hardware SPI. Needs to be called AFTER the **begin()** function. - Note: earlier experiments shows that on a ESP32 SW-SPI is equally fast as HW-SPI and in fact a bit more stable. The SW pulses are a bit slower than the HW pulses and therefore more square. @@ -190,7 +187,6 @@ The HW-SPI has some overhead SW-SPI hasn't. - get hardware for testing - test the library - #### Should - write examples @@ -200,7 +196,7 @@ The HW-SPI has some overhead SW-SPI hasn't. - multi device (LDAC sync) - Clean up / improve code. - move conditional code to variable. (setValue etc). - +- sync with AD5680 #### Could diff --git a/examples/AD568X_demo_ESP32/.arduino-ci.yml b/examples/AD568X_demo_ESP32/.arduino-ci.yml new file mode 100644 index 0000000..e1c447b --- /dev/null +++ b/examples/AD568X_demo_ESP32/.arduino-ci.yml @@ -0,0 +1,28 @@ +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 + diff --git a/examples/AD568X_demo_ESP32/AD568X_demo_ESP32.ino b/examples/AD568X_demo_ESP32/AD568X_demo_ESP32.ino new file mode 100644 index 0000000..d18647c --- /dev/null +++ b/examples/AD568X_demo_ESP32/AD568X_demo_ESP32.ino @@ -0,0 +1,57 @@ +// +// FILE: AD568X_demo_ESP32.ino +// AUTHOR: Rob Tillaart +// PUPROSE: test basic behaviour and performance + + +#include "AD568X.h" + + +#ifndef ESP32 +#error ESP32 only example, please select appropriate board +#endif + + +// HSPI uses default SCLK=14, MISO=12, MOSI=13, SELECT=15 +// VSPI uses default SCLK=18, MISO=19, MOSI=23, SELECT=5 +SPIClass * myspi = new SPIClass(VSPI); +AD568X AD16_HW(5, myspi); +AD568X AD16_SW(15, 13, 14); + + +void setup() +{ + Serial.begin(115200); + Serial.println(); + Serial.println(__FILE__); + Serial.print("AD568X_LIB_VERSION: "); + Serial.println(AD568X_LIB_VERSION); + + AD16_HW.begin(); + AD16_SW.begin(); + + Serial.print("HWSPI: "); + Serial.println(AD16_HW.usesHWSPI()); + Serial.print("HWSPI: "); + Serial.println(AD16_SW.usesHWSPI()); +} + + +void loop() +{ + uint32_t start = micros(); + for (int i = 0; i < 1000; i++) + { + // AD16_HW.setValue(i); + AD16_SW.setValue(i); + } + uint32_t duration = micros() - start; + Serial.print(duration); + Serial.print("\t"); + Serial.println(1e9 / duration); + + delay(1000); +} + + +// -- END OF FILE -- diff --git a/keywords.txt b/keywords.txt index 54f699f..3dafcc9 100644 --- a/keywords.txt +++ b/keywords.txt @@ -28,19 +28,11 @@ setReference KEYWORD2 enableGain KEYWORD2 enableDaisyChain KEYWORD2 - setSPIspeed KEYWORD2 getSPIspeed KEYWORD2 usesHWSPI KEYWORD2 -selectHSPI KEYWORD2 -selectVSPI KEYWORD2 -usesHSPI KEYWORD2 -usesVSPI KEYWORD2 - -setGPIOpins KEYWORD2 - # Constants (LITERAL1) AD568X_LIB_VERSION LITERAL1 diff --git a/library.json b/library.json index b3a1b37..63e9660 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/AD568X.git" }, - "version": "0.1.0", + "version": "0.2.0", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/library.properties b/library.properties index 234d6b7..88d6bee 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=AD568X -version=0.1.0 +version=0.2.0 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for AD568X series Digital Analog Convertor.