Skip to content

Commit

Permalink
esputil windows port
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Oct 2, 2021
1 parent 064db1d commit 1883742
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
9 changes: 7 additions & 2 deletions tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ CFLAGS ?= -W -Wall -Wextra -Werror -Wundef -Wshadow -Wdouble-promotion -Os
BINDIR ?= .
CWD ?= $(realpath $(CURDIR))
DOCKER = docker run $(DA) --rm -e WINEDEBUG=-all -v $(CWD):$(CWD) -w $(CWD)
SERIAL_PORT ?= /dev/ttyUSB0

all:
@echo available targets: slipterm esputil test examples
Expand All @@ -11,8 +12,12 @@ esputil: esputil.c

esputil.exe: esputil.c
$(DOCKER) mdashnet/vc98 wine cl /nologo /W3 /Os $? /Fe$@
$(DOCKER) mdashnet/vc98 wine $@ -v -p COM33 info
# $(DOCKER) mdashnet/mingw i686-w64-mingw32-gcc $(CFLAGS) $? -o $@

test_esputil_windows: esputil.exe
ln -fs $(SERIAL_PORT) ~/.wine/dosdevices/com55 && wine $? -p '\\.\COM55' -v info
# $(DOCKER) -it --device $(SERIAL_PORT) mdashnet/vc98 \
ln -fs $(SERIAL_PORT) /root/.wine/dosdevices/com3 ; \
wine $@ -v -p '\\.\COM3' monitor

slipterm: slipterm.c
$(CC) $(CFLAGS) $? -lpcap -lutil -o $(BINDIR)/$@
Expand Down
47 changes: 36 additions & 11 deletions tools/esputil.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,35 +187,60 @@ static uint8_t checksum(const uint8_t *buf, size_t len) {
return checksum2(0xef, buf, len);
}

#ifdef _WIN32
#ifdef _WIN32 // Windows - specific routines
static void sleep_ms(int milliseconds) {
Sleep(milliseconds);
}

static void flushio(int fd) {
(void) fd;
PurgeComm((HANDLE) _get_osfhandle(fd), PURGE_RXCLEAR | PURGE_TXCLEAR);
}

static int open_serial(const char *name, int baud, bool verbose) {
int fd = open(name, O_RDWR);
if (fd < 0) fail("open(%s): %d (%s)\n", name, fd, strerror(errno));
return fd;
static void change_baud(int fd, int baud, bool verbose) {
DCB cfg = {sizeof(cfg)};
HANDLE h = (HANDLE) _get_osfhandle(fd);
if (GetCommState(h, &cfg)) {
cfg.ByteSize = 8;
cfg.Parity = NOPARITY;
cfg.StopBits = ONESTOPBIT;
cfg.fBinary = TRUE;
cfg.fParity = TRUE;
cfg.BaudRate = baud;
SetCommState(h, &cfg);
} else {
fail("GetCommState(%x): %d\n", h, GetLastError());
}
}

static void change_baud(int fd, int baud, bool verbose) {
static int open_serial(const char *name, int baud, bool verbose) {
COMMTIMEOUTS ct = {1, 0, 1, 0, MAXDWORD}; // 1 ms read timeout
int fd = open(name, O_RDWR | O_BINARY);
if (fd < 0) fail("open(%s): %s\n", name, strerror(errno));
change_baud(fd, baud, verbose);
SetCommTimeouts((HANDLE) _get_osfhandle(fd), &ct);
return fd;
}

static int iowait(int fd, int ms) {
return 0;
COMSTAT cs = {0};
DWORD errors, flags = 0;
int i;
for (i = 0; i < ms && flags == 0; i++) {
sleep_ms(1);
ClearCommError((HANDLE) _get_osfhandle(fd), &errors, &cs);
if (cs.cbInQue > 0) flags |= 2; // There is something to read
}
return flags;
}

static void set_rts(int fd, bool value) {
EscapeCommFunction((HANDLE) _get_osfhandle(fd), value ? SETRTS : CLRRTS);
}

static void set_dtr(int fd, bool value) {
EscapeCommFunction((HANDLE) _get_osfhandle(fd), value ? SETDTR : CLRDTR);
}

#else
#else // UNIX - specific routines
static void set_rts(int fd, bool value) {
int v = TIOCM_RTS;
ioctl(fd, value ? TIOCMBIS : TIOCMBIC, &v);
Expand Down Expand Up @@ -305,7 +330,7 @@ static int iowait(int fd, int ms) {
if (FD_ISSET(fd, &rset)) ready |= 2;
return ready;
}
#endif
#endif // End of UNIX-specific routines

static void hard_reset(int fd) {
set_dtr(fd, false); // IO0 -> HIGH
Expand Down

0 comments on commit 1883742

Please sign in to comment.