Now compiling with MinGW.

- implemented Win32 serial IO
  - added Winsock init/exit
  - added missing strsep()
  - minor API fixups
This commit is contained in:
Daniel Beer 2011-07-27 01:21:44 +12:00
parent 7b261217c8
commit a81043a656
10 changed files with 245 additions and 44 deletions

View File

@ -43,30 +43,43 @@ else
endif
endif
MSPDEBUG_CFLAGS = -O1 -Wall -Wno-char-subscripts -ggdb
ifeq ($(OS),Windows_NT)
WIN32_LIBS = -lws2_32 -lregex
BINARY = mspdebug.exe
else
WIN32_LIBS =
BINARY = mspdebug
endif
all: mspdebug
GCC_CFLAGS = -O1 -Wall -Wno-char-subscripts -ggdb
MSPDEBUG_LDFLAGS = $(LDFLAGS) $(PORTS_LDFLAGS)
MSPDEBUG_LIBS = -lusb $(READLINE_LIBS) $(WIN32_LIBS)
MSPDEBUG_CFLAGS = $(CFLAGS) $(PORTS_CFLAGS) $(GCC_CFLAGS)
all: $(BINARY)
clean:
/bin/rm -f *.o
/bin/rm -f mspdebug
rm -f *.o
rm -f $(BINARY)
install: mspdebug mspdebug.man
install: $(BINARY) mspdebug.man
mkdir -p $(DESTDIR)$(PREFIX)/bin
mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1
$(INSTALL) -m 0755 mspdebug $(DESTDIR)$(PREFIX)/bin/mspdebug
$(INSTALL) -m 0644 mspdebug.man $(DESTDIR)$(PREFIX)/share/man/man1/mspdebug.1
$(INSTALL) -m 0755 $(BINARY) $(DESTDIR)$(PREFIX)/bin/mspdebug
$(INSTALL) -m 0644 mspdebug.man \
$(DESTDIR)$(PREFIX)/share/man/man1/mspdebug.1
.SUFFIXES: .c .o
mspdebug: main.o fet.o rf2500.o dis.o uif.o olimex.o ihex.o elf32.o stab.o \
util.o bsl.o sim.o symmap.o gdb.o btree.o rtools.o sym.o devcmd.o \
reader.o vector.o output_util.o expr.o fet_error.o binfile.o \
fet_db.o usbutil.o titext.o srec.o device.o coff.o opdb.o output.o \
cmddb.o stdcmd.o prog.o flash_bsl.o list.o simio.o simio_tracer.o \
simio_timer.o simio_wdt.o simio_hwmult.o simio_gpio.o aliasdb.o \
gdb_proto.o gdbc.o sport.o
$(CC) $(LDFLAGS) $(PORTS_LDFLAGS) -o $@ $^ -lusb $(READLINE_LIBS)
$(BINARY): main.o fet.o rf2500.o dis.o uif.o olimex.o ihex.o elf32.o stab.o \
util.o bsl.o sim.o symmap.o gdb.o btree.o rtools.o sym.o devcmd.o \
reader.o vector.o output_util.o expr.o fet_error.o binfile.o \
fet_db.o usbutil.o titext.o srec.o device.o coff.o opdb.o output.o \
cmddb.o stdcmd.o prog.o flash_bsl.o list.o simio.o simio_tracer.o \
simio_timer.o simio_wdt.o simio_hwmult.o simio_gpio.o aliasdb.o \
gdb_proto.o gdbc.o sport.o
$(CC) $(MSPDEBUG_LDFLAGS) -o $@ $^ $(MSPDEBUG_LIBS)
.c.o:
$(CC) $(CFLAGS) $(PORTS_CFLAGS) $(READLINE_CFLAGS) $(MSPDEBUG_CFLAGS) -o $@ -c $*.c
$(CC) $(MSPDEBUG_CFLAGS) -o $@ -c $*.c

View File

