This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

memcpy corrupts global variables

Other Parts Discussed in Thread: CONTROLSUITE

I deleted some unused test code and it cause a problem with global variables getting corrupted after:

memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, RamfuncsLoadEnd - RamfuncsLoadStart);

I tracked the problem to a static array of structs declared in one of the functions. If I decrease the size of the array to a particular point and the problem will appear. if .ebss is about 27% or less of the section of Memory, the problem occurs. below are the contents of the linker file. I can't see any reason for the interaction.

RAML0 : origin = 0x008000, length = 0x000800

L7DPSARAM : origin = 0x010000, length = 0x003000

.ebss : > L7DPSARAM, PAGE = 1

ramfuncs : LOAD = FLASH_ABCDEFGH,

RUN = RAML0,

LOAD_START(_RamfuncsLoadStart),

LOAD_END(_RamfuncsLoadEnd),

RUN_START(_RamfuncsRunStart),

PAGE = 0

  • I created a blank project and took the F28069.cmd and replaced it in my project. all I had to do is to move the .econst section to a different flash sector for it to build.

    I see the issue with every RAMLx region except for RAML8. What's going on? after the memcpy(), all the data is set to 0xFFFF.
     
    ramfuncs : LOAD = FLASHD,
    RUN = RAML0,
    LOAD_START(_RamfuncsLoadStart),
    LOAD_END(_RamfuncsLoadEnd),
    RUN_START(_RamfuncsRunStart),
    PAGE = 0

    PAGE 1 : /* Data Memory */
    /* Memory (RAM/FLASH/OTP) blocks can be moved to PAGE0 for program allocation */
    /* Registers remain on PAGE1 */

    BOOT_RSVD : origin = 0x000000, length = 0x000050 /* Part of M0, BOOT rom will use this for stack */
    RAMM0 : origin = 0x000050, length = 0x0003B0 /* on-chip RAM block M0 */
    RAMM1 : origin = 0x000400, length = 0x000400 /* on-chip RAM block M1 */
    RAML2 : origin = 0x008C00, length = 0x000400 /* on-chip RAM block L2 */
    RAML3 : origin = 0x009000, length = 0x001000 /* on-chip RAM block L3 */
    RAML4 : origin = 0x00A000, length = 0x002000 /* on-chip RAM block L4 */
    RAML5 : origin = 0x00C000, length = 0x002000 /* on-chip RAM block L5 */
    RAML6 : origin = 0x00E000, length = 0x002000 /* on-chip RAM block L6 */
    RAML7 : origin = 0x010000, length = 0x002000 /* on-chip RAM block L7 */
    RAML8 : origin = 0x012000, length = 0x002000 /* on-chip RAM block L8 */
    USB_RAM : origin = 0x040000, length = 0x000800 /* USB RAM */
    FLASHB : origin = 0x3F0000, length = 0x004000 /* on-chip FLASH */
    }

    /* Allocate uninitalized data sections: */
    .stack : > RAMM0, PAGE = 1
    .ebss : > RAML8, PAGE = 1 /*OK. any other region bad */
    .esysmem : > RAML2, PAGE = 1

  • Charles,
    what is the unused test code? can you post the code erased? This will help us to understand a little more the cause of the problem.
    Regards
    Gastón
  • What is the value of "RamfuncsLoadEnd - RamfuncsLoadStart" used in memcpy?
    Will the code actually fit inside RAML0?

    /Michael
  • You need to read this pdf spra958l ().
    This pdf will explain it. You have a example code (highlight in blue in the pdf. Read the SysCtrl pdf of your device.
    Regards
    Gastón
  • whatever is going on was masked by the fact that the declaration of the array was padding memory so the trashing of my variables went unnoticed.

    /** Fault record definition - packed */

    typedef struct {

    unsigned short utcMonthYear; /**< UTC month - UTC year */

    unsigned short utcDayHour; /**< UTC day - hour */

    unsigned short utcMinuteSecond; /**< UTC minute - second */

    unsigned short flightLeg; /**< Flight Leg */

    unsigned short faultIdflightPhase; /**< fault Id - Flight Phase */

    unsigned short faultCount; /**< Number of fault occurrences */

    unsigned char cm_005[8]; /**< CM_005 message contents */

    unsigned char cm_009[8]; /**< CM_009 message contents */

    unsigned char cm_007[8]; /**< CM_007 message contents */

    } *PNVM_FAULT_RECORD_T, NVM_FAULT_RECORD_T;

    #define NVM_SIZE0F_PACKED_FAULT_RECORD 18 /**< size in words */



    BOOL_T testnvmTestFaultRecord(void)

    {

    static FAULT_RECORD_T structFaultRecord;

    static FAULT_RECORD_T structReadFaultRecord[200];

    static unsigned short usCumulativeCount[128];

    }

  • memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, RamfuncsLoadEnd - RamfuncsLoadStart);
    RamfuncsRunStart = 0x08000
    RamfuncsLoadStart = 0x3E8000
    RamfuncsLoadEnd = 0x3E801F

    should only need 0x1F.
  • Looking thru example code, I saw it done this way and I took a shot in the dark and changed this line from
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, RamfuncsLoadEnd - RamfuncsLoadStart);
    to
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
    and added
    LOAD_SIZE(_RamfuncsLoadSize),
    to the linker file.

    no more corruption.
  • great news. Remember to check the example code in flash in the controlSuite to compare your code. Read the pdf link also, it will help you.
    Regards
    Gastón