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

Put static variables into context structure #1803

Merged
merged 54 commits into from
May 17, 2024

Conversation

stefanrueger
Copy link
Collaborator

This PR intends to move all static variables used in libavrdude functions into a single structure called cx_t; the idea is that there is one global context pointer cx_t *cx that libavrdude applications need to allocate memory for at the beginning of each instantiation. Ideally, one would hide the components of the context structure and write access function that are used in libavrdude functions. However, that ship has sailed given all the explicit PROGRAMMER, AVRPART and AVRMEM structures and the tons of existing direct -> access to their components. And more importantly, there are a lot of variables to convert (some 111), so writing 222 access functions, one each for get and set, wouldn't be much fun.

@dl8dtl @MCUdude @mcuee Have a look at the commit for the first source file that has been treated that way, avr.c, and see what you think. Other than that: it's one down and 34 to do.

... and convert static memory for strings returned by avr_prog_modes()
into closed-circuit memory area in the context structure
@stefanrueger stefanrueger linked an issue May 6, 2024 that may be closed by this pull request
@mcuee mcuee added the enhancement New feature or request label May 7, 2024
@mcuee
Copy link
Collaborator

mcuee commented May 7, 2024

@stefanrueger

I am not so sure if I understand the intention correctly, but this seems to be able to enable multiple context with libavrdude. In that case, it may be good to have a function of libavrdude_init(&context) and libavrdude_exit(contex).

Reference from libusb.
https://libusb.sourceforge.io/api-1.0/group__libusb__lib.html

int libusb_init_context	(	
libusb_context ** ctx,
const struct libusb_init_option options[],
int 	num_options 
)	

void libusb_exit(libusb_context * ctx)	

@stefanrueger
Copy link
Collaborator Author

libavrdude_init(&context)

Good shout, I wanted to work through the needed variables first and see which ones actually need initialisation.

The latter returns a string in a static buffer that is overwritten by the
next call. Unfortunately, this function was used more than once in
arguments of the *same* msg_error(), which would have given a wrong error
message. pins_to_strdup() returns a string to malloc'd space that needs
freeing.
... as it was used wrongly and led to errors. Better to use
pin_to_strdup()
No longer is the string returned in static space (which the next call
overwrites); instead a mmt_strdup()'d string is returned that needs
mmt_free()'ing.
@stefanrueger stefanrueger changed the title WIP: put static variables into context structure Put static variables into context structure May 10, 2024
@stefanrueger
Copy link
Collaborator Author

As far as I am aware all static variables are now dealt with and moved to the context structure. Following simple test works:

$ yes "" | tools/test-avrdude -Td0

With the exception of one variable (usb_buflen) all were initialised to 0, and I think(!) usb_buflen can live with also being initialised to 0. So, @mcuee looks like we don't need an init() function.

It's quite possible that something went wrong when I migrated the variables, so a few functionality checks with hardware will be needed. @mcuee? @MCUdude?

I suggest merging a batch of current PRs including this one early next week, so that @dl8dtl can progress his PR #1714 (as this PR is meant to make libavrdude ready for new applications such as his GDI, in particular ones that allow various different programming sessions without leaving the application).

@mcuee
Copy link
Collaborator

mcuee commented May 11, 2024

Okay, let me carry out some simple tests in the next few days.

@mcuee
Copy link
Collaborator

mcuee commented May 13, 2024

First test: the results are good.

MINGW64 /c/work/avr/avrdude_test/avrdude_sr
$ ./tools/test-avrdude -e ./build_mingw64/src/avrdude.exe -p "-c usbasp -p m328p"
Testing ./build_mingw64/src/avrdude.exe version 7.3-20240510 (627f320e)
Prepare "-c usbasp -p m328p" and press 'enter' or 'space' to continue. Press any other key to skip
✅   0.592 s: fuse access: clear, set and read eesave fuse bit
✅   0.519 s: fuse access: set eesave fusebit to delete EEPROM on chip erase
✅   0.788 s: chip erase
✅   2.730 s: flash -U write/verify holes_rjmp_loops_32768B.hex
✅   1.260 s: flash -T write/verify holes_rjmp_loops_32768B.hex
✅   0.620 s: eeprom check whether programmer can flip 0s to 1s
✅   3.247 s: eeprom -U write/verify holes_pack_my_box_1024B.hex
✅   6.143 s: eeprom -T write/verify holes_{the_five_boxing_wizards,pack_my_box}_1024B.hex
✅   1.468 s: chip erase and spot check flash is actually erased
✅   0.611 s: spot check eeprom is erased, too

