From b6e72dce4cc3604e38ff6469f4b48f1872cddbb3 Mon Sep 17 00:00:00 2001 From: Dawid Buchwald Date: Wed, 8 Dec 2021 10:09:52 +0000 Subject: [PATCH] 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 --- arduino.c | 3 ++- avr910.c | 3 ++- buspirate.c | 3 ++- butterfly.c | 3 ++- jtagmkI.c | 9 +++++---- jtagmkII.c | 23 +++++++++++++++-------- libavrdude.h | 31 ++++++++++++++++++++++++++++--- ser_posix.c | 28 +++++++++++++++++++++------- ser_win32.c | 44 ++++++++++++++++++++++++++++++++++++++------ stk500.c | 3 ++- stk500v2.c | 17 ++++++++++------- wiring.c | 3 ++- xbee.c | 12 +++++++----- 13 files changed, 136 insertions(+), 46 deletions(-) diff --git a/arduino.c b/arduino.c index 566f56ab..dbaafef2 100644 --- a/arduino.c +++ b/arduino.c @@ -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; } diff --git a/avr910.c b/avr910.c index bcc71d60..3e14fdc1 100644 --- a/avr910.c +++ b/avr910.c @@ -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; } diff --git a/buspirate.c b/buspirate.c index 90b50845..f2a6d9ec 100644 --- a/buspirate.c +++ b/buspirate.c @@ -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; diff --git a/butterfly.c b/butterfly.c index de9a3175..1d5fafdf 100644 --- a/butterfly.c +++ b/butterfly.c @@ -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; } diff --git a/jtagmkI.c b/jtagmkI.c index 2a5f27e4..fc5f3f70 100644 --- a/jtagmkI.c +++ b/jtagmkI.c @@ -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); } } } diff --git a/jtagmkII.c b/jtagmkII.c index df10f9f9..be6a4ed6 100644 --- a/jtagmkII.c +++ b/jtagmkII.c @@ -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 diff --git a/libavrdude.h b/libavrdude.h index 2431a218..0d1bf3a3 100644 --- a/libavrdude.h +++ b/libavrdude.h @@ -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 { - long baud; + 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) diff --git a/ser_posix.c b/ser_posix.c index 2f40d0e4..3d8e15a1 100644 --- a/ser_posix.c +++ b/ser_posix.c @@ -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, diff --git a/ser_win32.c b/ser_win32.c index 5fc17b1b..25412cf0 100644 --- a/ser_win32.c +++ b/ser_win32.c @@ -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; - dcb.ByteSize = 8; - dcb.Parity = NOPARITY; - dcb.StopBits = ONESTOPBIT; + 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, diff --git a/stk500.c b/stk500.c index ebb22a27..88944afc 100644 --- a/stk500.c +++ b/stk500.c @@ -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; } diff --git a/stk500v2.c b/stk500v2.c index 4687ad6b..f6c5b1ca 100644 --- a/stk500v2.c +++ b/stk500v2.c @@ -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 diff --git a/wiring.c b/wiring.c index 1dc4d6a6..c0a68055 100644 --- a/wiring.c +++ b/wiring.c @@ -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 */ diff --git a/xbee.c b/xbee.c index 454dc6de..1f974e55 100644 --- a/xbee.c +++ b/xbee.c @@ -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;