From 7cd008a0038bf503bee5b7641a1b4afbc85419d3 Mon Sep 17 00:00:00 2001 From: Kyle Schwarz Date: Tue, 25 Aug 2020 16:14:47 -0400 Subject: [PATCH] Switch to non-blocking I/O for STM32 STM32 can latency is not as good as it could be when it is synchronous due to read() timeouts, so switch to asynchronous reading with select(). --- platform/posix/stm32.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/platform/posix/stm32.cpp b/platform/posix/stm32.cpp index bb5b32a..d3c0977 100644 --- a/platform/posix/stm32.cpp +++ b/platform/posix/stm32.cpp @@ -10,6 +10,7 @@ #include #include #include +#include using namespace icsneo; @@ -25,7 +26,7 @@ bool STM32::open() { return false; } - fd = ::open(ttyPath.c_str(), O_RDWR | O_NOCTTY | O_SYNC); + fd = ::open(ttyPath.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); if(!isOpen()) { //std::cout << "Open of " << ttyPath.c_str() << " failed with " << strerror(errno) << ' '; report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error); @@ -113,10 +114,15 @@ bool STM32::close() { } void STM32::readTask() { - constexpr size_t READ_BUFFER_SIZE = 8; + constexpr size_t READ_BUFFER_SIZE = 2048; uint8_t readbuf[READ_BUFFER_SIZE]; EventManager::GetInstance().downgradeErrorsOnCurrentThread(); while(!closing) { + fd_set rfds = {0}; + struct timeval tv = {0}; + FD_SET(fd, &rfds); + tv.tv_usec = 50000; // 50ms + ::select(fd + 1, &rfds, NULL, NULL, &tv); auto bytesRead = ::read(fd, readbuf, READ_BUFFER_SIZE); if(bytesRead > 0) readQueue.enqueue_bulk(readbuf, bytesRead);