• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ I2C_Send7bitAddress函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中I2C_Send7bitAddress函数的典型用法代码示例。如果您正苦于以下问题:C++ I2C_Send7bitAddress函数的具体用法?C++ I2C_Send7bitAddress怎么用?C++ I2C_Send7bitAddress使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了I2C_Send7bitAddress函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: I2C_Send7bitAddress

void i2cMgr_t::SendAddrRX() {
    I2C_Send7bitAddress(I2C1, ((CmdToRead->Address) << 1), I2C_Direction_Receiver);
    Delay.Reset(&Timer);
}
开发者ID:Kreyl,项目名称:nute,代码行数:4,代码来源:i2c_mgr.cpp


示例2: 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 read from.
  * @param  NumByteToRead : pointer to the variable holding number of bytes to 
  *         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 
  *       configure 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.
  * 
  * @retval None
  */
void sEE_ReadBuffer(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. */
  sEEDataReadPointer = NumByteToRead;
  
  /*!< While the bus is busy */
  while(I2C_GetFlagStatus(sEE_I2C, I2C_FLAG_BUSY))
  {
  }
  
  /*!< Send START condition */
  I2C_GenerateSTART(sEE_I2C, ENABLE);
  
  /*!< Test on EV5 and clear it */
  while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
  {
  }
   
  /*!< Send EEPROM address for write */
  I2C_Send7bitAddress(sEE_I2C, sEEAddress, I2C_Direction_Transmitter);

  /*!< Test on EV6 and clear it */
  while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  {
  }

#ifdef sEE_M24C08  
  
  /*!< Send the EEPROM's internal address to read from: Only one byte address */
  I2C_SendData(sEE_I2C, ReadAddr);  

#elif defined (sEE_M24C64_32)

  /*!< Send the EEPROM's internal address to read from: MSB of the address first */
  I2C_SendData(sEE_I2C, (uint8_t)((ReadAddr & 0xFF00) >> 8));    
  
  /*!< Test on EV8 and clear it */
  while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  {
  }

  /*!< Send the EEPROM's internal address to read from: LSB of the address */
  I2C_SendData(sEE_I2C, (uint8_t)(ReadAddr & 0x00FF));    
  
#endif /*!< sEE_M24C08 */
  
  /*!< Test on EV8 and clear it */
  while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  {
  }
  
  /*!< Send STRAT condition a second time */  
  I2C_GenerateSTART(sEE_I2C, ENABLE);
  
  /*!< Test on EV5 and clear it */
  while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
  {
  }
  
  /*!< Send EEPROM address for read */
  I2C_Send7bitAddress(sEE_I2C, sEEAddress, I2C_Direction_Receiver);
  
  /*!< Test on EV6 and clear it */
  while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
  {
  }
  
  /* If number of data to be read is 1, then DMA couldn't be used */
  if ((uint16_t)(*NumByteToRead) < 2)
  {
    /*!< Disable Acknowledgement */
    I2C_AcknowledgeConfig(sEE_I2C, DISABLE);
      
    /*!< Send STOP Condition */
    I2C_GenerateSTOP(sEE_I2C, ENABLE);

    /*!< Test on EV7 and clear it */
    while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
//.........这里部分代码省略.........
开发者ID:LarryPham,项目名称:testing_on_board,代码行数:101,代码来源:stm32_eval_i2c_ee.c


示例3: while

bool clMPU6050::I2C_BufferRead(u8 slaveAddr, u8* pBuffer, u8 readAddr, u16 NumByteToRead)
{
	// Wait while the bus is busy
	this->restartTimeoutTimer();
	while(I2C_GetFlagStatus(this->i2c, I2C_FLAG_BUSY)){
		if(this->getTimeoutTimerTime() > 20*this->timeForOneByte){
			return false;
		}
	}

	// Send START condition
	I2C_GenerateSTART(this->i2c, ENABLE);

	// Check if START condition has been sent by checking the SB bit in SR 1,
	// this flag is cleared by reading SR1 and writing to DR, this will be done when sending the slave address.
	bool startConditionSent = false;
	this->restartTimeoutTimer();
	while(!startConditionSent){
		startConditionSent = this->i2c->SR1 && BIT0;
		if(this->getTimeoutTimerTime() > 20*this->timeForOneByte){
			return false;
		}
	}

	// Send MPU6050 address for write
	I2C_Send7bitAddress(this->i2c, slaveAddr, I2C_Direction_Transmitter);

	// Wait until the slave has acknowledged the address. To do this, we check the ADDR bit
	// in SR1. This bit is cleared by subsequently reading SR1 and SR2.
	this->restartTimeoutTimer();
	bool addressHasBeenReceived = false;
	while(!addressHasBeenReceived){
		addressHasBeenReceived = this->i2c->SR1 && BIT1;
		if(this->getTimeoutTimerTime() > 20*this->timeForOneByte){
			return false;
		}
	}

	// Clear ADDR flag by reading SR2; variable is declared as volatile to keep the compiler from optimizing it away.
	volatile uint16_t tmp = this->i2c->SR2;

	// Send the MPU6050's internal address to read from
	I2C_SendData(this->i2c, readAddr);

	// Wait until the byte transfer has been finished by checking on the BTF bit in SR1. It will
	// be cleared by the next read operation of the DR register.
	this->restartTimeoutTimer();
	bool byteHasBeenTransmitted = false;
	while(!byteHasBeenTransmitted){
		byteHasBeenTransmitted = this->i2c->SR1 && BIT2;
		if(this->getTimeoutTimerTime() > 20*this->timeForOneByte){
			return false;
		}
	}

	// Send START condition a second time
	I2C_GenerateSTART(this->i2c, ENABLE);

	// Check if START condition has been sent by checking the SB bit in SR 1,
	// this flag is cleared by reading SR1 and writing to DR, this will be done when sending the slave address.
	startConditionSent = false;
	this->restartTimeoutTimer();
	while(!startConditionSent){
		startConditionSent = this->i2c->SR1 && BIT0;
		if(this->getTimeoutTimerTime() > 20*this->timeForOneByte){
			return false;
		}
	}

	// Send MPU6050 address for read
	I2C_Send7bitAddress(this->i2c, slaveAddr, I2C_Direction_Receiver);

	// Wait until the slave has acknowledged the address. To do this, we check the ADDR bit
	// in SR1. This bit is cleared by subsequently reading SR1 and SR2.
	this->restartTimeoutTimer();
	addressHasBeenReceived = false;
	while(!addressHasBeenReceived){
		addressHasBeenReceived = this->i2c->SR1 && BIT1;
		if(this->getTimeoutTimerTime() > 20*this->timeForOneByte){
			return false;
		}
	}
	// Clear ADDR flag by reading SR2:
	tmp = this->i2c->SR2;
	I2C_GenerateSTOP(this->i2c, ENABLE);
/*

	// Read in bytes sent by the slave:
	// If we are about to receive 1 single byte, there is no need to acknowledge it, since the last byte
	// is not acknowledged. If we will receive more than one byte, every byte except the last one has to be acknowledged
	// by the master.
	I2C_AcknowledgeConfig(this->i2c, ENABLE);
	if(NumByteToRead == 1){
		I2C_AcknowledgeConfig(this->i2c, DISABLE);
	}
	// While there is data to be read:
	uint32_t timeoutTime = 20* NumByteToRead * this->timeForOneByte;
	this->restartTimeoutTimer();
	while(NumByteToRead > 0)
	{
//.........这里部分代码省略.........
开发者ID:jtec,项目名称:flightcode,代码行数:101,代码来源:defgrewf.hpp


示例4: 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_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;
//.........这里部分代码省略.........
开发者ID:Amalinda,项目名称:ruuvitracker_fw,代码行数:101,代码来源:stm322xg_eval_i2c_ee.c


示例5: I2C_GetLastEvent

void I2C::EventIRQ()
{
	static uint32_t I2C_Status=0;
	static u8 timeOut=0;
	I2C_Status = I2C_GetLastEvent(mI2C);
	switch(I2C_Status)//查询中断事件类型
	{
		case I2C_EVENT_MASTER_MODE_SELECT: //EV5   //SB、BUSY、MSL位置位	
			if(mState==STATE_SEND_ADW)//发送从机地址+写信号
			{	
			#ifdef I2C_USE_DMA
				if(mUseDma)
				{
					mI2C->CR2 &= ~I2C_IT_BUF; //使用DMA,关闭BUF中断
					mI2C->CR2 |= I2C_CR2_DMAEN; //使能IIC DMA传输
				}
			#endif
				I2C_Send7bitAddress(mI2C,mCurrentCmd.slaveAddr,I2C_Direction_Transmitter);
				mState = STATE_SEND_DATA;//状态设置为需要发送数据(发送寄存器地址)
			#ifdef I2C_USE_DMA
				if(mUseDma)
				{
						/* I2Cx Common Channel Configuration */
					if(mCurrentCmd.cmdType>=I2C_WRITE_BYTE)//写模式
					{
						mDMA_InitStructure.DMA_BufferSize = mCurrentCmd.outDataLen;
					}
					else                                        //读模式 设置长度为1 因为发送一个字节后需要再次发送起始信号
					{
						mDMA_InitStructure.DMA_BufferSize = 1;
					}
					mDMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&mCurrentCmd.DataOut[0];
					mDMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;
					
					DMA_Init(mDmaTxChannel,&mDMA_InitStructure);
					
					DMA_Cmd(mDmaTxChannel, ENABLE);
				}
			#endif
				
			}
			else if(mState==STATE_SEND_ADR)//发送从机地址+读信号
			{
				I2C_Send7bitAddress(mI2C,mCurrentCmd.slaveAddr,I2C_Direction_Receiver);
				mState = STATE_REVEIVE_DATA;//状态设置为需要发送数据(发送寄存器地址)
			#ifdef I2C_USE_DMA
				if(mUseDma)
				{
					if( mCurrentCmd.cmdType==I2C_READ_BYTE)//接收一个字节,不能使用DMA,使用中断的方式
					{
						mI2C->CR2 |= I2C_IT_BUF; //接收一个字节,打开 BUF中断,不使用DMA
						mI2C->CR2 &= ~I2C_CR2_DMAEN; //失能IIC DMA传输
					}
					else //接收的字节数大于1,使用DMA
					{
						mI2C->CR2|=I2C_CR2_LAST;//使能接收最后一位时发送NACK
						mI2C->CR2 &= ~I2C_IT_BUF; //使用DMA,关闭BUF中断
						mI2C->CR2 |= I2C_CR2_DMAEN; //使能IIC DMA传输
						
						mDMA_InitStructure.DMA_BufferSize = mCurrentCmd.inDataLen;
						mDMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)mCurrentCmd.pDataIn;
						mDMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
						
						DMA_Init(mDmaRxChannel,&mDMA_InitStructure);
						
						DMA_Cmd(mDmaRxChannel, ENABLE);
						
					}
				}
			#endif
			}
			break;
		
		
		case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED://EV6	//已: I2C_Send7bitAddress(W)   应: I2C_SendData()
			#ifndef I2C_USE_DMA
			//			I2C->CR1 |= I2C_CR1_PE;
			I2C_SendData(mI2C,mCurrentCmd.DataOut[0]);//发送寄存器地址或者命令
			++mIndex_Send;//发送计数下标后移
			--mCurrentCmd.outDataLen;//需要发送的数据长度减1
			#else
			if(mUseDma==false)
			{
				//			I2C->CR1 |= I2C_CR1_PE;
				I2C_SendData(mI2C,mCurrentCmd.DataOut[0]);//发送寄存器地址或者命令
				++mIndex_Send;//发送计数下标后移
				--mCurrentCmd.outDataLen;//需要发送的数据长度减1
			}
			#endif
			break;
		
		
		case I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED://EV6			//已: I2C_Send7bitAddress(R)		
			if(mCurrentCmd.cmdType==I2C_READ_BYTE)//只接收一个字节
			{
				I2C_AcknowledgeConfig(mI2C,DISABLE);//关应答
				I2C_GenerateSTOP(mI2C,ENABLE);//产生停止信号
			}
			break;
		
//.........这里部分代码省略.........
开发者ID:lissettecarlr,项目名称:Quadcopter,代码行数:101,代码来源:I2C.cpp


示例6: hal_i2c4hal_hid_standby

extern hal_result_t hal_i2c4hal_hid_standby(hal_i2c4hal_port_t port, uint8_t devaddr)      
{
    volatile uint16_t tmpSR1 = 0;
    volatile uint32_t trials = 0;
    volatile uint32_t timeout = 0;
    
    I2C_TypeDef* I2Cx = (hal_i2c4hal_port1 == port) ? (I2C1) : (I2C2);
    #warning --> ci sono tre i2c errore ...........
 
    // While the bus is busy 
    timeout = s_hal_i2c4hal_timeout_long;
    while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
    {
        if(0 == (timeout--)) return s_hal_i2c4hal_timeoutexpired();
    }

    //  keep looping till the slave acknowledge his address or maximum number of trials is reached
    while(1)
    {
        /*!< Send START condition */
        I2C_GenerateSTART(I2Cx, ENABLE);

        /*!< Test on EV5 and clear it */
        timeout = s_hal_i2c4hal_timeout_flag;
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
        {
            if(0 == (timeout--)) return s_hal_i2c4hal_timeoutexpired();
        }    

        /*!< Send EEPROM address for write */
        I2C_Send7bitAddress(I2Cx, devaddr, I2C_Direction_Transmitter);

        /* Wait for ADDR flag to be set (Slave acknowledged his address) */
        timeout = s_hal_i2c4hal_timeout_long;
        do
        {     
            /* Get the current value of the SR1 register */
            tmpSR1 = (I2Cx)->SR1;

            /* Update the timeout value and exit if it reach 0 */
            if(0 == (timeout--)) return s_hal_i2c4hal_timeoutexpired();
        }
        /* Keep looping till the Address is acknowledged or the AF (ack failure) 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)(I2Cx)->SR2;

            /*!< STOP condition */    
            I2C_GenerateSTOP(I2Cx, ENABLE);

            /* Exit the function */
            return hal_res_OK;
        }
        else
        {
            /*!< Clear AF flag */
            I2C_ClearFlag(I2Cx, I2C_FLAG_AF);                  
        }

        /* Check if the maximum allowed number of trials has been reached */
        if (s_hal_i2c4hal_ackaddress_maxtrials == trials++)
        {
            /* If the maximum number of trials has been reached, exit the function */
            return s_hal_i2c4hal_timeoutexpired();
        }
    }
}
开发者ID:Tarintote,项目名称:icub-firmware,代码行数:73,代码来源:hal_periph_i2c4hal.was.c


示例7: s_hal_i2c4hal_write

static hal_result_t s_hal_i2c4hal_write(hal_i2c4hal_port_t port, uint8_t devaddr, hal_i2c4hal_regaddr_t regaddr, uint8_t* data, uint16_t size)
{
    hal_result_t res = hal_res_NOK_generic;
    uint8_t reg1byteadr = 0;
    volatile uint32_t timeout = 0;
    
    I2C_TypeDef* I2Cx = (hal_i2c4hal_port1 == port) ? (I2C1) : (I2C2);
//    uint8_t usedma = s_stm32i2c4hal_generics.use_dma[port-1];
    
       
 ///////////////////////////////////////   
    
    
    
    // wait until the bus is not busy anymore 
    timeout = s_hal_i2c4hal_timeout_long;
    while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
    {
        if(0 == (timeout--)) return s_hal_i2c4hal_timeoutexpired();
    }

    // send START condition
    I2C_GenerateSTART(I2Cx, ENABLE);
    
    // test on ev5
    timeout = s_hal_i2c4hal_timeout_flag;
    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
    {
        if(0 == (timeout--)) return s_hal_i2c4hal_timeoutexpired();
    } 

    // send address of device
    I2C_Send7bitAddress(I2Cx, devaddr, I2C_Direction_Transmitter);
    
    // test on ev6
    timeout = s_hal_i2c4hal_timeout_flag;
    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
    {
        if(0 == (timeout--)) return s_hal_i2c4hal_timeoutexpired();
    }    

    // send address of register inside the device
    if(1 == regaddr.numofbytes)
    {
        reg1byteadr = regaddr.bytes.one;
        I2C_SendData(I2Cx, reg1byteadr);    
     
        // test on ev8
        timeout = s_hal_i2c4hal_timeout_flag;
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
        {
            if(0 == (timeout--)) return s_hal_i2c4hal_timeoutexpired();
        } 
    }
    else if(2 == regaddr.numofbytes)
    {   
        reg1byteadr = regaddr.bytes.two & 0xFF00;           // msb first
        I2C_SendData(I2Cx, reg1byteadr);    
     
        // test on ev8
        timeout = s_hal_i2c4hal_timeout_flag;
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
        {
            if(0 == (timeout--)) return s_hal_i2c4hal_timeoutexpired();
        }  

        reg1byteadr = regaddr.bytes.two & 0x00FF;           // then lsb
        I2C_SendData(I2Cx, reg1byteadr);    
     
        // test on ev8
        timeout = s_hal_i2c4hal_timeout_flag;
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
        {
            if(0 == (timeout--)) return s_hal_i2c4hal_timeoutexpired();
        }           
    }
    
    // now we decide if it is dma or direct send
    
    
//    if(1 == size)
//    {
//        usedma = 0;
//    }
//    
//    if(0 == usedma)
//    {
        s_hal_i2c4hal_write_bytes(I2Cx, data, size);
//    }
//    else
//    {
//        s_stm32i2c4hal_write_withdma(I2Cx, data, size);
//    }    
    
    
    
/////////////////////////////////////////////////////////////

    //s_stm32i2c4hal_i2c4hal_waitdevicestandbystate(port, devaddr);
    
//.........这里部分代码省略.........
开发者ID:Tarintote,项目名称:icub-firmware,代码行数:101,代码来源:hal_periph_i2c4hal.was.c


示例8: I2C_WriteDeviceRegister

/**
  * @brief  Writes a value in a register of the device through I2C.
  * @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 RegisterAddr, uint8_t RegisterValue)
{
  uint32_t read_verif = 0;

  /* Begin the configuration sequence */
  I2C_GenerateSTART(IOE_I2C, ENABLE);

  /* Test on EV5 and clear it */
  IOE_TimeOut = TIMEOUT_MAX;
  while (!I2C_GetFlagStatus(IOE_I2C, I2C_FLAG_SB))
  {
    if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
  }

  /* Transmit the slave address and enable writing operation */
  I2C_Send7bitAddress(IOE_I2C, IOE_ADDR, I2C_Direction_Transmitter);
  
  /* Test on EV6 and clear it */
  IOE_TimeOut = TIMEOUT_MAX;  
  while (!I2C_GetFlagStatus(IOE_I2C, I2C_FLAG_ADDR))
  {
    if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
  }
  
  /* Read status register 2 to clear ADDR flag */
  IOE_I2C->SR2;
  
  /* Test on EV8_1 and clear it */
  IOE_TimeOut = TIMEOUT_MAX;
  while (!I2C_GetFlagStatus(IOE_I2C, I2C_FLAG_TXE))
  {
    if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
  }
  
  /* Transmit the first address for r/w operations */
  I2C_SendData(IOE_I2C, RegisterAddr);
  
  /* Test on EV8 and clear it */
  IOE_TimeOut = TIMEOUT_MAX;
  while (!I2C_GetFlagStatus(IOE_I2C, I2C_FLAG_TXE))
  {
    if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
  }
  
  /* Prepare the register value to be sent */
  I2C_SendData(IOE_I2C, RegisterValue);
  
  /* Test on EV8_2 and clear it */
  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());
  }
  
  /* End the configuration sequence */
  I2C_GenerateSTOP(IOE_I2C, ENABLE);
  
#ifdef VERIFY_WRITTENDATA
  /* Verify (if needed) that the loaded data is correct  */
  
  /* Read the just written register*/
  read_verif = IOE_I2C_ReadDeviceRegister(RegisterAddr);

  /* Load the register and verify its value  */
  if (read_verif != RegisterValue)
  {
    /* Control data wrongly transferred */
    read_verif = IOE_FAILURE;
  }
  else
  {
    /* Control data correctly transferred */
    read_verif = 0;
  }
#endif
  
  /* Return the verifying value: 0 (Passed) or 1 (Failed) */
  return read_verif;
  
}
开发者ID:CHPeter,项目名称:stm32f429-gps,代码行数:86,代码来源:stm32f429i_discovery_ioe.c


示例9: I2C_ReadDeviceRegister

/**
  * @brief  Reads a register of the device through I2C without DMA.
  * @param  RegisterAddr: The target register address (between 00x and 0x24)
  * @retval The value of the read register (0xAA if Timeout occurred)   
  */ 
uint8_t I2C_ReadDeviceRegister(uint8_t RegisterAddr)
{
  uint8_t tmp = 0;
  
  /* Enable the I2C peripheral */
  I2C_GenerateSTART(IOE_I2C, ENABLE);
  
    /* Test on EV5 and clear it */
  IOE_TimeOut = TIMEOUT_MAX;
  while (!I2C_GetFlagStatus(IOE_I2C, I2C_FLAG_SB))
  {
    if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
  }
  /* Disable Acknowledgement */
  I2C_AcknowledgeConfig(IOE_I2C, DISABLE);
  
  /* Transmit the slave address and enable writing operation */
  I2C_Send7bitAddress(IOE_I2C, IOE_ADDR, I2C_Direction_Transmitter);
  
  /* Test on EV6 and clear it */
  IOE_TimeOut = TIMEOUT_MAX;  
  while (!I2C_GetFlagStatus(IOE_I2C, I2C_FLAG_ADDR))
  {
    if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
  }
  
  /* Read status register 2 to clear ADDR flag */
  IOE_I2C->SR2;
  
  /* Test on EV8 and clear it */
  IOE_TimeOut = TIMEOUT_MAX;
  while (!I2C_GetFlagStatus(IOE_I2C, I2C_FLAG_TXE))
  {
    if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
  }
  
  /* Transmit the first address for r/w operations */
  I2C_SendData(IOE_I2C, RegisterAddr);
  
  /* Test on EV8 and clear it */
  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());
  }
  
  /* Regenerate a start condition */
  I2C_GenerateSTART(IOE_I2C, ENABLE);
  
  /* Test on EV5 and clear it */
  IOE_TimeOut = TIMEOUT_MAX;
  while (!I2C_GetFlagStatus(IOE_I2C, I2C_FLAG_SB))
  {
    if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
  }
  
  /* Transmit the slave address and enable writing operation */
  I2C_Send7bitAddress(IOE_I2C, IOE_ADDR, I2C_Direction_Receiver);
  
  /* Test on EV6 and clear it */
  IOE_TimeOut = TIMEOUT_MAX;
  while (!I2C_GetFlagStatus(IOE_I2C, I2C_FLAG_ADDR))
  {
    if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
  }
  
    /* Read status register 2 to clear ADDR flag */
  IOE_I2C->SR2;
  
  /* Test on EV7 and clear it */
  IOE_TimeOut = TIMEOUT_MAX;
  while (!I2C_GetFlagStatus(IOE_I2C, I2C_FLAG_RXNE))
  {
    if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
  }
  
  /* End the configuration sequence */
  I2C_GenerateSTOP(IOE_I2C, ENABLE);
  
  /* Load the register value */
  tmp = I2C_ReceiveData(IOE_I2C);
  
  /* Enable Acknowledgement */
  I2C_AcknowledgeConfig(IOE_I2C, ENABLE);
  
  /* Return the read value */
  return tmp;
  
}
开发者ID:CHPeter,项目名称:stm32f429-gps,代码行数:94,代码来源:stm32f429i_discovery_ioe.c


示例10: LM75_WriteConfReg

/**
  * @brief  Write to the configuration register of the LM75.
  * @param  RegValue: sepecifies the value to be written to LM75 configuration
  *         register.
  * @retval None
  */
uint8_t LM75_WriteConfReg(uint8_t RegValue)
{
  uint8_t LM75_BufferTX = 0;
  LM75_BufferTX = (uint8_t)(RegValue);

  /* 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_TX, (uint8_t*)(&LM75_BufferTX), 1);

  /* 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) == RESET)
  {
    if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  }

  /* Transmit the slave address and enable writing operation */
  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();
  }

  /* Transmit the first address for r/w operations */
  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();
  }

  /* Enable I2C DMA request */
  I2C_DMACmd(LM75_I2C,ENABLE);

  /* Enable DMA TX Channel */
  DMA_Cmd(LM75_DMA_TX_CHANNEL, ENABLE);

  /* Wait until DMA Transfer Complete */
  LM75_Timeout = LM75_LONG_TIMEOUT;
  while (!DMA_GetFlagStatus(LM75_DMA_TX_TCFLAG))
  {
    if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  }

  /* Wait until BTF Flag is set before generating STOP */
  LM75_Timeout = LM75_LONG_TIMEOUT;
  while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF)))
  {
    if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  }

  /* Send STOP Condition */
  I2C_GenerateSTOP(LM75_I2C, ENABLE);

  /* Disable DMA TX Channel */
  DMA_Cmd(LM75_DMA_TX_CHANNEL, DISABLE);

  /* Disable I2C DMA request */
  I2C_DMACmd(LM75_I2C,DISABLE);

  /* Clear DMA TX Transfer Complete Flag */
  DMA_ClearFlag(LM75_DMA_TX_TCFLAG);

  return LM75_OK;

}
开发者ID:szymon2103,项目名称:Stm32,代码行数:86,代码来源:stm32_eval_i2c_tsensor.c


