S12Z EEPROM Read and write

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

S12Z EEPROM Read and write

Jump to solution
2,245 Views
charudattaingal
Contributor IV

Dear Team,

 

Requesting assistance.

 

Read and write not reflected in PE- debugger while free running.

Step by step debugging read and write is reflected but while reading second read is not working fine.

 

 

main function :

 

bus clock is 32m

 

void main()

{

 

byte data,data1;

 

HW_EEPROM_Init();

 

HW_EEPROM_EraseSector(0x100000UL);

 

HW_EEPROM_WriteWord(0x100000UL,arr[0]);

 

//for(i=0;i<100;i++){};

 

HW_EEPROM_WriteWord(0x100002UL,arr[1]);

 

data=EEPROM_Read_byte(0x100000UL);

data1=EEPROM_Read_byte(0x100002UL);  // problem with this write

 

}

 

init function:

 

EEPROMDR_etErrorStatus_t HW_EEPROM_Init(void)

{

   

    uint8_t ucRegValCheck = 0U;

    EEPROMDR_etErrorStatus_t etErrorStatus = EEPROMDrv_ERR_OK;

   

    /* Set up Clock Divider Register */

   

    FCLKDIV = 0x1FU;

   

    ucRegValCheck = FCLKDIV;

      

        if ( ucRegValCheck != 0x1F)

        {

            etErrorStatus = EEPROMDrv_ERR_FAULT;

        }

   

   

    INT_CFADDR = 0x58U;   /*(Vector base + 0x160) flash command Interrupt Request Configuration */

   

    ucRegValCheck = INT_CFADDR;

   

        if( 0x58U != ucRegValCheck )

        {

            etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

        }

 

 

    /* Int. priority initialization */

   

    INT_CFDATA0 = 0x04U;

   

    ucRegValCheck = INT_CFDATA0;

   

        if ( 0x04U != ucRegValCheck )

        {

            etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

        }

 

     return ( etErrorStatus );

}

 

EEPROMDR_etErrorStatus_t HW_EEPROM_WriteWord( uint32_t ulAddress,uint16_t usData )

{

    uint16_t ucRegValCheck = 0x00;

    EEPROMDR_etErrorStatus_t etErrorStatus = EEPROMDrv_ERR_OK;

 

 

        if ( 0U == FSTAT_CCIF )

        {                    /* Is reading from EEPROM possible? */

             return EEPROMDrv_ERR_BUSY;                   /* If no then error */

        }

 

        /* Clear error flags */

        /* FSTAT: ACCERR=1,FPVIOL=1 */

        FSTAT = 0x30U;

 

        /* Three FCCOBn registers will be loaded */

        FCCOBIX = 2U;

       

        ucRegValCheck = FCCOBIX;

            if ( 2U != ucRegValCheck )

            {

                etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

            }

 

        /* Program D-Flash command */

        FCCOB0HI = 0x11U;

       

        ucRegValCheck = FCCOB0HI;

            if ( 0x11U != ucRegValCheck )

            {

                etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

            }

 

        /* High address word */

        FCCOB0LO = ( uint8_t ) ( ( ( uint32_t ) ulAddress ) >> 16U );

       

        ucRegValCheck = FCCOB0LO;

            if ( ( uint8_t ) ( ( ( uint32_t ) ulAddress ) >> 16U ) != ucRegValCheck )

            {

                etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

            }

 

        /* Low address word */

        FCCOB1 = ( uint16_t ) ( ( uint32_t ) ulAddress );

       

        ucRegValCheck = FCCOB1;

            if ( ( uint16_t ) ( ( uint32_t ) ulAddress ) != ucRegValCheck )

            {

                etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

            }

 

        /* Load new data */

        FCCOB2 = usData;

       

        ucRegValCheck = FCCOB2;

            if ( usData != ucRegValCheck )

            {

                etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

            }

 

        /* Clear flag command complete */

        /* FSTAT: CCIF=1 */

        FSTAT = 0x80U;

        /* Enable interrupt */

        FCNFG_CCIE = 1U;

       

        ucRegValCheck = FCNFG_CCIE;

            if ( 1U != ucRegValCheck )

            {

                etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

            }

 

        if ( ( FSTAT & 0x30U ) != 0U ) /* Is protection violation or acces error detected ? */

        {

            /* Disable interrupt */

            FCNFG_CCIE = 0U;

            ucRegValCheck = FCNFG_CCIE;

           

            if(0U !=  ucRegValCheck)

            {

                etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

            }

 

            return EEPROMDrv_ERR_NOTAVAIL;               /* If yes then error */

        }

 

        ucRegValCheck = FSTAT_CCIF;

        while ( 0U == ucRegValCheck )

        {                /* Wait to command complete */

            ucRegValCheck = FSTAT_CCIF;

        }

 

        ucRegValCheck = FSTAT_MGSTAT;

        /* Is an error detected ? */ /* Was attempt to write data to the given address errorneous? */

            if ( 1U == ucRegValCheck )

            {

                /* Disable interrupt */

                FCNFG_CCIE = 0U;

                return EEPROMDrv_ERR_VALUE;                  /* If yes then error */

            }

      

        return ( etErrorStatus );

}

 

 

 

 

Write Function:

 

EEPROMDR_etErrorStatus_t EEPROM_WriteWord ( uint32_t ulAddress,uint16_t usData )

{

    uint16_t ucRegValCheck = 0x00;

    EEPROMDR_etErrorStatus_t etErrorStatus = EEPROMDrv_ERR_OK;

 

 

    if ( 0U == FSTAT_CCIF )

    {                    /* Is reading from EEPROM possible? */

         return EEPROMDrv_ERR_BUSY;                   /* If no then error */

    }

 

    /* Clear error flags */

    /* FSTAT: ACCERR=1,FPVIOL=1 */

    FSTAT = 0x30U;

 

    /* Three FCCOBn registers will be loaded */

    FCCOBIX = 2U;

    ucRegValCheck = FCCOBIX;

    if ( 2U != ucRegValCheck )

    {

        etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

    }

 

    /* Program D-Flash command */

    FCCOB0HI = 0x11U;

    ucRegValCheck = FCCOB0HI;

    if ( 0x11U != ucRegValCheck )

    {

        etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

    }

 

    /* High address word */

    FCCOB0LO = ( uint8_t ) ( ( ( uint32_t ) ulAddress ) >> 16U );

    ucRegValCheck = FCCOB0LO;

    if ( ( uint8_t ) ( ( ( uint32_t ) ulAddress ) >> 16U ) != ucRegValCheck )

    {

        etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

    }

 

    /* Low address word */

    FCCOB1 = ( uint16_t ) ( ( uint32_t ) ulAddress );

    ucRegValCheck = FCCOB1;

    if ( ( uint16_t ) ( ( uint32_t ) ulAddress ) != ucRegValCheck )

    {

        etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

    }

 

    /* Load new data */

    FCCOB2 = usData;

    ucRegValCheck = FCCOB2;

    if ( usData != ucRegValCheck )

    {

        etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

    }

 

    /* violates ref mc9s12z_eeprom_c_REF_3 MISRA rule 1.2 */

    EnterCritical();

    /* Clear flag command complete */

    /* FSTAT: CCIF=1 */

    FSTAT = 0x80U;

    /* Enable interrupt */

    FCNFG_CCIE = 1U;

    ucRegValCheck = FCNFG_CCIE;

    if ( 1U != ucRegValCheck )

    {

        etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

    }

 

    /* violates ref mc9s12z_eeprom_c_REF_3 MISRA rule 1.2 */

    ExitCritical();

    if ( ( FSTAT & 0x30U ) != 0U ) /* Is protection violation or acces

                                    error detected ? */

    {

        /* Disable interrupt */

        FCNFG_CCIE = 0U;

        ucRegValCheck = FCNFG_CCIE;

        if(0U !=  ucRegValCheck)

        {

            etErrorStatus = EEPROMDrv_ERR_RegisterNotSet;

        }

 

        return EEPROMDrv_ERR_NOTAVAIL;               /* If yes then error */

    }

 

 

Read Function:

 

byte EEPROM_Read_byte(uint32_t ulAddress )

{

  byte data;

  data = *(byte *)ulAddress ;

  return data;

}

Labels (1)
0 Kudos
1 Solution
1,355 Views
RadekS
NXP Employee
NXP Employee

Hi Charudatta,

I shortly checked your code and currently I do not see any obvious error, but just two potential issues in that code.

1. I am not sure with your HW_EEPROM_Init().

There you set FCLKDIV = 0x1FU; which means that you divide bus clock 32times.

But I do not see PLL settings in your main function.

Default Bus Clock after reset is 6.25MHz.

Could you please check whether you set PLL to 32MHz in any other function or change HW_EEPROM_Init() function to reflect lower bus clock? For example: FCLKDIV = 0x05U;

2. You enabled flash interrupt by “FCNFG_CCIE = 1U;”, but simultaneously you also waits for FSTAT_CCIF flag in while loop. From your code isn’t clear what you are doing in flash interrupt routine.

Also content HW_EEPROM_EraseSector() is not presented in your question. Instead of that, you placed here content of EEPROM_WriteWord() function, which is not used in your main() function.

Could you please clarify what you are mean by “second read is not working fine”? Is there any error flag, or you got wrong values? I would like to note that you wrote two words (4 bytes) but you read per bytes. So, you should call EEPROM_Read_byte() four times for reading all data which you wrote to EEPROM. Optionally you can read data by words...


I hope it helps you.

Have a great day,
RadekS

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
10 Replies
1,356 Views
RadekS
NXP Employee
NXP Employee

Hi Charudatta,

I shortly checked your code and currently I do not see any obvious error, but just two potential issues in that code.

1. I am not sure with your HW_EEPROM_Init().

There you set FCLKDIV = 0x1FU; which means that you divide bus clock 32times.

But I do not see PLL settings in your main function.

Default Bus Clock after reset is 6.25MHz.

Could you please check whether you set PLL to 32MHz in any other function or change HW_EEPROM_Init() function to reflect lower bus clock? For example: FCLKDIV = 0x05U;

2. You enabled flash interrupt by “FCNFG_CCIE = 1U;”, but simultaneously you also waits for FSTAT_CCIF flag in while loop. From your code isn’t clear what you are doing in flash interrupt routine.

Also content HW_EEPROM_EraseSector() is not presented in your question. Instead of that, you placed here content of EEPROM_WriteWord() function, which is not used in your main() function.

Could you please clarify what you are mean by “second read is not working fine”? Is there any error flag, or you got wrong values? I would like to note that you wrote two words (4 bytes) but you read per bytes. So, you should call EEPROM_Read_byte() four times for reading all data which you wrote to EEPROM. Optionally you can read data by words...


I hope it helps you.

Have a great day,
RadekS

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,355 Views
charudattaingal
Contributor IV

Thanks RadekS i got it.

Please clarify while debugging it is  writing and reading  EEPROM successfully.

When we terminate debugging and again start debugging the EEPROM having contain FF...

Previously written contain should be there on EEPROM .  

0 Kudos
1,355 Views
RadekS
NXP Employee
NXP Employee

Hi Charudatta,

I tested EEPROM at S12ZVL and everything works correctly. Data stays in EEPROM even after reset.

Please look at my example code in attachment. At start I added check whether EEPROM is erased or not (RED LED on TWR-S12ZVL32 board).

When you start debugging and you load new code, debugger automatically erases whole P-Flash and EEPROM.

Please use Attach instead Download for reading current values in EEPROM.

ZVL32-Attach.png


I hope it helps you.

Have a great day,
RadekS

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,355 Views
leonsun65
Contributor I

Hi Radek,

I tried to debug your code ZVL32-EEPROM-CW106.ZIP using TRK - S12ZVL demo board and CodeWarrior 10.6.4 and got error "No source available for "0x000001 (0x000001)()". Don't know what is wrong. Could you take a look this issue? By the way, it asked me to update the firmware in MC9S08JM60 when I first run it and I did it according to the directions.

Best Regards,

Leon Sun

0 Kudos
1,355 Views
RadekS
NXP Employee
NXP Employee

Hi Leon,

I would like to apologize for my late response.

I tested my example code on X-TWR-S12ZVL32 board.

In fact I am not sure what could be reason for error "No source available for "0x000001 (0x000001)()", but I suppose that this could be caused by any unexpected interrupt or reset – simply something what debugger do not expect. I suppose that BDM communication was interrupted.


I hope it helps you.

Have a great day,
RadekS

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
1,355 Views
leonsun65
Contributor I

Hi Radek,

Thank you for responding me. It's a debugger configuration setting issue. As a beginner, I usually just use an existing debugger configuration from an imported sample project or one from my project which may be also from a sample project. The debugger settings may be not 100% correct for my current target such as link type (BDM or P&E Multilink Universal FX) and controller (S12ZVC192 or S12ZVL32). I need to create a specific debugger settings for my target and link type. Also, sometimes I don't do a clean build. It could be a root cause too.

Best Regards,

Leon

0 Kudos
1,355 Views
RadekS
NXP Employee
NXP Employee

Hi Leon,

Thank you for notification.

I wish you good luck in future development.

Have a great day,
RadekS

0 Kudos
1,355 Views
leonsun65
Contributor I

After I started from scratch and created a S12ZVL32 bareboard project then copied those files over, the no source available error is gone. Don’t know why. Looks like I can’t import the example project and use it directly.

0 Kudos
1,355 Views
leonsun65
Contributor I

I figured out the root cause by myself. Case is closed.

0 Kudos
1,355 Views
RadekS
NXP Employee
NXP Employee

Hi Leon

Thank you for notification.

Could you please share with us the root cause?

Best Regards

RadekS

0 Kudos