Turn init_cx() into a libavrdude member

Beyond initializing the cx pointer, it turns out more cleanup might be
required when re-initializing the context pointer.  (Normal CLI only
initializes it once, so that's not an issue there.)

Thus, join all the required actions into an init_cx() function
provided by the library.
This commit is contained in:
Joerg Wunsch
2024-08-13 22:27:03 +02:00
parent 7b7b3d8837
commit d55a7901c4
5 changed files with 20 additions and 14 deletions

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -935,7 +935,7 @@ class adgui(QObject):
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)