/**
  * @brief  Checks the LM75 status.
  * @param  None
  * @retval ErrorStatus: LM75 Status (ERROR or SUCCESS).
  */
ErrorStatus LM75_GetStatus(void)
{
  uint32_t I2C_TimeOut = I2C_TIMEOUT;

  /*!< Clear the LM75_I2C AF flag */
  I2C_ClearFlag(LM75_I2C, I2C_FLAG_AF);

  /*!< Enable LM75_I2C acknowledgement if it is already disabled by other function */
  I2C_AcknowledgeConfig(LM75_I2C, ENABLE);

  /*---------------------------- Transmission Phase ---------------------------*/

  /*!< Send LM75_I2C START condition */
  I2C_GenerateSTART(LM75_I2C, ENABLE);

  /*!< Test on LM75_I2C EV5 and clear it */
  while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) && I2C_TimeOut)  /*!< EV5 */
  {
    I2C_TimeOut--;
  }
  if (I2C_TimeOut == 0)
  {
    return ERROR;
  }
  
  I2C_TimeOut = I2C_TIMEOUT;

  /*!< Send STLM75 slave address for write */
  I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter);

  while ((!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) && I2C_TimeOut)/* EV6 */
  {
    I2C_TimeOut--;
  }

  if ((I2C_GetFlagStatus(LM75_I2C, I2C_FLAG_AF) != 0x00) || (I2C_TimeOut == 0))
  {
    return ERROR;
  }
  else
  {
    return SUCCESS;
  }
}
Ejemplo n.º 2
0
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
    I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
    int timeout;
    int count;
  
/*
    // Wait until the bus is not busy anymore
    timeout = LONG_TIMEOUT;
    while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
        timeout--;
        if (timeout == 0) {
            return 0;
        }
    }
*/

    i2c_start(obj);

    // Send slave address for write
    I2C_Send7bitAddress(i2c, address, I2C_Direction_Transmitter);
  
    // Wait address is acknowledged
    timeout = FLAG_TIMEOUT;
    while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) {
        timeout--;
        if (timeout == 0) {
            return 0;
        }
    }

    for (count = 0; count < length; count++) {
        if (i2c_byte_write(obj, data[count]) != 1) {
            i2c_stop(obj);
            return 0;
        }
    }

    // If not repeated start, send stop.
    if (stop) {
        i2c_stop(obj);
    }

    return count;
}
Ejemplo n.º 3
0
Status I2C_Write(I2C_TypeDef* I2Cx, const uint8_t* buf,  uint32_t nbyte, uint8_t SlaveAddress)
{
    __IO uint32_t Timeout = 0;

    /* Enable Error IT (used in all modes: DMA, Polling and Interrupts */
    //    I2Cx->CR2 |= I2C_IT_ERR;

    if (nbyte)
      {
	Timed(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));

	// Intiate Start Sequence

	I2C_GenerateSTART(I2Cx, ENABLE);
	Timed(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));

	// Send Address  EV5

	I2C_Send7bitAddress(I2Cx, SlaveAddress, I2C_Direction_Transmitter);
    //Timed(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

	// EV6

	// Write first byte EV8_1

	I2C_SendData(I2Cx, *buf++);

	while (--nbyte) {

	  // wait on BTF

      //Timed(!I2C_GetFlagStatus(I2Cx, I2C_FLAG_BTF));
	  I2C_SendData(I2Cx, *buf++);
	}

    //Timed(!I2C_GetFlagStatus(I2Cx, I2C_FLAG_BTF));
	I2C_GenerateSTOP(I2Cx, ENABLE);
    //Timed(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF));
      }
    return Success;
 errReturn:
    return Error;
}
Ejemplo n.º 4
0
uint32_t AT24_Read(uint16_t Addr, uint8_t *pdata, int size)
{
  uint32_t Timeout;
   
  I2C_AcknowledgeConfig(I2C1, ENABLE);
    
  Timeout = I2C_TIMEOUT;   
  while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)) { if((Timeout--) == 0) return 1; }
 
  I2C_GenerateSTART(I2C1, ENABLE);
 
  Timeout = I2C_TIMEOUT;   
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) { if((Timeout--) == 0) return 1; }
 
  I2C_Send7bitAddress(I2C1, 0xA0, I2C_Direction_Transmitter);

  Timeout = I2C_TIMEOUT;   
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if((Timeout--) == 0) return 1; }
 
  I2C_SendData(I2C1, (uint8_t)((Addr & 0xFF00) >> 8));
 
  Timeout = I2C_TIMEOUT;   
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if((Timeout--) == 0) return 1; }
 
  I2C_SendData(I2C1, (uint8_t)(Addr & 0x00FF));
 
  Timeout = I2C_TIMEOUT;   
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if((Timeout--) == 0) return 0; }
 
  I2C_GenerateSTART(I2C1, ENABLE);
 
  Timeout = I2C_TIMEOUT;   
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) { if((Timeout--) == 0) return 1; }
 
  I2C_Send7bitAddress(I2C1, 0xA1, I2C_Direction_Receiver);
 
  while(size-- > 0)
  {
    Timeout = I2C_TIMEOUT;   
    while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED))
    {
      if((Timeout--) == 0) return 1; 
    }
    *(pdata++) = I2C_ReceiveData(I2C1);
  }
  
  I2C_AcknowledgeConfig(I2C1, DISABLE);
  I2C_GenerateSTOP(I2C1, ENABLE);
  return 0;
}
Ejemplo n.º 5
0
uint8_t ucI2C_ReadAck(I2C_TypeDef* I2Cx) {
	uint8_t data;

	/* Enable ACK */
	I2Cx->CR1 |= I2C_CR1_ACK;

	/* Wait till not received */
	TM_I2C_Timeout = TM_I2C_TIMEOUT;
	while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)) {
		if (--TM_I2C_Timeout == 0x00) {
			return 1;
		}
	}

	/* Read data */
	data = I2Cx->DR;

	/* Return data */
	return data;
}
/**
  * @brief  This function handles the DMA Tx Channel interrupt Handler.
  *     @note This function should be called in the
  *       DMA1_CHANNEL2_3_IRQHandler in the stm8l15x_it.c file.
  *
  *       // INTERRUPT_HANDLER(DMA1_CHANNEL2_3_IRQHandler, 3)
  *       // {
  *           // sEE_I2C_DMA_TX_IRQHandler();
  *       // }
  * @param  None
  * @retval None
  */
void sEE_I2C_DMA_TX_IRQHandler(void)
{
    /* Check if the DMA transfer is complete */
    if (DMA_GetFlagStatus(sEE_I2C_DMA_FLAG_TX_TC) != RESET)
    {
        /* Disable the DMA Tx Channel and Clear all its Flags */
        DMA_Cmd(sEE_I2C_DMA_CHANNEL_TX, DISABLE);
        DMA_ClearFlag(sEE_I2C_DMA_FLAG_TX_TC);

        /*!< Wait till all data have been physically transferred on the bus */
        while (!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
        {}

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

        /* Reset the variable holding the number of data to be written */
        *sEEDataWritePointer = 0;
    }
}
Ejemplo n.º 7
0
	u8 Temp_Read(void)
	{
		
		 u8 MSB,LSB;
		 
		//等待I2C閒置
		while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
		I2C_AcknowledgeConfig(I2C1,ENABLE); 
		//啟動訊號
		I2C_GenerateSTART(I2C1,ENABLE);	
		while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)!=SUCCESS);

		//發送裝置位置
		I2C_Send7bitAddress(I2C1,0xF0,I2C_Direction_Transmitter);
		while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)!=SUCCESS);
		I2C_Cmd(I2C1,ENABLE);

		//發送記憶體位置
		I2C_SendData(I2C1,0xE5);
		while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)!=SUCCESS);

		//啟動訊號
		I2C_GenerateSTART(I2C1,ENABLE);	
		while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)!=SUCCESS);
		
		//發送裝置位置
		I2C_Send7bitAddress(I2C1,0xF0,I2C_Direction_Receiver);
		while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)!=SUCCESS);

		//I2C_AcknowledgeConfig(I2C1,DISABLE); //ACK 關閉	
		MSB=I2C_ReceiveData(I2C1);
	
		while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)!=SUCCESS);
		
		LSB=I2C_ReceiveData(I2C1);
	
		while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)!=SUCCESS);
		
		 	//停止訊號
		I2C_GenerateSTOP(I2C1,ENABLE);
			
		USART_SendData(USART1,MSB);
	
	}
Ejemplo n.º 8
0
uint8_t sensor_read(uint8_t sensor_address, uint8_t SUB)//read a byte from sensor
{ 
	uint8_t data;  

	/* Send START condition */ 
	I2C_GenerateSTART(I2C2, ENABLE); 

	/* Test on EV5 and clear it */ 
	while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)); 

	/* In the case of a single data transfer disable ACK before reading the data */ 
    I2C_AcknowledgeConfig(I2C2, DISABLE); 

	/* Send EEPROM address for write */ 
	sensor_address=sensor_address|0x01;
	I2C_Send7bitAddress(I2C2, sensor_address, I2C_Direction_Transmitter); //0xD2

	/* Test on EV6 and clear it */ 
	while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); 

	/* Send the EEPROM's internal address to write to */ 
	I2C_SendData(I2C2, SUB);  

	/* 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 EEPROM address for read */ 
	I2C_Send7bitAddress(I2C2, sensor_address, I2C_Direction_Receiver); 

	/* Test on EV6 and clear it */ 
	while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); 

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

	/* Test on EV7 and clear it */ 
	while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED));    

	/* Read a byte from the EEPROM */ 
    data = I2C_ReceiveData(I2C2);   
	
	return data;   
} 
Ejemplo n.º 9
0
 uint32_t I2C_Read_Buf(uint8_t* buf_ptr, uint32_t len){
	 uint32_t nbytes = 0;
	 I2C_AcknowledgeConfig(I2Cx, ENABLE);

	     for (nbytes = 0; nbytes < (int) len; nbytes++) {
	         /* Send NACK on the last byte */
	         if (nbytes == ((int) len - 1)) {
	             I2C_AcknowledgeConfig(I2Cx, DISABLE);

	             /* Send STOP Condition */
	             I2C_GenerateSTOP(I2Cx, ENABLE);
	         }

	         while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)){}

	         /* Read a byte from the slave */
	         buf_ptr[nbytes] = I2C_ReceiveData(I2Cx);
	     }

	 return nbytes;
 }
Ejemplo n.º 10
0
void ITG3200_read_burst(I2C_TypeDef * I2Cx, uint8_t devwrite ,uint8_t * data_out, uint8_t nBytes){

     // I2C_read_burst(I2Cx, slave_addr,ITG3200_XOUT_H, data_out, nBytes);

    I2C_start(I2Cx, devwrite, I2C_Direction_Transmitter);
    I2C_SendData(I2Cx,ITG3200_XOUT_H);
    while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BTF) == RESET);
    I2C_stop(I2Cx);
    I2C_start(I2Cx,devwrite, I2C_Direction_Receiver);
    I2C_AcknowledgeConfig(I2Cx, ENABLE);

    for(int i= 0; i < nBytes;  i++) {

      while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
        data_out[i] = I2C_ReceiveData(I2Cx);
      }
      //set the stack pointer to X MSB
      //I2C_SendData(I2Cx,HMC8883_DATA_OUT_X_MSB_REG_ADDR);
      I2C_AcknowledgeConfig(I2Cx, DISABLE);
      I2C_stop(I2Cx);
}
Ejemplo n.º 11
0
/**
	*************************************************************************************
  * @brief  read one byte without acknowledge and end conversation
  * @param  None
  * @retval the received byte
	**************************************************************************************
  */
static uint8_t readEndByteI2C(void){
	
	/* buffer for the received byte */
	uint8_t ReceivedByte;
	
	/* disabe acknowledge of received data */
	I2C_AcknowledgeConfig(I2C, DISABLE);
	
	/* wait until one byte has been received */
	while( !I2C_CheckEvent(I2C, I2C_EVENT_MASTER_BYTE_RECEIVED) );
	
	
	/* read data from I2C data register and return data byte */
	ReceivedByte = I2C_ReceiveData(I2C);
	
	/* send stop condition */
	I2C_GenerateSTOP(I2C, ENABLE);
	
	/* return value */
	return ReceivedByte;
}
Ejemplo n.º 12
0
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
    I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
    int timeout;
    int count;
    int value;

    if (length == 0) return 0;

    i2c_start(obj);

    // Send slave address for read
    I2C_Send7bitAddress(i2c, address, I2C_Direction_Receiver);

    // Wait address is acknowledged
    timeout = FLAG_TIMEOUT;
    while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) {
        timeout--;
        if (timeout == 0) {
            return 0;
        }
    }

    // Read all bytes except last one
    for (count = 0; count < (length - 1); count++) {
        value = i2c_byte_read(obj, 0);
        data[count] = (char)value;
    }

    // If not repeated start, send stop.
    // Warning: must be done BEFORE the data is read.
    if (stop) {
        i2c_stop(obj);
    }

    // Read the last byte
    value = i2c_byte_read(obj, 1);
    data[count] = (char)value;

    return length;
}
Ejemplo n.º 13
0
void I2C_ReadSByte1(u8 id,u16 addr ,u8* pBuffer,u16 no)
{
    if(no==0)
		return;	
	while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));		
	/*允许1字节1应答模式*/
	I2C_AcknowledgeConfig(I2C2, ENABLE);
	/* 发送起始位 */
    I2C_GenerateSTART(I2C2, ENABLE);
    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));/*EV5,主模式*/	
    /*发送器件地址(写)*/
    I2C_Send7bitAddress(I2C2,  id&0xfe, I2C_Direction_Transmitter);
    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
	
	/*发送地址*/
	I2C_SendData(I2C2, addr>>8);
    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));/*数据已发送*/		
	I2C_SendData(I2C2, addr);
    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));/*数据已发送*/		
	
	/*起始位*/
	I2C_GenerateSTART(I2C2, ENABLE);
	while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));	
	/*器件读*/
	I2C_Send7bitAddress(I2C2, id|I2C_READ, I2C_Direction_Receiver);
	while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    while (no)
    {
		if(no==1)
		{
     		I2C_AcknowledgeConfig(I2C2, DISABLE);	//最后一位后要关闭应答的
    		I2C_GenerateSTOP(I2C2, ENABLE);			//发送停止位
		}	    
		while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)); /* EV7 */
	    *pBuffer = I2C_ReceiveData(I2C2);
	    pBuffer++;
	    /* Decrement the read bytes counter */
	    no--;
    }
	//再次允许应答模式
	I2C_AcknowledgeConfig(I2C2, ENABLE);
}
Ejemplo n.º 14
0
void i2cRead(uint8_t device, uint8_t address, uint8_t *data, uint8_t length)
{
	i2cSendAddress(device, READ, address);

	int i;
	for(i = 0; i<length; i++)
	{
		/* Test on EV7 and clear it */
		while(!I2C_CheckEvent(I2C_BUS, I2C_EVENT_MASTER_BYTE_RECEIVED));

		*data = I2C_ReceiveData(I2C_BUS);
		data++;

		 /* Disable Acknowledgement */
        if(!(i < (length - 1)))
            I2C_AcknowledgeConfig(I2C_BUS, DISABLE);

	}

    /* Send STOP Condition */
    I2C_GenerateSTOP(I2C_BUS, ENABLE);
}
short read8(I2C_TypeDef* I2Cx){
	uint8_t msb=0, lsb=0;
	unsigned char dummy1;
	unsigned short data;

	I2C_AcknowledgeConfig(I2Cx, ENABLE);
	while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));

	I2C_GenerateSTART(I2Cx, ENABLE);
	while(!I2C_GetFlagStatus(I2Cx, I2C_FLAG_SB));
	while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));

	I2C_Send7bitAddress(I2Cx,0x6B<<1,I2C_Direction_Transmitter);
	while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

	I2C_SendData(I2Cx,0x28);
	while(!I2C_GetFlagStatus(I2Cx,I2C_FLAG_TXE));
	while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

	I2C_GenerateSTART(I2Cx, ENABLE);
	while(!I2C_GetFlagStatus(I2Cx, I2C_FLAG_SB));

	I2C_Send7bitAddress(I2Cx, 0x6B<<1, I2C_Direction_Receiver);
	while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

	msb = I2C_ReceiveData(I2Cx);
	while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));

	I2C_NACKPositionConfig(I2Cx, I2C_NACKPosition_Current);
	I2C_AcknowledgeConfig(I2Cx, DISABLE);

	lsb = I2C_ReceiveData(I2Cx);
	while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));

	//dummy1 = I2C_ReceiveData(I2Cx);
	//while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));

	I2C_GenerateSTOP(I2Cx, ENABLE);
	while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_STOPF));


	data  = msb<<8;
	data |= lsb;

	return data;
}
Ejemplo n.º 16
0
void eeprom_write_int_addr(uint16_t addr,uint32_t data)
{
    eeprom_prelude();
    I2C_SendData(I2C1,  (uint8_t)addr>>8);
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) );
    I2C_SendData(I2C1, (uint8_t)addr&0xff);
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) );
    I2C_SendData(I2C1, (uint8_t)(data>>24));
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) );
    I2C_SendData(I2C1, (uint8_t)((data>>16)&0xff));
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) );
    I2C_SendData(I2C1, (uint8_t)((data>>8 )&0xff));
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) );
    I2C_SendData(I2C1, (uint8_t)((data    )&0xff));
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) );
    I2C_GenerateSTOP(I2C1,ENABLE);
}
Ejemplo n.º 17
0
uint8_t i2c::readNack(uint8_t address,  bool readMore = true)
{
	uint8_t data;

	if(started  == false)
	{
		started = true;
		I2C_start(I2Cx, address, I2C_Direction_Receiver);
	}
	
	
	I2C_AcknowledgeConfig(I2Cx, ENABLE); // enable acknowledge of recieved data
	I2C_GenerateSTOP(I2Cx, ENABLE);
	
	// wait until one byte has been received
	while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
	
	return I2C_ReceiveData(I2Cx); // read data from I2C data register and return data byte
	
	
	
	started = false;
}
Ejemplo n.º 18
0
int i2c_sendData(I2C_TypeDef* I2Cx,int data){
  I2C_SendData(I2Cx, data);
  //usart_printf(USARTx,"Sending data:%d\n\r",data);      
  int timeout;
        uint8_t flag1=0,flag2=0;
        /* Test on I2C1 EV8 and clear it */
        timeout = I2C_TIMEOUT_MAX; /* Initialize timeout value */
         
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
        {
             
          /* If the timeout delay is exeeded, exit with error code */
          if ((timeout--) == 0) {
            flag1 = I2Cx->SR1;
           flag2 = I2Cx->SR2;
           usart_printf(USARTx,"Flag1:%04x \n\r Flag2:%04x\n\r",flag1,flag2);
           usart_printf(USARTx,"Failing at sending data");
            return -3;
            
          }
        }  
return 0;
}
Ejemplo n.º 19
0
void ADXL345_read_burst(I2C_TypeDef * I2Cx, uint8_t devread, uint8_t * data_out, uint8_t nBytes ){

        //write mode byte to the fifo control register
          ADXL345_write_register(I2C1, ADXL345_FIFO_CONTROL_REG, devread, ADXL345_FIFO_MODE(ADXL345_FIFO_STREAM));

          I2C_start(I2Cx, devread, I2C_Direction_Transmitter);
          I2C_SendData(I2Cx, ADXL345_X_AXIS_DATA_REG0); 
          //send register address to be read from first (x0) the ADXL345 increments pointer 
          //to access follow up  registers automatically
       
          while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BTF) == RESET);
            I2C_stop(I2Cx);
            I2C_start(I2Cx,(uint8_t)ADXL345_SLAVE_READ_ADDR , I2C_Direction_Receiver);
            I2C_AcknowledgeConfig(I2Cx, ENABLE);

            for(int i = 0; i < nBytes-1; i++) {
            // wait until one byte has been received
             while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
          // read data from I2C data register and return data byte
            data_out[i] = I2C_ReceiveData(I2Cx);
            }
         data_out[nBytes -1]  = ADXL345_read_nack(I2Cx, ADXL345_Z_AXIS_DATA_REG1, (uint8_t)ADXL345_SLAVE_READ_ADDR);
}
Ejemplo n.º 20
0
void MPU6050_Read_Data()
{
	uint8_t i;

	I2C_AcknowledgeConfig(I2C2, ENABLE);

    I2C_GenerateSTART(I2C2, ENABLE);
    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

    I2C_Send7bitAddress(I2C2, MPU6050_I2C_ADDR, I2C_Direction_Transmitter);
    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

    I2C_SendData(I2C2, MPU6050_ACCEL_XOUT_H);
    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

    I2C_GenerateSTART(I2C2, ENABLE);
    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

    I2C_Send7bitAddress(I2C2, MPU6050_I2C_ADDR, I2C_Direction_Receiver);
    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

    for (i=0;i<14;i++)
    {
    	while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED));
        MPU6050_Data_Buffer[i] = I2C_ReceiveData(I2C2);

        if (i == 12)
        {
        	I2C_NACKPositionConfig(I2C2, I2C_NACKPosition_Current);
            I2C_AcknowledgeConfig(I2C2, DISABLE);
        }
    }

    Acc_X = MPU6050_Data_Buffer[0]<<8 | MPU6050_Data_Buffer[1];
    Acc_Y = MPU6050_Data_Buffer[2]<<8 | MPU6050_Data_Buffer[3];
    Acc_Z = MPU6050_Data_Buffer[4]<<8 | MPU6050_Data_Buffer[5];

    Gyr_X = MPU6050_Data_Buffer[8]<<8  | MPU6050_Data_Buffer[9];
    Gyr_Y = MPU6050_Data_Buffer[10]<<8 | MPU6050_Data_Buffer[11];
    Gyr_Z = MPU6050_Data_Buffer[12]<<8 | MPU6050_Data_Buffer[13];

    Temp = MPU6050_Data_Buffer[6]<<8 | MPU6050_Data_Buffer[7];

    Temp = (float)((float)((int16_t)Temp/(float)340.0) + (float)36.53);
}
Ejemplo n.º 21
0
static rt_size_t stm32_i2c_send_bytes(struct rt_i2c_msg *msg)
{
    rt_size_t len = msg->len;
	DMA_InitTypeDef DMA_InitStructure;
	
	if (len < 2) 
	{
		  I2C_SendData(I2Cx, msg->buf[0]);

		  /* Test on EV8 and clear it */
		  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
		return 1;
	} 
	else
	{
		DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)I2Cx_DR_ADDR;
		DMA_InitStructure.DMA_MemoryBaseAddr = (u32)(msg->buf);
		DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
		DMA_InitStructure.DMA_BufferSize = msg->len;
		DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
		DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
		DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
		DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
		DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
		DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
		DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; 
		DMA_Init(DMAx_TX_CHANNEL, &DMA_InitStructure);

		DMA_Cmd(DMAx_TX_CHANNEL, ENABLE);

		//Check finshed
		if(rt_sem_take(&DMA_TX_Sem,20)!=RT_EOK)
			return 0;
	}
    return len;
}
Ejemplo n.º 22
0
uint8_t i2c_read(uint8_t DeviceAddr, uint8_t IntAddr)
{
	printf("r\n\r");
	while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); 
	I2C_GenerateSTART(I2C1, ENABLE);
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
	printf("r\n\r");
	I2C_Send7bitAddress(I2C1, MPU6050, I2C_Direction_Transmitter);
        while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
	printf("ack\n\r");
	I2C_SendData(I2C1,IntAddr);							  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
	printf("r\n\r");
	I2C_GenerateSTART(I2C1, ENABLE);						  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));			    I2C_Send7bitAddress(I2C1, MPU6050, I2C_Direction_Receiver);
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
	I2C_NACKPositionConfig(I2C1, I2C_NACKPosition_Current);			
	I2C_GenerateSTOP(I2C1, ENABLE);				    
	return I2C_ReceiveData(I2C1);

}
Ejemplo n.º 23
0
uint32_t AT24Cxx_read(struct AT24Cxx_init_struct* init, uint8_t addr, uint8_t* buff, uint32_t count)
{
	/* Is it a valid address? */
	if((addr + count) >  (init->pages) * (init->page_size))
		return 0;

	/* start, dummy write, read, stop */
	/* start, I2c_address(W), memory_address, start, I2C_address(R), receive, stop */

  	I2C_GenerateSTART(init->I2C_peripheral, ENABLE);
  	while (!I2C_CheckEvent(init->I2C_peripheral, I2C_EVENT_MASTER_MODE_SELECT));

  	I2C_Send7bitAddress(init->I2C_peripheral, init->I2C_address, I2C_Direction_Transmitter);
  	while (!I2C_CheckEvent(init->I2C_peripheral, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

  	I2C_SendData(init->I2C_peripheral, addr);
	while (!I2C_CheckEvent(init->I2C_peripheral, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  	I2C_GenerateSTART(init->I2C_peripheral, ENABLE);
  	while (!I2C_CheckEvent(init->I2C_peripheral, I2C_EVENT_MASTER_MODE_SELECT));

  	I2C_Send7bitAddress(init->I2C_peripheral, init->I2C_address, I2C_Direction_Receiver);
  	while (!I2C_CheckEvent(init->I2C_peripheral, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

  	while (count--) {

  		/* In a sequential read if the master does not ack then this is the last byte to read */
  		if(count == 0)
  			I2C_AcknowledgeConfig(init->I2C_peripheral, DISABLE);

		while(!I2C_CheckEvent(init->I2C_peripheral, I2C_EVENT_MASTER_BYTE_RECEIVED));
	    *buff = I2C_ReceiveData(init->I2C_peripheral);
	    buff++;
  	}

  	/* Re-enable ack */
  	I2C_AcknowledgeConfig(init->I2C_peripheral, ENABLE);
  	I2C_GenerateSTOP(init->I2C_peripheral, ENABLE);

  	return count;
}
Ejemplo n.º 24
0
/*
 * 函数名:I2C_ByteRead
 * 描述  :从IIC设备寄存器中读取一个字节
 * 输入  :REG_Address 读取数据的寄存器的地址
 * 输出  :无
 * 返回  :无
 * 调用  :内部调用
*/
uint8_t I2C_ByteRead(uint8_t REG_Address)
{
    uint8_t REG_data;
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
    I2C_GenerateSTART(I2C1, ENABLE); //起始信号
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(I2C1, SlaveAddress, I2C_Direction_Transmitter); //发送设备地址+写信号
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //
//I2C_Cmd(I2C1,ENABLE);
    I2C_SendData(I2C1, REG_Address); //发送存储单元地址,从0开始
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_GenerateSTART(I2C1, ENABLE); //起始信号
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(I2C1, SlaveAddress, I2C_Direction_Receiver); //发送设备地址+读信号
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    I2C_AcknowledgeConfig(I2C1, DISABLE);
    I2C_GenerateSTOP(I2C1, ENABLE);
    while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)));
    REG_data = I2C_ReceiveData(I2C1); //读出寄存器数据
    return REG_data;
}
Ejemplo n.º 25
0
/*
@brief  .
@param  None.
@retval None.
*/
unsigned char I2C_ReadByte(unsigned char id, unsigned char read_address)
{  
	unsigned char temp;
	while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));
  	I2C_GenerateSTART(I2C2, ENABLE);
  	while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
  	I2C_Send7bitAddress(I2C2, id, I2C_Direction_Transmitter);
  	while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
  	I2C_Cmd(I2C2, ENABLE);
  	I2C_SendData(I2C2, read_address);  
  	while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  	I2C_GenerateSTART(I2C2, ENABLE);
  	while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
  	I2C_Send7bitAddress(I2C2, id, I2C_Direction_Receiver);
  	while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    I2C_AcknowledgeConfig(I2C2, DISABLE);
    I2C_GenerateSTOP(I2C2, ENABLE);
    while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)));
	          temp = I2C_ReceiveData(I2C2);
  	I2C_AcknowledgeConfig(I2C2, ENABLE);
	return temp;
}
Ejemplo n.º 26
0
uint32_t eeprom_read_int_addr(uint16_t addr)
{
    uint32_t retval=0;
    char i=3;
    
    eeprom_prelude();
    I2C_SendData(I2C1,  (uint8_t)addr>>8);
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) );
    I2C_SendData(I2C1, (uint8_t)addr&0xff);
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) );
    //  I2C_GenerateSTOP(I2C1,ENABLE);

    //seq read
    
    I2C_AcknowledgeConfig(I2C1, ENABLE); // ack 3 bytes
    I2C_GenerateSTART(I2C1,ENABLE);
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));   
    I2C_Send7bitAddress(I2C1,addr_eeprom_8b ,I2C_Direction_Receiver);
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    while(i!=0){
	while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED) );
	retval<<=8;
	retval |= I2C_ReceiveData(I2C1);
	i--;
	
    }
    
    I2C_AcknowledgeConfig(I2C1, DISABLE); // nack 1 byte
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED) );
    retval<<=8;
    retval |= I2C_ReceiveData(I2C1);
    
    I2C_GenerateSTOP(I2C1,ENABLE);
    
    return retval;
     
}
Ejemplo n.º 27
0
void LIS35_ReadRegister(char addr,char *v)
{
  //Reads one register value

  I2C_GenerateSTART(LIS35_I2C,ENABLE);
  //Test on EV5 and clear it
  while(!I2C_CheckEvent(LIS35_I2C, I2C_EVENT_MASTER_MODE_SELECT));  
  //Send LIS35 address, set I2C master in transmiter mode
  I2C_Send7bitAddress(LIS35_I2C, LIS35_Addr, I2C_Direction_Transmitter);
  //Test on EV6 and clear it
  while(!I2C_CheckEvent(LIS35_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
  //Send LIS35 local register address, which will be read
  I2C_SendData(LIS35_I2C, addr);
  // Test on EV8 and clear it
  while(!I2C_CheckEvent(LIS35_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  //Re-generate START, transmition from slave beginning
  I2C_GenerateSTART(LIS35_I2C,ENABLE);
  //Test on EV5 and clear it
  while(!I2C_CheckEvent(LIS35_I2C, I2C_EVENT_MASTER_MODE_SELECT)); 
  //Send LIS35 address, set I2C master in receiver mode
  I2C_Send7bitAddress(LIS35_I2C, LIS35_Addr, I2C_Direction_Receiver);
  //Only one byte will be received
  //NAck must be configured just before checking EV6 -> disable Acknowledge
  I2C_AcknowledgeConfig(LIS35_I2C, DISABLE);
  //Test on EV6 and clear it
  while(!I2C_CheckEvent(LIS35_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
  //Generate STOP Condition
  I2C_GenerateSTOP(LIS35_I2C,ENABLE);
  //Test on EV7 and clear it - Wait until DataN is in Shift register
  while(!I2C_CheckEvent(LIS35_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED));
  //Read DataN from DR register
  *v=I2C_ReceiveData(LIS35_I2C);
  //Enable Acknowledge for next transmission 
  I2C_AcknowledgeConfig(LIS35_I2C, ENABLE);
}
/**
  * @brief  Wait for EEPROM Standby state.
  * 
  * @note  This function allows to wait and check that EEPROM has finished the 
  *        last operation. It is mostly used after Write operation: after receiving
  *        the buffer to be written, the EEPROM may need additional time to actually
  *        perform the write operation. During this time, it doesn't answer to
  *        I2C packets addressed to it. Once the write operation is complete
  *        the EEPROM responds to its address.
  * 
  * @param  None
  * @retval sEE_OK (0) if operation is correctly performed, else return value 
  *         different from sEE_OK (0) or the timeout user callback.
  */
uint32_t sEE_WaitEepromStandbyState(void)      
{
  __IO uint16_t tmpSR1 = 0;
  __IO uint32_t sEETrials = 0;

  /*!< While the bus is busy */
  sEETimeout = sEE_LONG_TIMEOUT;
  while(I2C_GetFlagStatus(sEE_I2C, I2C_FLAG_BUSY))
  {
    if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  }

  /* Keep looping till the slave acknowledge his address or maximum number 
     of trials is reached (this number is defined by sEE_MAX_TRIALS_NUMBER define
     in stm32f429i_discovery_i2c_ee.h file) */
  while (1)
  {
    /*!< Send START condition */
    I2C_GenerateSTART(sEE_I2C, ENABLE);

    /*!< Test on EV5 and clear it */
    sEETimeout = sEE_FLAG_TIMEOUT;
    while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
    {
      if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
    }    

    /*!< Send EEPROM address for write */
    I2C_Send7bitAddress(sEE_I2C, sEEAddress, I2C_Direction_Transmitter);
    
    /* Wait for ADDR flag to be set (Slave acknowledged his address) */
    sEETimeout = sEE_LONG_TIMEOUT;
    do
    {     
      /* Get the current value of the SR1 register */
      tmpSR1 = sEE_I2C->SR1;
      
      /* Update the timeout value and exit if it reach 0 */
      if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
    }
    /* Keep looping till the Address is acknowledged or the AF flag is 
       set (address not acknowledged at time) */
    while((tmpSR1 & (I2C_SR1_ADDR | I2C_SR1_AF)) == 0);
     
    /* Check if the ADDR flag has been set */
    if (tmpSR1 & I2C_SR1_ADDR)
    {
      /* Clear ADDR Flag by reading SR1 then SR2 registers (SR1 have already 
         been read) */
      (void)sEE_I2C->SR2;
      
      /*!< STOP condition */    
      I2C_GenerateSTOP(sEE_I2C, ENABLE);
        
      /* Exit the function */
      return sEE_OK;
    }
    else
    {
      /*!< Clear AF flag */
      I2C_ClearFlag(sEE_I2C, I2C_FLAG_AF);                  
    }
    
    /* Check if the maximum allowed number of trials has bee reached */
    if (sEETrials++ == sEE_MAX_TRIALS_NUMBER)
    {
      /* If the maximum number of trials has been reached, exit the function */
      return sEE_TIMEOUT_UserCallback();
    }
  }
}
/**
  * @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();
  }
  
  /*!< 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));
  
  /*!< 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;
}
/**
  * @brief  Reads a block of data from the EEPROM.
  * @param  pBuffer : pointer to the buffer that receives the data read from 
  *         the EEPROM.
  * @param  ReadAddr : EEPROM's internal address to start reading from.
  * @param  NumByteToRead : pointer to the variable holding number of bytes to 
  *         be read from the EEPROM.
  * 
  *        @note The variable pointed by NumByteToRead is reset to 0 when all the 
  *              data are read from the EEPROM. Application should monitor this 
  *              variable in order know when the transfer is complete.
  * 
  * @note When number of data to be read is higher than 1, this function just 
  *       configures the communication and enable the DMA channel to transfer data.
  *       Meanwhile, the user application may perform other tasks.
  *       When number of data to be read is 1, then the DMA is not used. The byte
  *       is read in polling mode.
  * 
  * @retval sEE_OK (0) if operation is correctly performed, else return value 
  *         different from sEE_OK (0) or the timeout user callback.
  */
uint32_t sEE_ReadBuffer(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 */
  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 (cleared by reading SR1 then writing to DR) */
  sEETimeout = sEE_FLAG_TIMEOUT;
  while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
  {
    if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  }
  
  /*!< Send EEPROM address for write */
  I2C_Send7bitAddress(sEE_I2C, sEEAddress, I2C_Direction_Transmitter);

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

  /*!< 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 */
  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 read from: LSB of the address */
  I2C_SendData(sEE_I2C, (uint8_t)(ReadAddr & 0x00FF));    

  /*!< Test on EV8 and clear it */
  sEETimeout = sEE_FLAG_TIMEOUT;
  while(I2C_GetFlagStatus(sEE_I2C, I2C_FLAG_BTF) == RESET)
  {
    if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  }
  
  /*!< Send STRAT condition a second time */  
  I2C_GenerateSTART(sEE_I2C, ENABLE);
  
  /*!< Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
  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 read */
  I2C_Send7bitAddress(sEE_I2C, sEEAddress, I2C_Direction_Receiver);  
  
  /* If number of data to be read is 1, then DMA couldn't be used */
  /* One Byte Master Reception procedure (POLLING) ---------------------------*/
  if ((uint16_t)(*NumByteToRead) < 2)
  {
    /* Wait on ADDR flag to be set (ADDR is still not cleared at this level */
    sEETimeout = sEE_FLAG_TIMEOUT;
    while(I2C_GetFlagStatus(sEE_I2C, I2C_FLAG_ADDR) == RESET)
    {
      if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
    }     
    
    /*!< Disable Acknowledgement */
    I2C_AcknowledgeConfig(sEE_I2C, DISABLE);   
    
    /* Clear ADDR register by reading SR1 then SR2 register (SR1 has already been read) */
    (void)sEE_I2C->SR2;
    
    /*!< Send STOP Condition */
    I2C_GenerateSTOP(sEE_I2C, ENABLE);
    
    /* Wait for the byte to be received */
    sEETimeout = sEE_FLAG_TIMEOUT;
    while(I2C_GetFlagStatus(sEE_I2C, I2C_FLAG_RXNE) == RESET)
    {
      if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
    }
    
    /*!< Read the byte received from the EEPROM */
    *pBuffer = I2C_ReceiveData(sEE_I2C);
    
    /*!< Decrement the read bytes counter */
    (uint16_t)(*NumByteToRead)--;        
    
    /* Wait to make sure that STOP control bit has been cleared */
    sEETimeout = sEE_FLAG_TIMEOUT;
    while(sEE_I2C->CR1 & I2C_CR1_STOP)
    {
      if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
    }  
    
    /*!< Re-Enable Acknowledgement to be ready for another reception */
    I2C_AcknowledgeConfig(sEE_I2C, ENABLE);    
  }
  else/* More than one Byte Master Reception procedure (DMA) -----------------*/
  {
    /*!< Test on EV6 and clear it */
    sEETimeout = sEE_FLAG_TIMEOUT;
    while(!I2C_CheckEvent(sEE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
    {
      if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
    }  
    
    /* Configure the DMA Rx Channel with the buffer address and the buffer size */
    sEE_LowLevel_DMAConfig((uint32_t)pBuffer, (uint16_t)(*NumByteToRead), sEE_DIRECTION_RX);
    
    /* Inform the DMA that the next End Of Transfer Signal will be the last one */
    I2C_DMALastTransferCmd(sEE_I2C, ENABLE); 
    
    /* Enable the DMA Rx Stream */
    DMA_Cmd(sEE_I2C_DMA_STREAM_RX, ENABLE);    
  }
  
  /* If all operations OK, return sEE_OK (0) */
  return sEE_OK;
}