Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new functions (#1570): get_vtarget, get_varef, get_fosc, get_sck_period #1574

Merged
merged 10 commits into from
Dec 1, 2023
42 changes: 39 additions & 3 deletions src/jtagmkI.c
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,37 @@ static int jtagmkI_set_sck_period(const PROGRAMMER *pgm, double v) {
}


static int jtagmkI_get_sck_period(const PROGRAMMER *pgm, double *v) {
unsigned char dur = 0;
if (jtagmkI_getparm(pgm, PARM_CLOCK, &dur) < 0)
return -1;
if (dur == JTAG_BITRATE_1_MHz)
*v = 1e6;
else if (dur == JTAG_BITRATE_500_kHz)
*v = 500e3;
else if (dur == JTAG_BITRATE_250_kHz)
*v = 250e3;
else if (dur == JTAG_BITRATE_125_kHz)
*v = 125e3;
else { // something went wrong
pmsg_error("wrong JTAG_BITRATE ID %02X\n", dur);
return -1;
}
return 0;
}


static int jtagmkI_get_vtarget(const PROGRAMMER *pgm, double *v) {
unsigned char vtarget = 0;
if (jtagmkI_getparm(pgm, PARM_OCD_VTARGET, &vtarget) < 0) {
pmsg_error("jtagmkI_getparm PARM_OCD_VTARGET failed\n");
return -1;
}
*v = 6.25 * (unsigned)vtarget / 255.0;
return 0;
}


/*
* Read an emulator parameter. The result is exactly one byte,
* multi-byte parameters get two different parameter names for
Expand Down Expand Up @@ -1158,7 +1189,8 @@ static void jtagmkI_display(const PROGRAMMER *pgm, const char *p) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually my mistake. I just realized that the two lines (in jtagmkI.c):

  msg_info("%sICE HW version          : 0x%02x\n", p, hw);
  msg_info("%sICE FW version          : 0x%02x\n", p, fw);

has an incorrect amount of spaces. Would you mind removing two spaces for each of them, so they'll look like this instead?

  msg_info("%sICE HW version        : 0x%02x\n", p, hw);
  msg_info("%sICE FW version        : 0x%02x\n", p, fw);

Copy link
Contributor Author

@Ho-Ro Ho-Ro Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick thought: Let the computer do this nasty work. Could a format like this help to maintain an uniform appearance more easily instead of manual space counting?

const int msg_space = 21;
msg_info("%s%-*s : 0x%02x\n", p, msg_space, "ICE HW version", hw);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. We should probably have done this in the first place.
However, I think this should be done at a global scale, and as its very own PR.
Adding "dumb" spaces is sufficient for the time being. Maybe I'll look into fixing it after 7.3 has been released.


static void jtagmkI_print_parms1(const PROGRAMMER *pgm, const char *p, FILE *fp) {
unsigned char vtarget, jtag_clock;
unsigned char jtag_clock = 0;
double vtarget = 0;
const char *clkstr;
double clk;

Expand Down Expand Up @@ -1192,9 +1224,9 @@ static void jtagmkI_print_parms1(const PROGRAMMER *pgm, const char *p, FILE *fp)
}

if (pgm->extra_features & HAS_VTARG_READ) {
if (jtagmkI_getparm(pgm, PARM_OCD_VTARGET, &vtarget) < 0)
if (jtagmkI_get_vtarget(pgm, &vtarget) < 0)
return;
fmsg_out(fp, "%sVtarget : %.1f V\n", p, 6.25 * (unsigned)vtarget / 255.0);
fmsg_out(fp, "%sVtarget : %.1f V\n", p, vtarget);
Ho-Ro marked this conversation as resolved.
Show resolved Hide resolved
}
fmsg_out(fp, "%sJTAG clock : %s (%.1f us)\n", p, clkstr, 1.0e6 / clk);

Expand Down Expand Up @@ -1232,7 +1264,11 @@ void jtagmkI_initpgm(PROGRAMMER *pgm) {
pgm->paged_load = jtagmkI_paged_load;
pgm->print_parms = jtagmkI_print_parms;
pgm->set_sck_period = jtagmkI_set_sck_period;
pgm->get_sck_period = jtagmkI_get_sck_period;
pgm->setup = jtagmkI_setup;
pgm->teardown = jtagmkI_teardown;
pgm->page_size = 256;
if (pgm->extra_features & HAS_VTARG_READ) {
pgm->get_vtarget = jtagmkI_get_vtarget;
}
}
4 changes: 4 additions & 0 deletions src/libavrdude.h
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,13 @@ typedef struct programmer_t {
int (*term_keep_alive)(const struct programmer_t *pgm, const AVRPART *p);
void (*print_parms) (const struct programmer_t *pgm, FILE *fp);
int (*set_vtarget) (const struct programmer_t *pgm, double v);
int (*get_vtarget) (const struct programmer_t *pgm, double *v);
int (*set_varef) (const struct programmer_t *pgm, unsigned int chan, double v);
int (*get_varef) (const struct programmer_t *pgm, unsigned int chan, double *v);
int (*set_fosc) (const struct programmer_t *pgm, double v);
int (*get_fosc) (const struct programmer_t *pgm, double *v);
int (*set_sck_period) (const struct programmer_t *pgm, double v);
int (*get_sck_period) (const struct programmer_t *pgm, double *v);
int (*setpin) (const struct programmer_t *pgm, int pinfunc, int value);
int (*getpin) (const struct programmer_t *pgm, int pinfunc);
int (*highpulsepin) (const struct programmer_t *pgm, int pinfunc);
Expand Down
4 changes: 4 additions & 0 deletions src/pgm.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,13 @@ PROGRAMMER *pgm_new(void) {
pgm->term_keep_alive= NULL;
pgm->print_parms = NULL;
pgm->set_vtarget = NULL;
pgm->get_vtarget = NULL;
pgm->set_varef = NULL;
pgm->get_varef = NULL;
pgm->set_fosc = NULL;
pgm->get_fosc = NULL;
pgm->set_sck_period = NULL;
pgm->get_sck_period = NULL;
pgm->setpin = NULL;
pgm->getpin = NULL;
pgm->highpulsepin = NULL;
Expand Down
115 changes: 102 additions & 13 deletions src/stk500.c
Original file line number Diff line number Diff line change
Expand Up @@ -1190,13 +1190,29 @@ static int stk500_set_vtarget(const PROGRAMMER *pgm, double v) {

if (uaref > utarg) {
pmsg_warning("reducing V[aref] from %.1f to %.1f\n", uaref / 10.0, v);
if ((rc = stk500_setparm(pgm, Parm_STK_VADJUST, utarg)) != 0)
if ((rc = stk500_setparm(pgm, Parm_STK_VADJUST, utarg)) != 0) {
pmsg_error("cannot set V[aref]\n");
return rc;
}
}
return stk500_setparm(pgm, Parm_STK_VTARGET, utarg);
}


static int stk500_get_vtarget(const PROGRAMMER *pgm, double *v) {
unsigned utarg = 0;
int rv;

if ((rv = stk500_getparm(pgm, Parm_STK_VTARGET, &utarg)) != 0) {
pmsg_error("cannot obtain V[target]\n");
return rv;
}

*v = utarg / 10.0;
return 0;
}


static int stk500_set_varef(const PROGRAMMER *pgm, unsigned int chan /* unused */,
double v)
{
Expand All @@ -1214,7 +1230,25 @@ static int stk500_set_varef(const PROGRAMMER *pgm, unsigned int chan /* unused *
"V[target] = %.1f\n", utarg/10.0);
return -1;
}
return stk500_setparm(pgm, Parm_STK_VADJUST, uaref);

if ((rc = stk500_setparm(pgm, Parm_STK_VADJUST, uaref)) < 0)
pmsg_error("cannot set V[aref]\n");
return rc;
}


static int stk500_get_varef(const PROGRAMMER *pgm, unsigned int chan /* unused */,
double *v) {
unsigned uaref = 0;
int rv;

if ((rv = stk500_getparm(pgm, Parm_STK_VADJUST, &uaref)) != 0) {
pmsg_error("cannot obtain V[aref]\n");
return rv;
}

*v = uaref / 10.0;
return 0;
}


Expand All @@ -1224,7 +1258,7 @@ static int stk500_set_fosc(const PROGRAMMER *pgm, double v) {
1, 8, 32, 64, 128, 256, 1024
};
size_t idx;
int rc;
int rc = 0;

prescale = cmatch = 0;
if (v > 0.0) {
Expand Down Expand Up @@ -1252,15 +1286,45 @@ static int stk500_set_fosc(const PROGRAMMER *pgm, double v) {
}
}
if (idx == sizeof(ps) / sizeof(ps[0])) {
pmsg_warning("f = %u Hz too low, %u Hz min\n", fosc, PDATA(pgm)->xtal / (256 * 1024 * 2));
return -1;
pmsg_warning("f = %u Hz too low, using %u Hz\n", fosc, PDATA(pgm)->xtal / (256 * 1024 * 2));
Copy link
Collaborator

@stefanrueger stefanrueger Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thinking! I like that the code does the nearest best and warns

prescale = idx;
cmatch = 255;
}
}

