cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103R8Tx with STM32CubeMX Hal malfunction with i2c

JRuda
Associate

I'm using 24C512 EEPROM with my STM32F1, i initialized the project with stm32cubemx and defined my SCL and SDA pins as I2C, when i try isDeviceReady function i never get HAL_OK.

I've also grounded A0,A1,A2,Wp of the eeprom

My Code

int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration----------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_I2C2_Init();
  /* USER CODE BEGIN 2 */
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
 
 
		while(HAL_I2C_IsDeviceReady(&hi2c1,0xa0,64,HAL_MAX_DELAY)!=HAL_OK){};
////Code never gets here 
			while (1)
				{
					HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_2);
					HAL_Delay(1000);
				}
 
 
 
 
 
 
  /* USER CODE END WHILE */
 
  /* USER CODE BEGIN 3 */
 
  }

The STM32CUBEMX generated library

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)
 
 {
 
  _Error_Handler(__FILE__, __LINE__);
 
 }
 
 
 
}
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 */
    __HAL_RCC_GPIOB_CLK_ENABLE();
  /* USER CODE END I2C1_MspInit 1 */
  }
}

My digikey module link

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

any idea what goes wrong ? also i try the same approach on a STM32F0, the eeprom works

0 REPLIES 0