/**
  * @brief  Clears the I2Cx's pending flags.
  * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  * @param  I2C_FLAG: specifies the flag to clear.
  *   This parameter can be any combination of the following values:
  *     @arg I2C_FLAG_SMBALERT: SMBus Alert flag
  *     @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
  *     @arg I2C_FLAG_PECERR: PEC error in reception flag
  *     @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
  *     @arg I2C_FLAG_AF: Acknowledge failure flag
  *     @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
  *     @arg I2C_FLAG_BERR: Bus error flag
  *
  * @note
  *   - STOPF (STOP detection) is cleared by software sequence: a read operation
  *     to I2C_SR1 register (I2C_GetFlagStatus()) followed by a write operation
  *     to I2C_CR1 register (I2C_Cmd() to re-enable the I2C peripheral).
  *   - ADD10 (10-bit header sent) is cleared by software sequence: a read
  *     operation to I2C_SR1 (I2C_GetFlagStatus()) followed by writing the
  *     second byte of the address in DR register.
  *   - BTF (Byte Transfer Finished) is cleared by software sequence: a read
  *     operation to I2C_SR1 register (I2C_GetFlagStatus()) followed by a
  *     read/write to I2C_DR register (I2C_SendData()).
  *   - ADDR (Address sent) is cleared by software sequence: a read operation to
  *     I2C_SR1 register (I2C_GetFlagStatus()) followed by a read operation to
  *     I2C_SR2 register ((void)(I2Cx->SR2)).
  *   - SB (Start Bit) is cleared software sequence: a read operation to I2C_SR1
  *     register (I2C_GetFlagStatus()) followed by a write operation to I2C_DR
  *     register  (I2C_SendData()).
  * @retval None
  */
void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG) {
    uint32_t flagpos = 0;
    /* Check the parameters */
    assert_param(IS_I2C_ALL_PERIPH(I2Cx));
    assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
    /* Get the I2C flag position */
    flagpos = I2C_FLAG & FLAG_Mask;
    /* Clear the selected I2C flag */
    I2Cx->SR1 = (uint16_t)~flagpos;
}
/*******************************************************************************
* Function Name  : I2C_ClearFlag
* Description    : Clears the I2Cx's pending flags.
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
*                  - I2C_FLAG: specifies the flag to clear. 
*                    This parameter can be one of the following values:
*                       - I2C_FLAG_SMBALERT: SMBus Alert flag
*                       - I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
*                       - I2C_FLAG_PECERR: PEC error in reception flag
*                       - I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
*                       - I2C_FLAG_AF: Acknowledge failure flag
*                       - I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
*                       - I2C_FLAG_BERR: Bus error flag
*                       - I2C_FLAG_STOPF: Stop detection flag (Slave mode)
*                       - I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)
*                       - I2C_FLAG_BTF: Byte transfer finished flag
*                       - I2C_FLAG_ADDR: Address sent flag (Master mode) “ADSL”
*                                        Address matched flag (Slave mode)”ENDAD”
*                       - I2C_FLAG_SB: Start bit flag (Master mode)
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_ClearFlag(I2C_TypeDef* I2Cx, u32 I2C_FLAG)
{
  u32 flagpos = 0;
  u32 flagindex = 0;

  /* Check the parameters */
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));

  /* Get the I2C flag position */
  flagpos = I2C_FLAG & FLAG_Mask;

  /* Get the I2C flag index */
  flagindex = I2C_FLAG >> 28;

  /* Clear the flag by writing 0 */
  if (flagindex == 1)
  {
    /* Clear the selected I2C flag */
    I2Cx->SR1 = (u16)~flagpos;
  }
  /* Flags that need a read of the SR1 register to be cleared */
  else if (flagindex == 2)
  {
    /* Read the SR1 register */
    (void)I2Cx->SR1;
  }
  /* Flags that need a read of SR1 and a write on CR1 registers to be cleared */
  else if (flagindex == 6)
  {
    /* Read the SR1 register */
    (void)I2Cx->SR1;

    /* Write on the CR1 register */
    I2Cx->CR1 |= CR1_PE_Set;
  }
  /* Flags that need a read of SR1 and SR2 registers to be cleared */
  else /*flagindex == 0xA*/
  {
    /* Read the SR1 register */
    (void)I2Cx->SR1;

    /* Read the SR2 register */
    (void)I2Cx->SR2;
  }
}