2025-04-27 07:49:33 -04:00

117 lines
3.8 KiB
C

//----------------------------------------------------------------------------
// I2C.C
//----------------------------------------------------------------------------
// I2C peripheral programming
//----------------------------------------------------------------------------
// Copyright SGS Thomson Microelectronics ! Version alpha ! Jan 1st, 1995
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Include files
//----------------------------------------------------------------------------
#include "stdefs.h"
#include "aal.h"
#include "debug.h"
//----------------------------------------------------------------------------
// Private Types
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Private Constants
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Private GLOBAL Variables (static)
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Functions (statics one are private)
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Start a transfert
//----------------------------------------------------------------------------
static VOID I2CStart(VOID)
{
I2CWriteCMD(1, 1);
I2CWriteCMD(0, 1);
I2CWriteCMD(0, 0);
}
//----------------------------------------------------------------------------
// Stop a transfert
//----------------------------------------------------------------------------
static VOID I2CStop(VOID)
{
I2CWriteCMD(0, 0);
I2CWriteCMD(0, 1);
I2CWriteCMD(1, 1);
}
//----------------------------------------------------------------------------
// Get the acknowledge from the receiver
//----------------------------------------------------------------------------
static BOOL I2CAcknowledge(VOID)
{
I2CWriteCMD(1, 0);
I2CWriteCMD(1, 1);
I2CWriteCMD(1, 0);
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Here we should check that the acknowledge signal has arrived, but HW has
// not been implemented to do this, so we can not !
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
return TRUE;
}
//----------------------------------------------------------------------------
// Send a byte on the bus (Most Significant Bit first)
//----------------------------------------------------------------------------
static VOID I2CSendByte(BYTE Data)
{
BYTE i;
for (i = 0; i < 8; i++) {
if ((Data & 0x80) != 0) {
I2CWriteCMD(1, 0);
I2CWriteCMD(1, 1);
I2CWriteCMD(1, 0);
}
else {
I2CWriteCMD(0, 0);
I2CWriteCMD(0, 1);
I2CWriteCMD(0, 0);
}
Data = Data << 1;
}
}
//----------------------------------------------------------------------------
// Function to send a byte to an I2C peripheral
//----------------------------------------------------------------------------
BOOL I2CSend(BYTE Address, BYTE SubAddress, BYTE Data)
{
DebugAssert(Address != 0);
I2CStart();
I2CSendByte(Address);
if (!I2CAcknowledge())
return FALSE;
I2CSendByte(SubAddress);
if (!I2CAcknowledge())
return FALSE;
I2CSendByte(Data);
if (!I2CAcknowledge())
return FALSE;
I2CStop();
return TRUE;
}
//------------------------------- End of File --------------------------------