From 749578b0b0b3f1c9df841dfd33949921df6cec56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken=20A=2E=20Rederg=C3=A5rd?= <64542+kenr@users.noreply.github.com> Date: Thu, 11 Mar 2021 09:06:32 +0100 Subject: [PATCH] Feature/tune uart params (#264) * Add tunable parameters for Windows * Fix nrfutil in pipeline (Linux) --- .azure-pipelines/azure-pipelines.yml | 2 +- src/common/transport/uart_transport.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/.azure-pipelines/azure-pipelines.yml b/.azure-pipelines/azure-pipelines.yml index f828b6c88..1088113c8 100644 --- a/.azure-pipelines/azure-pipelines.yml +++ b/.azure-pipelines/azure-pipelines.yml @@ -213,7 +213,7 @@ jobs: sudo apt-get update sudo apt-get install ninja-build gcc-8 g++-8 clang-9 clang-format-9 clang-tidy-9 python3-pycurl python3-pip python3-certifi python3-setuptools libusb-1.0-0-dev libudev-dev /usr/bin/python3 -m pip install -U pip - /usr/bin/python3 -m pip install nrfutil + /usr/bin/python3 -m pip install --ignore-installed nrfutil chmod a+x $(System.ArtifactsDirectory)/nrftc $(System.ArtifactsDirectory)/nrftc install arm-toolchain -a $(Agent.BuildDirectory)/toolchain -i armgcc:${{ parameters.ARMGCC_VERSION }} -e $(Agent.BuildDirectory)/tcenv.sh cat $(Agent.BuildDirectory)/tcenv.sh diff --git a/src/common/transport/uart_transport.cpp b/src/common/transport/uart_transport.cpp index 72f261268..1f063ff2e 100644 --- a/src/common/transport/uart_transport.cpp +++ b/src/common/transport/uart_transport.cpp @@ -61,6 +61,11 @@ constexpr auto DELAY_BEFORE_READ_WRITE = std::chrono::milliseconds(200); // if opening a UART port right after close. constexpr auto DELAY_BEFORE_OPEN = std::chrono::milliseconds(200); +#if defined(_WIN32) +// Wait for 20ms for data before returning from a read +constexpr auto readTotalTimeoutConstant = 20; +#endif + struct UartTransport::impl : Transport { std::array readBuffer; @@ -283,6 +288,26 @@ struct UartTransport::impl : Transport } #endif +#if defined(_WIN32) + ::COMMTIMEOUTS timeouts; + + // See + // https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-commtimeouts + // for documentation of these parameters + timeouts.ReadIntervalTimeout = MAXDWORD; + timeouts.ReadTotalTimeoutMultiplier = MAXDWORD; + timeouts.ReadTotalTimeoutConstant = readTotalTimeoutConstant; + + timeouts.WriteTotalTimeoutMultiplier = 0; + timeouts.WriteTotalTimeoutConstant = 0; + + if (!::SetCommTimeouts(serialPort->native_handle(), &timeouts)) + { + const auto error = std::error_code(errno, std::system_category()); + throw std::system_error(error, "Failed to set communication timeout parameters"); + } +#endif + // Wait a bit before making the device available since there are problems // if data is sent right after open. //