AnsweredAssumed Answered

S12Z EEPROM Read and write

Question asked by Charudatta Ingale on Jul 27, 2015
Latest reply on Oct 5, 2015 by Radek Sestak

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;

}

Outcomes