Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for init #1

Closed
mue-barakat opened this issue Oct 25, 2018 · 25 comments
Closed

Example for init #1

mue-barakat opened this issue Oct 25, 2018 · 25 comments

Comments

@mue-barakat
Copy link

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

@mue-barakat
Copy link
Author

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

@mue-barakat
Copy link
Author

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

@nimaltd
Copy link
Owner

nimaltd commented Oct 25, 2018

Hello. Do you use stm32f1 series,?

@mue-barakat
Copy link
Author

yes stm32F103R8Tx

@nimaltd
Copy link
Owner

nimaltd commented Oct 25, 2018

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

@mue-barakat
Copy link
Author

What do you mean another I2C ?

@mue-barakat
Copy link
Author

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

@mue-barakat
Copy link
Author

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

@nimaltd
Copy link
Owner

nimaltd commented Oct 26, 2018

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

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

@mue-barakat
Copy link
Author

mue-barakat commented Oct 30, 2018

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
Copy link
Author

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

@nimaltd
Copy link
Owner

nimaltd commented Nov 4, 2018

please do not enable i2c interrupt and try again.

@nimaltd
Copy link
Owner

nimaltd commented Nov 4, 2018

and test all i2c address in a loop for get ack

@mue-barakat
Copy link
Author

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

@nimaltd
Copy link
Owner

nimaltd commented Nov 4, 2018

its set automatically by cube

@mue-barakat
Copy link
Author

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

@nimaltd
Copy link
Owner

nimaltd commented Nov 5, 2018

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

@mue-barakat
Copy link
Author

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

@nimaltd
Copy link
Owner

nimaltd commented Nov 9, 2018

I2c interrupt doesn't need to this library.

@persian1910
Copy link

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

@htan1375
Copy link

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?

@nimaltd
Copy link
Owner

nimaltd commented Apr 26, 2023

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

@PieterBosElectro
Copy link

PieterBosElectro commented Aug 21, 2023

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

@nimaltd
Copy link
Owner

nimaltd commented Aug 27, 2023

hi @htan1375 . only F1 series.

@htan1375
Copy link

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.

@nimaltd nimaltd closed this as completed Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants