- static void eraseUserID(uint32_t addr)
- {
- uint8_t GIEBitValue = INTCONbits.GIE; // Save interrupt enable
- TBLPTRU = (uint8_t) ((addr >> 16) & 0xff);
- TBLPTRH = (uint8_t) ((addr >> 8) & 0xff);
- TBLPTRL = (uint8_t) (addr & 0xff);
-
- EECON1bits.FREE = 1;
-
- EECON1bits.EEPGD = 1;
- EECON1bits.CFGS = 0;
- EECON1bits.WREN = 1;
-
- INTCONbits.GIE = 0; // Disable interrupts
- EECON2 = 0x55;
- EECON2 = 0xAA;
- EECON1bits.WR = 1; // Erase like the wind!
- INTCONbits.GIE = GIEBitValue; // Restore interrupt enable
- EECON1bits.WREN = 0; // Disable writes to memory
- }
- static void writeUserID(uint8_t *flashWrBufPtr)
- {
- int i;
- uint8_t GIEBitValue = INTCONbits.GIE; // Save interrupt enable
- TBLPTRU = (uint8_t) 0x20;
- TBLPTRH = (uint8_t) 0;
- TBLPTRL = (uint8_t) 0;
-
- // Write 3 bytes which uses 6 LOC cells (1 nibble each).
- TABLAT = (flashWrBufPtr[0] >> 4);
- asm("TBLWT*+");
- TABLAT = (flashWrBufPtr[0] & 0x0f);
- asm("TBLWT*+");
- TABLAT = (flashWrBufPtr[1] >> 4);
- asm("TBLWT*+");
- TABLAT = (flashWrBufPtr[1] & 0x0f);
- asm("TBLWT*+");
- TABLAT = (flashWrBufPtr[2] >> 4);
- asm("TBLWT*+");
- TABLAT = (flashWrBufPtr[2] & 0x0f);
- asm("TBLWT*");
-
- TBLPTRU = (uint8_t) 0x20;
- TBLPTRH = (uint8_t) 0;
- TBLPTRL = (uint8_t) 0;
-
- EECON1bits.EEPGD = 1;
- EECON1bits.CFGS = 0;
- EECON1bits.WREN = 1;
-
- INTCONbits.GIE = 0; // Disable interrupts
- EECON2 = 0x55;
- EECON2 = 0xAA;
- EECON1bits.WR = 1; // Write like the wind!
- INTCONbits.GIE = GIEBitValue; // Restore interrupt enable
- EECON1bits.WREN = 0; // Disable writes to memory
- }
|