Implemented basic serial code refactoring for upcoming SerialUPDI implementation

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/branches/serial_refactoring@1511 81a1dc3b-b13d-400b-aceb-764788c761c2
This commit is contained in:
Dawid Buchwald 2021-12-08 10:09:52 +00:00 committed by Marius Greuel
parent 2df2587155
commit b6e72dce4c
13 changed files with 136 additions and 46 deletions

View File

@ -84,7 +84,8 @@ static int arduino_open(PROGRAMMER * pgm, char * port)
{
union pinfo pinfo;
strcpy(pgm->port, port);
pinfo.baud = pgm->baudrate? pgm->baudrate: 115200;
pinfo.serialinfo.baud = pgm->baudrate? pgm->baudrate: 115200;
pinfo.serialinfo.cflags = SERIAL_8N1;
if (serial_open(port, pinfo, &pgm->fd)==-1) {
return -1;
}

View File

@ -370,7 +370,8 @@ static int avr910_open(PROGRAMMER * pgm, char * port)
}
strcpy(pgm->port, port);
pinfo.baud = pgm->baudrate;
pinfo.serialinfo.baud = pgm->baudrate;
pinfo.serialinfo.cflags = SERIAL_8N1;
if (serial_open(port, pinfo, &pgm->fd)==-1) {
return -1;
}

View File

@ -427,7 +427,8 @@ static int buspirate_open(struct programmer_t *pgm, char * port)
if(pgm->baudrate == 0)
pgm->baudrate = 115200;
pinfo.baud = pgm->baudrate;
pinfo.serialinfo.baud = pgm->baudrate;
pinfo.serialinfo.cflags = SERIAL_8N1;
strcpy(pgm->port, port);
if (serial_open(port, pinfo, &pgm->fd)==-1) {
return -1;

View File

@ -391,7 +391,8 @@ static int butterfly_open(PROGRAMMER * pgm, char * port)
if(pgm->baudrate == 0) {
pgm->baudrate = 19200;
}
pinfo.baud = pgm->baudrate;
pinfo.serialinfo.baud = pgm->baudrate;
pinfo.serialinfo.cflags = SERIAL_8N1;
if (serial_open(port, pinfo, &pgm->fd)==-1) {
return -1;
}

View File

@ -553,7 +553,7 @@ static int jtagmkI_initialize(PROGRAMMER * pgm, AVRPART * p)
progname, pgm->baudrate);
if (jtagmkI_setparm(pgm, PARM_BITRATE, b) == 0) {
PDATA(pgm)->initial_baudrate = pgm->baudrate; /* don't adjust again later */
serial_setspeed(&pgm->fd, pgm->baudrate);
serial_setparams(&pgm->fd, pgm->baudrate, SERIAL_8N1);
}
}
}
@ -648,9 +648,10 @@ static int jtagmkI_open(PROGRAMMER * pgm, char * port)
for (i = 0; i < sizeof(baudtab) / sizeof(baudtab[0]); i++) {
union pinfo pinfo;
pinfo.baud = baudtab[i].baud;
pinfo.serialinfo.baud = baudtab[i].baud;
pinfo.serialinfo.cflags = SERIAL_8N1;
avrdude_message(MSG_NOTICE2, "%s: jtagmkI_open(): trying to sync at baud rate %ld:\n",
progname, pinfo.baud);
progname, pinfo.serialinfo.baud);
if (serial_open(port, pinfo, &pgm->fd)==-1) {
return -1;
}
@ -697,7 +698,7 @@ static void jtagmkI_close(PROGRAMMER * pgm)
"trying to set baudrate to %d\n",
progname, PDATA(pgm)->initial_baudrate);
if (jtagmkI_setparm(pgm, PARM_BITRATE, b) == 0) {
serial_setspeed(&pgm->fd, pgm->baudrate);
serial_setparams(&pgm->fd, pgm->baudrate, SERIAL_8N1);
}
}
}

View File

@ -1312,7 +1312,7 @@ static int jtagmkII_initialize(PROGRAMMER * pgm, AVRPART * p)
"trying to set baudrate to %d\n",
progname, pgm->baudrate);
if (jtagmkII_setparm(pgm, PAR_BAUD_RATE, &b) == 0)
serial_setspeed(&pgm->fd, pgm->baudrate);
serial_setparams(&pgm->fd, pgm->baudrate, SERIAL_8N1);
}
}
if ((pgm->flag & PGM_FL_IS_JTAG) && pgm->bitclock != 0.0) {
@ -1490,7 +1490,8 @@ static int jtagmkII_open(PROGRAMMER * pgm, char * port)
* a higher baud rate, we switch to it later on, after establishing
* the connection with the ICE.
*/
pinfo.baud = 19200;
pinfo.serialinfo.baud = 19200;
pinfo.serialinfo.cflags = SERIAL_8N1;
/*
* If the port name starts with "usb", divert the serial routines
@ -1542,7 +1543,8 @@ static int jtagmkII_open_dw(PROGRAMMER * pgm, char * port)
* a higher baud rate, we switch to it later on, after establishing
* the connection with the ICE.
*/
pinfo.baud = 19200;
pinfo.serialinfo.baud = 19200;
pinfo.serialinfo.cflags = SERIAL_8N1;
/*
* If the port name starts with "usb", divert the serial routines
@ -1594,7 +1596,8 @@ static int jtagmkII_open_pdi(PROGRAMMER * pgm, char * port)
* a higher baud rate, we switch to it later on, after establishing
* the connection with the ICE.
*/
pinfo.baud = 19200;
pinfo.serialinfo.baud = 19200;
pinfo.serialinfo.cflags = SERIAL_8N1;
/*
* If the port name starts with "usb", divert the serial routines
@ -1647,7 +1650,8 @@ static int jtagmkII_dragon_open(PROGRAMMER * pgm, char * port)
* a higher baud rate, we switch to it later on, after establishing
* the connection with the ICE.
*/
pinfo.baud = 19200;
pinfo.serialinfo.baud = 19200;
pinfo.serialinfo.cflags = SERIAL_8N1;
/*
* If the port name starts with "usb", divert the serial routines
@ -1700,7 +1704,8 @@ static int jtagmkII_dragon_open_dw(PROGRAMMER * pgm, char * port)
* a higher baud rate, we switch to it later on, after establishing
* the connection with the ICE.
*/
pinfo.baud = 19200;
pinfo.serialinfo.baud = 19200;
pinfo.serialinfo.cflags = SERIAL_8N1;
/*
* If the port name starts with "usb", divert the serial routines
@ -1753,7 +1758,8 @@ static int jtagmkII_dragon_open_pdi(PROGRAMMER * pgm, char * port)
* a higher baud rate, we switch to it later on, after establishing
* the connection with the ICE.
*/
pinfo.baud = 19200;
pinfo.serialinfo.baud = 19200;
pinfo.serialinfo.cflags = SERIAL_8N1;
/*
* If the port name starts with "usb", divert the serial routines
@ -3330,7 +3336,8 @@ static int jtagmkII_open32(PROGRAMMER * pgm, char * port)
* a higher baud rate, we switch to it later on, after establishing
* the connection with the ICE.
*/
pinfo.baud = 19200;
pinfo.serialinfo.baud = 19200;
pinfo.serialinfo.cflags = SERIAL_8N1;
/*
* If the port name starts with "usb", divert the serial routines

View File

@ -530,9 +530,34 @@ union filedescriptor
} usb;
};
#define SERIAL_CS5 0x0000
#define SERIAL_CS6 0x0001
#define SERIAL_CS7 0x0002
#define SERIAL_CS8 0x0004
#define SERIAL_NO_CSTOPB 0x0000
#define SERIAL_CSTOPB 0x0008
#define SERIAL_NO_CREAD 0x0000
#define SERIAL_CREAD 0x0010
#define SERIAL_NO_PARITY 0x0000
#define SERIAL_PARENB 0x0020
#define SERIAL_PARODD 0x0040
#define SERIAL_NO_CLOCAL 0x0000
#define SERIAL_CLOCAL 0x0080
#define SERIAL_8N1 (SERIAL_CS8 | SERIAL_NO_CSTOPB | SERIAL_CREAD | SERIAL_NO_PARITY | SERIAL_CLOCAL)
#define SERIAL_8E1 (SERIAL_CS8 | SERIAL_NO_CSTOPB | SERIAL_CREAD | SERIAL_PARENB | SERIAL_CLOCAL)
#define SERIAL_8E2 (SERIAL_CS8 | SERIAL_CSTOPB | SERIAL_CREAD | SERIAL_PARENB | SERIAL_CLOCAL)
union pinfo
{
struct {
long baud;
unsigned long cflags;
} serialinfo;
struct
{
unsigned short vid;
@ -548,7 +573,7 @@ struct serial_device
{
// open should return -1 on error, other values on success
int (*open)(char * port, union pinfo pinfo, union filedescriptor *fd);
int (*setspeed)(union filedescriptor *fd, long baud);
int (*setparams)(union filedescriptor *fd, long baud, unsigned long cflags);
void (*close)(union filedescriptor *fd);
int (*send)(union filedescriptor *fd, const unsigned char * buf, size_t buflen);
@ -570,7 +595,7 @@ extern struct serial_device avrdoper_serdev;
extern struct serial_device usbhid_serdev;
#define serial_open (serdev->open)
#define serial_setspeed (serdev->setspeed)
#define serial_setparams (serdev->setparams)
#define serial_close (serdev->close)
#define serial_send (serdev->send)
#define serial_recv (serdev->recv)

View File

@ -55,6 +55,8 @@ struct baud_mapping {
/* There are a lot more baud rates we could handle, but what's the point? */
static struct baud_mapping baud_lookup_table [] = {
{ 300, B300 },
{ 600, B600 },
{ 1200, B1200 },
{ 2400, B2400 },
{ 4800, B4800 },
@ -96,7 +98,20 @@ static speed_t serial_baud_lookup(long baud)
return baud;
}
static int ser_setspeed(union filedescriptor *fd, long baud)
static tcflag_t translate_flags(unsigned long cflags)
{
return ((cflags & SERIAL_CS5) ? CS5 : 0) |
((cflags & SERIAL_CS6) ? CS6 : 0) |
((cflags & SERIAL_CS7) ? CS7 : 0) |
((cflags & SERIAL_CS8) ? CS8 : 0) |
((cflags & SERIAL_CSTOPB) ? CSTOPB : 0) |
((cflags & SERIAL_CREAD) ? CREAD : 0) |
((cflags & (SERIAL_PARENB | SERIAL_PARODD)) ? PARENB : 0) |
((cflags & SERIAL_PARODD) ? PARODD : 0) |
((cflags & SERIAL_CLOCAL) ? CLOCAL : 0) ;
}
static int ser_setparams(union filedescriptor *fd, long baud, unsigned long cflags)
{
int rc;
struct termios termios;
@ -110,7 +125,7 @@ static int ser_setspeed(union filedescriptor *fd, long baud)
*/
rc = tcgetattr(fd->ifd, &termios);
if (rc < 0) {
avrdude_message(MSG_INFO, "%s: ser_setspeed(): tcgetattr() failed",
avrdude_message(MSG_INFO, "%s: ser_setparams(): tcgetattr() failed",
progname);
return -errno;
}
@ -125,7 +140,7 @@ static int ser_setspeed(union filedescriptor *fd, long baud)
termios.c_iflag = IGNBRK;
termios.c_oflag = 0;
termios.c_lflag = 0;
termios.c_cflag = (CS8 | CREAD | CLOCAL);
termios.c_cflag = translate_flags(cflags);
termios.c_cc[VMIN] = 1;
termios.c_cc[VTIME] = 0;
@ -134,7 +149,7 @@ static int ser_setspeed(union filedescriptor *fd, long baud)
rc = tcsetattr(fd->ifd, TCSANOW, &termios);
if (rc < 0) {
avrdude_message(MSG_INFO, "%s: ser_setspeed(): tcsetattr() failed\n",
avrdude_message(MSG_INFO, "%s: ser_setparams(): tcsetattr() failed\n",
progname);
return -errno;
}
@ -298,7 +313,7 @@ static int ser_open(char * port, union pinfo pinfo, union filedescriptor *fdp)
/*
* set serial line attributes
*/
rc = ser_setspeed(fdp, pinfo.baud);
rc = ser_setparams(fdp, pinfo.serialinfo.baud, pinfo.serialinfo.cflags);
if (rc) {
avrdude_message(MSG_INFO, "%s: ser_open(): can't set attributes for device \"%s\": %s\n",
progname, port, strerror(-rc));
@ -308,7 +323,6 @@ static int ser_open(char * port, union pinfo pinfo, union filedescriptor *fdp)
return 0;
}
static void ser_close(union filedescriptor *fd)
{
/*
@ -501,7 +515,7 @@ static int ser_drain(union filedescriptor *fd, int display)
struct serial_device serial_serdev =
{
.open = ser_open,
.setspeed = ser_setspeed,
.setparams = ser_setparams,
.close = ser_close,
.send = ser_send,
.recv = ser_recv,

View File

@ -54,6 +54,8 @@ static unsigned char serial_over_ethernet = 0;
/* HANDLE hComPort=INVALID_HANDLE_VALUE; */
static struct baud_mapping baud_lookup_table [] = {
{ 300, CBR_300 },
{ 600, CBR_600 },
{ 1200, CBR_1200 },
{ 2400, CBR_2400 },
{ 4800, CBR_4800 },
@ -97,7 +99,7 @@ static BOOL serial_w32SetTimeOut(HANDLE hComPort, DWORD timeout) // in ms
return SetCommTimeouts(hComPort, &ctmo);
}
static int ser_setspeed(union filedescriptor *fd, long baud)
static int ser_setparams(union filedescriptor *fd, long baud, unsigned long cflags)
{
if (serial_over_ethernet) {
return -ENOTTY;
@ -111,9 +113,39 @@ static int ser_setspeed(union filedescriptor *fd, long baud)
dcb.fBinary = 1;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
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;
}
if (!SetCommState(hComPort, &dcb))
return -1;
@ -283,7 +315,7 @@ static int ser_open(char * port, union pinfo pinfo, union filedescriptor *fdp)
}
fdp->pfd = (void *)hComPort;
if (ser_setspeed(fdp, pinfo.baud) != 0)
if (ser_setparams(fdp, pinfo.serialinfo.baud, pinfo.serialinfo.cflags) != 0)
{
CloseHandle(hComPort);
avrdude_message(MSG_INFO, "%s: ser_open(): can't set com-state for \"%s\"\n",
@ -770,7 +802,7 @@ static int ser_drain(union filedescriptor *fd, int display)
struct serial_device serial_serdev =
{
.open = ser_open,
.setspeed = ser_setspeed,
.setparams = ser_setparams,
.close = ser_close,
.send = ser_send,
.recv = ser_recv,

View File

@ -657,7 +657,8 @@ static int stk500_open(PROGRAMMER * pgm, char * port)
{
union pinfo pinfo;
strcpy(pgm->port, port);
pinfo.baud = pgm->baudrate? pgm->baudrate: 115200;
pinfo.serialinfo.baud = pgm->baudrate? pgm->baudrate: 115200;
pinfo.serialinfo.cflags = SERIAL_8N1;
if (serial_open(port, pinfo, &pgm->fd)==-1) {
return -1;
}

View File

@ -1603,12 +1603,12 @@ static void stk500v2_enable(PROGRAMMER * pgm)
static int stk500v2_open(PROGRAMMER * pgm, char * port)
{
union pinfo pinfo = { .baud = 115200 };
union pinfo pinfo = { .serialinfo.baud = 115200, .serialinfo.cflags = SERIAL_8N1 };
DEBUG("STK500V2: stk500v2_open()\n");
if (pgm->baudrate)
pinfo.baud = pgm->baudrate;
pinfo.serialinfo.baud = pgm->baudrate;
PDATA(pgm)->pgmtype = PGMTYPE_UNKNOWN;
@ -1671,12 +1671,12 @@ static int stk500v2_open(PROGRAMMER * pgm, char * port)
static int stk600_open(PROGRAMMER * pgm, char * port)
{
union pinfo pinfo = { .baud = 115200 };
union pinfo pinfo = { .serialinfo.baud = 115200, .serialinfo.cflags = SERIAL_8N1 };
DEBUG("STK500V2: stk600_open()\n");
if (pgm->baudrate)
pinfo.baud = pgm->baudrate;
pinfo.serialinfo.baud = pgm->baudrate;
PDATA(pgm)->pgmtype = PGMTYPE_UNKNOWN;
@ -3392,7 +3392,8 @@ static int stk500v2_jtagmkII_open(PROGRAMMER * pgm, char * port)
* a higher baud rate, we switch to it later on, after establishing
* the connection with the ICE.
*/
pinfo.baud = 19200;
pinfo.serialinfo.baud = 19200;
pinfo.serialinfo.cflags = SERIAL_8N1;
/*
* If the port name starts with "usb", divert the serial routines
@ -3503,7 +3504,8 @@ static int stk500v2_dragon_isp_open(PROGRAMMER * pgm, char * port)
* a higher baud rate, we switch to it later on, after establishing
* the connection with the ICE.
*/
pinfo.baud = 19200;
pinfo.serialinfo.baud = 19200;
pinfo.serialinfo.cflags = SERIAL_8N1;
/*
* If the port name starts with "usb", divert the serial routines
@ -3581,7 +3583,8 @@ static int stk500v2_dragon_hv_open(PROGRAMMER * pgm, char * port)
* a higher baud rate, we switch to it later on, after establishing
* the connection with the ICE.
*/
pinfo.baud = 19200;
pinfo.serialinfo.baud = 19200;
pinfo.serialinfo.cflags = SERIAL_8N1;
/*
* If the port name starts with "usb", divert the serial routines

View File

@ -150,7 +150,8 @@ static int wiring_open(PROGRAMMER * pgm, char * port)
union pinfo pinfo;
strcpy(pgm->port, port);
pinfo.baud = pgm->baudrate ? pgm->baudrate: 115200;
pinfo.serialinfo.baud = pgm->baudrate ? pgm->baudrate: 115200;
pinfo.serialinfo.cflags = SERIAL_8N1;
serial_open(port, pinfo, &pgm->fd);
/* If we have a snoozetime, then we wait and do NOT toggle DTR/RTS */

12
xbee.c
View File

@ -1198,7 +1198,7 @@ static int xbeedev_open(char *port, union pinfo pinfo,
(unsigned int)xbs->xbee_address[6],
(unsigned int)xbs->xbee_address[7]);
if (pinfo.baud) {
if (pinfo.serialinfo.baud) {
/*
* User supplied the correct baud rate.
*/
@ -1222,7 +1222,7 @@ static int xbeedev_open(char *port, union pinfo pinfo,
* plugged in. The doubled clock rate means a doubled serial
* rate. Double 9600 baud == 19200 baud.
*/
pinfo.baud = 19200;
pinfo.serialinfo.baud = 19200;
} else {
/*
* In normal mode, default to 9600.
@ -1234,10 +1234,11 @@ static int xbeedev_open(char *port, union pinfo pinfo,
* XBee baud rate we should select. The baud rate of the AVR
* device is irrelevant.
*/
pinfo.baud = 9600;
pinfo.serialinfo.baud = 9600;
}
pinfo.serialinfo.cflags = SERIAL_8N1;
avrdude_message(MSG_NOTICE, "%s: Baud %ld\n", progname, (long)pinfo.baud);
avrdude_message(MSG_NOTICE, "%s: Baud %ld\n", progname, (long)pinfo.serialinfo.baud);
{
const int rc = xbs->serialDevice->open(tty, pinfo,
@ -1640,7 +1641,8 @@ static int xbee_open(PROGRAMMER *pgm, char *port)
{
union pinfo pinfo;
strcpy(pgm->port, port);
pinfo.baud = pgm->baudrate;
pinfo.serialinfo.baud = pgm->baudrate;
pinfo.serialinfo.cflags = SERIAL_8N1;
/* Wireless is lossier than normal serial */
serial_recv_timeout = 1000;