@mcuee
Copy link
Collaborator

mcuee commented May 13, 2024

Second test is also fine.

MINGW64 /c/work/avr/avrdude_test/avrdude_sr
$ ./tools/test-avrdude -e ./build_mingw64/src/avrdude.exe -p "-c urclock -P COM4 -p m328p"
Testing ./build_mingw64/src/avrdude.exe version 7.3-20240510 (627f320e)
Prepare "-c urclock -P COM4 -p m328p" and press 'enter' or 'space' to continue. Press any other key to skip
✅   1.770 s: chip erase
✅   3.251 s: flash -U write/verify holes_rjmp_loops_32768B.hex
✅   1.185 s: flash -T write/verify holes_rjmp_loops_32768B.hex
✅   0.617 s: eeprom check whether programmer can flip 0s to 1s
✅   1.619 s: eeprom -U write/verify holes_pack_my_box_1024B.hex
✅   2.893 s: eeprom -T write/verify holes_{the_five_boxing_wizards,pack_my_box}_1024B.hex
✅   2.319 s: chip erase and spot check flash is actually erased

@mcuee
Copy link
Collaborator

mcuee commented May 13, 2024

I have not tested the legacy Arduino optiboot bootloader for a while and it is okay. The failure is expected as the bootloader does not support EEPROM and chip erase.

MINGW64 /c/work/avr/avrdude_test/avrdude_sr

$ ./build_mingw64/src/avrdude.exe -c urclock -P COM4 -p m328p -xshowall
avrdude: AVR device initialized and ready to accept instructions
0 0000-00-00 00.00  application 0 store 0 meta 1 boot 512 o4.4 --s-h-r-- vector 0 (RESET) ATmega328P

avrdude done.  Thank you.

$ ./tools/test-avrdude -e ./build_mingw64/src/avrdude.exe -p "-c arduino -P COM4 -p m328p"
Testing ./build_mingw64/src/avrdude.exe version 7.3-20240510 (627f320e)
Prepare "-c arduino -P COM4 -p m328p" and press 'enter' or 'space' to continue. Press any other key to skip
✅   1.569 s: chip erase
✅   3.181 s: flash -U write/verify holes_rjmp_loops_32768B.hex
✅   2.168 s: flash -T write/verify holes_rjmp_loops_32768B.hex
✅   1.535 s: eeprom check whether programmer can flip 0s to 1s
❌   2.621 s: eeprom -U write/verify holes_pack_my_box_1024B.hex (failed command below)
$ ./build_mingw64/src/avrdude.exe -qq -c arduino -P COM4 -p m328p -Ueeprom:w:./tools/test_files/holes_pack_my_box_1024B.hex
❌   6.065 s: eeprom -U write/verify the_quick_brown_fox_1024B.hex (failed command below)
$ ./build_mingw64/src/avrdude.exe -qq -c arduino -P COM4 -p m328p -Ueeprom:w:./tools/test_files/the_quick_brown_fox_1024B.hex
✅   4.010 s: eeprom -T write/verify holes_{the_five_boxing_wizards,pack_my_box}_1024B.hex
❌   2.241 s: chip erase and spot check flash is actually erased (failed command below)
$ ./build_mingw64/src/avrdude.exe -qq -c arduino -P COM4 -p m328p -e -FAU flash:w:0xff:m -U flash:v:./tools/test_files/holes_flash_0xff_32768B.hex

One or more AVRDUDE "-c arduino -P COM4 -p m328p" tests failed. Do you want to retry this particular test? (y/n): n

