ATmega8 (961722), страница 44
Текст из файла (страница 44)
This means that the interrupt can be usedinstead of polling the SPMCR Register in software. When using the SPM interrupt, theInterrupt Vectors should be moved to the BLS section to avoid that an interrupt isaccessing the RWW section when it is blocked for reading. How to move the interruptsis described in “Interrupts” on page 44.Consideration While UpdatingBLSSpecial care must be taken if the user allows the Boot Loader section to be updated byleaving Boot Lock bit11 unprogrammed. An accidental write to the Boot Loader itself cancorrupt the entire Boot Loader, and further software updates might be impossible. If it isnot necessary to change the Boot Loader software itself, it is recommended to programthe Boot Lock bit11 to protect the Boot Loader software from any internal softwarechanges.Prevent Reading the RWWSection During SelfProgrammingDuring Self-Programming (either page erase or page write), the RWW section is alwaysblocked for reading.
The user software itself must prevent that this section is addressedduring the self programming operation. The RWWSB in the SPMCR will be set as longas the RWW section is busy. During Self-Programming the Interrupt Vector table shouldbe moved to the BLS as described in “Interrupts” on page 44, or the interrupts must bedisabled. Before addressing the RWW section after the programming is completed, theuser software must clear the RWWSB by writing the RWWSRE. See “Simple AssemblyCode Example for a Boot Loader” on page 216 for an example.2132486O–AVR–10/04Setting the Boot Loader LockBits by SPMTo set the Boot Loader Lock Bits, write the desired data to R0, write “X0001001” toSPMCR and execute SPM within four clock cycles after writing SPMCR.
The onlyaccessible Lock Bits are the Boot Lock Bits that may prevent the Application and BootLoader section from any software update by the MCU.Bit76543210R011BLB12BLB11BLB02BLB0111See Table 78 and Table 79 for how the different settings of the Boot Loader Bits affectthe Flash access.If bits 5..2 in R0 are cleared (zero), the corresponding Boot Lock bit will be programmedif an SPM instruction is executed within four cycles after BLBSET and SPMEN are set inSPMCR. The Z-pointer is don’t care during this operation, but for future compatibility it isrecommended to load the Z-pointer with 0x0001 (same as used for reading the LockBits). For future compatibility It is also recommended to set bits 7, 6, 1, and 0 in R0 to “1”when writing the Lock Bits.
When programming the Lock Bits the entire Flash can beread during the operation.EEPROM Write PreventsWriting to SPMCRNote that an EEPROM write operation will block all software programming to Flash.Reading the Fuses and Lock Bits from software will also be prevented during theEEPROM write operation. It is recommended that the user checks the status bit (EEWE)in the EECR Register and verifies that the bit is cleared before writing to the SPMCRRegister.Reading the Fuse and LockBits from SoftwareIt is possible to read both the Fuse and Lock Bits from software. To read the Lock Bits,load the Z-pointer with 0x0001 and set the BLBSET and SPMEN bits in SPMCR.
Whenan LPM instruction is executed within three CPU cycles after the BLBSET and SPMENbits are set in SPMCR, the value of the Lock Bits will be loaded in the destination register. The BLBSET and SPMEN bits will auto-clear upon completion of reading the LockBits or if no LPM instruction is executed within three CPU cycles or no SPM instructionis executed within four CPU cycles.
When BLBSET and SPMEN are cleared, LPM willwork as described in the Instruction set Manual.Bit76543210Rd––BLB12BLB11BLB02BLB01LB2LB1The algorithm for reading the Fuse Low bits is similar to the one described above forreading the Lock Bits. To read the Fuse Low bits, load the Z-pointer with 0x0000 and setthe BLBSET and SPMEN bits in SPMCR.
When an LPM instruction is executed withinthree cycles after the BLBSET and SPMEN bits are set in the SPMCR, the value of theFuse Low bits (FLB) will be loaded in the destination register as shown below. Refer toTable 88 on page 221 for a detailed description and mapping of the fuse low bits.Bit76543210RdFLB7FLB6FLB5FLB4FLB3FLB2FLB1FLB0Similarly, when reading the Fuse High bits, load 0x0003 in the Z-pointer. When an LPMinstruction is executed within three cycles after the BLBSET and SPMEN bits are set inthe SPMCR, the value of the Fuse High bits (FHB) will be loaded in the destination register as shown below. Refer to Table 87 on page 220 for detailed description andmapping of the fuse high bits.Bit76543210RdFHB7FHB6FHB5FHB4FHB3FHB2FHB1FHB0Fuse and Lock Bits that are programmed, will be read as zero. Fuse and Lock Bits thatare unprogrammed, will be read as one.214ATmega8(L)2486O–AVR–10/04ATmega8(L)Preventing Flash CorruptionDuring periods of low VCC, the Flash program can be corrupted because the supply voltage is too low for the CPU and the Flash to operate properly.
These issues are the sameas for board level systems using the Flash, and the same design solutions should beapplied.A Flash program corruption can be caused by two situations when the voltage is too low.First, a regular write sequence to the Flash requires a minimum voltage to operate correctly. Secondly, the CPU itself can execute instructions incorrectly, if the supply voltagefor executing instructions is too low.Flash corruption can easily be avoided by following these design recommendations (oneis sufficient):1. If there is no need for a Boot Loader update in the system, program the BootLoader Lock Bits to prevent any Boot Loader software updates.2.
Keep the AVR RESET active (low) during periods of insufficient power supplyvoltage. This can be done by enabling the internal Brown-out Detector (BOD) ifthe operating voltage matches the detection level. If not, an external low VCCReset Protection circuit can be used. If a reset occurs while a write operation isin progress, the write operation will be completed provided that the power supplyvoltage is sufficient.3. Keep the AVR core in Power-down sleep mode during periods of low VCC. Thiswill prevent the CPU from attempting to decode and execute instructions, effectively protecting the SPMCR Register and thus the Flash from unintentionalwrites.Programming Time for Flashwhen using SPMThe calibrated RC Oscillator is used to time Flash accesses.
Table 81 shows the typicalprogramming time for Flash accesses from the CPU.Table 81. SPM Programming TimeSymbolFlash write (page erase, page write,and write Lock Bits by SPM)Min Programming TimeMax Programming Time3.7 ms4.5 ms2152486O–AVR–10/04Simple Assembly CodeExample for a Boot Loader;-the routine writes one page of data from RAM to Flash; the first data location in RAM is pointed to by the Y pointer; the first data location in Flash is pointed to by the Z-pointer;-error handling is not included;-the routine must be placed inside the boot space; (at least the Do_spm sub routine). Only code inside NRWW sectioncan; be read during self-programming (page erase and page write).;-registers used: r0, r1, temp1 (r16), temp2 (r17), looplo (r24),; loophi (r25), spmcrval (r20); storing and restoring of registers is not included in the routine; register usage can be optimized at the expense of code size;-It is assumed that either the interrupt table is moved to theBoot; loader section or that the interrupts are disabled..equ PAGESIZEB = PAGESIZE*2;PAGESIZEB is page size in BYTES, notwords.org SMALLBOOTSTARTWrite_page:; page eraseldi spmcrval, (1<<PGERS) | (1<<SPMEN)rcallDo_spm; re-enable the RWW sectionldi spmcrval, (1<<RWWSRE) | (1<<SPMEN)rcallDo_spm; transfer data from RAM to Flash page bufferldi looplo, low(PAGESIZEB);init loop variableldi loophi, high(PAGESIZEB) ;not required for PAGESIZEB<=256Wrloop:ldr0, Y+ldr1, Y+ldi spmcrval, (1<<SPMEN)rcallDo_spmadiw ZH:ZL, 2sbiw loophi:looplo, 2;use subi for PAGESIZEB<=256brne Wrloop; execute page writesubi ZL, low(PAGESIZEB);restore pointersbci ZH, high(PAGESIZEB);not required for PAGESIZEB<=256ldi spmcrval, (1<<PGWRT) | (1<<SPMEN)rcallDo_spm; re-enable the RWW sectionldi spmcrval, (1<<RWWSRE) | (1<<SPMEN)rcallDo_spm; read back and check, optionalldi looplo, low(PAGESIZEB);init loop variableldi loophi, high(PAGESIZEB) ;not required for PAGESIZEB<=256subi YL, low(PAGESIZEB);restore pointersbci YH, high(PAGESIZEB)Rdloop:lpm r0, Z+ldr1, Y+cpse r0, r1rjmp Error216ATmega8(L)2486O–AVR–10/04ATmega8(L)sbiw loophi:looplo, 1brne Rdloop;use subi for PAGESIZEB<=256; return to RWW section; verify that RWW section is safe to readReturn:intemp1, SPMCRsbrs temp1, RWWSB; If RWWSB is set, the RWW section isnot ready yetret; re-enable the RWW sectionldi spmcrval, (1<<RWWSRE) | (1<<SPMEN)rcallDo_spmrjmp ReturnDo_spm:; check for previous SPM completeWait_spm:intemp1, SPMCRsbrc temp1, SPMENrjmp Wait_spm; input: spmcrval determines SPM action; disable interrupts if enabled, store statusintemp2, SREGcli; check that no EEPROM write access is presentWait_ee:sbic EECR, EEWErjmp Wait_ee; SPM timed sequenceout SPMCR, spmcrvalspm; restore SREG (to enable interrupts if originally enabled)out SREG, temp2retATmega8 Boot LoaderParametersIn Table 82 through Table 84, the parameters used in the description of the self programming are given.Table 82.