12.1.2 NVM Unlock Sequence

The unlock sequence is a mechanism that protects the NVM from unintended self-write programming or erasing. The sequence must be executed and completed without interruption to successfully complete any of the following operations:

  • PFM Row Erase
  • Write of PFM write latches to PFM memory
  • Write of PFM write latches to User IDs
  • Write to Data EEPROM Memory
  • Write to Configuration Words

The unlock sequence consists of the following steps and must be completed in order:

  • Write 55h to NVMCON2
  • Write AAh to NMVCON2
  • Set the WR bit of NVMCON1

Once the WR bit is set, the processor will stall internal operations until the operation is complete and then resume with the next instruction.

Since the unlock sequence must not be interrupted, the global interrupts must be disabled prior to the unlock sequence and re-enabled after the unlock sequence is completed.

Figure 12-6. NVM Unlock Sequence Flowchart

NVM Unlock Sequence

   BCF     INTCON,GIE      ; Recommended so sequence is not interrupted
   BANKSEL NVMCON1         ; Bank to NVMCON1 register
   BSF     NVMCON1,WREN    ; Enable write/erase
   MOVLW   55h             ; Load 55h
   MOVWF   NVMCON2         ; Step 1: Load 55h into NVMCON2
   MOVLW   AAh             ; Step 2: Load W with AAh
   MOVWF   NVMCON2         ; Step 3: Load AAh into NVMCON2
   BSF     NVMCON1,WR      ; Step 4: Set WR bit to begin write/erase
   BSF     INTCON,GIE      ; Re-enable interrupts
Important:
  1. Sequence begins when NVMCON2 is written; following 4 steps must occur in the cycle-accurate order shown. If the timing of the four steps is corrupted by an interrupt or a debugger Halt, the action will not take place.
  2. Opcodes shown are illustrative; any instruction that has the indicated effect may be used.