$ ./tools/test-avrdude -e ./build_mingw64/src/avrdude.exe -p "-c urclock -P COM4 -p m328p"
Testing ./build_mingw64/src/avrdude.exe version 7.3-20240510 (627f320e)
Prepare "-c urclock -P COM4 -p m328p" and press 'enter' or 'space' to continue. Press any other key to skip
✅  10.031 s: chip erase
✅  10.084 s: flash -U write/verify holes_rjmp_loops_32768B.hex
✅   1.824 s: flash -T write/verify holes_rjmp_loops_32768B.hex
❌   1.100 s: eeprom check whether programmer can flip 0s to 1s (failed command below)
$ ./build_mingw64/src/avrdude.exe -qq -c urclock -P COM4 -p m328p -Ueeprom:w:0x55:m -Ueeprom:w:0xaa:m
# ... the next test may therefore take longer
❌   1.155 s: eeprom -T write/verify holes_{the_five_boxing_wizards,pack_my_box}_1024B.hex (failed command below)
$ ./build_mingw64/src/avrdude.exe -qq -c urclock -P COM4 -p m328p -T "write eeprom ./tools/test_files/holes_the_five_boxing_wizards_1024B.hex:a" -T flush -T "write eeprom ./tools/test_files/holes_pack_my_box_1024B.hex:a"
❌   1.251 s: eeprom -T write/verify lorem_ipsum_1024B.srec (failed command below)
$ ./build_mingw64/src/avrdude.exe -qq -c urclock -P COM4 -p m328p -T "write eeprom ./tools/test_files/lorem_ipsum_1024B.srec:a"
✅  10.810 s: chip erase and spot check flash is actually erased

One or more AVRDUDE "-c urclock -P COM4 -p m328p" tests failed. Do you want to retry this particular test? (y/n):

@stefanrueger
Copy link
Collaborator Author

@mcuee Thanks for testing. And I can contribute two programmers as well:

$ ./tools/test-avrdude -p "-c usbtiny -p m2560 -B 4MHz" -d 0
Testing avrdude version 7.3-20240510 (627f320e)
Prepare "-c usbtiny -p m2560 -B4MHz" and press 'enter' or 'space' to continue. Press any other key to skip
✅   0.229 s: fuse access: clear, set and read eesave fuse bit
✅   0.110 s: fuse access: set eesave fusebit to delete EEPROM on chip erase
✅   0.421 s: chip erase
✅  50.063 s: flash -U write/verify holes_rjmp_loops_262144B.hex
✅  31.497 s: flash -T write/verify holes_rjmp_loops_262144B.hex
✅   1.096 s: eeprom check whether programmer can flip 0s to 1s
✅   4.591 s: eeprom -U write/verify holes_pack_my_box_4096B.hex
✅   9.512 s: eeprom -T write/verify holes_{the_five_boxing_wizards,pack_my_box}_4096B.hex
✅  32.042 s: chip erase and spot check flash is actually erased
✅   0.836 s: spot check eeprom is erased, too

$ ./tools/test-avrdude -p "-c usb-bub-ii -p m328p" -d 0
Testing avrdude version 7.3-20240510 (627f320e)
Prepare "-c usb-bub-ii -p m328p" and press 'enter' or 'space' to continue. Press any other key to skip
✅   0.220 s: fuse access: clear, set and read eesave fuse bit
✅   0.098 s: fuse access: set eesave fusebit to delete EEPROM on chip erase
✅   0.344 s: chip erase
✅   6.757 s: flash -U write/verify holes_rjmp_loops_32768B.hex
✅   3.281 s: flash -T write/verify holes_rjmp_loops_32768B.hex
✅   0.237 s: eeprom check whether programmer can flip 0s to 1s
✅   1.871 s: eeprom -U write/verify holes_pack_my_box_1024B.hex
✅   3.774 s: eeprom -T write/verify holes_{the_five_boxing_wizards,pack_my_box}_1024B.hex
✅   3.540 s: chip erase and spot check flash is actually erased
✅   0.462 s: spot check eeprom is erased, too

The latter being a ftdi_syncbb-type programmer rigged up with own cabelling defined in ~/.avrduderc

@MCUdude
Copy link
Collaborator

MCUdude commented May 14, 2024

PR looks good to me! Are there any specific programmers you want me to test with?

@stefanrueger
Copy link
Collaborator Author

PR looks good to me! Are there any specific programmers you want me to test with?

@MCUdude Thanks! If you have, an avrftdi-type programmer test would be great, particularly one where you copy the programmer entry into your private ~/.avrduderc and assign a wrong pin number to one of the signals so that avrftdi_check_pins_mpsse() triggers an error message.

Also bitbanging programmers (serbb type) or linuxgpio had a bit of a change...

@ndim
Copy link
Contributor

ndim commented May 14, 2024

Regarding cx_t... have you considered that POSIX reserves all type names ending in _t?

Answering my own question: The avrdude code is full of other foo_t types, so adding one more should not make much of a difference.

@stefanrueger
Copy link
Collaborator Author

@ndim Great shout. I'll change cx_t to sth else and, at another time, think about the 39 other foo_t that AVRDUDE uses:

$ cat $(find /usr/include/ -type f) | tr -c a-zA-Z_0-9 \\n | grep _t$ | sort -u >/tmp/p
$ grep -hr _t src | tr -c a-zA-Z_0-9 \\n | grep _t$ | sort -u  | comm -32 - /tmp/p
avrftdi_t
Blhash_t
CacheDesc_t
Cfg_opts_t
Cfg_t
Component_t
Configitem_t
conntype_t
ctl_stack_t
dry_prog_t
dryrun_t
exit_datahigh_t
exit_reset_t
exit_vcc_t
Flock_t
fl_t
Fusel_t
is_outside_int64_t
leds_t
memtable_t
memtype_t
pdata_t
pin_checklist_t
pindef_t
pinmask_t
ppipins_t
programmer_t
programmer_type_t
Register_file_t
sckoptions_t
Segment_t
Segorder_t
token_t
ulong_t
uPcore_t
update_t
Urclock_t
Valueitem_t
Write_mode_t

Maybe, we can replace xyx_t to Xyz? Just thinking aloud.

@mcuee
Copy link
Collaborator

mcuee commented May 16, 2024

@ndim Great shout. I'll change cx_t to sth else and, at another time, think about the 39 other foo_t that AVRDUDE uses:

Maybe, we can replace xyx_t to Xyz? Just thinking aloud.

I think this is a good idea (some of them is already Xyz_t but should still be okay).
Or maybe we can replace xyz_t with xyz_type.

BTW, changing cx_t to libavrdude_context is certainly a welcome one.

@mcuee
Copy link
Collaborator

mcuee commented May 17, 2024

Also bitbanging programmers (serbb type) or linuxgpio had a bit of a change...

No issues with linuxgpio.

mcuee@raspberrypi400arm64:~/build/avrdude_sr $ 
./tools/test-avrdude -e ./build_linux/src/avrdude -p "-c linuxgpio -p m328p"
Testing ./build_linux/src/avrdude version 7.3-20240514 (39336422)
Prepare "-c linuxgpio -p m328p" and press 'enter' or 'space' to continue. Press any other key to skip
✅   0.340 s: fuse access: clear, set and read eesave fuse bit
✅   0.328 s: fuse access: set eesave fusebit to delete EEPROM on chip erase
✅   0.489 s: chip erase
✅   1.845 s: flash -U write/verify holes_rjmp_loops_32768B.hex
✅   1.451 s: flash -T write/verify holes_rjmp_loops_32768B.hex
✅   0.321 s: eeprom check whether programmer can flip 0s to 1s
✅   1.334 s: eeprom -U write/verify holes_pack_my_box_1024B.hex
✅   2.111 s: eeprom -T write/verify holes_{the_five_boxing_wizards,pack_my_box}_1024B.hex
✅   1.042 s: chip erase and spot check flash is actually erased
✅   0.353 s: spot check eeprom is erased, too

mcuee@raspberrypi400arm64:~/build/avrdude_sr $ cat ~/.avrduderc 
# #------------------------------------------------------------
# # linuxgpio
# #------------------------------------------------------------
#
programmer
    id                   = "linuxgpio";
    desc                 = "Use the Linux libgpiod interface to bitbang GPIO lines";
    type                 = "linuxgpio";
    prog_modes           = PM_ISP;
    connection_type      = linuxgpio;
    reset                = 25;
    sck                  = 11;
    sdo                  = 10;
    sdi                  = 9;
;

@stefanrueger stefanrueger merged commit 9d02c37 into avrdudes:main May 17, 2024
13 checks passed
@stefanrueger stefanrueger deleted the context branch May 17, 2024 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0xff optimization when reading
4 participants