Capabilities of the boot loader, Self-programming the flash, Consideration while updating bls – Rainbow Electronics ATmega163L User Manual
Page 117: Atmega163(l)

ATmega163(L)
117
Capabilities of the Boot Loader
The program code within the Boot Loader section has the capability to read from and write into the entire Flash, including
the Boot Loader Memory. This allows the user to update both the Application code and the Boot Loader code that handles
the software update. The Boot Loader can thus even modify itself, and it can also erase itself from the code if the feature is
not needed anymore. Special care must be taken if the user allows the Boot Loader section to be updated by leaving Boot
Lock Bit 11 unprogrammed. An accidental write to the Boot Loader itself can corrupt the entire Boot Loader, and further
software updates might be impossible. If it is not necessary to change the Boot Loader software itself, it is recommended to
program the Boot Lock Bit 11 to protect the Boot Loader software from any internal software changes.
Self-programming the Flash
Programming of the Flash is executed one page at a time. The Flash page must be erased first for correct programming.
The general Write Lock (Lock Bit 2) does not control the programming of the Flash memory by SPM instruction. Similarly,
the general Read/Write Lock (Lock Bit 1) does not control reading nor writing by LPM/SPM, if it is attempted.
The program memory can only be updated page by page, not word by word. One page is 128 bytes (64 words). The pro-
gram memory will be modified by first performing page erase, then filling the temporary page buffer one word at a time
using SPM, and then executing page write. If only part of the page needs to be changed, the other parts must be stored
(for example in internal SRAM) before the erase, and then be rewritten. The temporary page buffer can be accessed in a
random sequence. It is essential that the page address used in both the page erase and page write operation is addressing
the same page. See “Assembly code example for a Boot Loader” on page 121 for an assembly code example.
Performing Page Erase by SPM
To execute page erase, set up the address in the Z pointer, write "00011" to the five LSB in SPMCR and execute SPM
within four clock cycles after writing SPMCR. The data in R1 and R0 is ignored. The page address must be written to
Z13:Z7. Other bits in the Z pointer will be ignored during this operation. It is recommended that the interrupts are disabled
during the page erase operation.
Fill the Temporary Buffer (Page Load)
To write an instruction word, set up the address in the Z pointer and data in R1:R0, write "00001" to the five LSB in SPMCR
and execute SPM within four clock cycles after writing SPMCR. The content of Z6:Z1 is used to address the data in the
temporary buffer. Z13:Z7 must point to the page that is supposed to be written.
Perform a Page Write
To execute page write, set up the address in the Z pointer, write "00101" to the five LSB in SPMCR and execute SPM
within four clock cycles after writing SPMCR. The data in R1 and R0 is ignored. The page address must be written to
Z13:Z7. During this operation, Z6:Z0 must be zero to ensure that the page is written correctly. It is recommended that the
interrupts are disabled during the page write operation.
Consideration while Updating BLS
Special care must be taken if the user allows the Boot Loader section to be updated by leaving Boot Lock Bit 11 unpro-
grammed. An accidental write to the Boot Loader itself can corrupt the entire Boot Loader, and further software updates
might be impossible. If it is not necessary to change the Boot Loader software itself, it is recommended to program the Boot
Lock Bit 11 to protect the Boot Loader software from any internal software changes.
Table 51. Boot Reset Fuse
BOOTRST
Reset Address
0
Reset Vector = Application reset (address $0000)
1
Reset Vector = Boot Loader reset (see Table 50)