Closed mue-barakat closed 5 months ago
i've also connected A0,A1,A2 to GND with WP too, i'm using CAT24C512WI
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
Hello. Do you use stm32f1 series,?
yes stm32F103R8Tx
I have the same problem with f1 Please check with another i2c
What do you mean another I2C ?
i can't use the other I2C output as it's already soldered in my PCB
I have 2 demo PCBs both aren't working, every single thing in the processor works except for I2C
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(); }
//////////////////////
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 */
}
}
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 */
}
}
please do not enable i2c interrupt and try again.
and test all i2c address in a loop for get ack
and the modes for SCL and SDA are right ? AF_OD and AF_Input ?
its set automatically by cube
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
Its a big problem with f1 and cube.
Please ask on stm32 forum.
thanks alot for your help, last question : shouldn't i enable the I2C interrupts ?
I2c interrupt doesn't need to this library.
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
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 Hello. you are welcome. unfortunately, I have not tried the G series.
@htan1375 I have the G0 serie but i dont have your exact problem. I use I2C1 and the EEPROM works good
hi @htan1375 . only F1 series.
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.
can you provide example for init of the EEPROM pins, as no clear documentation for that?