From d4c0853e1f8c2f00274ef50dbb63053a2fb2e1aa Mon Sep 17 00:00:00 2001 From: David Given Date: Thu, 25 May 2023 22:23:28 +0200 Subject: [PATCH] Reset the Greaseweazle data stream when connecting. --- lib/usb/greaseweazleusb.cc | 16 ++++++++++++---- lib/usb/serial.cc | 19 +++++++++++++++++++ lib/usb/serial.h | 1 + 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/usb/greaseweazleusb.cc b/lib/usb/greaseweazleusb.cc index 59a6a76f..7c091316 100644 --- a/lib/usb/greaseweazleusb.cc +++ b/lib/usb/greaseweazleusb.cc @@ -1,11 +1,12 @@ -#include "globals.h" -#include "usb.h" +#include "lib/globals.h" #include "protocol.h" -#include "fluxmap.h" -#include "bytes.h" +#include "lib/fluxmap.h" +#include "lib/bytes.h" #include "lib/usb/usb.pb.h" #include "greaseweazle.h" #include "serial.h" +#include "usb.h" +#include static const char* gw_error(int e) { @@ -96,6 +97,13 @@ public: version); } + /* Twiddle the baud rate, which indicates to the Greaseweazle that the + * data stream has been reset. */ + + _serial->setBaudRate(10000); + usleep(100000); + _serial->setBaudRate(9600); + /* Configure the hardware. */ do_command({CMD_SET_BUS_TYPE, 3, (uint8_t)config.bus_type()}); diff --git a/lib/usb/serial.cc b/lib/usb/serial.cc index 7fa0bf37..2e22b0f0 100644 --- a/lib/usb/serial.cc +++ b/lib/usb/serial.cc @@ -88,6 +88,17 @@ public: return wlen; } + void setBaudRate(int baudRate) override + { + DCB dcb = {.DCBlength = sizeof(DCB), + .BaudRate = baudRate, + .fBinary = true, + .ByteSize = 8, + .Parity = NOPARITY, + .StopBits = ONESTOPBIT}; + SetCommState(_handle, &dcb); + } + private: static std::string get_last_error_string() { @@ -181,6 +192,14 @@ public: return wlen; } + void setBaudRate(int baudRate) override + { + struct termios t; + tcgetattr(_fd, &t); + cfsetspeed(&t, baudRate); + tcsetattr(_fd, TCSANOW, &t); + } + private: int _fd; }; diff --git a/lib/usb/serial.h b/lib/usb/serial.h index 15e1d8e9..c629c080 100644 --- a/lib/usb/serial.h +++ b/lib/usb/serial.h @@ -10,6 +10,7 @@ public: virtual ~SerialPort(); virtual ssize_t readImpl(uint8_t* buffer, size_t len) = 0; virtual ssize_t write(const uint8_t* buffer, size_t len) = 0; + virtual void setBaudRate(int baudRate) = 0; void read(uint8_t* buffer, size_t len); void read(Bytes& bytes);