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

📄 iee1.c

📁 FREESCALE 16位单片机片MC9S12DG128的EEPROM读写操作程序,支持字节读写,字读写,长字读写,由产品验证,放心使用
💻 C
📖 第 1 页 / 共 2 页
字号:
/** ###################################################################
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : IEE1.C
**     Project   : displayboard
**     Processor : MC9S12DG128BCFU
**     Beantype  : IntEEPROM
**     Version   : Bean 02.056, Driver 01.06, CPU db: 2.87.238
**     Compiler  : Metrowerks HC12 C Compiler
**     Date/Time : 2005-11-2, 10:18
**     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[4];
    word w[2];
    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[Addr&0x0003] = 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[idx])); /* 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

⌨️ 快捷键说明

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