@ -123,16 +123,14 @@ static int flash_bsl_send(struct flash_bsl_device *dev,
cmd_buf[len + 4] = (crc >> 8) & 0xff;
if (sport_write_all(dev->serial_fd, cmd_buf, len + 5) < 0) {
printc_err("flash_bsl: serial write failed\n");
printc_err("flash_bsl: serial write failed: %s\n",
strerror(errno));
return -1;
}
if (sport_read_all(dev->serial_fd, &response, 1) < 0) {
if (errno == ETIMEDOUT) {
printc_err("flash_bsl: serial read timed out\n");
} else {
printc_err("flash_bsl: serial read failed\n");
}
printc_err("flash_bsl: serial read failed: %s\n",
strerror(errno));
return -1;
}
@ -178,14 +176,9 @@ static int flash_bsl_recv(struct flash_bsl_device *dev,
uint16_t crc_value;
if (sport_read_all(dev->serial_fd, header, 3) < 0) {
if (errno == ETIMEDOUT) {
printc_err("flash_bsl: response timed out\n");
return -1;
} else {
perror("read response header");
printc_err("flash_bsl: read response failed\n");
return -1;
}
printc_err("flash_bsl: read response failed: %s\n",
strerror(errno));
return -1;
}
if (header[0] != 0x80) {
@ -558,23 +551,23 @@ static int enter_via_dtr_rts(struct flash_bsl_device *dev)
sport_t fd = dev->serial_fd;
/* drive RST# line low */
if (sport_set_modem(fd, TIOCM_RTS | TIOCM_DTR) != 0) {
if (sport_set_modem(fd, SPORT_MC_RTS | SPORT_MC_DTR) != 0) {
return -1;
}
entry_delay( );
/* drive TEST line high then low again */
if (sport_set_modem(fd, TIOCM_DTR) != 0) {
if (sport_set_modem(fd, SPORT_MC_DTR) != 0) {
return -1;
}
entry_delay( );
if (sport_set_modem(fd, TIOCM_RTS | TIOCM_DTR) != 0) {
if (sport_set_modem(fd, SPORT_MC_RTS | SPORT_MC_DTR) != 0) {
return -1;
}
entry_delay( );
/* drive TEST line high followed by RST# line */
if (sport_set_modem(fd, TIOCM_DTR) != 0) {
if (sport_set_modem(fd, SPORT_MC_DTR) != 0) {
return -1;
}
entry_delay( );
@ -582,7 +575,7 @@ static int enter_via_dtr_rts(struct flash_bsl_device *dev)
return -1;
}
entry_delay( );
if (sport_set_modem(fd, TIOCM_RTS) != 0) {
if (sport_set_modem(fd, SPORT_MC_RTS) != 0) {
return -1;
}
entry_delay( );
@ -597,13 +590,13 @@ static void exit_via_dtr_rts(struct flash_bsl_device *dev)
sport_t fd = dev->serial_fd;
/* RST# and TEST LOW */
sport_set_modem(fd, TIOCM_RTS | TIOCM_DTR);
sport_set_modem(fd, SPORT_MC_RTS | SPORT_MC_DTR);
/* wait a brief period */
entry_delay( );
/* RST# HIGH */
sport_set_modem(fd, TIOCM_DTR);
sport_set_modem(fd, SPORT_MC_DTR);
}
static void flash_bsl_destroy(device_t dev_base)

3
gdb.c
View File

@ -457,7 +457,8 @@ static int gdb_server(int port)
}
arg = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0)
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(void *)&arg, sizeof(arg)) < 0)
pr_error("gdb: warning: can't reuse socket address");
addr.sin_family = AF_INET;

4
gdbc.c
View File

@ -377,8 +377,12 @@ static int connect_to(const char *spec)
printc_dbg("Looking up %s...\n", hostname);
ent = gethostbyname(hostname);
if (!ent) {
#ifdef WIN32
printc_err("No such host: %s: %s\n", hostname);
#else
printc_err("No such host: %s: %s\n", hostname,
hstrerror(h_errno));
#endif
return -1;
}

34
main.c
View File

@ -175,7 +175,7 @@ static int parse_cmdline_args(int argc, char **argv,
{"usb-list", 0, 0, 'I'},
{"version", 0, 0, 'V'},
{"long-password", 0, 0, 'P'},
{"force-reset", 0, 0, 'R'},
{"force-reset", 0, 0, 'R'},
{NULL, 0, 0, 0}
};
int want_usb = 0;
@ -298,6 +298,28 @@ int setup_driver(struct cmdline_args *args)
return 0;
}
#ifdef WIN32
static int sockets_init(void)
{
WSADATA data;
if (WSAStartup(MAKEWORD(2, 2), &data)) {
printc_err("Winsock init failed");
return -1;
}
return 0;
}
static void sockets_exit(void)
{
WSACleanup();
}
#else
static int sockets_init(void) { return 0; }
static void sockets_exit(void) { }
#endif
int main(int argc, char **argv)
{
struct cmdline_args args = {0};
@ -310,10 +332,15 @@ int main(int argc, char **argv)
if (parse_cmdline_args(argc, argv, &args) < 0)
return -1;
printc_dbg("%s\n", version_text);
if (setup_driver(&args) < 0)
if (sockets_init() < 0)
return -1;
printc_dbg("%s\n", version_text);
if (setup_driver(&args) < 0) {
sockets_exit();
return -1;
}
simio_init();
if (!args.no_rc)
@ -335,6 +362,7 @@ int main(int argc, char **argv)
simio_exit();
stab_exit();
device_destroy();
sockets_exit();
return ret;
}

100
sport.c
View File

@ -22,6 +22,8 @@
#include "sport.h"
#ifndef WIN32
sport_t sport_open(const char *device, int rate, int flags)
{
int fd = open(device, O_RDWR | O_NOCTTY);
@ -94,6 +96,104 @@ int sport_write(sport_t s, const uint8_t *data, int len)
return write(s, data, len);
}
#else /* WIN32 */
sport_t sport_open(const char *device, int rate, int flags)
{
HANDLE hs = CreateFile(device, GENERIC_READ | GENERIC_WRITE,
0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
0);
DCB params = {0};
COMMTIMEOUTS timeouts = {0};
if (hs == INVALID_HANDLE_VALUE)
return INVALID_HANDLE_VALUE;
if (!GetCommState(hs, &params)) {
CloseHandle(hs);
return INVALID_HANDLE_VALUE;
}
params.BaudRate = rate;
params.ByteSize = 8;
params.StopBits = ONESTOPBIT;
params.Parity = (flags & SPORT_EVEN_PARITY) ? EVENPARITY : NOPARITY;
if (!SetCommState(hs, &params)) {
CloseHandle(hs);
return INVALID_HANDLE_VALUE;
}
timeouts.ReadIntervalTimeout = 5000;
timeouts.ReadTotalTimeoutConstant = 5000;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 5000;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(hs, &timeouts)) {
CloseHandle(hs);
return INVALID_HANDLE_VALUE;
}
return hs;
}
void sport_close(sport_t s)
{
CloseHandle(s);
}
int sport_flush(sport_t s)
{
if (!PurgeComm(s, PURGE_RXABORT | PURGE_RXCLEAR))
return -1;
return 0;
}
int sport_set_modem(sport_t s, int bits)
{
if (!EscapeCommFunction(s, (bits & SPORT_MC_DTR) ? SETDTR : CLRDTR))
return -1;
if (!EscapeCommFunction(s, (bits & SPORT_MC_RTS) ? SETRTS : CLRRTS))
return -1;
return 0;
}
int sport_read(sport_t s, uint8_t *data, int len)
{
DWORD result = 0;
if (!ReadFile(s, (void *)data, len, &result, NULL))
return -1;
if (!result) {
errno = EAGAIN;
return -1;
}
return result;
}
int sport_write(sport_t s, const uint8_t *data, int len)
{
DWORD result = 0;
if (!WriteFile(s, (void *)data, len, &result, NULL))
return -1;
if (!result) {
errno = EAGAIN;
return -1;
}
return result;
}
#endif
int sport_read_all(sport_t s, uint8_t *data, int len)
{
while (len) {

32
sport.h
View File

@ -1,5 +1,5 @@
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009, 2010 Daniel Beer
* Copyright (C) 2009-2011 Daniel Beer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -19,6 +19,8 @@
#ifndef SPORT_H_
#define SPORT_H_
#ifndef WIN32
#include <stdint.h>
#include <termios.h>
#include <sys/ioctl.h>
@ -35,6 +37,34 @@ typedef int sport_t;
#define SPORT_ISERR(x) ((x) < 0)
#define SPORT_MC_DTR TIOCM_DTR
#define SPORT_MC_RTS TIOCM_RTS
#else /* WIN32 */
#include <windows.h>
typedef HANDLE sport_t;
#define SPORT_ISERR(x) ((x) == INVALID_HANDLE_VALUE)
#ifndef CBR_460800
#define CBR_460800 460800
#endif
#ifndef CBR_500000
#define CBR_500000 500000
#endif
#define B9600 CBR_9600
#define B460800 CBR_460800
#define B500000 CBR_500000
#define SPORT_MC_DTR 0x01
#define SPORT_MC_RTS 0x02
#endif
/* Various utility functions for IO */
#define SPORT_EVEN_PARITY 0x01

4
uif.c
View File

@ -147,9 +147,9 @@ transport_t uif_open(const char *device, uif_type_t type)
break;
}
if (tr->serial_fd < 0) {
if (SPORT_ISERR(tr->serial_fd)) {
printc_err("uif: can't open serial device: %s: %s\n",
device, strerror(errno));
device, strerror(errno));
free(tr);
return NULL;
}

28
util.c
View File

@ -194,3 +194,31 @@ int hexval(int c)
return 0;
}
#ifdef WIN32
char *strsep(char **strp, const char *delim)
{
char *start = *strp;
char *end = start;
if (!start)
return NULL;
while (*end) {
const char *d = delim;
while (*d) {
if (*d == *end) {
*(end++) = 0;
*strp = end;
return start;
}
}
end++;
}
*strp = NULL;
return start;
}
#endif

4
util.h
View File

@ -52,4 +52,8 @@ static inline int ishex(int c)
int hexval(int c);
#ifdef WIN32
char *strsep(char **strp, const char *delim);
#endif
#endif