2003-03-13 03:52:19 +00:00
|
|
|
/*
|
|
|
|
* avrdude - A Downloader/Uploader for AVR device programmers
|
2004-07-08 23:49:33 +00:00
|
|
|
* Copyright (C) 2003, 2004 Martin J. Thomas <mthomas@rhrk.uni-kl.de>
|
2006-08-31 20:52:47 +00:00
|
|
|
* Copyright (C) 2006 Joerg Wunsch <j@uriah.heep.sax.de>
|
2003-03-13 03:52:19 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2012-11-20 14:03:50 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-03-13 03:52:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/*
|
2003-03-13 19:25:27 +00:00
|
|
|
* Native Win32 serial interface for avrdude.
|
2003-03-13 03:52:19 +00:00
|
|
|
*/
|
|
|
|
|
2014-11-25 20:00:33 +00:00
|
|
|
#include "ac_cfg.h"
|
|
|
|
|
2004-06-24 11:05:07 +00:00
|
|
|
#if defined(WIN32NATIVE)
|
|
|
|
|
2014-11-25 20:00:33 +00:00
|
|
|
#ifdef HAVE_LIBWS2_32
|
|
|
|
/* winsock2.h must be included before windows.h from avrdude.h... */
|
|
|
|
# include <winsock2.h>
|
|
|
|
#endif
|
|
|
|
|
2004-06-24 11:05:07 +00:00
|
|
|
#include <windows.h>
|
2004-07-15 17:29:35 +00:00
|
|
|
#include <stdio.h>
|
2004-06-24 11:05:07 +00:00
|
|
|
#include <ctype.h> /* for isprint */
|
2014-11-25 20:00:33 +00:00
|
|
|
#include <errno.h> /* ENOTTY */
|
2003-03-13 19:25:27 +00:00
|
|
|
|
2014-05-19 10:01:59 +00:00
|
|
|
#include "avrdude.h"
|
|
|
|
#include "libavrdude.h"
|
2004-06-24 11:05:07 +00:00
|
|
|
|
Mega-commit to bring in both, the STK500v2 support from Erik
Walthinsen, as well as JTAG ICE mkII support (by me).
Erik's submission has been cleaned up a little bit, mostly to add his
name and the current year to the copyright of the new file, remove
trailing white space before importing the files, and fix the minor
syntax errors in his avrdude.conf.in additions (missing semicolons).
The JTAG ICE mkII support should be considered alpha to beta quality
at this point. Few things are still to be done, like defering the
hfuse (OCDEN) tweaks until they are really required. Also, for
reasons not yet known, the target MCU doesn't start to run after
signing off from the ICE, it needs a power-cycle first (at least on my
STK500).
Note that for the JTAG ICE, I did change a few things in the internal
API. Notably I made the serial receive timeout configurable by the
backends via an exported variable (done in both the Posix and the
Win32 implementation), and I made the serial_recv() function return a
-1 instead of bailing out with exit(1) upon encountering a receive
timeout (currently only done in the Posix implementation). Both
measures together allow me to receive a datastreem from the ICE at 115
kbps on a somewhat lossy PCI multi-UART card that occasionally drops a
character. The JTAG ICE mkII protocol has enough of safety layers to
allow recovering from these events, but the previous code wasn't
prepared for any kind of recovery. The Win32 change for this still
has to be done, and the traditional drivers need to be converted to
exit(1) upon encountering a timeout (as they're now getting a -1
returned they didn't see before in that case).
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@451 81a1dc3b-b13d-400b-aceb-764788c761c2
2005-05-10 19:17:12 +00:00
|
|
|
long serial_recv_timeout = 5000; /* ms */
|
|
|
|
|
2004-06-24 11:05:07 +00:00
|
|
|
#define W32SERBUFSIZE 1024
|
|
|
|
|
|
|
|
struct baud_mapping {
|
|
|
|
long baud;
|
|
|
|
DWORD speed;
|
|
|
|
};
|
2003-03-13 03:52:19 +00:00
|
|
|
|
2014-11-25 20:00:33 +00:00
|
|
|
static unsigned char serial_over_ethernet = 0;
|
|
|
|
|
2004-06-24 11:05:07 +00:00
|
|
|
/* HANDLE hComPort=INVALID_HANDLE_VALUE; */
|
|
|
|
|
|
|
|
static struct baud_mapping baud_lookup_table [] = {
|
2021-12-08 10:09:52 +00:00
|
|
|
{ 300, CBR_300 },
|
|
|
|
{ 600, CBR_600 },
|
2004-06-24 11:05:07 +00:00
|
|
|
{ 1200, CBR_1200 },
|
|
|
|
{ 2400, CBR_2400 },
|
|
|
|
{ 4800, CBR_4800 },
|
|
|
|
{ 9600, CBR_9600 },
|
|
|
|
{ 19200, CBR_19200 },
|
|
|
|
{ 38400, CBR_38400 },
|
|
|
|
{ 57600, CBR_57600 },
|
|
|
|
{ 115200, CBR_115200 },
|
|
|
|
{ 0, 0 } /* Terminator. */
|
|
|
|
};
|
|
|
|
|
|
|
|
static DWORD serial_baud_lookup(long baud)
|
|
|
|
{
|
|
|
|
struct baud_mapping *map = baud_lookup_table;
|
|
|
|
|
|
|
|
while (map->baud) {
|
|
|
|
if (map->baud == baud)
|
|
|
|
return map->speed;
|
|
|
|
map++;
|
|
|
|
}
|
|
|
|
|
2010-01-08 10:39:18 +00:00
|
|
|
/*
|
|
|
|
* If a non-standard BAUD rate is used, issue
|
|
|
|
* a warning (if we are verbose) and return the raw rate
|
|
|
|
*/
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_NOTICE, "%s: serial_baud_lookup(): Using non-standard baud rate: %ld",
|
2010-01-08 10:39:18 +00:00
|
|
|
progname, baud);
|
|
|
|
|
|
|
|
return baud;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-19 21:38:03 +00:00
|
|
|
static BOOL serial_w32SetTimeOut(HANDLE hComPort, DWORD timeout) // in ms
|
2004-06-24 11:05:07 +00:00
|
|
|
{
|
|
|
|
COMMTIMEOUTS ctmo;
|
|
|
|
ZeroMemory (&ctmo, sizeof(COMMTIMEOUTS));
|
|
|
|
ctmo.ReadIntervalTimeout = timeout;
|
|
|
|
ctmo.ReadTotalTimeoutMultiplier = timeout;
|
|
|
|
ctmo.ReadTotalTimeoutConstant = timeout;
|
|
|
|
|
|
|
|
return SetCommTimeouts(hComPort, &ctmo);
|
|
|
|
}
|
2003-03-13 03:52:19 +00:00
|
|
|
|
2021-12-08 10:09:52 +00:00
|
|
|
static int ser_setparams(union filedescriptor *fd, long baud, unsigned long cflags)
|
2003-03-13 03:52:19 +00:00
|
|
|
{
|
2014-11-25 20:00:33 +00:00
|
|
|
if (serial_over_ethernet) {
|
|
|
|
return -ENOTTY;
|
|
|
|
} else {
|
|
|
|
DCB dcb;
|
|
|
|
HANDLE hComPort = (HANDLE)fd->pfd;
|
|
|
|
|
|
|
|
ZeroMemory (&dcb, sizeof(DCB));
|
|
|
|
dcb.DCBlength = sizeof(DCB);
|
|
|
|
dcb.BaudRate = serial_baud_lookup (baud);
|
|
|
|
dcb.fBinary = 1;
|
|
|
|
dcb.fDtrControl = DTR_CONTROL_DISABLE;
|
|
|
|
dcb.fRtsControl = RTS_CONTROL_DISABLE;
|
2021-12-08 10:09:52 +00:00
|
|
|
switch ((cflags & (SERIAL_CS5 | SERIAL_CS6 | SERIAL_CS7 | SERIAL_CS8))) {
|
|
|
|
case SERIAL_CS5:
|
|
|
|
dcb.ByteSize = 5;
|
|
|
|
break;
|
|
|
|
case SERIAL_CS6:
|
|
|
|
dcb.ByteSize = 6;
|
|
|
|
break;
|
|
|
|
case SERIAL_CS7:
|
|
|
|
dcb.ByteSize = 7;
|
|
|
|
break;
|
|
|
|
case SERIAL_CS8:
|
|
|
|
dcb.ByteSize = 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch ((cflags & (SERIAL_NO_PARITY | SERIAL_PARENB | SERIAL_PARODD))) {
|
|
|
|
case SERIAL_NO_PARITY:
|
|
|
|
dcb.Parity = NOPARITY;
|
|
|
|
break;
|
|
|
|
case SERIAL_PARENB:
|
|
|
|
dcb.Parity = EVENPARITY;
|
|
|
|
break;
|
|
|
|
case SERIAL_PARODD:
|
|
|
|
dcb.Parity = ODDPARITY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch ((cflags & (SERIAL_NO_CSTOPB | SERIAL_CSTOPB))) {
|
|
|
|
case SERIAL_NO_CSTOPB:
|
|
|
|
dcb.StopBits = ONESTOPBIT;
|
|
|
|
break;
|
|
|
|
case SERIAL_CSTOPB:
|
|
|
|
dcb.StopBits = TWOSTOPBITS;
|
|
|
|
break;
|
|
|
|
}
|
2014-11-25 20:00:33 +00:00
|
|
|
|
|
|
|
if (!SetCommState(hComPort, &dcb))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBWS2_32
|
|
|
|
static int
|
|
|
|
net_open(const char *port, union filedescriptor *fdp)
|
|
|
|
{
|
|
|
|
WSADATA wsaData;
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
|
|
|
|
char *hstr, *pstr, *end;
|
|
|
|
unsigned int pnum;
|
|
|
|
int fd;
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
struct hostent *hp;
|
|
|
|
|
|
|
|
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
|
|
|
|
avrdude_message(MSG_INFO, "%s: net_open(): WSAStartup() failed\n", progname);
|
Mega-commit to bring in both, the STK500v2 support from Erik
Walthinsen, as well as JTAG ICE mkII support (by me).
Erik's submission has been cleaned up a little bit, mostly to add his
name and the current year to the copyright of the new file, remove
trailing white space before importing the files, and fix the minor
syntax errors in his avrdude.conf.in additions (missing semicolons).
The JTAG ICE mkII support should be considered alpha to beta quality
at this point. Few things are still to be done, like defering the
hfuse (OCDEN) tweaks until they are really required. Also, for
reasons not yet known, the target MCU doesn't start to run after
signing off from the ICE, it needs a power-cycle first (at least on my
STK500).
Note that for the JTAG ICE, I did change a few things in the internal
API. Notably I made the serial receive timeout configurable by the
backends via an exported variable (done in both the Posix and the
Win32 implementation), and I made the serial_recv() function return a
-1 instead of bailing out with exit(1) upon encountering a receive
timeout (currently only done in the Posix implementation). Both
measures together allow me to receive a datastreem from the ICE at 115
kbps on a somewhat lossy PCI multi-UART card that occasionally drops a
character. The JTAG ICE mkII protocol has enough of safety layers to
allow recovering from these events, but the previous code wasn't
prepared for any kind of recovery. The Win32 change for this still
has to be done, and the traditional drivers need to be converted to
exit(1) upon encountering a timeout (as they're now getting a -1
returned they didn't see before in that case).
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@451 81a1dc3b-b13d-400b-aceb-764788c761c2
2005-05-10 19:17:12 +00:00
|
|
|
return -1;
|
2014-11-25 20:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((hstr = strdup(port)) == NULL) {
|
|
|
|
avrdude_message(MSG_INFO, "%s: net_open(): Out of memory!\n", progname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((pstr = strchr(hstr, ':')) == NULL) || (pstr == hstr)) {
|
|
|
|
avrdude_message(MSG_INFO, "%s: net_open(): Mangled host:port string \"%s\"\n", progname, hstr);
|
|
|
|
free(hstr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Terminate the host section of the description.
|
|
|
|
*/
|
|
|
|
*pstr++ = '\0';
|
|
|
|
|
|
|
|
pnum = strtoul(pstr, &end, 10);
|
|
|
|
|
|
|
|
if ((*pstr == '\0') || (*end != '\0') || (pnum == 0) || (pnum > 65535)) {
|
|
|
|
avrdude_message(MSG_INFO, "%s: net_open(): Bad port number \"%s\"\n", progname, pstr);
|
|
|
|
free(hstr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((hp = gethostbyname(hstr)) == NULL) {
|
|
|
|
avrdude_message(MSG_INFO, "%s: net_open(): unknown host \"%s\"\n", progname, hstr);
|
|
|
|
free(hstr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(hstr);
|
Mega-commit to bring in both, the STK500v2 support from Erik
Walthinsen, as well as JTAG ICE mkII support (by me).
Erik's submission has been cleaned up a little bit, mostly to add his
name and the current year to the copyright of the new file, remove
trailing white space before importing the files, and fix the minor
syntax errors in his avrdude.conf.in additions (missing semicolons).
The JTAG ICE mkII support should be considered alpha to beta quality
at this point. Few things are still to be done, like defering the
hfuse (OCDEN) tweaks until they are really required. Also, for
reasons not yet known, the target MCU doesn't start to run after
signing off from the ICE, it needs a power-cycle first (at least on my
STK500).
Note that for the JTAG ICE, I did change a few things in the internal
API. Notably I made the serial receive timeout configurable by the
backends via an exported variable (done in both the Posix and the
Win32 implementation), and I made the serial_recv() function return a
-1 instead of bailing out with exit(1) upon encountering a receive
timeout (currently only done in the Posix implementation). Both
measures together allow me to receive a datastreem from the ICE at 115
kbps on a somewhat lossy PCI multi-UART card that occasionally drops a
character. The JTAG ICE mkII protocol has enough of safety layers to
allow recovering from these events, but the previous code wasn't
prepared for any kind of recovery. The Win32 change for this still
has to be done, and the traditional drivers need to be converted to
exit(1) upon encountering a timeout (as they're now getting a -1
returned they didn't see before in that case).
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@451 81a1dc3b-b13d-400b-aceb-764788c761c2
2005-05-10 19:17:12 +00:00
|
|
|
|
2014-11-25 20:00:33 +00:00
|
|
|
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
WSAGetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR)&lpMsgBuf,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
avrdude_message(MSG_INFO, "%s: net_open(): Cannot open socket: %s\n", progname, (char *)lpMsgBuf);
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&sockaddr, 0, sizeof(struct sockaddr_in));
|
|
|
|
sockaddr.sin_family = AF_INET;
|
|
|
|
sockaddr.sin_port = htons(pnum);
|
|
|
|
memcpy(&(sockaddr.sin_addr.s_addr), hp->h_addr, sizeof(struct in_addr));
|
|
|
|
|
|
|
|
if (connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
WSAGetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR)&lpMsgBuf,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
avrdude_message(MSG_INFO, "%s: net_open(): Connect failed: %s\n", progname, (char *)lpMsgBuf);
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fdp->ifd = fd;
|
|
|
|
|
|
|
|
serial_over_ethernet = 1;
|
Mega-commit to bring in both, the STK500v2 support from Erik
Walthinsen, as well as JTAG ICE mkII support (by me).
Erik's submission has been cleaned up a little bit, mostly to add his
name and the current year to the copyright of the new file, remove
trailing white space before importing the files, and fix the minor
syntax errors in his avrdude.conf.in additions (missing semicolons).
The JTAG ICE mkII support should be considered alpha to beta quality
at this point. Few things are still to be done, like defering the
hfuse (OCDEN) tweaks until they are really required. Also, for
reasons not yet known, the target MCU doesn't start to run after
signing off from the ICE, it needs a power-cycle first (at least on my
STK500).
Note that for the JTAG ICE, I did change a few things in the internal
API. Notably I made the serial receive timeout configurable by the
backends via an exported variable (done in both the Posix and the
Win32 implementation), and I made the serial_recv() function return a
-1 instead of bailing out with exit(1) upon encountering a receive
timeout (currently only done in the Posix implementation). Both
measures together allow me to receive a datastreem from the ICE at 115
kbps on a somewhat lossy PCI multi-UART card that occasionally drops a
character. The JTAG ICE mkII protocol has enough of safety layers to
allow recovering from these events, but the previous code wasn't
prepared for any kind of recovery. The Win32 change for this still
has to be done, and the traditional drivers need to be converted to
exit(1) upon encountering a timeout (as they're now getting a -1
returned they didn't see before in that case).
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@451 81a1dc3b-b13d-400b-aceb-764788c761c2
2005-05-10 19:17:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-25 20:00:33 +00:00
|
|
|
#endif
|
Mega-commit to bring in both, the STK500v2 support from Erik
Walthinsen, as well as JTAG ICE mkII support (by me).
Erik's submission has been cleaned up a little bit, mostly to add his
name and the current year to the copyright of the new file, remove
trailing white space before importing the files, and fix the minor
syntax errors in his avrdude.conf.in additions (missing semicolons).
The JTAG ICE mkII support should be considered alpha to beta quality
at this point. Few things are still to be done, like defering the
hfuse (OCDEN) tweaks until they are really required. Also, for
reasons not yet known, the target MCU doesn't start to run after
signing off from the ICE, it needs a power-cycle first (at least on my
STK500).
Note that for the JTAG ICE, I did change a few things in the internal
API. Notably I made the serial receive timeout configurable by the
backends via an exported variable (done in both the Posix and the
Win32 implementation), and I made the serial_recv() function return a
-1 instead of bailing out with exit(1) upon encountering a receive
timeout (currently only done in the Posix implementation). Both
measures together allow me to receive a datastreem from the ICE at 115
kbps on a somewhat lossy PCI multi-UART card that occasionally drops a
character. The JTAG ICE mkII protocol has enough of safety layers to
allow recovering from these events, but the previous code wasn't
prepared for any kind of recovery. The Win32 change for this still
has to be done, and the traditional drivers need to be converted to
exit(1) upon encountering a timeout (as they're now getting a -1
returned they didn't see before in that case).
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@451 81a1dc3b-b13d-400b-aceb-764788c761c2
2005-05-10 19:17:12 +00:00
|
|
|
|
|
|
|
|
2014-02-21 13:44:11 +00:00
|
|
|
static int ser_open(char * port, union pinfo pinfo, union filedescriptor *fdp)
|
Mega-commit to bring in both, the STK500v2 support from Erik
Walthinsen, as well as JTAG ICE mkII support (by me).
Erik's submission has been cleaned up a little bit, mostly to add his
name and the current year to the copyright of the new file, remove
trailing white space before importing the files, and fix the minor
syntax errors in his avrdude.conf.in additions (missing semicolons).
The JTAG ICE mkII support should be considered alpha to beta quality
at this point. Few things are still to be done, like defering the
hfuse (OCDEN) tweaks until they are really required. Also, for
reasons not yet known, the target MCU doesn't start to run after
signing off from the ICE, it needs a power-cycle first (at least on my
STK500).
Note that for the JTAG ICE, I did change a few things in the internal
API. Notably I made the serial receive timeout configurable by the
backends via an exported variable (done in both the Posix and the
Win32 implementation), and I made the serial_recv() function return a
-1 instead of bailing out with exit(1) upon encountering a receive
timeout (currently only done in the Posix implementation). Both
measures together allow me to receive a datastreem from the ICE at 115
kbps on a somewhat lossy PCI multi-UART card that occasionally drops a
character. The JTAG ICE mkII protocol has enough of safety layers to
allow recovering from these events, but the previous code wasn't
prepared for any kind of recovery. The Win32 change for this still
has to be done, and the traditional drivers need to be converted to
exit(1) upon encountering a timeout (as they're now getting a -1
returned they didn't see before in that case).
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@451 81a1dc3b-b13d-400b-aceb-764788c761c2
2005-05-10 19:17:12 +00:00
|
|
|
{
|
2004-06-24 11:05:07 +00:00
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
HANDLE hComPort=INVALID_HANDLE_VALUE;
|
2009-07-02 10:23:57 +00:00
|
|
|
char *newname = 0;
|
2004-06-24 11:05:07 +00:00
|
|
|
|
2006-08-31 20:52:47 +00:00
|
|
|
/*
|
|
|
|
* If the port is of the form "net:<host>:<port>", then
|
|
|
|
* handle it as a TCP connection to a terminal server.
|
|
|
|
*/
|
|
|
|
if (strncmp(port, "net:", strlen("net:")) == 0) {
|
2014-11-25 20:00:33 +00:00
|
|
|
#ifdef HAVE_LIBWS2_32
|
|
|
|
return net_open(port + strlen("net:"), fdp);
|
|
|
|
#else
|
|
|
|
avrdude_message(MSG_INFO, "%s: ser_open(): "
|
|
|
|
"not configured for TCP connections\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
progname);
|
2010-10-22 14:44:53 +00:00
|
|
|
return -1;
|
2014-11-25 20:00:33 +00:00
|
|
|
#endif
|
2006-08-31 20:52:47 +00:00
|
|
|
}
|
|
|
|
|
2009-07-02 10:23:57 +00:00
|
|
|
if (strncasecmp(port, "com", strlen("com")) == 0) {
|
|
|
|
|
|
|
|
// prepend "\\\\.\\" to name, required for port # >= 10
|
|
|
|
newname = malloc(strlen("\\\\.\\") + strlen(port) + 1);
|
|
|
|
|
|
|
|
if (newname == 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: ser_open(): out of memory\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
progname);
|
2009-07-02 10:23:57 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
strcpy(newname, "\\\\.\\");
|
|
|
|
strcat(newname, port);
|
|
|
|
|
|
|
|
port = newname;
|
|
|
|
}
|
2004-06-24 11:05:07 +00:00
|
|
|
|
|
|
|
hComPort = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, NULL,
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
|
|
|
if (hComPort == INVALID_HANDLE_VALUE) {
|
2014-11-25 20:00:33 +00:00
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
2004-06-24 11:05:07 +00:00
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|
|
|
(LPTSTR) &lpMsgBuf,
|
|
|
|
0,
|
|
|
|
NULL);
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: ser_open(): can't open device \"%s\": %s\n",
|
2004-06-24 11:05:07 +00:00
|
|
|
progname, port, (char*)lpMsgBuf);
|
|
|
|
LocalFree( lpMsgBuf );
|
2010-10-22 14:44:53 +00:00
|
|
|
return -1;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetupComm(hComPort, W32SERBUFSIZE, W32SERBUFSIZE))
|
|
|
|
{
|
|
|
|
CloseHandle(hComPort);
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: ser_open(): can't set buffers for \"%s\"\n",
|
2004-06-24 11:05:07 +00:00
|
|
|
progname, port);
|
2010-10-22 14:44:53 +00:00
|
|
|
return -1;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
fdp->pfd = (void *)hComPort;
|
2021-12-08 10:09:52 +00:00
|
|
|
if (ser_setparams(fdp, pinfo.serialinfo.baud, pinfo.serialinfo.cflags) != 0)
|
2004-06-24 11:05:07 +00:00
|
|
|
{
|
|
|
|
CloseHandle(hComPort);
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: ser_open(): can't set com-state for \"%s\"\n",
|
2004-06-24 11:05:07 +00:00
|
|
|
progname, port);
|
2010-10-22 14:44:53 +00:00
|
|
|
return -1;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!serial_w32SetTimeOut(hComPort,0))
|
|
|
|
{
|
|
|
|
CloseHandle(hComPort);
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: ser_open(): can't set initial timeout for \"%s\"\n",
|
2004-06-24 11:05:07 +00:00
|
|
|
progname, port);
|
2010-10-22 14:44:53 +00:00
|
|
|
return -1;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
2009-07-02 10:23:57 +00:00
|
|
|
|
|
|
|
if (newname != 0) {
|
|
|
|
free(newname);
|
|
|
|
}
|
2010-10-22 14:44:53 +00:00
|
|
|
return 0;
|
2003-03-13 03:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
static void ser_close(union filedescriptor *fd)
|
2003-03-13 03:52:19 +00:00
|
|
|
{
|
2014-11-25 20:00:33 +00:00
|
|
|
if (serial_over_ethernet) {
|
|
|
|
closesocket(fd->ifd);
|
|
|
|
WSACleanup();
|
|
|
|
} else {
|
|
|
|
HANDLE hComPort=(HANDLE)fd->pfd;
|
|
|
|
if (hComPort != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle (hComPort);
|
2004-06-24 11:05:07 +00:00
|
|
|
|
2014-11-25 20:00:33 +00:00
|
|
|
hComPort = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
2003-03-13 03:52:19 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 06:51:53 +00:00
|
|
|
static int ser_set_dtr_rts(union filedescriptor *fd, int is_on)
|
2009-10-10 01:41:40 +00:00
|
|
|
{
|
2014-11-25 20:00:33 +00:00
|
|
|
if (serial_over_ethernet) {
|
|
|
|
return 0;
|
2009-10-10 01:41:40 +00:00
|
|
|
} else {
|
2014-11-25 20:00:33 +00:00
|
|
|
HANDLE hComPort=(HANDLE)fd->pfd;
|
|
|
|
|
|
|
|
if (is_on) {
|
|
|
|
EscapeCommFunction(hComPort, SETDTR);
|
|
|
|
EscapeCommFunction(hComPort, SETRTS);
|
|
|
|
} else {
|
|
|
|
EscapeCommFunction(hComPort, CLRDTR);
|
|
|
|
EscapeCommFunction(hComPort, CLRRTS);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBWS2_32
|
2014-11-26 09:38:15 +00:00
|
|
|
static int net_send(union filedescriptor *fd, const unsigned char * buf, size_t buflen)
|
2014-11-25 20:00:33 +00:00
|
|
|
{
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
int rc;
|
2014-11-26 09:38:15 +00:00
|
|
|
const unsigned char *p = buf;
|
2014-11-25 20:00:33 +00:00
|
|
|
size_t len = buflen;
|
|
|
|
|
|
|
|
if (fd->ifd < 0) {
|
|
|
|
avrdude_message(MSG_NOTICE, "%s: net_send(): connection not open\n", progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
return 0;
|
2009-10-10 01:41:40 +00:00
|
|
|
}
|
2014-11-25 20:00:33 +00:00
|
|
|
|
|
|
|
if (verbose > 3) {
|
|
|
|
avrdude_message(MSG_TRACE, "%s: Send: ", progname);
|
|
|
|
|
|
|
|
while (buflen) {
|
|
|
|
unsigned char c = *buf;
|
|
|
|
if (isprint(c)) {
|
|
|
|
avrdude_message(MSG_TRACE, "%c ", c);
|
|
|
|
} else {
|
|
|
|
avrdude_message(MSG_TRACE, ". ");
|
|
|
|
}
|
|
|
|
avrdude_message(MSG_TRACE, "[%02x] ", c);
|
|
|
|
|
|
|
|
buf++;
|
|
|
|
buflen--;
|
|
|
|
}
|
|
|
|
|
|
|
|
avrdude_message(MSG_TRACE, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
rc = send(fd->ifd, p, (len > 1024) ? 1024 : len, 0);
|
|
|
|
if (rc < 0) {
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
WSAGetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR)&lpMsgBuf,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
avrdude_message(MSG_INFO, "%s: net_send(): send error: %s\n", progname, (char *)lpMsgBuf);
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
p += rc;
|
|
|
|
len -= rc;
|
|
|
|
}
|
|
|
|
|
2009-10-10 01:41:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-25 20:00:33 +00:00
|
|
|
#endif
|
2009-10-10 01:41:40 +00:00
|
|
|
|
2003-03-13 03:52:19 +00:00
|
|
|
|
2014-07-16 20:02:01 +00:00
|
|
|
static int ser_send(union filedescriptor *fd, const unsigned char * buf, size_t buflen)
|
2003-03-13 03:52:19 +00:00
|
|
|
{
|
2014-11-25 20:00:33 +00:00
|
|
|
#ifdef HAVE_LIBWS2_32
|
|
|
|
if (serial_over_ethernet) {
|
|
|
|
return net_send(fd, buf, buflen);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-24 11:05:07 +00:00
|
|
|
size_t len = buflen;
|
|
|
|
unsigned char c='\0';
|
|
|
|
DWORD written;
|
2014-07-16 20:02:01 +00:00
|
|
|
const unsigned char * b = buf;
|
2004-06-24 11:05:07 +00:00
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
HANDLE hComPort=(HANDLE)fd->pfd;
|
2004-06-24 11:05:07 +00:00
|
|
|
|
|
|
|
if (hComPort == INVALID_HANDLE_VALUE) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: ser_send(): port not open\n",
|
2004-06-24 11:05:07 +00:00
|
|
|
progname);
|
2014-05-16 15:52:25 +00:00
|
|
|
return -1;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!len)
|
2003-03-13 03:52:19 +00:00
|
|
|
return 0;
|
2004-06-24 11:05:07 +00:00
|
|
|
|
|
|
|
if (verbose > 3)
|
|
|
|
{
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_TRACE, "%s: Send: ", progname);
|
2004-06-24 11:05:07 +00:00
|
|
|
|
2005-11-24 15:00:49 +00:00
|
|
|
while (len) {
|
|
|
|
c = *b;
|
2004-06-24 11:05:07 +00:00
|
|
|
if (isprint(c)) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_TRACE, "%c ", c);
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_TRACE, ". ");
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_TRACE, "[%02x] ", c);
|
2005-11-24 15:00:49 +00:00
|
|
|
b++;
|
|
|
|
len--;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "\n");
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serial_w32SetTimeOut(hComPort,500);
|
|
|
|
|
|
|
|
if (!WriteFile (hComPort, buf, buflen, &written, NULL)) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: ser_send(): write error: %s\n",
|
2004-06-24 11:05:07 +00:00
|
|
|
progname, "sorry no info avail"); // TODO
|
2014-05-16 15:52:25 +00:00
|
|
|
return -1;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (written != buflen) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: ser_send(): size/send mismatch\n",
|
2004-06-24 11:05:07 +00:00
|
|
|
progname);
|
2014-05-16 15:52:25 +00:00
|
|
|
return -1;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2003-03-13 03:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-25 20:00:33 +00:00
|
|
|
#ifdef HAVE_LIBWS2_32
|
|
|
|
static int net_recv(union filedescriptor *fd, unsigned char * buf, size_t buflen)
|
|
|
|
{
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
struct timeval timeout, to2;
|
|
|
|
fd_set rfds;
|
|
|
|
int nfds;
|
|
|
|
int rc;
|
|
|
|
unsigned char *p = buf;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
if (fd->ifd < 0) {
|
|
|
|
avrdude_message(MSG_INFO, "%s: net_recv(): connection not open\n", progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout.tv_sec = serial_recv_timeout / 1000L;
|
|
|
|
timeout.tv_usec = (serial_recv_timeout % 1000L) * 1000;
|
|
|
|
to2 = timeout;
|
|
|
|
|
|
|
|
while (len < buflen) {
|
|
|
|
reselect:
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(fd->ifd, &rfds);
|
|
|
|
|
|
|
|
nfds = select(fd->ifd + 1, &rfds, NULL, NULL, &to2);
|
|
|
|
if (nfds == 0) {
|
|
|
|
if (verbose > 1) {
|
|
|
|
avrdude_message(MSG_NOTICE, "%s: ser_recv(): programmer is not responding\n", progname);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
} else if (nfds == -1) {
|
|
|
|
if (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEINPROGRESS) {
|
|
|
|
avrdude_message(MSG_NOTICE, "%s: ser_recv(): programmer is not responding, reselecting\n", progname);
|
|
|
|
goto reselect;
|
|
|
|
} else {
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
WSAGetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR)&lpMsgBuf,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
avrdude_message(MSG_INFO, "%s: ser_recv(): select(): %s\n", progname, (char *)lpMsgBuf);
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = recv(fd->ifd, p, (buflen - len > 1024) ? 1024 : buflen - len, 0);
|
|
|
|
if (rc < 0) {
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
WSAGetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR)&lpMsgBuf,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
avrdude_message(MSG_INFO, "%s: ser_recv(): read error: %s\n", progname, (char *)lpMsgBuf);
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
p += rc;
|
|
|
|
len += rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
|
|
|
|
if (verbose > 3) {
|
|
|
|
avrdude_message(MSG_TRACE, "%s: Recv: ", progname);
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
unsigned char c = *p;
|
|
|
|
if (isprint(c)) {
|
|
|
|
avrdude_message(MSG_TRACE, "%c ", c);
|
|
|
|
} else {
|
|
|
|
avrdude_message(MSG_TRACE, ". ");
|
|
|
|
}
|
|
|
|
avrdude_message(MSG_TRACE, "[%02x] ", c);
|
|
|
|
|
|
|
|
p++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
avrdude_message(MSG_TRACE, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
static int ser_recv(union filedescriptor *fd, unsigned char * buf, size_t buflen)
|
2003-03-13 03:52:19 +00:00
|
|
|
{
|
2014-11-25 20:00:33 +00:00
|
|
|
#ifdef HAVE_LIBWS2_32
|
|
|
|
if (serial_over_ethernet) {
|
|
|
|
return net_recv(fd, buf, buflen);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-24 11:05:07 +00:00
|
|
|
unsigned char c;
|
2006-12-11 12:47:35 +00:00
|
|
|
unsigned char * p = buf;
|
2004-06-24 11:05:07 +00:00
|
|
|
DWORD read;
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
HANDLE hComPort=(HANDLE)fd->pfd;
|
2004-06-24 11:05:07 +00:00
|
|
|
|
|
|
|
if (hComPort == INVALID_HANDLE_VALUE) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: ser_read(): port not open\n",
|
2004-06-24 11:05:07 +00:00
|
|
|
progname);
|
2014-05-16 15:52:25 +00:00
|
|
|
return -1;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
Mega-commit to bring in both, the STK500v2 support from Erik
Walthinsen, as well as JTAG ICE mkII support (by me).
Erik's submission has been cleaned up a little bit, mostly to add his
name and the current year to the copyright of the new file, remove
trailing white space before importing the files, and fix the minor
syntax errors in his avrdude.conf.in additions (missing semicolons).
The JTAG ICE mkII support should be considered alpha to beta quality
at this point. Few things are still to be done, like defering the
hfuse (OCDEN) tweaks until they are really required. Also, for
reasons not yet known, the target MCU doesn't start to run after
signing off from the ICE, it needs a power-cycle first (at least on my
STK500).
Note that for the JTAG ICE, I did change a few things in the internal
API. Notably I made the serial receive timeout configurable by the
backends via an exported variable (done in both the Posix and the
Win32 implementation), and I made the serial_recv() function return a
-1 instead of bailing out with exit(1) upon encountering a receive
timeout (currently only done in the Posix implementation). Both
measures together allow me to receive a datastreem from the ICE at 115
kbps on a somewhat lossy PCI multi-UART card that occasionally drops a
character. The JTAG ICE mkII protocol has enough of safety layers to
allow recovering from these events, but the previous code wasn't
prepared for any kind of recovery. The Win32 change for this still
has to be done, and the traditional drivers need to be converted to
exit(1) upon encountering a timeout (as they're now getting a -1
returned they didn't see before in that case).
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@451 81a1dc3b-b13d-400b-aceb-764788c761c2
2005-05-10 19:17:12 +00:00
|
|
|
serial_w32SetTimeOut(hComPort, serial_recv_timeout);
|
2004-06-24 11:05:07 +00:00
|
|
|
|
|
|
|
if (!ReadFile(hComPort, buf, buflen, &read, NULL)) {
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|
|
|
(LPTSTR) &lpMsgBuf,
|
|
|
|
0,
|
|
|
|
NULL );
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: ser_recv(): read error: %s\n",
|
2004-06-24 11:05:07 +00:00
|
|
|
progname, (char*)lpMsgBuf);
|
|
|
|
LocalFree( lpMsgBuf );
|
2014-05-16 15:52:25 +00:00
|
|
|
return -1;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
2011-12-17 20:18:36 +00:00
|
|
|
/* time out detected */
|
|
|
|
if (read == 0) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_NOTICE2, "%s: ser_recv(): programmer is not responding\n",
|
2014-05-18 08:41:46 +00:00
|
|
|
progname);
|
2011-12-17 20:18:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-06-24 11:05:07 +00:00
|
|
|
p = buf;
|
|
|
|
|
|
|
|
if (verbose > 3)
|
|
|
|
{
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_TRACE, "%s: Recv: ", progname);
|
2004-06-24 11:05:07 +00:00
|
|
|
|
2007-10-29 21:51:07 +00:00
|
|
|
while (read) {
|
2004-06-24 11:05:07 +00:00
|
|
|
c = *p;
|
|
|
|
if (isprint(c)) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_TRACE, "%c ", c);
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_TRACE, ". ");
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_TRACE, "[%02x] ", c);
|
2004-06-24 11:05:07 +00:00
|
|
|
|
|
|
|
p++;
|
2007-10-29 21:51:07 +00:00
|
|
|
read--;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "\n");
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
2003-03-13 03:52:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-27 21:15:55 +00:00
|
|
|
#ifdef HAVE_LIBWS2_32
|
|
|
|
static int net_drain(union filedescriptor *fd, int display)
|
|
|
|
{
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
struct timeval timeout;
|
|
|
|
fd_set rfds;
|
|
|
|
int nfds;
|
|
|
|
unsigned char buf;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (fd->ifd < 0) {
|
|
|
|
avrdude_message(MSG_INFO, "%s: ser_drain(): connection not open\n", progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (display) {
|
|
|
|
avrdude_message(MSG_INFO, "drain>");
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 250000;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(fd->ifd, &rfds);
|
|
|
|
|
|
|
|
reselect:
|
|
|
|
nfds = select(fd->ifd + 1, &rfds, NULL, NULL, &timeout);
|
|
|
|
if (nfds == 0) {
|
|
|
|
if (display) {
|
|
|
|
avrdude_message(MSG_INFO, "<drain\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (nfds == -1) {
|
|
|
|
if (WSAGetLastError() == WSAEINTR || WSAGetLastError() == WSAEINPROGRESS) {
|
|
|
|
avrdude_message(MSG_NOTICE, "%s: ser_drain(): programmer is not responding, reselecting\n", progname);
|
|
|
|
goto reselect;
|
|
|
|
} else {
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
WSAGetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR)&lpMsgBuf,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
avrdude_message(MSG_INFO, "%s: ser_drain(): select(): %s\n", progname, (char *)lpMsgBuf);
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = recv(fd->ifd, &buf, 1, 0);
|
|
|
|
if (rc < 0) {
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
WSAGetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPTSTR)&lpMsgBuf,
|
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
avrdude_message(MSG_INFO, "%s: ser_drain(): read error: %s\n", progname, (char *)lpMsgBuf);
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (display) {
|
|
|
|
avrdude_message(MSG_INFO, "%02x ", buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2003-03-13 03:52:19 +00:00
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
static int ser_drain(union filedescriptor *fd, int display)
|
2003-03-13 03:52:19 +00:00
|
|
|
{
|
2021-11-27 21:15:55 +00:00
|
|
|
#ifdef HAVE_LIBWS2_32
|
|
|
|
if (serial_over_ethernet) {
|
|
|
|
return net_drain(fd, display);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-24 11:05:07 +00:00
|
|
|
// int rc;
|
|
|
|
unsigned char buf[10];
|
|
|
|
BOOL readres;
|
|
|
|
DWORD read;
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
HANDLE hComPort=(HANDLE)fd->pfd;
|
2004-06-24 11:05:07 +00:00
|
|
|
|
|
|
|
if (hComPort == INVALID_HANDLE_VALUE) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: ser_drain(): port not open\n",
|
2004-06-24 11:05:07 +00:00
|
|
|
progname);
|
2014-05-16 15:52:25 +00:00
|
|
|
return -1;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serial_w32SetTimeOut(hComPort,250);
|
|
|
|
|
|
|
|
if (display) {
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "drain>");
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
readres=ReadFile(hComPort, buf, 1, &read, NULL);
|
|
|
|
if (!readres) {
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|
|
|
(LPTSTR) &lpMsgBuf,
|
|
|
|
0,
|
|
|
|
NULL );
|
2014-06-13 20:07:40 +00:00
|
|
|
avrdude_message(MSG_INFO, "%s: ser_drain(): read error: %s\n",
|
2004-06-24 11:05:07 +00:00
|
|
|
progname, (char*)lpMsgBuf);
|
|
|
|
LocalFree( lpMsgBuf );
|
2014-05-16 15:52:25 +00:00
|
|
|
return -1;
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (read) { // data avail
|
2014-06-13 20:07:40 +00:00
|
|
|
if (display) avrdude_message(MSG_INFO, "%02x ", buf[0]);
|
2004-06-24 11:05:07 +00:00
|
|
|
}
|
|
|
|
else { // no more data
|
2014-06-13 20:07:40 +00:00
|
|
|
if (display) avrdude_message(MSG_INFO, "<drain\n");
|
2004-06-24 11:05:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // while
|
2003-03-13 03:52:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-19 21:38:03 +00:00
|
|
|
struct serial_device serial_serdev =
|
|
|
|
{
|
|
|
|
.open = ser_open,
|
2021-12-08 10:09:52 +00:00
|
|
|
.setparams = ser_setparams,
|
2005-06-19 21:38:03 +00:00
|
|
|
.close = ser_close,
|
|
|
|
.send = ser_send,
|
|
|
|
.recv = ser_recv,
|
|
|
|
.drain = ser_drain,
|
2009-10-10 01:41:40 +00:00
|
|
|
.set_dtr_rts = ser_set_dtr_rts,
|
2006-10-27 08:45:47 +00:00
|
|
|
.flags = SERDEV_FL_CANSETSPEED,
|
2005-06-19 21:38:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct serial_device *serdev = &serial_serdev;
|
|
|
|
|
2004-06-24 11:05:07 +00:00
|
|
|
#endif /* WIN32NATIVE */
|