📄 at24c01.c
字号:
//============================================================================// File Name: at24c01.c//// (c) Copyright [2002] Cirrus Logic Inc. All rights reserved//// This source code is Cirrus Logic, Inc. proprietary and confidential// information//// Description:// The API for the AT24C01 EEPROM.//// Modification History:// $Id: at24c01.c,v 1.5 2003/12/05 01:01:19 matthieu Exp $// $Log: at24c01.c,v $// Revision 1.5 2003/12/05 01:01:19 matthieu// Use // for copyright comments//// Revision 1.4 2003/11/20 21:32:16 matthieu// Remove the //Perl skip lines...//// Revision 1.3 2003/11/20 08:42:08 akipnis// Renamed CLASI functions and responses to follow naming conventions//// Revision 1.2 2003/11/05 01:54:58 haluk// 9898 finialized eeprom//// Revision 1.1 2003/09/11 00:55:02 matthieu// Initial Sonata 2 tree////============================================================================#include "apiconfig.h"#include "apitypes.h"#include "eromdbg.h" /* dbgprint */#include "dvhw32.h"#include "eeprom.h"#include "i2capi.h"#include "errorcodes.h"#include "osapi.h"//-----------------------------------------------------------------------------// External definitions//-----------------------------------------------------------------------------extern void Delay(int n);//-----------------------------------------------------------------------------// Local definitions//-----------------------------------------------------------------------------#define AT_DELAY 50#define NUM_PAGE_BYTES 4//-----------------------------------------------------------------------------// Local variables//-----------------------------------------------------------------------------static Uint32 m_lockCount = 0;//-----------------------------------------------------------------------------// Local functions//-----------------------------------------------------------------------------static Boolean At24c01_Check_Ack();static void At24c01_StartCode();static void At24c01_StopCode();static Byte At24c01_ReadByte( );static void At24c01_SendByte( Byte value, Boolean isAdr );static void At24c01_SetReadWrite ( Boolean toRead );static void ProtectionStart(void);static void ProtectionEnd(void);int EEPROM_GetSize(Uint32 *size){ *size = 128; return SUCCESS;}//-----------------------------------------------------------------------------// Function:int EEPROM_Write8(//// Description:// This operation performs an 8 bit (byte) write to the AT24C01 eeprom.//// Parmeters:*/ Uint32 addr, // address to be written to Byte data // data byte to be written)//// Returns:// SUCCESS = 0 or FAIL////----------------------------------------------------------------------------{ ProtectionStart(); // signal start At24c01_StartCode(); // write address MSB first At24c01_SendByte( addr, 1 ); // send address // send write bit At24c01_SetReadWrite( 0 ); // write // wait for ack if ( !At24c01_Check_Ack() ) { ProtectionEnd(); return ERR_FAIL; } // end if // send data At24c01_SendByte( data, 0 ); // send data // wait for ack if ( !At24c01_Check_Ack() ) { ProtectionEnd(); return ERR_FAIL; } // end if // signal stop At24c01_StopCode(); ProtectionEnd(); return NO_ERR;} // end At24c01_Write8//-----------------------------------------------------------------------------// Function:int EEPROM_Write32(//// Description:// This operation performs an 32 bit write to the AT24C01 eeprom. This is referred// to as a Page Write in the data sheet.//// 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(); // signal start At24c01_StartCode(); // write address MSB first At24c01_SendByte( addr, 1 ); // send address // send write bit At24c01_SetReadWrite( 0 ); // write // wait for ack if ( !At24c01_Check_Ack() ) { ProtectionEnd(); return ERR_FAIL; } // end if for ( i = 0; i < NUM_PAGE_BYTES; i ++ ) { // send data At24c01_SendByte( *(bytePtr+i), 0 ); // send data // wait for ack if ( !At24c01_Check_Ack() ) { ProtectionEnd(); return ERR_FAIL; } // end if } // end for // signal stop At24c01_StopCode(); ProtectionEnd(); return NO_ERR;} // end At24c01_Write32//-----------------------------------------------------------------------------// Function:int EEPROM_BufWrite(//// Description:// This operation writes the buffer to the AT24C01 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 = NO_ERR; ProtectionStart(); // make the possible page writes for ( i = 0; (i < length / NUM_PAGE_BYTES) && !status; i++ ) { status = EEPROM_Write32( addr, *(Uint32*)bufPtr ); addr += 4; bufPtr += 4; } // end for // write the remaining bytes for ( i = 0; (i < length % NUM_PAGE_BYTES) && !status; i++ ) { status = EEPROM_Write8( addr++, *(Byte*)bufPtr++ ); } // end for ProtectionEnd(); return status;} // end At24c01_BufWrite//-----------------------------------------------------------------------------// Function:int EEPROM_Read(//// Description:// This operation reads data from the AT24C01 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 = NO_ERR; if ( !length ) { status = ERR_FAIL; } // end if else { ProtectionStart(); // signal start At24c01_StartCode(); // write address MSB first At24c01_SendByte( addr, 1 ); // send address // send write bit At24c01_SetReadWrite( 1 ); // read // wait for ack if ( !At24c01_Check_Ack() ) { status = ERR_FAIL; } // end if else { for ( bytesRead = 0; bytesRead < length-1; bytesRead++ ) { *(Byte*)bufPtr++ = At24c01_ReadByte( ); // send ack I2C_SetDataTriStateEnableBit(); // SDA write I2C_ClearDataBit(); // SDA = 0 Delay(AT_DELAY); I2C_SetClkBit(); // SCL = 1 Delay(AT_DELAY); I2C_ClearClkBit(); // SCL = 0 Delay(AT_DELAY); } // end for // read last byte *(Byte*)bufPtr++ = At24c01_ReadByte( ); // cycle scl for no ack I2C_SetClkTriStateEnableBit(); // SCL write Delay(AT_DELAY); I2C_ClearClkBit(); // SCL = 0 Delay(AT_DELAY); // signal stop At24c01_StopCode(); } // end else ProtectionEnd(); } // end else return status;} // end At24c01_Read//-----------------------------------------------------------------------------// Function:static Boolean At24c01_Check_Ack()//// Description:// This operation checks for the ACK from the AT24C01 device.//// Parmeters:*///// Returns:// 0 = NACK; 0 < ACK//----------------------------------------------------------------------------{ Boolean ack; // set tri-state to read I2C_ClearDataTriStateEnableBit(); // SDA read Delay(AT_DELAY); I2C_SetClkBit(); // SCL = 1 Delay(AT_DELAY); // read the ACK ack = !I2C_GetDataBit(); // read data I2C_ClearClkBit(); // SCL = 0 Delay(AT_DELAY);// bugzilla 3380 - for single core systems, must yield. #ifdef GECKO OS_TaskYield();#endif return ack;} // end At24c01_Check_Ack//-----------------------------------------------------------------------------// Function:static void At24c01_StartCode()//// Description:// This operation send a Start signal to the AT24C01.//// Parmeters:*///// Returns:// n/a//----------------------------------------------------------------------------{ I2C_SetClkTriStateEnableBit(); // SCL write I2C_SetDataTriStateEnableBit(); // SDA write I2C_SetDataBit(); // SDA = 1 I2C_SetClkBit(); // SCL = 1 Delay(AT_DELAY); I2C_ClearDataBit(); // SDA = 0 Delay(AT_DELAY); I2C_ClearClkBit(); // SCL = 0 Delay(AT_DELAY);} // end At24c01_StartCode//-----------------------------------------------------------------------------// Function:static void At24c01_StopCode()//// Description:// This operation send a Stop signal to the AT24C01.//// Parmeters:*///// Returns:// n/a//----------------------------------------------------------------------------{ I2C_SetClkTriStateEnableBit(); // SCL write I2C_SetDataTriStateEnableBit(); // SDA write I2C_ClearDataBit(); // SCL = 0 Delay(AT_DELAY); I2C_SetClkBit(); // SCL = 1 Delay(AT_DELAY); I2C_SetDataBit(); // SDA = 1 Delay(AT_DELAY*600); // Write cycle time delay} // end At24c01_StopCode//-----------------------------------------------------------------------------// Function:static void At24c01_SendByte(//// Description:// This operation write the given byte out to the AT24C01.//// Parmeters:*/ Byte value, // value to be sent Boolean isAdr // value is 7 bit address)//// Returns:// n/a//----------------------------------------------------------------------------{ int i; Uint8 numBits = 8; // data if ( isAdr ) { // send 7 bit address numBits = 7; } // end if I2C_SetClkTriStateEnableBit(); // SCL write I2C_SetDataTriStateEnableBit(); // SCL write Delay(AT_DELAY); for( i=numBits-1; i>=0; i-- ) { if( (value >> i) & 0x1 ) { I2C_SetDataBit(); // SDA = 1 } // end if else { I2C_ClearDataBit(); // SDA = 0 } // end else Delay(AT_DELAY); I2C_SetClkBit(); // SCL = 1 Delay(AT_DELAY); I2C_ClearClkBit(); // SCL = 0 Delay(AT_DELAY); } // end for} // end At24c01_SendByte//-----------------------------------------------------------------------------// Function:static void At24c01_SetReadWrite (//// Description:// This operation sends the read/write signal to the AT24C01.//// Parmeters:*/ Boolean toRead // true = read; false = write)//// Returns:// n/a//----------------------------------------------------------------------------{ I2C_SetClkTriStateEnableBit(); // SCL write I2C_SetDataTriStateEnableBit(); // SDA write Delay(AT_DELAY); if ( toRead ) { I2C_SetDataBit(); // SDA = 1 } // end if else { I2C_ClearDataBit(); // SDA = 0 } // end else Delay(AT_DELAY); I2C_SetClkBit(); // SCL = 1 Delay(AT_DELAY); I2C_ClearClkBit(); // SCL = 0 Delay(AT_DELAY);} // end At24c01_SetReadWrite//-----------------------------------------------------------------------------// Function:static Byte At24c01_ReadByte( )//// Description:// This operation will read a byte from the AT24C01. The SDA tri-state// will both return in the read state.//// Parmeters:*///// Returns:// the byte value read from the AT24C01////----------------------------------------------------------------------------{ Uint8 bit; Byte data = 0; I2C_ClearDataTriStateEnableBit(); // SDA read Delay(AT_DELAY); for ( bit = 0; bit < 8; bit++ ) { I2C_SetClkBit(); // SCL = 1 Delay(AT_DELAY); data <<= 1; if ( TRUE == I2C_GetDataBit() ) { data++; } // end if // clock in data I2C_ClearClkBit(); // SCL = 0 Delay(AT_DELAY); } // end for return data;} // end At24c01_ReadByte//-----------------------------------------------------------------------------// Function:static void ProtectionStart(//// Description:// This function grabs the I2C semaphore if this is the first time// it is called. This function also increments the lock count.//// Notes://// Parameters: void)//// Returns:////-----------------------------------------------------------------------------{ if ( 0 == m_lockCount ) { // get the semaphore I2C_ProtectionStart(); } m_lockCount++;}//-----------------------------------------------------------------------------// Function:static void ProtectionEnd(//// Description:// This function decrements the lock count. If the lock count is equal to// zero, this function releases the I2C semaphore.//// Notes://// Parameters: void)//// Returns:////-----------------------------------------------------------------------------{ if ( m_lockCount > 0 ) { m_lockCount--; if ( 0 == m_lockCount ) { // free the semaphore I2C_ProtectionEnd(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -