Implement UART HAL (#212)

- Implement UART HAL
- Improved I2C and SPI HAL mocking mechanism (removed mock files)
This commit is contained in:
Ken Van Hoeylandt
2025-02-08 23:10:31 +01:00
committed by GitHub
parent e1bfdd7c91
commit c5fc8790bb
15 changed files with 436 additions and 187 deletions
+42 -8
View File
@@ -1,11 +1,11 @@
#ifdef ESP_PLATFORM
#include "Tactility/hal/i2c/I2c.h"
#include <Tactility/Log.h>
#include <Tactility/Mutex.h>
#ifdef ESP_PLATFORM
#include <esp_check.h>
#endif // ESP_PLATFORM
#define TAG "i2c"
@@ -41,17 +41,21 @@ static void printInfo(const Data& data) {
TT_LOG_V(TAG, " initMode: %s", initModeToString(data.configuration.initMode));
TT_LOG_V(TAG, " canReinit: %d", data.configuration.canReinit);
TT_LOG_V(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) {
#ifdef ESP_PLATFORM
if (configuration.config.mode != I2C_MODE_MASTER) {
TT_LOG_E(TAG, "Currently only master mode is supported");
return false;
}
#endif // ESP_PLATFORM
Data& data = dataArray[configuration.port];
data.configuration = configuration;
data.isConfigured = true;
@@ -111,6 +115,7 @@ static bool startLocked(i2c_port_t port) {
return false;
}
#ifdef ESP_PLATFORM
esp_err_t result = i2c_param_config(port, &config.config);
if (result != ESP_OK) {
TT_LOG_E(TAG, "(%d) Starting: Failed to configure: %s", port, esp_err_to_name(result));
@@ -121,9 +126,10 @@ static bool startLocked(i2c_port_t port) {
if (result != ESP_OK) {
TT_LOG_E(TAG, "(%d) Starting: Failed to install driver: %s", port, esp_err_to_name(result));
return false;
} else {
data.isStarted = true;
}
#endif // ESP_PLATFORM
data.isStarted = true;
TT_LOG_I(TAG, "(%d) Started", port);
return true;
@@ -154,13 +160,15 @@ static bool stopLocked(i2c_port_t port) {
return false;
}
#ifdef ESP_PLATFORM
esp_err_t result = i2c_driver_delete(port);
if (result != ESP_OK) {
TT_LOG_E(TAG, "(%d) Stopping: Failed to delete driver: %s", port, esp_err_to_name(result));
return false;
} else {
data.isStarted = false;
}
#endif // ESP_PLATFORM
data.isStarted = false;
TT_LOG_I(TAG, "(%d) Stopped", port);
return true;
@@ -189,6 +197,7 @@ bool isStarted(i2c_port_t port) {
}
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout) {
#ifdef ESP_PLATFORM
if (lock(port)) {
// TODO: We're passing an inaccurate timeout value as we already lost time with locking and previous writes in this loop
esp_err_t result = i2c_master_read_from_device(port, address, data, dataSize, timeout);
@@ -198,9 +207,13 @@ bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
return false;
}
#else
return false;
#endif // ESP_PLATFORM
}
bool masterReadRegister(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, size_t dataSize, TickType_t timeout) {
#ifdef ESP_PLATFORM
if (!lock(port)) {
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
return false;
@@ -229,9 +242,13 @@ bool masterReadRegister(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t*
ESP_ERROR_CHECK_WITHOUT_ABORT(result);
return result == ESP_OK;
#else
return false;
#endif // ESP_PLATFORM
}
bool masterWrite(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
#ifdef ESP_PLATFORM
if (lock(port)) {
// TODO: We're passing an inaccurate timeout value as we already lost time with locking
esp_err_t result = i2c_master_write_to_device(port, address, data, dataSize, timeout);
@@ -241,9 +258,13 @@ bool masterWrite(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
return false;
}
#else
return false;
#endif // ESP_PLATFORM
}
bool masterWriteRegister(i2c_port_t port, uint8_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
#ifdef ESP_PLATFORM
tt_check(reg != 0);
if (!lock(port)) {
@@ -266,9 +287,13 @@ bool masterWriteRegister(i2c_port_t port, uint8_t address, uint8_t reg, const ui
ESP_ERROR_CHECK_WITHOUT_ABORT(result);
return result == ESP_OK;
#else
return false;
#endif // ESP_PLATFORM
}
bool masterWriteRegisterArray(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
#ifdef ESP_PLATFORM
assert(dataSize % 2 == 0);
bool result = true;
for (int i = 0; i < dataSize; i += 2) {
@@ -278,9 +303,13 @@ bool masterWriteRegisterArray(i2c_port_t port, uint8_t address, const uint8_t* d
}
}
return result;
#else
return false;
#endif // ESP_PLATFORM
}
bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData, size_t writeDataSize, uint8_t* readData, size_t readDataSize, TickType_t timeout) {
#ifdef ESP_PLATFORM
if (lock(port)) {
// TODO: We're passing an inaccurate timeout value as we already lost time with locking
esp_err_t result = i2c_master_write_read_device(port, address, writeData, writeDataSize, readData, readDataSize, timeout);
@@ -290,9 +319,13 @@ bool masterWriteRead(i2c_port_t port, uint8_t address, const uint8_t* writeData,
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
return false;
}
#else
return false;
#endif // ESP_PLATFORM
}
bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeout) {
#ifdef ESP_PLATFORM
if (lock(port)) {
uint8_t message[2] = { 0, 0 };
// TODO: We're passing an inaccurate timeout value as we already lost time with locking
@@ -303,6 +336,9 @@ bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeo
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
return false;
}
#else
return false;
#endif // ESP_PLATFORM
}
bool lock(i2c_port_t port, TickType_t timeout) {
@@ -314,5 +350,3 @@ bool unlock(i2c_port_t port) {
}
} // namespace
#endif