|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ATmega161(L) |
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||
|
Fill the Temporary Buffer |
To write an instruction word, set up the address in the Z-pointer and data in R1:R0, write |
||||||||||||||||
|
|
|
“0001” to 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 a page write, set up the address in the Z-pointer, write “0101” to SPMCR, |
||||||||||||||||
|
|
|
and execute SPM within four clock cycles after writing SPMCR. The data in R1 and R0 |
|||||||||||||||
|
|
|
are 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. When a page write operation is |
|||||||||||||||
|
|
|
completed, the Z-pointer will point to the first word in the successive page. |
|
||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code Example |
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
Wait: |
in |
r16,SPMCR ; read SPMCR register |
|
|
|
|
|
|
|||||||
|
|
|
sbrc |
r16,SPMEN |
; Wait for SPMEN to be cleared (indicates that previous |
|||||||||||||
|
|
|
|
|
|
|
|
write operation is completed) |
|
|
|
|
||||||
|
|
|
rjmp |
Wait |
|
; if not cleared, keep waiting |
|
|
|
|
||||||||
|
|
|
ldi |
r16,(1<<PGWRT) + (1<<SPMEN) |
; The previous writing is completed, |
|||||||||||||
|
|
|
|
|
|
|
|
|
|
|
set up for next erase |
|
|
|||||
|
|
|
out |
SPMCR,r16 |
; output to register |
|
|
|
|
|
|
|||||||
|
|
|
spm |
|
|
; start the erase operation |
|
|
|
|
|
|
||||||
|
Addressing the FLASH during |
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
The Z-pointer is used to address the SPM commands. |
|
|
|
|
|
|
|||||||||||
|
Self-programming |
Bit |
|
15 |
14 |
13 |
12 |
11 |
|
10 |
9 |
8 |
|
|||||
|
|
|
|
|
|
|||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$1F ($1F) |
|
Z15 |
Z14 |
|
Z13 |
Z12 |
|
Z11 |
|
Z10 |
|
Z9 |
Z8 |
ZH |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$1E ($1E) |
|
Z7 |
Z6 |
|
Z5 |
Z4 |
|
Z3 |
|
Z2 |
|
Z1 |
Z0 |
ZL |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
6 |
5 |
4 |
3 |
|
2 |
|
1 |
0 |
|
|||
|
|
|
Z15:Z14 |
always ignored |
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
Z13:Z7 page select, for page erase, page write |
|
|
|
|
|
|
|||||||||
|
|
|
Z6:Z1 |
word select, for filling temp buffer (must be zero during page write operation) |
||||||||||||||
|
|
|
Z0 |
should be zero for all SPM commands, byte select for the LPM instruction |
||||||||||||||
The only operation that does not use the Z-pointer is setting the Boot Loader Lock bits.
The content of the Z-pointer is ignored and will have no effect on the operation.
Note that the page erase and page write operation 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 operations.
The LPM instruction also uses the Z-pointer to store the address. Since this instruction addresses the Flash byte-by-byte, the LSB (bit Z0) of the Z-pointer is also used. See page 16 for a detailed description.
Accidental writing into Flash program by the SPM instruction is prevented by setting up an “SPM enable time window”. All accesses are executed by first setting I/O bits, and then by executing SPM within four clock cycles. The I/O register that controls the SPM accesses is defined below.
111
1228B–09/01
Store Program Memory |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The Store Program Memory Control Register contains the control bits needed to control |
|||||||||||||||||
Control Register – SPMCR |
the programming of the Flash from internal code execution. |
|
|
|
|||||||||||||
|
Bit |
7 |
6 |
5 |
|
|
|
|
4 |
3 |
|
2 |
|
1 |
0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$37 ($57) |
– |
– |
|
|
|
|
|
|
|
BLBSET |
|
PGWRT |
|
PGERS |
SPMEN |
SPMCR |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
Read/Write |
R |
R |
R |
R |
R/W |
R/W |
R/W |
R/W |
||||||||
|
Initial Value |
0 |
0 |
0 |
|
|
|
|
0 |
0 |
|
0 |
|
0 |
0 |
|
|
|
• Bits 7..4 – Res: Reserved Bits |
|
|
|
|
|
|
|
|
|
|||||||
|
These bits are reserved bits in the ATmega161 and always read as zero. |
||||||||||||||||
|
• Bit 3 – BLBSET: Boot Lock Bit Set |
|
|
|
|
|
|
|
|||||||||
|
If this bit is set at the same time as SPMEN, the next SPM instruction within four clock |
||||||||||||||||
|
cycles sets Boot Lock bits according to the data in R0. The data in R1 and the address |
||||||||||||||||
|
in the Z-pointer are ignored. The BLBSET bit will auto-clear upon completion of Lock bit |
||||||||||||||||
|
set, or if no SPM instruction is executed within four clock cycles. The CPU is halted dur- |
||||||||||||||||
|
ing Lock bit setting. Only a chip erase can clear the Lock bits. |
|
|
|
|||||||||||||
|
An LPM instruction within four cycles after BLBSET and SPMEN are set in the SPMCR |
||||||||||||||||
|
register will put either the Lock bits or the Fuse bits (depending od the Z0 in the Z- |
||||||||||||||||
|
pointer) into the destination register. See “Reading the Fuse and Lock Bits from Soft- |
||||||||||||||||
|
ware” for details. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
• Bit 2 – PGWRT: Page Write |
|
|
|
|
|
|
|
|
|
|||||||
|
If this bit is set at the same time as SPMEN, the next SPM instruction within four clock |
||||||||||||||||
|
cycles executes page write, with the data stored in the temporary buffer. The page |
||||||||||||||||
|
address is taken from the high part of the Z-pointer. The data in R1 and R0 are ignored. |
||||||||||||||||
|
The PGWRT bit will auto-clear upon completion of a page write or if no SPM instruction |
||||||||||||||||
|
is executed within four clock cycles. The CPU is halted during the entire page write |
||||||||||||||||
|
operation. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
• Bit 1 – PGERS: Page Erase |
|
|
|
|
|
|
|
|
|
|||||||
|
If this bit is set at the same time as SPMEN, the next SPM instruction within four clock |
||||||||||||||||
|
cycles executes a page erase. The page address is taken from the high part of the Z- |
||||||||||||||||
|
pointer. The data in R1 and R0 are ignored. The PGERS bit will auto-clear upon comple- |
||||||||||||||||
|
tion of a page erase or if no SPM instruction is executed within four clock cycles. The |
||||||||||||||||
|
CPU is halted during the entire page erase operation. |
|
|
|
|||||||||||||
|
• Bit 0 – SPMEN: Store Program Memory Enable |
|
|
|
|
|
|||||||||||
|
This bit enables the SPM instruction for the next four clock cycles. If set together with |
||||||||||||||||
|
either BLBSET, PGWRT or PGERS, the following SPM instruction will have a special |
||||||||||||||||
|
meaning (see description above). If only SPMEN is set, the following SPM instruction |
||||||||||||||||
|
will store the value in R1:R0 in the temporary page buffer addressed by the Z-pointer. |
||||||||||||||||
|
The LSB of the Z-pointer is ignored. The SPMEN bit will auto-clear upon completion of |
||||||||||||||||
|
an SPM instruction or if no SPM instruction is executed within four clock cycles. |
||||||||||||||||
|
Writing any combination other than “1001”, “0101”, “0011” or “0001” in the lower four |
||||||||||||||||
|
bits, or writing to the I/O register when any bits are set, will have no effect. |
||||||||||||||||
EEPROM Write Prevents |
Note that an EEPROM write operation will block all software programming to Flash. |
||||||||||||||||
Writing to SPMCR |
Reading the Fuse and Lock bits from software will also be prevented during the |
||||||||||||||||
|
EEPROM write operation. It is recommended that the user check the status bit (EEWE) |
||||||||||||||||
|
in the EECR register and verify that the bit is cleared before writing to the SPMCR |
||||||||||||||||
|
register. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 ATmega161(L)
1228B–09/01
ATmega161(L)
It is possible to read both the Fuse and Lock bits from software. To read the Lock bits, load the Z-pointer with $0001 and set the BLBSET and SPMEN bits in SPMCR. If an LPM instruction is executed within three CPU cycles after the BLBSET and SPMEN bits are set in SPMCR, the Lock bits will be written to the destination register. The BLBSET and SPMEN bits will auto-clear upon completion of reading the Lock bits or if no LPM/SPM instruction is executed within three/four CPU cycles. When BLBSET and SPMEN are cleared, LPM will work as described in “Constant Addressing Using the LPM Instruction” on page 16 and in the Instruction Set manual.
Bit |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
|
|
– |
– |
BLB12 |
BLB11 |
BLB02 |
BLB01 |
LB2 |
LB1 |
R0/Rd |
|
|
|
|
|
|
|
|
|
|
The algorithm for reading the Fuse bits is similar to the one described above for reading the Lock bits. But when reading the Fuse bits, load $0000 in the Z-pointer. When an LPM instruction is executed within three cycles after the BLBSET and SPMEN bits are set in the SPMCR, the Fuse bits can be read in the destination register as shown below.
Bit |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
|
|
– |
BOOTRST |
SPIEN |
BODLEVEL |
BODEN |
CKSEL[2] |
CKSEL[1] |
CKSEL[0] |
R0/Rd |
|
|
|
|
|
|
|
|
|
|
Fuse and Lock bits that are programmed will be read as zero.
113
1228B–09/01
Program Memory
Lock Bits
The ATmega161 MCU provides six Lock bits that can be left unprogrammed (“1”) or can be programmed (“0”) to obtain the additional features listed in Table 40. The Lock bits can only be erased to “1” with the Chip Erase command.
Table 40. Lock Bit Protection Modes (1)
Memory Lock Bits |
|
||
|
|
|
|
LB Mode |
LB1 |
LB2 |
Protection Type |
|
|
|
|
1 |
1 |
1 |
No memory lock features enabled |
|
|
|
|
2 |
0 |
1 |
Further programming of the Flash and EEPROM is |
|
|
|
disabled in parallel and serial programming modes. The |
|
|
|
Fuse bits are locked in both serial and parallel |
|
|
|
programming modes.(1) |
3 |
0 |
0 |
Further programming and verification of the Flash and |
|
|
|
EEPROM is disabled in parallel and serial programming |
|
|
|
modes. The Fuse bits are locked in both serial and parallel |
|
|
|
programming modes.(1) |
BLB0 Mode |
BLB02 |
BLB01 |
|
|
|
|
|
1 |
1 |
1 |
No restrictions for SPM, LPM accessing the Application |
|
|
|
Code section |
|
|
|
|
2 |
1 |
0 |
SPM is not allowed to write to the Application Code |
|
|
|
section. |
|
|
|
|
3 |
0 |
0 |
SPM is not allowed to write to the Application Code |
|
|
|
section and LPM executing from Boot Loader section is |
|
|
|
not allowed to read from the Application Code section. |
|
|
|
|
4 |
0 |
1 |
LPM executing from the Boot Loader section is not |
|
|
|
allowed to read from the Application Code section. |
|
|
|
|
BLB1 Mode |
BLB12 |
BLB11 |
|
|
|
|
|
1 |
1 |
1 |
No restrictions for SPM, LPM accessing the Boot Loader |
|
|
|
section |
|
|
|
|
2 |
1 |
0 |
SPM is not allowed to write the Boot Loader section. |
|
|
|
|
3 |
0 |
0 |
SPM is not allowed to write to the Boot Loader section and |
|
|
|
LPM executing from the Application Code section is not |
|
|
|
allowed to read from the Boot Loader section. |
|
|
|
|
4 |
0 |
1 |
LPM executing from the Application Code section is not |
|
|
|
allowed to read from the Boot Loader section. |
|
|
|
|
Note: 1. Program the Fuse bits before programming the Lock bits.
The ATmega161 has seven Fuse bits: BOOTRST, SPIEN, BODLEVEL, BODEN and CKSEL [2:0].
•When BOOTRST is programmed (“0”), the reset vector is set to address $1E00, which is the first address location in the Boot Loader section of the Flash. If the BOOTRST is unprogrammed (“1”), the reset vector is set to address $0000. Default value is unprogrammed (“1”).
•When the SPIEN Fuse is programmed (“0”), Serial Program and Data Downloading is enabled. Default value is programmed (“0”). The SPIEN Fuse is not accessible in serial programming mode.
114 ATmega161(L)
1228B–09/01
ATmega161(L)
Programming the Flash
and EEPROM
•The BODLEVEL Fuse selects the brown-out detection level and changes the startup times. See “Brown-out Detection” on page 28. Default value is unprogrammed (“1”).
•When the BODEN Fuse is programmed (“0”), the Brown-out Detector is enabled. See “Brown-out Detection” on page 28. Default value is unprogrammed (“1”).
•CKSEL2..0: See Table 4, “Reset Delay Selections(4),” on page 26, for which combination of CKSEL2..0 to use. Default value is “010”.
The status of the Fuse bits is not affected by Chip Erase. Note that the Fuse bits are locked if Lock bit1 (LB1) or Lock bit2 (LB2) is programmed. Program the Fuse bits before programming the Lock bits.
All Atmel microcontrollers have a 3-byte signature code that identifies the device. This code can be read in both serial and parallel modes. The three bytes reside in a separate address space, and for the ATmega161 they are:
1.$000: $1E (indicates manufactured by Atmel)
2.$001: $94 (indicates 16 KB Flash memory)
3.$002: $01 (indicates ATmega161 device when $001 is $94)
Atmel’s ATmega161 offers 16K bytes of In-System Reprogrammable Flash program memory and 512 bytes of EEPROM data memory.
The ATmega161 is normally shipped with the On-chip Flash program and EEPROM data memory arrays in the erased state (i.e., contents = $FF) and ready to be programmed. This device supports a high-voltage (12V) parallel programming mode and a low-voltage serial programming mode. The +12V is used for programming enable only, and no current of significance is drawn by this pin. The serial programming mode provides a convenient way to download the program and data into the ATmega161 inside the user’s system.
The program memory array on the ATmega161 is organized as 128 pages of 128 bytes each. When programming the Flash, the program data is latched into a page buffer. This allows one page of program data to be programmed simultaneously in either programming mode.
The EEPROM data memory array on the ATmega161 is programmed byte-by-byte in either programming mode. An auto-erase cycle is provided with the self-timed EEPROM programming operation in the serial programming mode.
During programming, the supply voltage must be in accordance with Table 41.
Table 41. Supply Voltage during Programming
Part |
Serial Programming |
Parallel Programming |
|
|
|
ATmega161L |
2.7 - 5.5V |
4.5 - 5.5V |
|
|
|
ATmega161 |
4.0 - 5.5V |
4.5 - 5.5V |
|
|
|
This section describes how to parallel program and verify Flash program memory, EEPROM data memory, Lock bits and Fuse bits in the ATmega161. Pulses are assumed to be at least 500 ns unless otherwise noted.
Signal Names |
In this section, some pins of the ATmega161 are referenced by signal names describing |
||||
|
their functionality during parallel programming (see Figure 76 and Table 42). Pins not |
||||
|
described in the following table are referenced by pin name. |
||||
|
|
|
|
|
|
|
|
|
|
|
115 |
1228B–09/01 |
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|