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

View File

@ -41,7 +41,7 @@ ContinuationIndentWidth: 4
EmptyLineBeforeAccessModifier: Always
EmptyLineAfterAccessModifier: Always
IndentCaseLabels: true
IndentPPDirectives: BeforeHash
IndentPPDirectives: None
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 2

View File

@ -80,5 +80,32 @@ extern const tt::hal::Configuration lilygo_tdeck = {
.hasMutableConfiguration = false,
.lock = tt::lvgl::getLvglSyncLockable() // esp_lvgl_port owns the lock for the display
}
},
.uart {
tt::hal::uart::Configuration {
.port = UART_NUM_0,
.initMode = tt::hal::uart::InitMode::ByTactility,
.canReinit = false,
.hasMutableConfiguration = false,
.rxPin = GPIO_NUM_44,
.txPin = GPIO_NUM_43,
.rtsPin = GPIO_NUM_NC,
.ctsPin = GPIO_NUM_NC,
.rxBufferSize = 1024,
.txBufferSize = 1024,
.config = {
.baud_rate = 9600,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = 0,
.source_clk = UART_SCLK_DEFAULT,
.flags = {
.allow_pd = 0,
.backup_before_sleep = 0,
}
}
}
}
};

View File

@ -1,4 +1,6 @@
# TODOs
- Start using non_null (either via MS GSL, or custom)
- `hal/Configuration.h` defines C function types: Use C++ std::function instead
- Fix system time to not be 1980 (use build year as minimum)
- Use std::span or string_view in StringUtils https://youtu.be/FRkJCvHWdwQ?t=2754
- Fix bug in T-Deck/etc: esp_lvgl_port settings has a large stack size (~9kB) to fix an issue where the T-Deck would get a stackoverflow. This sometimes happens when WiFi is auto-enabled and you open the app while it is still connecting.

View File

@ -3,6 +3,7 @@
#include "./SdCard.h"
#include "./i2c/I2c.h"
#include "Tactility/hal/spi/Spi.h"
#include "Tactility/hal/uart/Uart.h"
namespace tt::hal {
@ -47,6 +48,9 @@ struct Configuration {
/** A list of SPI interfaces */
const std::vector<spi::Configuration> spi = {};
/** A list of UART interfaces */
const std::vector<uart::Configuration> uart = {};
};
} // namespace

View File

@ -0,0 +1,7 @@
#pragma once
#ifdef ESP_PLATFORM
#include <driver/gpio.h>
#else
typedef unsigned int gpio_num_t;
#endif

View File

@ -1,39 +1,38 @@
#pragma once
#ifdef ESP_PLATFORM
#include <hal/i2c_types.h>
#include <driver/i2c.h>
#else
#include <cstdint>
typedef int esp_err_t;
typedef enum {
enum i2c_port_t {
I2C_NUM_0 = 0,
I2C_NUM_1,
LP_I2C_NUM_0,
I2C_NUM_MAX,
} i2c_port_t;
};
typedef enum{
enum i2c_mode_t {
I2C_MODE_MASTER,
I2C_MODE_MAX,
} i2c_mode_t;
};
typedef struct {
struct i2c_config_t {
i2c_mode_t mode;
int sda_io_num;
int scl_io_num;
bool sda_pullup_en;
bool scl_pullup_en;
union {
struct {
uint32_t clk_speed;
} master;
};
uint32_t clk_flags;
} i2c_config_t;
};
#endif

View File

@ -1,58 +1,19 @@
#pragma once
#include "../Gpio.h"
#ifdef ESP_PLATFORM
#include <driver/spi_common.h>
#else
#include <cstdint>
enum spi_host_device_t {
SPI1_HOST = 0,
SPI2_HOST = 1,
SPI3_HOST = 2,
SPI_HOST_MAX,
};
enum spi_common_dma_t {
SPI_DMA_DISABLED = 0, ///< No DMA
SPI_DMA_CH1 = 1, ///< DMA, select DMA Channel 1
SPI_DMA_CH2 = 2, ///< DMA, select DMA Channel 2
SPI_DMA_CH_AUTO = 3, ///< DMA, channel selected by driver
};
enum esp_intr_cpu_affinity_t {
ESP_INTR_CPU_AFFINITY_AUTO,
ESP_INTR_CPU_AFFINITY_0,
ESP_INTR_CPU_AFFINITY_1,
};
#define SPI_HOST_MAX 3
typedef int spi_host_device_t;
struct spi_bus_config_t {
union {
int mosi_io_num;
int data0_io_num;
};
union {
int miso_io_num;
int data1_io_num;
};
int sclk_io_num;
union {
int quadwp_io_num;
int data2_io_num;
};
union {
int quadhd_io_num;
int data3_io_num;
};
int data4_io_num;
int data5_io_num;
int data6_io_num;
int data7_io_num;
bool data_io_default_level;
int max_transfer_sz;
uint32_t flags;
esp_intr_cpu_affinity_t isr_cpu_id;
int intr_flags;
gpio_num_t miso_io_num;
gpio_num_t mosi_io_num;
gpio_num_t sclk_io_num;
};
struct spi_common_dma_t {};
#endif

View File

@ -0,0 +1,61 @@
#pragma once
#include <Tactility/RtosCompat.h>
#include "UartCompat.h"
#include "../Gpio.h"
#include <vector>
#include <memory>
namespace tt::hal::uart {
enum class InitMode {
ByTactility, // Tactility will initialize it in the correct bootup phase
ByExternal, // The device is already initialized and Tactility should assume it works
Disabled // Not initialized by default
};
struct Configuration {
uart_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 .config can be changed. */
bool hasMutableConfiguration;
/** Receive GPIO pin */
gpio_num_t rxPin;
/** Transmit GPIO pin */
gpio_num_t txPin;
/** Read-To-Send GPIO pin */
gpio_num_t rtsPin;
/** Clear-To-Send Send GPIO pin */
gpio_num_t ctsPin;
/** Receive buffer size in bytes */
unsigned int rxBufferSize;
/** Transmit buffer size in bytes */
unsigned int txBufferSize;
/** Native configuration */
uart_config_t config;
};
enum class Status {
Started,
Stopped,
Unknown
};
bool init(const std::vector<uart::Configuration>& configurations);
bool start(uart_port_t port);
bool stop(uart_port_t port);
bool isStarted(uart_port_t port);
bool lock(uart_port_t port, TickType_t timeout = 10 / portTICK_PERIOD_MS);
bool unlock(uart_port_t port);
size_t read(uart_port_t port, uint8_t* buffer, size_t bufferSize, TickType_t timeout = 10 / portTICK_PERIOD_MS);
size_t write(uart_port_t port, const uint8_t* buffer, size_t bufferSize, TickType_t timeout = 10 / portTICK_PERIOD_MS);
} // namespace tt::hal::uart

View File

@ -0,0 +1,17 @@
#pragma once
#ifdef ESP_PLATFORM
#include <driver/uart.h>
#include <driver/gpio.h>
#include <hal/uart_types.h>
#else
#define UART_NUM_MAX 3
typedef int uart_port_t;
typedef struct {
} uart_config_t;
#endif

View File

@ -11,6 +11,8 @@ enum class SystemEvent {
BootInitI2cEnd,
BootInitSpiBegin,
BootInitSpiEnd,
BootInitUartBegin,
BootInitUartEnd,
BootInitLvglBegin,
BootInitLvglEnd,
BootSplash,

View File

@ -1,7 +1,8 @@
#include "Tactility/hal/Configuration.h"
#include "Tactility/hal/Device.h"
#include "Tactility/hal/Hal_i.h"
#include "Tactility/hal/i2c/I2c.h"
#include "Tactility/hal/spi/Spi.h"
#include "Tactility/hal/uart/Uart.h"
#include "Tactility/hal/Power.h"
#include <Tactility/kernel/SystemEvents.h>
@ -21,6 +22,10 @@ void init(const Configuration& configuration) {
tt_check(spi::init(configuration.spi), "SPI init failed");
kernel::systemEventPublish(kernel::SystemEvent::BootInitSpiEnd);
kernel::systemEventPublish(kernel::SystemEvent::BootInitUartBegin);
tt_check(uart::init(configuration.uart), "UART init failed");
kernel::systemEventPublish(kernel::SystemEvent::BootInitUartEnd);
if (configuration.initBoot != nullptr) {
TT_LOG_I(TAG, "Init power");
tt_check(configuration.initBoot(), "Init power failed");

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

View File

@ -1,121 +0,0 @@
#ifndef ESP_PLATFORM
/**
* This code is based on i2c_manager from https://github.com/ropg/i2c_manager/blob/master/i2c_manager/i2c_manager.c (original has MIT license)
*/
#include "Tactility/TactilityCore.h"
#include "Tactility/hal/i2c/I2c.h"
namespace tt::hal::i2c {
typedef struct Data {
Mutex mutex;
bool isConfigured = false;
bool isStarted = false;
Configuration configuration;
} Data;
static Data dataArray[I2C_NUM_MAX];
#define TAG "i2c"
bool init(const std::vector<i2c::Configuration>& configurations) {
TT_LOG_I(TAG, "Init");
for (const auto& configuration: configurations) {
Data& data = dataArray[configuration.port];
data.configuration = configuration;
data.isConfigured = true;
}
for (const auto& config: configurations) {
if (config.initMode == InitMode::ByTactility && !start(config.port)) {
return false;
} else if (config.initMode == InitMode::ByExternal) {
dataArray[config.port].isStarted = true;
}
}
return true;
}
static bool configureLocked(i2c_port_t port, const i2c_config_t& configuration) {
Data& data = dataArray[port];
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);
return false;
} else {
data.configuration.config = configuration;
return true;
}
}
bool configure(i2c_port_t port, const i2c_config_t& configuration) {
lock(port);
bool result = configureLocked(port, configuration);
unlock(port);
return result;
}
bool start(i2c_port_t port) {
lock(port);
dataArray[port].isStarted = true;
unlock(port);
return true;
}
bool stop(i2c_port_t port) {
lock(port);
dataArray[port].isStarted = false;
unlock(port);
return true;
}
bool isStarted(i2c_port_t port) {
lock(port);
bool started = dataArray[port].isStarted;
unlock(port);
return started;
}
bool lock(i2c_port_t port, TickType_t timeout) {
return dataArray[port].mutex.lock(timeout);
}
bool unlock(i2c_port_t port) {
return dataArray[port].mutex.unlock();
}
bool masterRead(i2c_port_t port, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout) {
return false;
}
bool masterReadRegister(i2c_port_t port, uint8_t address, uint8_t reg, uint8_t* data, size_t dataSize, TickType_t timeout) {
return false;
}
bool masterWrite(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
return false;
}
bool masterWriteRegister(i2c_port_t port, uint8_t address, uint8_t reg, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
return false;
}
bool masterWriteRegisterArray(i2c_port_t port, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
return false;
}
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) {
return false;
}
bool masterHasDeviceAtAddress(i2c_port_t port, uint8_t address, TickType_t timeout) {
return (rand()) % 25 == 0;
}
} // namespace
#endif

View File

@ -0,0 +1,247 @@
#include "Tactility/hal/uart/Uart.h"
#include <Tactility/Log.h>
#include <Tactility/LogMessages.h>
#include <Tactility/Mutex.h>
#ifdef ESP_PLATFORM
#include <esp_check.h>
#endif
#define TAG "uart"
namespace tt::hal::uart {
struct Data {
Mutex mutex;
bool isConfigured = false;
bool isStarted = false;
Configuration configuration;
};
static Data dataArray[UART_NUM_MAX];
static const char* initModeToString(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_V(TAG, "UART info for port %d", data.configuration.port);
TT_LOG_V(TAG, " isStarted: %d", data.isStarted);
TT_LOG_V(TAG, " isConfigured: %d", data.isConfigured);
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);
TT_LOG_V(TAG, " RX pin: %d", data.configuration.rxPin);
TT_LOG_V(TAG, " TX pin: %d", data.configuration.txPin);
TT_LOG_V(TAG, " RTS pin: %d", data.configuration.rtsPin);
TT_LOG_V(TAG, " CTS pin: %d", data.configuration.ctsPin);
}
bool init(const std::vector<uart::Configuration>& configurations) {
TT_LOG_I(TAG, "Init");
for (const auto& configuration: configurations) {
Data& data = dataArray[configuration.port];
data.configuration = configuration;
data.isConfigured = true;
}
for (const auto& config: configurations) {
printInfo(dataArray[config.port]);
if (config.initMode == InitMode::ByTactility) {
if (!start(config.port)) {
return false;
}
} else if (config.initMode == InitMode::ByExternal) {
dataArray[config.port].isStarted = true;
}
}
return true;
}
static bool configureLocked(uart_port_t port, const uart_config_t& configuration) {
Data& data = dataArray[port];
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);
return false;
} else {
data.configuration.config = configuration;
return true;
}
}
bool configure(uart_port_t port, const uart_config_t& configuration) {
if (lock(port)) {
bool result = configureLocked(port, configuration);
unlock(port);
return result;
} else {
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
return false;
}
}
static bool startLocked(uart_port_t port) {
Data& data = dataArray[port];
printInfo(data);
if (data.isStarted) {
TT_LOG_E(TAG, "(%d) Starting: Already started", port);
return false;
}
if (!data.isConfigured) {
TT_LOG_E(TAG, "(%d) Starting: Not configured", port);
return false;
}
#ifdef ESP_PLATFORM
Configuration& config = data.configuration;
int intr_alloc_flags;
#if CONFIG_UART_ISR_IN_IRAM
intr_alloc_flags = ESP_INTR_FLAG_IRAM;
#else
intr_alloc_flags = 0;
#endif
esp_err_t result = uart_param_config(config.port, &config.config);
if (result != ESP_OK) {
TT_LOG_E(TAG, "(%d) Starting: Failed to configure: %s", port, esp_err_to_name(result));
return false;
}
result = uart_set_pin(config.port, config.txPin, config.rxPin, config.rtsPin, config.ctsPin);
if (result != ESP_OK) {
TT_LOG_E(TAG, "(%d) Starting: Failed set pins: %s", port, esp_err_to_name(result));
return false;
}
result = uart_driver_install(config.port, (int)config.rxBufferSize, (int)config.txBufferSize, 0, nullptr, intr_alloc_flags);
if (result != ESP_OK) {
TT_LOG_E(TAG, "(%d) Starting: Failed to install driver: %s", port, esp_err_to_name(result));
return false;
}
#endif // ESP_PLATFORM
data.isStarted = true;
TT_LOG_I(TAG, "(%d) Started", port);
return true;
}
bool start(uart_port_t port) {
if (lock(port)) {
bool result = startLocked(port);
unlock(port);
return result;
} else {
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
return false;
}
}
static bool stopLocked(uart_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);
return false;
}
if (!data.isStarted) {
TT_LOG_E(TAG, "(%d) Stopping: Not started", port);
return false;
}
#ifdef ESP_PLATFORM
esp_err_t result = uart_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;
}
#else
data.isStarted = true;
#endif // ESP_PLATFORM
TT_LOG_I(TAG, "(%d) Stopped", port);
return true;
}
bool stop(uart_port_t port) {
if (lock(port)) {
bool result = stopLocked(port);
unlock(port);
return result;
} else {
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
return false;
}
}
bool isStarted(uart_port_t port) {
if (lock(port, 50 / portTICK_PERIOD_MS)) {
bool started = dataArray[port].isStarted;
unlock(port);
return started;
} else {
// If we can't get a lock, we assume the device is busy and thus has started
return true;
}
}
bool lock(uart_port_t port, TickType_t timeout) {
return dataArray[port].mutex.lock(timeout);
}
bool unlock(uart_port_t port) {
return dataArray[port].mutex.unlock();
}
size_t read(uart_port_t port, uint8_t* buffer, size_t bufferSize, TickType_t timeout) {
#ifdef ESP_PLATFORM
auto start_time = kernel::getTicks();
if (lock(port, timeout)) {
auto lock_time = kernel::getTicks() - start_time;
auto remaining_timeout = std::max(timeout - lock_time, 0UL);
auto result = uart_read_bytes(port, buffer, bufferSize, remaining_timeout);
unlock(port);
return result;
} else {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "read()");
}
#endif // ESP_PLATFORM
return 0;
}
size_t write(uart_port_t port, const uint8_t* buffer, size_t bufferSize, TickType_t timeout) {
#ifdef ESP_PLATFORM
if (lock(port, timeout)) {
auto result = uart_write_bytes(port, buffer, bufferSize);
unlock(port);
return result;
} else {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "write()");
}
#endif // ESP_PLATFORM
return 0;
}
} // namespace tt::hal::uart

View File

@ -34,6 +34,10 @@ static const char* getEventName(SystemEvent event) {
return TT_STRINGIFY(BootInitSpiBegin);
case BootInitSpiEnd:
return TT_STRINGIFY(BootInitSpiEnd);
case BootInitUartBegin:
return TT_STRINGIFY(BootInitUartBegin);
case BootInitUartEnd:
return TT_STRINGIFY(BootInitUartEnd);
case BootInitLvglBegin:
return TT_STRINGIFY(BootInitLvglBegin);
case BootInitLvglEnd: