Matteo Martinelli
Newbie level 6
- Joined
- Jan 10, 2014
- Messages
- 11
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 77
Hi guys,
I need to flash program memory, actually I use this function for write/read EEPROM:
Write And Read:
And I use that functions as below
I don't know how I can write in Program memory because OPCODE is formed by 8 byte and Eeprom_WriteWord allow to write only 4 byte.
Eeprom_ReadWord is the same of write.
Thank you everybody.
I need to flash program memory, actually I use this function for write/read EEPROM:
Write And Read:
Code:
unsigned short Eeprom_ReadWord(unsigned short addressHi, unsigned short pushAddress) {
unsigned short ushResult;
register int eedata_addr;
register int eedata_val;
TBLPAG = addressHi; // __builtin_tblpage()
eedata_addr = (unsigned short)pushAddress; // __builtin_tbloffset()
__asm__("TBLRDL [%[addr]], %[val]" : [val]"=r"(eedata_val) : [addr]"r"(eedata_addr));
ushResult = eedata_val;
return (ushResult);
}
void Eeprom_WriteWord(unsigned short addressHi, unsigned short pushAddress, unsigned short ushValue) {
TBLPAG = addressHi;
NVMADRU = addressHi; // Write address of word to be erased into NVMADRU, NVMADR registers.
NVMADR = (unsigned short)pushAddress;
NVMCON = ERASE_WORD; // Setup NVMCON register to erase one EEPROM word.
//PROTECT_CODE_FROM_INTERRUPTS_START // Disable interrupts while the KEY sequence is written
NVMKEY = 0x55; // Write the KEY sequence step1
NVMKEY = 0xAA; // step2
NVMCONbits.WR = 1; // Start the erase cycle
//PROTECT_CODE_FROM_INTERRUPTS_STOP // Enable interrupts
while (NVMCONbits.WR == 1); // wait for the EEPROM
NVMCON = WRITE_WORD; // Setup NVMCON register to write one EEPROM word.
{
register int eedata_addr;
register int eedata_val;
eedata_addr = (unsigned short)pushAddress; // write low word of address
eedata_val = ushValue; // write data
__asm__ volatile ("TBLWTL %[val], [%[addr]]" : [val]"+r"(eedata_val) : [addr]"r"(eedata_addr));
}
//PROTECT_CODE_FROM_INTERRUPTS_START // Disable interrupts while the KEY sequence is written
NVMKEY = 0x55; // Write the KEY sequence step1
NVMKEY = 0xAA; // step2
NVMCONbits.WR = 1; // Start the erase cycle
//PROTECT_CODE_FROM_INTERRUPTS_STOP // Enable interrupts
while (NVMCONbits.WR == 1); // wait for the word to be written
// no proper watchdog protection in the 2 while loops
// no proper check if bytes are written correctly by re-reading them
}
And I use that functions as below
Code:
value = Eeprom_ReadWord(0x0000, 0x0002);
Eeprom_WriteWord(0x007F, 0xFC00, 0x0001);
I don't know how I can write in Program memory because OPCODE is formed by 8 byte and Eeprom_WriteWord allow to write only 4 byte.
Eeprom_ReadWord is the same of write.
Thank you everybody.