Skip to content

Commit

Permalink
Add HyperSPI support for RPi Pico (rp2040)
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Jul 23, 2023
1 parent 69da76c commit 3a89e06
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions sources/leddevice/dev_spi/LedDeviceAWA_spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ int LedDeviceAWA_spi::write(const std::vector<ColorRgb>& ledValues)
return writeBytesEsp8266(bufferLength, _ledBuffer.data());
else if (_spiType == "esp32")
return writeBytesEsp32(bufferLength, _ledBuffer.data());
else if (_spiType == "rp2040")
return writeBytesRp2040(bufferLength, _ledBuffer.data());
else
return writeBytes(bufferLength, _ledBuffer.data());
}
Expand Down
36 changes: 36 additions & 0 deletions sources/leddevice/dev_spi/ProviderSpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,42 @@ int ProviderSpi::writeBytesEsp8266(unsigned size, const uint8_t* data)
return retVal;
}

int ProviderSpi::writeBytesRp2040(unsigned size, const uint8_t* data)
{
// try to use constant buffer size similar to esp32
static const int REAL_BUFFER = 1536;
static const uint32_t BUFFER_SIZE = REAL_BUFFER + 8;

uint8_t* startData = (uint8_t*)data;
uint8_t* endData = (uint8_t*)data + size;
uint8_t buffer[BUFFER_SIZE];

if (_fid < 0)
{
return -1;
}

_spi.tx_buf = __u64(&buffer);
_spi.len = __u32(BUFFER_SIZE);
_spi.delay_usecs = 0;

int retVal = 0;

while (retVal >= 0 && startData < endData)
{
memset(buffer, 0, sizeof(buffer));
for (int i = 0; i < REAL_BUFFER && startData < endData; i++, startData++)
{
buffer[i] = *startData;
}
buffer[REAL_BUFFER] = 0xAA;
retVal = ioctl(_fid, SPI_IOC_MESSAGE(1), &_spi);
ErrorIf((retVal < 0), _log, "SPI failed to write. errno: %d, %s", errno, strerror(errno));
}

return retVal;
}

int ProviderSpi::writeBytesEsp32(unsigned size, const uint8_t* data)
{
static const int REAL_BUFFER = 1536;
Expand Down
3 changes: 3 additions & 0 deletions sources/leddevice/dev_spi/ProviderSpi.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public slots:
// esp32 spi packet protocol
int writeBytesEsp32(unsigned size, const uint8_t* data);

// rp2040 spi packet protocol
int writeBytesRp2040(unsigned size, const uint8_t* data);

/// The name of the output device
QString _deviceName;

Expand Down
4 changes: 2 additions & 2 deletions sources/leddevice/schemas/schema-awa_spi.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@
"spitype": {
"type": "string",
"title":"edt_conf_smooth_type_title",
"enum" : ["esp8266","esp32","standard"],
"enum" : ["esp8266","esp32","rp2040","standard"],
"options" : {
"enum_titles" : ["esp8266","esp32","standard"]
"enum_titles" : ["esp8266","esp32","Raspberry Pi Pico (rp2040)","standard"]
},
"default" : "esp8266",
"required" : true,
Expand Down

0 comments on commit 3a89e06

Please sign in to comment.