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

📄 stm8s_i2c.c

📁 STM8s
💻 C
📖 第 1 页 / 共 2 页
字号:
}

/**
  * @brief Selects the specified I2C fast mode duty cycle.
  * @param[in] DutyCycle : Specifies the duty cycle to apply.
  * This parameter can be any of the @ref I2C_DutyCycle_TypeDef enumeration.
  * @retval None
  */
void I2C_FastModeDutyCycleConfig(I2C_DutyCycle_TypeDef DutyCycle)
{

  /* Check function parameters */
  assert_param(IS_I2C_DUTYCYCLE_OK(DutyCycle));

  if (DutyCycle == I2C_DUTYCYCLE_16_9)
  {
    /* I2C fast mode Tlow/Thigh = 16/9 */
    I2C->CCRH |= I2C_CCRH_DUTY;
  }
  else /* I2C_DUTYCYCLE_2 */
  {
    /* I2C fast mode Tlow/Thigh = 2 */
    I2C->CCRH &= (u8)(~I2C_CCRH_DUTY);
  }
}

/**
  * @brief Checks whether the last I2C event is equal to the one passed as parameter.
  * @param[in] I2C_Event : Specifies the event to be checked.
  * This parameter can be any of the  @ref I2C_Event_TypeDef enumeration.
  * @retval ErrorStatus : Status of the event
  * SUCCESS : last event is equal to the I2C_Event
  * ERROR : last event is different from the I2C_Event
  * @par Required preconditions:
  * This function must be called only once the flags can be reset by reading the registers.
  */
ErrorStatus I2C_CheckEvent(I2C_Event_TypeDef I2C_Event)
{

  u8 flag1 = 0;
  u8 flag2 = 0;
  ErrorStatus status = ERROR;

  /* Check the parameters */
  assert_param(IS_I2C_EVENT_OK(I2C_Event));

  flag1 = I2C->SR1;
  flag2 = I2C->SR2;

  /* Check which SRx register must be read */
  if (((u16)I2C_Event & (u16)0x0F00) == 0x0700)
  {
    /* Check whether the last event is equal to I2C_EVENT */
    if (flag1 & (u8)I2C_Event)
    {
      /* SUCCESS: last event is equal to I2C_EVENT */
      status = SUCCESS;
    }
    else
    {
      /* ERROR: last event is different from I2C_EVENT */
      status = ERROR;
    }
  }
  else /* Returns whether the status register to check is SR2 */
  {
    if (flag2 & (u8)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;

}

/**
  * @brief Returns the most recent received data.
  * @par Parameters:
  * None
  * @retval u8 : The value of the received byte data.
  */
u8 I2C_ReceiveData(void)
{
  /* Return the data present in the DR register */
  return ((u8)I2C->DR);
}

/**
  * @brief Transmits the 7-bit address (to select the) slave device.
  * @param[in] Address : Specifies the slave address which will be transmitted.
  * @param[in] Direction : Specifies whether the I2C device will be a Transmitter or a Receiver.
  * This parameter can be any of the @ref I2C_Direction_TypeDef enumeration.
  * @retval None
  */
void I2C_Send7bitAddress(u8 Address, I2C_Direction_TypeDef Direction)
{
  /* Check function parameters */
  assert_param(IS_I2C_ADDRESS_OK(Address));
  assert_param(IS_I2C_DIRECTION_OK(Direction));

  /* Clear bit0 (direction) just in case */
  Address &= (u8)0xFE;

  /* Send the Address + Direction */
  I2C->DR = (u8)(Address | (u8)Direction);
}

/**
  * @brief Send a byte by writing in the DR register.
  * @param[in] Data : Byte to be sent.
  * @retval None
  */
void I2C_SendData(u8 Data)
{
  /* Write in the DR register the data to be sent */
  I2C->DR = Data;
}

/**
  * @brief Returns the specified I2C flag status
  * @param[in] Flag : Specifies the flag to read
  * This parameter can be any of the  @ref I2C_Flag_TypeDef enumeration.
  * @retval FlagStatus : State of Flag.
  * This parameter can be any of the @ref FlagStatus enumeration.
  */
FlagStatus I2C_GetFlagStatus(I2C_Flag_TypeDef Flag)
{

  FlagStatus bitstatus = RESET;

  /* Check the parameters */
  assert_param(IS_I2C_FLAG_OK(Flag));

  /* Check SRx index */
  switch ((u16)Flag & (u16)0xF000)
  {

      /* Returns whether the status register to check is SR1 */
    case 0x1000:
      /* Check the status of the specified I2C flag */
      if ((I2C->SR1 & (u8)Flag) != 0)
      {
        /* Flag is set */
        bitstatus = SET;
      }
      else
      {
        /* Flag is reset */
        bitstatus = RESET;
      }
      break;

      /* Returns whether the status register to check is SR2 */
    case 0x2000:
      /* Check the status of the specified I2C flag */
      if ((I2C->SR2 & (u8)Flag) != 0)
      {
        /* Flag is set */
        bitstatus = SET;
      }
      else
      {
        /* Flag is reset */
        bitstatus = RESET;
      }
      break;

      /* Returns whether the status register to check is SR3 */
    case 0x3000:
      /* Check the status of the specified I2C flag */
      if ((I2C->SR3 & (u8)Flag) != 0)
      {
        /* Flag is set */
        bitstatus = SET;
      }
      else
      {
        /* Flag is reset */
        bitstatus = RESET;
      }
      break;

    default:
      break;

  }
  /* Return the flag status */
  return bitstatus;
}

/**
  * @brief Clear flags
  * @param[in] Flag : Specifies the flag to clear
  * This parameter can be one of the following:
  * I2C_FLAG_STOPDETECTION
  * I2C_FLAG_HEADERSENT
  * I2C_FLAG_TRANSFERFINISHED
  * I2C_FLAG_ADDRESSSENTMATCHED
  * I2C_FLAG_STARTDETECTION
  * I2C_FLAG_WAKEUPFROMHALT
  * I2C_FLAG_OVERRUNUNDERRUN
  * I2C_FLAG_ACKNOWLEDGEFAILURE
  * I2C_FLAG_ARBITRATIONLOSS
  * I2C_FLAG_BUSERROR   
  * @retval None
  */
void I2C_ClearFlag(I2C_Flag_TypeDef Flag)
{
  u8 tmp1 = 0;
  u8 tmp2 = 0;
  u16 tmp3 = 0;

  /* Check the parameters */
  assert_param(IS_I2C_CLEAR_FLAG_OK(Flag));

  /* Check the clear flag methodology index */
  tmp3 = ((u16)Flag & (u16)0x0F00);
  
   /* Clear the flag directly in the SR2 register */
  if(tmp3 == 0x0100)
  {
	/* Clear the selected I2C flag */
      I2C->SR2 = (u8)(~(u8)Flag);
  }
  /* Flags that need a read of SR1 register and a dummy write in CR2 register to be cleared */
  else if(tmp3 == 0x0200)
  {
      /* Read the SR1 register */
      tmp1 = I2C->SR1;
      /* Dummy write in the CR2 register */
      I2C->CR2 = I2C->CR2;
  }
  /* Flags that need a read of SR1 register followed by a read of SR3 register to be cleared */
  else if(tmp3 == 0x0300)
  {
	  /* 2 variables are used to avoid any compiler optimization */
      /* Read the SR1 register */
      tmp1 = I2C->SR1;
      /* Read the SR3 register */
      tmp2 = I2C->SR3;
  }
  /* Flags that need a read of SR1 register followed by a read of DR register to be cleared */
  else if(tmp3 == 0x0400)
  {
      /* 2 variables are used to avoid any compiler optimization */
      /* Read the SR1 register */
      tmp1 = I2C->SR1;
      /* Read the DR register */
      tmp2 = I2C->DR;
  }
}

/**
  * @brief Returns the specified I2C pending bit status
  * @param[in] ITPendingBit : Specifies the flag to read
  * This parameter can be any of the  @ref I2C_ITPendingBit_TypeDef enumeration.
  * @retval ITStatus : Status of the pending bit.
	* This parameter can be any of the @ref ITStatus enumeration.
  */
ITStatus I2C_GetITStatus(I2C_ITPendingBit_TypeDef ITPendingBit)
{
    ITStatus itstatus = RESET;

    /* Check the parameters */
    assert_param(IS_I2C_ITPENDINGBIT_OK(ITPendingBit));

    /* Check SRx index */
    if (((u16)ITPendingBit & 0xF000) == 0x1000) /* Returns whether the status register to check is SR1 */
    {
        /* Check the status of the specified I2C pending bit */
        if ((I2C->SR1 & (u8)ITPendingBit) != 0)
        {
            /* Flag is set */
            itstatus = SET;
        }
        else
        {
            /* Flag is reset */
            itstatus = RESET;
        }
    }
    else /* Returns whether the status register to check is SR2 */
    {
        /* Check the status of the specified I2C pending bit */
        if ((I2C->SR2 & (u8)ITPendingBit) != 0)
        {
            /* Flag is set */
            itstatus = SET;
        }
        else
        {
            /* Flag is reset */
            itstatus = RESET;
        }
    }

    /* Return the pending bit status */
    return itstatus;

}
/**
  * @brief Clear IT pending bit
  * @param[in] ITPendingBit Specifies the pending bit to clear
  * This parameter can be one of the following:
  * I2C_ITPENDINGBIT_STOPDETECTION
  * I2C_ITPENDINGBIT_HEADERSENT
  * I2C_ITPENDINGBIT_TRANSFERFINISHED 
  * I2C_ITPENDINGBIT_ADDRESSSENTMATCHED
  * I2C_ITPENDINGBIT_STARTDETECTION
  * I2C_ITPENDINGBIT_WAKEUPFROMHALT 
  * I2C_ITPENDINGBIT_OVERRUNUNDERRUN
  * I2C_ITPENDINGBIT_ACKNOWLEDGEFAILURE
  * I2C_ITPENDINGBIT_ARBITRATIONLOSS 
  * I2C_ITPENDINGBIT_BUSERROR  
  * @retval None
  */

void I2C_ClearITPendingBit(I2C_ITPendingBit_TypeDef ITPendingBit)
{
  u8 tmp1 = 0;
  u8 tmp2 = 0;
  u16 tmp3 = 0;

  /* Check the parameters */
  assert_param(IS_I2C_ITPENDINGBIT_OK(ITPendingBit));

  /* Check the clear flag methodology index */
  tmp3 = ((u16)ITPendingBit & (u16)0x0F00);
  
  /* Clear the flag directly in the SR2 register */
  if(tmp3 == 0x0100)
  {
	/* Clear the selected I2C flag */
      I2C->SR2 = (u8)(~(u8)ITPendingBit);
  }
  /* Flags that need a read of SR1 register and a dummy write in CR2 register to be cleared */
  else if(tmp3 == 0x0200)
  {
      /* Read the SR1 register */
      tmp1 = I2C->SR1;
      /* Dummy write in the CR2 register */
      I2C->CR2 = I2C->CR2;
  }
  /* Flags that need a read of SR1 register followed by a read of SR3 register to be cleared */
  else if(tmp3 == 0x0300)
  {
	  /* 2 variables are used to avoid any compiler optimization */
      /* Read the SR1 register */
      tmp1 = I2C->SR1;
      /* Read the SR3 register */
      tmp2 = I2C->SR3;
  }
  /* Flags that need a read of SR1 register followed by a read of DR register to be cleared */
  else if(tmp3 == 0x0400)
  {
      /* 2 variables are used to avoid any compiler optimization */
      /* Read the SR1 register */
      tmp1 = I2C->SR1;
      /* Read the DR register */
      tmp2 = I2C->DR;
  }
}
/**
  * @}
  */


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

⌨️ 快捷键说明

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