AnsweredAssumed Answered

Writing to flash works in debug but not in run mode

Question asked by Abdullah Kahraman on Dec 15, 2014
Latest reply on Dec 19, 2014 by Abdullah Kahraman

Hello,

 

I am working with a MKE04Z8M4 microcontroller. I am trying to use FLASH area to store some configuration values. I have a routine to write to a certain FLASH memory address but it works only when I do step by step debug. It doesn't work in run mode. Also, when I debug it and click run, it doesn't work that way, either. There is no compiler optimization. Here is my code:

 

void FLASH_program(void)
{
    // Read FCLKDIV register
    if (FTMRE_FCLKDIV != 0x17)
    {
        // If FCLKDIV register not correct,
        // Wait for any flash command in progress.
        while ((FTMRE_FSTAT & FTMRE_FSTAT_CCIF_MASK) == 0x00)
            ;
        // Write FLCKDIV register
        FTMRE_FCLKDIV = FTMRE_FCLKDIV_FDIV(0x17);
    }
    else
    {
        // If FCLKDIV register is correct,
        // Wait for any flash command in progress.
        while ((FTMRE_FSTAT & FTMRE_FSTAT_CCIF_MASK) == 0x00)
            ;
    }
    // Access Error and or Protection Violation Check
    if (((FTMRE_FSTAT & FTMRE_FSTAT_ACCERR_MASK) == 1)
            || ((FTMRE_FSTAT & FTMRE_FSTAT_FPVIOL_MASK) == 1))
    {
        // Write: FSTAT register: Clear ACCERR and FPVIOL
        FTMRE_FSTAT = 0x30;
    }

    // Write to FCCOBIX register to identify specific command parameter to load
    FTMRE_FCCOBIX = 0x00;
    // Write to FCCOB register to load required command parameter
    FTMRE_FCCOBHI = 0x06; // Program flash command code
    FTMRE_FCCOBLO = 0x00; // Global address [23:16] to identify flash block

    FTMRE_FCCOBIX = 0x01;
    FTMRE_FCCOBHI = 0x1D; // Global address [15:0] of longwords location to be programmed
    FTMRE_FCCOBLO = 0xFC; // Global address [1:0] must be 0x00

    FTMRE_FCCOBIX = 0x02;
    FTMRE_FCCOBHI = 0x00; // Word 0 (longword 0) program value
    FTMRE_FCCOBLO = 0x00; // (unsigned char) (PWM_duty_H1);

    FTMRE_FCCOBIX = 0x03;
    FTMRE_FCCOBHI = 0x00; // Word 1 (longword 0) program value
    FTMRE_FCCOBLO = 0x00; // (unsigned char) (PWM_duty_H2);

    FTMRE_FCCOBIX = 0x04;
    FTMRE_FCCOBHI = 0x00; // (unsigned char) (current_limit_FW >> 8); // Word 2 (long3word 1) program value
    FTMRE_FCCOBLO = 0x00; // (unsigned char) (current_limit_FW);

    FTMRE_FCCOBIX = 0x05;
    FTMRE_FCCOBHI = 0x00; // (unsigned char) (current_limit_BW >> 8); // Word 3 (longword 1) program value
    FTMRE_FCCOBLO = 0x00; // (unsigned char) (current_limit_BW);

    // Write: FSTAT register to launch command
    FTMRE_FSTAT = 0x80;

    // Wait for the flash command in progress.
    while ((FTMRE_FSTAT & FTMRE_FSTAT_CCIF_MASK) == 0x00)
        ;
}

 

Here is how my memory configured in the Processor Expert:

 

addr.png

Outcomes