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

📄 stm8s_uart3.c

📁 STM8全部资料
💻 C
📖 第 1 页 / 共 2 页
字号:
/**
  ********************************************************************************
  * @file stm8s_uart3.c
  * @brief This file contains all the functions for the uart3 peripheral.
  * @author STMicroelectronics - MCD Application Team
  * @version V1.0.1
  * @date 09/22/2008
  ******************************************************************************
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2008 STMicroelectronics</center></h2>
  * @image html logo.bmp
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm8s_uart3.h"
#include "stm8s_clk.h"

/* LINKER SECTIONS DEFINITION FOR THIS FILE ONLY */
#ifdef USE_COSMIC_SECTIONS
#pragma section (UART3_CODE)
#pragma section const {UART3_CONST}
#pragma section @near [UART3_URAM]
#pragma section @near {UART3_IRAM}
#pragma section @tiny [UART3_UZRAM]
#pragma section @tiny {UART3_IZRAM}
#endif

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

/** @}
  * @addtogroup UART3_Public_Functions
  * @{
  */

/**
  * @brief Deinitializes the UART3 peripheral.
  * @par Full description:
  * Set the UART3 peripheral registers to their default reset values.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Deinitialize UART3 peripheral.
  * @code
  * UART3_DeInit();
  * @endcode
  */

void UART3_DeInit(void)
{
  u8 dummy = 0;
  /*< Clear the Idle Line Detected bit in the status rerister by a read
     to the UART3_SR register followed by a Read to the UART3_DR register */
  dummy = UART3->SR;
  dummy = UART3->DR;

  UART3->BRR2 = UART3_BRR2_RESET_VALUE;  /*< Set UART3_BRR2 to reset value 0x00 */
  UART3->BRR1 = UART3_BRR1_RESET_VALUE;  /*< Set UART3_BRR1 to reset value 0x00 */

  UART3->CR1 = UART3_CR1_RESET_VALUE; /*< Set UART3_CR1 to reset value 0x00  */
  UART3->CR2 = UART3_CR2_RESET_VALUE; /*< Set UART3_CR2 to reset value 0x00  */
  UART3->CR3 = UART3_CR3_RESET_VALUE;  /*< Set UART3_CR3 to reset value 0x00  */
  UART3->CR4 = UART3_CR4_RESET_VALUE;  /*< Set UART3_CR4 to reset value 0x00  */
  UART3->CR6 = UART3_CR6_RESET_VALUE; /*< Set UART3_CR6 to reset value 0x00  */

}

/**
  * @brief Initializes the UART3 according to the specified parameters.
  * @param[in] BaudRate: The baudrate.
 * @param[in] WordLength : This parameter can be any of the @ref UART3_WordLength_TypeDef enumeration.
  * @param[in] StopBits: This parameter can be any of the @ref UART3_StopBits_TypeDef enumeration.
  * @param[in] Parity: This parameter can be any of the @ref UART3_Parity_TypeDef enumeration.
  * @param[in] Mode: This parameter can be any of the @ref UART3_Mode_TypeDef values
 * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * - CLK_GetClockFreq().
  * @par Example:
  * @code
  * UART3_Init(9600, UART3_WORDLENGTH_8D, UART3_STOPBITS_1, UART3_PARITY_NO, UART3_MODE_TXRX_ENABLE);
  * @endcode
  */
void UART3_Init(u32 BaudRate, UART3_WordLength_TypeDef WordLength, UART3_StopBits_TypeDef StopBits, UART3_Parity_TypeDef Parity, UART3_Mode_TypeDef Mode)
{
  u8 BRR2_1, BRR2_2 = 0;
  u32 BaudRate_Mantissa, BaudRate_Mantissa100 = 0;

  assert_param(IS_UART3_WORDLENGTH_OK(WordLength));

  assert_param(IS_UART3_STOPBITS_OK(StopBits));

  assert_param(IS_UART3_PARITY_OK(Parity));

  /* assert_param: BaudRate value should be <= 625000 bps */
  assert_param(IS_UART3_BAUDRATE_OK(BaudRate));

  /* assert_param: UART3_Mode value should exclude values such as  UART3_ModeTx_Enable|UART3_ModeTx_Disable */
  assert_param(IS_UART3_MODE_OK((u8)Mode));

  /* Wait for no Transmition before modifying the M bit */
  /* while(!(UART3->SR&UART3_SR_TC));      */

  UART3->CR1 &= (u8)(~UART3_CR1_M);     /**< Clear the word length bit */
  UART3->CR1 |= (u8)WordLength; /**< Set the word length bit according to UART3_WordLength value */

  UART3->CR3 &= (u8)(~UART3_CR3_STOP);  /**< Clear the STOP bits */
  UART3->CR3 |= (u8)StopBits;  /**< Set the STOP bits number according to UART3_StopBits value  */

  UART3->CR1 &= (u8)(~(UART3_CR1_PCEN | UART3_CR1_PS));  /**< Clear the Parity Control bit */
  UART3->CR1 |= (u8)Parity;     /**< Set the Parity Control bit to UART3_Parity value */

  UART3->BRR1 &= (u8)(~UART3_BRR1_DIVM);  /**< Clear the LSB mantissa of UART3DIV  */
  UART3->BRR2 &= (u8)(~UART3_BRR2_DIVM);  /**< Clear the MSB mantissa of UART3DIV  */
  UART3->BRR2 &= (u8)(~UART3_BRR2_DIVF);  /**< Clear the Fraction bits of UART3DIV */

  /**< Set the UART3 BaudRates in BRR1 and BRR2 registers according to UART3_BaudRate value */
  BaudRate_Mantissa    = ((u32)CLK_GetClockFreq() / (BaudRate << 4));
  BaudRate_Mantissa100 = (((u32)CLK_GetClockFreq() * 100) / (BaudRate << 4));
  /**< The fraction and MSB mantissa should be loaded in one step in the BRR2 register */
  BRR2_1 = (u8)((u8)(((BaudRate_Mantissa100 - (BaudRate_Mantissa * 100))
                      << 4) / 100) & (u8)0x0F); /**< Set the fraction of UART3DIV  */
  BRR2_2 = (u8)((BaudRate_Mantissa >> 4) & (u8)0xF0);

  UART3->BRR2 = (u8)(BRR2_1 | BRR2_2);
  UART3->BRR1 = (u8)BaudRate_Mantissa;           /**< Set the LSB mantissa of UART3DIV  */

  if ((u8)Mode&(u8)UART3_MODE_TX_ENABLE)
  {
    UART3->CR2 |= UART3_CR2_TEN;  /**< Set the Transmitter Enable bit */
  }
  else
  {
    UART3->CR2 &= (u8)(~UART3_CR2_TEN);  /**< Clear the Transmitter Disable bit */
  }
  if ((u8)Mode & (u8)UART3_MODE_RX_ENABLE)
  {
    UART3->CR2 |= UART3_CR2_REN;  /**< Set the Receiver Enable bit */
  }
  else
  {
    UART3->CR2 &= (u8)(~UART3_CR2_REN);  /**< Clear the Receiver Disable bit */
  }
}
/**
  * @brief Enable the UART3 peripheral.
  * @par Full description:
  * Enable the UART3 peripheral.
  * @param[in] NewState new state of the UART3 Communication.
  * This parameter can be:
  * - ENABLE
  * - DISABLE
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Enable UART3 peripheral.
  * @code
  * UART3_Cmd(ENABLE);
  * @endcode
  */
void UART3_Cmd(FunctionalState NewState)
{

  if (NewState != DISABLE)
  {
    UART3->CR1 &= (u8)(~UART3_CR1_UARTD); /**< UART3 Enable */
  }
  else
  {
    UART3->CR1 |= UART3_CR1_UARTD;  /**< UART3 Disable (for low power consumption) */
  }
}

/**
 * @brief Enables or disables the specified UART3 interrupts.
 * @par Full description:
 * Enables or disables the specified UART3 interrupts.
 * @param[in] UART3_IT specifies the UART3 interrupt sources to be enabled or disabled.
 * This parameter can be one of the following values:
 *   - UART3_IT_LBDF:  LIN Break detection interrupt
 *   - UART3_IT_LHDF:  LIN Break detection interrupt
 *   - UART3_IT_TXE:  Tansmit Data Register empty interrupt
 *   - UART3_IT_TC:   Transmission complete interrupt
 *   - UART3_IT_RXNE_OR: Receive Data register not empty/Over run error interrupt
 *   - UART3_IT_IDLE: Idle line detection interrupt
 *   - UART3_IT_PE:   Parity Error interrupt
 * @param[in] NewState new state of the specified UART3 interrupts.
 * This parameter can be: ENABLE or DISABLE.
 * @retval void None
 * @par Required preconditions:
 * None
 * @par Called functions:
 * None
 * @par Example:
 * @code
 * UART3_ITConfig(UART3_IT_PE, ENABLE)
 * @endcode
 */
void UART3_ITConfig(UART3_IT_TypeDef UART3_IT, FunctionalState NewState)
{
  u8 uartreg, itpos = 0x00;
  assert_param(IS_UART3_CONFIG_IT_OK(UART3_IT));
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

  /* Get the UART3 register index */
  uartreg = (u8)(UART3_IT >> 0x08);

  /* Get the UART3 IT index */
  itpos = (u8)((u8)1 << (u8)((u8)UART3_IT & (u8)0x0F));

  if (NewState != DISABLE)
  {
    /**< Enable the Interrupt bits according to UART3_IT mask */
    if (uartreg == 0x01)
    {
      UART3->CR1 |= itpos;
    }
    else if (uartreg == 0x02)
    {
      UART3->CR2 |= itpos;
    }
    else if (uartreg == 0x03)
    {
      UART3->CR4 |= itpos;
    }
    else
    {
      UART3->CR6 |= itpos;
    }
  }
  else
  {
    /**< Disable the interrupt bits according to UART3_IT mask */
    if (uartreg == 0x01)
    {
      UART3->CR1 &= (u8)(~itpos);
    }
    else if (uartreg == 0x02)
    {
      UART3->CR2 &= (u8)(~itpos);
    }
    else if (uartreg == 0x03)
    {
      UART3->CR4 &= (u8)(~itpos);
    }
    else
    {
      UART3->CR6 &= (u8)(~itpos);
    }
  }
}

/**
  * @brief Sets the UART3 LIN Break detection length.
  * @par Full description:
  * Sets the UART3 LIN Break detection length.
  * @param[in] UART3_LINBreakDetectionLength specifies the LIN break detection length.
  * This parameter can be any of the @ref UART3_LINBreakDetectionLength_TypeDef values.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * @code
  * UART3_LINBreakDetectionConfig(UART3_LINBREAKDETECTIONLENGTH_11BITS)
  * @endcode
  */
void UART3_LINBreakDetectionConfig(UART3_LINBreakDetectionLength_TypeDef UART3_LINBreakDetectionLength)
{
  assert_param(IS_UART3_LINBREAKDETECTIONLENGTH_OK(UART3_LINBreakDetectionLength));

  if (UART3_LINBreakDetectionLength != UART3_LINBREAKDETECTIONLENGTH_10BITS)
  {
    UART3->CR4 |= UART3_CR4_LBDL;
  }
  else
  {
    UART3->CR4 &= ((u8)~UART3_CR4_LBDL);
  }
}

/**
  * @brief Configue the UART3 peripheral.
  * @par Full description:
  * Configue the UART3 peripheral.
  * @param[in] UART3_Mode specifies the LIN mode.
  * This parameter can be any of the @ref UART3_LinMode_TypeDef values.
 * @param[in] UART3_Autosync specifies the LIN automatic resynchronization mode.
  * This parameter can be any of the @ref UART3_LinAutosync_TypeDef values.
 * @param[in] UART3_DivUp specifies the LIN divider update method.
  * This parameter can be any of the @ref UART3_LinDivUp_TypeDef values.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * @code
  * UART3_LINConfig(UART3_LIN_MODE_MASTER, UART3_LIN_AUTOSYNC_DISABLE,UART3_LIN_DIVUP_LBRR1);
  * @endcode
  */
void UART3_LINConfig(UART3_LinMode_TypeDef UART3_Mode, UART3_LinAutosync_TypeDef UART3_Autosync, UART3_LinDivUp_TypeDef UART3_DivUp)
{
  assert_param(IS_UART3_SLAVE_OK(UART3_Mode));

  assert_param(IS_UART3_AUTOSYNC_OK(UART3_Autosync));

  assert_param(IS_UART3_DIVUP_OK(UART3_DivUp));

  if (UART3_Mode != UART3_LIN_MODE_MASTER)
  {
    UART3->CR6 |=  UART3_CR6_LSLV;
  }
  else
  {
    UART3->CR6 &= ((u8)~UART3_CR6_LSLV);
  }

  if (UART3_Autosync != UART3_LIN_AUTOSYNC_DISABLE)
  {
    UART3->CR6 |=  UART3_CR6_LASE ;
  }
  else
  {
    UART3->CR6 &= ((u8)~ UART3_CR6_LASE );
  }

  if (UART3_DivUp != UART3_LIN_DIVUP_LBRR1)
  {
    UART3->CR6 |=  UART3_CR6_LDUM;
  }
  else
  {
    UART3->CR6 &= ((u8)~ UART3_CR6_LDUM);
  }

}

/**
  * @brief Enables or disables the UART3 LIN mode.
  * @par Full description:
  * Enables or disables the UART3抯 LIN mode.
  * @param[in] NewState is new state of the UART3 LIN mode.
  * This parameter can be:
  * - ENABLE
  * - DISABLE
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * @code
  * UART3_LINCmd(ENABLE)
  * @endcode
  */
void UART3_LINCmd(FunctionalState NewState)
{
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

  if (NewState != DISABLE)
  {
    /* Enable the LIN mode by setting the LINE bit in the CR2 register */
    UART3->CR3 |= UART3_CR3_LINEN;
  }
  else
  {
    /* Disable the LIN mode by clearing the LINE bit in the CR2 register */
    UART3->CR3 &= ((u8)~UART3_CR3_LINEN);
  }
}

/**
  * @brief Selects the UART3 WakeUp method.
  * @par Full description:
  * Selects the UART3 WakeUp method.
  * @param[in] UART3_WakeUp: specifies the UART3 wakeup method.
  * This parameter can be any of the @ref UART3_WakeUp_TypeDef values.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * UART3 WakeUp Idle line .
  * @code
    * UART3_WakeUpConfig(UART3_WAKEUP_IDLELINE);
  * @endcode
  */
void UART3_WakeUpConfig(UART3_WakeUp_TypeDef UART3_WakeUp)
{
  assert_param(IS_UART3_WAKEUP_OK(UART3_WakeUp));

  UART3->CR1 &= ((u8)~UART3_CR1_WAKE);
  UART3->CR1 |= (u8)UART3_WakeUp;
}

/**
  * @brief Determines if the UART3 is in mute mode or not.
  * @par Full description:
  * Determines if the UART3 is in mute mode or not.
  * @param[in] NewState: new state of the UART3 mode.
  * This parameter can be:
  * - ENABLE
  * - DISABLE
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * @code
  * UART3_ReceiverWakeUpCmd(DISABLE);
  * @endcode
  */
void UART3_ReceiverWakeUpCmd(FunctionalState NewState)
{
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

  if (NewState != DISABLE)
  {
    /* Enable the mute mode UART3 by setting the RWU bit in the CR2 register */
    UART3->CR2 |= UART3_CR2_RWU;
  }
  else
  {
    /* Disable the mute mode UART3 by clearing the RWU bit in the CR1 register */
    UART3->CR2 &= ((u8)~UART3_CR2_RWU);
  }
}


/**

⌨️ 快捷键说明

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