nnimo 发表于 2014-5-28 17:42:38

【代码分享】飞思卡尔单片机MC9S12DG128的EEPROM读写程序源码

本帖最后由 FSL_TICS_ZJJ 于 2014-5-29 11:09 编辑

提供一个FRE/** ###################################################################
**   THIS BEAN MODULE IS GENERATED BY THE TOOL.
**   Filename: IEE1.C
**   Project   : displayboard
**   Processor : MC9S12DG128BCFU
**   Beantype: IntEEPROM
**   Version   : Bean 03.056, Driver 02.06, CPU db: 3.87.238
**   Compiler: Metrowerks HC12 C Compiler
**   Date/Time :
**   Abstract:
**         This device "IntEEPROM" implements internal EEPROM
**   Comment   :
**         The EEPROM array is organized as rows of word (2 bytes), the EEPROM block's
**         erase sector size is 2 rows (2 words). Therefore it is preferable
**         to use word aligned data for writting - methods SetWord and SetLong -
**         with word aligned address.
**         Driver expects that all security options of EEPROM are disabled.
**         If some security option is enabled methods performing write
**         operation (such as SetWord) can return error.
**   Settings:
**         EEPROM size               : 2048 byte
**         Initialization:
**            Wait in methods      : Disabled
**            EEPROM clock         : 181 kHz
**
**   Contents:
**         SetByte - byte IEE1_SetByte(word Addr,byte Data);
**         GetByte - byte IEE1_GetByte(word Addr,byte *Data);
**         SetWord - byte IEE1_SetWord(word Addr,word Data);
**         GetWord - byte IEE1_GetWord(word Addr,word *Data);
**         SetWait - void IEE1_SetWait(bool Wait);
**         Busy    - bool IEE1_Busy(void);
**
**   (c) Copyright UNIS, spol. s r.o. 1997-2002
**   UNIS, spol. s r.o.
**   Jundrovska 33
**   624 00 Brno
**   Czech Republic
**   http      : www.processorexpert.com
**   mail      : info@processorexpert.com
** ###################################################################*/


/* MODULE IEE1. */

#include "IEE1.h"

/* Definition of DATA and CODE segments for this bean. User can specify where
   these segments will be located on "Build options" tab of the selected CPU bean. */
#pragma DATA_SEG IEE1_DATA             /* Data section for this module. */
#pragma CODE_SEG IEE1_CODE             /* Code section for this module. */

bool IEE1_Wait ;                        /* Wait status of writting methods */
//dword tmpDword;
//word tmpWord;       //用于Word变量的返回
//byte tmpByte;       //用于Byte变量的返回

/*
** ===================================================================
**   Method      :WriteWord (bean IntEEPROM)
**
**   Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
byte WriteWord(word AddrRow,word Data16)
{
if (ESTAT_CBEIF == 0)                /* Is command buffer full ? */
    return ERR_BUSY;                   /* If yes then error */
/* ESTAT: PVIOL=1,ACCERR=1 */
ESTAT |= 48;                         /* Clear error flags */
*(volatile word *) AddrRow = Data16; /* Array address and program data */
/* ECMD: ??=0,CMDB6=0,CMDB5=1,??=0,??=0,CMDB2=0,??=0,CMDB0=0 */
ECMD = 32;                           /* Word program command */
ESTAT_CBEIF = 1;                     /* Clear flag command buffer empty */
if ((ESTAT_PVIOL == 1)||(ESTAT_ACCERR == 1)) /* Is protection violation or acces error detected ? */
    return ERR_NOTAVAIL;               /* If yes then error */
if (IEE1_Wait) {                     /* Is the flag set ? */
    while (ESTAT_CBEIF == 0);          /* If yes then wait to buffer empty */
    while (ESTAT_CCIF == 0);         /* Wait for command completition */
    if (*(volatile word *) AddrRow != Data16) /* Was attempt to write data to the given address errorneous? */
      return ERR_VALUE;                /* If yes then error */
}
return ERR_OK;
}
/*
** ===================================================================
**   Method      :WriteSector (bean IntEEPROM)
**
**   Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
byte WriteSector(word AddrSec,dword Data32)
{
byte err;                            /* Temporary variable */

if (ESTAT_CBEIF == 0)                /* Is command buffer full ? */
    return ERR_BUSY;                   /* If yes then error */
/* ESTAT: PVIOL=1,ACCERR=1 */
ESTAT |= 48;                         /* Clear error flags */
*(volatile word *) AddrSec = (word)(Data32 >> 16); /* Array address and program data - higher part */
/* ECMD: ??=0,CMDB6=1,CMDB5=1,??=0,??=0,CMDB2=0,??=0,CMDB0=0 */
ECMD = 96;                           /* Sector modify command */
ESTAT_CBEIF = 1;                     /* Clear flag command buffer empty */
if ((ESTAT_PVIOL == 1)||(ESTAT_ACCERR == 1)) /* Is protection violation or acces error detected ? */
    return ERR_NOTAVAIL;               /* If yes then error */
