cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F03 I2C1 not working?

parth kothiya
Senior

i want to store data in to I2C EEPROM AT24C02 so in MCU I2C1 is used but i saw in oscilloscope I2C1 both pins are remains low

I2C2 works fine with i2c LCD.

i placed pull up resistor 10k both line CLK & SDA in EEPROM.

EEPROM address 0xA0

how to solve it?

eeprom.h file

#ifndef EEPROM_H_
#define EEPROM_H_
 
#include <stdint.h>
 
#define	EEPROM_ADDRESS 0xA0
uint8_t read_eeprom_reg(uint16_t MemAddress);
void write_eeprom_reg(uint16_t eep_mem_add, uint8_t value);
void write_eeprom_sequance(uint16_t mem_address,uint8_t *bytes,uint8_t nBytes);
void read_eeprom_sequance(uint16_t mem_address,uint8_t nBytes,uint8_t *buffer);
void write_u16t_eeprom(uint16_t data,uint16_t mem_address);
uint16_t read_u16t_eeprom(uint16_t mem_address);
 
#endif /* EEPROM_H_ */

eeprom.c file

/*
 * eeprom.c
 *
 *  Created on: Jul 16, 2020
 *      Author: Dell
 */
 
#include "eeprom.h"
#include <string.h>
#include "i2c.h"
 
extern I2C_HandleTypeDef hi2c1;
 
uint8_t read_eeprom_reg(uint16_t MemAddress)
{
	uint8_t data = 0;
	HAL_I2C_Mem_Read(&hi2c1,EEPROM_ADDRESS,MemAddress,2,&data,sizeof(data),10);
	return data;
}
 
void write_eeprom_reg(uint16_t eep_mem_add, uint8_t value)
{
	HAL_I2C_Mem_Write(&hi2c1,EEPROM_ADDRESS,eep_mem_add,2,&value,sizeof(value),10);
	HAL_Delay(10);
}
 
void write_eeprom_sequance(uint16_t mem_address,uint8_t *bytes,uint8_t nBytes){
	for (uint8_t i = 0; i < nBytes; ++i) {
		HAL_I2C_Mem_Write(&hi2c1,EEPROM_ADDRESS,(mem_address+i),0xFFFF,&bytes[i],1,10);
		HAL_Delay(10);
	}
}
 
void read_eeprom_sequance(uint16_t mem_address,uint8_t nBytes,uint8_t *buffer){
	for (uint8_t i = 0; i < nBytes; ++i) {
		HAL_I2C_Mem_Read(&hi2c1,EEPROM_ADDRESS,(mem_address+i),0xFFFF,&buffer[i],1,1);
	}
}
 
void write_u16t_eeprom(uint16_t data,uint16_t mem_address){
	uint8_t bytes[2];
	memcpy(&bytes,&data,2);
	write_eeprom_sequance(mem_address,bytes,2);
}
 
uint16_t read_u16t_eeprom(uint16_t mem_address){
	uint16_t u16_val = 0;
	uint8_t tbuffer[2] = {0,0};
	read_eeprom_sequance(mem_address,2,tbuffer);
	memcpy(&u16_val,&tbuffer,2);
	return u16_val;
}

0693W00000Hq4jlQAB.bmpI2C.C

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    i2c.c
  * @brief   This file provides code for the configuration
  *          of the I2C instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "i2c.h"
 
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
I2C_HandleTypeDef hi2c1;
I2C_HandleTypeDef hi2c2;
 
/* I2C1 init function */
void MX_I2C1_Init(void)
{
 
  /* USER CODE BEGIN I2C1_Init 0 */
 
  /* USER CODE END I2C1_Init 0 */
 
  /* USER CODE BEGIN I2C1_Init 1 */
 
  /* USER CODE END I2C1_Init 1 */
  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x0000020B;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Analogue filter
  */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Digital filter
  */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C1_Init 2 */
 
  /* USER CODE END I2C1_Init 2 */
 
}
/* I2C2 init function */
void MX_I2C2_Init(void)
{
 
  /* USER CODE BEGIN I2C2_Init 0 */
 
  /* USER CODE END I2C2_Init 0 */
 
  /* USER CODE BEGIN I2C2_Init 1 */
 
  /* USER CODE END I2C2_Init 1 */
  hi2c2.Instance = I2C2;
  hi2c2.Init.Timing = 0x2000090E;
  hi2c2.Init.OwnAddress1 = 0;
  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c2.Init.OwnAddress2 = 0;
  hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Analogue filter
  */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Digital filter
  */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C2_Init 2 */
 
  /* USER CODE END I2C2_Init 2 */
 
}
 
void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{
 
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(i2cHandle->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */
 
  /* USER CODE END I2C1_MspInit 0 */
 
    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**I2C1 GPIO Configuration
    PB8     ------> I2C1_SCL
    PB9     ------> I2C1_SDA
    */
    GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
    /* I2C1 clock enable */
    __HAL_RCC_I2C1_CLK_ENABLE();
  /* USER CODE BEGIN I2C1_MspInit 1 */
 
  /* USER CODE END I2C1_MspInit 1 */
  }
  else if(i2cHandle->Instance==I2C2)
  {
  /* USER CODE BEGIN I2C2_MspInit 0 */
 
  /* USER CODE END I2C2_MspInit 0 */
 
    __HAL_RCC_GPIOF_CLK_ENABLE();
    /**I2C2 GPIO Configuration
    PF6     ------> I2C2_SCL
    PF7     ------> I2C2_SDA
    */
    GPIO_InitStruct.Pin = LCD_SCL_Pin|LCD_SDA_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
 
    /* I2C2 clock enable */
    __HAL_RCC_I2C2_CLK_ENABLE();
  /* USER CODE BEGIN I2C2_MspInit 1 */
 
  /* USER CODE END I2C2_MspInit 1 */
  }
}
 
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
{
 
  if(i2cHandle->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspDeInit 0 */
 
  /* USER CODE END I2C1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_I2C1_CLK_DISABLE();
 
    /**I2C1 GPIO Configuration
    PB8     ------> I2C1_SCL
    PB9     ------> I2C1_SDA
    */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8);
 
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_9);
 
  /* USER CODE BEGIN I2C1_MspDeInit 1 */
 
  /* USER CODE END I2C1_MspDeInit 1 */
  }
  else if(i2cHandle->Instance==I2C2)
  {
  /* USER CODE BEGIN I2C2_MspDeInit 0 */
 
  /* USER CODE END I2C2_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_I2C2_CLK_DISABLE();
 
    /**I2C2 GPIO Configuration
    PF6     ------> I2C2_SCL
    PF7     ------> I2C2_SDA
    */
    HAL_GPIO_DeInit(LCD_SCL_GPIO_Port, LCD_SCL_Pin);
 
    HAL_GPIO_DeInit(LCD_SDA_GPIO_Port, LCD_SDA_Pin);
 
  /* USER CODE BEGIN I2C2_MspDeInit 1 */
 
  /* USER CODE END I2C2_MspDeInit 1 */
  }
}
 
/* USER CODE BEGIN 1 */
 
/* USER CODE END 1 */

3 REPLIES 3
TDK
Guru

You don't show your usage of these functions anywhere. Include the entire code if possible.

Your I2C2 initialization is missing a setting to GPIO_InitStruct.Alternate.

The very first thing you should do to debug I2C is to see if HAL_I2C_IsDeviceReady returns HAL_OK. If not, there is a connectivity issue.

If SDA/SCL are both low despite the presence of external pullups, it suggests the pins are not initialized correctly, or there is an issue with your measurement, or there is some other hardware issue.

If you feel a post has answered your question, please click "Accept as Solution".

My problem with I2C1 EEPROM not working in I2C1 I'm using alternate pins and in cods line number 132 alternate function it's configured.

1) PB8 PB9 configure as I2C1 then I get EEPROM CONNECTED

as you said i tried I2C1 HAL_I2C_IsDeviceReady is returns HAL_ERROR is there hardware EEPROM problem or any other issue?

 

2) PB6 PB7 configured as I2C1 then its work fine i get clock in oscilloscope FOR TRIAL

 

so what is the problem in gpio init code how to solve it?

both cases 1&2 GPIOB RESISTOR VALUE image is given.0693W00000Hq8OhQAJ.bmp

0693W00000Hq8NpQAJ.bmp