EEPROM read and write the STM32 I2C- study notes the I2C library functions

stm32f10x_i2c.h

typedef struct
{
  uint32_t I2C_ClockSpeed;          /*!< Specifies the clock frequency.传输速率
                                         This parameter must be set to a value lower than 400kHz */

  uint16_t I2C_Mode;                /*!< Specifies the I2C mode.设置工作模式I2C/SMBUS
                                         This parameter can be a value of @ref I2C_mode */

  uint16_t I2C_DutyCycle;           /*!< Specifies the I2C fast mode duty cycle.占空比
                                         This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */

  uint16_t I2C_OwnAddress1;         /*!< Specifies the first device own address.自身I2C设备地址
                                         This parameter can be a 7-bit or 10-bit address. */

  uint16_t I2C_Ack;                 /*!< Enables or disables the acknowledgement.使能或关闭响应
                                         This parameter can be a value of @ref I2C_acknowledgement */

  uint16_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.指定地址长度
                                         This parameter can be a value of @ref I2C_acknowledged_address */
}I2C_InitTypeDef;

• I2C_ClockSpeed
set the transmission rate of the I2C, when calling initialization function, function after the operation is written to the clock factor I2C clock control register CCR according to the value of our inputs. The value of this parameter we have written shall not exceed 400KHz.
In fact, a little slower than the time, will not cause other effects in addition to the I2C standard communication since communication can not be written in the CCR type of clock decimal factor SCL affect the actual frequency may be lower than the parameter value of the present set membership, .
• I2C_Mode
select I2C mode of use, there is I2C mode (I2C_Mode_I2C) and SMBus master and slave mode (I2C_Mode_SMBusHost, I2C_Mode_SMBusDevice).
I2C is no need here to distinguish from the master mode, can be set directly I2C_Mode_I2C.
• I2C_DutyCycle
set the duty cycle of I2C SCL line clock. This configuration has two options, namely low time ratio is high time 2: 1 (I2C_DutyCycle_2) and 16: 9 (I2C_DutyCycle_16_9).
In fact, the proportion of difference between the two modes is not large, the general requirements are not so strict, where you can pick up.
• I2C_OwnAddress1
configuration STM32 I2C device of its own address, each device connected to the I2C bus must have its own address, as the host is no exception. Address can be set to 7 or 10 (determined by I2C_AcknowledgeAddress members below), as long as the address is unique to the I2C bus.
STM32 I2C peripherals can be used simultaneously to two addresses, i.e., simultaneously in response to two addresses, a member of this configuration is the default configuration I2C_OwnAddress1, OAR1 register stored address, a second address if necessary OAR2 registers, may be used I2C_OwnAddress2Config function to configure, OAR2 does not support 10-bit addressing.
• I2C_Ack_Enable
configure I2C answer is enabled, you can set to enable transmits a response signal. Generally configured to allow response (I2C_Ack_Enable), which is the vast majority follow standard I2C communication equipment requirements, instead ban response (I2C_Ack_Disable) often lead to communication errors.
• I2C_AcknowledgeAddress
select I2C addressing mode is 7 or 10-bit address. This requires the address on the I2C bus is connected to the actual selection device, this configuration also affects members I2C_OwnAddress1 member, only when set to 10-bit mode, 10-bit address I2C_OwnAddress1 is supported here.

After configuring these structures members values, calling the library function I2C_Init to the structure written into the configuration register.

Other library functions:

void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
//产生一个起始条件
{
  /* Check the parameters */
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  if (NewState != DISABLE)
  {
    /* Generate a START condition */
    I2Cx->CR1 |= CR1_START_Set;
  }
  else
  {
    /* Disable the START condition generation */
    I2Cx->CR1 &= CR1_START_Reset;
  }
}
FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
//检查flag位,检查I2C工作状态
{
  FlagStatus bitstatus = RESET;
  __IO uint32_t i2creg = 0, i2cxbase = 0;

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

  /* Get the I2Cx peripheral base address */
  i2cxbase = (uint32_t)I2Cx;
  
  /* Read flag register index */
  i2creg = I2C_FLAG >> 28;
  
  /* Get bit[23:0] of the flag */
  I2C_FLAG &= FLAG_Mask;
  
  if(i2creg != 0)
  {
    /* Get the I2Cx SR1 register address */
    i2cxbase += 0x14;
  }
  else
  {
    /* Flag in I2Cx SR2 Register */
    I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);
    /* Get the I2Cx SR2 register address */
    i2cxbase += 0x18;
  }
  
  if(((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)
  {
    /* I2C_FLAG is set */
    bitstatus = SET;
  }
  else
  {
    /* I2C_FLAG is reset */
    bitstatus = RESET;
  }
  
  /* Return the I2C_FLAG status */
  return  bitstatus;
}
void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)
//发送地址7位
{
  /* Check the parameters */
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  assert_param(IS_I2C_DIRECTION(I2C_Direction));
  /* Test on the direction to set/reset the read/write bit */
  if (I2C_Direction != I2C_Direction_Transmitter)
  {
    /* Set the address bit0 for read */
    Address |= OAR1_ADD0_Set;
  }
  else
  {
    /* Reset the address bit0 for write */
    Address &= OAR1_ADD0_Reset;
  }
  /* Send the address */
  I2Cx->DR = Address;
}
void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
//发送数据
{
  /* Check the parameters */
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  /* Write in the DR register the data to be sent */
  I2Cx->DR = Data;
}

Guess you like

Origin blog.csdn.net/weixin_37676403/article/details/91444374