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

Figure 29-2. Memory Sections

 

 

 

Program Memory

 

 

 

BOOTSZ = '11'

 

 

 

 

Section

 

 

Write-While-Read

 

Application Flash Section

Section

 

 

 

 

 

 

 

 

Write-While-

 

Application Flash Section

 

 

 

ReadNo

 

 

 

 

Boot Loader Flash Section

 

 

 

 

 

 

Program Memory

 

 

 

 

 

 

BOOTSZ = '01'

 

 

 

 

Section

 

 

Write-While-Read

 

Application Flash Section

Section

 

Application Flash Section

 

 

 

 

While-Write

 

 

 

 

Boot Loader Flash Section

No Read-

 

 

 

 

 

0x0000

 

Read-While-Write Section

End Application

Write-WhileSection

End RWW

 

Start NRWW

 

Flashend

-

Read

Start Boot Loader

 

 

No

0x0000

 

 

Read-While-Write Section

End Application

SWriteection

End RWW

 

Start NRWW

 

Flashend

-

-ReadWhile

Start Boot Loader

 

 

No

Program Memory

BOOTSZ = '10'

0x0000

Application Flash Section

End RWW

Start NRWW

Application Flash Section

End Application

Start Boot Loader

Boot Loader Flash Section

Flashend

Program Memory

BOOTSZ = '00'

0x0000

Application Flash Section

End RWW, End Application

Start NRWW, Start Boot Loader

Boot Loader Flash Section

Flashend

Note: 1. The parameters in the figure above are given in Table 29-7 on page 320.

29.4Boot Loader Lock Bits

If no Boot Loader capability is needed, the entire Flash is available for application code. The Boot Loader has two separate sets of Boot Lock bits which can be set independently. This gives the user a unique flexibility to select different levels of protection.

The user can select:

To protect the entire Flash from a software update by the MCU

To protect only the Boot Loader Flash section from a software update by the MCU

To protect only the Application Flash section from a software update by the MCU

Allow software update in the entire Flash

See Table 29-2 on page 313 and Table 29-3 on page 313 for further details. The Boot Lock bits can be set in software and in Serial or Parallel Programming mode, but they can be cleared by a Chip Erase command only. The general Write Lock (Lock Bit mode 2) does not control the programming of the Flash memory by SPM instruction. Similarly, the general Read/Write Lock (Lock Bit mode 1) does not control reading nor writing by (E)LPM/SPM, if it is attempted.

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

312

2549Q–AVR–02/2014

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Table 29-2.

Boot Lock Bit0 Protection Modes (Application Section)(1)

 

BLB0 Mode

 

BLB02

BLB01

Protection

 

 

 

 

 

 

 

 

 

1

 

1

1

No restrictions for SPM or (E)LPM accessing the Application section.

 

 

 

 

 

 

 

 

 

2

 

1

0

SPM is not allowed to write to the Application section.

 

 

 

 

 

 

 

 

 

 

 

 

 

SPM is not allowed to write to the Application section, and (E)LPM executing from

 

 

3

 

0

0

the Boot Loader section is not allowed to read from the Application section. If

 

 

 

Interrupt Vectors are placed in the Boot Loader section, interrupts are disabled while

 

 

 

 

 

 

 

 

 

 

 

 

executing from the Application section.

 

 

 

 

 

 

 

 

 

 

 

 

 

(E)LPM executing from the Boot Loader section is not allowed to read from the

 

 

4

 

0

1

Application section. If Interrupt Vectors are placed in the Boot Loader section,

 

 

 

 

 

 

interrupts are disabled while executing from the Application section.

 

 

 

 

 

 

 

 

Note: 1. “1” means unprogrammed, “0” means programmed.

Table 29-3.

Boot Lock Bit1 Protection Modes (Boot Loader Section)(1)

BLB1 Mode

 

BLB12

BLB11

Protection

 

 

 

 

 

1

 

1

1

No restrictions for SPM or (E)LPM accessing the Boot Loader section.

 

 

 

 

 

2

 

1

0

SPM is not allowed to write to the Boot Loader section.

 

 

 

 

 

 

 

 

 

SPM is not allowed to write to the Boot Loader section, and (E)LPM executing from

3

 

0

0

the Application section is not allowed to read from the Boot Loader section. If

 

Interrupt Vectors are placed in the Application section, interrupts are disabled while

 

 

 

 

 

 

 

 

executing from the Boot Loader section.

 

 

 

 

 

 

 

 

 

(E)LPM executing from the Application section is not allowed to read from the Boot

4

 

0

1

Loader section. If Interrupt Vectors are placed in the Application section, interrupts

 

 

 

 

are disabled while executing from the Boot Loader section.

 

 

 

 

 

Note: 1. “1” means unprogrammed, “0” means programmed.

29.4.1Entering the Boot Loader Program

Entering the Boot Loader takes place by a jump or call from the application program. This may be initiated by a trigger such as a command received via USART, or SPI interface. Alternatively, the Boot Reset Fuse can be programmed so that the Reset Vector is pointing to the Boot Flash start address after a reset. In this case, the Boot Loader is started after a reset. After the application code is loaded, the program can start executing the application code. Note that the fuses cannot be changed by the MCU itself. This means that once the Boot Reset Fuse is programmed, the Reset Vector will always point to the Boot Loader Reset and the fuse can only be changed through the serial or parallel programming interface.

Table 29-4.

Boot Reset Fuse(1)

BOOTRST

 

Reset Address

 

 

 

1

 

Reset Vector = Application Reset (address 0x0000)

 

 

 

0

 

Reset Vector = Boot Loader Reset (see Table 29-7 on page 320)

 

 

 

Note: 1. “1” means unprogrammed, “0” means programmed.

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

313

2549Q–AVR–02/2014

29.5Addressing the Flash During Self-Programming

The Z-pointer is used to address the SPM commands. The Z pointer consists of the Z-registers ZL and ZH in the register file, and RAMPZ in the I/O space. The number of bits actually used is implementation dependent. Note that the RAMPZ register is only implemented when the program space is larger than 64Kbytes.

Bit

23

22

21

20

19

18

17

16

 

15

14

13

12

11

10

9

8

 

 

 

 

 

 

 

 

 

RAMPZ

RAMPZ7

RAMPZ6

RAMPZ5

RAMPZ4

RAMPZ3

RAMPZ2

RAMPZ1

RAMPZ0

 

 

 

 

 

 

 

 

 

ZH (R31)

Z15

Z14

Z13

Z12

Z11

Z10

Z9

Z8

 

 

 

 

 

 

 

 

 

ZL (R30)

Z7

Z6

Z5

Z4

Z3

Z2

Z1

Z0

 

 

 

 

 

 

 

 

 

 

7

6

5

4

3

2

1

0

Since the Flash is organized in pages (see Table 30-7 on page 328), the Program Counter can be treated as having two different sections. One section, consisting of the least significant bits, is addressing the words within a page, while the most significant bits are addressing the pages. This is shown in Figure 29-3. Note that the Page Erase and Page Write operations are addressed independently. Therefore it is of major importance that the Boot Loader software addresses the same page in both the Page Erase and Page Write operation. Once a programming operation is initiated, the address is latched and the Z-pointer can be used for other operations.

The (E)LPM instruction use the Z-pointer to store the address. Since this instruction addresses the Flash byte-by- byte, also bit Z0 of the Z-pointer is used.

Figure 29-3. Addressing the Flash During SPM(1)

BIT 15

 

 

 

ZPCMSB

 

 

 

ZPAGEMSB 1 0

 

Z - REGISTER

 

 

 

 

 

 

 

 

 

 

 

 

0

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

PROGRAM

 

PCMSB

 

 

 

PAGEMSB

 

 

 

 

 

 

 

PCPAGE

PCWORD

 

 

 

 

 

 

COUNTER

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

PAGE ADDRESS

 

 

WORD ADDRESS

 

 

 

 

WITHIN THE FLASH

 

 

WITHIN A PAGE

 

PROGRAM MEMORY

 

 

 

 

 

 

 

 

 

PAGE

PCWORD[PAGEMSB:0]:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

PAGE

 

 

 

 

 

 

 

 

 

 

 

 

INSTRUCTION WORD

00

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

01

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

02

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

PAGEEND

Note: 1. The different variables used in Figure 29-3 are listed in Table 29-9 on page 320.

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

314

2549Q–AVR–02/2014

29.6Self-Programming the Flash

The program memory is updated in a page by page fashion. Before programming a page with the data stored in the temporary page buffer, the page must be erased. The temporary page buffer is filled one word at a time using SPM and the buffer can be filled either before the Page Erase command or between a Page Erase and a Page Write operation:

Alternative 1, fill the buffer before a Page Erase

Fill temporary page buffer

Perform a Page Erase

Perform a Page Write

Alternative 2, fill the buffer after Page Erase

Perform a Page Erase

Fill temporary page buffer

Perform a Page Write

If only a part of the page needs to be changed, the rest of the page must be stored (for example in the temporary page buffer) before the erase, and then be rewritten. When using alternative 1, the Boot Loader provides an effective Read-Modify-Write feature which allows the user software to first read the page, do the necessary changes, and then write back the modified data. If alternative 2 is used, it is not possible to read the old data while loading since the page is already erased. 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 “Simple Assembly Code Example for a Boot Loader” on page 318 for an assembly code example.

29.6.1Performing Page Erase by SPM

To execute Page Erase, set up the address in the Z-pointer, write “X0000011” to SPMCSR and execute SPM within four clock cycles after writing SPMCSR. The data in R1 and R0 is ignored. The page address must be written to PCPAGE in the Z-register. Other bits in the Z-pointer will be ignored during this operation.

Page Erase to the RWW section: The NRWW section can be read during the Page Erase

Page Erase to the NRWW section: The CPU is halted during the operation

29.6.2Filling the Temporary Buffer (Page Loading)

To write an instruction word, set up the address in the Z-pointer and data in R1:R0, write “00000001” to SPMCSR and execute SPM within four clock cycles after writing SPMCSR. The content of PCWORD in the Z-register is used to address the data in the temporary buffer. The temporary buffer will auto-erase after a Page Write operation or by writing the RWWSRE bit in SPMCSR. It is also erased after a system reset. Note that it is not possible to write more than one time to each address without erasing the temporary buffer.

If the EEPROM is written in the middle of an SPM Page Load operation, all data loaded is still buffered.

29.6.3Performing a Page Write

To execute Page Write, set up the address in the Z-pointer, write “X0000101” to SPMCSR and execute SPM within four clock cycles after writing SPMCSR. The data in R1 and R0 is ignored. The page address must be written to PCPAGE. Other bits in the Z-pointer must be written to zero during this operation.

Page Write to the RWW section: The NRWW section can be read during the Page Write

Page Write to the NRWW section: The CPU is halted during the operation

29.6.4Using the SPM Interrupt

If the SPM interrupt is enabled, the SPM interrupt will generate a constant interrupt when the SPMEN bit in SPMCSR is cleared. This means that the interrupt can be used instead of polling the SPMCSR Register in soft-

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

315

2549Q–AVR–02/2014