Reset the Greaseweazle data stream when connecting.

This commit is contained in:
David Given
2023-05-25 22:23:28 +02:00
parent 363a4e959c
commit d4c0853e1f
3 changed files with 32 additions and 4 deletions

View File

@@ -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 <unistd.h>
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()});

View File

@@ -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;
};

View File

@@ -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);