⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mem_handler.c

📁 飞思卡尔车身控制技术研讨会资料范例 基于LIN的收发通信
💻 C
字号:
/*******************************************************************************/
/**
Copyright (c) 2008 Freescale Semiconductor
Freescale Confidential Proprietary
\file       Mem_Handler.c
\brief      Memory Management functions.
\author     Freescale Semiconductor
\author     B05114
\version    0.1
\date       January/ 2008
*/
/*******************************************************************************/

#include "derivative.h" 
#include "typedefs.h"  
#include "Mem_handler.h" 
#include "EEPROM_Drv.h"
//#include "checksum.h"
#include <string.h> //for memcpy function

/* -- Variables --------------------------------------------------------*/

/* Shadow RAM Checksum */
volatile UINT16 gu16RAMChecksum;

/* EEPROM flags, used to determine which specific eeprom sectors are to be updated*/
UINT32 gu32EEPROMflagsL;
UINT32 gu32EEPROMflagsH;

/* Shadow RAM definition */
struct tCalibrations sShadow_RAM_Cals;

/* Shadow ROM Calibration Values*/
#pragma CONST_SEG SHADOW_ROM
const struct tCalibrations sShadow_ROM_Cals =
{
{0xAAFFFFFF},           /* UINT32   u32Cal_EEPROM_Restore. this value has to be in its own sector*/
{MEM_FILL_COUNTERS},
{MEM_FILL_LEARNCODE},                   
0x00,
0x00,
0x00,
0x00                  
};
#pragma CONST_SEG DEFAULT

/* Shadow EEPROM definition*/
#pragma DATA_SEG SHADOW_EEPROM
struct tCalibrations sShadow_EEPROM_Cals;
 
#pragma DATA_SEG DEFAULT
/* BCM Memory Status flags*/
struct tMemoryStatus sBCM_Mem_Status;

/*******************************************************************************/
/**
* \brief    vfnMemory_Init - Perform first Shadow EEPROM and Shadow RAM initial initialization
* \author   B01246/ B07297
* \param    void
* \return   void
* \todo     
*/
void vfnMemory_Init(void)
{ 
    sBCM_Mem_Status.bfShadowEEPROM_Update =  NO_REQUEST; 
    
    /* Perform Shadow EEPROM Initialization */
    vfnShadowEEPROM_Init();
    
    /*Declare EEPROM Non-dirty*/
    sBCM_Mem_Status.bfDirtyMemory=0;
    
    /* Perform Shadow RAM Initialization */
    vfnShadowRAM_Init();
    
    /*Initialize checksum value for RAM*/
    gu16RAMChecksum=u16checksum((UINT8 *)&sShadow_RAM_Cals,sizeof(sShadow_RAM_Cals),1);
}

/*******************************************************************************/
/**
* \brief    vfnShadowEEPROM_Init 
* \author   B01246
* \param    void
* \return   void
* \brief    
* \brief    This function performs three actions depending on the state of
            the bits sBCM_Mem_Status.bfShadowEEPROM_Update

            NO_REQUEST : This is state by default after RESET.
                         If the first byte of the structure sShadow_EEPROM_Cals is different
                         than the value 0xAA, The calibration data is erased from eeprom and a 
                         request for update is submitted. 
                         
            REQUEST    : This state will request to program the contents of Calibration data in FLASH into
                         the EEPROM copy. 
                         
    PENDING_COMPLETION : This is a wait state valid while the EEPROM is busy. 

* \todo     
*/
void vfnShadowEEPROM_Init(void)
{
    /* Local variable to read u8EEPROM_Restore flag from Shadow EEPROM */
    UINT8 u8EEPROM_Restore;
    UINT8 u8size;
    /* Status of EE_GetByte function call */
    static UINT8 u8EEPROM_Status;

    /* If, and only if, EEPROM is not being accessed, assess initialization state of Shadow EEPROM */
    if ( (IsEEPROMBusy() == EEPROM_READY)  && (sBCM_Mem_Status.bfBackupShadowRAM == NO_BACKUP_REQUEST) )
    {    
        switch(sBCM_Mem_Status.bfShadowEEPROM_Update)
        {
        case (NO_REQUEST):
            u8EEPROM_Status = EE_GetByte((UINT16)&sShadow_EEPROM_Cals.u32Cal_EEPROM_Restore,&u8EEPROM_Restore);
            /* Verify EEPROM restore flag, if predefined value is not there, initialize whole Shadow EEPROM */
            if ((u8EEPROM_Restore != 0xAA) && (u8EEPROM_Status == ERR_OK))
            {
                /* Shadow EEPROM initialized flag needs to be invalidated*/
                sBCM_Mem_Status.bfShadowEEPROM_Initialized = 0;
                
                u8size=sizeof(sShadow_EEPROM_Cals);
                if ( (u8size&0x0003) != 0)
                  {
                  u8size=u8size/4 +1;
                  }
                 else 
                  {
                  u8size=u8size/4;
                  }

                if (u8SectorEraseEEPROM((UINT16)&sShadow_EEPROM_Cals,u8size) == ERR_OK)
                {
                    sBCM_Mem_Status.bfShadowEEPROM_Update = REQUEST;
                }
            }
            else
            {
                sBCM_Mem_Status.bfShadowEEPROM_Initialized = 1; 
            }

            break;
        
        case (REQUEST):
            /*Schedule copy process from Shadow ROM */
            /*Do not program the 0xAA value at this point*/
            if (u8ByteWriteOnlyEEPROM( (UINT16)&sShadow_EEPROM_Cals+4, (UINT8 *)&sShadow_ROM_Cals +4, 
                    sizeof(sShadow_EEPROM_Cals)) == ERR_OK)
            {
                sBCM_Mem_Status.bfShadowEEPROM_Update = PENDING_COMPLETION;
            }
            break;
            
        case (PENDING_COMPLETION):
            /* Program the 0xAA value last, to avoid problems in a powerdown scenario*/
            if (u8ByteWriteOnlyEEPROM( (UINT16)&sShadow_EEPROM_Cals, (UINT8*)&sShadow_ROM_Cals,
                    1) == ERR_OK)
            {
            sBCM_Mem_Status.bfShadowEEPROM_Update = END_EEPROM_INIT;
            }
            break;
            
        case (END_EEPROM_INIT):
        
            /* Set EEPROM_Initialized flag */
            sBCM_Mem_Status.bfShadowEEPROM_Initialized = 1;
            sBCM_Mem_Status.bfShadowEEPROM_Update = NO_REQUEST;
            

            
        default:
            sBCM_Mem_Status.bfShadowEEPROM_Update = NO_REQUEST;
            break;
        }
    }
}

/*******************************************************************************/
/**
* \brief    vfnShadowRAM_Init - Validate Shadow RAM initialization.
* \author   B01246/ B07297
* \param    void
* \return   void
* \todo     
*/
void vfnShadowRAM_Init(void)
{       
  /*Inhibit RAM update if EEPROM is dirty*/
  if (sBCM_Mem_Status.bfDirtyMemory != 1)
  {
      /* Status of memcpy function call */
      struct tCalibrations *sdummy ;
      
      /*By default invalidate Shadow RAM initialization flag */
      sBCM_Mem_Status.bfShadowRAM_Initialized = 0;
      
      /* If Shadow EEPROM is initialized, then initalize Shadow RAM from Shadow EEPROM (latest values) */
      if (sBCM_Mem_Status.bfShadowEEPROM_Initialized == 1)
      {
          sdummy = memcpy(&sShadow_RAM_Cals,&sShadow_EEPROM_Cals,sizeof(sShadow_EEPROM_Cals));
          
          /* If both memory maps match, set Shadow EEPROM initialization flag */
          if (memcmp(&sShadow_EEPROM_Cals,&sShadow_RAM_Cals,sizeof(sShadow_EEPROM_Cals)) == 0)
          {
              /* Shadow RAM is now ready, set appropriate flag */
              sBCM_Mem_Status.bfShadowRAM_Initialized = 1;
          }    
      }
      /* If Shadow EEPROM is NOT initalized, then initialize Shadow RAM from Flash (default settings) */
      else
      {
          sdummy = memcpy(&sShadow_RAM_Cals,&sShadow_ROM_Cals,sizeof(sShadow_ROM_Cals));
          
          /* Only if both memory maps match set Shadow EEPROM initialization flag and clear UpdateRequest flag */
          if (memcmp(&sShadow_ROM_Cals,&sShadow_RAM_Cals,sizeof(sShadow_ROM_Cals)) == 0)
          {
              /* Shadow RAM is now ready, set appropriate flag */
              sBCM_Mem_Status.bfShadowRAM_Initialized = 1;
          }
      }
  }
}


