From d0a21c17d1a95d67c1858e91a45914d98613e0da Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Mon, 22 Apr 2024 13:41:26 +0100 Subject: [PATCH 1/4] Move static variables to PDATA for linuxspi.c --- src/linuxspi.c | 55 +++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/src/linuxspi.c b/src/linuxspi.c index 270ed0f8..9f2ee267 100644 --- a/src/linuxspi.c +++ b/src/linuxspi.c @@ -62,16 +62,14 @@ #define LINUXSPI "linuxspi" -/* - * Private data for this programmer. - */ +// Private data for this programmer struct pdata { int disable_no_cs; + int fd_spidev, fd_gpiochip, fd_linehandle; }; -#define PDATA(pgm) ((struct pdata *)(pgm->cookie)) - -static int fd_spidev, fd_gpiochip, fd_linehandle; +// Use private programmer data as if they were a global structure my +#define my (*(struct pdata *)(pgm->cookie)) /** * @brief Sends/receives a message in full duplex mode @@ -91,7 +89,8 @@ static int linuxspi_spi_duplex(const PROGRAMMER *pgm, const unsigned char *tx, u }; errno = 0; - ret = ioctl(fd_spidev, SPI_IOC_MESSAGE(1), &tr); + + ret = ioctl(my.fd_spidev, SPI_IOC_MESSAGE(1), &tr); if (ret != len) { int ioctl_errno = errno; msg_error("\n"); @@ -118,10 +117,10 @@ static int linuxspi_reset_mcu(const PROGRAMMER *pgm, bool active) { /* * Set the reset state and keep it. The pin will be released and set back to - * its initial value, once the fd_gpiochip is closed. + * its initial value, once the my.fd_gpiochip is closed. */ data.values[0] = active ^ !(pgm->pinno[PIN_AVR_RESET] & PIN_INVERSE); - ret = ioctl(fd_linehandle, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data); + ret = ioctl(my.fd_linehandle, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data); #ifdef GPIO_V2_LINE_SET_VALUES_IOCTL if (ret == -1) { struct gpio_v2_line_values val; @@ -129,7 +128,7 @@ static int linuxspi_reset_mcu(const PROGRAMMER *pgm, bool active) { val.mask = 1; val.bits = active ^ !(pgm->pinno[PIN_AVR_RESET] & PIN_INVERSE); - ret = ioctl(fd_linehandle, GPIO_V2_LINE_SET_VALUES_IOCTL, &val); + ret = ioctl(my.fd_linehandle, GPIO_V2_LINE_SET_VALUES_IOCTL, &val); } #endif if (ret == -1) { @@ -179,26 +178,26 @@ static int linuxspi_open(PROGRAMMER *pgm, const char *pt) { } pgm->port = port; - fd_spidev = open(pgm->port, O_RDWR); - if (fd_spidev < 0) { + my.fd_spidev = open(pgm->port, O_RDWR); + if (my.fd_spidev < 0) { pmsg_ext_error("unable to open the spidev device %s: %s\n", pgm->port, strerror(errno)); return -1; } uint32_t mode = SPI_MODE_0; - if (!PDATA(pgm)->disable_no_cs) + if (!my.disable_no_cs) mode |= SPI_NO_CS; - ret = ioctl(fd_spidev, SPI_IOC_WR_MODE32, &mode); + ret = ioctl(my.fd_spidev, SPI_IOC_WR_MODE32, &mode); if (ret == -1) { int ioctl_errno = errno; pmsg_ext_error("unable to set SPI mode %02X on %s: %s\n", mode, spidev, strerror(errno)); - if(ioctl_errno == EINVAL && !PDATA(pgm)->disable_no_cs) + if(ioctl_errno == EINVAL && !my.disable_no_cs) pmsg_error("try -x disable_no_cs\n"); goto close_spidev; } - fd_gpiochip = open(gpiochip, 0); - if (fd_gpiochip < 0) { + my.fd_gpiochip = open(gpiochip, 0); + if (my.fd_gpiochip < 0) { pmsg_ext_error("unable to open the gpiochip %s: %s\n", gpiochip, strerror(errno)); ret = -1; goto close_spidev; @@ -210,9 +209,9 @@ static int linuxspi_open(PROGRAMMER *pgm, const char *pt) { req.default_values[0] = !!(pgm->pinno[PIN_AVR_RESET] & PIN_INVERSE); req.flags = GPIOHANDLE_REQUEST_OUTPUT; - ret = ioctl(fd_gpiochip, GPIO_GET_LINEHANDLE_IOCTL, &req); + ret = ioctl(my.fd_gpiochip, GPIO_GET_LINEHANDLE_IOCTL, &req); if (ret != -1) - fd_linehandle = req.fd; + my.fd_linehandle = req.fd; #ifdef GPIO_V2_GET_LINE_IOCTL if (ret == -1) { struct gpio_v2_line_request reqv2; @@ -227,9 +226,9 @@ static int linuxspi_open(PROGRAMMER *pgm, const char *pt) { reqv2.config.attrs[0].mask = 1; reqv2.num_lines = 1; - ret = ioctl(fd_gpiochip, GPIO_V2_GET_LINE_IOCTL, &reqv2); + ret = ioctl(my.fd_gpiochip, GPIO_V2_GET_LINE_IOCTL, &reqv2); if (ret != -1) - fd_linehandle = reqv2.fd; + my.fd_linehandle = reqv2.fd; } #endif if (ret == -1) { @@ -254,11 +253,11 @@ static int linuxspi_open(PROGRAMMER *pgm, const char *pt) { return 0; close_out: - close(fd_linehandle); + close(my.fd_linehandle); close_gpiochip: - close(fd_gpiochip); + close(my.fd_gpiochip); close_spidev: - close(fd_spidev); + close(my.fd_spidev); return ret; } @@ -276,9 +275,9 @@ static void linuxspi_close(PROGRAMMER *pgm) { break; } - close(fd_linehandle); - close(fd_spidev); - close(fd_gpiochip); + close(my.fd_linehandle); + close(my.fd_spidev); + close(my.fd_gpiochip); } static void linuxspi_disable(const PROGRAMMER* pgm) { @@ -410,7 +409,7 @@ static int linuxspi_parseextparams(const PROGRAMMER *pgm, const LISTID extparms) extended_param = ldata(ln); if (str_eq(extended_param, "disable_no_cs")) { - PDATA(pgm)->disable_no_cs = 1; + my.disable_no_cs = 1; continue; } if (str_eq(extended_param, "help")) { From bd730da36b1a154b50032bb8921e2afe0dcbf04e Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Mon, 22 Apr 2024 13:45:23 +0100 Subject: [PATCH 2/4] Utilise magic memory tree interface --- src/avrdude.h | 1 + src/linuxspi.c | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/avrdude.h b/src/avrdude.h index c679a654..6b777d30 100644 --- a/src/avrdude.h +++ b/src/avrdude.h @@ -44,6 +44,7 @@ extern const char *pgmid; // Programmer -c string #define mmt_strdup(s) cfg_strdup(__func__, s) #define mmt_malloc(n) cfg_malloc(__func__, n) #define mmt_realloc(p, n) cfg_realloc(__func__, p, n) +#define mmt_free(p) free(p) int avrdude_message2(FILE *fp, int lno, const char *file, const char *func, int msgmode, int msglvl, const char *format, ...); diff --git a/src/linuxspi.c b/src/linuxspi.c index 9f2ee267..6a30368e 100644 --- a/src/linuxspi.c +++ b/src/linuxspi.c @@ -104,11 +104,12 @@ static int linuxspi_spi_duplex(const PROGRAMMER *pgm, const unsigned char *tx, u } static void linuxspi_setup(PROGRAMMER *pgm) { - pgm->cookie = cfg_malloc("linuxspi_setup()", sizeof(struct pdata)); + pgm->cookie = mmt_malloc(sizeof(struct pdata)); } static void linuxspi_teardown(PROGRAMMER* pgm) { - free(pgm->cookie); + mmt_free(pgm->cookie); + pgm->cookie = NULL; } static int linuxspi_reset_mcu(const PROGRAMMER *pgm, bool active) { @@ -379,7 +380,7 @@ static int linuxspi_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) { } static int linuxspi_parseexitspecs(PROGRAMMER *pgm, const char *sp) { - char *cp, *s, *str = cfg_strdup("linuxspi_parseextitspecs()", sp); + char *cp, *s, *str = mmt_strdup(sp); s = str; while ((cp = strtok(s, ","))) { @@ -392,11 +393,11 @@ static int linuxspi_parseexitspecs(PROGRAMMER *pgm, const char *sp) { pgm->exit_reset = EXIT_RESET_DISABLED; continue; } - free(str); + mmt_free(str); return -1; } - free(str); + mmt_free(str); return 0; } From 38d8bf0b7aa452f0a3174c0d25b9c89a240cb8b4 Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Mon, 22 Apr 2024 13:47:46 +0100 Subject: [PATCH 3/4] Remove exit from programmer code in linuxspi.c --- src/linuxspi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linuxspi.c b/src/linuxspi.c index 6a30368e..caac1554 100644 --- a/src/linuxspi.c +++ b/src/linuxspi.c @@ -417,7 +417,7 @@ static int linuxspi_parseextparams(const PROGRAMMER *pgm, const LISTID extparms) msg_error("%s -c %s extended options:\n", progname, pgmid); msg_error(" -xdisable_no_cs Do not use the SPI_NO_CS bit for the SPI driver\n"); msg_error(" -xhelp Show this help menu and exit\n"); - exit(0); + return LIBAVRDUDE_SOFTFAIL; } pmsg_error("invalid extended parameter '%s'\n", extended_param); From bedfaed6c6cef06abf0914f875e6b5616fe886de Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Mon, 22 Apr 2024 15:17:45 +0100 Subject: [PATCH 4/4] return LIBAVRDUDE_EXIT instead of exit(0) --- src/libavrdude.h | 1 + src/linuxspi.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libavrdude.h b/src/libavrdude.h index 453b22a0..60cf4ebf 100644 --- a/src/libavrdude.h +++ b/src/libavrdude.h @@ -59,6 +59,7 @@ typedef uint32_t pinmask_t; #define LIBAVRDUDE_NOTSUPPORTED (-2) // operation not supported #define LIBAVRDUDE_SOFTFAIL (-3) // returned, eg, by avr_signature() if caller // might proceed with chip erase +#define LIBAVRDUDE_EXIT (-4) // End all operations in this session /* formerly lists.h */ diff --git a/src/linuxspi.c b/src/linuxspi.c index caac1554..550c3b35 100644 --- a/src/linuxspi.c +++ b/src/linuxspi.c @@ -417,7 +417,7 @@ static int linuxspi_parseextparams(const PROGRAMMER *pgm, const LISTID extparms) msg_error("%s -c %s extended options:\n", progname, pgmid); msg_error(" -xdisable_no_cs Do not use the SPI_NO_CS bit for the SPI driver\n"); msg_error(" -xhelp Show this help menu and exit\n"); - return LIBAVRDUDE_SOFTFAIL; + return LIBAVRDUDE_EXIT; } pmsg_error("invalid extended parameter '%s'\n", extended_param);