Q)Does this mean that i must copy 4kB of data, erase the sector and then re-write with data along with my new data?
A) Your understanding is correct. You need to copy the whole of 4KB, erase and then re write.
But it is not true in all the cases. Say initially the data in the location is 0x07(00000111) you can program it to 0x05(00000101) as you are not making any zeroes to one. But you can not write 0x08 as it involves making the 4th bit zero which is not possible. You need to erase so that all the bits will be 1 and then make 4th bit zero.
Also when you are writing for the first there is no need to erase. Only if you are updating the already written data and if it involves changing 0 to 1 you need to erase adn rewrite it. This needs to be implemented in your code.
As we know in flash programming means changing 1 to 0. We can not make 0 to 1 which is why we erase and make all 1s.
Thank you for your reply,
i will implement it in my code.
i've implemented this in my code but i'm finding that it is taking some time (up to seven seconds to write 5 bytes) and i'm concerned not only for the time, but the current consumption.
My method is to erase a sector which will store my temporary data. I then read the entire sector i wish to store the new data in.
I do this in chunks of 256 bytes into ram. When full i write it as a complete page. If my new data falls in the range, it gets stored in the buffer array. i repeat 16 times to copy the entire sector.
Once the entire sector is temporarily stored, i erase the "final storage" sector and transfer the data back from the temporary storage sector.
I've noted that to erase even one sector can take 450mS and i have to erase two sectors.
Can someone recommend another strategy that i can use. I'm guessing it will involve using pointers to the changing memory location instead of a fixed memory location.
Can you please send the code to us. We will review it and suggest you if it needs any improvement.