if ((rc = stk500_setparm(pgm, Parm_STK_OSC_PSCALE, prescale)) != 0
|| (rc = stk500_setparm(pgm, Parm_STK_OSC_CMATCH, cmatch)) != 0)
if ((rc = stk500_setparm(pgm, Parm_STK_OSC_PSCALE, prescale)) != 0 ) {
pmsg_error("cannot set Parm_STK_OSC_PSCALE\n");
return rc;

}

if ((rc = stk500_setparm(pgm, Parm_STK_OSC_CMATCH, cmatch)) != 0) {
pmsg_error("cannot set Parm_STK_OSC_CMATCH\n");
return rc;
}

return 0;
}


static int stk500_get_fosc(const PROGRAMMER *pgm, double *v) {
unsigned prescale=0, cmatch=0;
static unsigned ps[] = {
1, 8, 32, 64, 128, 256, 1024
};
int rc;

if ((rc = stk500_getparm(pgm, Parm_STK_OSC_PSCALE, &prescale)) != 0) {
pmsg_error("cannot get Parm_STK_OSC_PSCALE\n");
return rc;
}

if ((rc = stk500_getparm(pgm, Parm_STK_OSC_CMATCH, &cmatch)) != 0) {
pmsg_error("cannot get Parm_STK_OSC_CMATCH\n");
return rc;
}

*v = !prescale ? 0 : PDATA(pgm)->xtal / ((cmatch + 1) * 2 * ps[prescale - 1]);

return 0;
}

Expand All @@ -1275,11 +1339,12 @@ static int stk500_set_fosc(const PROGRAMMER *pgm, double v) {
static int stk500_set_sck_period(const PROGRAMMER *pgm, double v) {
int dur;
double min, max;
int rv = 0;

min = 8.0 / PDATA(pgm)->xtal;
max = 255 * min;
dur = v / min + 0.5;

if (v < min) {
dur = 1;
pmsg_warning("p = %.1f us too small, using %.1f us\n",
Expand All @@ -1289,8 +1354,25 @@ static int stk500_set_sck_period(const PROGRAMMER *pgm, double v) {
pmsg_warning("p = %.1f us too large, using %.1f us\n",
v/1e-6, dur*min/1e-6);
}

return stk500_setparm(pgm, Parm_STK_SCK_DURATION, dur);

if ((rv = stk500_setparm(pgm, Parm_STK_SCK_DURATION, dur)) < 0) {
pmsg_error("cannot set Parm_STK_SCK_DURATION\n");
return rv;
}
return 0;
}


static int stk500_get_sck_period(const PROGRAMMER *pgm, double *v) {
unsigned dur;
int rv = 0;

if ((rv = stk500_getparm(pgm, Parm_STK_SCK_DURATION, &dur)) < 0) {
pmsg_error("cannot obtain Parm_STK_SCK_DURATION\n");
return rv;
}
*v = dur * 8.0 / PDATA(pgm)->xtal;
return 0;
}


Expand Down Expand Up @@ -1535,6 +1617,7 @@ void stk500_initpgm(PROGRAMMER *pgm) {
pgm->paged_load = stk500_paged_load;
pgm->print_parms = stk500_print_parms;
pgm->set_sck_period = stk500_set_sck_period;
pgm->get_sck_period = stk500_get_sck_period;
pgm->parseextparams = stk500_parseextparms;
pgm->setup = stk500_setup;
pgm->teardown = stk500_teardown;
Expand All @@ -1545,8 +1628,14 @@ void stk500_initpgm(PROGRAMMER *pgm) {
*/
if (pgm->extra_features & HAS_VTARG_ADJ)
pgm->set_vtarget = stk500_set_vtarget;
if (pgm->extra_features & HAS_VAREF_ADJ)
if (pgm->extra_features & HAS_VTARG_READ)
pgm->get_vtarget = stk500_get_vtarget;
if (pgm->extra_features & HAS_VAREF_ADJ) {
pgm->set_varef = stk500_set_varef;
if (pgm->extra_features & HAS_FOSC_ADJ)
pgm->get_varef = stk500_get_varef;
}
if (pgm->extra_features & HAS_FOSC_ADJ) {
pgm->set_fosc = stk500_set_fosc;
pgm->get_fosc = stk500_get_fosc;
}
}
Loading