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().
This commit is contained in:
Kyle Schwarz
2020-08-25 16:14:47 -04:00
committed by Paul Hollinsky
parent dfe5845b3c
commit 7cd008a003
+8 -2
View File
@@ -10,6 +10,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <sys/select.h>
using namespace icsneo; using namespace icsneo;
@@ -25,7 +26,7 @@ bool STM32::open() {
return false; 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()) { if(!isOpen()) {
//std::cout << "Open of " << ttyPath.c_str() << " failed with " << strerror(errno) << ' '; //std::cout << "Open of " << ttyPath.c_str() << " failed with " << strerror(errno) << ' ';
report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error); report(APIEvent::Type::DriverFailedToOpen, APIEvent::Severity::Error);
@@ -113,10 +114,15 @@ bool STM32::close() {
} }
void STM32::readTask() { void STM32::readTask() {
constexpr size_t READ_BUFFER_SIZE = 8; constexpr size_t READ_BUFFER_SIZE = 2048;
uint8_t readbuf[READ_BUFFER_SIZE]; uint8_t readbuf[READ_BUFFER_SIZE];
EventManager::GetInstance().downgradeErrorsOnCurrentThread(); EventManager::GetInstance().downgradeErrorsOnCurrentThread();
while(!closing) { 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); auto bytesRead = ::read(fd, readbuf, READ_BUFFER_SIZE);
if(bytesRead > 0) if(bytesRead > 0)
readQueue.enqueue_bulk(readbuf, bytesRead); readQueue.enqueue_bulk(readbuf, bytesRead);