forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: SPIPortManager allows simultaneous use of CMT2300 and Huawei…
… charger * backport SPIPortManager from @skippermeister * adapt to support ESP32 and ESP32-S3 and ESP32-C3 * use logic to work with SPI numbering as in the official documentation: start with SPI0 and go up to SPI3 * increase Huawei CAN controller stack size to 2000 * increase startup delay for USB_CDC enabled builds to be able to catch bootlogs over USB
- Loading branch information
1 parent
5162466
commit df53f34
Showing
15 changed files
with
156 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#pragma once | ||
|
||
#include <array> | ||
#include <optional> | ||
#include <string> | ||
#include <driver/spi_master.h> | ||
|
||
/** | ||
* SPI# to SPI ID and SPI_HOST mapping | ||
* | ||
* ESP32 | ||
* | SPI # | SPI ID | SPI_HOST | | ||
* | 2 | 2 | 1 | | ||
* | 3 | 3 | 2 | | ||
* | ||
* ESP32-S3 | ||
* | SPI # | SPI ID | SPI_HOST | | ||
* | 2 | 0 | 1 | | ||
* | 3 | 1 | 2 | | ||
* | ||
* ESP32-C3 | ||
* | SPI # | SPI ID | SPI_HOST | | ||
* | 2 | 0 | 1 | | ||
* | ||
*/ | ||
|
||
class SPIPortManagerClass { | ||
public: | ||
void init(); | ||
|
||
std::optional<uint8_t> allocatePort(std::string const& owner); | ||
void freePort(std::string const& owner); | ||
spi_host_device_t SPIhostNum(uint8_t spi_num); | ||
|
||
private: | ||
// the amount of SPIs available on supported ESP32 chips | ||
#if CONFIG_IDF_TARGET_ESP32 || 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 = -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 = 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 | ||
|
||
std::array<std::string, _num_controllers> _ports = { "" }; | ||
}; | ||
|
||
extern SPIPortManagerClass SPIPortManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#include "SPIPortManager.h" | ||
#include "MessageOutput.h" | ||
|
||
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"; | ||
} | ||
|
||
std::optional<uint8_t> SPIPortManagerClass::allocatePort(std::string const& owner) | ||
{ | ||
for (size_t i = 0; i < _ports.size(); ++i) { | ||
if (_ports[i] != "") { | ||
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, i, owner.c_str()); | ||
|
||
return i + _offset_spi_num; | ||
} | ||
|
||
MessageOutput.printf("%s Cannot assign another SPI port to '%s'\r\n", TAG, owner.c_str()); | ||
return std::nullopt; | ||
} | ||
|
||
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 + _offset_spi_num, owner.c_str()); | ||
_ports[i] = ""; | ||
} | ||
} | ||
|
||
spi_host_device_t SPIPortManagerClass::SPIhostNum(uint8_t spi_num) | ||
{ | ||
return (spi_host_device_t)(spi_num + _offset_spi_host); | ||
} |
Oops, something went wrong.