uint8 I2C_MasterWriteBuf(uint8 slaveAddr, uint8 * xferData, uint8 cnt,uint8 mode)
{
uint8 tmr;
uint8 errStatus = I2C_MSTR_NO_ERROR;
/* Determine whether or not to automatically generate a stop condition */
if(mode & I2C_MODE_NO_STOP)
{
/* Do not generate a Stop at the end of transfer */
I2C_mstrControl |= I2C_MSTR_NO_STOP;
}
else /* Generate a Stop */
{
I2C_mstrControl &= ~I2C_MSTR_NO_STOP;
}
if((I2C_State == I2C_SM_IDLE) || (I2C_State == I2C_SM_MSTR_HALT))
{
errStatus = I2C_MSTR_BUS_TIMEOUT;
if(I2C_State != I2C_SM_MSTR_HALT)
{
for(tmr = I2C_MSTR_TIMEOUT; tmr > 0; tmr--)
{
if((I2C_MCSR & I2C_MCSR_BUS_BUSY) == 0)
{
errStatus = I2C_MSTR_NO_ERROR;
break;
}
CyDelay(1); /* Wait 1 mSec */
}
}
else /* Bus halted waiting for restart */
{
errStatus = I2C_MSTR_NO_ERROR;
I2C_mstrStatus &= ~I2C_MSTAT_XFER_HALT;
CyIntClearPending(I2C_ISR_NUMBER);
}
/* If no timeout error, generate start */
if(errStatus == I2C_MSTR_NO_ERROR)
{
slaveAddr = (slaveAddr << 1);
I2C_State = I2C_SM_MSTR_WR_ADDR;
I2C_mstrWrBufPtr = xferData; /* Set buffer pointer */
I2C_mstrWrBufIndex = 0u; /* Start buffer at zero */
I2C_mstrWrBufSize = cnt; /* Set buffer size */
I2C_DATA = slaveAddr; /* Write address to data reg */
/* Generate a Start or ReStart depending on flag passed */
if(mode & I2C_MODE_REPEAT_START)
{
I2C_MCSR = I2C_MCSR_RESTART_GEN; /* Generate a ReStart */
}
else
{
I2C_MCSR = I2C_MCSR_START_GEN; /* Generate a Start */
}
CyIntEnable(I2C_ISR_NUMBER); /* IRQ must be enabled for this to work */
/* Clear write complete flag */
I2C_mstrStatus &= ~I2C_MSTAT_WR_CMPLT;
}
}
else
{
errStatus = I2C_MSTR_SLAVE_BUSY;
}
return(errStatus);
}
在I2C的主模式下,只有一个Slave地址,还一个REG_Addr在哪呢?初学不懂,请高人指教。谢谢!! |