Skip to main content
David Beltran Lacasa
Associate II
Jan 15, 2019 1:13 PM UTC
Solved

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) 

0690X000006D6h8QAC.png 

2)     This reads the value in the address point by X :

0x80d9 <main+11>           0xF6           LD   A,(X)

 0690X000006D6hDQAS.png

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

 0690X000006D6hIQAS.png

Or the Srecord:

 0690X000006D6hNQAS.png

  

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

    This topic has been closed for replies.
    S.Ma
    S.MaBest answer
    Principal
    Jan 15, 2019 2:55 PM UTC

    Have you tried to make the flash reading instruction reside in RAM and called from the flash program to see if there is a difference?

    2 replies

    S.Ma
    S.MaBest answer
    Principal
    Jan 15, 2019 2:55 PM UTC

    Have you tried to make the flash reading instruction reside in RAM and called from the flash program to see if there is a difference?

    David Beltran Lacasa
    Associate II
    Jan 16, 2019 11:07 AM UTC

    I haven't tried, I had a bit of pressure to find a solution so I decided to forget about the algorithm we were using and switch to the _checksum16 routines provided by Cosmic.

    Then I found out the same issue was happening:

    https://community.st.com/s/question/0D50X00009XkhppSAB/cosmic-checksum16-problems

    The answer to my problem is there: a software breakpoint. I wasn't familiarized with software breakpoints implementations so I didn't figured out before.

    Thanks for the help.

    David