📄 lpc177x_8x_i2c.c
字号:
| ((OwnSlaveAddrConfigStruct->GeneralCallState == ENABLE) ? 0x01 : 0x00))& I2C_I2ADR_BITMASK;
switch (OwnSlaveAddrConfigStruct->SlaveAddrChannel)
{
case 0:
I2Cx->ADR0 = tmp;
I2Cx->MASK0 = I2C_I2MASK_MASK((uint32_t) \
(OwnSlaveAddrConfigStruct->SlaveAddrMaskValue));
break;
case 1:
I2Cx->ADR1 = tmp;
I2Cx->MASK1 = I2C_I2MASK_MASK((uint32_t) \
(OwnSlaveAddrConfigStruct->SlaveAddrMaskValue));
break;
case 2:
I2Cx->ADR2 = tmp;
I2Cx->MASK2 = I2C_I2MASK_MASK((uint32_t) \
(OwnSlaveAddrConfigStruct->SlaveAddrMaskValue));
break;
case 3:
I2Cx->ADR3 = tmp;
I2Cx->MASK3 = I2C_I2MASK_MASK((uint32_t) \
(OwnSlaveAddrConfigStruct->SlaveAddrMaskValue));
break;
}
}
/*********************************************************************//**
* @brief Configures functionality in I2C monitor mode
* @param[in] I2Cx I2C peripheral selected, should be
* - LPC_I2C0
* - LPC_I2C1
* - LPC_I2C2
* @param[in] MonitorCfgType Monitor Configuration type, should be:
* - I2C_MONITOR_CFG_SCL_OUTPUT: I2C module can 'stretch'
* the clock line (hold it low) until it has had time to
* respond to an I2C interrupt.
* - I2C_MONITOR_CFG_MATCHALL: When this bit is set to '1'
* and the I2C is in monitor mode, an interrupt will be
* generated on ANY address received.
* @param[in] NewState New State of this function, should be:
* - ENABLE: Enable this function.
* - DISABLE: Disable this function.
* @return None
**********************************************************************/
void I2C_MonitorModeConfig(uint8_t i2cId, uint32_t MonitorCfgType, FunctionalState NewState)
{
LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
if (NewState == ENABLE)
{
I2Cx->MMCTRL |= MonitorCfgType;
}
else
{
I2Cx->MMCTRL &= (~MonitorCfgType) & I2C_I2MMCTRL_BITMASK;
}
}
/*********************************************************************//**
* @brief Enable/Disable I2C monitor mode
* @param[in] I2Cx I2C peripheral selected, should be
* - LPC_I2C0
* - LPC_I2C1
* - LPC_I2C2
* @param[in] NewState New State of this function, should be:
* - ENABLE: Enable monitor mode.
* - DISABLE: Disable monitor mode.
* @return None
**********************************************************************/
void I2C_MonitorModeCmd(uint8_t i2cId, FunctionalState NewState)
{
LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
if (NewState == ENABLE)
{
I2Cx->MMCTRL |= I2C_I2MMCTRL_MM_ENA;
I2Cx->CONSET = I2C_I2CONSET_AA;
I2Cx->CONCLR = I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC;
}
else
{
I2Cx->MMCTRL &= (~I2C_I2MMCTRL_MM_ENA) & I2C_I2MMCTRL_BITMASK;
I2Cx->CONCLR = I2C_I2CONCLR_SIC | I2C_I2CONCLR_STAC | I2C_I2CONCLR_AAC;
}
I2C_MonitorBufferIndex = 0;
}
/*********************************************************************//**
* @brief Get data from I2C data buffer in monitor mode.
* @param[in] I2Cx I2C peripheral selected, should be
* - LPC_I2C0
* - LPC_I2C1
* - LPC_I2C2
* @return None
* Note: In monitor mode, the I2C module may lose the ability to stretch
* the clock (stall the bus) if the ENA_SCL bit is not set. This means that
* the processor will have a limited amount of time to read the contents of
* the data received on the bus. If the processor reads the DAT shift
* register, as it ordinarily would, it could have only one bit-time to
* respond to the interrupt before the received data is overwritten by
* new data.
**********************************************************************/
uint8_t I2C_MonitorGetDatabuffer(uint8_t i2cId)
{
LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
return ((uint8_t)(I2Cx->DATA_BUFFER));
}
/*********************************************************************//**
* @brief Get data from I2C data buffer in monitor mode.
* @param[in] I2Cx I2C peripheral selected, should be
* - LPC_I2C0
* - LPC_I2C1
* - LPC_I2C2
* @return None
* Note: In monitor mode, the I2C module may lose the ability to stretch
* the clock (stall the bus) if the ENA_SCL bit is not set. This means that
* the processor will have a limited amount of time to read the contents of
* the data received on the bus. If the processor reads the DAT shift
* register, as it ordinarily would, it could have only one bit-time to
* respond to the interrupt before the received data is overwritten by
* new data.
**********************************************************************/
BOOL_8 I2C_MonitorHandler(uint8_t i2cId, uint8_t *buffer, uint32_t size)
{
LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
BOOL_8 ret=FALSE;
I2Cx->CONCLR = I2C_I2CONCLR_SIC;
buffer[I2C_MonitorBufferIndex] = (uint8_t)(I2Cx->DATA_BUFFER);
I2C_MonitorBufferIndex++;
if(I2C_MonitorBufferIndex >= size)
{
ret = TRUE;
}
return ret;
}
/*********************************************************************//**
* @brief Get status of Master Transfer
* @param[in] I2Cx I2C peripheral selected, should be:
* - LPC_I2C0
* - LPC_I2C1
* - LPC_I2C2
* @return Master transfer status, could be:
* - TRUE master transfer completed
* - FALSE master transfer have not completed yet
**********************************************************************/
uint32_t I2C_MasterTransferComplete(uint8_t i2cId)
{
uint32_t retval;
retval = I2C_MasterComplete[i2cId];
I2C_MasterComplete[i2cId] = FALSE;
return retval;
}
/*********************************************************************//**
* @brief Get status of Slave Transfer
* @param[in] I2Cx I2C peripheral selected, should be:
* - LPC_I2C0
* - LPC_I2C1
* - LPC_I2C2
* @return Complete status, could be: TRUE/FALSE
**********************************************************************/
uint32_t I2C_SlaveTransferComplete(uint8_t i2cId)
{
uint32_t retval;
retval = I2C_SlaveComplete[i2cId];
I2C_SlaveComplete[i2cId] = FALSE;
return retval;
}
/*******************************************************************************
功能:设置SA56004相应寄存器
参数:i2cId --- IIC通道。default值为I2C_0
off_addr --SA56004寄存器地址
data 初始化值
********************************************************************************/
uint8_t Sa50064_Write(uint8_t i2cId,uint8_t off_addr,uint8_t data){
LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
uint8_t txdat,retry_num = 0;
uint32_t CodeStatus;
txdat = data;
retry:
CodeStatus = 0;
// Start command
CodeStatus = I2C_Start(I2Cx);
if ((CodeStatus != I2C_I2STAT_M_TX_START) && (CodeStatus != I2C_I2STAT_M_TX_RESTART))
{
retry_num ++;
if (retry_num > 3)
{
goto error;
}
else
{
goto retry;
}
}
/* In case of sending data first --------------------------------------------------- */
/* Send slave address + WR direction bit = 0 ----------------------------------- */
CodeStatus = I2C_SendByte(I2Cx, (Sa56004_I2CADDR << 1)); //发送地址
if (CodeStatus != I2C_I2STAT_M_TX_SLAW_ACK)
{
retry_num++;
if (retry_num > 3)
{
// save status
goto error;
}
else
{
goto retry;
}
}
CodeStatus = I2C_SendByte(I2Cx, off_addr); //发送数据
if (CodeStatus != I2C_I2STAT_M_TX_DAT_ACK)
{
retry_num++;
if (retry_num > 3)
{
// save status
goto error;
}
else
{
goto retry;
}
}
CodeStatus = I2C_SendByte(I2Cx, txdat); //发送数据
if (CodeStatus != I2C_I2STAT_M_TX_DAT_ACK)
{
retry_num++;
if (retry_num > 3)
{
// save status
goto error;
}
else
{
goto retry;
}
}
/* Send STOP condition ------------------------------------------------- */
I2C_Stop(I2Cx);
return 1;
error:
// Send stop condition
I2C_Stop(I2Cx);
return 0;
}
/**********************************************************************************
功能:读SA56004寄存器
参数:i2cId --- IIC通道。default值为I2C_0
off_addr --SA56004寄存器地址
*data 返回读取值
***********************************************************************************/
uint8_t Sa50064_Read(uint8_t i2cId,uint8_t off_addr,uint8_t *data){
LPC_I2C_TypeDef* I2Cx = I2C_GetPointer(i2cId);
uint8_t rxdat,retry_num = 0;
uint32_t CodeStatus;
retry:
CodeStatus = 0;
// Start command
CodeStatus = I2C_Start(I2Cx);
if ((CodeStatus != I2C_I2STAT_M_TX_START) && (CodeStatus != I2C_I2STAT_M_TX_RESTART))
{
retry_num ++;
if (retry_num > 3)
{
goto error;
}
else
{
goto retry;
}
}
/* In case of sending data first --------------------------------------------------- */
/* Send slave address + WR direction bit = 0 ----------------------------------- */
CodeStatus = I2C_SendByte(I2Cx, (Sa56004_I2CADDR << 1)); //发送地址
if (CodeStatus != I2C_I2STAT_M_TX_SLAW_ACK)
{
retry_num++;
if (retry_num > 3)
{
// save status
goto error;
}
else
{
goto retry;
}
}
CodeStatus = I2C_SendByte(I2Cx, off_addr); //发送数据
if (CodeStatus != I2C_I2STAT_M_TX_DAT_ACK)
{
retry_num++;
if (retry_num > 3)
{
// save status
goto error;
}
else
{
goto retry;
}
}
I2C_Stop(I2Cx);
// Start command
CodeStatus = I2C_Start(I2Cx);
if ((CodeStatus != I2C_I2STAT_M_TX_START) && (CodeStatus != I2C_I2STAT_M_TX_RESTART))
{
retry_num ++;
if (retry_num > 3)
{
goto error;
}
else
{
goto retry;
}
}
/* Send slave address + RD direction bit = 1 ----------------------------------- */
CodeStatus = I2C_SendByte(I2Cx, ((Sa56004_I2CADDR << 1) | 0x01));
if (CodeStatus != I2C_I2STAT_M_RX_SLAR_ACK)
{
retry_num ++;
if (retry_num > 3)
{
goto error;
}
else
{
goto retry;
}
}
CodeStatus = I2C_GetByte(I2Cx, &rxdat, 0);
if (CodeStatus != I2C_I2STAT_M_RX_DAT_NACK)
{
retry_num ++;
if (retry_num > 3)
{
goto error;
}
else
{
goto retry;
}
}
*data = rxdat;
/* Send STOP condition ------------------------------------------------- */
I2C_Stop(I2Cx);
return 1;
error:
// Send stop condition
I2C_Stop(I2Cx);
return 0;
}
/**
* @}
*/
/**
* @}
*/
/* --------------------------------- End Of File ------------------------------ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -