Skip to content
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
16 changes: 16 additions & 0 deletions .arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -9,3 +24,4 @@ compile:
- esp32
# - esp8266
# - mega2560
# - rpipico
29 changes: 27 additions & 2 deletions MCP_DAC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ void MCP_DAC::begin(uint8_t select)
mySPI->end();
mySPI->begin(18, 19, 23, select); // CLK=18 MISO=19 MOSI=23
}

#elif defined(ARDUINO_ARCH_RP2040)

if (_useSPI1 == true) {
mySPI = &SPI1;
}else{
mySPI = &SPI;
}

mySPI->end();
mySPI->begin();


#else // generic hardware SPI
mySPI = &SPI;
mySPI->end();
Expand All @@ -85,8 +98,7 @@ void MCP_DAC::begin(uint8_t select)
}
}


#if defined(ESP32)
#if defined(ESP32) or defined(ARDUINO_ARCH_RP2040)
void MCP_DAC::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)
{
_clock = clk;
Expand All @@ -96,7 +108,20 @@ void MCP_DAC::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t selec
digitalWrite(_select, HIGH);

mySPI->end(); // disable SPI

#if defined(ESP32)

mySPI->begin(clk, miso, mosi, select);

#elif defined(ARDUINO_ARCH_RP2040)

mySPI->setCS(select);
mySPI->setSCK(clk);
mySPI->setTX(mosi);

mySPI->begin();

#endif
}
#endif

Expand Down
66 changes: 48 additions & 18 deletions MCP_DAC.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "SPI.h"


#define MCP_DAC_LIB_VERSION (F("0.1.6"))
#define MCP_DAC_LIB_VERSION (F("0.1.7"))



Expand Down Expand Up @@ -74,47 +74,77 @@ class MCP_DAC
void reset();
bool usesHWSPI() { return _hwSPI; };

// ESP32 specific
#if defined(ESP32)

#if defined(ESP32) // ESP32 specific

void selectHSPI() { _useHSPI = true; };
void selectVSPI() { _useHSPI = false; };
bool usesHSPI() { return _useHSPI; };
bool usesVSPI() { return !_useHSPI; };

// to overrule ESP32 default hardware pins
// to overrule the ESP32s default hardware pins
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);


#elif defined(ARDUINO_ARCH_RP2040) // RP2040 specific

// check which SPI-Bus (SPI or SPI1) is used
void selectSPI() { _useSPI1 = false; };
void selectSPI1() { _useSPI1 = true; };
bool usesSPI() { return !_useSPI1; };
bool usesSPI1() { return _useSPI1; };

// to overrule the RP2040s default hardware pins
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);

#endif


protected:
uint8_t _dataOut;
uint8_t _clock;
uint8_t _select;
uint8_t _latchPin = 255;
bool _hwSPI;
uint32_t _SPIspeed = 16000000;

uint8_t _channels;
uint16_t _maxValue;
uint16_t _value[2];
uint8_t _gain;
bool _buffered = false;
uint8_t _dataOut; // Data out Pin (MOSI)
uint8_t _clock; // Clock Pin (SCK)
uint8_t _select; // Chip Select Pin (CS)
uint8_t _latchPin = 255; // Latch-DAC Pin (LDAC)
bool _hwSPI; // Hardware SPI (true) or Software SPI (false)
uint32_t _SPIspeed = 16000000; // SPI-Bus Frequency

uint8_t _channels; // Number of DAC-Channels of a given Chip
uint16_t _maxValue; // maximum value of a given Chip
uint16_t _value[2]; // Current value
uint8_t _gain; // Programmable Gain Amplifier variable
bool _buffered = false; // Buffer for the Reference Voltage of the MCP49XX Series Chips
bool _active = true;

void transfer(uint16_t data);
uint8_t swSPI_transfer(uint8_t d);

#if defined(ARDUINO_ARCH_RP2040)

SPIClassRP2040 * mySPI;

#else

SPIClass * mySPI;

#endif

SPISettings _spi_settings;

#if defined(ESP32)

bool _useHSPI = true;

#elif defined(ARDUINO_ARCH_RP2040)

bool _useSPI1 = false;

#endif
};


///////////////////////////////////////////////////////////////
//
// 4800 Series
// MCP4800 Series
//
class MCP4801 : public MCP_DAC
{
Expand Down Expand Up @@ -160,7 +190,7 @@ class MCP4822 : public MCP_DAC

///////////////////////////////////////////////////////////////
//
// 4900 Series
// MCP4900 Series
//
class MCP4901 : public MCP_DAC
{
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,48 @@ SPI0 and SPI1 are used to access flash memory. SPI2 and SPI3 are "user" SPI cont
| not used | MISO = 12 | MISO = 19 |



### RP2040 specific

Select SPI bus on which the Device is on. Both need to be called before the **begin()** function. If the function is called after the **begin()** function, changes will only apply if the **end()** and then the **begin()** functions are called

- **void selectSPI()** Select the SPI bus (Standard, does not need to be called)
- **void selectSPI1()** Select the SPI1 bus

- **bool usesSPI()** returns true if SPI is used
- **bool usesSPI1()** returns true if SPI1 is used

#### experimental

- **void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)** overrule GPIO pins of RP2040 for different SPI pins. needs to be called
AFTER the **begin()** function. Selcted pins must match the RP2040 pinout!


```cpp
void setup()
{
MCP.selectSPI(); //use for SPI / SPI0
MCP.selectSPI1(); //use for SPI1
MCP.begin(17);
MCP.setGPIOpins(CLK, MISO, MOSI, SELECT); // SELECT should match the param of begin()
}
```

#### Pico connections to MCP4922 (example)

The RP2040 has **two** SPI peripherals from which two can be used.

SPI (SPI0) and SPI1 can both be usd to connect devices


| MCP4922 | SPI / SPI0 | SPI1 |
|:--------:|:-------------:|:-------------:|
| CS | SELECT = 17 | SELECT = 13 |
| SCK | SCLK = 18 | SCLK = 14 |
| SDI | MOSI = 19 | MOSI = 15 |
| not used | MISO = 16 | MISO = 12 |


## Future

- test test test and ....
Expand Down
27 changes: 27 additions & 0 deletions examples/MCP4921_wave_generator_RP2040/.arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -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
Loading