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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 46 additions and 133 deletions

View File

@ -22,8 +22,7 @@ const tt::hal::Configuration cyd_2432S024c_config = {
.name = "First",
.port = I2C_NUM_0,
.initMode = tt::hal::i2c::InitMode::ByTactility,
.canReinit = true,
.hasMutableConfiguration = true,
.isMutable = true,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_33,
@ -40,8 +39,7 @@ const tt::hal::Configuration cyd_2432S024c_config = {
.name = "Second",
.port = I2C_NUM_1,
.initMode = tt::hal::i2c::InitMode::Disabled,
.canReinit = true,
.hasMutableConfiguration = true,
.isMutable = true,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_NC,
@ -76,8 +74,7 @@ const tt::hal::Configuration cyd_2432S024c_config = {
.intr_flags = 0
},
.initMode = tt::hal::spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = tt::lvgl::getSyncLock() // esp_lvgl_port owns the lock for the display
},
tt::hal::spi::Configuration {
@ -100,8 +97,7 @@ const tt::hal::Configuration cyd_2432S024c_config = {
.intr_flags = 0
},
.initMode = tt::hal::spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = nullptr
},
}

View File

@ -25,8 +25,7 @@ extern const Configuration crowpanel_advance_28 = {
.name = "Main",
.port = I2C_NUM_0,
.initMode = i2c::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_15,
@ -62,8 +61,7 @@ extern const Configuration crowpanel_advance_28 = {
.intr_flags = 0
},
.initMode = spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = tt::lvgl::getSyncLock() // esp_lvgl_port owns the lock for the display
},
// SD card
@ -87,8 +85,7 @@ extern const Configuration crowpanel_advance_28 = {
.intr_flags = 0
},
.initMode = spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = nullptr // No custom lock needed
}
},

View File

@ -25,8 +25,7 @@ extern const Configuration crowpanel_advance_35 = {
.name = "Main",
.port = I2C_NUM_0,
.initMode = i2c::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_15,
@ -62,8 +61,7 @@ extern const Configuration crowpanel_advance_35 = {
.intr_flags = 0
},
.initMode = spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = tt::lvgl::getSyncLock() // esp_lvgl_port owns the lock for the display
},
// SD card
@ -87,8 +85,7 @@ extern const Configuration crowpanel_advance_35 = {
.intr_flags = 0
},
.initMode = spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = nullptr // No custom lock needed
}
},

View File

@ -27,8 +27,7 @@ extern const Configuration crowpanel_basic_28 = {
.name = "Main",
.port = I2C_NUM_0,
.initMode = i2c::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_22,
@ -64,8 +63,7 @@ extern const Configuration crowpanel_basic_28 = {
.intr_flags = 0
},
.initMode = spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = tt::lvgl::getSyncLock() // esp_lvgl_port owns the lock for the display
},
// SD card
@ -89,8 +87,7 @@ extern const Configuration crowpanel_basic_28 = {
.intr_flags = 0
},
.initMode = spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = nullptr // No custom lock needed
}
},

View File

@ -27,8 +27,7 @@ extern const Configuration crowpanel_basic_35 = {
.name = "Main",
.port = I2C_NUM_0,
.initMode = i2c::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_22,
@ -64,8 +63,7 @@ extern const Configuration crowpanel_basic_35 = {
.intr_flags = 0
},
.initMode = spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = tt::lvgl::getSyncLock() // esp_lvgl_port owns the lock for the display
},
// SD card
@ -89,8 +87,7 @@ extern const Configuration crowpanel_basic_35 = {
.intr_flags = 0
},
.initMode = spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = nullptr // No custom lock needed
}
},

View File

@ -24,8 +24,7 @@ extern const Configuration lilygo_tdeck = {
.name = "Internal",
.port = I2C_NUM_0,
.initMode = i2c::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_18,
@ -42,8 +41,7 @@ extern const Configuration lilygo_tdeck = {
.name = "External",
.port = I2C_NUM_1,
.initMode = i2c::InitMode::Disabled,
.canReinit = true,
.hasMutableConfiguration = true,
.isMutable = true,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_43,
@ -78,8 +76,7 @@ extern const Configuration lilygo_tdeck = {
.intr_flags = 0
},
.initMode = spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = tt::lvgl::getSyncLock() // esp_lvgl_port owns the lock for the display
}
},

