Skip to content

Commit

Permalink
change logic to work with SPI numbering as in the official documentation
Browse files Browse the repository at this point in the history
With this commit we start with SPI0 and go up to SPI3, just like in the documentation.
  • Loading branch information
AndreasBoehm committed Aug 12, 2024
1 parent db1df5b commit 1516e6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
10 changes: 7 additions & 3 deletions include/SPIPortManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ class SPIPortManagerClass {

private:
// the amount of SPIs available on supported ESP32 chips
static size_t constexpr _num_controllers = SOC_SPI_PERIPH_NUM - 1; // minus one because SPI1 can't be used
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
static size_t constexpr _num_controllers = 4;
#else
static size_t constexpr _num_controllers = 3;
#endif

#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
static int8_t constexpr _offset_spi_num = 0; // FSPI=0, HSPI=1
static int8_t constexpr _offset_spi_num = -2; // FSPI=0, HSPI=1
static int8_t constexpr _offset_spi_host = 1; // SPI1_HOST=0 but not usable, SPI2_HOST=1 and SPI3_HOST=2, first usable is SPI2_HOST
#else
static int8_t constexpr _offset_spi_num = 2; // HSPI=2, VSPI=3
static int8_t constexpr _offset_spi_num = 0; // HSPI=2, VSPI=3
static int8_t constexpr _offset_spi_host = -1; // SPI1_HOST=0 but not usable, SPI2_HOST=1 and SPI3_HOST=2, first usable is SPI2_HOST
#endif

Expand Down
14 changes: 8 additions & 6 deletions src/SPIPortManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@
SPIPortManagerClass SPIPortManager;
static constexpr char TAG[] = "[SPIPortManager]";

void SPIPortManagerClass::init() {}
void SPIPortManagerClass::init() {
MessageOutput.printf("%s SPI0 and SPI1 reserved by 'Flash and PSRAM'\r\n", TAG);
_ports[0] = "Flash";
_ports[1] = "PSRAM";
}

std::optional<uint8_t> SPIPortManagerClass::allocatePort(std::string const& owner)
{
for (size_t i = 0; i < _ports.size(); ++i) {
auto spiNum = i + _offset_spi_num;

if (_ports[i] != "") {
MessageOutput.printf("%s SPI%d already in use by '%s'\r\n", TAG, spiNum, _ports[i].c_str());
MessageOutput.printf("%s SPI%d already in use by '%s'\r\n", TAG, i, _ports[i].c_str());
continue;
}

_ports[i] = owner;

MessageOutput.printf("%s SPI%d now in use by '%s'\r\n", TAG, spiNum, owner.c_str());
MessageOutput.printf("%s SPI%d now in use by '%s'\r\n", TAG, i, owner.c_str());

return spiNum;
return i + _offset_spi_num;
}

MessageOutput.printf("%s Cannot assign another SPI port to '%s'\r\n", TAG, owner.c_str());
Expand Down

0 comments on commit 1516e6a

Please sign in to comment.