www.pudn.com > Bit1611_demo_code.rar > EEPROM.C


/* ********************************************************************** 
 
         Copyright (c) 2002-2006 Beyond Innovation Technology Co., Ltd 
 
        All rights are reserved. Reproduction in whole or in parts is 
    prohibited without the prior written consent of the copyright owner. 
   ---------------------------------------------------------------------- 
 
    Module: EEPROM.C 
 
    Purpose: Implementation of EEPROM module. 
 
    Version: 0.01                                   07:24PM  2005/05/13 
 
    Compiler: Keil 8051 C Compiler v8.01 
 
    Reference: 
    [1] AN709 System Level Design Considerations When Using I2C Serial EEPROM 
        Devices, 1999 Microchip Technology Inc. 
 
   ---------------------------------------------------------------------- 
    Modification: 
 
    R0.01 07:24PM  2005/05/13 Jeffrey Chang 
    Reason: 
        1. Original. 
    Solution: 
  
   ********************************************************************** */ 
 
#define _EEPROM_C_ 
 
/* ------------------------------------ 
    Header Files 
   ------------------------------------ */ 
 
#include "eeprom.h" 
#include "i2c.h" 
#include "platform.h" 
#include "timer.h" 
 
 
/* ------------------------------------ 
    Macro Definitions 
   ------------------------------------ */ 
 
 
/* ------------------------------------ 
    Type Definitions 
   ------------------------------------ */ 
 
 
/* ------------------------------------ 
    Variables Definitions 
   ------------------------------------ */ 
 
/* ------------------------------------ 
    Function Prototypes 
   ------------------------------------ */ 
 
 
/* ------------------------------------------------------------------- 
    Name: EEPROM_Default - 
    Purpose: 
        This function writes default values of data structures to  
        specific address of EEPROM block. 
    Passed: None. 
    Returns: None. 
    Notes: 
   ------------------------------------------------------------------- */ 
void EEPROM_Default (void) 
{ 
    EEPROM_TxBurst(EGD_ADDR, 
                   sizeof(tsEGD_Default), 
                   (UB8 *)&tsEGD_Default); 
 
    EEPROM_TxBurst(EYD_ADDR, 
                   sizeof(tsEYD_Default), 
                   (UB8 *)&tsEYD_Default); 
} /* EEPROM_Default */ 
 
 
/* ------------------------------------------------------------------- 
    Name: EEPROM_Init - 
    Purpose: To initialize the EEPROM module. 
    Passed: None. 
    Returns: None. 
    Notes: 
   ------------------------------------------------------------------- */ 
void EEPROM_Init (void) 
{ 
    // ++++++++++++++++++++++++++++++++ 
    // [JC011] Added by JC 03:15PM  2006/04/26 
    // [1]2 Forcing EEPROM internal reset via software 
    I2C_Start(); 
 
    I2C_TxData(0xFF); 
    I2C_SetNAK(); 
 
    I2C_Start(); 
    I2C_Stop(); 
    // ++++++++++++++++++++++++++++++++ 
} /* EEPROM_Init */ 
 
 
#if (EEPROM_RX_BURST) 
/* ------------------------------------------------------------------- 
    Name: EEPROM_RxBurst - 
    Purpose: To receive bulk data from I2C EEPROM. 
    Passed: 
        UW16 wAddr  = EEPROM address. 
        UB8  bCnt   = The number of data which will be received 
                    excluding EEPROM address (bCnt: 1..255). 
        UB8  *pbData    = The pointer which points to the first data item. 
    Returns: None. 
    Notes: 
        The EEPROM_MAD should be the slave device's 'WRITE' module addres, 
        not 'READ' one. 
   ------------------------------------------------------------------- */ 
void EEPROM_RxBurst ( 
UW16 wAddr,         /* EEPROM address */ 
UB8  bCnt,          /* The number of data which will be transmitted */ 
UB8  *pbData        /* Point to the first DATA item */ 
) 
{ 
    UB8     bIdx; 
 
 
 
    I2C_Start(); 
 
    /* -------------------------------- 
        Write 
       -------------------------------- */ 
    I2C_TxData(EEPROM_MAD | ((wAddr >> 8) << 1)); 
    I2C_GetACK(); 
 
    I2C_TxData(wAddr & 0x00FF); 
    I2C_GetACK(); 
 
    /* -------------------------------- 
        Read 
       -------------------------------- */ 
    I2C_Start(); 
    I2C_TxData(EEPROM_MAD | ((wAddr >> 8) << 1) | 0x01); 
    I2C_GetACK(); 
 
 
    for (bIdx = 0; bIdx < (bCnt - 1); bIdx++) 
    { 
        *pbData++ = I2C_RxData(); 
 
        I2C_SetACK(); 
    } /* for */ 
 
 
    /* -------------------------------- 
        Last Read 
       -------------------------------- */ 
    *pbData = I2C_RxData(); 
    I2C_SetNAK(); 
 
    I2C_Stop(); 
 
} /* EEPROM_RxBurst */ 
#endif 
 
 
#if (EEPROM_RX_BYTE) 
/* ------------------------------------------------------------------- 
    Name: EEPROM_RxByte - 
    Purpose: 
        To receive one byte data from I2C slave device. 
 
    Passed: 
        wAddr = EEPROM address. 
 
    Returns: 
        The one byte data. 
 
    Notes: 
        The bSLA should be the slave device's 'WRITE' module addres, 
        not 'READ' one. 
   ------------------------------------------------------------------- */ 
UB8 EEPROM_RxByte (UW16 wAddr) 
{ 
    UB8 bData; 
 
 
    I2C_Start(); 
 
    /* -------------------------------- 
        Write 
       -------------------------------- */ 
    /* 'WRITE' MAD and High byte of address */ 
    I2C_TxData(EEPROM_MAD | ((wAddr >> 8) << 1)); 
    I2C_GetACK(); 
 
    /* Low byte of address */ 
    I2C_TxData(wAddr & 0x00FF); 
    I2C_GetACK(); 
 
    /* -------------------------------- 
        Read 
       -------------------------------- */ 
    I2C_Start(); 
    I2C_TxData(EEPROM_MAD | ((wAddr >> 8) << 1) | 0x01); 
    I2C_GetACK(); 
 
    /* -------------------------------- 
        Last Read 
       -------------------------------- */ 
    bData = I2C_RxData(); 
    I2C_SetNAK(); 
 
    I2C_Stop(); 
 
    return( bData ); 
 
} /* EEPROM_RxByte */ 
#endif 
 
 
#if (EEPROM_TX_BURST) 
/* ------------------------------------------------------------------- 
    Name: EEPROM_TxBurst - 
    Purpose: 
        To transmit bulk data to I2C EEPROM. 
 
    Passed: 
        UW16 wAddr  = EEPROM address. 
        UB8  bCnt   = The number of data which will be transmitted 
                    excluding EEPROM address (bCnt: 1..255). 
        UB8  *pbData    = The pointer which points to the first data item. 
 
    Returns: None. 
    Notes: 
   ------------------------------------------------------------------- */ 
void EEPROM_TxBurst ( 
UW16 wAddr,         /* EEPROM address */ 
UB8  bCnt,          /* The number of data which will be transmitted */ 
UB8  *pbData        /* Point to the first DATA item */ 
) 
{ 
    for (   ; bCnt; bCnt--) 
    { 
        I2C_TxByte(EEPROM_MAD | ((wAddr >> 8) << 1), wAddr++, *pbData++); 
 
        TIMER_DelayMS(EEPROM_WRITE_CYCLE_TIME); 
    } // for 
} /* EEPROM_TxBurst */ 
#endif 
 
 
#if (EEPROM_TX_BYTE) 
/* ------------------------------------------------------------------- 
    Name: EEPROM_TxByte - 
    Purpose: 
        To transmit one byte data to I2C slave device. 
 
    Passed: 
    Returns: None. 
    Notes: 
        The bSLA should be the slave device's 'WRITE' module addres, 
        not 'READ' one. 
   ------------------------------------------------------------------- */ 
void EEPROM_TxByte ( 
UW16    wAddr,         /* I2C sub-address */ 
UB8     bData          /* Data item */ 
) 
{ 
    I2C_Start(); 
 
    I2C_TxData(EEPROM_MAD | ((wAddr >> 8) << 1)); 
    I2C_GetACK(); 
 
    I2C_TxData(wAddr & 0x00FF); 
    I2C_GetACK(); 
 
    I2C_TxData(bData); 
    I2C_GetACK(); 
 
    I2C_Stop(); 
 
    TIMER_DelayMS(EEPROM_WRITE_CYCLE_TIME); 
} /* EEPROM_TxByte */ 
#endif 
 
 
 
/* ------------------------------------------------------------------- 
    Name:  - 
    Purpose: . 
    Passed: None. 
    Returns: None. 
    Notes: 
   ------------------------------------------------------------------- */ 
 
 
/* ********************************************************************** 
 
    Description: 
 
 
   ********************************************************************** */ 
 
/* %% End Of File %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */