Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
4
Добавлен:
18.07.2023
Размер:
8.82 Mб
Скачать

ware. When using the SPM interrupt, the Interrupt Vectors should be moved to the BLS section to avoid that an interrupt is accessing the RWW section when it is blocked for reading. How to move the interrupts is described in “Interrupts” on page 101.

29.6.5Consideration While Updating BLS

Special care must be taken if the user allows the Boot Loader section to be updated by leaving Boot Lock bit11 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 bit11 to protect the Boot Loader software from any internal software changes.

29.6.6Prevent Reading the RWW Section During Self-Programming

During Self-Programming (either Page Erase or Page Write), the RWW section is always blocked for reading. The user software itself must prevent that this section is addressed during the self programming operation. The RWWSB in the SPMCSR will be set as long as the RWW section is busy. During Self-Programming the Interrupt Vector table should be moved to the BLS as described in “Interrupts” on page 101, or the interrupts must be disabled. Before addressing the RWW section after the programming is completed, the user software must clear the RWWSB by writing the RWWSRE. See “Simple Assembly Code Example for a Boot Loader” on page 318 for an example.

29.6.7Setting the Boot Loader Lock Bits by SPM

To set the Boot Loader Lock bits and general Lock bits, write the desired data to R0, write “X0001001” to SPMCSR and execute SPM within four clock cycles after writing SPMCSR.

Bit

7

6

5

4

3

2

1

0

R0

1

1

BLB12

BLB11

BLB02

BLB01

LB2

LB1

 

 

 

 

 

 

 

 

 

See Table 29-2 on page 313 and Table 29-3 on page 313 for how the different settings of the Boot Loader bits affect the Flash access.

If bits 5:0 in R0 are cleared (zero), the corresponding Lock bit will be programmed if an SPM instruction is executed within four cycles after BLBSET and SPMEN are set in SPMCSR. The Z-pointer is don’t care during this operation, but for future compatibility it is recommended to load the Z-pointer with 0x0001 (same as used for reading the lOck bits). For future compatibility it is also recommended to set bits 7 and 6 in R0 to “1” when writing the Lock bits. When programming the Lock bits the entire Flash can be read during the operation.

29.6.8EEPROM Write Prevents Writing to SPMCSR

Note 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 the EEPROM write operation. It is recommended that the user checks the status bit (EEPE) in the EECR Register and verifies that the bit is cleared before writing to the SPMCSR Register.

29.6.9Reading the Fuse and Lock Bits from Software

It 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 SPMCSR. When an (E)LPM instruction is executed within three CPU cycles after the BLBSET and SPMEN bits are set in SPMCSR, 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 Lock bits or if no (E)LPM instruction is executed within three CPU cycles or no SPM instruction is executed within four CPU cycles. When BLBSET and SPMEN are cleared, (E)LPM will work as described in the Instruction set Manual.

Bit

7

6

5

4

3

2

1

0

Rd

BLB12

BLB11

BLB02

BLB01

LB2

LB1

 

 

 

 

 

 

 

 

 

ATmega640/V-1280/V-1281/V-2560/V-2561/V [DATASHEET]

316

2549Q–AVR–02/2014

The algorithm for reading the Fuse Low byte is similar to the one described above for reading the Lock bits. To read the Fuse Low byte, load the Z-pointer with 0x0000 and set the BLBSET and SPMEN bits in SPMCSR. When an (E)LPM instruction is executed within three cycles after the BLBSET and SPMEN bits are set in the SPMCSR, the value of the Fuse Low byte (FLB) will be loaded in the destination register as shown below. Refer to Table 30- 5 on page 327 for a detailed description and mapping of the Fuse Low byte.

Bit

7

6

5

4

3

2

1

0

Rd

FLB7

FLB6

FLB5

FLB4

FLB3

FLB2

FLB1

FLB0

 

 

 

 

 

 

 

 

 

 

Similarly, when reading the Fuse High byte, load 0x0003 in the Z-pointer. When an (E)LPM instruction is executed within three cycles after the BLBSET and SPMEN bits are set in the SPMCSR, the value of the Fuse High byte (FHB) will be loaded in the destination register as shown below. Refer to Table 30-4 on page 327 for detailed description and mapping of the Fuse High byte.

Bit

7

6

5

4

3

2

1

0

Rd

FHB7

FHB6

FHB5

FHB4

FHB3

FHB2

FHB1

FHB0

 

 

 

 

 

 

 

 

 

 

When reading the Extended Fuse byte, load 0x0002 in the Z-pointer. When an (E)LPM instruction is executed within three cycles after the BLBSET and SPMEN bits are set in the SPMCSR, the value of the Extended Fuse byte (EFB) will be loaded in the destination register as shown below. Refer to Table 30-3 on page 326 for detailed description and mapping of the Extended Fuse byte.

Bit

7

6

5

4

3

2

1

0

Rd

EFB2

EFB1

EFB0

 

 

 

 

 

 

 

 

 

 

Fuse and Lock bits that are programmed, will be read as zero. Fuse and Lock bits that are unprogrammed, will be read as one.

29.6.10Reading the Signature Row from Software

