Section 5. FLASH and EEPROM Programming: Highlights
Section 5. FLASH and EEPROM Programming: Highlights
HIGHLIGHTS
This section of the manual contains the following topics:
5
FLASH and EEPROM
Programming
5.1 Introduction
This section describes programming techniques for FLASH program memory and data EEPROM
memory. The dsPIC30F family of devices contains internal program FLASH memory for
executing user code. There are two methods by which the user can program this memory:
1. Run-Time Self Programming (RTSP)
2. In-Circuit Serial Programming™ (ICSP™)
RTSP is performed by the user’s software. ICSP is performed using a serial data connection to
the device and allows much faster programming times than RTSP. RTSP techniques are
described in this chapter. The ICSP protocol is described in the dsPIC30F Programming
Specification document, which may be downloaded from the Microchip web site.
The data EEPROM is mapped into the program memory space. The EEPROM is organized as
16-bit wide memory and the memory size can be up to 2K words (4 Kbytes). The amount of
EEPROM is device dependent. Refer to the device data sheet for further information.
The programming techniques used for the data EEPROM are similar to those used for FLASH
program memory RTSP. The key difference between FLASH and data EEPROM programming
operations is the amount of data that can be programmed or erased during each program/erase
cycle.
5.2 Table Instruction Operation
The table instructions provide one method of transferring data between the program memory
space and the data memory space of dsPIC30F devices. A summary of the table instructions is
provided here since they are used during programming of the FLASH program memory and data
EEPROM. There are four basic table instructions:
• TBLRDL: Table Read Low
• TBLRDH: Table Read High
• TBLWTL: Table Write Low
• TBLWTH: Table Write High
The TBLRDL and the TBLWTL instructions are used to read and write to bits <15:0> of program
memory space. TBLRDL and TBLWTL can access program memory in Word or Byte mode.
The TBLRDH and TBLWTH instructions are used to read or write to bits <23:16> of program
memory space. TBLRDH and TBLWTH can access program memory in Word or Byte mode. Since
the program memory is only 24-bits wide, the TBLRDH and TBLWTH instructions have the ability
to address an upper byte of program memory that does not exist. This byte is called the ‘phantom
byte’. Any read of the phantom byte will return 0x00 and a write to the phantom byte has no
effect.
Always remember that the 24-bit program memory can be regarded as two side-by-side 16-bit
spaces, with each space sharing the same address range. Therefore, the TBLRDL and TBLWTL
instructions access the ‘low’ program memory space (PM<15:0>). The TBLRDH and TBLWTH
instructions access the ‘high’ program memory space (PM<31:16>). Any reads or writes to
PM<31:24> will access the phantom (unimplemented) byte. When any of the table instructions
are used in Byte mode, the LSb of the table address will be used as the byte select bit. The LSb
determines which byte in the high or low program memory space is accessed.
Figure 5-1 shows how the program memory is addressed using the table instructions. A 24-bit
program memory address is formed using bits <7:0> of the TBLPAG register and the effective
address (EA) from a W register, specified in the table instruction. The 24-bit program counter is
shown in Figure 5-1 for reference. The upper 23 bits of the EA are used to select the program
memory location. For the Byte mode table instructions, the LSb of the W register EA is used to
pick which byte of the 16-bit program memory word is addressed. A ‘1’ selects bits <15:8>, a ‘0’
selects bits <7:0>. The LSb of the W register EA is ignored for a table instruction in Word mode.
In addition to the program memory address, the table instruction also specifies a W register (or
a W register pointer to a memory location) that is the source of the program memory data to be
written, or the destination for a program memory read. For a table write operation in Byte mode,
bits <15:8> of the source working register are ignored.
7 0 15 0
TBLPAG EA
24-bit EA
The following code example shows how to read a word of program memory using the table
instructions in Word mode:
; Setup the address pointer to program space
MOV #tblpage(PROG_ADDR),W0 ; get table page value
MOV W0,TBLPAG ; load TBLPAG register
MOV #tbloffset(PROG_ADDR),W0 ; load address LS word
; Read the program memory location
TBLRDH [W0],W3 ; Read high byte to W3
TBLRDL [W0],W4 ; Read low word to W4
In the code example above, the post-increment operator on the read of the low byte causes the
address in the working register to increment by one. This sets EA<0> to a ‘1’ for access to the
middle byte in the third write instruction. The last post-increment sets W0 back to an even
address, pointing to the next program memory location.
Note: The tblpage() and tbloffset() directives are provided by the Microchip
assembler for the dsPIC30F. These directives select the appropriate TBLPAG and
W register values for the table instruction from a program memory address value.
Refer to the Microchip software tools documentation for further details. 5
FLASH and EEPROM
Programming
Table write instructions do not write directly to the non-volatile program and data memory.
Instead, the table write instructions load holding latches that store the write data. The holding
latches are not memory mapped and can only be accessed using table write instructions. When
all of the holding latches have been loaded, the actual memory programming operation is started
by executing a special sequence of instructions.
The number of holding latches will determine the maximum memory block size that can be
programmed and may vary depending on the type of non-volatile memory and the device variant.
For example, the number of holding latches could be different for program memory, data
EEPROM memory and Device Configuration registers for a given device.
In general, the program memory is segmented into rows and panels. Each panel will have its own
set of table write holding latches. This allows multiple memory panels to be programmed at once,
reducing the overall programming time for the device. For each memory panel, there are
generally enough holding latches to program one row of memory at a time. The memory logic
automatically decides which set of write latches to load based on the address value used in the
table write instruction.
Please refer to the specific device data sheet for further details.
The following sequence can be used to write a single program memory latch location in Word
mode:
; Setup the address pointer to program space
MOV #tblpage(PROG_ADDR),W0 ; get table page value
MOV W0,TBLPAG ; load TBLPAG register
MOV #tbloffset(PROG_ADDR),W0 ; load address LS word
; Load write data into W registers
MOV #PROG_LOW_WORD,W2
MOV #PROG_HI_BYTE,W3
; Perform the table writes to load the latch
TBLWTL W2,[W0]
TBLWTH W3,[W0++]
In this example, the contents of the upper byte of W3 does not matter because this data will be
written to the phantom byte location. W0 is post-incremented by 2, after the second TBLWTH
instruction, to prepare for the write to the next program memory location.
To write a single program memory latch location in Byte mode, the following code sequence can
be used:
; Setup the address pointer to program space
MOV #tblpage(PROG_ADDR),W0 ; get table page value
MOV W0,TBLPAG ; load TBLPAG register
MOV #tbloffset(PROG_ADDR),W0 ; load address LS word
; Load data into working registers
MOV #LOW_BYTE,W2
MOV #MID_BYTE,W3
MOV #HIGH_BYTE,W4
; Write data to the latch
TBLWTH.B W4,[W0] ; write high byte
TBLWTL.B W2,[W0++] ; write low byte
TBLWTL.B W3,[W0++] ; write middle byte
In the code example above, the post-increment operator on the write to the low byte causes the
address in W0 to increment by one. This sets EA<0> = 1 for access to the middle byte in the third
write instruction. The last post-increment sets W0 back to an even address pointing to the next
program memory location.
5.3 Control Registers
FLASH and data EEPROM programming operations are controlled using the following
Non-Volatile Memory (NVM) control registers:
• NVMCON: Non-Volatile Memory Control Register
• NVMKEY: Non-Volatile Memory Key Register
• NVMADR: Non-Volatile Memory Address Register
24-bit PM address
EA<0> is
Byte Select
W Register EA
NVMADR Register EA
Using
NVMADR TBLPAG Reg
Addressing
8 bits 16 bits
MOV #0x55,W0
MOV #0xAA,W0
MOV W0,NVMKEY
MOV W0,NVMKEY ; NOP not required
BSET NVMCON,#WR ; Start the program/erase cycle
NOP
NOP
POP SR ; Re-enable interrupts
Refer to Section 5.4.2 “FLASH Programming Operations” for further programming examples.
Lower Byte:
R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0
PROGOP<7:0>
bit 7 bit 0
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
S = Settable bit -n = Value at POR ‘1’ = Bit is set
‘0’ = Bit is cleared x = Bit is unknown
5
FLASH and EEPROM
Programming
Lower Byte:
R/W-x R/W-x R/W-x R/W-x R/W-x R/W-x R/W-x R/W-x
NVMADR<7:0>
bit 7 bit 0
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Lower Byte:
W-0 W-0 W-0 W-0 W-0 W-0 W-0 W-0
NVMKEY<7:0>
bit 7 bit 0
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
5
FLASH and EEPROM
Programming
The user can erase program FLASH memory by rows (32 instruction words). The user can
program FLASH in blocks of 4 instruction words. The general process is as follows:
1. Read one row of program FLASH (32 instruction words) and store into data RAM as a data
“image”. The RAM image must be read from an even 32-word program memory address
boundary.
2. Update the RAM data image with the new program memory data.
3. Erase program FLASH row.
• Setup NVMCON register to erase 1 row of FLASH program memory.
• Write address of row to be erased into NVMADR.
• Disable interrupts.
• Write the key sequence to NVMKEY to enable the erase.
• Set the WR bit. This will begin erase cycle.
• CPU will stall for the duration of the erase cycle.
• The WR bit is cleared when erase cycle ends.
• Re-enable interrupts.
4. Write four instruction words of data from RAM into the FLASH program memory write
latches.
5. Program 4 instruction words into program FLASH.
• Setup NVMCON to program one row of FLASH program memory.
• Disable interrupts.
• Write the key sequence to NVMKEY to enable the program cycle.
• Set the WR bit. This will begin the program cycle.
• CPU will stall for duration of the program cycle.
• The WR bit is cleared by the hardware when program cycle ends.
• Re-enable interrupts.
6. Repeat steps (4-5) seven more times to finish programming the row.
7. Repeat steps 1 through 6, as needed, to program the desired amount of FLASH program
memory.
Note: The user should remember that the minimum amount of program memory that can
be modified using RTSP is 32 instruction word locations. Therefore, it is important
that an image of these locations be stored in general purpose RAM before an erase
cycle is initiated. An erase cycle must be performed on any previously written
locations before any programming is done.
The following is a code sequence that can be used to erase a row (32 instructions) of program
memory. The NVMCON register is configured to erase one row of program memory. The
TBLPAG and NVMADR registers are loaded with the address of the row to be erased. The
program memory must be erased at ‘even’ row boundaries. Therefore, the 6 LSBs of the value
written to the NVMADR register have no effect when a row is erased.
The erase operation is initiated by writing a special unlock or key sequence to the NVMKEY
register before setting the WR control bit (NVMCON<15>). The unlock sequence needs to be
executed in the exact order shown without interruption. Therefore, interrupts should be disabled
prior to writing the sequence.
Two NOP instructions should be inserted in the code at the point where the CPU will resume
operation. Finally, interrupts can be enabled (if required).
; Setup NVMCON to erase one row of Flash program memory
MOV #0x4041,W0
MOV W0,NVMCON
; Setup address pointer to row to be ERASED
MOV #tblpage(PROG_ADDR),W0
MOV W0,TBLPAG
MOV #tbloffset(PROG_ADDR),W0
MOV W0,NVMADR
; Disable interrupts, if enabled
PUSH SR
MOV #0x00E0,W0
IOR SR
; Write the KEY sequence
MOV #0x55,W0
MOV W0,NVMKEY
MOV #0xAA,W0
MOV W0,NVMKEY
; Start the erase operation
BSET NVMCON,#WR
; Insert two NOPs after the erase cycle (required)
NOP
NOP
; Re-enable interrupts, if needed
POP SR
5
FLASH and EEPROM
Programming
The following is a sequence of instructions that can be used to load the 96 bits of write latches
(four instruction words). Four TBLWTL and four TBLWTH instructions are needed to load the write
latches selected by the table pointer.
The TBLPAG register is loaded with the 8 MSbits of the program memory address. The user does
not need to write the NVMADR register for a FLASH programming operation. The 16 LSbits of
the program memory address are automatically captured into the NVMADR register when each
table write instruction is executed. The program memory must be programmed at an ‘even’
4-instruction word address boundary. In effect, the 3 LSbits of the value captured in the NVMADR
register are not used during the programming operation.
The group of four write latches do not necessarily have to be written in sequential order. The
3 LSbits of the table write address determine which of the latches will be written. However, all
four latches should be written for each programming cycle to overwrite old data.
MOV #LOW_WORD_0,W2
MOV #HIGH_BYTE_0,W3
TBLWTL W2,[W0]
TBLWTH W3,[W0++] ; 0th_program_word
MOV #LOW_WORD_1,W2
MOV #HIGH_BYTE_1,W3
TBLWTL W2,[W0]
TBLWTH W3,[W0++] ; 1st_program_word
MOV #LOW_WORD_2,W2
MOV #HIGH_BYTE_2,W3
TBLWTL W2, [W0]
TBLWTH W3, [W0++] ; 2nd_program_word
MOV #LOW_WORD_3,W2
MOV #HIGH_BYTE_3,W3
TBLWTL W2,[W0]
TBLWTH W3,[W0++] ; 3rd_program_word
; The following code segment should be repeated 8 times to program the entire row
; (32 instructions)
1. Write the new configuration value to the table write latch using a TBLWTL instruction.
2. Configure NVMCON for a Configuration register write (NVMCON = 0x4008).
3. Disable interrupts, if enabled.
4. Write the key sequence to NVMKEY.
5. Start the write sequence by setting WR (NVMCON<15>).
6. CPU execution will resume when the write is finished.
7. Re-enable interrupts, if needed.
5
FLASH and EEPROM
Programming
The following code sequence can be used to modify a Device Configuration register:
; Set up a pointer to the location to be written.
MOV #tblpage(CONFIG_ADDR),W0
MOV W0,TBLPAG
MOV #tbloffset(CONFIG_ADDR),W0
; Get the new data to write to the configuration register
MOV #ConfigValue,W1
; Perform the table write to load the write latch
TBLWTL W1,[W0]
; Configure NVMCON for a configuration register write
MOV #0x4008,W0
MOV W0,NVMCON
; Disable interrupts, if enabled
PUSH SR
MOV #0x00E0,W0
IOR SR
; Write the KEY sequence
MOV #0x55,W0
MOV W0,NVMKEY
MOV #0xAA,W0
MOV W0,NVMKEY
; Start the programming sequence
BSET NVMCON,#WR
; Insert two NOPs after programming
NOP
NOP
; Re-enable interrupts, if required
POP SR
Note: Unexpected results will be obtained should the user attempt to read the EEPROM
while a programming or erase operation is underway.
5
FLASH and EEPROM
Programming
5
FLASH and EEPROM
Programming
Note: Sixteen table write instructions have been used in this code segment to provide
clarity in the example. The code segment could be simplified by using a single table
write instruction in a REPEAT loop. 5
FLASH and EEPROM
Programming
Note: Program Space Visibility (PSV) can also be used to read locations in the program
memory address space. See Section 4. “Program Memory” for further information
about PSV.
Question 1: I cannot get the device to program or erase properly. My code appears to
be correct. What could be the cause?
Answer: Interrupts should be disabled when a program or erase cycle is initiated to ensure that
the key sequence executes without interruption. Interrupts can be disabled by raising the current
CPU priority to level 7. The code examples in this chapter disable interrupts by saving the current
SR register value on the stack, then ORing the value 0x00E0 with SR to force IPL<2:0> = 111.
If no priority level 7 interrupts are enabled, then the DISI instruction provides another method to
temporarily disable interrupts, while the key sequence is executed.
Question 2: What is an easy way to read data EEPROM without using table
instructions?
Answer: The data EEPROM is mapped into the program memory space. PSV can be used to
map the EEPROM region into data memory space. See Section 4. “Program Memory” for
further information about PSV.
Note: Please visit the Microchip web site (www.microchip.com) for additional Application
Notes and code examples for the dsPIC30F Family of devices.
5
FLASH and EEPROM
Programming