I am writing data to a large flash memory and wanted to follow the write with a read and verify operation. The verify code reported a failure even though subsequent investigations showed that the flash write had been successful. So the problem lies with the verification code.
Because the flash read and write code acts on 16-bit words and my original data is in an 8-bit byte array, my verification code does a comparison of words, and collates two of the bytes with this line (inside a loop):
valueWord = value[j] + (value[j+1] << 8);
This line produces an error when j=0 (but not when j>0 - the problem only happens the first time round the loop). On the other hand, this line always gives the expected result:
valueWord = (value[j] & 0xff) + (value[j+1] << 8);
It should not be necessary to mask off the MS bits of value[j] since it should be treated as an 8-bit quantity. IMHO this is a bug.
Behaviour is consistent with this: the first time round the loop the value[j] is treated as a 16-bit word, and the value accumulating in valueWord includes the next byte in the array treated as the 8 MS bits of a 16-bit word. This next byte is value[j+1], with the overall effect:
valueWord = ( value[j] + (value[j+1] << 8) ) + (value[j+1] << 8);
So for example when value = 0x30 and value = 0x01 the correct value of the 16-bit word should be 0x0130
but instead valueWord = 0x0230.
This hypothesis is bourne out by this code:
PRINT("[%04x %04x %04x] ", valueWord, value[j] + 0x100, (value[j] & 0xff) + 0x100);
which gives this result (the first time round the loop, when j=0): [0230 0230 0130]
I have the compiler list files if anyone wants to check teh code (I don't understand the assembler, so can't see where the bug lies).
Regards - Charles