From 4e2413bacbff7859a0db3f2da4240d28204a3087 Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Sat, 6 Jul 2024 16:01:46 +0100 Subject: [PATCH] Use page erase for UPDI programming --- src/avrdude.1 | 47 ++++++++++++++++++++++++------------- src/doc/avrdude.texi | 56 +++++++++++++++++++++++++++----------------- src/main.c | 19 ++++++++++----- 3 files changed, 78 insertions(+), 44 deletions(-) diff --git a/src/avrdude.1 b/src/avrdude.1 index 35fce6dd4..b540d244c 100644 --- a/src/avrdude.1 +++ b/src/avrdude.1 @@ -472,17 +472,15 @@ arduino. .It Fl D Disable auto erase for flash. When the .Fl U -option with flash memory is specified, +option for writing to any flash memory is specified, .Nm will perform a chip erase before starting any of the programming -operations, since it generally is a mistake to program the flash -without performing an erase first. This option disables that. -Auto erase is not used for ATxmega devices as these devices can -use page erase before writing each page so no explicit chip erase -is required. -Note however that any page not affected by the current operation -will retain its previous contents. -Setting +operations, since it generally is a mistake to program the flash without +performing an erase first. This option disables that. Auto erase is not +used for ATxmega parts nor for the UPDI (AVR8X family) parts as these can +use page erase before writing each page so no explicit chip erase is +required. Note, however, that any flash page not affected by the current +operation will retain its previous contents. Setting .Fl D implies .Fl A. @@ -491,9 +489,9 @@ Causes a chip erase to be executed. This will reset the contents of the flash ROM and EEPROM to the value .Ql 0xff , and clear all lock bits. -Except for ATxmega devices which can use page erase, -it is basically a prerequisite command before the flash ROM can be -reprogrammed again. The only exception would be if the new +Except for ATxmega and UPDI (AVR8X family) devices, all of which can use +page erase, it is basically a prerequisite command before the flash ROM +can be reprogrammed again. The only exception would be if the new contents would exclusively cause bits to be programmed from the value .Ql 1 to @@ -797,6 +795,7 @@ The available memory types are device-dependent, the actual configuration can be viewed with the .Cm part command in terminal mode. +.Pp Typically, a device's memory configuration at least contains the memories .Ar flash , @@ -812,10 +811,26 @@ memory contains the three device signature bytes, which should be, but not always are, unique for the part. The .Ar lock memory of one or four bytes typically details whether or not external -reading/writing of the flash memory, or parts of it, is allowed. Parts -will also typically have fuse bytes, which are read/write memories for -configuration of the device and calibration memories that typically -contain read-only factory calibration values. +reading/writing of the flash memory, or parts of it, is allowed. After +restricting access via the lock memory, often the only way to unlock +memory is via a chip erase. Parts will also typically have fuse bytes, +which are read/write memories for configuration of the device and +calibration memories that typically contain read-only factory calibration +values. +.Pp +The flash memory, being physically implemented as NOR-memory, is special +in the sense that it is normally only possible to program bits to change +from 1 to 0. Before reprogramming takes place normally flash memory has +to be erased. Older parts would only offer a chip erase to do so, which +also erases EEPROM unless a fuse configuration preserves its contents. If +AVRDUDE detects a -U option that writes to a flash memory it will +automatically trigger a chip erase for these older parts. ATxmegas or +UPDI parts (AVR8X family) offer a page erase, and AVRDUDE takes advantage +of that by erasing pages before programming them unless -e (chip erase) or +-D (do not erase before writing) was requested. It should be noted that in +absence of the -e chip erase option any ATxmega or UPDI flash pages not +affected by the programming will retain their previous content. + .Pp Classic devices may have the following memories in addition to eeprom, flash, signature and lock: .Bl -tag -width " calibration" -compact diff --git a/src/doc/avrdude.texi b/src/doc/avrdude.texi index 2d1db42de..4645d5af2 100644 --- a/src/doc/avrdude.texi +++ b/src/doc/avrdude.texi @@ -584,31 +584,28 @@ engaged by default when specifying -c arduino. @item -D @cindex Option @code{-D} -Disable auto erase for flash. When the -U option with flash memory is -specified, avrdude will perform a chip erase before starting any of the -programming operations, since it generally is a mistake to program the flash -without performing an erase first. This option disables that. -Auto erase is not used for ATxmega devices as these devices can -use page erase before writing each page so no explicit chip erase -is required. -Note however that any page not affected by the current operation -will retain its previous contents. -Setting -D implies -A. +Disable auto erase for flash. When the -U option for writing to any +flash memory is specified, AVRDUDE will perform a chip erase before +starting any of the programming operations, since it generally is a +mistake to program the flash without performing an erase first. This +option disables that. Auto erase is not used for ATxmega parts nor for the +UPDI (AVR8X family) parts as these can use page erase before writing each +page so no explicit chip erase is required. Note, however, that any flash +page not affected by the current operation will retain its previous +contents. Setting -D implies -A. @item -e @cindex Option @code{-e} Causes a chip erase to be executed. This will reset the contents of the -flash and EEPROM to the value `0xff', and clear all lock bits. -Except for ATxmega devices which can use page erase, -it is basically a -prerequisite command before the flash can be reprogrammed again. -The only exception would be if the new contents would exclusively cause -bits to be programmed from the value `1' to `0'. Note that in order -to reprogram EERPOM cells, no explicit prior chip erase is required -since the MCU provides an auto-erase cycle in that case before +flash and EEPROM to the value `0xff', and clear all lock bits. Except for +ATxmega and UPDI (AVR8X family) devices, all of which can use page erase, +it is basically a prerequisite command before the flash ROM can be +reprogrammed again. The only exception would be if the new contents would +exclusively cause bits to be programmed from the value `1' to `0'. Note +that in order to reprogram EERPOM cells, no explicit prior chip erase is +required since the MCU provides an auto-erase cycle in that case before programming the cell. - @item -E @var{exitspec}[,@dots{}] @cindex Option @code{-E} @var{exitspec}[,@dots{}] By default, AVRDUDE leaves the parallel port in the same state at exit @@ -860,9 +857,24 @@ is sometimes known as @code{lockbits}. The signature memory contains the three device signature bytes, which should be, but not always are, unique for the part. The @code{lock} memory of one or four bytes typically details whether or not external reading/writing of the flash memory, or -parts of it, is allowed. Parts will also typically have fuse bytes, which -are read/write memories for configuration of the device and calibration -memories that typically contain read-only factory calibration values. +parts of it, is allowed. After restricting access via the lock memory, +often the only way to unlock memory is via a chip erase. Parts will also +typically have fuse bytes, which are read/write memories for configuration +of the device and calibration memories that typically contain read-only +factory calibration values. + +The flash memory, being physically implemented as NOR-memory, is special +in the sense that it is normally only possible to program bits to change +from 1 to 0. Before reprogramming takes place normally flash memory has to +be erased. Older parts would only offer a chip erase to do so, which also +erases EEPROM unless a fuse configuration preserves its contents. If +AVRDUDE detects a -U option that writes to a flash memory it will +automatically trigger a chip erase for these older parts. ATxmegas or +UPDI parts (AVR8X family) offer a page erase, and AVRDUDE takes advantage +of that by erasing pages before programming them unless -e (chip erase) or +-D (do not erase before writing) was requested. It should be noted that in +absence of the -e chip erase option any ATxmega or UPDI flash pages not +affected by the programming will retain their previous content. Classic devices may have the following memories in addition to @code{eeprom}, @code{flash}, @code{signature} and @code{lock}: diff --git a/src/main.c b/src/main.c index 3e15575f5..51b0cb46a 100644 --- a/src/main.c +++ b/src/main.c @@ -1681,18 +1681,25 @@ int main(int argc, char * argv []) } if (uflags & UF_AUTO_ERASE) { - if ((p->prog_modes & PM_PDI) && pgm->page_erase && lsize(updates) > 0) { - pmsg_info("Note: programmer supports page erase for Xmega devices.\n"); - imsg_info("Each page will be erased before programming it, but no chip erase is performed.\n"); - imsg_info("To disable page erases, specify the -D option; for a chip-erase, use the -e option.\n"); + if((p->prog_modes & (PM_PDI | PM_UPDI)) && pgm->page_erase && lsize(updates) > 0) { + for(ln=lfirst(updates); ln; ln=lnext(ln)) { + upd = ldata(ln); + if(upd->memstr && upd->op == DEVICE_WRITE && memlist_contains_flash(upd->memstr, p)) { + cx->avr_disableffopt = 1; // Must write full flash file including trailing 0xff + pmsg_info("NOT erasing chip as page erase will be used for new flash%s contents\n", + avr_locate_bootrow(p)? "/bootrow": ""); + imsg_notice("unprogrammed flash contents remains: use -e for an explicit chip-erase\n"); + break; + } + } } else { uflags &= ~UF_AUTO_ERASE; for(ln=lfirst(updates); !erase && ln; ln=lnext(ln)) { upd = ldata(ln); if(upd->memstr && upd->op == DEVICE_WRITE && memlist_contains_flash(upd->memstr, p)) { erase = 1; - pmsg_info("Note: carrying out an erase cycle as flash memory needs programming (-U %s:w:...)\n", upd->memstr); - imsg_info("specify the -D option to disable this feature\n"); + pmsg_info("Performing a chip erase as flash memory needs programming (-U %s:w:...)\n", upd->memstr); + imsg_notice("specify the -D option to disable this feature\n"); } } }