⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stm32f10x_i2c.c

📁 STM32F10xx的LCD驱动源码
💻 C
📖 第 1 页 / 共 3 页
字号:
  if (NewState != DISABLE)
  {
    /* Enable the selected I2C ARP */
    I2Cx->CR1 |= CR1_ENARP_Set;
  }
  else
  {
    /* Disable the selected I2C ARP */
    I2Cx->CR1 &= CR1_ENARP_Reset;
  }
}

/*******************************************************************************
* Function Name  : I2C_StretchClockCmd
* Description    : Enables or disables the specified I2C Clock stretching.
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
*                  - NewState: new state of the I2Cx Clock stretching.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
  /* Check the parameters */
  assert(IS_FUNCTIONAL_STATE(NewState));

  if (NewState == DISABLE)
  {
    /* Enable the selected I2C Clock stretching */
    I2Cx->CR1 |= CR1_NOSTRETCH_Set;
  }
  else
  {
    /* Disable the selected I2C Clock stretching */
    I2Cx->CR1 &= CR1_NOSTRETCH_Reset;
  }
}

/*******************************************************************************
* Function Name  : I2C_FastModeDutyCycleConfig
* Description    : Selects the specified I2C fast mode duty cycle.
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
*                  - I2C_DutyCycle: specifies the fast mode duty cycle.
*                    This parameter can be one of the following values:
*                       - I2C_DutyCycle_2: I2C fast mode Tlow/Thigh = 2
*                       - I2C_DutyCycle_16_9: I2C fast mode Tlow/Thigh = 16/9
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, u16 I2C_DutyCycle)
{
  /* Check the parameters */
  assert(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));

  if (I2C_DutyCycle != I2C_DutyCycle_16_9)
  {
    /* I2C fast mode Tlow/Thigh=2 */
    I2Cx->CCR &= I2C_DutyCycle_2;
  }
  else
  {
    /* I2C fast mode Tlow/Thigh=16/9 */
    I2Cx->CCR |= I2C_DutyCycle_16_9;
  }
}

/*******************************************************************************
* Function Name  : I2C_GetLastEvent
* Description    : Returns the last I2Cx Event.
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* Output         : None
* Return         : The last event
*******************************************************************************/
u32 I2C_GetLastEvent(I2C_TypeDef* I2Cx)
{
  u32 LastEvent = 0;
  u32 Flag1 = 0, Flag2 = 0;

  Flag1 = I2Cx->SR1;
  Flag2 = I2Cx->SR2;
  Flag2 = Flag2 << 16;

  /* Get the last event value from I2C status register */
  LastEvent = (Flag1 | Flag2) & I2C_FLAG_Mask;

  /* Return status */
  return LastEvent;
}

/*******************************************************************************
* Function Name  : I2C_CheckEvent
* Description    : Checks whether the last I2Cx Event is equal to the one passed
*                  as parameter.
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
*                  - I2C_EVENT: specifies the event to be checked. 
*                    This parameter can be one of the following values:
*                       - I2C_EVENT_SLAVE_ADDRESS_MATCHED   : EV1
*                       - I2C_EVENT_SLAVE_BYTE_RECEIVED     : EV2
*                       - I2C_EVENT_SLAVE_BYTE_TRANSMITTED  : EV3
*                       - I2C_EVENT_SLAVE_ACK_FAILURE       : EV3-1
*                       - I2C_EVENT_MASTER_MODE_SELECT      : EV5
*                       - I2C_EVENT_MASTER_MODE_SELECTED    : EV6
*                       - I2C_EVENT_MASTER_BYTE_RECEIVED    : EV7
*                       - I2C_EVENT_MASTER_BYTE_TRANSMITTED : EV8
*                       - I2C_EVENT_MASTER_MODE_ADDRESS10   : EV9
*                       - I2C_EVENT_SLAVE_STOP_DETECTED     : EV4
* Output         : None
* Return         : An ErrorStatus enumuration value:
*                       - SUCCESS: Last event is equal to the I2C_Event
*                       - ERROR: Last event is different from the I2C_Event
*******************************************************************************/
ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, u32 I2C_EVENT)
{
  u32 LastEvent = 0;
  u32 Flag1 = 0, Flag2 = 0;
  ErrorStatus status = ERROR;

  /* Check the parameters */
  assert(IS_I2C_EVENT(I2C_EVENT));

  Flag1 = I2Cx->SR1;
  Flag2 = I2Cx->SR2;
  Flag2 = Flag2 << 16;

  /* Get the last event value from I2C status register */
  LastEvent = (Flag1 | Flag2) & I2C_FLAG_Mask;

  /* Check whether the last event is equal to I2C_EVENT */
  if (LastEvent == I2C_EVENT )
  {
    /* SUCCESS: last event is equal to I2C_EVENT */
    status = SUCCESS;
  }
  else
  {
    /* ERROR: last event is different from I2C_EVENT */
    status = ERROR;
  }

  /* Return status */
  return status;
}

/*******************************************************************************
* Function Name  : I2C_GetFlagStatus
* Description    : Checks whether the specified I2C flag is set or not.
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
*                  - I2C_FLAG: specifies the flag to check. 
*                    This parameter can be one of the following values:
*                       - I2C_FLAG_DUALF: Dual flag (Slave mode)
*                       - I2C_FLAG_SMBHOST: SMBus host header (Slave mode)
*                       - I2C_FLAG_SMBDEFAULT: SMBus default header (Slave mode)
*                       - I2C_FLAG_GENCALL: General call header flag (Slave mode)
*                       - I2C_FLAG_TRA: Transmitter/Receiver flag
*                       - I2C_FLAG_BUSY: Bus busy flag
*                       - I2C_FLAG_MSL: Master/Slave flag
*                       - 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_TXE: Data register empty flag (Transmitter)
*                       - I2C_FLAG_RXNE: Data register not empty (Receiver) 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) 揂DSL?*                                        Address matched flag (Slave mode)擡NDAD?*                       - I2C_FLAG_SB: Start bit flag (Master mode)
* Output         : None
* Return         : The new state of I2C_FLAG (SET or RESET).
*******************************************************************************/
FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, u32 I2C_FLAG)
{
  FlagStatus bitstatus = RESET;
  u32 i2cstatus = 0;
  u32 Flag1 = 0, Flag2 = 0;

  /* Check the parameters */
  assert(IS_I2C_GET_FLAG(I2C_FLAG));

  /* Read the I2Cx status register */
  Flag1 = I2Cx->SR1;
  Flag2 = I2Cx->SR2;
  Flag2 = (Flag2 & I2C_FLAG_Mask) << 16;

  /* Get the I2C status value */
  i2cstatus = Flag1 | Flag2;

  /* Get bit[27:0] of the flag */
  I2C_FLAG &= I2C_FLAG_Mask;

  /* Check the status of the specified I2C flag */
  if ((i2cstatus & I2C_FLAG) != (u32)RESET)
  {
    /* I2C_FLAG is set */
    bitstatus = SET;
  }
  else
  {
    /* I2C_FLAG is reset */
    bitstatus = RESET;
  }
  /* Return the I2C_FLAG status */
  return  bitstatus;
}

/*******************************************************************************
* 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) 揂DSL?*                                        Address matched flag (Slave mode)擡NDAD?*                       - I2C_FLAG_SB: Start bit flag (Master mode)
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_ClearFlag(I2C_TypeDef* I2Cx, u32 I2C_FLAG)
{
  u32 flagpos = 0;
  u8 flagindex = 0;

  /* Check the parameters */
  assert(IS_I2C_CLEAR_FLAG(I2C_FLAG));

  /* Get the I2C flag position */
  flagpos = I2C_FLAG & I2C_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 &= ~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 CR2 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 a write on CR2 registers to be cleared */
  else /*flagindex == 0xA*/
  {
    /* Read the SR1 register */
    (void)I2Cx->SR1;

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

/*******************************************************************************
* Function Name  : I2C_GetITStatus
* Description    : Checks whether the specified I2C interrupt has occurred or not.
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
*                  - I2C_IT: specifies the interrupt source to check. 
*                    This parameter can be one of the following values:
*                       - I2C_IT_SMBALERT: SMBus Alert flag
*                       - I2C_IT_TIMEOUT: Timeout or Tlow error flag
*                       - I2C_IT_PECERR: PEC error in reception flag
*                       - I2C_IT_OVR: Overrun/Underrun flag (Slave mode)
*                       - I2C_IT_AF: Acknowledge failure flag
*                       - I2C_IT_ARLO: Arbitration lost flag (Master mode)
*                       - I2C_IT_BERR: Bus error flag
*                       - I2C_IT_TXE: Data register empty flag (Transmitter)
*                       - I2C_IT_RXNE: Data register not empty (Receiver) flag
*                       - I2C_IT_STOPF: Stop detection flag (Slave mode)
*                       - I2C_IT_ADD10: 10-bit header sent flag (Master mode)
*                       - I2C_IT_BTF: Byte transfer finished flag
*                       - I2C_IT_ADDR: Address sent flag (Master mode) 揂DSL?*                                        Address matched flag (Slave mode)擡NDAD?*                       - I2C_IT_SB: Start bit flag (Master mode)
* Output         : None
* Return         : The new state of I2C_IT (SET or RESET).
*******************************************************************************/
ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, u32 I2C_IT)
{
  ITStatus bitstatus = RESET;
  u32 i2cstatus = 0;
  u32 Flag1 = 0, Flag2 = 0;

  /* Check the parameters */
  assert(IS_I2C_GET_IT(I2C_IT));

  /* Read the I2Cx status register */
  Flag1 = I2Cx->SR1;
  Flag2 = I2Cx->SR2;
  Flag2 = (Flag2 & I2C_FLAG_Mask) << 16;

  /* Get the I2C status value */
  i2cstatus = Flag1 | Flag2;

  /* Get bit[27:0] of the flag */
  I2C_IT &= I2C_FLAG_Mask;

  /* Check the status of the specified I2C flag */
  if ((i2cstatus & I2C_IT) != (u32)RESET)
  {
    /* I2C_IT is set */
    bitstatus = SET;
  }
  else
  {
    /* I2C_IT is reset */
    bitstatus = RESET;
  }
  /* Return the I2C_IT status */
  return  bitstatus;
}

/*******************************************************************************
* Function Name  : I2C_ClearITPendingBit
* Description    : Clears the I2Cx抯 interrupt pending bits.
* Input          : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
*                  - I2C_IT: specifies the interrupt pending to clear. 
*                    This parameter can be one of the following values:
*                       - I2C_IT_SMBALERT: SMBus Alert flag
*                       - I2C_IT_TIMEOUT: Timeout or Tlow error flag
*                       - I2C_IT_PECERR: PEC error in reception flag
*                       - I2C_IT_OVR: Overrun/Underrun flag (Slave mode)
*                       - I2C_IT_AF: Acknowledge failure flag
*                       - I2C_IT_ARLO: Arbitration lost flag (Master mode)
*                       - I2C_IT_BERR: Bus error flag
*                       - I2C_IT_STOPF: Stop detection flag (Slave mode)
*                       - I2C_IT_ADD10: 10-bit header sent flag (Master mode)
*                       - I2C_IT_BTF: Byte transfer finished flag
*                       - I2C_IT_ADDR: Address sent flag (Master mode) 揂DSL?*                                        Address matched flag (Slave mode)擡NDAD?*                       - I2C_IT_SB: Start bit flag (Master mode)
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, u32 I2C_IT)
{
  u32 flagpos = 0;
  u8 flagindex = 0;

  /* Check the parameters */
  assert(IS_I2C_CLEAR_IT(I2C_IT));

  /* Get the I2C flag position */
  flagpos = I2C_IT & I2C_FLAG_Mask;

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

  /* Clear the flag by writing 0 */
  if (flagindex == 1)
  {
    /* Clear the selected I2C flag */
    I2Cx->SR1 &= ~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 CR2 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 a write on CR2 registers to be cleared */
  else /*flagindex == 0xA*/
  {
    /* Read the SR1 register */
    (void)I2Cx->SR1;

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

/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -