Issue reading program data from the flash
Good afternoon,
I would like to share an issue I’m facing with a stm8s105c4.
A feature of our main application is to calculate a CRC of the entire program in the flash. Unfortunately, when I read flash data from addresses close to the instruction in execution, the data read it isn’t always correct.
I’ve been able to reproduce the issue in this small program:
main()
{
unsigned char* p_address;
unsigned char dataRead;
volatile dbg;
/* Loop and read the registers*/
for (p_address = (unsigned char*)0x80ce; ((unsigned int)p_address) < 0x80FF; p_address++)
{
dataRead = *p_address;
dbg++;
}
dataRead++;
}In this case, when p_address points to 0x80dc, this is what we can observe in the dissasembly and the core registers:
1) This set the address we want to read in X
0x80d7 <main+9> 0x1E04 LDW X,(0x04,SP)
2) This reads the value in the address point by X :
0x80d9 <main+11> 0xF6 LD A,(X)

But the data stored in the accumulator (0x8b) is wrong according to the memory view:

Or the Srecord:

On the main application, all the data that was read incorrectly was where a LD or LDW instruction was placed. Also in this example, as 0x80dc has this disassembly:
0x80dc <main+14> 0x1E01 LDW X,(0x01,SP)
Cosmic and ST Visual Develop was used for this example. I’ve also reproduced this in a stm8s105C6 using a Discovery board. A zip with the project is provided.
Any help will be appreciated. Kind regards,
David