/*******************************************************************************/
/**
* \brief    vfnBackUpShadowRAM - Copy content of Shadow RAM into ShadowEEPROM.
* \author   B01246
* \param    void
* \return   void
* \todo     
*/
void vfnBackUpShadowRAM(void)
{        
    static UINT32 lu32EEPROMflagsH; 
    static UINT32 lu32EEPROMflagsL; 
    volatile static UINT8  lu8sectornumber;
    static UINT16 dest, offset;
    static UINT8* source;
    static UINT8  restore = 0xAA;
    UINT32 temp;
    
    /* Run state machine only if eeprom not busy*/
    if (IsEEPROMBusy() == EEPROM_READY)
    {    
       
        switch(sBCM_Mem_Status.bfBackupShadowRAM)
        {
        
        case (START):
            /*check if any flag is set (coherency)*/
            if ((gu32EEPROMflagsL != 0)||(gu32EEPROMflagsH != 0))
              {
             
                /*erase the eeprom restore value indicating that eeprom is not valid anymore*/
                /*this is done should there be a powerdown in the middle of an update*/
                if (u8SectorEraseEEPROM((UINT16)&sShadow_EEPROM_Cals.u32Cal_EEPROM_Restore, 1) == ERR_OK)
                {
                  /* do snapshot of flags at the time of request*/
                  lu32EEPROMflagsH= gu32EEPROMflagsH;
                  lu32EEPROMflagsL= gu32EEPROMflagsL;
                  
                  /*clean global flags*/
                  gu32EEPROMflagsH=0;
                  gu32EEPROMflagsL=0;
                  
                  
                  
                  sBCM_Mem_Status.bfBackupShadowRAM = ERASE;
                }
              }
             else
              {
              sBCM_Mem_Status.bfBackupShadowRAM = END;
              }
            
        break;
        
        case (ERASE):
            lu8sectornumber=0;
            
            if (lu32EEPROMflagsL==0)
              { /*scan high part*/ 
              while ( !(lu32EEPROMflagsH & (UINT32)((UINT32)1<<lu8sectornumber)) )
                {
                lu8sectornumber++;
                }
                lu8sectornumber+=32;
              }
             else
              {
              /*scan low part*/
              while ( !(lu32EEPROMflagsL & (UINT32)((UINT32)1<<lu8sectornumber)) )
              lu8sectornumber++;
              }
              
            
             /*At thispoint, lu8sectornumber should contain the sector number to be erased*/
             offset = 4*lu8sectornumber;
             dest   = ((UINT16) &sShadow_EEPROM_Cals + offset);
             source = ((UINT8*) &sShadow_RAM_Cals + offset);
              
              if (u8SectorEraseEEPROM( dest,1/*1 sector*/) == ERR_OK)
              {
                sBCM_Mem_Status.bfBackupShadowRAM = PROGRAM;
              }
               
           break;
           
        case (PROGRAM):
            /*program sector lu8sectornumber*/
            if (u8ByteWriteOnlyEEPROM(  dest,source,4 /*sector size*/)== ERR_OK)
            {
                    /*put down the detected sector flag*/
                    if(lu8sectornumber>31)
                    lu32EEPROMflagsH &=~(UINT32)((UINT32)1<<(lu8sectornumber-32));
                    else{
                      
                    temp=(UINT32)((UINT32)1<<lu8sectornumber);
                    //lu32EEPROMflagsL &=~(UINT32)((UINT32)1<<lu8sectornumber);
                    lu32EEPROMflagsL &=~temp;
                    }
                    
                    if ((lu32EEPROMflagsL == 0)&&(lu32EEPROMflagsH == 0))
                      {
                      /*if no more sectors are pending*/
                      sBCM_Mem_Status.bfBackupShadowRAM= END;  
                      }
                    else
                      {
                      /*do next sector*/
                      sBCM_Mem_Status.bfBackupShadowRAM= ERASE;
                      }
            }
            break;

        case (END):
            /*restore eeprom valid value*/
            if (u8ByteWriteOnlyEEPROM((UINT16)&sShadow_EEPROM_Cals.u32Cal_EEPROM_Restore,&restore,1)== ERR_OK)
              {
              sBCM_Mem_Status.bfDirtyMemory=0;
              sBCM_Mem_Status.bfBackupShadowRAM = NO_BACKUP_REQUEST;
              }
              
              break;    
              
        case (NO_BACKUP_REQUEST):
            break;
            
        default:
            sBCM_Mem_Status.bfBackupShadowRAM =NO_BACKUP_REQUEST;
            break;
        }
    }
}



  
/*******************************************************************************/
/*                                                                             */
/* All software, source code, included documentation, and any implied know-how */
/* are property of Freescale Semiconductor and therefore considered            */ 
/* CONFIDENTIAL INFORMATION.                                                   */
/*                                                                             */
/* This confidential information is disclosed FOR DEMONSTRATION PURPOSES ONLY. */
/*                                                                             */
/* All Confidential Information remains the property of Freescale Semiconductor*/
/* and will not be copied or reproduced without the express written permission */
/* of the Discloser, except for copies that are absolutely necessary in order  */
/* to fulfill the Purpose.                                                     */
/*                                                                             */
/* Services performed by FREESCALE in this matter are performed AS IS and      */
/* without any warranty. CUSTOMER retains the final decision relative to the   */
/* total design and functionality of the end product.                          */
/*                                                                             */
/* FREESCALE neither guarantees nor will be held liable by CUSTOMER for the    */
/* success of this project.                                                    */
/*                                                                             */
/* FREESCALE disclaims all warranties, express, implied or statutory including,*/
/* but not limited to, implied warranty of merchantability or fitness for a    */
/* particular purpose on any hardware, software ore advise supplied to the     */
/* project by FREESCALE, and or any product resulting from FREESCALE services. */
/*                                                                             */
/* In no event shall FREESCALE be liable for incidental or consequential       */
/* damages arising out of this agreement. CUSTOMER agrees to hold FREESCALE    */
/* harmless against any and all claims demands or actions by anyone on account */
/* of any damage,or injury, whether commercial, contractual, or tortuous,      */
/* rising directly or indirectly as a result of the advise or assistance       */
/* supplied CUSTOMER in connectionwith product, services or goods supplied     */
/* under this Agreement.                                                       */
/*                                                                             */
/*******************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -