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

📄 iee1.c

📁 飞思卡尔智能车的程序。红外循迹
💻 C
字号:
/** ###################################################################
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : IEE1.C
**     Project   : che_free
**     Processor : MC9S12DG128BCPV
**     Beantype  : IntEEPROM
**     Version   : Bean 02.078, Driver 01.14, CPU db: 2.87.280
**     Compiler  : Metrowerks HC12 C Compiler
**     Date/Time : 2008-3-20, 下午 08:26
**     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
**         Virtual page size           : 4 byte
**         Initialization:
**              Wait in methods        : Enabled
**              EEPROM clock           : 181 kHz
**
**     Contents  :
**         SetByte - byte IEE1_SetByte(IEE1_TAddress Addr,byte Data);
**         GetByte - byte IEE1_GetByte(IEE1_TAddress Addr,byte *Data);
**         SetWord - byte IEE1_SetWord(IEE1_TAddress Addr,word Data);
**         GetWord - byte IEE1_GetWord(IEE1_TAddress Addr,word *Data);
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2005
**     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"

#pragma DATA_SEG IEE1_DATA
#pragma CODE_SEG IEE1_CODE

static byte Page[IEE1_PageSize];       /* Virtual page */

/*
** ===================================================================
**     Method      :  WriteWord (bean IntEEPROM)
**
**     Description :
**         The method writes the word data to an EEPROM memory.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static byte WriteWord(IEE1_TAddress 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 */
  }
  while ((ESTAT & 0xC0) != 0xC0) {}    /* 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;                       /* OK */
}

/*
** ===================================================================
**     Method      :  WriteSector (bean IntEEPROM)
**
**     Description :
**         The method writes the dword sector data to an EEPROM memory.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static byte WriteSector(IEE1_TAddress 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 (*(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 a given byte to a specified address in
**         EEPROM.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Addr            - Address to EEPROM
**         Data            - Data to write
**     Returns     :
**         ---             - Error code, possible codes:  
**                           - ERR_OK 
**                           - ERR_SPEED 
**                           - ERR_BUSY 
**                           - ERR_VALUE 
**                           - ERR_NOTAVAIL 
**                           - ERR_RANGE
** ===================================================================
*/
byte IEE1_SetByte(IEE1_TAddress 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 */
  }
  if ((ESTAT & 192) != 192) {          /* Is reading from EEPROM possible? */
    return ERR_BUSY;                   /* If no 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,(dword)backup.l)); /* If yes then write new content */
  }
}

/*
** ===================================================================
**     Method      :  IEE1_GetByte (bean IntEEPROM)
**
**     Description :
**         Method reads a byte from a specified EEPROM address
**     Parameters  :
**         NAME            - DESCRIPTION
**         Addr            - EEPROM Address
**       * Data            - A pointer to the returned 8-bit data
**     Returns     :
**         ---             - Error code, possible codes:  
**                           - ERR_OK 
**                           - ERR_BUSY 
**                           - ERR_RANGE
** ===================================================================
*/
byte IEE1_GetByte(IEE1_TAddress Addr,byte *Data)
{
  if ((Addr < EEPROMStart)||(Addr > EEPROMEnd)) { /* Is given address out of EEPROM area array ? */
    return ERR_RANGE;                  /* If yes then error */
  }
  if ((ESTAT & 192) != 192) {          /* Is reading from EEPROM possible? */
    return ERR_BUSY;                   /* If no then error */
  }
  *Data = *(volatile byte *) Addr;     /* Return data from given address */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  IEE1_SetWord (bean IntEEPROM)
**
**     Description :
**         Method writes a given word to the specified address in
**         EEPROM.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Addr            - Address to EEPROM
**         Data            - Data to write
**     Returns     :
**         ---             - Error code, possible codes:  
**                           - ERR_OK 
**                           - ERR_SPEED 
**                           - ERR_BUSY 
**                           - ERR_VALUE 
**                           - ERR_NOTAVAIL 
**                           - ERR_RANGE
** ===================================================================
*/
byte IEE1_SetWord(IEE1_TAddress Addr,word Data)
{
  if ((Addr < EEPROMStart)||(Addr > EEPROMEnd)) { /* Is given address out of EEPROM area array ? */
    return ERR_RANGE;                  /* If yes then error */
  }
  if (Addr & 0x0001) {                 /* Aligned address ? */
    return ERR_NOTAVAIL;
  }
  if ((ESTAT & 192) != 192) {          /* Is reading from EEPROM possible? */
    return ERR_BUSY;                   /* If no then error */
  }
  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 | ((*(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 a word from the specified EEPROM address
**     Parameters  :
**         NAME            - DESCRIPTION
**         Addr            - Address to EEPROM
**       * Data            - Pointer to returned 16-bit data
**     Returns     :
**         ---             - Error code, possible codes:  
**                           - ERR_OK 
**                           - ERR_BUSY 
**                           - ERR_RANGE
** ===================================================================
*/
byte IEE1_GetWord(IEE1_TAddress Addr,word *Data)
{
  if ((Addr < EEPROMStart)||(Addr > EEPROMEnd)) { /* Is given address out of EEPROM area array ? */
    return ERR_RANGE;                  /* If yes then error */
  }
  if ((ESTAT & 192) != 192) {          /* Is reading from EEPROM possible? */
    return ERR_BUSY;                   /* If no then error */
  }
  *Data = *(volatile word *)(Addr);    /* Return data from given address */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  IEE1_Init (bean IntEEPROM)
**
**     Description :
**         Initializes the associated peripheral(s) and the bean internal 
**         variables. The Method is called automatically as a part of the 
**         application initialization code
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void IEE1_Init(void)
{
  byte i;                              /* Temporary variable */

  ECLKDIV = 74;                        /* Set up Clock Divider Register */
  for(i = 0; i < IEE1_PageSize; i++) { /* Fill virtual page */
    Page[i] = 255;
  }
}

/* END IEE1. */

/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 2.96 [03.76]
**     for the Freescale HCS12 series of microcontrollers.
**
** ###################################################################
*/

⌨️ 快捷键说明

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