3.4.2.2 Non-IRT Regions (OTP and firmware): Code-Protect

Code-Protect is a device locking option enabled by the CP (FCP[0]) bit in the User Configuration A (UCA) configuration area. When activated, the Code-Protect feature:

  • Disables debug
  • Disables Configuration Word override in ICSP Programming mode
  • Disables UCA write permissions in ICSP programming mode
  • Restricts access to Program Memory in ICSP Programming mode
  • Restricts ECC error reporting for the program memory in the ICSP Programming mode

If the CRC (FCP[1]) bit is set, Code-Protect optionally allows a CRC calculation over user-program memory, though Flash protection regions may further restrict CRC access. Code-Protect is disabled after a chip erase, which allows the device to be reprogrammed without exposing the original Flash contents; however, permanent OTP and IRT regions are not erased.

Enabling the Code-Protection Feature and Disabling the CRC Access in ICSP Mode via the Configuration Bits

// Enable Code-Protect (CP) and CRC in the UCA configuration area
#pragma config FCP_CP = ON   // Enable Code-Protect (FCP[0])
#pragma config FCP_CRC = OFF // Disable CRC access in ICSP mode (FCP[1])

The Code-Protect bit is in the UCA configuration bit section. When enabling the Code-Protect feature, the UCA is blocked from ICSP page erase/write operations, but code executing on the chip can still issue write/erase commands as allowed by the regions (RTSP). The write-protect UCA configuration bits, WPUCA (FCP[ 3:2]), should be set to prevent self-writing code from modifying the code-protection setting, if required.

Code protection is always disabled on a chip erase, even if the UCA write protections are enabled. On a chip erase, the program memory is erased before the UCA is erased which disables code protection. The ICSP Write Inhibit feature can be used to disable all ICSP write/erase operations, including chip erase.