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

📄 at24c02.c

📁 sonata2standardsourcedriver的EEProm读写方案
💻 C
📖 第 1 页 / 共 2 页
字号:
//=============================================================================// File Name: at24c01a_02.c//// Copyright 2000 Cirrus Logic Corporation//// Description://  The API for the AT24C01A and AT24C02 EEPROM's.//// Modification History://  02/5/2001 JGT - created//  7/17/2002 JTC - adapted AT24C01 code to work for the AT24C01A////============================================================================#include "apitypes.h"//#define DEBUG_MSG#include "eromdbg.h"#include "dvhw32.h"#include "eeprom.h"#include "i2capi.h"#include "osapi.h"//-----------------------------------------------------------------------------// External definitions//-----------------------------------------------------------------------------extern void   Delay(int n);//-----------------------------------------------------------------------------// Local definitions//-----------------------------------------------------------------------------#define AT_DELAY        50#define AT_DEVICE_ADDR  (0x50<<1)#define AT_WAIT_ACK()               \    if ( !At24c02_Check_Ack() )     \    {                               \        ProtectionEnd();            \        ErrPrint("Error waiting for Ack\n");\        return FAIL;            \    }#define AT_15MS_DELAY  (250000)#define AT_PAGESIZE    8//-----------------------------------------------------------------------------// Local variables//-----------------------------------------------------------------------------static Uint32 m_lockCount = 0;//-----------------------------------------------------------------------------// Local functions//-----------------------------------------------------------------------------static Boolean  At24c02_Check_Ack();static void     At24c02_StartCode();static void     At24c02_StopCode();static Byte     At24c02_ReadByte( );static void     At24c02_SendByte( Byte value );static int      At24c02_WritePage(Uint32 addr, Byte *data_ptr);static void     ProtectionStart(void);static void     ProtectionEnd(void);//================= GLOBAL FUNCTIONS =====================//      ||                                 ||//      ||                                 ||//      ||                                 ||//   ---  ---                           ---  ---//   \      /                           \      ///    \    /                             \    ///     \  /                               \  ///      \/                                 \///int EEPROM_GetSize(Uint32 *size){	*size = 256;	return SUCCESS;}//-----------------------------------------------------------------------------// Function:int EEPROM_Write8(//// Description://  This operation performs an 8 bit (byte) write to the AT24C02 eeprom.//// Parmeters:    Uint32 addr, // address to be written to    Byte data    // data byte to be written)////  Returns://          SUCCESS = 0 or FAIL////----------------------------------------------------------------------------{    ProtectionStart();    // this sets the pins as i/o pins    I2C_ConfigurePort(TRUE);    DbgPrint("EEPROM: writing %0.2x @ %0.2x\n",data,addr);    // signal start    At24c02_StartCode();    At24c02_SendByte( AT_DEVICE_ADDR ); // send address    AT_WAIT_ACK();    // write address MSB first    At24c02_SendByte( addr ); // send address    AT_WAIT_ACK();    At24c02_SendByte( data ); // send data    AT_WAIT_ACK();    // signal stop    At24c02_StopCode();    I2C_ConfigurePort(FALSE);    ProtectionEnd();    return SUCCESS;} // end EEPROM_Write8//-----------------------------------------------------------------------------// Function:int EEPROM_Write32(//// Description://  This operation performs a 32 bit write to the AT24C02 eeprom. This used to be//  a page write for the at24c01, but for the at24c02 a page write is 8 bytes, not//  4.  This function is deprecated, and included for backwards compatibility with//  external functions that may call it.  The preferred method for writing is to call//  At24c02_BufWrite().////// Parmeters:    Uint32 addr,    // address to be written to    Uint32 data     // data double word (32 bits) to be written)////  Returns://          SUCCESS = 0 or FAIL////----------------------------------------------------------------------------{    Uint8 i;    Byte * bytePtr = (Byte *)&data;    ProtectionStart();    // this sets the pins as i/o pins    I2C_ConfigurePort(TRUE);    DbgPrint("--- EEPROM: Write32 called for addr %0.2x  mode: %02x---\n", addr, I2C_GetModeRegister());    // just mimic the old page write mode by writing 4 bytes in a loop    for (i = 0; i < 4; i++)    {        EEPROM_Write8(addr++, *(bytePtr++));//        Delay(AT_15MS_DELAY);                     // give the eeprom time to write    }    I2C_ConfigurePort(FALSE);    ProtectionEnd();    return SUCCESS;} // end EEPROM_Write32//-----------------------------------------------------------------------------// Function:int EEPROM_BufWrite(//// Description://  This operation writes the buffer to the AT24C02 at the given address.//// Parmeters:*/    Uint32 addr,    // address to be written to    void * bufPtr,  // pointer to the source buffer pointer    Uint32 length   // length in bytes of data to be written)////  Returns://          SUCCESS = 0 or FAIL////----------------------------------------------------------------------------{    Uint32 i;    int status = SUCCESS;    ProtectionStart();    // this sets the pins as i/o pins    I2C_ConfigurePort(TRUE);    DbgPrint("EEPROM_BufWrite start---- mode: %02x\n", I2C_GetModeRegister());    // make the possible page writes    for ( i = 0; (i < length / AT_PAGESIZE) && !status; i++ )    {        status = At24c02_WritePage( addr, (Byte *)bufPtr );        (Byte *)bufPtr += AT_PAGESIZE;        Delay(AT_15MS_DELAY);                     // give the eeprom time to write        addr += AT_PAGESIZE;    } // end for    // write the remaining bytes    for ( i = 0; (i < length % AT_PAGESIZE) && !status; i++ )    {        status = EEPROM_Write8( addr++, *(Byte*)bufPtr++ );        Delay(AT_15MS_DELAY);                     // give the eeprom time to write    } // end for    I2C_ConfigurePort(FALSE);    ProtectionEnd();    return status;} // end EEPROM_BufWriteint EEPROM_WritePage(//// Description://// Parmeters:*/    Uint32 addr,    // address to be written to    void* buf,  // pointer to the source buffer pointer    Uint32 length   // length in bytes of data to be written)////  Returns://          SUCCESS = 0 or FAIL////----------------------------------------------------------------------------{	return EEPROM_BufWrite(addr, buf, length);}//-----------------------------------------------------------------------------// Function:int EEPROM_Read(//// Description://  This operation reads data from the AT24C02 at the given address//// Parmeters:*/    Uint32 addr,    // address to be read from    void * bufPtr,  // pointer to the target buffer    Uint32 length   // size in bytes of the target buffer)////  Returns://      n/a////----------------------------------------------------------------------------{    Uint32 bytesRead;    int status = SUCCESS;    ProtectionStart();    I2C_ConfigurePort(TRUE);    DbgPrint("--- In EEPROM_Read --- (starting addr=%0.2x), mode: %02x\n",addr, I2C_GetModeRegister());    if ( !length )    {        status = FAIL;        ErrPrint("Failure reading -tried to read 0\n");    } // end if    else    {        // signal start        At24c02_StartCode();        At24c02_SendByte( AT_DEVICE_ADDR ); // send address        AT_WAIT_ACK();        // write address MSB first        At24c02_SendByte( addr );  // send address        AT_WAIT_ACK();        At24c02_StartCode();        At24c02_SendByte( AT_DEVICE_ADDR|0x01 ); // send address        AT_WAIT_ACK();            for ( bytesRead = 0; bytesRead < length-1; bytesRead++ )            {                *(Byte*)bufPtr++ = At24c02_ReadByte( );                // send ack                I2C_SetDataTriStateEnableBit();                I2C_ClearDataBit();                Delay(AT_DELAY);                I2C_SetClkTriStateEnableBit();                I2C_SetClkBit();                Delay(AT_DELAY);                I2C_ClearClkBit();                Delay(AT_DELAY);            } // end for

⌨️ 快捷键说明

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