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

Ensure full words are loaded, low-byte first, for ISP programming #1265

Merged
merged 3 commits into from
Jan 5, 2023

Conversation

stefanrueger
Copy link
Collaborator

Addresses Issue #1263

Although the ISP interface for classic parts loads pages byte for byte (not word for word) into a buffer on chip, the data sheets say that the low byte must be written before the high byte for programming to work. This PR ensures that. The problem with the current code is that perfectly legal hex files can just set high bytes from time to time (odd start address of blocks etc).

@cinderblock
Copy link

Seems to work with ATMega32U4-usbdevice_dfu-1_0_0.hex

@mcuee
Copy link
Collaborator

mcuee commented Jan 4, 2023

Seems to work with ATMega32U4-usbdevice_dfu-1_0_0.hex

I can reproduce Issue #1263 and I can confirm this PR #1265 fixed the issue reported in #1263.

pi@raspberrypi:~/build/avr/avrdude_bin $ ./avrdude -c linuxspi -p m32u4
 -U ./ATMega32U4-usbdevice_dfu-1_0_0.hex 

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9587 (probably m32u4)
avrdude: Note: flash memory has been specified, an erase cycle will be performed.
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file ./ATMega32U4-usbdevice_dfu-1_0_0.hex for flash
         with 3948 bytes in 5 sections within [0x7000, 0x7fff]
         using 32 pages and 148 pad bytes
avrdude: writing 3948 bytes flash ...

Writing | ################################################## | 100% 1.06 s 

avrdude: 3948 bytes of flash written
avrdude: verifying flash memory against ./ATMega32U4-usbdevice_dfu-1_0_0.hex

Reading | ################################################## | 100% 0.92 s 

avrdude warning: verification mismatch
        device 0xff != input 0x7f at addr 0x7090 (error)
avrdude error: verification mismatch

avrdude done.  Thank you.

pi@raspberrypi:~/build/avr/avrdude_bin $ ./avrdude_pr1265 -c linuxspi -p m32u4 
-U ./ATMega32U4-usbdevice_dfu-1_0_0.hex 

avrdude_pr1265: AVR device initialized and ready to accept instructions
avrdude_pr1265: device signature = 0x1e9587 (probably m32u4)
avrdude_pr1265: Note: flash memory has been specified, an erase cycle will be performed.
                To disable this feature, specify the -D option.
avrdude_pr1265: erasing chip
avrdude_pr1265: reading input file ./ATMega32U4-usbdevice_dfu-1_0_0.hex for flash
                with 3948 bytes in 5 sections within [0x7000, 0x7fff]
                using 32 pages and 148 pad bytes
avrdude_pr1265: writing 3948 bytes flash ...

Writing | ################################################## | 100% 1.05 s 

avrdude_pr1265: 3948 bytes of flash written
avrdude_pr1265: verifying flash memory against ./ATMega32U4-usbdevice_dfu-1_0_0.hex

Reading | ################################################## | 100% 0.92 s 

avrdude_pr1265: 3948 bytes of flash verified

avrdude_pr1265 done.  Thank you.


Copy link
Collaborator

@mcuee mcuee left a comment

Choose a reason for hiding this comment

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

Tested to be good.

@mcuee
Copy link
Collaborator

mcuee commented Jan 4, 2023

Same for linuxgpio. Issue #1263 also affected linuxgpio and this PR fixed the issue.

pi@raspberrypi:~/build/avr/avrdude_bin $ ./avrdude_pr1265 -c linuxgpio -p m32u4 -U ./ATMega32U4-usbdevice_dfu-1_0_0.hex 

avrdude_pr1265: AVR device initialized and ready to accept instructions
avrdude_pr1265: device signature = 0x1e9587 (probably m32u4)
avrdude_pr1265: Note: flash memory has been specified, an erase cycle will be performed.
                To disable this feature, specify the -D option.
avrdude_pr1265: erasing chip
avrdude_pr1265: reading input file ./ATMega32U4-usbdevice_dfu-1_0_0.hex for flash
                with 3948 bytes in 5 sections within [0x7000, 0x7fff]
                using 32 pages and 148 pad bytes
avrdude_pr1265: writing 3948 bytes flash ...

Writing | ################################################## | 100% 0.85 s 

avrdude_pr1265: 3948 bytes of flash written
avrdude_pr1265: verifying flash memory against ./ATMega32U4-usbdevice_dfu-1_0_0.hex

Reading | ################################################## | 100% 0.67 s 

avrdude_pr1265: 3948 bytes of flash verified

avrdude_pr1265 done.  Thank you.


@mcuee
Copy link
Collaborator

mcuee commented Jan 5, 2023

I am having some connection issues with the ribbon cables with the ATmega2560 so that I can not test Issue #455 now with the ATmega2560.

Edit: never mind, the hex file is generic and I can reproduce issue #455 using ATmega328P, Again PR #1265 is good to fix the issue.

pi@raspberrypi:~/build/avr/avrdude_bin $ ./avrdude -c linuxgpio -p m328p -U ./blink-2560-mod.hex 

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e950f (probably m328p)
avrdude: Note: flash memory has been specified, an erase cycle will be performed.
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file ./blink-2560-mod.hex for flash
         with 1489 bytes in 2 sections within [0, 0x5d1]
         using 12 pages and 47 pad bytes
avrdude: writing 1489 bytes flash ...

Writing | ################################################## | 100% 0.33 s 

avrdude: 1489 bytes of flash written
avrdude: verifying flash memory against ./blink-2560-mod.hex

Reading | ################################################## | 100% 0.26 s 

avrdude warning: verification mismatch
        device 0xff != input 0x00 at addr 0x05ce (error)
avrdude error: verification mismatch

avrdude done.  Thank you.

pi@raspberrypi:~/build/avr/avrdude_bin $ ./avrdude_pr1265 -c linuxgpio -p m328p -U ./blink-2560-mod.hex 

avrdude_pr1265: AVR device initialized and ready to accept instructions
avrdude_pr1265: device signature = 0x1e950f (probably m328p)
avrdude_pr1265: Note: flash memory has been specified, an erase cycle will be performed.
                To disable this feature, specify the -D option.
avrdude_pr1265: erasing chip
avrdude_pr1265: reading input file ./blink-2560-mod.hex for flash
                with 1489 bytes in 2 sections within [0, 0x5d1]
                using 12 pages and 47 pad bytes
avrdude_pr1265: writing 1489 bytes flash ...

Writing | ################################################## | 100% 0.36 s 

avrdude_pr1265: 1489 bytes of flash written
avrdude_pr1265: verifying flash memory against ./blink-2560-mod.hex

Reading | ################################################## | 100% 0.27 s 

avrdude_pr1265: 1489 bytes of flash verified

avrdude_pr1265 done.  Thank you.


@mcuee
Copy link
Collaborator

mcuee commented Jan 5, 2023

Issue #1263 can be reproduced with ATmega328P as well, for linuxgpio. Again this PR fixed the issue.

pi@raspberrypi:~/build/avr/avrdude_bin $ ./avrdude -c linuxgpio -p m328p -U ./ATMega32U4-usbdevice_dfu-1_0_0.hex 

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e950f (probably m328p)
avrdude: Note: flash memory has been specified, an erase cycle will be performed.
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file ./ATMega32U4-usbdevice_dfu-1_0_0.hex for flash
         with 3948 bytes in 5 sections within [0x7000, 0x7fff]
         using 32 pages and 148 pad bytes
avrdude: writing 3948 bytes flash ...

Writing | ################################################## | 100% 0.85 s 

avrdude: 3948 bytes of flash written
avrdude: verifying flash memory against ./ATMega32U4-usbdevice_dfu-1_0_0.hex

Reading | ################################################## | 100% 0.67 s 

avrdude warning: verification mismatch
        device 0xff != input 0x7f at addr 0x7090 (error)
avrdude error: verification mismatch

avrdude done.  Thank you.

pi@raspberrypi:~/build/avr/avrdude_bin $ ./avrdude_pr1265 -c linuxgpio -p m328p -U ./ATMega32U4-usbdevice_dfu-1_0_0.hex 

avrdude_pr1265: AVR device initialized and ready to accept instructions
avrdude_pr1265: device signature = 0x1e950f (probably m328p)
avrdude_pr1265: Note: flash memory has been specified, an erase cycle will be performed.
                To disable this feature, specify the -D option.
avrdude_pr1265: erasing chip
avrdude_pr1265: reading input file ./ATMega32U4-usbdevice_dfu-1_0_0.hex for flash
                with 3948 bytes in 5 sections within [0x7000, 0x7fff]
                using 32 pages and 148 pad bytes
avrdude_pr1265: writing 3948 bytes flash ...

Writing | ################################################## | 100% 0.85 s 

avrdude_pr1265: 3948 bytes of flash written
avrdude_pr1265: verifying flash memory against ./ATMega32U4-usbdevice_dfu-1_0_0.hex

Reading | ################################################## | 100% 0.68 s 

avrdude_pr1265: 3948 bytes of flash verified

avrdude_pr1265 done.  Thank you.


@mcuee
Copy link
Collaborator

mcuee commented Jan 5, 2023

@stefanrueger
This PR is good to go from my side.

For sanity check, I also tested with usbasp and there is no issue with git main or this PR.

@stefanrueger
Copy link
Collaborator Author

stefanrueger commented Jan 5, 2023

Great to know this solved the issue. Thanks for feedback @cinderblock and @mcuee.

I reviewed the current PR again and identified two subtle problems:

  • Loading flash low-byte first is only need when the flash memory is actually paged
  • Marking extra bytes as TAG_ALLOCATED can lead to false verification errors, eg, when programming odd bytes of flash first, then even bytes, as in -e -U odd.hex -U even.hex; this will program correctly but throw false verification errors on -U even.hex as the additional padding 0xff bytes (that write as NOP) would be checked against previously loaded odd bytes.

So I changed tack and made do without marking extra bytes TAG_ALLOCATED. The new PR also streamlines the programming code (though some might think steamrolls the programming code 😉)

I believe this PR to be production ready, but would be cool if it could be tested.

@stefanrueger stefanrueger changed the title Ensure both bytes of a needed word in flash are TAG_ALLOCATED Ensure full words are loaded, low-byte first, for ISP programming Jan 5, 2023
@mcuee
Copy link
Collaborator

mcuee commented Jan 5, 2023

@stefanrueger

It seems to be fine.

pi@raspberrypi:~/build/avr/avrdude_bin $ ./avrdude_pr1265v1 -c linuxgpio -p m328p
 -U ./ATMega32U4-usbdevice_dfu-1_0_0.hex 

avrdude_pr1265v1: AVR device initialized and ready to accept instructions
avrdude_pr1265v1: device signature = 0x1e950f (probably m328p)
avrdude_pr1265v1: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1265v1: erasing chip
avrdude_pr1265v1: reading input file ./ATMega32U4-usbdevice_dfu-1_0_0.hex for flash
                  with 3948 bytes in 5 sections within [0x7000, 0x7fff]
                  using 32 pages and 148 pad bytes
avrdude_pr1265v1: writing 3948 bytes flash ...

Writing | ################################################## | 100% 0.87 s 

avrdude_pr1265v1: 3948 bytes of flash written
avrdude_pr1265v1: verifying flash memory against ./ATMega32U4-usbdevice_dfu-1_0_0.hex

Reading | ################################################## | 100% 0.67 s 

avrdude_pr1265v1: 3948 bytes of flash verified

avrdude_pr1265v1 done.  Thank you.

pi@raspberrypi:~/build/avr/avrdude_bin $ ./avrdude_pr1265v1 -c linuxgpio -p m328p
 -U ./blink-2560-mod.hex 

avrdude_pr1265v1: AVR device initialized and ready to accept instructions
avrdude_pr1265v1: device signature = 0x1e950f (probably m328p)
avrdude_pr1265v1: Note: flash memory has been specified, an erase cycle will be performed.
                  To disable this feature, specify the -D option.
avrdude_pr1265v1: erasing chip
avrdude_pr1265v1: reading input file ./blink-2560-mod.hex for flash
                  with 1489 bytes in 2 sections within [0, 0x5d1]
                  using 12 pages and 47 pad bytes
avrdude_pr1265v1: writing 1489 bytes flash ...

Writing | ################################################## | 100% 0.34 s 

avrdude_pr1265v1: 1489 bytes of flash written
avrdude_pr1265v1: verifying flash memory against ./blink-2560-mod.hex

Reading | ################################################## | 100% 0.27 s 

avrdude_pr1265v1: 1489 bytes of flash verified

avrdude_pr1265v1 done.  Thank you.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants