📄 i2c.c
字号:
{
CLI_ToeWr((U32_T)realReg, &i2cSlvRxPkt[3], (U8_T)realLen);
}
else if (cmdIndReg == SFR_MCIR)
{
CLI_MacWr((U32_T)realReg, &i2cSlvRxPkt[3], (U8_T)realLen);
}
}
else if ((slvCmd & 0xF0) == I2C_SLV_IRSFR)
{
cmdIndReg = i2cSlvRxPkt[1];
realReg = i2cSlvRxPkt[2];
realLen = (slvCmd & 0x0F) + 1;
if (cmdIndReg == SFR_I2CCIR)
{
CLI_I2cRd((U32_T)realReg, &i2cSlvTxPkt[i2cDataLenCnt], (U8_T)realLen);
}
else if (cmdIndReg == SFR_SPICIR)
{
CLI_SpiRd((U32_T)realReg, &i2cSlvTxPkt[i2cDataLenCnt], (U8_T)realLen);
}
else if (cmdIndReg == SFR_OWCIR)
{
CLI_OwRd((U32_T)realReg, &i2cSlvTxPkt[i2cDataLenCnt], (U8_T)realLen);
}
else if (cmdIndReg == SFR_CANCIR)
{
CLI_CanRd((U32_T)realReg, &i2cSlvTxPkt[i2cDataLenCnt], (U8_T)realLen);
}
else if (cmdIndReg == SFR_TCIR)
{
CLI_ToeRd((U32_T)realReg, &i2cSlvTxPkt[i2cDataLenCnt], (U8_T)realLen);
}
else if (cmdIndReg == SFR_MCIR)
{
CLI_MacRd((U32_T)realReg, &i2cSlvTxPkt[i2cDataLenCnt], (U8_T)realLen);
}
}
else if (slvCmd == I2C_SLV_BWDM)
{
memAddr = ((U32_T)i2cSlvRxPkt[3] << 16) | ((U32_T)i2cSlvRxPkt[2] << 8) | ((U32_T)i2cSlvRxPkt[1]);
for (i = 4 ; i < i2cDataLen ; i ++)
{
CLI_ExtMemWr((U32_T)memAddr, (U32_T)i2cSlvRxPkt[i]);
memAddr ++;
}
}
else if (slvCmd == I2C_SLV_BRDM)
{
if (i2cDataLenCnt == 0)
{
memAddr = ((U32_T)i2cSlvRxPkt[3] << 16) | ((U32_T)i2cSlvRxPkt[2] << 8) | ((U32_T)i2cSlvRxPkt[1]);
}
CLI_ExtMemRd((U32_T)memAddr, &memData);
i2cSlvTxPkt[i2cDataLenCnt] = (U8_T)memData;
memAddr ++;
}
}
#endif
/*
*--------------------------------------------------------------------------------
* static void i2c_MasterXmit(U8_T wrData, U8_T mstCmd)
* Purpose : Putting the data into i2c transmitting register and setting the
* master's command and condition in I2C master mode.
* Params : wrData - one byte data to transmit.
* mstCmd - master command of the current byte data.
* Returns : none
* Note : none
*--------------------------------------------------------------------------------
*/
static void i2c_MasterXmit(U8_T wrData, U8_T mstCmd)
{
/* Record the globe flag of command condition */
i2cActF = mstCmd;
/* First the master flipper sends the slave address to access */
I2C_Cmd(SI_WR, I2CTR, &wrData);
/* Order command to I2CCR */
I2C_Cmd(SI_WR, I2CCR, &mstCmd);
}
/*
*--------------------------------------------------------------------------------
* static void i2c_MasterRcvr(U8_T *rdData, U8_T mstCmd)
* Purpose : Getting the receiving byte data in I2C master mode.
* Params : *rdData - a pointer to store receiving data.
* mstCmd - master command of the current byte data.
* Returns : none
* Note : none
*--------------------------------------------------------------------------------
*/
static void i2c_MasterRcvr(U8_T *rdData, U8_T mstCmd)
{
/* Record the globe flag of command condition */
i2cActF = mstCmd;
/* After ACK, read data from I2CRR */
I2C_Cmd(SI_RD, I2CRR, rdData);
/* Then, reply ACK to slave */
I2C_Cmd(SI_WR, I2CCR, &mstCmd);
i2cDataLenCnt ++;
}
/*
*--------------------------------------------------------------------------------
* static void i2c_SlaveXmit(U8_T wrData, U8_T slvCmd)
* Purpose : Putting the data into i2c transmitting register and setting the
* master's command and condition in I2C slave mode.
* Params : wrData - one byte data to transmit.
* slvCmd - slave command of the current byte data.
* Returns : none
* Note : none
*--------------------------------------------------------------------------------
*/
static void i2c_SlaveXmit(U8_T wrData, U8_T slvCmd)
{
/* Record the globe flag of command condition */
i2cActF = slvCmd;
/* transmit the data byte */
I2C_Cmd(SI_WR, I2CTR, &wrData);
/* Order command to I2CCR */
I2C_Cmd(SI_WR, I2CCR, &slvCmd);
}
/*
*--------------------------------------------------------------------------------
* static void i2c_SlaveRcvr(U8_T *rdData, U8_T slvCmd)
* Purpose : Getting the receiving byte data in I2C slave mode.
* Params : *rdData - a pointer to store receiving data.
* slvCmd - slave command of the current byte data.
* Returns : none
* Note : none
*--------------------------------------------------------------------------------
*/
static void i2c_SlaveRcvr(U8_T *rdData, U8_T slvCmd)
{
/* Record the globe flag of command condition */
i2cActF = slvCmd;
/* After ACK, read data from I2CRR */
I2C_Cmd(SI_RD, I2CRR, rdData);
/* Then, reply ACK to slave */
I2C_Cmd(SI_WR, I2CCR, &slvCmd);
}
/* EXPORTED SUBPROGRAM BODIES */
/*
*--------------------------------------------------------------------------------
* void I2C_Setup(U8_T ctrlCmd, U16_T preClk, U16_T axIdAddr)
* Purpose : This function is used to setup the I2C module and
* to initial the globe values in this module.
* Params : ctrlCmd - control register value.
* preClk - a pre-scale parameter calculates the bus speed.
* axIdAddr - a device address of local AX11000 I2C module.
* Returns : none
* Note : none
*--------------------------------------------------------------------------------
*/
void I2C_Setup(U8_T ctrlCmd, U16_T preClk, U16_T axIdAddr)
{
U16_T i;
/* Values initial */
i2cCtrl = 0;
i2cEndCond = 0;
i2cActF = 0;
i2cDataLen = 0;
i2cDataLenCnt = 0;
i2cPktDir = 0;
ptI2cTxBuf = NULL;
ptI2cRxBuf = NULL;
i2cState = 0;
for (i=0 ; i<SLV_MAX_PKT_NUM ; i++)
{
i2cSlvRxPkt[i] = 0;
i2cSlvTxPkt[i] = 0;
}
/* Pre-scale Clock */
I2CDR = (U8_T)(0x00FF & preClk);
I2CDR = (U8_T)((0xFF00 & preClk) >> 8);
I2CCIR = I2CCPR;
/* Flipper device address for slave mode */
I2CDR = (U8_T)(axIdAddr & 0x00FF);
I2CDR = (U8_T)((axIdAddr & 0xFF00) >> 8);
I2CCIR = I2CSDAR;
/* Setup I2C mode */
I2C_Cmd(SI_WR, I2CCTL, &ctrlCmd);
}
/*
*--------------------------------------------------------------------------------
* void I2C_Func(void)
* Purpose : Handling serial interface I2C interrupt function.
* Params : none
* Returns : none
* Note : The I2C_RLE_ING (reloading I2C EEPROM) bit and
* the I2C_BL_DONE (Bootloader done) bit must always be checked,
* whichever in master mode or slave mode.
*--------------------------------------------------------------------------------
*/
void I2C_Func(void)
{
U8_T i2cStatus;
I2C_Cmd(SI_RD, I2CCTL, &i2cCtrl);
if (i2cCtrl & I2C_MASTER_MODE)
{
EA = 0;
I2C_Cmd(SI_RD, I2CMSR, &i2cStatus);
EA = 1;
if (!(i2cStatus & I2C_RLE_ING) && (i2cStatus & I2C_BL_DONE))
{
if (i2cCtrl & I2C_MST_IE)
{
if (i2cStatus & I2C_INTR_FLAG)
{
i2c_MstStatus(i2cStatus);
}
}
else
{
if (!(i2cStatus & I2C_TIP))
{
i2c_MstStatus(i2cStatus);
}
}
}
}
else
{
EA = 0;
I2C_Cmd(SI_RD, I2CMSR, &i2cStatus);
EA = 1;
if (!(i2cStatus & I2C_RLE_ING) && (i2cStatus & I2C_BL_DONE))
{
i2cStatus = 0;
EA = 0;
I2C_Cmd(SI_RD, I2CSSR, &i2cStatus);
EA = 1;
if (i2cCtrl & I2C_SLV_IE)
{
if (i2cStatus & I2C_SLV_TXR_OK)
{
i2c_SlvStatus(i2cStatus);
}
}
else
{
if (i2cStatus & I2C_SLV_TXR_OK)
{
i2c_SlvStatus(i2cStatus);
}
}
}
}
}
/*
*--------------------------------------------------------------------------------
* void I2C_PktBuf(I2C_BUF *ptI2cBuf)
* Purpose : Packeting a packet and transmitting the first byte
* Params : ptI2cBuf - a pointer stores the packet.
* Returns : none
* Note : none
*--------------------------------------------------------------------------------
*/
void I2C_PktBuf(I2C_BUF *ptI2cBuf)
{
U8_T firstAddr;
I2C_Cmd(SI_RD, I2CCTL, &i2cCtrl);
if (i2cCtrl & I2C_MASTER_MODE) // I2C Master Mode
{
i2cDataLenCnt = 0;
i2cEndCond = ptI2cBuf->I2cEnd;
i2cPktDir = ptI2cBuf->I2cDir;
i2cDataLen = ptI2cBuf->DataLen;
if (i2cCtrl & I2C_10BIT)
{
firstAddr = ((U8_T)((ptI2cBuf->I2cAddr.TenBitAddr & 0x0300) >> 7) | 0xF0);
if (i2cPktDir & I2C_XMIT)
{
ptI2cTxBuf = ptI2cBuf;
i2c_MasterXmit(firstAddr & ~BIT0, I2C_MASTER_GO | I2C_CMD_WRITE | I2C_START_COND);
}
else
{
ptI2cRxBuf = ptI2cBuf;
i2c_MasterXmit(firstAddr | BIT0, I2C_MASTER_GO | I2C_CMD_WRITE | I2C_START_COND);
}
}
else
{
firstAddr = ptI2cBuf->I2cAddr.SevenBitAddr << 1;
if (i2cPktDir & I2C_XMIT)
{
ptI2cTxBuf = ptI2cBuf;
i2c_MasterXmit(firstAddr & ~BIT0, I2C_MASTER_GO | I2C_CMD_WRITE | I2C_START_COND);
}
else
{
ptI2cRxBuf = ptI2cBuf;
i2c_MasterXmit(firstAddr | BIT0, I2C_MASTER_GO | I2C_CMD_WRITE | I2C_START_COND);
}
}
}
else // I2C Slave Mode
{
if (ptI2cBuf->I2cDir & I2C_XMIT)
{
/* Get the slave data to transmit */
//ptI2cTxBuf->ptI2c = ??;
ptI2cBuf->DataLen = i2cDataLen;
}
else
{
/* packet the received data to upper layer */
ptI2cBuf->DataLen = i2cDataLen;
}
}
}
/*
*--------------------------------------------------------------------------------
* BOOL I2C_FlagChk(U8_T chkBit)
* Purpose : Checking the current state in I2C driver.
* Params : chkBit - the state bit to be checked.
* Returns : TRUE - the state value meets the chkBit.
* Note : none
*--------------------------------------------------------------------------------
*/
BOOL I2C_FlagChk(U8_T chkBit)
{
if (i2cState & chkBit)
return TRUE;
else
return FALSE;
}
/*
*--------------------------------------------------------------------------------
* void I2C_FlagEnb(U8_T enbBit)
* Purpose : Setting the current state in I2C driver.
* Params : enbBit - the state bit to be set.
* Returns : none
* Note : none
*--------------------------------------------------------------------------------
*/
void I2C_FlagEnb(U8_T enbBit)
{
i2cState = i2cState | enbBit;
}
/*
*--------------------------------------------------------------------------------
* void I2C_FlagClr(U8_T clrBit)
* Purpose : Clearing the current state in I2C driver.
* Params : clrBit - the state bit to be cleared.
* Returns : none
* Note : none
*--------------------------------------------------------------------------------
*/
void I2C_FlagClr(U8_T clrBit)
{
i2cState = i2cState & ~clrBit;
}
/*
*--------------------------------------------------------------------------------
* void I2C_Cmd(U8_T cmdType, U8_T i2cCmdIndex, U8_T *i2cData)
* Purpose : Accessing the I2C interface indirectly through I2C's SFR.
* Params : cmdType - command type of writing or reading.
* i2cCmdIndex - I2C register address.
* *i2cData - a pointer store the data.
* Returns : none
* Note : none
*--------------------------------------------------------------------------------
*/
void I2C_Cmd(U8_T cmdType, U8_T i2cCmdIndex, U8_T *i2cData)
{
if (cmdType == SI_WR)
{
I2CDR = *i2cData;
I2CCIR = i2cCmdIndex;
}
else if (cmdType == SI_RD)
{
I2CCIR = i2cCmdIndex;
*i2cData = I2CDR;
}
}
/*
*--------------------------------------------------------------------------------
* void I2C_Post(void)
* Purpose :
* Params :
* Returns :
* Note :
*--------------------------------------------------------------------------------
*/
void I2C_Post(void)
{
}
/* End of i2c.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -