dsPIC33F FRM Section 5. Flash Programming (DS70191B)
dsPIC33F FRM Section 5. Flash Programming (DS70191B)
Flash Programming
HIGHLIGHTS
This section of the manual contains the following topics:
5
Programming
Flash
5.1 INTRODUCTION
This section describes the technique for programming Flash program memory. The dsPIC33F
family of devices have an internal programmable Flash memory for execution of user code.
There are two methods to program this memory:
• Run-Time Self Programming (RTSP)
• In-Circuit Serial Programming (ICSP)
This section describes RTSP programming, which is performed by the user’s software.
ICSP is performed using a serial data connection to the device and allows for faster programming
than RTSP. The ICSP protocol is defined in the dsPIC33F/PIC24H Flash Programming
Specification (DS70152), which can be downloaded from the Microchip web site.
Working Reg EA
Using
Table 1/0 TBLPAG Reg
Instruction
8 bits 16 bits
User/Configuration Byte
Space Select 24-bit EA Select
Note: The tblpage() and tbloffset() directives are provided by the Microchip
assembler for the dsPIC33F. These directives select the appropriate TBLPAG and
W register values for the table instruction from a program memory address value.
Refer to the MPLAB ASM 30, MPLAB LINK30 and Utilities User’s Guide (DS51317)
for further details.
5
Example 5-2: Read Byte Mode
; Set up the address pointer to program space
Programming
Example 5-3: Writing 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 Example 5-3, the content 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 in preparation for the write to the next program memory location.
Example 5-4: Writing a Single Program Memory Latch Location in Byte 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 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 Example 5-4, 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.
MOV #0x55,W0
MOV W0, NVMKEY
MOV #0xAA,W0
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.
5
Programming
Flash
If ERASE = 0,
1111 = No operation
1110 = Reserved
1101 = No operation
1100 = No operation
1011 = Reserved
0011 = Memory word program operation
0010 = No operation
0001 = Memory row program operation
0000 = Program a single Configuration register byte
5
Programming
Flash
Note 1: The user should remember that the minimum amount of program memory that can
be erased using RTSP is 512 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.
2: A row or word in Flash program memory should not be programmed more than
twice before being erased.
BSET NVMCON,#WR
; Insert two NOPs after the erase cycle (required)
Flash
NOP
NOP
;Re-enable interrupts, if needed
POP SR
Note: The code shown in Example 5-7 is the Load_Write_Latch code that will be
referred to in subsequent examples.
Note 1: See Section 5.4.2.3 “Loading Write Latches” for additional information.
5
Programming
Flash
5
Programming
Flash
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, the DISI instruction provides another method to temporarily disable
interrupts while the key sequence is executed.
Note: Please visit the Microchip web site (www.microchip.com) for additional Application
Notes and code examples for the dsPIC33F family of devices.
5
Programming
Flash