Simplify I2C and SPI code (#237)

This commit is contained in:
Ken Van Hoeylandt
2025-02-26 17:47:19 +01:00
committed by GitHub
parent b85ef7a2e7
commit 83a82be901
16 changed files with 46 additions and 133 deletions
@@ -24,10 +24,11 @@ struct Configuration {
i2c_port_t port;
/** Whether this bus should be initialized when device starts up */
InitMode initMode;
/** Whether this bus can stopped and re-started. */
bool canReinit;
/** Whether configuration can be changed. */
bool hasMutableConfiguration;
/**
* Whether this bus can be changed after booting.
* If the bus is internal and/or used for core features like touch screen, then it can be declared static.
*/
bool isMutable;
/** Configuration that must be valid when initAtBoot is set to true. */
i2c_config_t config;
};
@@ -40,8 +41,7 @@ enum class Status {
/**
* Reconfigure a port with the provided settings.
* @warning This fails when the HAL Configuration does not allow for reinit.
* @warning This fails when the HAL Configuration does not allow for mutation of the device.
* @warning This fails when the HAL Configuration is not mutable.
* @param[in] port the port to reconfigure
* @param[in] configuration the new configuration
* @return true on success
@@ -21,10 +21,8 @@ struct Configuration {
spi_bus_config_t config;
/** Whether this bus should be initialized when device starts up */
InitMode initMode;
/** Whether this bus can stopped and re-started. */
bool canReinit;
/** Whether configuration can be changed. */
bool hasMutableConfiguration;
bool isMutable;
/** Optional custom lock */
std::shared_ptr<Lock> _Nullable lock;
};
+4 -32
View File
@@ -21,32 +21,6 @@ struct Data {
static const uint8_t ACK_CHECK_EN = 1;
static Data dataArray[I2C_NUM_MAX];
static const char* toString(InitMode mode) {
switch (mode) {
using enum InitMode;
case ByTactility:
return TT_STRINGIFY(InitMode::ByTactility);
case ByExternal:
return TT_STRINGIFY(InitMode::ByExternal);
case Disabled:
return TT_STRINGIFY(InitMode::Disabled);
}
tt_crash("not implemented");
}
static void printInfo(const Data& data) {
TT_LOG_D(TAG, "I2C info for port %d", data.configuration.port);
TT_LOG_D(TAG, " isStarted: %d", data.isStarted);
TT_LOG_D(TAG, " isConfigured: %d", data.isConfigured);
TT_LOG_D(TAG, " initMode: %s", toString(data.configuration.initMode));
TT_LOG_D(TAG, " canReinit: %d", data.configuration.canReinit);
TT_LOG_D(TAG, " hasMutableConfiguration: %d", data.configuration.hasMutableConfiguration);
#ifdef ESP_PLATFORM
TT_LOG_V(TAG, " SDA pin: %d", data.configuration.config.sda_io_num);
TT_LOG_V(TAG, " SCL pin: %d", data.configuration.config.scl_io_num);
#endif // ESP_PLATFORM
}
bool init(const std::vector<i2c::Configuration>& configurations) {
TT_LOG_I(TAG, "Init");
for (const auto& configuration: configurations) {
@@ -62,7 +36,6 @@ bool init(const std::vector<i2c::Configuration>& configurations) {
}
for (const auto& config: configurations) {
printInfo(dataArray[config.port]);
if (config.initMode == InitMode::ByTactility) {
if (!start(config.port)) {
return false;
@@ -83,8 +56,8 @@ bool configure(i2c_port_t port, const i2c_config_t& configuration) {
if (data.isStarted) {
TT_LOG_E(TAG, "(%d) Cannot reconfigure while interface is started", port);
return false;
} else if (!data.configuration.hasMutableConfiguration) {
TT_LOG_E(TAG, "(%d) Mutation not allowed by original configuration", port);
} else if (!data.configuration.isMutable) {
TT_LOG_E(TAG, "(%d) Mutation not allowed because configuration is immutable", port);
return false;
} else {
data.configuration.config = configuration;
@@ -97,7 +70,6 @@ bool start(i2c_port_t port) {
lock.lock();
Data& data = dataArray[port];
printInfo(data);
Configuration& config = data.configuration;
if (data.isStarted) {
@@ -137,8 +109,8 @@ bool stop(i2c_port_t port) {
Data& data = dataArray[port];
Configuration& config = data.configuration;
if (!config.canReinit) {
TT_LOG_E(TAG, "(%d) Stopping: Not allowed to re-init", port);
if (!config.isMutable) {
TT_LOG_E(TAG, "(%d) Stopping: Not allowed for immutable configuration", port);
return false;
}
+3 -30
View File
@@ -15,31 +15,6 @@ struct Data {
static Data dataArray[SPI_HOST_MAX];
static const char* toString(InitMode mode) {
switch (mode) {
using enum InitMode;
case ByTactility:
return TT_STRINGIFY(InitMode::ByTactility);
case ByExternal:
return TT_STRINGIFY(InitMode::ByExternal);
case Disabled:
return TT_STRINGIFY(InitMode::Disabled);
}
tt_crash("not implemented");
}
static void printInfo(const Data& data) {
TT_LOG_D(TAG, "SPI info for device %d", data.configuration.device);
TT_LOG_D(TAG, " isStarted: %d", data.isStarted);
TT_LOG_D(TAG, " isConfigured: %d", data.isConfigured);
TT_LOG_D(TAG, " initMode: %s", toString(data.configuration.initMode));
TT_LOG_D(TAG, " canReinit: %d", data.configuration.canReinit);
TT_LOG_D(TAG, " hasMutableConfiguration: %d", data.configuration.hasMutableConfiguration);
TT_LOG_D(TAG, " MISO pin: %d", data.configuration.config.miso_io_num);
TT_LOG_D(TAG, " MOSI pin: %d", data.configuration.config.mosi_io_num);
TT_LOG_D(TAG, " SCLK pin: %d", data.configuration.config.sclk_io_num);
}
bool init(const std::vector<spi::Configuration>& configurations) {
TT_LOG_I(TAG, "Init");
for (const auto& configuration: configurations) {
@@ -54,7 +29,6 @@ bool init(const std::vector<spi::Configuration>& configurations) {
}
for (const auto& config: configurations) {
printInfo(dataArray[config.device]);
if (config.initMode == InitMode::ByTactility) {
if (!start(config.device)) {
return false;
@@ -75,7 +49,7 @@ bool configure(spi_host_device_t device, const spi_bus_config_t& configuration)
if (data.isStarted) {
TT_LOG_E(TAG, "(%d) Cannot reconfigure while interface is started", device);
return false;
} else if (!data.configuration.hasMutableConfiguration) {
} else if (!data.configuration.isMutable) {
TT_LOG_E(TAG, "(%d) Mutation not allowed by original configuration", device);
return false;
} else {
@@ -89,7 +63,6 @@ bool start(spi_host_device_t device) {
lock.lock();
Data& data = dataArray[device];
printInfo(data);
if (data.isStarted) {
TT_LOG_E(TAG, "(%d) Starting: Already started", device);
@@ -129,8 +102,8 @@ bool stop(spi_host_device_t device) {
Data& data = dataArray[device];
Configuration& config = data.configuration;
if (!config.canReinit) {
TT_LOG_E(TAG, "(%d) Stopping: Not allowed to re-init", device);
if (!config.isMutable) {
TT_LOG_E(TAG, "(%d) Stopping: Not allowed, immutable", device);
return false;
}