title: STM32 - I2C Write Failure
body: <p>I'm using STM32 with EEPROM 512KB, i inilized the project using STM32CubeMX</p>
  
  <blockquote>
    <p>PB7 as I2C_SDA PB6 as I2C_SCL</p>
  </blockquote>
  
  <p>Generated Functions</p>
  
  <pre><code>I2C_HandleTypeDef hi2c1;
  
  /* I2C1 init function */
  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(&amp;hi2c1) != HAL_OK)
    {
      _Error_Handler(__FILE__, __LINE__);
    }
  
  }
  
  void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
  {
  
    GPIO_InitTypeDef GPIO_InitStruct;
    if(i2cHandle-&gt;Instance==I2C1)
    {
    /* USER CODE BEGIN I2C1_MspInit 0 */
  
    /* USER CODE END I2C1_MspInit 0 */
  
      /**I2C1 GPIO Configuration    
      PB6     ------&gt; I2C1_SCL
      PB7     ------&gt; 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, &amp;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 */
    }
  }
  
  void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
  {
  
    if(i2cHandle-&gt;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    
      PB6     ------&gt; I2C1_SCL
      PB7     ------&gt; I2C1_SDA 
      */
      HAL_GPIO_DeInit(GPIOB, I2C_SCL_Pin|I2C_SDA_Pin);
  
      /* I2C1 interrupt Deinit */
      HAL_NVIC_DisableIRQ(I2C1_EV_IRQn);
      HAL_NVIC_DisableIRQ(I2C1_ER_IRQn);
    /* USER CODE BEGIN I2C1_MspDeInit 1 */
  
    /* USER CODE END I2C1_MspDeInit 1 */
    }
  } 
  </code></pre>
  
  <p>In main i call <code>MX_I2C1_Init()</code> then <code>HAL_I2C_MspInit(&amp;hi2c1)</code> , but when i call </p>
  
  <pre><code>while(HAL_I2C_Mem_Write(hi2c,(uint16_t)DevAddress,(uint16_t)MemAddress,I2C_MEMADD_SIZE_8BIT,pData,(uint16_t)16-MemAddress,1000)!= HAL_OK &amp;&amp; 1);
  </code></pre>
  
  <p>This while loop never returns HAL_OK, so i can not processed to read the written data to verify.
  My EEPROM is </p>
  
  <blockquote>
    <p>CAT24C512WI-GT3OSCT-ND
    and the A0->A2, WP are connected to ground</p>
  </blockquote>