-
Notifications
You must be signed in to change notification settings - Fork 136
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
Support for STK500 v1 and v2 devices with STK500_XTAL != 7372800 #1566
Conversation
1st part of PR for avrdudes#1560 - clean up `-x` command handling of stk500v1 2nd part will add `-x xtal` handling for stk500v1 Signed-off-by: Martin <Ho-Ro@users.noreply.github.com>
fix parsing of frequencies, values w/o k or M prefix were not recognised -x fosc=100k worked, -x fosc=100000 resulted in 0.000 now frequency parameter fosc=nnnn or fosc=nnnnH[z] are also ok more consistent XTAL frequency display - this parameter is constant for the used programmer contrary to variables like fosc and sck Signed-off-by: Martin <Ho-Ro@users.noreply.github.com>
STK500 v2 takes some more time. The source code is more confusing because so many different programmers are involved and the crystal frequency has also been coded into many different "magic constants". |
Thanks for looking into this! I think it will be worth our while. Let us know once you are finished with code and documentation in avrdude.1 and doc/avrdude.texi; then the team will be able to review and test. |
unify xtra argument messages for v1 and v2, better error message wording update man and doc Signed-off-by: Martin <Ho-Ro@users.noreply.github.com>
stk500v2 support for |
Signed-off-by: Martin <Ho-Ro@users.noreply.github.com>
src/stk500.c
Outdated
@@ -707,7 +704,7 @@ static int stk500_parseextparms(const PROGRAMMER *pgm, const LISTID extparms) | |||
if (str_eq(fosc_str, "off")) | |||
PDATA(pgm)->fosc_data = 0.0; | |||
else { | |||
pmsg_error("cannot parse fosc value %s\n", fosc_str); | |||
pmsg_error("invalid fosc value '%s'\n", fosc_str); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ever so slightly prefer %s without single quotes in common with other messages
@@ -590,7 +590,7 @@ static int stk500_initialize(const PROGRAMMER *pgm, const AVRPART *p) { | |||
stk500_getparm(pgm, Parm_STK_OSC_CMATCH, &osc_cmatch); | |||
if(osc_pscale) { | |||
int prescale = 1; | |||
f_get = STK500_XTAL / 2; | |||
f_get = PDATA(pgm)->xtal / 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better divide by 2.0 (otherwise lsb is lost)
src/stk500.c
Outdated
char xtal_str[16] = {0}; | ||
int sscanf_success = sscanf(extended_param, "xtal=%10s", xtal_str); | ||
if (sscanf_success < 1) { | ||
pmsg_error("invalid xtal value '%s'\n", extended_param); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no single quotes please in message (multiple times)
} | ||
char *endp; | ||
double v = strtod(xtal_str, &endp); | ||
if (endp == xtal_str){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be neat to allow space between number and unit; eg, with code to the effect of
if(*endp == ' ')
endp++;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good POLA point, I will check.
EDIT: Does not work as expected, "1.23 MHz" -> v=1.23, *endp=''
But I do not expect the user to provide frequencies with space inside as you must put it into quotes:
-x"xtal=1.23 MHz"
instead of -xxtal=1.23M
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Users are unlikely to use quotes but scripts that extract the frequency from another AVRDUDE or otherwise call might have a frequency as 1.23 MHz
and find it easy to use quotes but marginally harder to remove the space between 3 and MHz. It is always neat if the output of AVRDUDE (correctly done with space between number and unit) can be used as input without mods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, c983e30 allows spaces in extra parameter, e.g. -x "fosc= .1e3 kHz"
src/stk500.c
Outdated
@@ -1167,31 +1206,31 @@ static int stk500_set_fosc(const PROGRAMMER *pgm, double v) { | |||
|
|||
prescale = cmatch = 0; | |||
if (v > 0.0) { | |||
if (v > STK500_XTAL / 2) { | |||
if (v > PDATA(pgm)->xtal / 2) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2.0 (multiple times)
src/stk500.c
Outdated
unit = "kHz"; | ||
} else | ||
unit = "Hz"; | ||
fmsg_out(fp, "%sXTAL frequency : %.3f %s\n", p, f, unit); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is .3 sufficient? Might .5 or .6 be better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I changed to variable decimal count 6 for MHz, 3 for kHz, 0 for Hz
@@ -57,6 +57,9 @@ struct pdata { | |||
bool fosc_get; | |||
bool fosc_set; | |||
double fosc_data; | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps neater to have inline comment at column 32 without empty line? So adding a component is one line, not three.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks vvv good; I only have minor (minor) suggestions, which need applying multiple times; still needs to be tested independently, but all looks vvv good
@stefanrueger et. al. What's the next step, should I add another commit with all requested changes and you "squash and merge" them as one commit? |
Signed-off-by: Martin <Ho-Ro@users.noreply.github.com>
Unfortunately my original STK500 was dead so I can not carry out real test. I have a few STK500 clones which only implement the programming portion (including one with HVPP capability) but not other features. @MCUdude |
Sure! I'll give it a test tonight. "How dead" are your STK500 board? I accidentally bricked one of mine, but I was able to get it working again by re-flashing the firmware for the ATtiny2313 (running AVR910) and the ATmega8515 (running STK500 FW). I think I have the correct hex files somewhere if that's your issue. |
I have the necessary STK500 V1/V2 FW hex files -- that is why I can switch between STK500 V1 and STK500 V2 for two clones using ATmega8535 MCU. Unfortunately for my original STK500 it is HW failure and it is hard to repair now due to broken traces. It was a very old unit (probably made in 2005/2006) and I got from my colleague a few years ago. |
I just tested this PR on two separate STK500 boards running V1 and v2 firmware, and when I changed -xxtal to 14.7456 MHz, I got the reported Two things:
|
When thinking a bit; probably not, as it doesn't change anything on the board other than a constant used for calculation. However, it should probably not be present when running the |
I agree, as it cannot be changed from terminal cmd, but @stefanrueger had a good argument for leaving it in. |
src/stk500.c
Outdated
fmsg_out(fp, "%.3f %s\n", f, unit); | ||
decimals = get_decimals(f); | ||
f = f_to_kHz_MHz(f, &unit); | ||
fmsg_out(fp, "%.*f %s\n", f, decimals, unit); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Must be fmsg_out(fp, "%.*f %s\n", decimals, f, unit);
ie, decimals first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, star comes before f in the string - I know I checked it later with the C standard, but only corrected line 1465 below and forgot the other occurrences here and in v2.
Funny thing is that gcc doesn't complain when I do it the wrong way round and does "what I mean" - I don't know what clang does though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does "what I mean"
A fluke maybe? Maybe doubles are passed in a different frame to functions than smaller data types via the stack?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gcc doesn't complain
gcc does not realise that the called function resolves to or calls printf()
src/stk500v2.c
Outdated
f = f_to_kHz_MHz(f, &unit); | ||
fmsg_out(fp, "%.3f %s\n", f, unit); | ||
fmsg_out(fp, "%.*f %s\n", f, decimals, unit); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change order of f and decimals arguments (multiple times)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, 2 times
@@ -1306,7 +1314,7 @@ static int stk500v2_initialize(const PROGRAMMER *pgm, const AVRPART *p) { | |||
stk500v2_getparm(pgm, PARAM_OSC_CMATCH, &osc_cmatch); | |||
if(osc_pscale) { | |||
int prescale = 1; | |||
f_get = STK500V2_XTAL / 2; | |||
f_get = PDATA(pgm)->xtal / 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fault of this PR, but osc_pscale
and osc_cmatch
ought to be set to 0 above b/c stk500v2_getparm()
might not set them! Ideally, also the return value of stk500v2_getparm()
calls ought to be checked to guard against not setting the osc_...
variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be a bigger separate commit, because stk500v2_getparm()
(as well as stk500_getparm()
in stk500.c
) is used quite often w/o checking the return value. Nevertheless I can adopt this issue and fix it in the course of my stk500* overhaul.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for cleaning this in your overhaul of stk500* - much appreciated.
@@ -1620,7 +1628,7 @@ static int stk500hv_initialize(const PROGRAMMER *pgm, const AVRPART *p, enum hvm | |||
stk500v2_getparm(pgm, PARAM_OSC_CMATCH, &osc_cmatch); | |||
if(osc_pscale) { | |||
int prescale = 1; | |||
f_get = STK500V2_XTAL / 2; | |||
f_get = PDATA(pgm)->xtal / 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above:
Not a fault of this PR, but osc_pscale and osc_cmatch ought to be set to 0 above b/c stk500v2_getparm() might not set them! Ideally, also the return value of stk500v2_getparm() calls ought to be checked to guard against not setting the osc_... variables.
// STK500: Write analog reference voltage | ||
else { | ||
msg_info("Changing analog reference voltage from %.2f to %.2fV\n", | ||
msg_info("Changing analog reference voltage from %.2f V to %.2f V\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsigned char varef_read
above should be initialised to 0 (same reason as below, four times in this code)
// Write target voltage value | ||
else { | ||
msg_info("Changing target voltage from %.2f to %.2fV\n", (vtarg_read / 10.0), PDATA(pgm)->vtarg_data); | ||
msg_info("Changing target voltage from %.2f V to %.2f V\n", (vtarg_read / 10.0), PDATA(pgm)->vtarg_data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsigned char vtarg_read;
should be initialised with 0 (same reason as below, needs to be done twice in this file).
Normally someone reviews all changes and also the surrounding code (sometimes there are problems that are outside the PR but should be fixed "while at it"). Then things are tested. Then the PR hangs around for a few days (can be weeks) until a bunch of PRs is merged. I sometimes squash and merge (eg, when a few ideas were tested) or merge when it's not problematic that a few fixes were committed. If it's important to you that a PR goes in with a particular pristine commit sequence it is normally better to recreate a new PR. Force pushes are not really encouraged as they can go wrong. |
I think it would then be ok to stack some more commits e.g. the check of |
minor format change remove ':' from pmsg_error() to be consistent with other output lines PR avrdudes#1566 should be done Signed-off-by: Martin <Ho-Ro@users.noreply.github.com>
Signed-off-by: Martin <Ho-Ro@users.noreply.github.com>
I'm back at my home desk and can also provide some evidence for tests with my (original) STK500 with v2 FW. |
Just tested with two separate STK500 board running v1 and v2 firmware. Everything seems to work fine! I've not found any issues. I also verified using an oscilloscope to measure the clock input frequency on the target chip. |
This PR replaces #1565 because it introduced a lot of whitespace-only changes. Sorry for the confusion.
1st part fixed some errors while 2nd commit does the real work.