I am facing a strange issue while using the Freescale's NVM library of MC1322x ARM7 Chip to read from/write to Flash. I am not sure what is it that I am doing incorrectly in s/w and would like some help.
I have a piece of code that looks something like this:
main()
{
unsigned char temp1, temp2;
unsigned char temp3 = 0x45;
//power on NVM
NVM_Erase(0x0001C000); //Erase sector starting at 0x0001C000
NVM_Read(&temp1, 0x0001C000, 0x01); //Reading one byte starting at address 0x0001C000 //and copying information to temp1
NVM_Write(&temp3, 0x0001C000, 0x01); //Write 0x45 at address 0x0001C000
}
When I executed this, and stepped in, I was hoping to get temp1 as 0xFF (since I did an erase). Strangely, I get temp1 as 0x45! I noticed that whatever value I set to temp3, is what I get when I watch temp1 in the watch window. I know for sure, that the NVM Address actually holds 0xFF, and I can't figure out how it gets temp3's value.
I tried making these variables static or global but the result remains the same.
I tried doing read and write in different functions but the result remains the same.
I slightly altered the code and did this:
main()
{
unsigned char temp1, temp2;
unsigned char temp3 = 0x45;
//power on NVM
NVM_Read(&temp1, 0x0001C000, 0x01); //Reading one byte starting at address 0x0001C000 //and copying information to temp1
NVM_Write(&temp3, 0x0001C000, 0x01); //Write 0x45 at address 0x0001C000
NVM_Erase(0x0001C000); //Erase sector starting at 0x0001C000
}
Now, when I read temp1, I got 0xFF!
The conclusion is that the NVM Read function gives the value, that is actually planned to be written into it. (in the first case, even though the NVM Write was never executed, temp1 still always read temp3's value). In the second case, given that the erase code comes after write, temp1 reads 0xFF.
I don't understand this strange correlation, and NVM_Read reading a value, even before NVM_Write is executed!! Given that NVM Read and Write is pure assembly code, I find it difficult to interpret it.
Any help deeply appreciated!!!
Mads,
I use the Erase with bitfield as parameter. I mentioned address here, for easier understanding.
In the same code if I dont do the write after read, the read returns 0xFF. The problem is with the NVM Write command which is not executed at all, influencing the result of the NVM Read that is done before it.
Hello,
I know little of the device and library you are using, but I do know that Codewarrior assumes that flash is like ROM for the purposes of simulation and debugging. That is, it does not necessarily update flash memory locations on screen as it assumes they will not change. You need to change settings or manually update to be sure of the values.
Also you have to be careful stepping through erase/programme code!
Sowmya,
your problem is that your erase command is not doing what you think.
the NV_Erase() function does NOT take an address as a parameter, but it takes a bit field that tells what sector to erase.
so if you want to erase address 0x1C000 you will need to set the bit equilvant of that sector.
each sector is 4 KB big.
BR,
Mads