本文整理汇总了C++中I2C_ReceiveData函数的典型用法代码示例。如果您正苦于以下问题:C++ I2C_ReceiveData函数的具体用法?C++ I2C_ReceiveData怎么用?C++ I2C_ReceiveData使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了I2C_ReceiveData函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: I2C_Read8
uint8_t I2C_Read8(uint8_t addr, uint8_t reg)
{
uint8_t data = 0;
I2C_AcknowledgeConfig(I2C1, ENABLE);
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_GetFlagStatus(I2C1, I2C_FLAG_SB));
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, addr<<1,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, reg);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_GetFlagStatus(I2C1, I2C_FLAG_SB));
I2C_Send7bitAddress(I2C1, addr<<1, I2C_Direction_Receiver);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_NACKPositionConfig(I2C1, I2C_NACKPosition_Current);
I2C_AcknowledgeConfig(I2C1, DISABLE);
//need to add a wait here for new data, right now it goes to fast to get the new data.
while(!I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE));
data = I2C_ReceiveData(I2C1);
//while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED));
I2C_GenerateSTOP(I2C1, ENABLE);
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF));
return data;
}
示例2: I2CRXByte
unsigned char I2CRXByte(I2C_TypeDef *I2Cx)
{
unsigned short temp;
#ifdef I2C_INT_MODE
I2C_ITConfig(I2Cx, I2C_IT_RX_FULL, ENABLE);
while(1)
{
if(rx_flag)
{
rx_flag = 0;
break;
}
}
#else
I2CRXFullCheck(I2Cx);
#endif
temp = I2C_ReceiveData(I2Cx);
return (unsigned char)temp;
}
示例3: crI2c
//.........这里部分代码省略.........
if ( idc->elapsed++ > idc->timeout )
{
idc->status = I2C_ERROR_BTF;
I2C_GenerateSTOP( idc->i2c, ENABLE );
goto i2c_end;
}
crDELAY( xHandle, 1 );
idc = i2c( uxIndex );
}
}
// Receiving data if necessary.
if ( idc->receiveCnt )
{
I2C_AcknowledgeConfig( idc->i2c, ENABLE );
// Generate START condition if there was at least one byte written.
I2C_GenerateSTART( idc->i2c, ENABLE );
idc->status = I2C_MMS_R;
// Wait for SB to be set
idc->elapsed = 0;
while ( !I2C_CheckEvent( idc->i2c, I2C_EVENT_MASTER_MODE_SELECT ) )
{
if ( idc->elapsed++ > idc->timeout )
{
idc->status = I2C_ERROR_MMS_R;
I2C_GenerateSTOP( idc->i2c, ENABLE );
goto i2c_end;
}
crDELAY( xHandle, 1 );
idc = i2c( uxIndex );
}
I2C_Send7bitAddress( idc->i2c, idc->address, I2C_Direction_Receiver );
// Test on ADDR Flag
idc->status = I2C_RMS;
idc->elapsed = 0;
while ( !I2C_CheckEvent( idc->i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ) )
{
if ( idc->elapsed++ > idc->timeout )
{
idc->status = I2C_ERROR_RMS;
I2C_GenerateSTOP( idc->i2c, ENABLE );
goto i2c_end;
}
crDELAY( xHandle, 1 );
idc = i2c( uxIndex );
}
// Receiving a number of bytes from slave.
for ( i=0; i<idc->receiveCnt; i++ )
{
// Turn acknowledge off when reading the last byte.
if ( i == (idc->receiveCnt-1) )
I2C_AcknowledgeConfig( idc->i2c, DISABLE );
// Wait for data available.
idc->status = I2C_MBR;
idc->elapsed = 0;
while ( !I2C_CheckEvent( idc->i2c, I2C_EVENT_MASTER_BYTE_RECEIVED ) )
{
if ( idc->elapsed++ > idc->timeout )
{
idc->status = I2C_ERROR_MBR;
I2C_GenerateSTOP( idc->i2c, ENABLE );
goto i2c_end;
}
crDELAY( xHandle, 1 );
idc = i2c( uxIndex );
}
// Read the data.
uint8_t data = I2C_ReceiveData( idc->i2c );
idc->receiveQueue[i] = data;
idc->bytesRead = i+1;
}
}
// Generating STOP.
I2C_GenerateSTOP( idc->i2c, ENABLE );
// Idle status when finished in regular way.
idc->status = I2C_IDLE;
}
}
else // master.
{
// slave mode IO.
//...... implementation.....
// Idle status when finished in regular way.
//idc->status = I2C_IDLE;
crDELAY( xHandle, 1 );
}
i2c_end:
// To prevent cyclic writes of zero data.
idc->sendCnt = 0;
idc->receiveCnt = 0;
// Give other coroutines time for running.
crDELAY( xHandle, 1 );
}
crEND();
}
示例4: BH1750_Read
uint32_t BH1750_Read(void)
{
uint16_t BH_H;
uint16_t BH_L;
uint32_t value;
int iTimeout = DEF_TIMEOUT_I2C;
I2C_AcknowledgeConfig(BH1750_I2C,ENABLE); // Enable I2C acknowledge
I2C_GenerateSTART(BH1750_I2C,ENABLE); // Send START condition
iTimeout = DEF_TIMEOUT_I2C;
while (!I2C_CheckEvent(BH1750_I2C,I2C_EVENT_MASTER_MODE_SELECT)) { // Wait for EV5
iTimeout--;
if(!(iTimeout)) {
return 0;
}
}
iTimeout = DEF_TIMEOUT_I2C;
while (!I2C_CheckEvent(BH1750_I2C, I2C_EVENT_MASTER_MODE_SELECT)) { // Wait for EV5
iTimeout--;
if(!(iTimeout)) {
return 0;
}
}
I2C_Send7bitAddress(BH1750_I2C, BH1750_ADDRESS, I2C_Direction_Receiver); // Send slave address for READ
iTimeout = DEF_TIMEOUT_I2C;
while (!I2C_CheckEvent(BH1750_I2C,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) { // Wait for EV6
iTimeout--;
if(!(iTimeout)) {
return 0;
}
}
iTimeout = DEF_TIMEOUT_I2C;
while (!I2C_CheckEvent(BH1750_I2C,I2C_EVENT_MASTER_BYTE_RECEIVED)) { // Wait for EV7 (Byte received from slave)
iTimeout--;
if(!(iTimeout)) {
return 0;
}
}
BH_H = (uint16_t)I2C_ReceiveData(BH1750_I2C); // Receive MSB
iTimeout = DEF_TIMEOUT_I2C;
while (!I2C_CheckEvent(BH1750_I2C,I2C_EVENT_MASTER_BYTE_RECEIVED)) { // Wait for EV7 (Byte received from slave)
iTimeout--;
if(!(iTimeout)) {
return 0;
}
}
BH_L = (uint32_t)I2C_ReceiveData(BH1750_I2C); // Receive LSB
I2C_AcknowledgeConfig(BH1750_I2C,DISABLE); // Disable I2C acknowledgment
I2C_GenerateSTOP(BH1750_I2C,ENABLE); // Send STOP condition
value = BH_H*256 + BH_L;
value = value*10/12;
return value;
}
示例5: I2C_BufferRead
void I2C_BufferRead(I2C_TypeDef* I2Cx,u8* pBuffer,u8 devAddr, u8 ReadAddr, u16 NumByteToRead)
{
#if 0
/* Send START condition */
I2C_GenerateSTART(I2Cx, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2Cx, devAddr, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2Cx, ENABLE);
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2Cx, ReadAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2Cx, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2Cx, devAddr, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* While there is data to be read */
while(NumByteToRead)
{
if(NumByteToRead == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2Cx, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(I2Cx, ENABLE);
}
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the EEPROM */
*pBuffer = I2C_ReceiveData(I2Cx);
/* Point to the next location where the byte read will be saved */
pBuffer++;
/* Decrement the read bytes counter */
NumByteToRead--;
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(I2Cx, ENABLE);
#endif
}
示例6: 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 */
//.........这里部分代码省略.........
示例7: LSM303DLH_I2C_BufferRead
void LSM303DLH_I2C_BufferRead(u8 slAddr, u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{
iNEMO_ENTER_CRITICAL();
/* While the bus is busy */
while(I2C_GetFlagStatus(LSM_I2C, I2C_FLAG_BUSY));
/* Send START condition */
I2C_GenerateSTART(LSM_I2C, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(LSM_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send LSM303DLH_Magn address for write */
I2C_Send7bitAddress(LSM_I2C, slAddr, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(LSM_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(LSM_I2C, ENABLE);
/* Send the LSM303DLH_Magn's internal address to write to */
I2C_SendData(LSM_I2C, ReadAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(LSM_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STRAT condition a second time */
I2C_GenerateSTART(LSM_I2C, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(LSM_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send LSM303DLH address for read */
I2C_Send7bitAddress(LSM_I2C, slAddr, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(LSM_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* While there is data to be read */
while(NumByteToRead)
{
if(NumByteToRead == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(LSM_I2C, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(LSM_I2C, ENABLE);
}
/* Test on EV7 and clear it */
if(I2C_CheckEvent(LSM_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the LSM303DLH */
*pBuffer = I2C_ReceiveData(LSM_I2C);
/* Point to the next location where the byte read will be saved */
pBuffer++;
/* Decrement the read bytes counter */
NumByteToRead--;
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(LSM_I2C, ENABLE);
iNEMO_EXIT_CRITICAL();
}
示例8: AJS_TargetIO_I2cRead
uint8_t AJS_TargetIO_I2cRead(void* ctx)
{
I2C_Pin* i2c = (I2C_Pin*)ctx;
return I2C_ReceiveData(i2c->I2Cx);
}
示例9: main
/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* NVIC configuration ------------------------------------------------------*/
NVIC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
/* Enable I2C1 and I2C2 ----------------------------------------------------*/
I2C_Cmd(I2C1, ENABLE);
I2C_Cmd(I2C2, ENABLE);
/* I2C1 configuration ------------------------------------------------------*/
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
I2C_Init(I2C1, &I2C_InitStructure);
/* I2C2 configuration ------------------------------------------------------*/
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_10bit;
I2C_InitStructure.I2C_OwnAddress1 = I2C2_SLAVE_ADDRESS10;
I2C_Init(I2C2, &I2C_InitStructure);
/*----- Transmission Phase -----*/
/* Send I2C1 START condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on I2C1 EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send Header to I2C2 for write */
I2C_SendData(I2C1, HeaderAddressWrite);
/* Test on I2C1 EV9 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_ADDRESS10));
/* Send I2C2 slave Address for write */
I2C_Send7bitAddress(I2C1, I2C2_SLAVE_ADDRESS7, I2C_Direction_Transmitter);
/* Test on I2C2 EV1 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED));
/* Test on I2C1 EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Send data */
while (RxIdx < BufferSize)
{
/* Send I2C1 data */
I2C_SendData(I2C1, I2C1_Buffer_Tx[TxIdx++]);
/* Test on I2C2 EV2 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_SLAVE_BYTE_RECEIVED));
/* Store received data on I2C2 */
I2C2_Buffer_Rx[RxIdx++] = I2C_ReceiveData(I2C2);
/* Test on I2C1 EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
/* Send I2C1 STOP Condition */
I2C_GenerateSTOP(I2C1, ENABLE);
/* Test on I2C2 EV4 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_SLAVE_STOP_DETECTED));
/* Clear I2C2 STOPF flag: read operation to I2C_SR1 followed by a
write operation to I2C_CR1 */
(void)(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF));
I2C_Cmd(I2C2, ENABLE);
/* Check the corectness of written data */
TransferStatus = Buffercmp(I2C1_Buffer_Tx, I2C2_Buffer_Rx, BufferSize);
/* TransferStatus = PASSED, if the transmitted and received data
are equal */
/* TransferStatus = FAILED, if the transmitted and received data
are different */
while (1)
{
}
}
示例10: MPU9250_I2C_ByteRead
char MPU9250_I2C_ByteRead(u8 slaveAddr, u8 readAddr)
{
char tmp = 0;
// ENTR_CRT_SECTION();
/* While the bus is busy */
while (I2C_GetFlagStatus(MPU9250_I2C, I2C_FLAG_BUSY));
/* Send START condition */
I2C_GenerateSTART(MPU9250_I2C, ENABLE);
/* Test on EV5 and clear it */
while (!I2C_CheckEvent(MPU9250_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send MPU9250 address for write */
I2C_Send7bitAddress(MPU9250_I2C, slaveAddr, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while (!I2C_CheckEvent(MPU9250_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(MPU9250_I2C, ENABLE);
/* Send the MPU9250's internal address to write to */
I2C_SendData(MPU9250_I2C, readAddr);
/* Test on EV8 and clear it */
while (!I2C_CheckEvent(MPU9250_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STRAT condition a second time */
I2C_GenerateSTART(MPU9250_I2C, ENABLE);
/* Test on EV5 and clear it */
while (!I2C_CheckEvent(MPU9250_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send MPU9250 address for read */
I2C_Send7bitAddress(MPU9250_I2C, slaveAddr, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while (!I2C_CheckEvent(MPU9250_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* While there is data to be read */
for (;;) {
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(MPU9250_I2C, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(MPU9250_I2C, ENABLE);
/* Test on EV7 and clear it */
if (I2C_CheckEvent(MPU9250_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED)) {
/* Read a byte from the MPU9250 */
tmp = I2C_ReceiveData(MPU9250_I2C);
break;
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(MPU9250_I2C, ENABLE);
// EXT_CRT_SECTION();
return tmp;
}
示例11: TWI_MasterWriteRead
//.........这里部分代码省略.........
/*!< Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
sEETimeout = sEE_FLAG_TIMEOUT;
while(!I2C_CheckEvent(sEE_I2C[TwiStruct->TwiNr], I2C_EVENT_MASTER_MODE_SELECT))
{
if((sEETimeout--) == 0) {
return false;
}
}
/*!< Send EEPROM address for read */
I2C_Send7bitAddress(sEE_I2C[TwiStruct->TwiNr], TwiStruct->MasterSlaveAddr << 1, 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 (NumByteToRead < 2)
//{
/* Wait on ADDR flag to be set (ADDR is still not cleared at this level */
sEETimeout = sEE_FLAG_TIMEOUT;
while(I2C_GetFlagStatus(sEE_I2C[TwiStruct->TwiNr], I2C_FLAG_ADDR) == RESET)
{
if((sEETimeout--) == 0) {
return false;
}
}
(void)sEE_I2C[TwiStruct->TwiNr]->SR2;
cnt = 0;
for(; cnt < ReceiveBytes; cnt++)
{
if(cnt == ReceiveBytes - 1)
{
/*!< Disable Acknowledgement */
I2C_AcknowledgeConfig(sEE_I2C[TwiStruct->TwiNr], DISABLE);
/* Call User callback for critical section start (should typically disable interrupts) */
sEE_EnterCriticalSection_UserCallback();
/* Clear ADDR register by reading SR1 then SR2 register (SR1 has already been read) */
(void)sEE_I2C[TwiStruct->TwiNr]->SR2;
/*!< Send STOP Condition */
I2C_GenerateSTOP(sEE_I2C[TwiStruct->TwiNr], ENABLE);
/* Call User callback for critical section end (should typically re-enable interrupts) */
sEE_ExitCriticalSection_UserCallback();
}
/* Wait for the byte to be received */
sEETimeout = sEE_FLAG_TIMEOUT;
while(I2C_GetFlagStatus(sEE_I2C[TwiStruct->TwiNr], I2C_FLAG_RXNE) == RESET)
{
if((sEETimeout--) == 0) {
return false;
}
}
/*!< Read the byte received from the EEPROM */
TwiStruct->RxBuff[cnt] = I2C_ReceiveData(sEE_I2C[TwiStruct->TwiNr]);
}
/* Wait to make sure that STOP control bit has been cleared */
sEETimeout = sEE_FLAG_TIMEOUT;
while(sEE_I2C[TwiStruct->TwiNr]->CR1 & I2C_CR1_STOP)
{
if((sEETimeout--) == 0) {
return false;
}
}
/*!< Re-Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(sEE_I2C[TwiStruct->TwiNr], ENABLE);
}
}
else {
/* Call User callback for critical section start (should typically disable interrupts) */
sEE_EnterCriticalSection_UserCallback();
/* Clear ADDR register by reading SR1 then SR2 register (SR1 has already been read) */
(void)sEE_I2C[TwiStruct->TwiNr]->SR2;
/*!< Send STOP Condition */
I2C_GenerateSTOP(sEE_I2C[TwiStruct->TwiNr], ENABLE);
/* Call User callback for critical section end (should typically re-enable interrupts) */
sEE_ExitCriticalSection_UserCallback();
/* Wait for the byte to be received */
sEETimeout = sEE_FLAG_TIMEOUT;
while(I2C_GetFlagStatus(sEE_I2C[TwiStruct->TwiNr], I2C_FLAG_RXNE) == RESET)
{
if((sEETimeout--) == 0) {
return true;
}
}
}
/* If all operations OK, return sEE_OK (0) */
return true;
}
示例12: HAL_I2C_EV_InterruptHandler
static void HAL_I2C_EV_InterruptHandler(HAL_I2C_Interface i2c)
{
/* Process Last I2C Event */
switch (I2C_GetLastEvent(i2cMap[i2c]->I2C_Peripheral))
{
/********** Slave Transmitter Events ************/
/* Check on EV1 */
case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:
i2cMap[i2c]->transmitting = 1;
i2cMap[i2c]->txBufferIndex = 0;
i2cMap[i2c]->txBufferLength = 0;
if(NULL != i2cMap[i2c]->callback_onRequest)
{
// alert user program
i2cMap[i2c]->callback_onRequest();
}
i2cMap[i2c]->txBufferIndex = 0;
break;
/* Check on EV3 */
case I2C_EVENT_SLAVE_BYTE_TRANSMITTING:
case I2C_EVENT_SLAVE_BYTE_TRANSMITTED:
if (i2cMap[i2c]->txBufferIndex < i2cMap[i2c]->txBufferLength)
{
I2C_SendData(i2cMap[i2c]->I2C_Peripheral, i2cMap[i2c]->txBuffer[i2cMap[i2c]->txBufferIndex++]);
}
break;
/*********** Slave Receiver Events *************/
/* check on EV1*/
case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
i2cMap[i2c]->rxBufferIndex = 0;
i2cMap[i2c]->rxBufferLength = 0;
break;
/* Check on EV2*/
case I2C_EVENT_SLAVE_BYTE_RECEIVED:
case (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_SR1_BTF):
i2cMap[i2c]->rxBuffer[i2cMap[i2c]->rxBufferIndex++] = I2C_ReceiveData(i2cMap[i2c]->I2C_Peripheral);
break;
/* Check on EV4 */
case I2C_EVENT_SLAVE_STOP_DETECTED:
/* software sequence to clear STOPF */
I2C_GetFlagStatus(i2cMap[i2c]->I2C_Peripheral, I2C_FLAG_STOPF);
I2C_Cmd(i2cMap[i2c]->I2C_Peripheral, ENABLE);
i2cMap[i2c]->rxBufferLength = i2cMap[i2c]->rxBufferIndex;
i2cMap[i2c]->rxBufferIndex = 0;
if(NULL != i2cMap[i2c]->callback_onReceive)
{
// alert user program
i2cMap[i2c]->callback_onReceive(i2cMap[i2c]->rxBufferLength);
}
break;
default:
break;
}
}
示例13: HAL_I2C_Request_Data
uint32_t HAL_I2C_Request_Data(HAL_I2C_Interface i2c, uint8_t address, uint8_t quantity, uint8_t stop, void* reserved)
{
uint32_t _millis;
uint8_t bytesRead = 0;
// clamp to buffer length
if(quantity > BUFFER_LENGTH)
{
quantity = BUFFER_LENGTH;
}
/* Send START condition */
I2C_GenerateSTART(i2cMap[i2c]->I2C_Peripheral, ENABLE);
_millis = HAL_Timer_Get_Milli_Seconds();
while(!I2C_CheckEvent(i2cMap[i2c]->I2C_Peripheral, I2C_EVENT_MASTER_MODE_SELECT))
{
if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis))
{
/* SW Reset the I2C Peripheral */
HAL_I2C_SoftwareReset(i2c);
return 0;
}
}
/* Initialized variables here to minimize delays
* between sending of slave addr and read loop
*/
uint8_t *pBuffer = i2cMap[i2c]->rxBuffer;
uint8_t numByteToRead = quantity;
/* Ensure ackFailure flag is cleared */
i2cMap[i2c]->ackFailure = false;
/* Set ACK/NACK prior to I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED check
* to minimize delays between sending of slave addr and read loop.
* I2C_CheckEvent() will clear ADDR bit and allow SCL clocks to run, so we don't
* have much time to check/set this correctly while I2C is in operation.
*/
if (quantity == 1)
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(i2cMap[i2c]->I2C_Peripheral, DISABLE);
else
I2C_AcknowledgeConfig(i2cMap[i2c]->I2C_Peripheral, ENABLE);
/* Send Slave address for read */
I2C_Send7bitAddress(i2cMap[i2c]->I2C_Peripheral, address << 1, I2C_Direction_Receiver);
_millis = HAL_Timer_Get_Milli_Seconds();
while(!I2C_CheckEvent(i2cMap[i2c]->I2C_Peripheral, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
/* STOP/RESET immediately if ACK failure detected */
if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis) || i2cMap[i2c]->ackFailure)
{
/* Send STOP Condition */
I2C_GenerateSTOP(i2cMap[i2c]->I2C_Peripheral, ENABLE);
/* Wait to make sure that STOP control bit has been cleared */
_millis = HAL_Timer_Get_Milli_Seconds();
while(i2cMap[i2c]->I2C_Peripheral->CR1 & I2C_CR1_STOP)
{
if(EVENT_TIMEOUT < (HAL_Timer_Get_Milli_Seconds() - _millis))
{
break;
}
}
/* SW Reset the I2C Peripheral */
HAL_I2C_SoftwareReset(i2c);
/* Ensure ackFailure flag is cleared */
i2cMap[i2c]->ackFailure = false;
return 0;
}
}
/* DATA on SDA --> Shift Register (SR) [8 clocks by SCL] --> Data Register (DR) --> ACK/NACK (9th clock)
*
* SINGLE BYTE READ
* - SCL will run immediately after ADDR bit is cleared by reading SR2 (any I2C_CheckEvent)
* - DR will be empty
* - 1st byte from slave will be transferred to DR after 8th SCL clock
* - ACK/NACK will be sent immediately (SCL will run 9th clock)
*
* 2 BYTES LEFT TO READ
* - DR is not yet empty (2nd last byte is in DR; it was ACKed on its xfer to DR)
* - Last byte is being assembled in SR
* - SCL will be stretched after 8th clock (SR full) until DR is empty
* - Reading DR for 2nd last byte will then move last byte to DR and send ACK/NACK
*
* While there is data to be read, perform blocking read into buffer
*/
while(numByteToRead)
{
/* Wait for DR to be full. As soon as DR is full, the byte that was xfer'd from SR
* to DR will be ACK/NACK'd. So it is important to enable/disable ACK bit ahead of this event
*/
_millis = HAL_Timer_Get_Milli_Seconds();
//.........这里部分代码省略.........
示例14: i2c1_read
int i2c1_read(uint8_t u8addr, uint8_t *u8data,uint8_t u8qty)
{
volatile uint32_t u32timeout;
uint8_t recived = 0;
/* While the bus is busy */
//u32timeout = I2C_WAIT_TIMEOUT;
//while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)&& --u32timeout);
//if (!u32timeout) return 1;
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
u32timeout = I2C_WAIT_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)&& --u32timeout);
if (!u32timeout) return fail;
/* Send paj7620 address for write */
I2C_Send7bitAddress(I2C1, APDS9960_I2C_ADDR << 1, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
u32timeout = I2C_WAIT_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)&& --u32timeout);
if (!u32timeout) return fail;
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2C1, ENABLE);
/* Send the paj7620's internal address to write to */
I2C_SendData(I2C1, u8addr);
/* Test on EV8 and clear it */
u32timeout = I2C_WAIT_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)&& --u32timeout);
if (!u32timeout) return fail;
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
u32timeout = I2C_WAIT_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)&& --u32timeout);
if (!u32timeout) return fail;
/* Send paj7620 address for read */
I2C_Send7bitAddress(I2C1, APDS9960_I2C_ADDR << 1, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
u32timeout = I2C_WAIT_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)&& --u32timeout);
if (!u32timeout) return fail;
/* While there is data to be read */
while(u8qty)
{
if(u8qty == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C1, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(I2C1, ENABLE);
}
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the paj7620 */
//*u8data = I2C_ReceiveData(I2C1);
u8data[recived] = I2C_ReceiveData(I2C1);
/* Point to the next location where the byte read will be saved */
//u8data++;
recived++;
/* Decrement the read bytes counter */
u8qty--;
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(I2C1, ENABLE);
return recived;
}/* End of this function */
示例15: sEE_ReadBuffer
uint32_t sEE_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
{
/* While the bus is busy */
sEETimeout = sEE_LONG_TIMEOUT;
while(I2C_GetFlagStatus( I2C_FLAG_BUSBUSY))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
/* Send START condition */
I2C_GenerateSTART(ENABLE);
/* Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
sEETimeout = sEE_FLAG_TIMEOUT;
while(!I2C_CheckEvent( I2C_EVENT_MASTER_MODE_SELECT))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
/* Send EEPROM address for write */
I2C_Send7bitAddress( (uint8_t)sEEAddress, I2C_DIRECTION_TX);
/* Test on EV6 and clear it */
sEETimeout = sEE_FLAG_TIMEOUT;
while(!I2C_CheckEvent( I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
#ifdef sEE_M24C64_32
/* Send the EEPROM's internal address to read from: MSB of the address first */
I2C_SendData( (uint8_t)((ReadAddr & 0xFF00) >> 8));
/* Test on EV8 and clear it */
sEETimeout = sEE_FLAG_TIMEOUT;
while(!I2C_CheckEvent( I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
#endif /* sEE_M24C64_32 */
/* Send the EEPROM's internal address to read from: LSB of the address */
I2C_SendData( (uint8_t)(ReadAddr & 0x00FF));
/* Test on EV8 and clear it */
sEETimeout = sEE_FLAG_TIMEOUT;
while(I2C_GetFlagStatus(I2C_FLAG_TRANSFERFINISHED) == RESET)
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
/* Send START condition a second time */
I2C_GenerateSTART( ENABLE);
/* Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
sEETimeout = sEE_FLAG_TIMEOUT;
while(!I2C_CheckEvent( I2C_EVENT_MASTER_MODE_SELECT))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
/* Send EEPROM address for read */
I2C_Send7bitAddress((uint8_t)sEEAddress, I2C_DIRECTION_RX);
if ((uint16_t)(NumByteToRead)> 2)
{
sEETimeout = sEE_FLAG_TIMEOUT;
while(!I2C_GetFlagStatus( I2C_FLAG_ADDRESSSENTMATCHED))
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
I2C->SR3;
/* Read data from first byte until byte N-3 */
while ((uint16_t)(NumByteToRead)> 3)
{
/* Poll on BTF */
sEETimeout = sEE_FLAG_TIMEOUT;
while (I2C_GetFlagStatus( I2C_FLAG_TRANSFERFINISHED) == RESET)
{
if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
}
/* Read a byte from the EEPROM */
*pBuffer = I2C_ReceiveData();
/* Point to the next location where the byte read will be saved */
pBuffer++;
/* Decrement the read bytes counter */
(uint16_t)(NumByteToRead)--;
}
//}
/* Remains three data for read: data N-2, data N-1, Data N */
/* Three Bytes Master Reception procedure (POLLING) ------------------------*/
// if ((uint16_t)(*NumByteToRead) == 3)
//{
//.........这里部分代码省略.........