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

SerialUPDI does not support page erase #1111

Closed
MCUdude opened this issue Oct 6, 2022 · 7 comments
Closed

SerialUPDI does not support page erase #1111

MCUdude opened this issue Oct 6, 2022 · 7 comments
Labels
enhancement New feature or request

Comments

@MCUdude
Copy link
Collaborator

MCUdude commented Oct 6, 2022

Now that #1106 is merged, page erase support is getting more relevant, as Avrdude only needs to erase the pages modified and not the whole memory. @dbuchwald did a fantastic job with the current SerialUPDI implementation but is lacking (flash) page erase support.

avrdude/src/serialupdi.c

Lines 885 to 891 in 44fe5be

static int serialupdi_page_erase(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m,
unsigned int baseaddr)
{
avrdude_message(MSG_INFO, "%s: error: page erase not implemented yet\n",
progname);
return -1;
}

However, it appears that the underlying functionality is already present:

avrdude/src/updi_nvm.c

Lines 1075 to 1088 in 44fe5be

int updi_nvm_erase_flash_page(const PROGRAMMER *pgm, const AVRPART *p, uint32_t address) {
switch(updi_get_nvm_mode(pgm))
{
case UPDI_NVM_MODE_V0:
return nvm_erase_flash_page_V0(pgm, p, address);
case UPDI_NVM_MODE_V2:
return nvm_erase_flash_page_V2(pgm, p, address);
case UPDI_NVM_MODE_V3:
return nvm_erase_flash_page_V3(pgm, p, address);
default:
avrdude_message(MSG_INFO, "%s: Invalid NVM Mode %d\n", progname, updi_get_nvm_mode(pgm));
return -1;
}
}

I'm currently working on UPDI page erase support for JTAG3 compatible programmers.

@mcuee mcuee added the enhancement New feature or request label Oct 6, 2022
@dbuchwald
Copy link
Contributor

dbuchwald commented Oct 6, 2022 via email

@MCUdude
Copy link
Collaborator Author

MCUdude commented Oct 6, 2022

Hi Dawid!

Sorry to have not responded before, I’m in a middle of life-induced shitstorm, but I will try to make some time to look into it.

I'm sorry to hear that! There's no rush, so do it if/when you have the energy to look into it.

Here are some test commands you can use for testing with the latest upstream version of Avrdude. You can paste it all at once in the terminal window. For more info regarding terminal flash write, see #1106.

echo erase | ./avrdude -qqp atmega3209 -c pkobn_updi -t
echo 'read flash 0x3100 16' | ./avrdude -qqp atmega3209 -c pkobn_updi -t
echo 'write flash 0x3100 "hello, world\n"' | ./avrdude -qqp atmega3209 -c pkobn_updi -t
echo 'read flash 0x3100 16' | ./avrdude -qqp atmega3209 -c pkobn_updi -t
echo 'write flash 0x3100 "Hello, world\n"' | ./avrdude -qqp atmega3209 -c pkobn_updi -t
echo 'read flash 0x3100 16' | ./avrdude -qqp atmega3209 -c pkobn_updi -t
echo 'write flash 0x3100 "hello, world\n"' | ./avrdude -qqp atmega3209 -c pkobn_updi -t
echo 'read flash 0x3100 16' | ./avrdude -qqp atmega3209 -c pkobn_updi -t

Here's the result with and without page erase support for a JTAG3 programmer. Notice the warning message and the read time for the second last command:

Without jtag3 UPDI page erase fix
$ echo erase | ./avrdude -p avr128db48 -c pkobn_updi -t 

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> erase 
avrdude: erasing chip

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|


avrdude done.  Thank you.

$ echo 'write flash 0x1000 "hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> write flash 0x1000 "hello, world\n" 

Writing | ################################################## | 100% 0.09s

avrdude: writing cache to device... done

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  68 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |hello, world ...|


avrdude done.  Thank you.

