Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #13, refactor interface #14

Merged
merged 4 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.0] - 2023-12-01
- refactor constructor interface - breaking changes.
- minimize conditional code. -- create SPI_CLASS macro to solve it.
- update readme.md
- update examples

----

## [0.2.6] - 2023-10-19
- update readme.md
- minor edits


## [0.2.5] - 2022-10-31
- add changelog.md
- add rp2040 to build-CI
Expand Down
84 changes: 35 additions & 49 deletions DAC8554.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FILE: DAC8554.cpp
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for DAC8554 SPI Digital Analog Convertor
// VERSION: 0.2.6
// VERSION: 0.3.0
// DATE: 2017-12-19
// URL: https://github.com/RobTillaart/DAC8554

Expand All @@ -16,20 +16,24 @@
#define DAC8554_BROADCAST 0x30


DAC8554::DAC8554(uint8_t slaveSelect, uint8_t address)
DAC8554::DAC8554(uint8_t select, __SPI_CLASS__ * spi, uint8_t address)
{
_hwSPI = true;
_select = slaveSelect;
_select = select;
_dataOut = 255;
_clock = 255;
_mySPI = spi;
_hwSPI = true;
_address = (address & 0x03) << 6;
}


DAC8554::DAC8554(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect, uint8_t address)
DAC8554::DAC8554(uint8_t select, uint8_t spiData, uint8_t spiClock, uint8_t address)
{
_hwSPI = false;
_select = select;
_dataOut = spiData;
_clock = spiClock;
_select = slaveSelect;
_mySPI = NULL;
_hwSPI = false;
_address = (address & 0x03) << 6;
}

Expand All @@ -45,27 +49,11 @@ void DAC8554::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);
Expand All @@ -81,22 +69,6 @@ void DAC8554::begin()
}


#if defined(ESP32)
void DAC8554::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);
}
#endif



//////////////////////////////////////////////////////////////////////
//
// SETVALUE
Expand Down Expand Up @@ -231,19 +203,19 @@ void DAC8554::setSPIspeed(uint32_t speed)

//////////////////////////////////////////////////////////////////
//
// PRIVATE
// PROTECTED
//

void DAC8554::writeDevice(uint8_t configRegister, uint16_t value)
{
digitalWrite(_select, LOW);
if (_hwSPI)
{
mySPI->beginTransaction(_spi_settings);
mySPI->transfer(configRegister);
mySPI->transfer(value >> 8);
mySPI->transfer(value & 0xFF);
mySPI->endTransaction();;
_mySPI->beginTransaction(_spi_settings);
_mySPI->transfer(configRegister);
_mySPI->transfer(value >> 8);
_mySPI->transfer(value & 0xFF);
_mySPI->endTransaction();;
}
else // Software SPI
{
Expand All @@ -269,5 +241,19 @@ void DAC8554::swSPI_transfer(uint8_t value)
}


// -- END OF FILE --
/////////////////////////////////////////////////////////
//
// DERIVED CLASSES DAC8534
//
DAC8534::DAC8534(uint8_t select, __SPI_CLASS__ * spi, uint8_t address) : DAC8554(select, spi, address)
{
}

DAC8534::DAC8534(uint8_t select, uint8_t spiData, uint8_t spiClock, uint8_t address)
: DAC8554(select, spiData, spiClock, address)
{
}


// -- END OF FILE --

60 changes: 34 additions & 26 deletions DAC8554.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@
// FILE: DAC8554.h
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for DAC8554 SPI Digital Analog Convertor
// VERSION: 0.2.6
// VERSION: 0.3.0
// DATE: 2017-12-19
// URL: https://github.com/RobTillaart/DAC8554
//

#include "Arduino.h"
#include "SPI.h"

#define DAC8554_LIB_VERSION (F("0.2.6"))
#define DAC8554_LIB_VERSION (F("0.3.0"))

#define DAC8554_POWERDOWN_NORMAL 0x00
#define DAC8554_POWERDOWN_1K 0x40
#define DAC8554_POWERDOWN_100K 0x80
#define DAC8554_POWERDOWN_HIGH_IMP 0xC0


#ifndef __SPI_CLASS__
#if defined(ARDUINO_ARCH_RP2040)
#define __SPI_CLASS__ SPIClassRP2040
#else
#define __SPI_CLASS__ SPIClass
#endif
#endif


class DAC8554
{
public:
// hardware SPI constructor
DAC8554(uint8_t slaveSelect, uint8_t address = 0); // address is 0,1,2,3

DAC8554(uint8_t select, __SPI_CLASS__ * spi = &SPI, uint8_t address = 0);
// software SPI constructor
DAC8554(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect, uint8_t address = 0);
DAC8554(uint8_t select, uint8_t spiData, uint8_t spiClock, uint8_t address = 0);

void begin();

Expand Down Expand Up @@ -55,31 +63,16 @@ class DAC8554
// speed in Hz
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed() { return _SPIspeed; };


bool usesHWSPI() { return _hwSPI; };


// 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
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
#endif


private:
protected:
uint8_t _dataOut = 255;
uint8_t _clock = 255;
uint8_t _select = 255;

bool _hwSPI = false;
uint8_t _address;
uint8_t _address = 0;

uint16_t _value[4]; // holds last written / buffered value per channel
uint8_t _register[4]; // holds powerDownMode per channel
Expand All @@ -90,12 +83,27 @@ class DAC8554
void swSPI_transfer(uint8_t value);


SPIClass * mySPI;
__SPI_CLASS__ * _mySPI;
SPISettings _spi_settings;
};

#if defined(ESP32)
bool _useHSPI = true;
#endif

/////////////////////////////////////////////////////////
//
// DERIVED DAC8534
//

#define DAC8534_POWERDOWN_NORMAL 0
#define DAC8534_POWERDOWN_1K 1
#define DAC8534_POWERDOWN_100K 2
#define DAC8534_POWERDOWN_HIGH_IMP 3


class DAC8534 : public DAC8554
{
public:
DAC8534(uint8_t select, __SPI_CLASS__ * spi = &SPI, uint8_t address = 0);
DAC8534(uint8_t select, uint8_t spiData, uint8_t spiClock, uint8_t address = 0);
};


Expand Down
48 changes: 25 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ The DAC8554 is a SPI based 16 bit DAC with four channels.
**Warning** This library is not tested extensively


#### 0.3.0 Breaking change

The version 0.3.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 order of parameters changed.


#### Related

- https://github.com/RobTillaart/DAC8550
- https://github.com/RobTillaart/DAC8551
- https://github.com/RobTillaart/DAC8552
- https://github.com/RobTillaart/DAC8554
- https://github.com/RobTillaart/MCP_DAC
- https://github.com/RobTillaart/AD5680 (18 bit DAC)


## Interface

```cpp
Expand All @@ -29,10 +49,10 @@ The DAC8554 is a SPI based 16 bit DAC with four channels.

### Core

- **DAC8554(uint8_t slaveSelect, uint8_t address = 0)** Constructor for hardware SPI,
since 0.2.0 the slaveSelect pin needs to be defined.
- **DAC8554(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect, uint8_t address = 0)**
Constructor for the software SPI
- **DAC8554(uint8_t select, SPIClassRP2040 \* spi = &SPI)** Constructor HW SPI RP2040.
- **DAC8554(uint8_t select, SPIClass \* spi = &SPI)** Constructor HW SPI other.
- **DAC8554(uint8_t select, uint8_t spiData, uint8_t spiClock)** Constructor SW SPI.
- **DAC8534(...)** idem constructors for DAC8534.
- **void begin()** initializes all pins to default state
- **void setValue(uint8_t channel, uint16_t value)** set the value of the channel to 0 - 65535
- **void setSingleValue(uint8_t channel, uint16_t value)** writes the value to the channel but
Expand All @@ -49,24 +69,6 @@ To be used only if one needs a specific speed.
- **bool usesHWSPI()** returns true if HW SPI is used.


### ESP32 specific

- **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.


### Power down

Check datasheet for details.
Expand Down Expand Up @@ -126,7 +128,7 @@ See examples
#### Could

- performance measurements

- add channels?

#### Wont

Expand Down
Loading
Loading