View File

@ -19,8 +19,7 @@ extern const tt::hal::Configuration m5stack_core2 = {
.name = "Internal",
.port = I2C_NUM_0,
.initMode = tt::hal::i2c::InitMode::ByTactility,
.canReinit = false, // Might be set to try after trying out what it does AXP and screen
.hasMutableConfiguration = false,
.isMutable = false,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_21,
@ -37,8 +36,7 @@ extern const tt::hal::Configuration m5stack_core2 = {
.name = "External", // (Grove)
.port = I2C_NUM_1,
.initMode = tt::hal::i2c::InitMode::ByTactility,
.canReinit = true,
.hasMutableConfiguration = true,
.isMutable = true,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_32,
@ -73,8 +71,7 @@ extern const tt::hal::Configuration m5stack_core2 = {
.intr_flags = 0
},
.initMode = tt::hal::spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = tt::lvgl::getSyncLock() // esp_lvgl_port owns the lock for the display
}
}

View File

@ -18,8 +18,7 @@ const tt::hal::Configuration m5stack_cores3 = {
.name = "Internal",
.port = I2C_NUM_0,
.initMode = tt::hal::i2c::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_12,
@ -36,8 +35,7 @@ const tt::hal::Configuration m5stack_cores3 = {
.name = "External", // Grove
.port = I2C_NUM_1,
.initMode = tt::hal::i2c::InitMode::ByTactility,
.canReinit = true,
.hasMutableConfiguration = true,
.isMutable = true,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_2,
@ -72,8 +70,7 @@ const tt::hal::Configuration m5stack_cores3 = {
.intr_flags = 0
},
.initMode = tt::hal::spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = tt::lvgl::getSyncLock() // esp_lvgl_port owns the lock for the display
}
}

View File

@ -39,8 +39,7 @@ extern const Configuration hardware = {
.name = "Internal",
.port = I2C_NUM_0,
.initMode = i2c::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = 1,
@ -57,8 +56,7 @@ extern const Configuration hardware = {
.name = "External",
.port = I2C_NUM_1,
.initMode = i2c::InitMode::ByTactility,
.canReinit = true,
.hasMutableConfiguration = true,
.isMutable = true,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = 3,

View File

@ -20,8 +20,7 @@ extern const tt::hal::Configuration unPhone = {
.name = "Internal",
.port = I2C_NUM_0,
.initMode = tt::hal::i2c::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_3,
@ -38,8 +37,7 @@ extern const tt::hal::Configuration unPhone = {
.name = "Unused",
.port = I2C_NUM_1,
.initMode = tt::hal::i2c::InitMode::Disabled,
.canReinit = true,
.hasMutableConfiguration = true,
.isMutable = true,
.config = (i2c_config_t) {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_NC,
@ -74,8 +72,7 @@ extern const tt::hal::Configuration unPhone = {
.intr_flags = 0
},
.initMode = tt::hal::spi::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.isMutable = false,
.lock = tt::lvgl::getSyncLock() // esp_lvgl_port owns the lock for the display
}
}

View File

@ -239,7 +239,7 @@ void I2cScannerApp::onScanTimer() {
for (uint8_t address = 0; address < 128; ++address) {
if (hal::i2c::masterHasDeviceAtAddress(port, address, 10 / portTICK_PERIOD_MS)) {
TT_LOG_I(TAG, "Found device at address %d", address);
TT_LOG_I(TAG, "Found device at address 0x%02X", address);
if (!shouldStopScanTimer()) {
addAddressToList(address);
} else {

View File

@ -40,7 +40,7 @@ static void show(lv_obj_t* parent, const hal::i2c::Configuration& configuration)
// On-off switch
if (configuration.canReinit) {
if (configuration.isMutable) {
auto* state_switch = lv_switch_create(card);
lv_obj_align(state_switch, LV_ALIGN_TOP_RIGHT, 0, 0);

View File

@ -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

View File

@ -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;
};

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;
}

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;
}