From d8db6d57f7c946759478eae88aae554fee4c366e Mon Sep 17 00:00:00 2001 From: Nicolas "Pixel" Noble Date: Sat, 22 Jan 2011 03:52:48 +0100 Subject: [PATCH] Adding windows support for BusPirate. --- src/jtag/drivers/buspirate.c | 135 ++++++++++++++++++++++++++++++++++------- 1 files changed, 112 insertions(+), 23 deletions(-) diff --git a/src/jtag/drivers/buspirate.c b/src/jtag/drivers/buspirate.c index 13819ba..d22f0d1 100644 --- a/src/jtag/drivers/buspirate.c +++ b/src/jtag/drivers/buspirate.c @@ -25,9 +25,13 @@ #include #include +#ifdef WIN32 +#include +#else #include #include #include +#endif #undef DEBUG_SERIAL /*#define DEBUG_SERIAL */ @@ -80,7 +84,14 @@ enum { }; -static int buspirate_fd = -1; +#ifdef WIN32 +typedef HANDLE fdt; +#define fdtinvalid INVALID_HANDLE_VALUE +#else +typedef int fdt; +#define fdtinvalid -1 +#endif +static fdt buspirate_fd = fdtinvalid; static int buspirate_pinmode = MODE_JTAG_OD; static int buspirate_baudrate = SERIAL_NORMAL; static int buspirate_vreg; @@ -99,20 +110,20 @@ static void buspirate_tap_make_space(int scan, int bits); static void buspirate_reset(int trst, int srst); /* low level interface */ -static void buspirate_jtag_reset(int); -static void buspirate_jtag_enable(int); -static unsigned char buspirate_jtag_command(int, char *, int); -static void buspirate_jtag_set_speed(int, char); -static void buspirate_jtag_set_mode(int, char); -static void buspirate_jtag_set_feature(int, char, char); -static void buspirate_jtag_get_adcs(int); +static void buspirate_jtag_reset(fdt fd); +static void buspirate_jtag_enable(fdt fd); +static unsigned char buspirate_jtag_command(fdt fd, char *, int); +static void buspirate_jtag_set_speed(fdt fd, char); +static void buspirate_jtag_set_mode(fdt fd, char); +static void buspirate_jtag_set_feature(fdt fd, char, char); +static void buspirate_jtag_get_adcs(fdt fd); /* low level HW communication interface */ -static int buspirate_serial_open(char *port); -static int buspirate_serial_setspeed(int fd, char speed); -static int buspirate_serial_write(int fd, char *buf, int size); -static int buspirate_serial_read(int fd, char *buf, int size); -static void buspirate_serial_close(int fd); +static fdt buspirate_serial_open(char *port); +static int buspirate_serial_setspeed(fdt fd, char speed); +static int buspirate_serial_write(fdt fd, char *buf, int size); +static int buspirate_serial_read(fdt fd, char *buf, int size); +static void buspirate_serial_close(fdt fd); static void buspirate_print_buffer(char *buf, int size); static int buspirate_speed(int speed) @@ -217,7 +228,7 @@ static int buspirate_init(void) } buspirate_fd = buspirate_serial_open(buspirate_port); - if (buspirate_fd == -1) { + if (buspirate_fd == fdtinvalid) { LOG_ERROR("Could not open serial port."); return ERROR_JTAG_INIT_FAILED; } @@ -267,7 +278,7 @@ COMMAND_HANDLER(buspirate_handle_adc_command) return ERROR_OK; } - if (buspirate_fd == -1) + if (buspirate_fd == fdtinvalid) return ERROR_OK; /* send the command */ @@ -703,7 +714,7 @@ static void buspirate_reset(int trst, int srst) } /*************** jtag lowlevel functions ********************/ -static void buspirate_jtag_enable(int fd) +static void buspirate_jtag_enable(fdt fd) { int ret; char tmp[21] = { [0 ... 20] = 0x00 }; @@ -750,7 +761,7 @@ static void buspirate_jtag_enable(int fd) } -static void buspirate_jtag_reset(int fd) +static void buspirate_jtag_reset(fdt fd) { int ret; char tmp[5]; @@ -766,7 +777,7 @@ static void buspirate_jtag_reset(int fd) LOG_ERROR("Bad reply :( Please restart manually"); } -static void buspirate_jtag_set_speed(int fd, char speed) +static void buspirate_jtag_set_speed(fdt fd, char speed) { int ret; char tmp[2]; @@ -799,7 +810,7 @@ static void buspirate_jtag_set_speed(int fd, char speed) } -static void buspirate_jtag_set_mode(int fd, char mode) +static void buspirate_jtag_set_mode(fdt fd, char mode) { char tmp[2]; tmp[0] = CMD_PORT_MODE; @@ -807,7 +818,7 @@ static void buspirate_jtag_set_mode(int fd, char mode) buspirate_jtag_command(fd, tmp, 2); } -static void buspirate_jtag_set_feature(int fd, char feat, char action) +static void buspirate_jtag_set_feature(fdt fd, char feat, char action) { char tmp[3]; tmp[0] = CMD_FEATURE; @@ -816,7 +827,7 @@ static void buspirate_jtag_set_feature(int fd, char feat, char action) buspirate_jtag_command(fd, tmp, 3); } -static void buspirate_jtag_get_adcs(int fd) +static void buspirate_jtag_get_adcs(fdt fd) { uint8_t tmp[10]; uint16_t a, b, c, d; @@ -833,7 +844,7 @@ static void buspirate_jtag_get_adcs(int fd) ((float)c)/155.1515, ((float)d)/155.1515); } -static unsigned char buspirate_jtag_command(int fd, +static unsigned char buspirate_jtag_command(fdt fd, char *cmd, int cmdlen) { int res; @@ -869,7 +880,84 @@ static unsigned char buspirate_jtag_command(int fd, } /* low level serial port */ -/* TODO add support for WIN32 and others ! */ +#ifdef WIN32 +static HANDLE buspirate_serial_open(char *port) +{ + return CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); +} + +static int buspirate_serial_setspeed(HANDLE fd, char speed) +{ + DCB dcbSerialParams; + dcbSerialParams.DCBlength = sizeof(dcbSerialParams); + if (!GetCommState(fd, &dcbSerialParams)) + return -1; + + dcbSerialParams.BaudRate = (speed == SERIAL_FAST) ? CBR_256000 : CBR_115200; + dcbSerialParams.ByteSize = 8; + dcbSerialParams.StopBits = ONESTOPBIT; + dcbSerialParams.Parity = NOPARITY; + + if (!SetCommState(fd, &dcbSerialParams)) + return -1; + + return 0; +} + +static int buspirate_serial_write(HANDLE fd, char *buf, int size) +{ + DWORD in = size, ret = 0; + + if (!WriteFile(fd, buf, in, &ret, NULL)) { + LOG_ERROR("Error sending data"); + ret = 0; + } + + LOG_DEBUG("size = %d ret = %u", size, (unsigned int) ret); + buspirate_print_buffer(buf, size); + + if (ret != (unsigned int) size) + LOG_ERROR("Error sending data"); + + return ret; +} + +static int buspirate_serial_read(HANDLE fd, char *buf, int size) +{ + DWORD len = 0; + DWORD ret = 0; + int timeout = 0; + + while (len < (unsigned int) size) { + if (!ReadFile(fd, buf + len, size - len, &ret, NULL)) + return -1; + + if (ret == 0) { + timeout++; + + if (timeout >= 10) + break; + + continue; + } + + len += ret; + } + + LOG_DEBUG("should have read = %d actual size = %u", size, (unsigned int) len); + buspirate_print_buffer(buf, len); + + if (len != (unsigned int) size) + LOG_ERROR("Error receiving data"); + + return len; +} + +static void buspirate_serial_close(HANDLE fd) +{ + CloseHandle(fd); +} +#else static int buspirate_serial_open(char *port) { int fd; @@ -954,6 +1042,7 @@ static void buspirate_serial_close(int fd) { close(fd); } +#endif #define LINE_SIZE 81 #define BYTES_PER_LINE 16 -- 1.7.2.3