📄 i2cse2pd.c
字号:
/*=================================================================
File name : I2CSE2PD.C
Originator : Digital Control Systems Group
Texas Instruments
Description : This file contains the I2C bus Serial EEPROM driver
implemented using I2C master driver (GPIO)
Target : C28x
Date : 30/01/2002 (DD/MM/YYYY)
====================================================================*/
#include "..\include\i2cse2p.h"
#include "..\include\i2cm_io.h"
static VI2CM_IO i2c=VI2CM_IO_DEFAULTS;
/********************************************************************/
/******* I2C bus Serial EEPROM driver Initialization routine ********/
/********************************************************************/
void I2CSE2P_init(I2CSE2P_IO *eeprom)
{
i2c.init(&i2c);
eeprom->csr=0;
eeprom->msgPtr=0;
}
/********************************************************************/
/******* I2C bus Serial EEPROM driver Tick function *****************/
/********************************************************************/
void I2CSE2P_tick(I2CSE2P_IO *eeprom)
{
static int step=0;
static int dataCount=0;
switch(step)
{
case 0:
/* If write request is SET, then trigger the Write operation
If read request is SET, then trigger the Read operation
If Read request is also not SET, then continue to poll */
if(eeprom->csr&I2CSE2P_WRRQ)
{ step=1;
eeprom->csr|=I2CSE2P_WRIP; /*Set Write in progress */
}
if(eeprom->csr&I2CSE2P_RDRQ)
{ step=1;
eeprom->csr|=I2CSE2P_RDIP; /* Set Read in progress */
}
break;
/************************************************************
*********** Send Control + Address *************************
*************************************************************/
case 1:
/* Issue START Condition */
i2c.taskIndex=START_CMD;
step++;
break;
case 2:
/* Wait for START & Send device Id/block number */
if(i2c.taskIndex == IDLE_STATE)
{ i2c.I2CDAT=(0xA0)|(((eeprom->msgPtr->Did_Blknr)&0x7)<<1);
/* Issue Transmit command */
i2c.taskIndex=TXMIT_CMD;
step++;
}
break;
case 3:
/* Wait for the device Id or Black number transmission */
if(i2c.taskIndex==IDLE_STATE)
{ if(i2c.I2CCSR & I2C_ACKSTAT)
step=16; /* No Ack from slave, re-try */
else
{
#if ADDRWIDTH==TWOBYTE
i2c.I2CDAT=( (eeprom->msgPtr->addr)&0xFF00)>>8;
#endif
#if ADDRWIDTH==ONEBYTE
i2c.I2CDAT=((eeprom->msgPtr->addr) );
#endif
/* Issue Transmit command */
i2c.taskIndex=TXMIT_CMD;
step++;
}
}
break;
case 4:
/* Wait for address transmit. send second address, if any */
if(i2c.taskIndex == IDLE_STATE)
{ if(i2c.I2CCSR & I2C_ACKSTAT)
{ eeprom->csr|=I2CSE2P_ERR; /* Error Occured, Issue Stop*/
step=14;
}
else
{
#if ADDRWIDTH==TWOBYTE
i2c.I2CDAT=eeprom->msgPtr->addr;
/* Issue Transmit command */
i2c.taskIndex=TXMIT_CMD;
#endif
step++;
}
}
break;
case 5:
/* Wait for Second address transmit */
if(i2c.taskIndex == IDLE_STATE)
{ if(i2c.I2CCSR & I2C_ACKSTAT)
{ eeprom->csr|=I2CSE2P_ERR; /* Error Occured, Issue Stop*/
step=14;
}
else
{ /* Check Read or Write */
if(eeprom->csr & I2CSE2P_WRIP)
step++;
else
step=8;
}
}
break;
/***********************************************************/
/************ Serial EEPROM Write operation ****************/
/***********************************************************/
case 6:
/* Send Data */
i2c.I2CDAT=*(eeprom->msgPtr->dataPtr+dataCount);
/* Issue Transmit command */
i2c.taskIndex=TXMIT_CMD;
step++;
dataCount++;
break;
case 7:
/* Wait for data transmission and check Ack */
if(i2c.taskIndex == IDLE_STATE)
{ if(i2c.I2CCSR & I2C_ACKSTAT)
{ eeprom->csr|=I2CSE2P_ERR; /* Error Occured, Issue Stop*/
step=14;
}
else
{
if (dataCount==eeprom->msgPtr->nrData)
step=14; /* Issue STOP condition */
else
step--;
}
}
break;
/***********************************************************/
/************ Serial EEPROM Read operation *****************/
/***********************************************************/
case 8:
/* Issue Sr (Repeat Start Condition) */
i2c.taskIndex=RSTART_CMD;
step++;
break;
case 9:
/* Wait for Sr & Send device Id/block number */
if(i2c.taskIndex == IDLE_STATE)
{ i2c.I2CDAT=(0xA1)|((eeprom->msgPtr->Did_Blknr&0x7)<<1);
/* Issue Transmit command */
i2c.taskIndex=TXMIT_CMD;
step++;
}
break;
case 10:
/* Wait for device Id/block number txmit */
if(i2c.taskIndex == IDLE_STATE)
{ if(i2c.I2CCSR & I2C_ACKSTAT)
{ eeprom->csr|=I2CSE2P_ERR; /* Error Occured, Issue Stop*/
step=14;
}
else
step++;
}
break;
case 11:
/* Enable Receive operation */
i2c.taskIndex=RECV_CMD;
step++;
break;
case 12:
/* Wait for receive */
if(i2c.taskIndex == IDLE_STATE)
{ *(eeprom->msgPtr->dataPtr+dataCount)=(i2c.I2CDAT);
dataCount++;
step++;
if (dataCount==eeprom->msgPtr->nrData)
i2c.I2CCSR|=I2C_ACKDT; /* Acknowledge=1 */
else
i2c.I2CCSR&=(~I2C_ACKDT); /* Acknowledge=0 */
i2c.taskIndex=ACK_CMD; /* Issue Acknowledge */
}
break;
case 13:
/* Send Acknowledge to serial EEPROM */
if(i2c.taskIndex == IDLE_STATE)
{ if (dataCount==eeprom->msgPtr->nrData)
step++;
else
step=step-2;
}
break;
/***********************************************************/
/************ STOP Condition *******************************/
/***********************************************************/
case 14:
/* Issue STOP Condition */
i2c.taskIndex=STOP_CMD;
step++;
break;
case 15:
/* Wait for STOP condition */
if(i2c.taskIndex == IDLE_STATE)
{ eeprom->csr&=~(I2CSE2P_RDIP | I2CSE2P_WRIP);
eeprom->csr&=~(I2CSE2P_RDRQ | I2CSE2P_WRRQ);
step=0;
dataCount=0;
}
break;
/***********************************************************/
/************ Error handling *******************************/
/***********************************************************/
case 16:
/* Issue STOP condition and re-try */
i2c.taskIndex=STOP_CMD;
step++;
break;
case 17:
/* Wait for STOP condition and Re-try */
if(i2c.taskIndex == IDLE_STATE)
step=1;
break;
}
i2c.tick(&i2c);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -