本文整理汇总了C++中I2C_GenerateSTART函数的典型用法代码示例。如果您正苦于以下问题:C++ I2C_GenerateSTART函数的具体用法?C++ I2C_GenerateSTART怎么用?C++ I2C_GenerateSTART使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了I2C_GenerateSTART函数的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_RandRead
int I2C_RandRead(uint8_t slave,uint16_t addr,uint8_t addrsize,void *pbuffer,uint16_t len)
{
struct i2c_job_st i2c_jobs[2];
uint8_t buf_offset[2];
xSemaphoreTake(xSemaphoreI2C_Mutex,portMAX_DELAY);
if (I2C_isBusy())
{
i2c_error_flags = I2C_SR1_SB;
xSemaphoreGive(xSemaphoreI2C_Mutex);
return FALSE;
}
i2c_cntr = 0;
i2c_oper = I2C_Direction_Transmitter;
job = i2c_jobs;
i2c_addr = slave << 1;
memset(i2c_jobs,0,sizeof(i2c_jobs));
if (addrsize == 1)
buf_offset[0] = (uint8_t)addr;
else
{
buf_offset[0] = (uint8_t)(addr >> 8);
buf_offset[1] = (uint8_t)(addr & 0xff);
}
i2c_jobs[0].buf = buf_offset;
i2c_jobs[0].len = addrsize;
i2c_jobs[0].dir = I2C_Direction_Transmitter;
i2c_jobs[1].buf = (uint8_t *)pbuffer;
i2c_jobs[1].len = len;
i2c_jobs[1].dir = I2C_Direction_Receiver;
i2c_jobs[1].last = TRUE;
i2c_error_flags = 0;
i2c_done = i2c_err = FALSE;
// clear the semaphore
while (xSemaphoreTake(xSemaphoreI2C_Work,0));
I2C_AcknowledgeConfig(I2Cx,ENABLE);
I2C_NACKPositionConfig(I2Cx,I2C_NACKPosition_Current);
I2C_ITConfig(I2Cx,I2C_IT_BUF | I2C_IT_ERR | I2C_IT_EVT,ENABLE);
/* Send START condition */
I2C_GenerateSTART(I2Cx, ENABLE);
while (!i2c_done && !i2c_err)
{
if (!xSemaphoreTake(xSemaphoreI2C_Work,SEMA_DELAY))
{
I2C_Open(0);
i2c_err = TRUE;
break;
}
}
I2C_ITConfig(I2Cx,I2C_IT_BUF | I2C_IT_ERR | I2C_IT_EVT,DISABLE);
xSemaphoreGive(xSemaphoreI2C_Mutex);
return !i2c_err;
}
示例3: LM75_ReadReg
/**
* @brief Read the specified register from the LM75.
* @param RegName: specifies the LM75 register to be read.
* This member can be one of the following values:
* - LM75_REG_TEMP: temperature register
* - LM75_REG_TOS: Over-limit temperature register
* - LM75_REG_THYS: Hysteresis temperature register
* @retval LM75 register value.
*/
uint16_t LM75_ReadReg(uint8_t RegName)
{
uint8_t LM75_BufferRX[2] ={0,0};
uint16_t tmp = 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, RegName);
/* 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 */
//.........这里部分代码省略.........
示例4: i2c_ev_handler
void i2c_ev_handler(void)
{
static uint8_t subaddress_sent, final_stop; // flag to indicate if subaddess sent, flag to indicate final bus condition
static int8_t index; // index is signed -1 == send the subaddress
uint8_t SReg_1 = I2Cx->SR1; // read the status register here
if (SReg_1 & 0x0001) { // we just sent a start - EV5 in ref manual
I2Cx->CR1 &= ~0x0800; // reset the POS bit so ACK/NACK applied to the current byte
I2C_AcknowledgeConfig(I2Cx, ENABLE); // make sure ACK is on
index = 0; // reset the index
if (reading && (subaddress_sent || 0xFF == reg)) { // we have sent the subaddr
subaddress_sent = 1; // make sure this is set in case of no subaddress, so following code runs correctly
if (bytes == 2)
I2Cx->CR1 |= 0x0800; // set the POS bit so NACK applied to the final byte in the two byte read
I2C_Send7bitAddress(I2Cx, addr, I2C_Direction_Receiver); // send the address and set hardware mode
} else { // direction is Tx, or we havent sent the sub and rep start
I2C_Send7bitAddress(I2Cx, addr, I2C_Direction_Transmitter); // send the address and set hardware mode
if (reg != 0xFF) // 0xFF as subaddress means it will be ignored, in Tx or Rx mode
index = -1; // send a subaddress
}
} else if (SReg_1 & 0x0002) { // we just sent the address - EV6 in ref manual
// Read SR1,2 to clear ADDR
__DMB(); // memory fence to control hardware
if (bytes == 1 && reading && subaddress_sent) { // we are receiving 1 byte - EV6_3
I2C_AcknowledgeConfig(I2Cx, DISABLE); // turn off ACK
__DMB();
(void)I2Cx->SR2; // clear ADDR after ACK is turned off
I2C_GenerateSTOP(I2Cx, ENABLE); // program the stop
final_stop = 1;
I2C_ITConfig(I2Cx, I2C_IT_BUF, ENABLE); // allow us to have an EV7
} else { // EV6 and EV6_1
(void)I2Cx->SR2; // clear the ADDR here
__DMB();
if (bytes == 2 && reading && subaddress_sent) { // rx 2 bytes - EV6_1
I2C_AcknowledgeConfig(I2Cx, DISABLE); // turn off ACK
I2C_ITConfig(I2Cx, I2C_IT_BUF, DISABLE); // disable TXE to allow the buffer to fill
} else if (bytes == 3 && reading && subaddress_sent) // rx 3 bytes
I2C_ITConfig(I2Cx, I2C_IT_BUF, DISABLE); // make sure RXNE disabled so we get a BTF in two bytes time
else // receiving greater than three bytes, sending subaddress, or transmitting
I2C_ITConfig(I2Cx, I2C_IT_BUF, ENABLE);
}
} else if (SReg_1 & 0x004) { // Byte transfer finished - EV7_2, EV7_3 or EV8_2
final_stop = 1;
if (reading && subaddress_sent) { // EV7_2, EV7_3
if (bytes > 2) { // EV7_2
I2C_AcknowledgeConfig(I2Cx, DISABLE); // turn off ACK
read_p[index++] = (uint8_t)I2Cx->DR; // read data N-2
I2C_GenerateSTOP(I2Cx, ENABLE); // program the Stop
final_stop = 1; // required to fix hardware
read_p[index++] = (uint8_t)I2Cx->DR; // read data N - 1
I2C_ITConfig(I2Cx, I2C_IT_BUF, ENABLE); // enable TXE to allow the final EV7
} else { // EV7_3
if (final_stop)
I2C_GenerateSTOP(I2Cx, ENABLE); // program the Stop
else
I2C_GenerateSTART(I2Cx, ENABLE); // program a rep start
read_p[index++] = (uint8_t)I2Cx->DR; // read data N - 1
read_p[index++] = (uint8_t)I2Cx->DR; // read data N
index++; // to show job completed
}
} else { // EV8_2, which may be due to a subaddress sent or a write completion
if (subaddress_sent || (writing)) {
if (final_stop)
I2C_GenerateSTOP(I2Cx, ENABLE); // program the Stop
else
I2C_GenerateSTART(I2Cx, ENABLE); // program a rep start
index++; // to show that the job is complete
} else { // We need to send a subaddress
I2C_GenerateSTART(I2Cx, ENABLE); // program the repeated Start
subaddress_sent = 1; // this is set back to zero upon completion of the current task
}
}
// we must wait for the start to clear, otherwise we get constant BTF
while (I2Cx->CR1 & 0x0100) {
;
}
} else if (SReg_1 & 0x0040) { // Byte received - EV7
read_p[index++] = (uint8_t)I2Cx->DR;
if (bytes == (index + 3))
I2C_ITConfig(I2Cx, I2C_IT_BUF, DISABLE); // disable TXE to allow the buffer to flush so we can get an EV7_2
if (bytes == index) // We have completed a final EV7
index++; // to show job is complete
} else if (SReg_1 & 0x0080) { // Byte transmitted EV8 / EV8_1
if (index != -1) { // we dont have a subaddress to send
I2Cx->DR = write_p[index++];
if (bytes == index) // we have sent all the data
I2C_ITConfig(I2Cx, I2C_IT_BUF, DISABLE); // disable TXE to allow the buffer to flush
} else {
index++;
I2Cx->DR = reg; // send the subaddress
if (reading || !bytes) // if receiving or sending 0 bytes, flush now
I2C_ITConfig(I2Cx, I2C_IT_BUF, DISABLE); // disable TXE to allow the buffer to flush
}
}
if (index == bytes + 1) { // we have completed the current job
subaddress_sent = 0; // reset this here
if (final_stop) // If there is a final stop and no more jobs, bus is inactive, disable interrupts to prevent BTF
I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_ERR, DISABLE); // Disable EVT and ERR interrupts while bus inactive
busy = 0;
}
//.........这里部分代码省略.........
示例5: I2C_ByteRead
ErrorStatus I2C_ByteRead(uint8_t I2C_Addrs,uint8_t ReadAddr,uint8_t Data_Buffer[],uint8_t Number_Bytes_to_Read)
{
uint8_t i=0;
ErrorStatus status=ERROR;
while(i<=7)
{
switch(i)
{
case 0:
i++; //Pass
I2C_Time_Out_Counter=I2C_Time_Out;
#ifdef I2C_By_Function_Base
while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY)) //Line busy
#else
while(I2C->SR3 & 0x02) //等待總線空閒 檢測i2c-SR3 busy位
#endif
if(!(I2C_Time_Out_Counter--)) //Timeout Detect
{
//UART1_Printf("Byte Read case 0: Line Busy!! \n");
i=7;
break;
}
break;
case 1:
i++; //Pass
I2C_GenerateSTART(ENABLE);
I2C_Time_Out_Counter=I2C_Time_Out;
#ifdef I2C_By_Function_Base
while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT)) //EV5
#else
while(!(I2C->SR1 & 0x01)) //等待START發送完 EV5
#endif
if(!(I2C_Time_Out_Counter--)) //Timeout Detect
{
//UART1_Printf("Byte Read case 1: EV5 Error!! \n");
i=7;
break;
}
break;
case 2:
i++; //Pass
I2C_Send7bitAddress(I2C_Addrs, I2C_DIRECTION_TX);
I2C_Time_Out_Counter=I2C_Time_Out;
#ifdef I2C_By_Function_Base
//while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) //EV6
while(!(I2C->SR1 & 0x02)) //等特7位器件地址?送完并且收到ack,ADDR置1
if(!(I2C_Time_Out_Counter--)) //Timeout Detect
{
//UART1_Printf("Byte Read case 2: EV6 Error!! \n");
i=7;
break;
}
//I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED);
I2C_Clear_ADDRESSSENTMATCHED_Flag();
#else
while(!(I2C->SR1 & 0x02)) //等特7位器件地址?送完并且收到ack,ADDR置1
if(!(I2C_Time_Out_Counter--)) //Timeout Detect
{
//UART1_Printf("Byte Read case 2: EV6 Error!! \n");
i=7;
break;
}
I2C_Clear_ADDRESSSENTMATCHED_Flag();
#endif
break;
case 3:
i++;
I2C_SendData((uint8_t) (ReadAddr)); //Send Read Data Address
I2C_Time_Out_Counter=I2C_Time_Out;
#ifdef I2C_By_Function_Base
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING)) //EV8
#else
//EV8_2 TxE=1 ,BTF=1,產生停止條件時由硬件清除。
while(!(I2C->SR1 & 0x84)) //檢測SR1 TXE1 BTF位置(只有當stm8收到ack,TxE才會置1,其實這句相當于判斷收到ack沒有?)
//在發送地址和清除ADDR 之后,I2C接口進入主設備接收模式。以下見stm8s中文數据手冊P252(圖97主設備接收模式接收序列圖)
#endif
if(!(I2C_Time_Out_Counter--)) //Timeout Detect
{
//UART1_Printf("Byte Read case 3: EV8 Error!! \n");
i=7;
break;
}
break;
case 4:
i++;
I2C_GenerateSTART(ENABLE);
//.........这里部分代码省略.........
示例6: I2C_GenerateSTART
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
{
system_tick_t _millis;
// clamp to buffer length
if(quantity > BUFFER_LENGTH){
quantity = BUFFER_LENGTH;
}
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE);
_millis = millis();
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
{
if(EVENT_TIMEOUT < (millis() - _millis)) return 0;
}
/* Send Slave address for read */
I2C_Send7bitAddress(I2C1, address, I2C_Direction_Receiver);
_millis = millis();
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if(EVENT_TIMEOUT < (millis() - _millis)) return 0;
}
/* perform blocking read into buffer */
uint8_t *pBuffer = rxBuffer;
uint8_t numByteToRead = quantity;
uint8_t bytesRead = 0;
/* While there is data to be read */
while(numByteToRead)
{
if(numByteToRead == 1 && sendStop == true)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C1, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(I2C1, ENABLE);
}
if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the Slave */
*pBuffer = I2C_ReceiveData(I2C1);
bytesRead++;
/* 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(I2C1, ENABLE);
// set rx buffer iterator vars
rxBufferIndex = 0;
rxBufferLength = bytesRead;
return bytesRead;
}
示例7: 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();
}
#ifdef sEE_M24C08
/*!< 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_TRANSMITTED))
{
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_TRANSMITTED))
{
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 Channel */
DMA_Cmd(sEE_I2C_DMA_CHANNEL_TX, ENABLE);
/* If all operations OK, return sEE_OK (0) */
return sEE_OK;
//.........这里部分代码省略.........
示例8: I2C_WriteDeviceRegister
/**
* @brief Writes a value in a register of the device through I2C.
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR
* or IOE_2_ADDR.
* @param RegisterAddr: The target register address
* @param RegisterValue: The target register value to be written
* @retval IOE_OK: if all operations are OK. Other value if error.
*/
uint8_t I2C_WriteDeviceRegister(uint8_t DeviceAddr, uint8_t RegisterAddr, uint8_t RegisterValue)
{
uint8_t read_verif = 0;
uint8_t IOE_BufferTX = 0;
/* Get Value to be written */
IOE_BufferTX = RegisterValue;
/* Configure DMA Peripheral */
IOE_DMA_Config(IOE_DMA_TX, (uint8_t*)(&IOE_BufferTX));
/* 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) == RESET)
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Transmit the slave address and enable writing operation */
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());
}
/* Transmit the first address for r/w operations */
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());
}
/* Enable I2C DMA request */
I2C_DMACmd(IOE_I2C,ENABLE);
/* Enable DMA TX Channel */
DMA_Cmd(IOE_DMA_TX_STREAM, ENABLE);
/* Wait until DMA Transfer Complete */
IOE_TimeOut = TIMEOUT_MAX;
while (!DMA_GetFlagStatus(IOE_DMA_TX_STREAM,IOE_DMA_TX_TCFLAG))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Wait until BTF Flag is set before generating STOP */
IOE_TimeOut = 2 * TIMEOUT_MAX;
while ((!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_BTF)))
{
}
/* Send STOP Condition */
I2C_GenerateSTOP(IOE_I2C, ENABLE);
/* Disable DMA TX Channel */
DMA_Cmd(IOE_DMA_TX_STREAM, DISABLE);
/* Disable I2C DMA request */
I2C_DMACmd(IOE_I2C,DISABLE);
/* Clear DMA TX Transfer Complete Flag */
DMA_ClearFlag(IOE_DMA_TX_STREAM,IOE_DMA_TX_TCFLAG);
#ifdef VERIFY_WRITTENDATA
/* Verify (if needed) that the loaded data is correct */
/* Read the just written register*/
read_verif = I2C_ReadDeviceRegister(DeviceAddr, RegisterAddr);
/* Load the register and verify its value */
if (read_verif != RegisterValue)
{
/* Control data wrongly transfered */
read_verif = IOE_FAILURE;
}
else
{
/* Control data correctly transfered */
read_verif = 0;
}
#endif
/* Return the verifying value: 0 (Passed) or 1 (Failed) */
return (read_verif);
//.........这里部分代码省略.........
示例9: interface
/**
* @brief Writes a Byte to a given register into the audio codec through the
control interface (I2C)
* @param RegisterAddr: The address (location) of the register to be written.
* @param RegisterValue: the Byte value to be written into destination register.
* @retval o if correct communication, else wrong communication
*/
static uint32_t Codec_WriteRegister(uint32_t RegisterAddr, uint32_t RegisterValue)
{
uint32_t result = 0;
/*!< While the bus is busy */
CODECTimeout = CODEC_LONG_TIMEOUT;
while(I2C_GetFlagStatus(CODEC_I2C, I2C_FLAG_BUSY))
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/* Start the config sequence */
I2C_GenerateSTART(CODEC_I2C, ENABLE);
/* Test on EV5 and clear it */
CODECTimeout = CODEC_FLAG_TIMEOUT;
while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_MODE_SELECT))
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/* Transmit the slave address and enable writing operation */
I2C_Send7bitAddress(CODEC_I2C, CODEC_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
CODECTimeout = CODEC_FLAG_TIMEOUT;
while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/* Transmit the first address for write operation */
I2C_SendData(CODEC_I2C, RegisterAddr);
/* Test on EV8 and clear it */
CODECTimeout = CODEC_FLAG_TIMEOUT;
while (!I2C_CheckEvent(CODEC_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
{
if((CODECTimeout--) == 0) return Codec_TIMEOUT_UserCallback();
}
/* Disable the interrupts mechanism to prevent the I2C communication from corruption */
__disable_irq();
/* Prepare the register value to be sent */
I2C_SendData(CODEC_I2C, RegisterValue);
/*!< Wait till all data have been physically transferred on the bus */
CODECTimeout = CODEC_LONG_TIMEOUT;
while(!I2C_GetFlagStatus(CODEC_I2C, I2C_FLAG_BTF))
{
if((CODECTimeout--) == 0) Codec_TIMEOUT_UserCallback();
}
/* End the configuration sequence */
I2C_GenerateSTOP(CODEC_I2C, ENABLE);
#ifdef VERIFY_WRITTENDATA
/* Verify that the data has been correctly written */
result = (Codec_ReadRegister(RegisterAddr) == RegisterValue)? 0:1;
#endif /* VERIFY_WRITTENDATA */
/* Re-enable the interrupt mechanism */
__enable_irq();
/* Return the verifying value: 0 (Passed) or 1 (Failed) */
return result;
}
示例10: I2Cx_EV_IRQHandler
//.........这里部分代码省略.........
{
/* Disable buffer Interrupts */
I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
}
else
{
/* Enable buffer Interrupts */
I2C_ITConfig(I2Cx, I2C_IT_BUF , ENABLE);
}
}
#endif /* I2C_10BITS_ADDRESS */
else if(I2C_GetITStatus(I2Cx, I2C_IT_ADDR)== SET)
{
if (NumberOfByteToReceive == 1)
{
I2C_AcknowledgeConfig(I2Cx, DISABLE);
}
/* Clear ADDR Register */
(void)(I2Cx->SR1);
(void)(I2Cx->SR2);
if (GenerateStartStatus == 0x00)
{
if (NumberOfByteToReceive == 1)
{
I2C_GenerateSTOP(I2Cx, ENABLE);
}
if (NumberOfByteToReceive == 2)
{
I2C_AcknowledgeConfig(I2Cx, DISABLE);
I2C_NACKPositionConfig(I2Cx, I2C_NACKPosition_Next);
/* Disable buffer Interrupts */
I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
}
}
#ifdef I2C_10BITS_ADDRESS
if (GenerateStartStatus == 0x01)
{
/* Repeated Start */
I2C_GenerateSTART(I2Cx, ENABLE);
GenerateStartStatus = 0x00;
}
#endif /* I2C_10BITS_ADDRESS */
}
else if((I2C_GetITStatus(I2Cx, I2C_IT_RXNE)== SET)&&(I2C_GetITStatus(I2Cx, I2C_IT_BTF)== RESET))
{
/* Store I2C received data */
RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
NumberOfByteToReceive--;
if (NumberOfByteToReceive == 0x03)
{
/* Disable buffer Interrupts */
I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
}
if (NumberOfByteToReceive == 0x00)
{
/* Disable Error and Buffer Interrupts */
I2C_ITConfig(I2Cx, (I2C_IT_EVT | I2C_IT_BUF), DISABLE);
}
}
/* BUSY, MSL and RXNE flags */
else if(I2C_GetITStatus(I2Cx, I2C_IT_BTF)== SET)
{
/* if Three bytes remaining for reception */
if (NumberOfByteToReceive == 3)
{
I2C_AcknowledgeConfig(I2Cx, DISABLE);
/* Store I2C received data */
RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
NumberOfByteToReceive--;
}
else if (NumberOfByteToReceive == 2)
{
I2C_GenerateSTOP(I2Cx, ENABLE);
/* Store I2C received data */
RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
NumberOfByteToReceive--;
/* Store I2C received data */
RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
NumberOfByteToReceive--;
/* Disable Error and Buffer Interrupts */
I2C_ITConfig(I2Cx, (I2C_IT_EVT | I2C_IT_BUF), DISABLE);
}
else
{
/* Store I2C received data */
RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
NumberOfByteToReceive--;
}
}
#endif /* I2C_MASTER*/
}
示例11: I2C_ReadDataBuffer
/**
* @brief Reads a buffer of 2 bytes from the device registers.
* @param DeviceAddr: The address of the device, could be : IOE_1_ADDR
* or IOE_2_ADDR.
* @param RegisterAddr: The target register address (between 00x and 0x24)
* @retval A pointer to the buffer containing the two returned bytes (in halfword).
*/
uint16_t I2C_ReadDataBuffer(uint8_t DeviceAddr, uint8_t RegisterAddr)
{
uint8_t tmp= 0;
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_STREAM, ENABLE);
/* Wait until DMA Transfer Complete */
IOE_TimeOut = 2 * TIMEOUT_MAX;
while (!DMA_GetFlagStatus(IOE_DMA_RX_STREAM, 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_STREAM, DISABLE);
/* Disable I2C DMA request */
I2C_DMACmd(IOE_I2C,DISABLE);
/* Clear DMA RX Transfer Complete Flag */
DMA_ClearFlag(IOE_DMA_RX_STREAM,IOE_DMA_RX_TCFLAG);
/* Reorganize received data */
tmp = IOE_BufferRX[0];
IOE_BufferRX[0] = IOE_BufferRX[1];
IOE_BufferRX[1] = tmp;
/* return a pointer to the IOE_Buffer */
return (*(__IO uint16_t *) IOE_BufferRX);
//.........这里部分代码省略.........
示例12: SENSOR_I2C_BUS_EV_ISR
/********************
- 传感器I2C总线的事件中断服务程序
*********************/
void SENSOR_I2C_BUS_EV_ISR(void)
{
//-SB
if(I2C_GetITStatus(SENSOR_I2C_BUS, I2C_IT_SB))
{
I2C_AcknowledgeConfig(SENSOR_I2C_BUS, DISABLE);
if((sensor_state.data_direction==I2C_Direction_Receiver) && sensor_state.SUB_Transmitted)
{
sensor_state.SUB_Transmitted = SET;
I2C_Send7bitAddress(SENSOR_I2C_BUS, sensor_state.sensor_address, I2C_Direction_Receiver);
}
else
{
I2C_Send7bitAddress(SENSOR_I2C_BUS, sensor_state.sensor_address, I2C_Direction_Transmitter);
}
}
//-ADDR
else if(I2C_GetITStatus(SENSOR_I2C_BUS, I2C_IT_ADDR))
{
volatile uint8_t a=SENSOR_I2C_BUS->SR1;//Read SR1,2 to clear ADDR
a=SENSOR_I2C_BUS->SR2;
if(sensor_state.data_direction==I2C_Direction_Receiver && sensor_state.SUB_Transmitted) //已发送SUB,开始接收1字节[ref manual P735]
{
I2C_AcknowledgeConfig(SENSOR_I2C_BUS, DISABLE);//turn off ACK
I2C_GenerateSTOP(SENSOR_I2C_BUS,ENABLE);//program the stop
I2C_ITConfig(SENSOR_I2C_BUS, I2C_IT_BUF, ENABLE);
}
else //接收多于3字节;发送SUB或正在发送
{
I2C_ITConfig(SENSOR_I2C_BUS, I2C_IT_BUF, ENABLE);
}
}/*
//-BTF
else if(I2C_GetITStatus(SENSOR_I2C_BUS, I2C_IT_BTF))
{
volatile uint8_t b=SENSOR_I2C_BUS->SR1;
if(sensor_state.data_direction==I2C_Direction_Receiver && sensor_state.SUB_Transmitted)
{}
else
{
I2C_GenerateSTART(SENSOR_I2C_BUS, ENABLE);
}
}*/
//-RXNE
else if(I2C_GetITStatus(SENSOR_I2C_BUS, I2C_IT_RXNE))
{
sensor_state.receive_data=I2C_ReceiveData(SENSOR_I2C_BUS);
I2C_ITConfig(SENSOR_I2C_BUS, I2C_IT_BUF, DISABLE);
sensor_state.data_receive_finish=SET;
}
//-TXE
else if(I2C_GetITStatus(SENSOR_I2C_BUS, I2C_IT_TXE))
{
I2C_SendData(SENSOR_I2C_BUS, sensor_state.SUB);
sensor_state.SUB_Transmitted=SET;
if(sensor_state.data_direction==I2C_Direction_Receiver)
I2C_ITConfig(SENSOR_I2C_BUS, I2C_IT_BUF, DISABLE);
I2C_GenerateSTART(SENSOR_I2C_BUS, ENABLE);
}
/**/
if(sensor_state.data_receive_finish)
{
uint8_t temp=(sensor_state.I2C_run_flag&0x07);//读出偏移量(低3位)
sensor_state.data_receive_finish=RESET;
sensor_state.SUB_Transmitted=RESET;
switch(sensor_state.I2C_run_flag&0xE0)
{
case ACCER_READING:
{
(*(ACCER_DATA_BASE+temp))=sensor_state.receive_data;
}
break;
case GYRO_READING:
{
(*(GYRO_DATA_BASE+temp))=sensor_state.receive_data;
}
break;
case MEGN_READING://reserve!
{
}
break;
default:
break;
}
if(temp<5)//未读够6个字节数据(0:5)
{
sensor_state.I2C_run_flag++;//接收完一个字节,标志加1
sensor_state.SUB++;
sensor_state.SUB_Transmitted=RESET;
I2C_GenerateSTART(SENSOR_I2C_BUS, ENABLE);
}
//.........这里部分代码省略.........
示例13: millis
I2C_RESULT I2CDev::readBytes(uint8_t devAddr, uint8_t regAddr, uint16_t count, uint8_t * buf)
{
uint32_t tmpTime = millis();
// busy check
while(IsBusy);
IsBusy = 1;
// ENTR_CRT_SECTION();
/* While the bus is busy */
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));
/* Send START condition */
I2C_GenerateSTART(I2C2, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
/* Send MPU6050 address (0xD0) for write */
I2C_Send7bitAddress(I2C2, devAddr, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2C2, ENABLE);
/* Send the MPU6050_Magn's internal address to write to */
I2C_SendData(I2C2, regAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C2, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
/* Send MPU6050 address for read */
I2C_Send7bitAddress(I2C2, devAddr, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* While there is data to be read */
while(count)
{
if(count == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C2, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(I2C2, ENABLE);
}
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the MPU6050 */
*buf = I2C_ReceiveData(I2C2);
/* Point to the next location where the byte read will be saved */
buf++;
/* Decrement the read bytes counter */
count--;
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(I2C2, ENABLE);
// EXT_CRT_SECTION();
//
IsBusy = 0;
return I2C_OK;
}
示例14: while
I2C_RESULT I2CDev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint16_t count, uint8_t * buf)
{
uint32_t tmpTime = 0;
// busy check
while(IsBusy);
IsBusy = 1;
if (count == 1) // multi-bytes haven't been implemented yet
{
/* Test on BUSY Flag */
tmpTime = millis();
while (I2C_GetFlagStatus(I2C2,I2C_FLAG_BUSY))
{
if(millis() - tmpTime > TimeOut)
{
IsBusy = 0;
return I2C_TIMEOUT;
}
}
/* Enable the I2C peripheral */
I2C_GenerateSTART(I2C2, ENABLE);
/* Test on SB Flag */
tmpTime = millis();
while (I2C_GetFlagStatus(I2C2,I2C_FLAG_SB) == RESET)
{
if(millis() - tmpTime > TimeOut)
{
IsBusy = 0;
return I2C_TIMEOUT;
}
}
/* Transmit the slave address and enable writing operation */
I2C_Send7bitAddress(I2C2, devAddr, I2C_Direction_Transmitter);
/* Test on ADDR Flag */
tmpTime = millis();
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if(millis() - tmpTime > TimeOut)
{
IsBusy = 0;
return I2C_TIMEOUT;
}
}
/* Transmit the first address for r/w operations */
I2C_SendData(I2C2, regAddr);
/* Test on TXE FLag (data sent) */
tmpTime = millis();
while ((!I2C_GetFlagStatus(I2C2,I2C_FLAG_TXE)) || (!I2C_GetFlagStatus(I2C2,I2C_FLAG_BTF)))
{
if(millis() - tmpTime > TimeOut)
{
IsBusy = 0;
return I2C_TIMEOUT;
}
}
/* Transmit the first address for r/w operations */
I2C_SendData(I2C2, *buf);
/* Test on TXE FLag (data sent) */
tmpTime = millis();
while ((!I2C_GetFlagStatus(I2C2,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(I2C2,I2C_FLAG_BTF)))
{
if(millis() - tmpTime > TimeOut)
{
IsBusy = 0;
return I2C_TIMEOUT;
}
}
/*!< Send STOP Condition */
I2C_GenerateSTOP(I2C2, ENABLE);
/* Wait to make sure that STOP control bit has been cleared */
tmpTime = millis();
while(I2C2->CR1 & I2C_CR1_STOP)
{
if(millis() - tmpTime > TimeOut)
{
IsBusy = 0;
return I2C_TIMEOUT;
}
}
//
IsBusy = 0;
return I2C_OK;
}
//
IsBusy = 0;
return I2C_FAIL;
//.........这里部分代码省略.........
示例15: MPU6050_I2C_BufferRead
/**
* @brief Reads a block of data from the MPU6050.
* @param slaveAddr : slave address MPU6050_DEFAULT_ADDRESS
* @param pBuffer : pointer to the buffer that receives the data read from the MPU6050.
* @param readAddr : MPU6050's internal address to read from.
* @param NumByteToRead : number of bytes to read from the MPU6050 ( NumByteToRead >1 only for the Mgnetometer readinf).
* @return None
*/
void MPU6050_I2C_BufferRead(u8 slaveAddr, u8* pBuffer, u8 readAddr, u16 NumByteToRead)
{
// ENTR_CRT_SECTION();
/* While the bus is busy */
while (I2C_GetFlagStatus(MPU6050_I2C, I2C_FLAG_BUSY));
/* Send START condition */
I2C_GenerateSTART(MPU6050_I2C, ENABLE);
/* Test on EV5 and clear it */
while (!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send MPU6050 address for write */
I2C_Send7bitAddress(MPU6050_I2C, slaveAddr, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while (!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(MPU6050_I2C, ENABLE);
/* Send the MPU6050's internal address to write to */
I2C_SendData(MPU6050_I2C, readAddr);
/* Test on EV8 and clear it */
while (!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STRAT condition a second time */
I2C_GenerateSTART(MPU6050_I2C, ENABLE);
/* Test on EV5 and clear it */
while (!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send MPU6050 address for read */
I2C_Send7bitAddress(MPU6050_I2C, slaveAddr, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while (!I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* While there is data to be read */
while (NumByteToRead)
{
if (NumByteToRead == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(MPU6050_I2C, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(MPU6050_I2C, ENABLE);
}
/* Test on EV7 and clear it */
if (I2C_CheckEvent(MPU6050_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the MPU6050 */
*pBuffer = I2C_ReceiveData(MPU6050_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(MPU6050_I2C, ENABLE);
// EXT_CRT_SECTION();
}