diff --git a/src/avr.c b/src/avr.c index 55542f438..0ac0f7837 100644 --- a/src/avr.c +++ b/src/avr.c @@ -614,6 +614,20 @@ double avr_timestamp() { return avr_ustimestamp()/1e6; } +/* + * Initialize the global context pointer cx. + * + * This must be called once at program startup (with a NULL argument), + * and at each (re-)initialization of a programmer (with the + * respective programmer as argument). + */ +void init_cx(PROGRAMMER *pgm) { + if (pgm) + pgm->flag = 0; // Clear out remnants of previous session(s) + mmt_free(cx); + cx = mmt_malloc(sizeof *cx); // Allocate and initialise context structure + (void) avr_ustimestamp(); // Base timestamps from program start +} int avr_read_byte_silent(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *mem, unsigned long addr, unsigned char *datap) { diff --git a/src/libavrdude.h b/src/libavrdude.h index a3c04c056..3b7414198 100644 --- a/src/libavrdude.h +++ b/src/libavrdude.h @@ -1183,6 +1183,8 @@ uint64_t avr_mstimestamp(void); double avr_timestamp(void); +void init_cx(PROGRAMMER *pgm); + int avr_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *mem, unsigned long addr, unsigned char data); diff --git a/src/libavrdude.i b/src/libavrdude.i index a4204dc86..8efc82eb9 100644 --- a/src/libavrdude.i +++ b/src/libavrdude.i @@ -53,15 +53,6 @@ static PyObject *msg_cb = NULL; static PyObject *progress_cb = NULL; static void swig_progress(int percent, double etime, const char *hdr, int finish); -#define mmt_malloc(n) cfg_malloc(__func__, n) -#define mmt_free(p) free(p) - -void init_cx(void) { - mmt_free(cx); - cx = mmt_malloc(sizeof *cx); // Allocate and initialise context structure - (void) avr_ustimestamp(); // Base timestamps from program start -} - void set_msg_callback(PyObject *PyFunc) { if (PyFunc == Py_None) { if (msg_cb) @@ -267,8 +258,6 @@ typedef struct avrmem_alias AVRMEM_ALIAS; typedef struct programmer PROGRAMMER; typedef void pgm_initpgm(PROGRAMMER*); -void init_cx(void); - enum msglvl { MSG_EXT_ERROR = (-3), // OS-type error, no -v option, can be suppressed with -qqqqq MSG_ERROR = (-2), // Avrdude error, no -v option, can be suppressed with -qqqq @@ -599,6 +588,8 @@ typedef struct programmer { %mutable; %clear struct programmer *pgm; +void init_cx(PROGRAMMER *pgm = NULL); + // Config file handling int init_config(void); diff --git a/src/main.c b/src/main.c index a43a3aa30..427788942 100644 --- a/src/main.c +++ b/src/main.c @@ -663,8 +663,7 @@ int main(int argc, char * argv []) char * logfile; /* Use logfile rather than stderr for diagnostics */ enum updateflags uflags = UF_AUTO_ERASE | UF_VERIFY; /* Flags for do_op() */ - cx = mmt_malloc(sizeof *cx); // Allocate and initialise context structure - (void) avr_ustimestamp(); // Base timestamps from program start + init_cx(NULL); #ifdef _MSC_VER _set_printf_count_output(1); diff --git a/src/python/adgui.py b/src/python/adgui.py index 4fef35ba9..56680d0e0 100755 --- a/src/python/adgui.py +++ b/src/python/adgui.py @@ -935,7 +935,7 @@ def loglevel_changed(self, checked: bool): def start_programmer(self): if self.connected: return - ad.init_cx() + ad.init_cx(self.pgm) self.pgm.initpgm() self.pgm.setup() rv = self.pgm.open(self.port)