Swys
Joined: 24 Feb 2010 Posts: 22
|
Problem with write_program_memory() |
Posted: Tue May 18, 2010 7:09 am |
|
|
Hi all,
I am using a PIC24HJ128GP502 with version 4.107 of the PCD compiler.
I am writing a bootloader and I am trying to save the goto instruction and reset address (located at 0x0000 - 0x0004). My plan is then, after erasing the program memory up to my bootloader, to restore these values so that the PIC will go to the bootloader after a reset.
My code looks something like this at the moment:
Code: | unsigned int8 goto_val[4] = {0xff, 0xff, 0xff, 0xff};
unsigned int8 reset_val[4] = {0xff, 0xff, 0xff, 0xff};
//Backup the boot information
read_program_memory(0x000, goto_val, 4);
read_program_memory(0x002, reset_val, 4);
//Erase the memory
//erase_memory();
//Restore the boot information
write_program_memory(0x00, goto_val, 4);
write_program_memory(0x02, reset_val, 4);
|
And here is my erase function:
Code: | void erase_memory()
{
unsigned int32 erase;
for (erase = 0; erase < LOADER_START_ADDR; erase += (getenv("FLASH_ERASE_SIZE")/2))
{
erase_program_memory(erase);
}
} |
My problem is, that the values don't get restored after the erase. I does work, however, while I'm debugging with my ICD3, but it won't run correctly on its own.
I tried inserting a delay between the erase and the restore, but that didn't produce any fruit either.
Does anyone know how I can fix this?
[EDIT] Also, I noticed that in the generated HEX file, if the starting address of the user program was 0x200, the HEX file has it as 0x400. Why is that? Is it something I can always expect? Because then I could always subtract 0x200 from the HEX address before writing the value to the program memory. |
|