Added HAL docs, improved HAL init&locking (#218)
This commit is contained in:
committed by
GitHub
parent
b7f39f883d
commit
2e86d4774b
@@ -71,7 +71,10 @@ bool init(const std::vector<uart::Configuration>& configurations) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool configureLocked(uart_port_t port, const uart_config_t& configuration) {
|
||||
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);
|
||||
@@ -85,18 +88,10 @@ static bool configureLocked(uart_port_t port, const uart_config_t& configuration
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
bool start(uart_port_t port) {
|
||||
auto lock = getLock(port).asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
static bool startLocked(uart_port_t port) {
|
||||
Data& data = dataArray[port];
|
||||
printInfo(data);
|
||||
|
||||
@@ -147,18 +142,10 @@ static bool startLocked(uart_port_t 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;
|
||||
}
|
||||
}
|
||||
bool stop(uart_port_t port) {
|
||||
auto lock = getLock(port).asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
static bool stopLocked(uart_port_t port) {
|
||||
Data& data = dataArray[port];
|
||||
Configuration& config = data.configuration;
|
||||
|
||||
@@ -186,72 +173,54 @@ static bool stopLocked(uart_port_t port) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool stop(uart_port_t port) {
|
||||
if (lock(port)) {
|
||||
bool result = stopLocked(port);
|
||||
unlock(port);
|
||||
return result;
|
||||
} else {
|
||||
bool isStarted(uart_port_t port) {
|
||||
auto lock = getLock(port).asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
return dataArray[port].isStarted;
|
||||
}
|
||||
|
||||
Lockable& 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;
|
||||
}
|
||||
}
|
||||
|
||||
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()");
|
||||
}
|
||||
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 read(port, output, 1, timeout) == 1;
|
||||
return readBytes(port, output, 1, timeout) == 1;
|
||||
}
|
||||
|
||||
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()");
|
||||
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) {
|
||||
while (*buffer != 0) {
|
||||
if (write(port, (const uint8_t*)buffer, 1, timeout)) {
|
||||
if (writeBytes(port, (const uint8_t*)buffer, 1, timeout)) {
|
||||
buffer++;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to write - breaking off");
|
||||
@@ -263,47 +232,38 @@ bool writeString(uart_port_t port, const char* buffer, TickType_t timeout) {
|
||||
}
|
||||
|
||||
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;
|
||||
if (lock(port, timeout)) {
|
||||
uart_get_buffered_data_len(port, &size);
|
||||
unlock(port);
|
||||
return size;
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "write()");
|
||||
}
|
||||
#endif // ESP_PLATFORM
|
||||
uart_get_buffered_data_len(port, &size);
|
||||
return size;
|
||||
#else
|
||||
return 0;
|
||||
}
|
||||
|
||||
void flush(uart_port_t port, TickType_t timeout) {
|
||||
#ifdef ESP_PLATFORM
|
||||
size_t size = 0;
|
||||
if (lock(port, timeout)) {
|
||||
uart_flush(port);
|
||||
unlock(port);
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "write()");
|
||||
}
|
||||
#endif // ESP_PLATFORM
|
||||
}
|
||||
|
||||
void flushInput(uart_port_t port, TickType_t timeout) {
|
||||
void flush(uart_port_t port) {
|
||||
#ifdef ESP_PLATFORM
|
||||
size_t size = 0;
|
||||
if (lock(port, timeout)) {
|
||||
uart_flush_input(port);
|
||||
unlock(port);
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "write()");
|
||||
}
|
||||
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;
|
||||
uart_get_baudrate(port, &baud_rate);
|
||||
auto result = uart_get_baudrate(port, &baud_rate);
|
||||
ESP_ERROR_CHECK_WITHOUT_ABORT(result);
|
||||
return baud_rate;
|
||||
#else
|
||||
return 0;
|
||||
@@ -311,15 +271,19 @@ uint32_t getBaudRate(uart_port_t port) {
|
||||
}
|
||||
|
||||
bool setBaudRate(uart_port_t port, uint32_t baudRate, TickType_t timeout) {
|
||||
#ifdef ESP_PLATFORM
|
||||
if (lock(port, timeout)) {
|
||||
uart_set_baudrate(port, baudRate);
|
||||
unlock(port);
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "write()");
|
||||
auto lock = getLock(port).asScopedLock();
|
||||
if (!lock.lock(timeout)) {
|
||||
TT_LOG_E(TAG, "(%d) Mutex timeout", port);
|
||||
return false;
|
||||
}
|
||||
#endif // ESP_PLATFORM
|
||||
|
||||
#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
|
||||
}
|
||||
|
||||
bool readUntil(uart_port_t port, uint8_t* buffer, size_t bufferSize, uint8_t untilByte, TickType_t timeout) {
|
||||
@@ -330,9 +294,8 @@ bool readUntil(uart_port_t port, uint8_t* buffer, size_t bufferSize, uint8_t unt
|
||||
if (*buffer == untilByte) {
|
||||
success = true;
|
||||
// We have the extra space because index < index_limit
|
||||
if (buffer++) {
|
||||
*buffer = 0;
|
||||
}
|
||||
buffer++;
|
||||
*buffer = 0;
|
||||
break;
|
||||
}
|
||||
buffer++;
|
||||
|
||||
Reference in New Issue
Block a user