- •Features
- •Overview
- •Block Diagram
- •Pin Descriptions
- •Port A (PA7..PA0)
- •Port B (PB7..PB0)
- •Port C (PC7..PC0)
- •Port D (PD7..PD0)
- •Port E (PE7..PE0)
- •Port F (PF7..PF0)
- •Port G (PG4..PG0)
- •RESET
- •XTAL1
- •XTAL2
- •AVCC
- •AREF
- •Resources
- •Data Retention
- •Capacitive touch sensing
- •AVR CPU Core
- •Introduction
- •Status Register
- •Stack Pointer
- •I/O Memory
- •Overview
- •Timing
- •Using all Locations of External Memory Smaller than 64 Kbyte
- •Clock Systems and their Distribution
- •CPU Clock – clkCPU
- •I/O Clock – clkI/O
- •Flash Clock – clkFLASH
- •ADC Clock – clkADC
- •Clock Sources
- •Crystal Oscillator
- •External Clock
- •Idle Mode
- •Power-down Mode
- •Power-save Mode
- •Standby Mode
- •Analog Comparator
- •Brown-out Detector
- •Watchdog Timer
- •Port Pins
- •Resetting the AVR
- •Reset Sources
- •Power-on Reset
- •External Reset
- •Watchdog Reset
- •Watchdog Timer
- •Timed Sequences for Changing the Configuration of the Watchdog Timer
- •Safety Level 0
- •Safety Level 1
- •Safety Level 2
- •Interrupts
- •I/O Ports
- •Introduction
- •Configuring the Pin
- •Reading the Pin Value
- •Unconnected pins
- •Alternate Port Functions
- •Register Description for I/O Ports
- •8-bit Timer/Counter0 with PWM and Asynchronous Operation
- •Overview
- •Registers
- •Definitions
- •Counter Unit
- •Normal Mode
- •Fast PWM Mode
- •8-bit Timer/Counter Register Description
- •Overview
- •Registers
- •Definitions
- •Compatibility
- •Counter Unit
- •Input Capture Unit
- •Noise Canceler
- •Force Output Compare
- •Normal Mode
- •Fast PWM Mode
- •16-bit Timer/Counter Register Description
- •Internal Clock Source
- •Prescaler Reset
- •External Clock Source
- •8-bit Timer/Counter2 with PWM
- •Overview
- •Registers
- •Definitions
- •Counter Unit
- •Normal Mode
- •Fast PWM Mode
- •8-bit Timer/Counter Register Description
- •Output Compare Modulator (OCM1C2)
- •Overview
- •Description
- •Timing Example
- •Slave Mode
- •Master Mode
- •Data Modes
- •USART
- •Dual USART
- •Overview
- •AVR USART vs. AVR UART – Compatibility
- •Clock Generation
- •External Clock
- •Frame Formats
- •Parity Bit Calculation
- •Parity Generator
- •Receiver Error Flags
- •Parity Checker
- •Disabling the Receiver
- •Using MPCM
- •Features
- •TWI Terminology
- •Transferring Bits
- •Address Packet Format
- •Data Packet Format
- •Overview of the TWI Module
- •Scl and SDA Pins
- •Bus Interface Unit
- •Address Match Unit
- •Control Unit
- •Using the TWI
- •Master Receiver Mode
- •Slave Receiver Mode
- •Miscellaneous States
- •Analog Comparator
- •Analog to Digital Converter
- •Features
- •Operation
- •Changing Channel or Reference Selection
- •ADC Input Channels
- •Analog Input Circuitry
- •Features
- •Overview
- •TAP Controller
- •PRIVATE0; $8
- •PRIVATE1; $9
- •PRIVATE2; $A
- •PRIVATE3; $B
- •Bibliography
- •Features
- •System Overview
- •Data Registers
- •Bypass Register
- •Reset Register
- •EXTEST; $0
- •IDCODE; $1
- •AVR_RESET; $C
- •BYPASS; $F
- •Scanning the ADC
- •ATmega128 Boundary-scan Order
- •Application Section
- •Programming Time for Flash when Using SPM
- •Simple Assembly Code Example for a Boot Loader
- •Fuse Bits
- •Latching of Fuses
- •Signature Bytes
- •Calibration Byte
- •Signal Names
- •Chip Erase
- •Reading the Flash
- •Reading the EEPROM
- •Data Polling Flash
- •Data Polling EEPROM
- •AVR_RESET ($C)
- •PROG_ENABLE ($4)
- •Data Registers
- •Reset Register
- •Programming Enable Register
- •Programming Command Register
- •Virtual Flash Page Read Register
- •Performing Chip Erase
- •Reading the Flash
- •Reading the EEPROM
- •Electrical Characteristics
- •Absolute Maximum Ratings*
- •DC Characteristics
- •Speed Grades
- •External Clock Drive Waveforms
- •External Clock Drive
- •Two-wire Serial Interface Characteristics
- •ADC Characteristics
- •External Data Memory Timing
- •Idle Supply Current
- •Pin Pull-up
- •Pin Driver Strength
- •Register Summary
- •Instruction Set Summary
- •Ordering Information
- •Packaging Information
- •Errata
- •ATmega128 Rev. F to M
ATmega128
Preventing Flash
Corruption
Programming Time for Flash when Using SPM
Simple Assembly Code Example for a Boot Loader
Fuse and Lock bits that are programmed, will be read as zero. Fuse and Lock bits that are unprogrammed, will be read as one.
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.
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 volt-
age 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.
The calibrated RC Oscillator is used to time Flash accesses. Table 111 shows the typical programming time for Flash accesses from the CPU.
Table 111. SPM Programming Time.
Symbol |
Min Programming Time |
Max Programming Time |
|
|
|
|
|
Flash write (page erase, page write, |
3.7 ms |
4.5 ms |
|
and write lock bits by SPM) |
|||
|
|
||
|
|
|
;-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), spmcsrval (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 |
spmcsrval, (1<<PGERS) | (1<<SPMEN) |
|
call |
Do_spm |
|
; re-enable the RWW section |
|
|
ldi |
spmcsrval, (1<<RWWSRE) | (1<<SPMEN) |
|
call |
Do_spm |
|
|
|
282 |
2467X–AVR–06/11
ATmega128
; 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 |
spmcsrval, (1<<SPMEN) |
|
call |
Do_spm |
|
adiw |
ZH:ZL, 2 |
|
sbiw |
loophi:looplo, 2 |
;use subi for PAGESIZEB<=256 |
brne |
Wrloop |
|
; execute page write |
|
|
subi |
ZL, low(PAGESIZEB) |
;restore pointer |
sbci |
ZH, high(PAGESIZEB) |
;not required for PAGESIZEB<=256 |
ldi |
spmcsrval, (1<<PGWRT) | (1<<SPMEN) |
|
call |
Do_spm |
|
; re-enable the RWW section |
|
|
ldi |
spmcsrval, (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: |
|
|
lpm |
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:
lds temp1, SPMCSR
sbrs temp1, RWWSB |
; If RWWSB is set, the RWW section is not ready |
yet |
|
ret |
|
; re-enable the RWW section
ldi spmcsrval, (1<<RWWSRE) | (1<<SPMEN) call Do_spm
rjmp Return
Do_spm:
;check for previous SPM complete Wait_spm:
lds temp1, SPMCSR sbrc temp1, SPMEN rjmp Wait_spm
;input: spmcsrval determines SPM action
;disable interrupts if enabled, store status in temp2, SREG
cli
;check that no EEPROM write access is present
283
2467X–AVR–06/11
ATmega128
Wait_ee:
sbic EECR, EEWE rjmp Wait_ee
;SPM timed sequence sts SPMCSR, spmcsrval spm
;restore SREG (to enable interrupts if originally enabled) out SREG, temp2
ret
ATmega128 Boot |
In Table 112 through Table 114, the parameters used in the description of the self programming |
|||||||||||
Loader Parameters |
are given. |
|
|
|
|
|
|
|
|
|
|
|
|
Table 112. Boot Size Configuration |
|
|
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Boot |
|
|
|
|
|
|
|
|
|
|
|
|
Reset |
|
|
|
|
|
|
|
|
|
Boot |
|
|
Address |
|
|
|
|
|
|
|
|
Application |
Loader |
|
End |
(start Boot |
|
|
|
|
|
Boot |
|
|
Flash |
Flash |
|
Application |
Loader |
|
|
|
BOOTSZ1 |
BOOTSZ0 |
Size |
|
Pages |
Section |
Section |
|
section |
Section) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
1 |
512 |
|
4 |
$0000 - |
$FE00 - |
|
$FDFF |
$FE00 |
|
|
|
words |
|
$FDFF |
$FFFF |
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
0 |
1024 |
|
8 |
$0000 - |
$FC00 - |
|
$FBFF |
$FC00 |
|
|
|
words |
|
$FBFF |
$FFFF |
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
0 |
1 |
2048 |
|
16 |
$0000 - |
$F800 - |
|
$F7FF |
$F800 |
|
|
|
words |
|
$F7FF |
$FFFF |
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
0 |
0 |
4096 |
|
32 |
$0000 - |
$F000 - |
|
$EFFF |
$F000 |
|
|
|
words |
|
$EFFF |
$FFFF |
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Note: The different BOOTSZ fuse configurations are shown in Figure 133 |
|
|
|
||||||||
|
Table 113. |
Read-While-Write Limit(1) |
|
|
|
|
|
|
|
|||
|
Section |
|
|
|
|
|
Pages |
|
Address |
|
|
|
|
|
|
|
|
|
|
|
|||||
|
Read-While-Write section (RWW) |
|
|
480 |
|
$0000 - $EFFF |
|
|
||||
|
|
|
|
|
|
|
||||||
|
No Read-While-Write section (NRWW) |
|
32 |
|
$F000 - $FFFF |
|
|
|||||
|
|
|
|
|
|
|
|
|
|
|
|
|
Note: 1. For details about these two section, see “No Read-While-Write Section – NRWW” on page 274 and “Read-While-Write Section – RWW” on page 274
284
2467X–AVR–06/11
ATmega128
Table 114. Explanation of Different Variables Used in Figure 134 and the Mapping to the Z- Pointer(3)
|
|
Corresponding |
Description(2) |
Variable |
|
Z-value |
|
PCMSB |
15 |
|
Most significant bit in the program counter. (The |
|
|
program counter is 16 bits PC[15:0]) |
|
|
|
|
|
|
|
|
|
|
6 |
|
Most significant bit which is used to address the |
PAGEMSB |
|
|
words within one page (128 words in a page |
|
|
|
requires 7 bits PC [6:0]). |
|
|
|
|
|
|
Z16(1) |
Bit in Z-register that is mapped to PCMSB. |
ZPCMSB |
|
|
Because Z0 is not used, the ZPCMSB equals |
|
|
|
PCMSB + 1. |
|
|
|
|
|
|
Z7 |
Bit in Z-register that is mapped to PAGEMSB. |
ZPAGEMSB |
|
|
Because Z0 is not used, the ZPAGEMSB |
|
|
|
equals PAGEMSB + 1. |
|
|
|
|
PCPAGE |
PC[15:7] |
Z16(1):Z8 |
Program counter page address: Page select, for |
|
|
page erase and page write |
|
|
|
|
|
|
|
|
|
|
PC[6:0] |
Z7:Z1 |
Program counter word address: Word select, for |
PCWORD |
|
|
filling temporary buffer (must be zero during |
|
|
|
page write operation) |
|
|
|
|
Notes: 1. The Z-register is only 16 bits wide. Bit 16 is located in the RAMPZ register in the I/O map.
2.Z0: should be zero for all SPM commands, byte select for the (E)LPM instruction.
3.See “Addressing the Flash During Self-Programming” on page 278 for details about the use of Z-pointer during self-programming.
285
2467X–AVR–06/11
ATmega128
Memory
Programming
Program and Data
Memory Lock Bits
The Atmel® AVR®ATmega128 provides six Lock bits which can be left unprogrammed (“1”) or can be programmed (“0”) to obtain the additional features listed in Table 116. The Lock bits can only be erased to “1” with the Chip Erase command.
Table 115. Lock Bit Byte
Lock Bit Byte |
Bit No. |
Description |
Default Value |
|
|
|
|
|
7 |
– |
1 (unprogrammed) |
|
|
|
|
|
6 |
– |
1 (unprogrammed) |
|
|
|
|
BLB12 |
5 |
Boot lock bit |
1 (unprogrammed) |
|
|
|
|
BLB11 |
4 |
Boot lock bit |
1 (unprogrammed) |
|
|
|
|
BLB02 |
3 |
Boot lock bit |
1 (unprogrammed) |
|
|
|
|
BLB01 |
2 |
Boot lock bit |
1 (unprogrammed) |
|
|
|
|
LB2 |
1 |
Lock bit |
1 (unprogrammed) |
|
|
|
|
LB1 |
0 |
Lock bit |
1 (unprogrammed) |
|
|
|
|
Note: “1” means unprogrammed, “0´means programmed
Table 116. Lock Bit Protection Modes
Memory Lock Bits |
Protection Type |
|||
|
|
|
|
|
LB mode |
LB2 |
LB1 |
|
|
|
|
|
|
|
1 |
1 |
1 |
No memory lock features enabled. |
|
|
|
|
|
|
|
|
|
Further programming of the Flash and EEPROM is |
|
2 |
1 |
0 |
disabled in Parallel and SPI/JTAG Serial Programming |
|
mode. The Fuse bits are locked in both Serial and Parallel |
||||
|
|
|
||
|
|
|
Programming mode.(1) |
|
|
|
|
Further programming and verification of the Flash and |
|
3 |
0 |
0 |
EEPROM is disabled in Parallel and SPI/JTAG Serial |
|
Programming mode. The Fuse bits are locked in both |
||||
|
|
|
||
|
|
|
Serial and Parallel Programming mode.(1) |
|
BLB0 mode |
BLB02 |
BLB01 |
|
|
|
|
|
|
|
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 the Boot Loader section is not |
|
3 |
0 |
0 |
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 |
|
4 |
0 |
1 |
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. |
|
|
|
|
|
|
BLB1 mode |
BLB12 |
BLB11 |
|
|
|
|
|
|
286
2467X–AVR–06/11