This would effect many string and array operations, as well as a bunch of library code.
This has been fixed in the latest silicon, but if you're like me you ended up with the first batch with the bug!
Copy the following code to a file (ie 'K40_errata.bas') and place it in your userlibrary folder.
K40_errata.bas:
Code: Select all
module K40_errata
//
// **note**: include this module first before ANY others since it
// must execute before any other startup code that uses TBLRD
//
// Errata for the K40 family:
// TBLRD requires NVMREG value to point to appropriate memory
//
// The affected silicon revisions of the PIC18FXXK40 devices improperly require the
// NVMREG<1:0> bits in the NVMCON register to be set for TBLRD access of the various
// memory regions.
// The issue is most apparent in compiled C programs when the user defines a const type
// and the compiler uses TBLRD instructions to retrieve the data from program Flash memory.
// The issue is also apparent when the user defines an array in RAM for which the complier
// creates start-up code, executed before main(), that uses TBLRD instructions to initialize
// RAM from PFM.
//
// Work around
// Set the NVMREG<1:0> bits to select the appropriate memory region before executing
// TBLRD instructions.
//
// If there is a need to change the NVMREG<1:0> value to anything other than 0b10 and
// the Interrupt Service Routine uses constants or literal strings, then interrupts must
// be disabled before the change and restored to 0b10 before interrupts are enabled.
//
// In other words:
// You will have to change NVMCON1bits.NVMREG to access User ID, configuration bits,
// Rev ID, Device ID or Data EEPROM. If you do any of things, be sure to change it back to 0b10
//
public inline sub k40_nvm_errata()
asm
;NVMCON1 is located in the access bank, so no banksel reqd
bsf NVMCON1, 7 ;NVMREG1
bcf NVMCON1, 6 ;NVMREG0
end asm
end sub
k40_nvm_errata()
end module
When you use this, you MUST add it as the first 'include' in your program...
Code: Select all
program main
device=18F27K40
include "K40_errata.bas"
//<other includes go here>
end program