Skip to content

Commit

Permalink
Turn init_cx() into a libavrdude member
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dl8dtl committed Aug 13, 2024
1 parent 7b7b3d8 commit d55a790
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/avr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/libavrdude.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 2 additions & 11 deletions src/libavrdude.i
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/python/adgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d55a790

Please sign in to comment.