$ echo 'write flash 0x1000 "Hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> write flash 0x1000 "Hello, world\n" 

Writing | ################################################## | 100% 0.09s

avrdude: writing cache to device... done

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  48 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |Hello, world ...|


avrdude done.  Thank you.

$ echo 'write flash 0x1000 "hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> write flash 0x1000 "hello, world\n" 

Writing | ################################################## | 100% 0.09s

avrdude: writing cache to device... avrdude: jtag3_page_erase: not an Xmega device

Reading | ################################################## | 100% 21.94s
Erasing | ################################################## | 100% 0.02s
Writing | ################################################## | 100% 0.24s

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  68 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |hello, world ...|


avrdude done.  Thank you.
With jtag3 UPDI page erase fix
echo erase | ./avrdude -p avr128db48 -c pkobn_updi -t 

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> erase 
avrdude: erasing chip

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|


avrdude done.  Thank you.

$ echo 'write flash 0x1000 "hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> write flash 0x1000 "hello, world\n" 

Writing | ################################################## | 100% 0.09s

avrdude: writing cache to device... done

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  68 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |hello, world ...|


avrdude done.  Thank you.

$ echo 'write flash 0x1000 "Hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> write flash 0x1000 "Hello, world\n" 

Writing | ################################################## | 100% 0.09s

avrdude: writing cache to device... done

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  48 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |Hello, world ...|


avrdude done.  Thank you.

$ echo 'write flash 0x1000 "hello, world\n"' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> write flash 0x1000 "hello, world\n" 

Writing | ################################################## | 100% 0.09s

avrdude: writing cache to device... done

avrdude done.  Thank you.

$ echo 'read flash 0x1000 16' | ./avrdude -p avr128db48 -c pkobn_updi -t

         Vtarget                      : 3.31 V
         PDI/UPDI clock Xmega/megaAVR : 100 kHz

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e970c (probably avr128db48)
>>> read flash 0x1000 16 

Reading | ################################################## | 100% 0.09s

1000  68 65 6c 6c 6f 2c 20 77  6f 72 6c 64 0a 00 ff ff  |hello, world ...|


avrdude done.  Thank you.

@stefanrueger
Copy link
Collaborator

@dbuchwald Thanks for reply - I hope things go well.

In order to force page_erase() one can stay in the avrdude terminal (now there is the new flush command):

erase
write flash 0 0
flush
write flash 0 255
flush

Or, from a linux bash

$ echo "erase;write flash 0 0;flush;write flash 0 255;flush" | tr \; \\n | avrdude -c ... -qqp ... -t
avrdude> erase
avrdude: erasing chip
avrdude> write flash 0 0
avrdude> flush
avrdude: synching cache to device... done
avrdude> write flash 0 255
avrdude> flush
avrdude: synching cache to device... reading/chip erase/writing cycle needed ... done

The reading/chip erase/writing cycle is not necessary when page_erase is properly implemented. The terminal tries to use page_erase() whenever it needs to set a previously cleared bit in flash; if it's not there or doesn't work the chip is read, erased and written back.

@mcuee
Copy link
Collaborator

mcuee commented Oct 17, 2022

There is a write-up here, not so sure if it is relevant or not.
https://github.com/SpenceKonde/AVR-Guidance/blob/master/UPDI/jtag2updi.md#thoughts-on-paged-erase

@dbuchwald
Copy link
Contributor

dbuchwald commented Oct 23, 2022 via email

@stefanrueger
Copy link
Collaborator

avrdude> write flash 0 255

255 is interpreted as signed integer. Either use 0xff or 255U to make it unsigned. A signed 255 does not fit into one signed byte that has a range of [-128, 127]. This needs two bytes.

@mcuee
Copy link
Collaborator

mcuee commented Nov 1, 2022

@mcuee mcuee closed this as completed Nov 1, 2022
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

No branches or pull requests

4 participants