本文整理汇总了C++中I2C_GetFlagStatus函数的典型用法代码示例。如果您正苦于以下问题:C++ I2C_GetFlagStatus函数的具体用法?C++ I2C_GetFlagStatus怎么用?C++ I2C_GetFlagStatus使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了I2C_GetFlagStatus函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: LM75_ShutDown
/**
* @brief Enables or disables the LM75.
* @param NewState: specifies the LM75 new status. This parameter can be ENABLE
* or DISABLE.
* @retval None
*/
uint8_t LM75_ShutDown(FunctionalState NewState)
{
uint8_t LM75_BufferRX[2] ={0,0};
uint8_t LM75_BufferTX = 0;
__IO uint8_t RegValue = 0;
/* Test on BUSY Flag */
LM75_Timeout = LM75_LONG_TIMEOUT;
while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY))
{
if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Configure DMA Peripheral */
LM75_DMA_Config(LM75_DMA_RX, (uint8_t*)LM75_BufferRX, 2);
/* Enable DMA NACK automatic generation */
I2C_DMALastTransferCmd(LM75_I2C, ENABLE);
/* Enable the I2C peripheral */
I2C_GenerateSTART(LM75_I2C, ENABLE);
/* Test on SB Flag */
LM75_Timeout = LM75_FLAG_TIMEOUT;
while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB))
{
if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Send device address for write */
I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter);
/* Test on ADDR Flag */
LM75_Timeout = LM75_FLAG_TIMEOUT;
while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Send the device's internal address to write to */
I2C_SendData(LM75_I2C, LM75_REG_CONF);
/* Test on TXE FLag (data sent) */
LM75_Timeout = LM75_FLAG_TIMEOUT;
while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF)))
{
if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Send START condition a second time */
I2C_GenerateSTART(LM75_I2C, ENABLE);
/* Test on SB Flag */
LM75_Timeout = LM75_FLAG_TIMEOUT;
while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB))
{
if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Send LM75 address for read */
I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Receiver);
/* Test on ADDR Flag */
LM75_Timeout = LM75_FLAG_TIMEOUT;
while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Enable I2C DMA request */
I2C_DMACmd(LM75_I2C,ENABLE);
/* Enable DMA RX Channel */
DMA_Cmd(LM75_DMA_RX_CHANNEL, ENABLE);
/* Wait until DMA Transfer Complete */
LM75_Timeout = LM75_LONG_TIMEOUT;
while (!DMA_GetFlagStatus(LM75_DMA_RX_TCFLAG))
{
if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Send STOP Condition */
I2C_GenerateSTOP(LM75_I2C, ENABLE);
/* Disable DMA RX Channel */
DMA_Cmd(LM75_DMA_RX_CHANNEL, DISABLE);
/* Disable I2C DMA request */
I2C_DMACmd(LM75_I2C,DISABLE);
/* Clear DMA RX Transfer Complete Flag */
DMA_ClearFlag(LM75_DMA_RX_TCFLAG);
//.........这里部分代码省略.........
示例2: I2C_DMA_ReadReg
/*=====================================================================================================*/
u32 I2C_DMA_ReadReg(u8 SlaveAddr, u8 ReadAddr, u8 *ReadBuf, u8 NumByte)
{
I2C_ReadPtr = &NumByte;
I2C_TimeCnt = I2C_TIMEOUT;
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
if ((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_GenerateSTART(I2Cx, ENABLE);
I2C_TimeCnt = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
if ((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_Send7bitAddress(I2Cx, SlaveAddr, I2C_Direction_Transmitter);
I2C_TimeCnt = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
if ((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_SendData(I2Cx, ReadAddr);
I2C_TimeCnt = I2C_TIMEOUT;
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BTF) == RESET)
if ((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_GenerateSTART(I2Cx, ENABLE);
I2C_TimeCnt = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
if ((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_Send7bitAddress(I2Cx, SlaveAddr, I2C_Direction_Receiver);
if (NumByte < 2) {
I2C_TimeCnt = I2C_TIMEOUT;
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_ADDR) == RESET)
if ((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_AcknowledgeConfig(I2Cx, DISABLE);
(void)I2Cx->SR2;
I2C_GenerateSTOP(I2Cx, ENABLE);
I2C_TimeCnt = I2C_TIMEOUT;
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_RXNE) == RESET)
if ((I2C_TimeCnt--) == 0) return I2C_TimeOut();
*ReadBuf = I2C_ReceiveData(I2Cx);
NumByte--;
I2C_TimeCnt = I2C_TIMEOUT;
while (I2Cx->CR1 & I2C_CR1_STOP)
if ((I2C_TimeCnt--) == 0) return I2C_TimeOut();
I2C_AcknowledgeConfig(I2Cx, ENABLE);
} else {
I2C_TimeCnt = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
if ((I2C_TimeCnt--) == 0) return I2C_TimeOut();
DMA_InitStruct.DMA_Channel = DMAx_RX_CHANNEL;
DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)I2Cx_DR_ADDR;
DMA_InitStruct.DMA_Memory0BaseAddr = (u32)ReadBuf;
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStruct.DMA_BufferSize = (u32)(NumByte);
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
DMA_InitStruct.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMAx_RX_STREAM, &DMA_InitStruct);
I2C_DMALastTransferCmd(I2Cx, ENABLE);
DMA_Cmd(DMAx_RX_STREAM, ENABLE);
}
I2C_TimeCnt = I2C_TIMEOUT;
while (NumByte > 0)
if ((I2C_TimeCnt--) == 0) return I2C_TimeOut();
//.........这里部分代码省略.........
示例3: LM75_ReadConfReg
/**
* @brief Read the configuration register from the LM75.
* @param None
* @retval LM75 configuration register value.
*/
uint8_t LM75_ReadConfReg(void)
{
__IO uint8_t RegValue = 0;
/* Enable LM75_I2C acknowledgement if it is already disabled by other function */
I2C_AcknowledgeConfig(LM75_I2C, ENABLE);
/*----------------------------- Transmission Phase --------------------------*/
/* Send LM75_I2C START condition */
I2C_GenerateSTART(LM75_I2C, ENABLE);
/* Test on LM75_I2C EV5 and clear it */
while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
{
}
/* Send STLM75 slave address for write */
I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter);
/* Test on LM75_I2C EV6 and clear it */
while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6 */
{
}
/* Send the configuration register data pointer */
I2C_SendData(LM75_I2C, LM75_REG_CONF);
/* Test on LM75_I2C EV8 and clear it */
while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
{
}
/*----------------------------- Reception Phase -----------------------------*/
/* Send Re-STRAT condition */
I2C_GenerateSTART(LM75_I2C, ENABLE);
/* Test on EV5 and clear it */
while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
{
}
/* Send STLM75 slave address for read */
I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) /* EV6 */
{
}
/* Disable LM75_I2C acknowledgement */
I2C_AcknowledgeConfig(LM75_I2C, DISABLE);
/* Send LM75_I2C STOP Condition */
I2C_GenerateSTOP(LM75_I2C, ENABLE);
/* Test on RXNE flag */
while (I2C_GetFlagStatus(LM75_I2C, I2C_FLAG_RXNE) == RESET);
/* Store LM75_I2C received data */
RegValue = I2C_ReceiveData(LM75_I2C);
/* Return configuration register value */
return (RegValue);
}
示例4: I2C_ReadDeviceRegister
/**
* @brief Reads a register of the device through I2C.
* @param DeviceAddr: The address of the device, could be : IOE_1_ADDR.
* @param RegisterAddr: The target register adress (between 00x and 0x24)
* @retval The value of the read register (0xAA if Timout occured)
*/
uint8_t I2C_ReadDeviceRegister(uint8_t DeviceAddr, uint8_t RegisterAddr)
{
uint8_t IOE_BufferRX[2] = {0x00, 0x00};
/* Configure DMA Peripheral */
IOE_DMA_Config(IOE_DMA_RX, (uint8_t*)IOE_BufferRX);
/* Enable DMA NACK automatic generation */
I2C_DMALastTransferCmd(IOE_I2C, ENABLE);
/* Enable the I2C peripheral */
I2C_GenerateSTART(IOE_I2C, ENABLE);
/* Test on SB Flag */
IOE_TimeOut = TIMEOUT_MAX;
while (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_SB))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Send device address for write */
I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Transmitter);
/* Test on ADDR Flag */
IOE_TimeOut = TIMEOUT_MAX;
while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Send the device's internal address to write to */
I2C_SendData(IOE_I2C, RegisterAddr);
/* Test on TXE FLag (data dent) */
IOE_TimeOut = TIMEOUT_MAX;
while ((!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_BTF)))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Send START condition a second time */
I2C_GenerateSTART(IOE_I2C, ENABLE);
/* Test on SB Flag */
IOE_TimeOut = TIMEOUT_MAX;
while (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_SB))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Send IOExpander address for read */
I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Receiver);
/* Test on ADDR Flag */
IOE_TimeOut = TIMEOUT_MAX;
while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Enable I2C DMA request */
I2C_DMACmd(IOE_I2C,ENABLE);
/* Enable DMA RX Channel */
DMA_Cmd(IOE_DMA_RX_CHANNEL, ENABLE);
/* Wait until DMA Transfer Complete */
IOE_TimeOut = 2 * TIMEOUT_MAX;
while (!DMA_GetFlagStatus(IOE_DMA_RX_TCFLAG))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Send STOP Condition */
I2C_GenerateSTOP(IOE_I2C, ENABLE);
/* Disable DMA RX Channel */
DMA_Cmd(IOE_DMA_RX_CHANNEL, DISABLE);
/* Disable I2C DMA request */
I2C_DMACmd(IOE_I2C,DISABLE);
/* Clear DMA RX Transfer Complete Flag */
DMA_ClearFlag(IOE_DMA_RX_TCFLAG);
/* return a pointer to the IOE_Buffer */
return (uint8_t)IOE_BufferRX[0];
}
示例5: I2C_Write_n_byteNS
/*******************************************************************************
* Function Name : I2C_Write_n_byteNS
* Description : Write n Bytes to address via the I2C Bus without generating stop
* Input : I2C TypeDef, I2C address, pointer to array of n Databytes
* Output : None
* Return : None
*******************************************************************************/
void I2C_Write_n_byteNS(I2C_TypeDef * I2Cx, u8 address, u8 n, u8 * data)
{
ErrorStatus state;
u32 timeout;
u8 ByteToWrite = n, ByteWrite = 0;
// Send START condition for receiving
I2C_GenerateSTART(I2Cx, ENABLE);
// Test on I2Cx EV5 and clear it
timeout = TIMEOUT;
state = ERROR;
while(!state && timeout--) state = I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT);
if(state == ERROR)
{
// Test on I2Cx Busy Flag
if(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
{
// Send STOP Condition
I2C_GenerateSTOP(I2Cx, ENABLE);
}
// SCF: Start condition failure
I2C_SetState(I2Cx, SCF);
return;
}
// Send EEPROM address for write
I2C_Send7bitAddress(I2Cx, address, I2C_Direction_Transmitter);
// Test on I2Cx EV6 and clear it
timeout = TIMEOUT;
state = ERROR;
while(!state && timeout--) state = I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED);
if(state == ERROR)
{
// Test on I2Cx Busy Flag
if(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
{
// Send STOP Condition
I2C_GenerateSTOP(I2Cx, ENABLE);
}
// ADNM: Address not matched to any slave
I2C_SetState(I2Cx, ADNM);
return;
}
while(0 < ByteToWrite)
{
// Write n Databytes
I2C_SendData(I2Cx, *(data + ByteWrite++));
// Decrease Number of byte to send
ByteToWrite--;
// Test on I2Cx EV8 and clear it
timeout = TIMEOUT;
state = ERROR;
while(!state && timeout--) state = I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED);
if(state == ERROR)
{
// Test on I2Cx Busy Flag
if(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
{
// Send STOP Condition
I2C_GenerateSTOP(I2Cx, ENABLE);
}
// B*NT: Byte * not transmitted (sets state to B1NT, B2NT, B3NT, B4NT or BxNT depending on ByteWrite)
I2C_SetState(I2Cx, ByteWrite < 5 ? TxError[ByteWrite - 1] : BxNT);
return;
}
}
// BTF: Byte transfer finished
I2C_SetState(I2Cx, BTF);
}
示例6: LM75_WriteReg
/**
* @brief Write to the specified register of the LM75.
* @param RegName: specifies the LM75 register to be written.
* This parameter can be one of the following values:
* @arg LM75_REG_TOS: Over-limit temperature register
* @arg LM75_REG_THYS: Hysteresis temperature register
* @param RegValue: value to be written to LM75 register.
* @retval None
*/
uint8_t LM75_WriteReg(uint8_t RegName, uint16_t RegValue)
{
uint32_t DataNum = 0;
uint8_t LM75_BufferTX[2] ={0,0};
LM75_BufferTX[0] = (uint8_t)(RegValue >> 8);
LM75_BufferTX[1] = (uint8_t)(RegValue);
/* Test on BUSY Flag */
LM75Timeout = LM75_LONG_TIMEOUT;
while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_BUSY) != RESET)
{
if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);
/* Wait until TXIS flag is set */
LM75Timeout = LM75_LONG_TIMEOUT;
while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
{
if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Send Register address */
I2C_SendData(LM75_I2C, (uint8_t)RegName);
/* Wait until TCR flag is set */
LM75Timeout = LM75_LONG_TIMEOUT;
while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TCR) == RESET)
{
if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(LM75_I2C, LM75_ADDR, 2, I2C_AutoEnd_Mode, I2C_No_StartStop);
while (DataNum != 2)
{
/* Wait until TXIS flag is set */
LM75Timeout = LM75_LONG_TIMEOUT;
while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
{
if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Write data to TXDR */
I2C_SendData(LM75_I2C, (uint8_t)(LM75_BufferTX[DataNum]));
/* Update number of transmitted data */
DataNum++;
}
/* Wait until STOPF flag is set */
LM75Timeout = LM75_LONG_TIMEOUT;
while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET)
{
if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Clear STOPF flag */
I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF);
return LM75_OK;
}
示例7: LM75_ShutDown
/**
* @brief Enables or disables the LM75.
* @param NewState: specifies the LM75 new status. This parameter can be ENABLE
* or DISABLE.
* @retval None
*/
uint8_t LM75_ShutDown(FunctionalState NewState)
{
uint8_t LM75_BufferRX[2] ={0,0};
uint8_t LM75_BufferTX = 0;
__IO uint8_t RegValue = 0;
/* Test on BUSY Flag */
LM75Timeout = LM75_LONG_TIMEOUT;
while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_BUSY) != RESET)
{
if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
/* Wait until TXIS flag is set */
LM75Timeout = LM75_LONG_TIMEOUT;
while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
{
if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Send Register address */
I2C_SendData(LM75_I2C, (uint8_t)LM75_REG_CONF);
/* Wait until TC flag is set */
LM75Timeout = LM75_LONG_TIMEOUT;
while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TC) == RESET)
{
if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
/* Wait until RXNE flag is set */
LM75Timeout = LM75_LONG_TIMEOUT;
while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_RXNE) == RESET)
{
if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Read data from RXDR */
LM75_BufferRX[0]= I2C_ReceiveData(LM75_I2C);
/* Wait until STOPF flag is set */
LM75Timeout = LM75_LONG_TIMEOUT;
while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET)
{
if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Clear STOPF flag */
I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF);
/*!< Get received data */
RegValue = (uint8_t)LM75_BufferRX[0];
/*---------------------------- Transmission Phase ---------------------------*/
/*!< Enable or disable SD bit */
if (NewState != DISABLE)
{
/*!< Enable LM75 */
LM75_BufferTX = RegValue & LM75_SD_RESET;
}
else
{
/*!< Disable LM75 */
LM75_BufferTX = RegValue | LM75_SD_SET;
}
/* Test on BUSY Flag */
LM75Timeout = LM75_LONG_TIMEOUT;
while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_BUSY) != RESET)
{
if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);
/* Wait until TXIS flag is set */
LM75Timeout = LM75_LONG_TIMEOUT;
while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
{
if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
}
/* Send Register address */
I2C_SendData(LM75_I2C, (uint8_t)LM75_REG_CONF);
/* Wait until TCR flag is set */
//.........这里部分代码省略.........
示例8: i2c_read
uint32_t i2c_read(i2c_dev *dev, uint8_t addr, uint8_t *tx_buff, uint8_t txlen, uint8_t *rx_buff, uint8_t *rxlen)
{
/* Set the pointer to the Number of data to be read. This pointer will be used
by the DMA Transfer Completer interrupt Handler in order to reset the
variable to 0. User should check on this variable in order to know if the
DMA transfer has been complete or not. */
if(dev->I2Cx == I2C1)
I2C1DataReadPointer = (uint16_t*) rxlen;
else
I2C2DataReadPointer = (uint16_t*) rxlen;
//uint16_t received;
//int flags;
//uint16_t ptr;
uint8_t *buffer8 = rx_buff;
//errno_r = 0;
//received = 0;
// While the bus is busy
dev->timeout = sEE_LONG_TIMEOUT;
while (I2C_GetFlagStatus(dev->I2Cx, I2C_FLAG_BUSY ))
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
// Send START condition
I2C_GenerateSTART(dev->I2Cx, ENABLE);
// Test on EV5 and clear it (cleared by reading SR1 then writing to DR)
dev->timeout = sEE_FLAG_TIMEOUT;
while (!I2C_CheckEvent(dev->I2Cx, I2C_EVENT_MASTER_MODE_SELECT ))
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
// Send address for write
I2C_Send7bitAddress(dev->I2Cx, addr, I2C_Direction_Transmitter );
dev->timeout = sEE_FLAG_TIMEOUT;
// Test on EV6 and clear it
while (!I2C_CheckEvent(dev->I2Cx,
I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ))
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
I2C_SendData(dev->I2Cx, *tx_buff++);
// Test on EV8 and clear it
dev->timeout = sEE_FLAG_TIMEOUT;
while (I2C_GetFlagStatus(dev->I2Cx, I2C_FLAG_BTF ) == RESET)
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
// Send STRAT condition a second time
I2C_GenerateSTART(dev->I2Cx, ENABLE);
// Test on EV5 and clear it (cleared by reading SR1 then writing to DR)
dev->timeout = sEE_FLAG_TIMEOUT;
while (!I2C_CheckEvent(dev->I2Cx, I2C_EVENT_MASTER_MODE_SELECT ))
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
// Send address for read
I2C_Send7bitAddress(dev->I2Cx, addr, I2C_Direction_Receiver );
if ((uint16_t)(*rxlen) < 2)
{
dev->timeout = sEE_FLAG_TIMEOUT;
while (I2C_GetFlagStatus(dev->I2Cx, I2C_FLAG_ADDR ) == RESET)
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
// Disable Acknowledgement
I2C_AcknowledgeConfig(dev->I2Cx, DISABLE);
/* Clear ADDR register by reading SR1 then SR2 register (SR1 has already been read) */
(void) dev->I2Cx->SR2;
/*!< STOP condition */
I2C_GenerateSTOP(dev->I2Cx, DISABLE);
I2C_ClearFlag(dev->I2Cx, I2C_FLAG_STOPF );
/* Send STOP condition */
I2C_GenerateSTOP(dev->I2Cx, ENABLE);
/* Wait for the byte to be received */
dev->timeout = sEE_FLAG_TIMEOUT;
while (I2C_GetFlagStatus(dev->I2Cx, I2C_FLAG_RXNE ) == RESET)
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
//.........这里部分代码省略.........
示例9: sEE_ReadBuffer
/**
* @brief Reads a block of data from the EEPROM.
* @param pBuffer : pointer to the buffer that receives the data read from
* the EEPROM.
* @param ReadAddr : EEPROM's internal address to start reading from.
* @param NumByteToRead : pointer to the variable holding number of bytes to
* be read from the EEPROM.
*
* @note The variable pointed by NumByteToRead is reset to 0 when all the
* data are read from the EEPROM. Application should monitor this
* variable in order know when the transfer is complete.
*
* @note When number of data to be read is higher than 1, this function just
* configures the communication and enable the DMA channel to transfer data.
* Meanwhile, the user application may perform other tasks.
* When number of data to be read is 1, then the DMA is not used. The byte
* is read in polling mode.
*
* @retval sEE_OK (0) if operation is correctly performed, else return value
* different from sEE_OK (0) or the timeout user callback.
*/
uint32_t sEE_ReadBuffer(i2c_dev *dev, uint8_t* pBuffer, uint16_t ReadAddr,
uint16_t* NumByteToRead)
{
/* Set the pointer to the Number of data to be read. This pointer will be used
by the DMA Transfer Completer interrupt Handler in order to reset the
variable to 0. User should check on this variable in order to know if the
DMA transfer has been complete or not. */
I2C2DataReadPointer = NumByteToRead;
/*!< While the bus is busy */
sEETimeout = sEE_LONG_TIMEOUT;
while (I2C_GetFlagStatus(dev->I2Cx, I2C_FLAG_BUSY ))
{
if ((sEETimeout--) == 0)
return I2C_ERROR;
}
/*!< Send START condition */
I2C_GenerateSTART(dev->I2Cx, ENABLE);
/*!< Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
sEETimeout = sEE_FLAG_TIMEOUT;
while (!I2C_CheckEvent(dev->I2Cx, I2C_EVENT_MASTER_MODE_SELECT ))
{
if ((sEETimeout--) == 0)
return I2C_ERROR;
}
/*!< Send EEPROM address for write */
I2C_Send7bitAddress(dev->I2Cx, sEEAddress, I2C_Direction_Transmitter );
/*!< Test on EV6 and clear it */
sEETimeout = sEE_FLAG_TIMEOUT;
while (!I2C_CheckEvent(dev->I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ))
{
if ((sEETimeout--) == 0)
return I2C_ERROR;
}
/*!< Send the EEPROM's internal address to read from: MSB of the address first */
I2C_SendData(dev->I2Cx, (uint8_t)((ReadAddr & 0xFF00) >> 8));
/*!< Test on EV8 and clear it */
sEETimeout = sEE_FLAG_TIMEOUT;
while (!I2C_CheckEvent(dev->I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING ))
{
if ((sEETimeout--) == 0)
return I2C_ERROR;
}
/*!< Send the EEPROM's internal address to read from: LSB of the address */
I2C_SendData(dev->I2Cx, (uint8_t)(ReadAddr & 0x00FF));
/*!< Test on EV8 and clear it */
sEETimeout = sEE_FLAG_TIMEOUT;
while (I2C_GetFlagStatus(dev->I2Cx, I2C_FLAG_BTF ) == RESET)
{
if ((sEETimeout--) == 0)
return I2C_ERROR;
}
/*!< Send STRAT condition a second time */
I2C_GenerateSTART(dev->I2Cx, ENABLE);
/*!< Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
sEETimeout = sEE_FLAG_TIMEOUT;
while (!I2C_CheckEvent(dev->I2Cx, I2C_EVENT_MASTER_MODE_SELECT ))
{
if ((sEETimeout--) == 0)
return I2C_ERROR;
}
/*!< Send EEPROM address for read */
I2C_Send7bitAddress(dev->I2Cx, sEEAddress, I2C_Direction_Receiver );
/* If number of data to be read is 1, then DMA couldn't be used */
/* One Byte Master Reception procedure (POLLING) ---------------------------*/
if ((uint16_t)(*NumByteToRead) < 2)
{
//.........这里部分代码省略.........
示例10: sEE_WaitEepromStandby
/**
* @brief Wait for EEPROM Standby state.
*
* @note This function allows to wait and check that EEPROM has finished the
* last operation. It is mostly used after Write operation: after receiving
* the buffer to be written, the EEPROM may need additional time to actually
* perform the write operation. During this time, it doesn't answer to
* I2C packets addressed to it. Once the write operation is complete
* the EEPROM responds to its address.
*
* @param None
* @retval sEE_OK (0) if operation is correctly performed, else return value
* different from sEE_OK (0) or the timeout user callback.
*/
uint32_t sEE_WaitEepromStandby(i2c_dev *dev, uint8_t addr)
{
__IO uint16_t tmpSR1 = 0;
__IO uint32_t sEETrials = 0;
/*!< While the bus is busy */
dev->timeout = I2C_LONG_TIMEOUT;
while (I2C_GetFlagStatus(dev->I2Cx, I2C_FLAG_BUSY ))
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
/* Keep looping till the slave acknowledge his address or maximum number
of trials is reached (this number is defined by sEE_MAX_TRIALS_NUMBER define
in STM324x7I_eval_i2c_ee.h file) */
while (1)
{
//Disable interrupts
//I2C_ITConfig(dev->I2Cx, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR, DISABLE);
/*!< Send START condition */
I2C_GenerateSTART(dev->I2Cx, ENABLE);
/*!< Test on EV5 and clear it */
dev->timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(dev->I2Cx, I2C_EVENT_MASTER_MODE_SELECT ))
{
}
if ((dev->timeout--) == 0)
{
//I2C_ITConfig(dev->I2Cx, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR, ENABLE);
return I2C_ERROR;
}
/*!< Send EEPROM address for write */
I2C_Send7bitAddress(dev->I2Cx, addr, I2C_Direction_Transmitter );
/* Wait for ADDR flag to be set (Slave acknowledged his address) */
dev->timeout = I2C_LONG_TIMEOUT;
do
{
/* Get the current value of the SR1 register */
tmpSR1 = dev->I2Cx->SR1;
/* Update the timeout value and exit if it reach 0 */
if ((dev->timeout--) == 0)
{
//I2C_ITConfig(dev->I2Cx, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR, ENABLE);
return I2C_ERROR;
}
}
/* Keep looping till the Address is acknowledged or the AF flag is
set (address not acknowledged at time) */
while ((tmpSR1 & (I2C_SR1_ADDR | I2C_SR1_AF ))== 0);
/* Check if the ADDR flag has been set */
if( tmpSR1 & I2C_SR1_ADDR)
{
/* Clear ADDR Flag by reading SR1 then SR2 registers (SR1 have already
been read) */
(void)dev->I2Cx->SR2;
/*!< STOP condition */
I2C_GenerateSTOP(dev->I2Cx, ENABLE);
//I2C_ITConfig(dev->I2Cx, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR, ENABLE);
/* Exit the function */
return I2C_OK;
}
else
{
/*!< Clear AF flag */
I2C_ClearFlag(dev->I2Cx, I2C_FLAG_AF);
}
/* Check if the maximum allowed number of trials has bee reached */
if (sEETrials++ == sEE_MAX_TRIALS_NUMBER)
{
//I2C_ITConfig(dev->I2Cx, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR, ENABLE);
/* If the maximum number of trials has been reached, exit the function */
return I2C_ERROR;
}
//.........这里部分代码省略.........
示例11: i2c_write
/* Send a buffer to the i2c port */
uint32_t i2c_write(i2c_dev *dev, uint8_t addr, uint8_t *tx_buff, uint8_t *len)
{
/* Set the pointer to the Number of data to be written. This pointer will be used
by the DMA Transfer Completer interrupt Handler in order to reset the
variable to 0. User should check on this variable in order to know if the
DMA transfer has been complete or not. */
if(dev->I2Cx == I2C1)
I2C1DataWritePointer = len;
else
I2C2DataWritePointer = len;
uint16_t sent = 0;
uint8_t *buffer = tx_buff;
sent = 0;
// While the bus is busy
/*!< While the bus is busy */
dev->timeout = sEE_LONG_TIMEOUT;
while (I2C_GetFlagStatus(dev->I2Cx, I2C_FLAG_BUSY ))
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
// Send START condition
I2C_GenerateSTART(dev->I2Cx, ENABLE);
// Test on EV5 and clear it (cleared by reading SR1 then writing to DR)
dev->timeout = sEE_FLAG_TIMEOUT;
while (!I2C_CheckEvent(dev->I2Cx, I2C_EVENT_MASTER_MODE_SELECT ))
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
// Send address for write
I2C_Send7bitAddress(dev->I2Cx, addr, I2C_Direction_Transmitter );
dev->timeout = sEE_FLAG_TIMEOUT;
// Test on EV6 and clear it
while (!I2C_CheckEvent(dev->I2Cx,
I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ))
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
I2C_SendData(dev->I2Cx, *buffer++);
// Test on EV8 and clear it
dev->timeout = sEE_FLAG_TIMEOUT;
while (!I2C_CheckEvent(dev->I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED ))
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
if ((uint16_t)(*len) < 2)
{
/* Send the current byte */
I2C_SendData(dev->I2Cx, *buffer);
/* Point to the next byte to be written */
sent++;
/* Test on EV8 and clear it */
dev->timeout = sEE_FLAG_TIMEOUT;
while (!I2C_CheckEvent(dev->I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED ))
{
if ((dev->timeout--) == 0)
return I2C_ERROR;
}
/*!< STOP condition */
I2C_GenerateSTOP(dev->I2Cx, DISABLE);
I2C_ClearFlag(dev->I2Cx, I2C_FLAG_STOPF );
/* Send STOP condition */
I2C_GenerateSTOP(dev->I2Cx, ENABLE);
}
else
{
/* Configure the DMA Tx Channel with the buffer address and the buffer size */
sEE_LowLevel_DMAConfig(dev,(uint32_t) buffer, (uint8_t)(*len),
sEE_DIRECTION_TX);
/* Enable the DMA Tx Stream */
if (dev->I2Cx == I2C1){
DMA_Cmd(sEE_I2C1_DMA_STREAM_TX, ENABLE);
} else if(dev->I2Cx == I2C2){
DMA_Cmd(sEE_I2C2_DMA_STREAM_TX, ENABLE);
}
/* Enable the sEE_I2C peripheral DMA requests */
I2C_DMACmd(dev->I2Cx, ENABLE);
}
return I2C_OK;
}
示例12: sEE_WaitEepromStandbyState
/**
* @brief Wait for EEPROM Standby state.
*
* @note This function allows to wait and check that EEPROM has finished the
* last operation. It is mostly used after Write operation: after receiving
* the buffer to be written, the EEPROM may need additional time to actually
* perform the write operation. During this time, it doesn't answer to
* I2C packets addressed to it. Once the write operation is complete
* the EEPROM responds to its address.
*
* @param None
* @retval sEE_OK (0) if operation is correctly performed, else return value
* different from sEE_OK (0) or the timeout user callback.
*/
uint32_t sEE_WaitEepromStandbyState(void)
{
__IO uint16_t tmpSR1 = 0;
__IO uint32_t sEETrials = 0;
/*!< While the bus is busy */
sEETimeout = sEE_LONG_TIMEOUT;
while(I2C_GetFlagStatus(sEE_I2C, I2C_FLAG_BUSY))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
/* Keep looping till the slave acknowledge his address or maximum number
of trials is reached (this number is defined by sEE_MAX_TRIALS_NUMBER define
in stm324xg_eval_i2c_ee.h file) */
while (1)
{
/*!< Send START condition */
I2C_GenerateSTART(sEE_I2C, ENABLE);
/*!< Test on EV5 and clear it */
sEETimeout = sEE_FLAG_TIMEOUT;
while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
/*!< Send EEPROM address for write */
I2C_Send7bitAddress(sEE_I2C, sEEAddress, I2C_Direction_Transmitter);
/* Wait for ADDR flag to be set (Slave acknowledged his address) */
sEETimeout = sEE_LONG_TIMEOUT;
do
{
/* Get the current value of the SR1 register */
tmpSR1 = sEE_I2C->SR1;
/* Update the timeout value and exit if it reach 0 */
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
/* Keep looping till the Address is acknowledged or the AF flag is
set (address not acknowledged at time) */
while((tmpSR1 & (I2C_SR1_ADDR | I2C_SR1_AF)) == 0);
/* Check if the ADDR flag has been set */
if (tmpSR1 & I2C_SR1_ADDR)
{
/* Clear ADDR Flag by reading SR1 then SR2 registers (SR1 have already
been read) */
(void)sEE_I2C->SR2;
/*!< STOP condition */
I2C_GenerateSTOP(sEE_I2C, ENABLE);
/* Exit the function */
return sEE_OK;
}
else
{
/*!< Clear AF flag */
I2C_ClearFlag(sEE_I2C, I2C_FLAG_AF);
}
/* Check if the maximum allowed number of trials has bee reached */
if (sEETrials++ == sEE_MAX_TRIALS_NUMBER)
{
/* If the maximum number of trials has been reached, exit the function */
return sEE_TIMEOUT_UserCallback();
}
}
}
示例13: sEE_WritePage
/**
* @brief Writes more than one byte to the EEPROM with a single WRITE cycle.
*
* @note The number of bytes (combined to write start address) must not
* cross the EEPROM page boundary. This function can only write into
* the boundaries of an EEPROM page.
* This function doesn't check on boundaries condition (in this driver
* the function sEE_WriteBuffer() which calls sEE_WritePage() is
* responsible of checking on Page boundaries).
*
* @param pBuffer : pointer to the buffer containing the data to be written to
* the EEPROM.
* @param WriteAddr : EEPROM's internal address to write to.
* @param NumByteToWrite : pointer to the variable holding number of bytes to
* be written into the EEPROM.
*
* @note The variable pointed by NumByteToWrite is reset to 0 when all the
* data are written to the EEPROM. Application should monitor this
* variable in order know when the transfer is complete.
*
* @note This function just configure the communication and enable the DMA
* channel to transfer data. Meanwhile, the user application may perform
* other tasks in parallel.
*
* @retval sEE_OK (0) if operation is correctly performed, else return value
* different from sEE_OK (0) or the timeout user callback.
*/
uint32_t sEE_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t* NumByteToWrite)
{
/* Set the pointer to the Number of data to be written. This pointer will be used
by the DMA Transfer Completer interrupt Handler in order to reset the
variable to 0. User should check on this variable in order to know if the
DMA transfer has been complete or not. */
sEEDataWritePointer = NumByteToWrite;
/*!< While the bus is busy */
sEETimeout = sEE_LONG_TIMEOUT;
while(I2C_GetFlagStatus(sEE_I2C, I2C_FLAG_BUSY))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
/*!< Send START condition */
I2C_GenerateSTART(sEE_I2C, ENABLE);
/*!< Test on EV5 and clear it */
sEETimeout = sEE_FLAG_TIMEOUT;
while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
/*!< Send EEPROM address for write */
sEETimeout = sEE_FLAG_TIMEOUT;
I2C_Send7bitAddress(sEE_I2C, sEEAddress, I2C_Direction_Transmitter);
/*!< Test on EV6 and clear it */
sEETimeout = sEE_FLAG_TIMEOUT;
while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
#if defined sEE_M24C08 || defined sEE_AT24C02
/*!< Send the EEPROM's internal address to write to : only one byte Address */
I2C_SendData(sEE_I2C, WriteAddr);
#elif defined(sEE_M24C64_32)
/*!< Send the EEPROM's internal address to write to : MSB of the address first */
I2C_SendData(sEE_I2C, (uint8_t)((WriteAddr & 0xFF00) >> 8));
/*!< Test on EV8 and clear it */
sEETimeout = sEE_FLAG_TIMEOUT;
while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
/*!< Send the EEPROM's internal address to write to : LSB of the address */
I2C_SendData(sEE_I2C, (uint8_t)(WriteAddr & 0x00FF));
#endif /*!< sEE_M24C08 */
/*!< Test on EV8 and clear it */
sEETimeout = sEE_FLAG_TIMEOUT;
while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
/* Configure the DMA Tx Channel with the buffer address and the buffer size */
sEE_LowLevel_DMAConfig((uint32_t)pBuffer, (uint8_t)(*NumByteToWrite), sEE_DIRECTION_TX);
/* Enable the DMA Tx Stream */
DMA_Cmd(sEE_I2C_DMA_STREAM_TX, ENABLE);
/* If all operations OK, return sEE_OK (0) */
return sEE_OK;
//.........这里部分代码省略.........
示例14: main
/**
* @brief Main program.
* @param None
* @retval None
*/
void main()
{
/* I2C clock Enable*/
CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE);
#ifdef FAST_I2C_MODE
/* system_clock / 1 */
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
#else
/* system_clock / 2 */
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_2);
#endif
/* Initialize LEDs mounted on STM8L1526-EVAL board */
STM_EVAL_LEDInit(LED1);
STM_EVAL_LEDInit(LED2);
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
/* Initialize I2C peripheral */
I2C_Init(I2C1, I2C_SPEED, 0xA0,
I2C_Mode_I2C, I2C_DutyCycle_2,
I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit);
/* Enable Buffer and Event Interrupt*/
I2C_ITConfig(I2C1, (I2C_IT_TypeDef)(I2C_IT_EVT | I2C_IT_BUF) , ENABLE);
enableInterrupts();
/* TXBuffer initialization */
for (i = 0; i < BUFFERSIZE; i++)
TxBuffer[i] = i;
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE);
while (NumOfBytes);
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
/* Add a delay to be sure that communication is finished */
Delay(0xFFFF);
/***** reception phase ***/
/* Wait while the bus is busy */
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
#ifdef TEN_BITS_ADDRESS
/* Send Header to Slave for write */
I2C_SendData(I2C1, HEADER_ADDRESS_Write);
/* Test on EV9 and clear it*/
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_ADDRESS10));
/* Send slave Address */
I2C_Send7bitAddress(I2C1, (uint8_t)SLAVE_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* Repeated Start */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send Header to Slave for Read */
I2C_SendData(I2C1, HEADER_ADDRESS_Read);
/* Test on EV6 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
#else
/* Send slave Address for write */
I2C_Send7bitAddress(I2C1, SLAVE_ADDRESS, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
#endif /* TEN_BITS_ADDRESS */
/* While there is data to be read */
while (NumByteToRead)
{
#ifdef SAFE_PROCEDURE
if (NumByteToRead != 3) /* Receive bytes from first byte until byte N-3 */
{
while ((I2C_GetLastEvent(I2C1) & 0x04) != 0x04); /* Poll on BTF */
/* Read a byte from the Slave */
RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
//.........这里部分代码省略.........
示例15: i2cInit
void i2cInit(I2CDevice device)
{
i2cDevice_t *i2c;
i2c = &(i2cHardwareMap[device]);
I2C_TypeDef *I2Cx;
I2Cx = i2c->dev;
IO_t scl = IOGetByTag(i2c->scl);
IO_t sda = IOGetByTag(i2c->sda);
RCC_ClockCmd(i2c->rcc, ENABLE);
RCC_I2CCLKConfig(I2Cx == I2C2 ? RCC_I2C2CLK_SYSCLK : RCC_I2C1CLK_SYSCLK);
IOInit(scl, OWNER_I2C, RESOURCE_I2C_SCL, RESOURCE_INDEX(device));
IOConfigGPIOAF(scl, IOCFG_I2C, GPIO_AF_4);
IOInit(sda, OWNER_I2C, RESOURCE_I2C_SDA, RESOURCE_INDEX(device));
IOConfigGPIOAF(sda, IOCFG_I2C, GPIO_AF_4);
I2C_InitTypeDef i2cInit = {
.I2C_Mode = I2C_Mode_I2C,
.I2C_AnalogFilter = I2C_AnalogFilter_Enable,
.I2C_DigitalFilter = 0x00,
.I2C_OwnAddress1 = 0x00,
.I2C_Ack = I2C_Ack_Enable,
.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit,
.I2C_Timing = (i2c->overClock ? I2C_HIGHSPEED_TIMING : I2C_STANDARD_TIMING)
};
I2C_Init(I2Cx, &i2cInit);
I2C_StretchClockCmd(I2Cx, ENABLE);
I2C_Cmd(I2Cx, ENABLE);
}
uint16_t i2cGetErrorCounter(void)
{
return i2cErrorCount;
}
bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t data)
{
addr_ <<= 1;
I2C_TypeDef *I2Cx;
I2Cx = i2cHardwareMap[device].dev;
/* Test on BUSY Flag */
i2cTimeout = I2C_LONG_TIMEOUT;
while (I2C_GetFlagStatus(I2Cx, I2C_ISR_BUSY) != RESET) {
if ((i2cTimeout--) == 0) {
return i2cTimeoutUserCallback();
}
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(I2Cx, addr_, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);
/* Wait until TXIS flag is set */
i2cTimeout = I2C_LONG_TIMEOUT;
while (I2C_GetFlagStatus(I2Cx, I2C_ISR_TXIS) == RESET) {
if ((i2cTimeout--) == 0) {
return i2cTimeoutUserCallback();
}
}
/* Send Register address */
I2C_SendData(I2Cx, (uint8_t) reg);
/* Wait until TCR flag is set */
i2cTimeout = I2C_LONG_TIMEOUT;
while (I2C_GetFlagStatus(I2Cx, I2C_ISR_TCR) == RESET)
{
if ((i2cTimeout--) == 0) {
return i2cTimeoutUserCallback();
}
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(I2Cx, addr_, 1, I2C_AutoEnd_Mode, I2C_No_StartStop);
/* Wait until TXIS flag is set */
i2cTimeout = I2C_LONG_TIMEOUT;
while (I2C_GetFlagStatus(I2Cx, I2C_ISR_TXIS) == RESET) {
if ((i2cTimeout--) == 0) {
return i2cTimeoutUserCallback();
}
}
/* Write data to TXDR */
I2C_SendData(I2Cx, data);
/* Wait until STOPF flag is set */
i2cTimeout = I2C_LONG_TIMEOUT;
while (I2C_GetFlagStatus(I2Cx, I2C_ISR_STOPF) == RESET) {
if ((i2cTimeout--) == 0) {
return i2cTimeoutUserCallback();
//.........这里部分代码省略.........