UART refactored (#236)
`Uart` is now an abstract class with a `UartEsp` and a `UartPosix` implementation.
This commit is contained in:
committed by
GitHub
parent
de46401d85
commit
b85ef7a2e7
@@ -1,226 +1,46 @@
|
||||
#include "Tactility/hal/uart/Uart.h"
|
||||
|
||||
#include <Tactility/Log.h>
|
||||
#include <Tactility/LogMessages.h>
|
||||
#include <Tactility/Mutex.h>
|
||||
|
||||
#include <sstream>
|
||||
#include <ranges>
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include <esp_check.h>
|
||||
#include "Tactility/hal/uart/UartEsp.h"
|
||||
#else
|
||||
#include "Tactility/hal/uart/UartPosix.h"
|
||||
#endif
|
||||
|
||||
#define TAG "uart"
|
||||
|
||||
namespace tt::hal::uart {
|
||||
|
||||
struct Data {
|
||||
Mutex mutex;
|
||||
bool isConfigured = false;
|
||||
bool isStarted = false;
|
||||
constexpr uint32_t uartIdNotInUse = 0;
|
||||
|
||||
struct UartEntry {
|
||||
uint32_t usageId = uartIdNotInUse;
|
||||
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_D(TAG, "UART 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", initModeToString(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, " RX pin: %d", data.configuration.rxPin);
|
||||
TT_LOG_D(TAG, " TX pin: %d", data.configuration.txPin);
|
||||
TT_LOG_D(TAG, " RTS pin: %d", data.configuration.rtsPin);
|
||||
TT_LOG_D(TAG, " CTS pin: %d", data.configuration.ctsPin);
|
||||
}
|
||||
static std::vector<UartEntry> uartEntries = {};
|
||||
static uint32_t lastUartId = uartIdNotInUse;
|
||||
|
||||
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;
|
||||
}
|
||||
uartEntries.push_back({
|
||||
.usageId = uartIdNotInUse,
|
||||
.configuration = configuration
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool configure(uart_port_t port, const uart_config_t& configuration) {
|
||||
auto lock = getLock(port).asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
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 start(uart_port_t port) {
|
||||
auto lock = getLock(port).asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
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 stop(uart_port_t port) {
|
||||
auto lock = getLock(port).asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
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;
|
||||
}
|
||||
#endif // ESP_PLATFORM
|
||||
|
||||
data.isStarted = false;
|
||||
|
||||
TT_LOG_I(TAG, "(%d) Stopped", port);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isStarted(uart_port_t port) {
|
||||
auto lock = getLock(port).asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
return dataArray[port].isStarted;
|
||||
}
|
||||
|
||||
Lock& getLock(uart_port_t port) {
|
||||
return dataArray[port].mutex;
|
||||
}
|
||||
|
||||
size_t readBytes(uart_port_t port, uint8_t* buffer, size_t bufferSize, TickType_t timeout) {
|
||||
auto lock = getLock(port).asScopedLock();
|
||||
if (!lock.lock(timeout)) {
|
||||
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
auto start_time = kernel::getTicks();
|
||||
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);
|
||||
return result;
|
||||
#endif // ESP_PLATFORM
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool readByte(uart_port_t port, uint8_t* output, TickType_t timeout) {
|
||||
return readBytes(port, output, 1, timeout) == 1;
|
||||
}
|
||||
|
||||
size_t writeBytes(uart_port_t port, const uint8_t* buffer, size_t bufferSize, TickType_t timeout) {
|
||||
auto lock = getLock(port).asScopedLock();
|
||||
if (!lock.lock(timeout)) {
|
||||
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
return uart_write_bytes(port, buffer, bufferSize);
|
||||
#endif // ESP_PLATFORM
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool writeString(uart_port_t port, const char* buffer, TickType_t timeout) {
|
||||
bool Uart::writeString(const char* buffer, TickType_t timeout) {
|
||||
while (*buffer != 0) {
|
||||
if (writeBytes(port, (const uint8_t*)buffer, 1, timeout)) {
|
||||
if (writeBytes(reinterpret_cast<const std::byte*>(buffer), 1, timeout)) {
|
||||
buffer++;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to write - breaking off");
|
||||
@@ -231,69 +51,12 @@ bool writeString(uart_port_t port, const char* buffer, TickType_t timeout) {
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t available(uart_port_t port, TickType_t timeout) {
|
||||
auto lock = getLock(port).asScopedLock();
|
||||
if (!lock.lock(timeout)) {
|
||||
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
size_t size = 0;
|
||||
uart_get_buffered_data_len(port, &size);
|
||||
return size;
|
||||
#else
|
||||
return 0;
|
||||
#endif // ESP_PLATFORM
|
||||
}
|
||||
|
||||
void flush(uart_port_t port) {
|
||||
#ifdef ESP_PLATFORM
|
||||
uart_flush(port);
|
||||
#endif // ESP_PLATFORM
|
||||
}
|
||||
|
||||
void flushInput(uart_port_t port) {
|
||||
#ifdef ESP_PLATFORM
|
||||
uart_flush_input(port);
|
||||
#endif // ESP_PLATFORM
|
||||
}
|
||||
|
||||
uint32_t getBaudRate(uart_port_t port) {
|
||||
#ifdef ESP_PLATFORM
|
||||
uint32_t baud_rate = 0;
|
||||
auto result = uart_get_baudrate(port, &baud_rate);
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(result);
|
||||
return baud_rate;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool setBaudRate(uart_port_t port, uint32_t baudRate, TickType_t timeout) {
|
||||
auto lock = getLock(port).asScopedLock();
|
||||
if (!lock.lock(timeout)) {
|
||||
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
auto result = uart_set_baudrate(port, baudRate);
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(result);
|
||||
return result == ESP_OK;
|
||||
#else
|
||||
return true;
|
||||
#endif // ESP_PLATFORM
|
||||
}
|
||||
|
||||
// #define DEBUG_READ_UNTIL
|
||||
|
||||
size_t readUntil(uart_port_t port, uint8_t* buffer, size_t bufferSize, uint8_t untilByte, TickType_t timeout, bool addNullTerminator) {
|
||||
size_t Uart::readUntil(std::byte* buffer, size_t bufferSize, uint8_t untilByte, TickType_t timeout, bool addNullTerminator) {
|
||||
TickType_t start_time = kernel::getTicks();
|
||||
uint8_t* buffer_write_ptr = buffer;
|
||||
uint8_t* buffer_limit = buffer + bufferSize - 1; // Keep 1 extra char as mull terminator
|
||||
auto* buffer_write_ptr = reinterpret_cast<uint8_t*>(buffer);
|
||||
uint8_t* buffer_limit = buffer_write_ptr + bufferSize - 1; // Keep 1 extra char as mull terminator
|
||||
TickType_t timeout_left = timeout;
|
||||
while (readByte(port, buffer_write_ptr, timeout_left) && buffer_write_ptr < buffer_limit) {
|
||||
while (readByte(reinterpret_cast<std::byte*>(buffer_write_ptr), timeout_left) && buffer_write_ptr < buffer_limit) {
|
||||
#ifdef DEBUG_READ_UNTIL
|
||||
// If first successful read and we're not receiving an empty response
|
||||
if (buffer_write_ptr == buffer && *buffer_write_ptr != 0x00U && *buffer_write_ptr != untilByte) {
|
||||
@@ -334,11 +97,52 @@ size_t readUntil(uart_port_t port, uint8_t* buffer, size_t bufferSize, uint8_t u
|
||||
}
|
||||
#endif
|
||||
|
||||
if (addNullTerminator && (buffer_write_ptr > buffer)) {
|
||||
if (addNullTerminator && (buffer_write_ptr > reinterpret_cast<uint8_t*>(buffer))) {
|
||||
return reinterpret_cast<size_t>(buffer_write_ptr) - reinterpret_cast<size_t>(buffer) - 1UL;
|
||||
} else {
|
||||
return reinterpret_cast<size_t>(buffer_write_ptr) - reinterpret_cast<size_t>(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<Uart> open(std::string name) {
|
||||
auto result = std::views::filter(uartEntries, [&name](auto& entry) {
|
||||
return entry.configuration.name == name;
|
||||
});
|
||||
|
||||
if (result.empty()) {
|
||||
TT_LOG_E(TAG, "UART not found: %s", name.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto& entry = *result.begin();
|
||||
if (entry.usageId != uartIdNotInUse) {
|
||||
TT_LOG_E(TAG, "UART in use: %s", name.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto uart = create(entry.configuration);
|
||||
assert(uart != nullptr);
|
||||
entry.usageId = uart->getId();
|
||||
return uart;
|
||||
}
|
||||
|
||||
void close(uint32_t uartId) {
|
||||
auto result = std::views::filter(uartEntries, [&uartId](auto& entry) {
|
||||
return entry.usageId == uartId;
|
||||
});
|
||||
|
||||
if (!result.empty()) {
|
||||
auto& entry = *result.begin();
|
||||
entry.usageId = uartIdNotInUse;
|
||||
} else {
|
||||
TT_LOG_W(TAG, "Auto-closing UART, but can't find it");
|
||||
}
|
||||
}
|
||||
|
||||
Uart::Uart() : id(++lastUartId) {}
|
||||
|
||||
Uart::~Uart() {
|
||||
close(getId());
|
||||
}
|
||||
|
||||
} // namespace tt::hal::uart
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include "Tactility/hal/uart/UartEsp.h"
|
||||
|
||||
#include <Tactility/Log.h>
|
||||
#include <Tactility/Mutex.h>
|
||||
|
||||
#include <sstream>
|
||||
#include <esp_check.h>
|
||||
|
||||
#define TAG "uart"
|
||||
|
||||
namespace tt::hal::uart {
|
||||
|
||||
bool UartEsp::start() {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
if (started) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Already started", configuration.port);
|
||||
return false;
|
||||
}
|
||||
|
||||
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(configuration.port, &configuration.config);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Failed to configure: %s", configuration.port, esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
result = uart_set_pin(configuration.port, configuration.txPin, configuration.rxPin, configuration.rtsPin, configuration.ctsPin);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Failed set pins: %s", configuration.port, esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
result = uart_driver_install(configuration.port, (int)configuration.rxBufferSize, (int)configuration.txBufferSize, 0, nullptr, intr_alloc_flags);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Failed to install driver: %s", configuration.port, esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
started = true;
|
||||
|
||||
TT_LOG_I(TAG, "(%d) Started", configuration.port);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UartEsp::stop() {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
if (!started) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Not started", configuration.port);
|
||||
return false;
|
||||
}
|
||||
|
||||
esp_err_t result = uart_driver_delete(configuration.port);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Failed to delete driver: %s", configuration.port, esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
started = false;
|
||||
|
||||
TT_LOG_I(TAG, "(%d) Stopped", configuration.port);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UartEsp::isStarted() const {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
return started;
|
||||
}
|
||||
|
||||
size_t UartEsp::readBytes(std::byte* buffer, size_t bufferSize, TickType_t timeout) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
if (!lock.lock(timeout)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto start_time = kernel::getTicks();
|
||||
auto lock_time = kernel::getTicks() - start_time;
|
||||
auto remaining_timeout = std::max(timeout - lock_time, 0UL);
|
||||
auto result = uart_read_bytes(configuration.port, buffer, bufferSize, remaining_timeout);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool UartEsp::readByte(std::byte* output, TickType_t timeout) {
|
||||
return readBytes(output, 1, timeout) == 1;
|
||||
}
|
||||
|
||||
size_t UartEsp::writeBytes(const std::byte* buffer, size_t bufferSize, TickType_t timeout) {
|
||||
if (!mutex.lock(timeout)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return uart_write_bytes(configuration.port, buffer, bufferSize);
|
||||
}
|
||||
|
||||
size_t UartEsp::available(TickType_t timeout) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
if (!lock.lock(timeout)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t size = 0;
|
||||
uart_get_buffered_data_len(configuration.port, &size);
|
||||
return size;
|
||||
}
|
||||
|
||||
void UartEsp::flushInput() {
|
||||
uart_flush_input(configuration.port);
|
||||
}
|
||||
|
||||
uint32_t UartEsp::getBaudRate() {
|
||||
uint32_t baud_rate = 0;
|
||||
auto result = uart_get_baudrate(configuration.port, &baud_rate);
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(result);
|
||||
return baud_rate;
|
||||
}
|
||||
|
||||
bool UartEsp::setBaudRate(uint32_t baudRate, TickType_t timeout) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
if (!lock.lock(timeout)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto result = uart_set_baudrate(configuration.port, baudRate);
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(result);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
std::unique_ptr<Uart> create(const Configuration& configuration) {
|
||||
return std::make_unique<UartEsp>(configuration);
|
||||
}
|
||||
|
||||
} // namespace tt::hal::uart
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,193 @@
|
||||
#ifndef ESP_PLATFORM
|
||||
|
||||
#include "Tactility/hal/uart/UartPosix.h"
|
||||
#include "Tactility/hal/uart/Uart.h"
|
||||
|
||||
#include <Tactility/Log.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define TAG "uart"
|
||||
|
||||
namespace tt::hal::uart {
|
||||
|
||||
bool UartPosix::start() {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
if (device != nullptr) {
|
||||
TT_LOG_E(TAG, "(%s) Starting: Already started", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
auto file = fopen(configuration.name.c_str(), "w");
|
||||
if (file == nullptr) {
|
||||
TT_LOG_E(TAG, "(%s) failed to open", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
auto new_device = std::unique_ptr<FILE, AutoCloseFileDeleter>(file);
|
||||
|
||||
struct termios tty;
|
||||
if (tcgetattr(fileno(file), &tty) < 0) {
|
||||
printf("(%s) tcgetattr failed: %s\n", configuration.name.c_str(), strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cfsetospeed(&tty, (speed_t)configuration.baudRate) == -1) {
|
||||
TT_LOG_E(TAG, "(%s) failed to set output speed", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cfsetispeed(&tty, (speed_t)configuration.baudRate) == -1) {
|
||||
TT_LOG_E(TAG, "(%s) failed to set input speed", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
tty.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
|
||||
tty.c_cflag &= ~CSIZE;
|
||||
tty.c_cflag |= CS8; /* 8-bit characters */
|
||||
tty.c_cflag &= ~PARENB; /* no parity bit */
|
||||
tty.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
|
||||
tty.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
|
||||
|
||||
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
|
||||
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
||||
tty.c_oflag &= ~OPOST;
|
||||
|
||||
/* fetch bytes as they become available */
|
||||
tty.c_cc[VMIN] = 1;
|
||||
tty.c_cc[VTIME] = 1;
|
||||
|
||||
if (tcsetattr(fileno(file), TCSANOW, &tty) != 0) {
|
||||
printf("(%s) tcsetattr failed: %s\n", configuration.name.c_str(), strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
device = std::move(new_device);
|
||||
|
||||
TT_LOG_I(TAG, "(%s) Started", configuration.name.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UartPosix::stop() {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
if (device == nullptr) {
|
||||
TT_LOG_E(TAG, "(%s) Stopping: Not started", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
device = nullptr;
|
||||
|
||||
TT_LOG_I(TAG, "(%s) Stopped", configuration.name.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UartPosix::isStarted() const {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
return device != nullptr;
|
||||
}
|
||||
|
||||
size_t UartPosix::readBytes(std::byte* buffer, size_t bufferSize, TickType_t timeout) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
if (!lock.lock(timeout)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (awaitAvailable(timeout)) {
|
||||
return read(fileno(device.get()), buffer, bufferSize);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool UartPosix::readByte(std::byte* output, TickType_t timeout) {
|
||||
if (awaitAvailable(timeout)) {
|
||||
return read(fileno(device.get()), output, 1) == 1;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
size_t UartPosix::writeBytes(const std::byte* buffer, size_t bufferSize, TickType_t timeout) {
|
||||
if (!mutex.lock(timeout)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return write(fileno(device.get()), buffer, bufferSize);
|
||||
}
|
||||
|
||||
size_t UartPosix::available(TickType_t timeout) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
if (!lock.lock(timeout)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t bytes_available = 0;
|
||||
ioctl(fileno(device.get()), FIONREAD, bytes_available);
|
||||
return bytes_available;
|
||||
}
|
||||
|
||||
void UartPosix::flushInput() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
uint32_t UartPosix::getBaudRate() {
|
||||
struct termios tty;
|
||||
if (tcgetattr(fileno(device.get()), &tty) < 0) {
|
||||
printf("(%s) tcgetattr failed: %s\n", configuration.name.c_str(), strerror(errno));
|
||||
return false;
|
||||
} else {
|
||||
return (uint32_t)cfgetispeed(&tty);
|
||||
}
|
||||
}
|
||||
|
||||
bool UartPosix::setBaudRate(uint32_t baudRate, TickType_t timeout) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
if (!lock.lock(timeout)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct termios tty;
|
||||
if (tcgetattr(fileno(device.get()), &tty) < 0) {
|
||||
printf("(%s) tcgetattr failed: %s\n", configuration.name.c_str(), strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cfsetospeed(&tty, (speed_t)configuration.baudRate) == -1) {
|
||||
TT_LOG_E(TAG, "(%s) failed to set output speed", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cfsetispeed(&tty, (speed_t)configuration.baudRate) == -1) {
|
||||
TT_LOG_E(TAG, "(%s) failed to set input speed", configuration.name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UartPosix::awaitAvailable(TickType_t timeout) {
|
||||
auto start_time = kernel::getTicks();
|
||||
do {
|
||||
if (available(timeout) > 0) {
|
||||
return true;
|
||||
}
|
||||
kernel::delayTicks(timeout / 10);
|
||||
} while ((kernel::getTicks() - start()) < timeout);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<Uart> create(const Configuration& configuration) {
|
||||
return std::make_unique<UartPosix>(configuration);
|
||||
}
|
||||
|
||||
} // namespace tt::hal::uart
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user