示例11: 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);

//.........这里部分代码省略.........
开发者ID:szymon2103,项目名称:Stm32,代码行数:101,代码来源:stm32_eval_i2c_tsensor.c


示例12: LM75_ReadConfReg

/**
  * @brief  Read the configuration register from the LM75.
  * @param  None
  * @retval LM75 configuration register value.
  */
uint8_t LM75_ReadConfReg(void)
{
  uint8_t LM75_BufferRX[2] ={0,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);

  /*!< Return Temperature value */
  return (uint8_t)LM75_BufferRX[0];
}
开发者ID:szymon2103,项目名称:Stm32,代码行数:100,代码来源:stm32_eval_i2c_tsensor.c


示例13: Codec_WriteRegister

void Codec_WriteRegister(uint8_t RegisterAddr, uint8_t RegisterValue) //dodane ale zmodyfikowane, nie zwraca info o bledzie
{

  /*!< 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_I2C_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();
  }

  /* 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 */

  /* Return the verifying value: 0 (Passed) or 1 (Failed) */

}
开发者ID:PUT-PTM,项目名称:STM32F4_musicPlayer,代码行数:61,代码来源:codec.c


示例14: I2C1_EV_IRQHandler

/*******************************************************************************
* Function Name  : I2C1_EV_IRQHandler
* Description    : This function handles I2C1 Event interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void I2C1_EV_IRQHandler(void)
{
  switch (I2C_GetLastEvent(I2C1))
  {
    case I2C_EVENT_MASTER_MODE_SELECT:                 /* EV5 */
      if(Direction == Transmitter)
      {
        /* Master Transmitter ----------------------------------------------*/
        /* Send slave Address for write */
        I2C_Send7bitAddress(I2C1, I2C2_SLAVE_ADDRESS7, I2C_Direction_Transmitter);
      }
      else
      {
        /* Master Receiver -------------------------------------------------*/
        /* Send slave Address for read */
        I2C_Send7bitAddress(I2C1, I2C2_SLAVE_ADDRESS7, I2C_Direction_Receiver);
       
      }
      break;
        
    /* Master Transmitter --------------------------------------------------*/
    /* Test on I2C1 EV6 and first EV8 and clear them */
    case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED:  
      
      /* Send the first data */
      I2C_SendData(I2C1, I2C1_Buffer_Tx[Tx1_Idx++]); 
      break;

    /* Test on I2C1 EV8 and clear it */
  case I2C_EVENT_MASTER_BYTE_TRANSMITTING:  /* Without BTF, EV8 */     
      if(Tx1_Idx < (Tx1BufferSize))
      {
        /* Transmit I2C1 data */
        I2C_SendData(I2C1, I2C1_Buffer_Tx[Tx1_Idx++]);
      
      }
      else
      {
        I2C_TransmitPEC(I2C1, ENABLE); 
        I2C_ITConfig(I2C1, I2C_IT_BUF, DISABLE);
      }            
      break;

    case I2C_EVENT_MASTER_BYTE_TRANSMITTED: /* With BTF EV8-2 */
        I2C_ITConfig(I2C1, I2C_IT_BUF, ENABLE);
        /* I2C1 Re-START Condition */
        I2C_GenerateSTART(I2C1, ENABLE);
      break;

    /* Master Receiver -------------------------------------------------------*/
    case I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED:
      if(Tx2BufferSize == 1)
      {
        /* Disable I2C1 acknowledgement */
        I2C_AcknowledgeConfig(I2C1, DISABLE);
        /* Send I2C1 STOP Condition */
        I2C_GenerateSTOP(I2C1, ENABLE);
      }
      break;

   /* Test on I2C1 EV7 and clear it */
   case I2C_EVENT_MASTER_BYTE_RECEIVED:
     /* Store I2C1 received data */
     I2C1_Buffer_Rx[Rx1_Idx++] = I2C_ReceiveData (I2C1);
     /* Disable ACK and send I2C1 STOP condition before receiving the last data */
     if(Rx1_Idx == (Tx2BufferSize - 1))
     {
       /* Disable I2C1 acknowledgement */
       I2C_AcknowledgeConfig(I2C1, DISABLE);
       /* Send I2C1 STOP Condition */
       I2C_GenerateSTOP(I2C1, ENABLE);
     }
     break;

    default:
      break;
  }
}
开发者ID:zaurus04,项目名称:cortexm3,代码行数:85,代码来源:stm32f10x_it.c


示例15: I2C_Read

/**
  * @brief  Read from a device connected to I2C - cannot receive less than 2 bytes at a time
  * @param  Pointer to the data string, number of bytes to read, device address, sub address
  * @retval I2C success/error code
  */
I2C_Returntype I2C_Read(uint8_t* Data_Pointer,uint8_t Bytes, uint8_t Addr, uint8_t Sub_Addr) {//Reads from an i2c device
	int8_t n=0;								//0xFF as the Sub_Addr disables sub address
	uint16_t Time=0;
	if(Sub_Addr!=0xFF) {							//0xFF disables this - so we wont setup addr pointer
		I2C_GenerateSTART( I2C1, ENABLE );
		while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) {
			Time++;
			if(Time>I2C_TIMEOUT) return I2C_START_TIMEOUT;
		}
		Time=0;
		I2C_Send7bitAddress( I2C1, Addr, I2C_Direction_Transmitter );	//Address write
		while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) {
			Time++;
			if(Time>I2C_TIMEOUT) return I2C_SACK_TIMEOUT;		//Checks that the slave acknowledged
			if(SET==I2C_GetFlagStatus(I2C1, I2C_FLAG_AF)) {
				I2C_ClearFlag(I2C1, I2C_FLAG_AF);
				I2C_GenerateSTOP( I2C1, ENABLE );		//Enable the STOP here - so hardware is ready again
				return I2C_SACK_FAILURE;			//Slave did not ack
			}
		}
		Time=0;
		I2C_SendData( I2C1, Sub_Addr );					//Write sub address register
		while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) {
			Time++;
			if(Time>I2C_TIMEOUT) return I2C_TX_TIMEOUT;
		}
		//I2C_GenerateSTOP( I2C1, ENABLE );				//This code doesnt _seem_ to be needed
		//while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)==SET);		//Wait for bus to go inactive
	}
	Time=0;
	I2C_GenerateSTART( I2C1, ENABLE );					//Repeated start or the first start
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) {
		Time++;
		if(Time>I2C_TIMEOUT) return I2C_RSTART_TIMEOUT;			//note that if we disable sub addr, then a start error
	}									//becomes a repeated start error
	Time=0;
	I2C_Send7bitAddress( I2C1, Addr|0x01, I2C_Direction_Receiver );		//Address to read
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) {
		Time++;
		if(Time>I2C_TIMEOUT) return I2C_SACK_TIMEOUT;			//Checks that the slave acknowledged
		if(SET==I2C_GetFlagStatus(I2C1, I2C_FLAG_AF)) {
			I2C_ClearFlag(I2C1, I2C_FLAG_AF);
			I2C_GenerateSTOP( I2C1, ENABLE );			//Enable the STOP here - so hardware is ready again
			return I2C_SACK_FAILURE;				//Slave did not ack
		}
	}									//We now auto switch to rx mode
	if(Bytes>2) {								//More than two bytes to receive
		Time=0;
		while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) {	//Wait for the first byte
			Time++;
			if(Time>I2C_TIMEOUT) return I2C_RX_TIMEOUT;
		}
		for(;n<((int8_t)Bytes-3);n++) {
			Time=0;
			Data_Pointer[n]=I2C_ReceiveData(I2C1);
			while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) {
				Time++;
				if(Time>I2C_TIMEOUT) return I2C_RX_TIMEOUT;
			}
		}
		Time=0;
		while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BTF)!=SET) {		//Wait for two bytes to be received - ref man p712
			Time++;
			if(Time>I2C_TIMEOUT) return I2C_RX_TIMEOUT;
		}
		I2C_AcknowledgeConfig(I2C1, DISABLE);				//Do not ack the last byte
		Data_Pointer[n++]=I2C_ReceiveData(I2C1);			//Third to last byte
		I2C_GenerateSTOP( I2C1, ENABLE );				//Enable the STOP here
		Data_Pointer[n++]=I2C_ReceiveData(I2C1);			//Read the Penultimate from buffer
		Time=0;
		while(I2C_GetFlagStatus(I2C1,I2C_FLAG_RXNE)!=SET) {		//Last byte received here with a NACK and STOP
			Time++;
			if(Time>I2C_TIMEOUT) return I2C_RX_TIMEOUT;
		}
	}
	else {
		I2C_AcknowledgeConfig(I2C1, DISABLE);				//Do not ack the last byte
		Time=0;
		while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BTF)!=SET) {		//Wait for two bytes to be received - ref man p713
			Time++;
			if(Time>I2C_TIMEOUT) return I2C_RX_TIMEOUT;
		}
		I2C_GenerateSTOP( I2C1, ENABLE );				//Enable the STOP here
		Data_Pointer[n++]=I2C_ReceiveData(I2C1);			//First byte to lowest location
	}
	Data_Pointer[n]=I2C_ReceiveData(I2C1);					//Clear the buffer (last byte is in it)
	I2C_AcknowledgeConfig(I2C1, ENABLE);					//Re-enable ACK
	return I2C_SUCCESS;							//Exit ok
}
开发者ID:Laurenceb,项目名称:Dactyl,代码行数:94,代码来源:i2c.c


示例16: I2C_ReadDataBuffer


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ I2C_Stop函数代码示例发布时间:2022-05-30
下一篇:
C++ I2C_ReceiveData函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap