cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Interface

bhavesh
Associate II
Posted on September 29, 2015 at 23:28

I Trying To I2C Interface With DS1621 Using HAL Driver STM32Cube-MX 4.7.0

As Per HAL Driver I Try To Check Device Is Ready or NOT It Stuck At 

if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_FLAG_SB, RESET, Timeout) != HAL_OK)

It Always Getting timeout error what is the role of 

I2C_FLAG_SB bit

&sharpdefine Slave_Address    0x90

int main(void)

{

  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();

  //MX_I2C1_Init();

  while (1)

  {

    MX_I2C1_Init();

    if(HAL_I2C_IsDeviceReady(&hi2c1, Slave_Address, 10, 1000) == HAL_OK);

 if(HAL_I2C_Master_Transmit(&hi2c1, Slave_Address, ''0xEE'', 1, 1000) == HAL_OK);

    if(HAL_I2C_IsDeviceReady(&hi2c1, Slave_Address, 10, 1000) == HAL_OK);

 if(HAL_I2C_Master_Transmit(&hi2c1, Slave_Address, ''0xAA'', 1, 1000) == HAL_OK);

    if(HAL_I2C_IsDeviceReady(&hi2c1, 0x91, 10, 1000) == HAL_OK);

    if(HAL_I2C_Master_Receive(&hi2c1, 0x91, Temp, 2, 1000) == HAL_OK);

  }

}

void MX_I2C1_Init(void)

{

  hi2c1.Instance = I2C1;

  hi2c1.Init.ClockSpeed = 100000;

  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;

  hi2c1.Init.OwnAddress1 = 64;

  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;

  hi2c1.Init.OwnAddress2 = 0;

  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_ENABLED;

  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;

  HAL_I2C_Init(&hi2c1);

}

i used 1k resistor for pull-up 

what is the flow to use this driver please suggest me for initialization of I2C interface

also suggest for tool where i can show signal activity

#stm32f4-i2c-hal-driver #i2c-stm32
1 REPLY 1
bhavesh
Associate II
Posted on October 01, 2015 at 07:31

here is Standard Peripheral Used But still no data received please help me what thing i missed

#include ''main.h''

uint32_t  DS1621Timeout = DS1621_LONG_TIMEOUT;

uint8_t DS1621_BufferRX[2] ={0,0};

uint16_t tmp = 0, Add = 0;

uint32_t DataNum = 0;

uint16_t tempature = 0;

void Delay(volatile int i)

{

  for(volatile int y = 0; y <= 600; y++)

  {

    for(volatile int z = i; z<=100; z++);

  }

}

  

static void data_read(uint8_t DS1621_Add)

{

  I2C_AcknowledgeConfig(DS1621_I2C, ENABLE);

  if((I2C_GetFlagStatus(DS1621_I2C, I2C_FLAG_RXNE)) == SET)

  {

    DS1621_BufferRX[0] = I2C_ReceiveData(DS1621_I2C);

  }

  I2C_AcknowledgeConfig(DS1621_I2C, DISABLE);

  I2C_GenerateSTOP(DS1621_I2C, ENABLE);

  if((I2C_GetFlagStatus(DS1621_I2C, I2C_FLAG_RXNE)) == SET)

  {

    DS1621_BufferRX[1] = I2C_ReceiveData(DS1621_I2C);

  } 

}

static uint8_t data_write(uint8_t data)

{

  I2C_SendData(DS1621_I2C, data);

  while((I2C_GetFlagStatus(DS1621_I2C, I2C_FLAG_TC)) == SET);

  return SUCCESS;

}

int main(void)

{

  DS1621_Init();

  DS1621_LowLevel_Init();

  while (1)

  {

    if(DS1621_GetStatus() == SUCCESS)

    {

      Basic_Configuration(1, I2C_Direction_Transmitter);

      data_write(0xEE);

      data_write(0xAA);

      I2C_stop(I2C1);

    }

    if(DS1621_GetStatus() == SUCCESS)

    {

      Basic_Configuration(2, I2C_Direction_Receiver);

      data_read(DS1621_ADDR_R);

      I2C_stop(I2C1);

    }

  }

}

void DS1621_Init(void)

{  

  I2C_InitTypeDef  I2C_InitStructure;

  

  I2C_DeInit(I2C1);

  I2C_SoftwareResetCmd(I2C1);

  /* DS1621_I2C configuration */

  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

  I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Disable;

  I2C_InitStructure.I2C_DigitalFilter = 0x00;

  I2C_InitStructure.I2C_OwnAddress1 = 0x90;

  I2C_InitStructure.I2C_Ack = I2C_Ack_Disable;

  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  I2C_InitStructure.I2C_Timing = 1000;//0x1045061D;

  

  /* Apply DS1621_I2C configuration after enabling it */

  I2C_Init(DS1621_I2C, &I2C_InitStructure);

  

  /* DS1621_I2C Peripheral Enable */

  I2C_Cmd(DS1621_I2C, ENABLE);

}

ErrorStatus DS1621_GetStatus(void)

{

  uint32_t I2C_TimeOut = I2C_TIMEOUT;

  while(I2C_GetFlagStatus(DS1621_I2C, I2C_FLAG_BUSY));

  /* Generate start */

  I2C_GenerateSTART(DS1621_I2C, ENABLE);  

  

  /* Wait until timeout elapsed */

  while ((I2C_GetFlagStatus(DS1621_I2C, I2C_FLAG_BUSY) == SET) && (I2C_TimeOut-- != 0)); 

  

  /* Check if Temp sensor is ready for use */

  if ((I2C_GetFlagStatus(DS1621_I2C, I2C_ISR_NACKF) == RESET) || (I2C_TimeOut == 0))

  {      

    /* Clear NACKF and STOPF */

    I2C_ClearFlag(DS1621_I2C, I2C_ICR_NACKCF | I2C_ICR_STOPCF);

    

    return ERROR;

  }

  else

  {

    /* Clear STOPF */

    I2C_ClearFlag(DS1621_I2C, I2C_ICR_STOPCF);

    

    return SUCCESS;

  }    

}

void DS1621_LowLevel_Init(void)

{

  GPIO_InitTypeDef  GPIO_InitStructure;

  /* DS1621_I2C Periph clock enable */

  RCC_APB1PeriphClockCmd(DS1621_I2C_CLK, ENABLE);

    

  /* Configure the I2C clock source. The clock is derived from the HSI */

  RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);

  RCC_AHBPeriphClockCmd(DS1621_I2C_SCL_GPIO_CLK | DS1621_I2C_SDA_GPIO_CLK, ENABLE);

  

  /* Connect PXx to I2C_SCL */

  GPIO_PinAFConfig(DS1621_I2C_SCL_GPIO_PORT, DS1621_I2C_SCL_SOURCE, DS1621_I2C_SCL_AF);

  /* Connect PXx to I2C_SDA */

  GPIO_PinAFConfig(DS1621_I2C_SDA_GPIO_PORT, DS1621_I2C_SDA_SOURCE, DS1621_I2C_SDA_AF); 

    

  /* Configure DS1621_I2C pins: SCL */

  GPIO_InitStructure.GPIO_Pin = DS1621_I2C_SCL_PIN;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;

  GPIO_Init(DS1621_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);

  /* Configure DS1621_I2C pins: SDA */

  GPIO_InitStructure.GPIO_Pin = DS1621_I2C_SDA_PIN;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;

  GPIO_Init(DS1621_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);

}

void DS1621_LowLevel_DeInit(void)

{

  GPIO_InitTypeDef  GPIO_InitStructure;

  /* Disable DS1621_I2C */

  I2C_Cmd(DS1621_I2C, DISABLE);

  

  /* DeInitializes the DS1621_I2C */

  I2C_DeInit(DS1621_I2C);

  

  /* DS1621_I2C Periph clock disable */

  RCC_APB1PeriphClockCmd(DS1621_I2C_CLK, DISABLE);

    

  /* Configure DS1621_I2C pins: SCL */

  GPIO_InitStructure.GPIO_Pin = DS1621_I2C_SCL_PIN;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_Init(DS1621_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);

  /* Configure DS1621_I2C pins: SDA */

  GPIO_InitStructure.GPIO_Pin = DS1621_I2C_SDA_PIN;

  GPIO_Init(DS1621_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);

}

void Basic_Configuration(uint8_t Data_Size, uint8_t I2C_Direction)

{

  I2C_NumberOfBytesConfig(DS1621_I2C, Data_Size);

  if (I2C_Direction == I2C_Direction_Transmitter)

  {

    I2C_MasterRequestConfig(DS1621_I2C, I2C_Direction_Transmitter);

    I2C_SlaveAddressConfig(DS1621_I2C, DS1621_ADDR);

  }

  else

  {

    I2C_MasterRequestConfig(DS1621_I2C, I2C_Direction_Receiver);

    I2C_SlaveAddressConfig(DS1621_I2C, DS1621_ADDR_R);

  }

}

void I2C_stop(I2C_TypeDef* I2Cx)

{

// Send I2C1 STOP Condition

I2C_GenerateSTOP(I2Cx, ENABLE);

}        

        

#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

  while (1)

  {

  }

}

#endif