S12XHY128 flash driver issue

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

S12XHY128 flash driver issue

2,688 Views
julieliu
Contributor I

Dear friend ,

I am now working on S12XHY128 flash driver.

but now it can not work well.

Here is the code of write function.


tFlashStatus FLASH_RAM_WriteRoutine(tFlashAddress uAddr, u16 uwLen, const u8 FLASH_FAR_POINTER aubData)
{
    u16 uwNbData;
    u8 ubIdx;
    u8 ubStatus;
    u8 ebStatus;

    tFlashStatus eFlashStatus;
    tFlashBoolean eFlashBoolean;

    eFlashStatus = FLASH_NO_ERROR;
    eFlashBoolean = FLASH_TRUE;
    uwNbData = uwLen;

    /* if no data to write */
    if (uwNbData)
    {
        /* if FDIVLD not set */
        if ((FLASH_FCLKDIV & FLASH_FCLKDIV_FDIVLD_MASK) == 0)
        {
            /* FCLKDIV must be set after each reset. */
            FLASH_FCLKDIV = FLASH_FCLKDIV_CONF;
        }

        /* FCCOB availability check */
        while ((FLASH_FSTAT & FLASH_FSTAT_CCIF_MASK) == 0U)
        {
            /* do nothing */
        }

        if (((FLASH_FSTAT & FLASH_FSTAT_ACCERR_MASK) != 0U) &&
            ((FLASH_FSTAT & FLASH_FSTAT_FPVIOL_MASK) != 0U))
        {
            /*  Clear any error flags  */
            FLASH_FSTAT |= (FLASH_FSTAT_ACCERR_MASK | FLASH_FSTAT_FPVIOL_MASK);
        }

        /* Write the program command and the GPAGE part */
        FLASH_FCCOBIX = 0U;
        FLASH_FCCOBHI = FLASH_PROGRAM_P_FLASH;
        FLASH_FCCOBLO = FLASH_WP_PAGE(uAddr);
        uwtest = FLASH_FCCOB;
        /* Write the 16-bit address part of phrase */
        (FLASH_FCCOBIX)++;
        FLASH_FCCOB = FLASH_WP_ADDR(uAddr);
uwtest = FLASH_FCCOB;
        uAddr = (tFlashAddress)((u32)uAddr + 8U);

        /*  Fill the 8 bytes    */
        for (ubIdx = 0U; ubIdx < 8U; ubIdx++)
        {
            /* If we still have datas to write */
            if (uwNbData != 0U)
            {
                /* If the byte is pair write the low part
                           else increment FCCOBIX index and write the high part */
                if ((ubIdx % 2U) == 1U)
                {
                    /* Write next data word to CCOB buffer. */
                    FLASH_FCCOBLO = *aubData;
                    aubData++;
                    uwtest = FLASH_FCCOB;
                }

                else
                {
                    (FLASH_FCCOBIX)++;
                    FLASH_FCCOBHI = *aubData;
                    aubData++;
                }
                /* Decrement the byte number to write */
                uwNbData--;
            }

            /* Else write 0xFFF in the last byte */
            else
            {
                if ((ubIdx % 2U) == 1U)
                {
                    /* Write next data word to CCOB buffer. */
                    FLASH_FCCOBLO = 0xFFU;
                }
                else
                {
                    (FLASH_FCCOBIX)++;
                    FLASH_FCCOBHI = 0xFFU;
                }
            }
        } /* End for(ubIdx = 0; ubIdx< 8;   ubIdx++) */
        ubfstat = FLASH_FSTAT;
        /* Launch command */
        FLASH_FSTAT |= FLASH_FSTAT_CCIF_MASK;

        /* Bit polling for command completion check */
        while ((FLASH_FSTAT & FLASH_FSTAT_CCIF_MASK) == 0U)
        {
            /* do nothing */
        }
        ubfstat = FLASH_FSTAT;
    }

    /* Check the error flags and return the error status */
    if (((FLASH_FSTAT & (u8)(FLASH_FSTAT_ACCERR_MASK | FLASH_FSTAT_MGSTAT0_MASK | FLASH_FSTAT_MGSTAT1_MASK)) != 0U) ||
        (eFlashBoolean == FLASH_FALSE))
    {
        eFlashStatus = FLASH_ACCESS_ERROR;
    }
    else if ((FLASH_FSTAT & FLASH_FSTAT_FPVIOL_MASK) != 0U)
    {
        eFlashStatus = FLASH_PROT_ERROR;
    }
    else
    {
        /* Nothing to do */
    }

    return eFlashStatus;
}
/*---------------------------{end FLASH_RAM_WriteRoutine}-----------------------------*/

But it does not work and I have erased before the write action.

The address I used is global address like  following pic:

pastedImage_3.png

Does anyone know what is wrong and do you have sample code for me to reference?

Thanks a lot for your help.

br,

julie

0 Kudos
6 Replies

2,388 Views
danielmartynek
NXP TechSupport
NXP TechSupport

Hi,

Please refer to the attached example code.

S12XS128 has the same Flash module and the memory map.

Regards,

Daniel

0 Kudos

2,388 Views
julieliu
Contributor I

Hello Daniel,

Thanks for you help and I have check the code.But still could not test yet as the board is go into secured mode.

you can see the error and warnings here.

pastedImage_1.png

seems I have erased some configuration field.

16 bytes (0x7F_FF00 - 0x7F_FF0F)

And this have effect on secure mode.

Do you have anyidea how to fix this issue.

thanks a lot.

br,

julie

0 Kudos

2,388 Views
danielmartynek
NXP TechSupport
NXP TechSupport

Hello Julie,

Please read S12(X) MCU Security.

The document also describes how to unsecure the MCU.

Regards,

Daniel

0 Kudos

2,388 Views
julieliu
Contributor I

Hello,

Thanks for your before reply and according to flash program and erase sequence.

I test the code both on S12XHY128 and S12G128 which I think could use the same flash driver.

But the code could not work on both board.

And I saw something very strange and I will take S12G128 (0x20000-0x2FFFF)for example.

Before I do anything we can see flash memory like this which contain some strange number inside.

It also change nothing after I erase a sector form 0x20000.

The same when I try to write something in it.

But I actually write something in somewhere else which I think it’s the wrong place.

So do you have any idea what’s the issue.

Hope for your suggestion.

Thanks.

Br,

Julie

0 Kudos

2,388 Views
danielmartynek
NXP TechSupport
NXP TechSupport

Hello Julie,

 

S12G128 has different memory map. See attachments.

I’m not sure if you can use the code without any changes, I haven’t tested it.

We don’t have any Flash code ported on S12XHY128. The S12XS code was only for your reference.

Attached is an example ported on S12G240.

 

You should remove the pages that you use from the memory space in the linker file so that it would be used by compiler for code placement.

For example, the S12G240 code writes to 0x0E8000 - 0x0EBFFF so PAGE_0E is removed.

S12G240_linker_file.png

If you want to use 0x20000 - 0x2FFFF, remove pages PAGE_08 – 0B.

 

Do you read the flash with pointers? Or only using the memory window? Do you refresh it.

What is the status of error flags after a flash operations?

 

Regards,

Daniel

0 Kudos

2,388 Views
julieliu
Contributor I

Hello Daniel,

Thanks a lot for your support now both S12G128 and S12XHY128 could work now.

S12XHY128 as well as S12G128 could write and erase with global address but read only with paged flash.

Now I could move forward with bootloader on this two boards.

Thanks again as I may also have issue with bootloader and will disturb you again.

Br,

Julie

0 Kudos