Skip to content

Commit

Permalink
adapt changes to support ESP32 and ESP32-S3
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBoehm committed Jul 31, 2024
1 parent 67c299f commit 9abc7be
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
36 changes: 17 additions & 19 deletions include/SPIPortManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,33 @@
#include <optional>
#include <string>

/**
* SPI# to SPI ID and SPI_HOST mapping
*
* ESP32-S3
* | SPI # | SPI ID | SPI_HOST |
* | 0 | 0 | 0 |
* | 1 | 1 | 1 |
* | 2 | 3 | 2 |
*
* ESP32
* | SPI # | SPI ID | SPI_HOST |
* | 0 | 1 | 0 |
* | 1 | 2 | 1 |
* | 2 | 3 | 2 |
*/

class SPIPortManagerClass {
public:
void init();

std::optional<uint8_t> allocatePort(std::string const& owner);
void freePort(std::string const& owner);

#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
static int8_t constexpr _offset_spi_host = 1;
#else
static int8_t constexpr _offset_spi_host = -1;
#endif

private:
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
static int8_t constexpr _offset_spi_num = 0;
#else
static int8_t constexpr _offset_spi_num = 2;
#endif

// the amount of SPIs available on supported ESP32 chips
// TODO(AndreasBoehm): this does not work as expected on non S3 ESPs
static size_t constexpr _num_controllers = SOC_SPI_PERIPH_NUM;
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
static size_t constexpr _num_controllers = 2;
#else
static size_t constexpr _num_controllers = 1;
#endif

// TODO(AndreasBoehm): using a simple array like this does not work well when we want to support ESP32 and ESP32-S3
std::array<std::string, _num_controllers> _ports = { "" };
};

Expand Down
8 changes: 3 additions & 5 deletions src/InverterSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void InverterSettingsClass::init(Scheduler& scheduler)
const PinMapping_t& pin = PinMapping.get();

// Initialize inverter communication
MessageOutput.print("Initialize Hoymiles interface... ");
MessageOutput.println("Initialize Hoymiles interface... ");

Hoymiles.setMessageOutput(&MessageOutput);
Hoymiles.init();
Expand All @@ -34,7 +34,7 @@ void InverterSettingsClass::init(Scheduler& scheduler)
auto oSPInum = SPIPortManager.allocatePort("NRF24");

if (oSPInum) {
SPIClass* spiClass = new SPIClass(*oSPInum); // 0 gut, 1 gut, 2 schlecht
SPIClass* spiClass = new SPIClass(*oSPInum);
spiClass->begin(pin.nrf24_clk, pin.nrf24_miso, pin.nrf24_mosi, pin.nrf24_cs);
Hoymiles.initNRF(spiClass, pin.nrf24_en, pin.nrf24_irq);
}
Expand All @@ -44,9 +44,7 @@ void InverterSettingsClass::init(Scheduler& scheduler)
auto oSPInum = SPIPortManager.allocatePort("CMT2300A");

if (oSPInum) {
// TODO(AndreasBoehm): this only works on an ESP32-S3 like this, we need to find a different way to also make this work on an ESP32
// oSPInum + 1 to make it work
Hoymiles.initCMT(*oSPInum + 1, pin.cmt_sdio, pin.cmt_clk, pin.cmt_cs, pin.cmt_fcs, pin.cmt_gpio2, pin.cmt_gpio3);
Hoymiles.initCMT(*oSPInum + SPIPortManagerClass::_offset_spi_host, pin.cmt_sdio, pin.cmt_clk, pin.cmt_cs, pin.cmt_fcs, pin.cmt_gpio2, pin.cmt_gpio3);
MessageOutput.println(" Setting country mode... ");
Hoymiles.getRadioCmt()->setCountryMode(static_cast<CountryModeId_t>(config.Dtu.Cmt.CountryMode));
MessageOutput.println(" Setting CMT target frequency... ");
Expand Down
17 changes: 7 additions & 10 deletions src/SPIPortManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@
SPIPortManagerClass SPIPortManager;
static constexpr char TAG[] = "[SPIPortManager]";

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

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, i, _ports[i].c_str());
MessageOutput.printf("%s SPI%d already in use by '%s'\r\n", TAG, spiNum, _ports[i].c_str());
continue;
}

_ports[i] = owner;

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

return i;
return spiNum;
}

MessageOutput.printf("%s Cannot assign another SPI port to '%s'\r\n", TAG, owner.c_str());
Expand All @@ -36,7 +33,7 @@ void SPIPortManagerClass::freePort(std::string const& owner)
for (size_t i = 0; i < _ports.size(); ++i) {
if (_ports[i] != owner) { continue; }

MessageOutput.printf("%s Freeing SPI%d, owner was '%s'\r\n", TAG, i, owner.c_str());
MessageOutput.printf("%s Freeing SPI%d, owner was '%s'\r\n", TAG, i + _offset_spi_num, owner.c_str());
_ports[i] = "";
}
}

0 comments on commit 9abc7be

Please sign in to comment.