ee24 icon indicating copy to clipboard operation
ee24 copied to clipboard

Example for init

Open mue-barakat opened this issue 7 years ago • 20 comments

can you provide example for init of the EEPROM pins, as no clear documentation for that?

mue-barakat avatar Oct 25 '18 12:10 mue-barakat

i've also connected A0,A1,A2 to GND with WP too, i'm using CAT24C512WI

mue-barakat avatar Oct 25 '18 12:10 mue-barakat

my init function

  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 100000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

and my EEPROM seller https://www.digikey.com/product-detail/en/on-semiconductor/CAT24C512WI-GT3/CAT24C512WI-GT3OSCT-ND/2699493

I have tested your code but it doesn't work as the Is_Device_Ready never returns HAL_OK

mue-barakat avatar Oct 25 '18 12:10 mue-barakat

Hello. Do you use stm32f1 series,?

nimaltd avatar Oct 25 '18 13:10 nimaltd

yes stm32F103R8Tx

mue-barakat avatar Oct 25 '18 13:10 mue-barakat

I have the same problem with f1 Please check with another i2c

nimaltd avatar Oct 25 '18 13:10 nimaltd

What do you mean another I2C ?

mue-barakat avatar Oct 25 '18 13:10 mue-barakat

i can't use the other I2C output as it's already soldered in my PCB

mue-barakat avatar Oct 25 '18 13:10 mue-barakat

I have 2 demo PCBs both aren't working, every single thing in the processor works except for I2C

mue-barakat avatar Oct 25 '18 13:10 mue-barakat

I think this is a bug for HAL. Some time i2c pin does't work.

please try this /////////////////////// void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c) {
GPIO_InitTypeDef GPIO_InitStruct;

I2Cx_CLK_ENABLE(); <<<<<<<<<<<<<<<<< add here /##-1- Enable GPIO Clocks #################################################/ /* Enable GPIO TX/RX clock */ I2Cx_SCL_GPIO_CLK_ENABLE(); I2Cx_SDA_GPIO_CLK_ENABLE();

/##-2- Configure peripheral GPIO ##########################################/
/* I2C TX GPIO pin configuration */ GPIO_InitStruct.Pin = I2Cx_SCL_PIN; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Alternate = I2Cx_SCL_AF; HAL_GPIO_Init(I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);

/* I2C RX GPIO pin configuration */ GPIO_InitStruct.Pin = I2Cx_SDA_PIN; GPIO_InitStruct.Alternate = I2Cx_SDA_AF; HAL_GPIO_Init(I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);

/##-3- Enable I2C peripheral Clock ########################################/ /* Enable I2C1 clock */ I2Cx_CLK_ENABLE(); }

//////////////////////

nimaltd avatar Oct 26 '18 08:10 nimaltd

I got no definition for I2Cx_CLK_ENABLE function, only got __HAL_RCC_I2C1_CLK_ENABLE();

void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(i2cHandle->Instance==I2C1 || 1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */

  /* USER CODE END I2C1_MspInit 0 */
  
    /**I2C1 GPIO Configuration    
    PB6     ------> I2C1_SCL
    PB7     ------> I2C1_SDA 
    */
	
    GPIO_InitStruct.Pin = I2C_SCL_Pin|I2C_SDA_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* I2C1 clock enable */
    __HAL_RCC_I2C1_CLK_ENABLE();

    /* I2C1 interrupt Init */
    HAL_NVIC_SetPriority(I2C1_EV_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
    HAL_NVIC_SetPriority(I2C1_ER_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
  /* USER CODE BEGIN I2C1_MspInit 1 */

  /* USER CODE END I2C1_MspInit 1 */
  }
}

mue-barakat avatar Oct 30 '18 12:10 mue-barakat

I've added this, but still no hope

void MX_I2C1_Init(void)
{

  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 1000000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0xEE;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;

  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
  HAL_I2C_MspInit(&hi2c1);

}

void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(i2cHandle->Instance==I2C1 || 1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */

  /* USER CODE END I2C1_MspInit 0 */
  
    /**I2C1 GPIO Configuration    
    PB6     ------> I2C1_SCL
    PB7     ------> I2C1_SDA 
    */
	  __HAL_RCC_I2C1_CLK_ENABLE();

	  GPIO_InitStruct.Pin = I2C_SCL_Pin;
	  GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
	  GPIO_InitStruct.Pull = GPIO_PULLUP;
	  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
	  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	  GPIO_InitStruct.Pin = I2C_SDA_Pin;
	  GPIO_InitStruct.Mode = GPIO_MODE_AF_INPUT;

	  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);


    /* I2C1 clock enable */
    __HAL_RCC_I2C1_CLK_ENABLE();

    /* I2C1 interrupt Init */
    HAL_NVIC_SetPriority(I2C1_EV_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
    HAL_NVIC_SetPriority(I2C1_ER_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
  /* USER CODE BEGIN I2C1_MspInit 1 */

  /* USER CODE END I2C1_MspInit 1 */
  }
}

mue-barakat avatar Oct 31 '18 11:10 mue-barakat

please do not enable i2c interrupt and try again.

nimaltd avatar Nov 04 '18 07:11 nimaltd

and test all i2c address in a loop for get ack

nimaltd avatar Nov 04 '18 07:11 nimaltd

and the modes for SCL and SDA are right ? AF_OD and AF_Input ?

mue-barakat avatar Nov 04 '18 09:11 mue-barakat

its set automatically by cube

nimaltd avatar Nov 04 '18 09:11 nimaltd

Okie, current generated code

void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */

  /* USER CODE END I2C1_MspInit 0 */
  
    /**I2C1 GPIO Configuration    
    PB6     ------> I2C1_SCL
    PB7     ------> I2C1_SDA 
    */

    GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __HAL_RCC_I2C1_CLK_ENABLE();
  /* USER CODE BEGIN I2C1_MspInit 1 */

  /* USER CODE END I2C1_MspInit 1 */
  }

}
static void MX_I2C1_Init(void)
{

  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 100000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK) //always HAL_OK so no error here
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

my main

MX_GPIO_Init(); MX_I2C1_Init();

for(int i =0x00;i<0xFF;i++)
	{
		if(HAL_I2C_IsDeviceReady(&hi2c1,i,64,HAL_MAX_DELAY)==HAL_OK){
			while (1)
				{
					HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_2); toggle buzzer
					HAL_Delay(1000);
				}		
		}
	}

i never get HAL_OK in any round

mue-barakat avatar Nov 04 '18 09:11 mue-barakat

Its a big problem with f1 and cube.
Please ask on stm32 forum.

nimaltd avatar Nov 05 '18 20:11 nimaltd

thanks alot for your help, last question : shouldn't i enable the I2C interrupts ?

mue-barakat avatar Nov 07 '18 12:11 mue-barakat

I2c interrupt doesn't need to this library.

nimaltd avatar Nov 09 '18 11:11 nimaltd

hi i using stm32h743 when i want write data stm crashed and stuck, my i2c code is ::: hi2c1.Instance = I2C1; //hi2c1.Init.Timing = 100000; //hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; //hi2c1.Init.Timing = 0x10707DBC; hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.OwnAddress2 = 0; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

i use this library in stm32f4 very well but h7 not working

persian1910 avatar Feb 14 '21 08:02 persian1910

Hi First, thank you for your excellent libraries. I used this library for STM32G030 microcontroller and it worked perfectly. EEPROM was connected to I2C1 and for some reasons I have to use I2C2. Since I changed the I2C, without any other changes in the library and codes, the function isConnected and consequently isDeviceReady returns HAL_ERROR error. Your solution for STM32F1 microcontrollers didn't work either. Do you have any suggestions to fix this problem?

htan1375 avatar Apr 26 '23 09:04 htan1375

@htan1375 Hello. you are welcome. unfortunately, I have not tried the G series.

nimaltd avatar Apr 26 '23 09:04 nimaltd

@htan1375 I have the G0 serie but i dont have your exact problem. I use I2C1 and the EEPROM works good

PieterBosElectro avatar Aug 21 '23 14:08 PieterBosElectro

hi @htan1375 . only F1 series.

nimaltd avatar Aug 27 '23 07:08 nimaltd

Hi. Thanks to Mr. Askari and @PieterBosElectro. My problem solved and library works well. We had a very strange electronic circuit problem related to EMI noise affecting the I2C pins.

htan1375 avatar Aug 30 '23 08:08 htan1375