To read the Signature Row from software, load the Z-pointer with the signature byte address given in Table 29-5 on page 317 and set the SIGRD and SPMEN bits in SPMCSR. When an LPM instruction is executed within three CPU cycles after the SIGRD and SPMEN bits are set in SPMCSR, the signature byte value will be loaded in the destination register. The SIGRD and SPMEN bits will auto-clear upon completion of reading the Signature Row Lock bits or if no LPM instruction is executed within three CPU cycles. When SIGRD and SPMEN are cleared, LPM will work as described in the Instruction set Manual.

Table 29-5. Signature Row Addressing

Signature Byte

Z-Pointer Address

 

 

Device Signature Byte 1

0x0000

 

 

Device Signature Byte 2

0x0002

 

 

Device Signature Byte 3

0x0004

 

 

RC Oscillator Calibration Byte

0x0001

 

 

Note: All other addresses are reserved for future use.

29.6.11Preventing Flash Corruption

During 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 same as for board level systems using the Flash, and the same design solutions should be applied.

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 voltage for executing instructions is too low.

ATmega640/V-1280/V-1281/V-2560/V-2561/V [DATASHEET]

317

2549Q–AVR–02/2014

Flash corruption can easily be avoided by following these design recommendations (one is sufficient):

1.If there is no need for a Boot Loader update in the system, program the Boot Loader Lock bits to prevent any Boot Loader software updates.

2.Keep the AVR RESET active (low) during periods of insufficient power supply voltage. This can be done by enabling the internal Brown-out Detector (BOD) if the operating voltage matches the detection level. If not,

an external low VCC reset protection circuit can be used. If a reset occurs while a write operation is in progress, the write operation will be completed provided that the power supply voltage is sufficient.

3.Keep the AVR core in Power-down sleep mode during periods of low VCC. This will prevent the CPU from attempting to decode and execute instructions, effectively protecting the SPMCSR Register and thus the Flash from unintentional writes.

29.6.12Programming Time for Flash when Using SPM

The calibrated RC Oscillator is used to time Flash accesses. Table 29-6 shows the typical programming time for Flash accesses from the CPU.

Table 29-6. SPM Programming Time

Symbol

Min Programming Time

Max Programming Time

 

 

 

Flash write (Page Erase, Page Write, and write Lock bits by SPM)

3.7ms

4.5ms

 

 

 

29.6.13 Simple Assembly Code Example 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 section can

;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 the Boot

; loader section or that the interrupts are disabled.

.equ PAGESIZEB = PAGESIZE*2

;PAGESIZEB is page size in BYTES, not words

.org SMALLBOOTSTART

 

Write_page:

 

; Page Erase

 

ldi

spmcrval, (1<<PGERS) | (1<<SPMEN)

call Do_spm

 

; re-enable the RWW section

 

ldi

spmcrval, (1<<RWWSRE) | (1<<SPMEN)

call Do_spm

 

; transfer data from RAM to Flash page buffer

ldi

looplo, low(PAGESIZEB)

;init loop variable

ldi

loophi, high(PAGESIZEB)

;not required for PAGESIZEB<=256

Wrloop:

 

ld

r0, Y+

 

ld

r1, Y+

 

ldi

spmcrval, (1<<SPMEN)

 

call

Do_spm

 

adiw

ZH:ZL, 2

 

sbiw

loophi:looplo, 2

;use subi for PAGESIZEB<=256

brne

Wrloop

 

ATmega640/V-1280/V-1281/V-2560/V-2561/V [DATASHEET]

318

2549Q–AVR–02/2014

 

 

 

 

 

 

; execute Page Write

 

subi

ZL, low(PAGESIZEB)

;restore pointer

sbci

ZH, high(PAGESIZEB)

;not required for PAGESIZEB<=256

ldi

spmcrval, (1<<PGWRT) | (1<<SPMEN)

call Do_spm

 

; re-enable the RWW section

 

ldi

spmcrval, (1<<RWWSRE) | (1<<SPMEN)

call Do_spm

 

; read back and check, optional

ldi

looplo, low(PAGESIZEB)

;init loop variable

ldi

loophi, high(PAGESIZEB)

;not required for PAGESIZEB<=256

subi

YL, low(PAGESIZEB)

;restore pointer

sbci

YH, high(PAGESIZEB)

 

Rdloop:

 

elpm r0, Z+

 

ld

r1, Y+

 

cpse

r0, r1

 

jmp

Error

 

sbiw

loophi:looplo, 1

;use subi for PAGESIZEB<=256

brne

Rdloop

 

;return to RWW section

;verify that RWW section is safe to read Return:

in temp1, SPMCSR

sbrs temp1, RWWSB ; If RWWSB is set, the RWW section is not ready yet ret

; re-enable the RWW section

ldi spmcrval, (1<<RWWSRE) | (1<<SPMEN) call Do_spm

rjmp Return

Do_spm:

;check for previous SPM complete Wait_spm:

in temp1, SPMCSR sbrc temp1, SPMEN rjmp Wait_spm

;input: spmcrval determines SPM action

;disable interrupts if enabled, store status in temp2, SREG

cli

;check that no EEPROM write access is present Wait_ee:

sbic EECR, EEPE rjmp Wait_ee

;SPM timed sequence

out SPMCSR, spmcrval spm

; restore SREG (to enable interrupts if originally enabled) out SREG, temp2

ret

ATmega640/V-1280/V-1281/V-2560/V-2561/V [DATASHEET]

319

2549Q–AVR–02/2014