while (ESTAT_CBEIF == 0);            /* Wait to buffer empty */
err=WriteWord(AddrSec + 2,(word)Data32); /* Write lower part */
if (err != ERR_OK)                   /* Was attemp to write data to the given address errorneous? */
    return err;                        /* If yes then error */
if (IEE1_Wait) {                     /* Is the flag set ? */
    if (*(volatile word *) AddrSec != (word)(Data32 >> 16)) /* Was attempt to write data to the given address errorneous? */
      return ERR_VALUE;                /* If yes then error */
}
return ERR_OK;
}
/*
** ===================================================================
**   Method      :IEE1_SetByte (bean IntEEPROM)
**
**   Description :
**         Method writes given byte to the given address in EEPROM.
**   Parameters:
**         NAME            - DESCRIPTION
**         Addr            - Address to EEPROM
**         Data            - Data to write
**   Returns   :
**         ---             - Error code
** ===================================================================
*/
byte IEE1_SetByte(word Addr,byte Data)
{
union {
    byte b;
    word w;
    long l;
} backup;
byte idx;

if ((Addr < EEPROMStart)||(Addr > EEPROMEnd)) /* Is given address out of EEPROM area array ? */
    return ERR_RANGE;                  /* If yes then error */
backup.l = *(volatile dword *)(Addr & 0xFFFC); /* Load sector to variable backup */
backup.b = Data;      /* Store data to variable backup */
Addr &= 0xFFFE;                      /* Aligned address */
if (*(volatile word *)(Addr) == 0xFFFF) { /* Is given EEPROM row erased ? */
    idx = (byte) ((Addr & 0x0002) >> 1); /* Word index in sector */
    return (WriteWord(Addr,backup.w)); /* Write new content */
}
else                                 /* Is given address non-erased ? */
    return (WriteSector(Addr & 0xFFFC, backup.l)); /* If yes then write new content */
}

/*
** ===================================================================
**   Method      :IEE1_GetByte (bean IntEEPROM)
**
**   Description :
**         Method reads byte from the given EEPROM address
**   Parameters:
**         NAME            - DESCRIPTION
**         Addr            - Address to EEPROM
**       * Data            - Pointer to returned 8-bit data
**   Returns   :
**         ---             - Error code
** ===================================================================
*/
byte IEE1_GetByte(word Addr,byte *Data)
{
if ((Addr < EEPROMStart)||(Addr > EEPROMEnd)) /* Is given address out of EEPROM area array ? */
    return ERR_RANGE;                  /* If yes then error */
*Data = *(volatile byte *) Addr;   /* Return data from given address */
return ERR_OK;                     /* OK */
}

/*
** ===================================================================
**   Method      :IEE1_SetWord (bean IntEEPROM)
**
**   Description :
**         Method writes given word to the given address in EEPROM.
**   Parameters:
**         NAME            - DESCRIPTION
**         Addr            - Address to EEPROM
**         Data            - Data to write
**   Returns   :
**         ---             - Error code
** ===================================================================
*/
byte IEE1_SetWord(word Addr,word Data)
{
if ((Addr < EEPROMStart)||(Addr > (EEPROMEnd - 1))) /* Is given address out of EEPROM area array ? */
    return ERR_RANGE;                  /* If yes then error */
if (Addr & 0x0001)                   /* Aligned address ? */
    return ERR_NOTAVAIL;
if (*(volatile word *)Addr == 0xFFFF) { /* Is given EEPROM row erased ? */
    return (WriteWord(Addr,Data));   /* Write new content */
}
else {                               /* Is given address non-erased ? */
    if (Addr & 2)                      /* Is given address from low part of the sector ? */
      return (WriteSector(Addr & 0xFFFC,(dword)Data | ((*(volatile dword *)(Addr & 0xFFFC) & 0xFFFF0000))));
    else                               /* Is given address from high part of the sector ? */
      return (WriteSector(Addr,((dword)Data << 16) | (*(volatile word *)(Addr + 2))));
}
}

/*
** ===================================================================
**   Method      :IEE1_GetWord (bean IntEEPROM)
**
**   Description :
**         Method reads word from the given EEPROM address
**   Parameters:
**         NAME            - DESCRIPTION
**         Addr            - Address to EEPROM
**       * Data            - Pointer to returned 16-bit data
**   Returns   :
**         ---             - Error code
** ===================================================================
*/
byte IEE1_GetWord(word Addr,word *Data)
{
if ((Addr < EEPROMStart)||(Addr > (EEPROMEnd - 1))) /* Is given address out of EEPROM area array ? */
    return ERR_RANGE;                  /* If yes then error */
*Data = *(volatile word *) Addr;   /* Return data from given address */
return ERR_OK;                     /* OK */
}

/*
** ===================================================================
**   Method      :IEE1_SetWait (bean IntEEPROM)
**
**   Description :
**         Method changes wait status of methods SetByte, SetActByte
**         and SetPage.
**   Parameters:
**         NAME            - DESCRIPTION
**         Wait            - TRUE - methods wait till the write
**                           operation to EEPROM is finished,
**                           FALSE - methods do not wait for the end
**                           of write operation
**   Returns   : Nothing
** ===================================================================
*/
/*
void IEE1_SetWait(bool Wait)

**      This method is implemented as macro. See IEE1.h file.      **
*/

/*
** ===================================================================
**   Method      :IEE1_Busy (bean IntEEPROM)
**
**   Description :
**         Method return status of EEPROM device
**   Parameters: None
**   Returns   :
**         ---             - TRUE/FALSE - EEPROM is busy/ready
** ===================================================================
*/
/*
bool IEE1_Busy(void)

**      This method is implemented as macro. See IEE1.h file.      **
*/

/*
** ===================================================================
**   Method      :IEE1_Init (bean IntEEPROM)
**
**   Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void IEE1_Init(void)
{
ECLKDIV = 74;                        /* Set up Clock Divider Register */
IEE1_Wait = TRUE;                   /* No wait in loop to command complete */
}

/* END IEE1. */

/*
** ###################################################################
**
**   This file was created by UNIS Processor Expert 03.33 for
**   the Motorola HCS12 series of microcontrollers.
**
** ###################################################################
*/

/*****************************************************
* 在Eeprom的Addr位置写入word类型的数据Data
*****************************************************/
void WriteEepromWord(word Addr, word Data)
{
        word tmpWord;
       
    while (IEE1_Busy());
       
        IEE1_GetWord(Addr, &tmpWord);
       
        if (Data != tmpWord)
        {
          while (IEE1_Busy());
                IEE1_SetWord(Addr, Data);
        }
}



/*****************************************************
* 在Eeprom的Addr位置读取word类型的数据
*****************************************************/
word ReadEepromWord(word Addr)
{
    word tmpWord;
   
        while (IEE1_Busy());
       
        IEE1_GetWord(Addr, &tmpWord);
       
        return tmpWord;
}




/*****************************************************
* 在Eeprom的Addr位置写入byte类型的数据Data
*****************************************************/
void WriteEepromByte(word Addr, byte Data)
{
        byte tmpByte;
       
        while (IEE1_Busy());

        IEE1_GetByte(Addr, &tmpByte);

        if (Data != tmpByte)
        {
          while (IEE1_Busy());

          IEE1_SetByte(Addr, Data);
        }
}


/*****************************************************
* 在Eeprom的Addr位置读取byte类型的数据
*****************************************************/
byte ReadEepromByte(word Addr)
{
    byte tmpByte;
   
        while (IEE1_Busy());
       
        IEE1_GetByte(Addr, &tmpByte);
       
        return tmpByte;
}



/*
** ===================================================================
**   Method      :IEE2_SetLong (bean IntEEPROM)
**
**   Description :
**         Method writes given long word to the given address in
**         EEPROM.
**   Parameters:
**         NAME            - DESCRIPTION
**         Addr            - Address to EEPROM
**         Data            - Data to write
**   Returns   :
**         ---             - Error code
** ===================================================================
*/
byte IEE2_SetLong(word Addr,dword Data)
{
if ((Addr < EEPROMStart)||(Addr > (EEPROMEnd - 3))) /* Is given address out of EEPROM area array ? */
    return ERR_RANGE;                  /* If yes then error */
if (Addr & 0x0003)                   /* Aligned address ? */
    return ERR_NOTAVAIL;
return (WriteSector(Addr,Data));   /* Write new content of given sector */
}

/*
** ===================================================================
**   Method      :IEE2_GetLong (bean IntEEPROM)
**
**   Description :
**         Method reads long word from the given EEPROM address
**   Parameters:
**         NAME            - DESCRIPTION
**         Addr            - Address to EEPROM
**       * Data            - Pointer to returned 32-bit data
**   Returns   :
**         ---             - Error code
** ===================================================================
*/
byte IEE2_GetLong(word Addr,dword *Data)
{
if ((Addr < EEPROMStart)||(Addr > (EEPROMEnd - 3))) /* Is given address out of EEPROM area array ? */
    return ERR_RANGE;                  /* If yes then error */
*Data = *(volatile dword *) Addr;    /* Return data from given address */
return ERR_OK;                     /* OK */
}




/*****************************************************
* 在Eeprom的Addr位置写入lonh类型的数据Data
*****************************************************/
void WriteEepromDword(word Addr, dword Data)
{
        dword tmpDword;
       
    while (IEE1_Busy());
        IEE2_GetLong(Addr, &tmpDword);
        if (Data != tmpDword)
        {
          while (IEE1_Busy());
                IEE2_SetLong(Addr, Data);
        }
}


ESCALE 16位单片机片MC9S12DG128的EEPROM读写操作程序,支持字节读写,字读写,长字读写,比较实用 .

FSL_TICS_ZJJ 发表于 2014-5-29 11:09:18

楼主,你分享代码,就用代码分享,这里没有总结经验哦。

jiang887786 发表于 2014-9-3 14:27:00

楼主这个是PE生成的吧,谢谢分享!

rockyyangyang 发表于 2014-9-4 10:38:00

mark                  
页: [1]
查看完整版本: 【代码分享】飞思卡尔单片机MC9S12DG128的EEPROM读写程序源码