4
votes

I am working on making a bootloader. I have to erase 1024 bytes of memory before I write anything to those registers in that block. Even if I want to write 2 bytes, I am forced to erase 1024 bytes. My problem is that I don't know where each block starts. For example, lets say I want to write the following bytes to this address.

Address: 0x198F0

Bytes:C80E00010001616FDFECD6F08C8C92EC

When I try to erase 1024 bytes starting from address 0x198F0I noticed that it starts erasing from 0x19800 instead.

How do I know where each block starts from so I can calculate it in software?

The reason I want to know this is so I can copy the entire block into ram before I erase it, then modify it, and write it back to the same block. I am using PIC18f87J11 with MPLAB XC8 compiler. I hope its clear what I am trying to do, otherwise let me know in the comments.

Thanks!

1
You already asked a near identical question yesterday : stackoverflow.com/questions/23895253/…. Moreover the behaviour you describe is normal for flash memory and described in detail in the part's user manual - including the block layout and addresses.Clifford
@Clifford, it wouldn't make sense if I asked this question in that thread after it was answered? In that thread I was asking if I could erase some bytes from memory, but here I wanted to know how to find the start of each block. Yes, both are related, but I don't agree that you should mark this as duplicate. From past experience, users don't like it when I ask multiple questions inside one thread that started with just one question.Ammar
I have retracted the duplicate. I might suggest that there is perhaps a little too much preamble before you get to the actual question. You might pose the question first, then add the supporting/clarifying information (though that is probably unnecessary). The thing is that any one that can answer your question probably already understands about flash memory erase blocks. You can mask the low-order address bits as suggested by GJ, or you can simply divide the address by the block size with normal truncating integer division. GJ's solution is more efficient especially on a PIC.Clifford
@Clifford, Thank you!Ammar

1 Answers

5
votes

The FLASH memory blocks of PIC18f87J11 are 1024 bytes align. To calcolate start address of some block set last 10 bits of address to 0, so you can use:

StartAddress = AddressPtr and 0xFFFC00

In your case:

0x198F0 and 0xFFFC00 = 0x19800