dsPIC33 PIC24 FRM, Flash Programming DS70000609E
dsPIC33 PIC24 FRM, Flash Programming DS70000609E
HIGHLIGHTS
This section of the manual contains the following major topics:
Note: This family reference manual section is meant to serve as a complement to device
data sheets. Depending on the device variant, this manual section may not apply to
all dsPIC33/PIC24 devices.
Please consult the note at the beginning of the “Flash Program Memory” chapter
in the current device data sheet to check whether this document supports the
device you are using.
Device data sheets and family reference manual sections are available for
download from the Microchip Worldwide Website at: http://www.microchip.com
1.0 INTRODUCTION
This section describes the technique for programming Flash program memory. The dsPIC33/
PIC24 families of devices have an internal programmable Flash program memory for execution
of user code. There are up to three methods to program this memory:
• Run-Time Self-Programming (RTSP)
• In-Circuit Serial Programming™ (ICSP™)
• Enhanced In-Circuit Serial Programming (EICSP)
RTSP is performed by the application software during execution, while ICSP and EICSP are
performed from an external programmer using a serial data connection to the device. ICSP and
EICSP allow much faster programming time than RTSP. RTSP techniques are described in
Section 4.0 “Run-Time Self-Programming (RTSP)”. The ICSP and EICSP protocols are
defined in the Programming Specification documents for the respective devices, which can be
downloaded from the Microchip website (http://www.microchip.com).
When programming in the C language, several built-in functions are available that facilitate Flash
programming. See the “MPLAB® XC16 C Compiler User’s Guide” (DS50002071) for details
regarding built-in functions.
24 Bits
Using
Program 0 Program Counter 1
Counter
Working Reg EA
Using
Table 1/0 TBLPAG Reg
Instruction
8 Bits 16 Bits
User/Configuration Byte
Space Select 24-Bit EA Select
varWord1 = __builtin_tblrdl(addrOffset);
varWord2 = __builtin_tblrdh(addrOffset);
Note: The number of write latches varies by device. Refer to the “Flash Program
Memory” chapter of the specific device data sheet for the number of available write
latches.
Refer to Section 4.2 “Flash Programming Operations” for more programming examples.
Note 1: This bit can only be reset (i.e., cleared) on a Power-on Reset (POR).
2: When exiting Idle mode, there is a power-up delay (TVREG) before Flash program memory becomes
operational. Refer to the “Electrical Characteristics” chapter of the specific device data sheet for more
information.
3: All other combinations of NVMOP<3:0> are unimplemented.
4: This functionality is not available on all devices. Refer to the “Flash Program Memory” chapter in the
specific device data sheet for available operations.
5: Entry into a power-saving mode after executing a PWRSAV instruction is contingent on completion of all
pending NVM operations.
6: These bit are only available on devices that support RAM buffered row programming. Refer to the
device-specific data sheet for availability.
Note 1: This bit can only be reset (i.e., cleared) on a Power-on Reset (POR).
2: When exiting Idle mode, there is a power-up delay (TVREG) before Flash program memory becomes
operational. Refer to the “Electrical Characteristics” chapter of the specific device data sheet for more
information.
3: All other combinations of NVMOP<3:0> are unimplemented.
4: This functionality is not available on all devices. Refer to the “Flash Program Memory” chapter in the
specific device data sheet for available operations.
5: Entry into a power-saving mode after executing a PWRSAV instruction is contingent on completion of all
pending NVM operations.
6: These bit are only available on devices that support RAM buffered row programming. Refer to the
device-specific data sheet for availability.
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
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
Note: The NVM Address register should always point to a double instruction word boundary when performing a
double instruction word programming operation, a row boundary when performing a row programming
operation or a page boundary when performing a page erase operation.
Note: Refer to Section 3.2 “NVMKEY Register” for NVMKEY register operation.
Note: It is recommended that the TBLPAG register be saved prior to modification and
restored after use.
CAUTION
On some devices, the Configuration bits are stored in the last page of program Flash user
memory space in a section called “Flash Configuration Bytes”. With these devices, performing
a page erase operation on the last page of program memory erases the Flash Configuration
bytes, which enables code protection. Therefore, users should not perform page erase
operations on the last page of program memory. This is not a concern when the Configuration
bits are stored in Configuration memory space in a section called “Device Configuration
Registers”. Refer to the Program Memory Map in the “Memory Organization” chapter of the
specific device data sheet to determine where Configuration bits are located.
Note 1: If a POR or BOR event occurs while an RTSP erase or programming operation is
in progress, the RTSP operation is aborted immediately. The user should execute
the RTSP operation again after the device comes out of Reset.
2: If an EXTR, SWR, WDTO, TRAPR, CM or IOPUWR Reset event occurs while an
RTSP erase or programming operation is in progress, the device will be reset only
after the RTSP operation is complete.
Note 1: The user should remember that the minimum amount of Flash program memory
that can be erased using RTSP is a singe erased page. 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.
3: On devices with Configuration bytes stored in the last page of Flash, performing a
page erase operation on the last page of program memory clears the Configuration
bytes, which enables code protection. On these devices, the last page of Flash
memory should not be erased.
Note 1: The code for Load_Write_Latch_Row is shown in Example 4-3 and the code for
Load_Write_Latch_Word is shown in Example 4-4. The code in both of these
examples is referred to in subsequent examples.
2: Refer to the specific device data sheet for the number of latches.
loop:
TBLWTL.b [W2++], [W1++]
TBLWTL.b [W2++], [W1--]
TBLWTH.b [W2++], [W1]
INC2 W1, W1
DEC W3, W3
BRA NZ, loop
Note: Not all devices have row programming capability. Refer to the “Flash Program
Memory” chapter of the specific device data sheet to determine if this option is
available.
Two NOP instructions should be inserted in the code after the programming cycle.
Figure 4-1: Uncompressed and Compressed Storage Formats for Program Data
15 7 0 15 7 0
LSW1 LSW1
00h MSB1 MSB2 MSB1
Address
LSW2 LSW2
00h MSB2
Example 4-7: Writing Program Memory from a Data RAM Buffer (in C)
int data[128]; // Data to be programmed in RAM
int targetWriteAddressL; // bits<15:0>
int targetWriteAddressH; // bits<22:16>
Note 1: Writing to device Configuration registers is not available in all devices. Refer to the
“Special Features” chapter in the specific device data sheet to determine the modes
that are available according to the device-specific NVMOP<3:0> bits definition.
2: While performing RTSP on device Configuration registers, the device must be
operating using the internal FRC Oscillator (without PLL). If the device is operating
from a different clock source, a clock switch to the internal FRC Oscillator
(NOSC<2:0> = 000) must be performed prior to performing RTSP operation in the
device Configuration registers.
3: If the Primary Oscillator Mode Select bits (POSCMD<1:0>) in the Oscillator
Configuration register (FOSC) are being reprogrammed to a new value, the user
must ensure that the Clock Switching Mode bits (FCKSM<1:0>) in the FOSC
register have an initial programmed value of ‘0’, prior to performing this RTSP
operation.
DS70000609E-page 20
NVMCON WR WREN WRERR NVMSIDL — — RPDF URERR — — — — NVMOP<3:0> 0000
NVMADRU — — — — — — — — NVMADRU<7:0> 0000
NVMADR NVMADR<15:0> 0000
NVMKEY — — — — — — — — NVMKEY<7:0> 0000
NVMSRCADR(1) NVMSRCADR<15:0> 0000
Legend: x = unknown value on Reset; — = unimplemented, read as ‘0’. Reset values are shown in hexadecimal.
Note 1: Not available for all devices. Refer to the specific device data sheet for details.
dsPIC33/PIC24 Family Reference Manual
Note: Please visit the Microchip website (www.microchip.com) for additional Application
Notes and code examples for the dsPIC33/PIC24 families of devices.
• Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
• There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
• Microchip is willing to work with the customer who is concerned about the integrity of their code.
• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.