AnsweredAssumed Answered

S12G Program/Erase flash problem

Question asked by zaeuiezaonje on Dec 2, 2014
Latest reply on Dec 4, 2014 by zaeuiezaonje

Hi,

 

I try to erase or program flash on a S12G MCU but I've a trouble. The code running without error (ACCERR and FPVIOL bits still at 0) but the flash still in the same state.

 

I've tried the following code given as example without sucess :

 

tU16 FLASH_addr(tU08 type)

{

    tU16 base, addr, j;

    tU32 i;

    // Fill the range with its address

    asm BGND;   /* Software Breakpoint 3 - by running to here the erase,

                   erase verify and program commands have all been launched.

                   The P-flash has now been programmed with the addresses as

                   shown in the memory maps */ 

                 

    base = FlashStartAddrGL[type];

    for (i = 0; i < FlashSectNum[type]; i++)

    {

        if(LaunchFlashCommand(2, EraseFlashSector[type], FlashStartAddrGH[type], base, 0, 0, 0, 0) != CCIF_MASK) /* erase the flash block again*/

            return FLASH_ACCESS_ERROR;     

        if(LaunchFlashCommand(3, EraseVerifyFlashSector[type], FlashStartAddrGH[type], base, FlashSectSize[type]>>(type+(2^type)-1), 0, 0, 0) != CCIF_MASK) /* timing sector erase command */

            return FLASH_ACCESS_ERROR;

 

        addr = base;

        for (j = 0; j < FlashSectSize[type]; j+=8)

        {

            if(LaunchFlashCommand(2*(2+type), ProgramFlash[type], FlashStartAddrGH[type], addr, addr, addr+2, addr+4, addr+6) != CCIF_MASK) /* timing 1 word program command */

            return FLASH_ACCESS_ERROR;

            addr += 8;

        }

        base += FlashSectSize[type];

    }

    return FLASH_OK;

}

 

tU08 LaunchFlashCommand(char params, tU08 command, tU08 ccob0, tU16 ccob1, tU16 ccob2, tU16 ccob3, tU16 ccob4, tU16 ccob5)

{

    if(FSTAT_CCIF == 1)

    {

        /* Clear any error flags*/

        FSTAT = (FPVIOL_MASK | ACCERR_MASK);

 

        /* Write the command id / ccob0 */

        FCCOBIX = 0;

        FCCOBHI = command;

        FCCOBLO = ccob0;

 

        if(++FCCOBIX != params)

        {

            FCCOB = ccob1;                /* Write next data word to CCOB buffer. */

            if(++FCCOBIX != params)

            {

                FCCOB = ccob2;             /* Write next data word to CCOB buffer. */

                if(++FCCOBIX != params)

                {

                    FCCOB = ccob3;        /* Write next data word to CCOB buffer. */

                    if(++FCCOBIX != params)

                    {

                        FCCOB = ccob4;    /* Write next data word to CCOB buffer. */

                        if(++FCCOBIX != params)

                            FCCOB = ccob5;          /* Write next data word to CCOB buffer. */

                    }

                } 

            }

        }

        FCCOBIX = params-1;

 

        /* Clear command buffer empty flag by writing a 1 to it */

        FSTAT = CCIF_MASK;

        while (!FSTAT_CCIF) {     /* wait for the command to complete */

        /* Return status. */

        }

        return(FSTAT);     /* command completed */

    }

 

    return(FLASH_BUSY); /* state machine busy */

}

I've also heared that the CCIF clearing and the wait have to be in RAM, so I've put the previous red section of code in RAM but this handling have no effects...

Do you know where can be the mistake ?

Thank you very much for any help.

Outcomes