?? st79_usart.c
字號:
/**
********************************************************************************
* @file st79_usart.c
* @brief This file contains all the functions for the USART peripheral.
* @author STMicroelectronics - MCD & APG Car Body Application Labs
* @version V0.01
* @date 04-JUL-2007
******************************************************************************
*
* THE PRESENT SOFTWARE 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 SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2007 STMicroelectronics</center></h2>
* @image html logo.bmp
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "st79_usart.h"
/* LINKER SECTIONS DEFINITION FOR THIS FILE ONLY */
#pragma section (USART_CODE)
#pragma section const {USART_CONST}
#pragma section @near [USART_URAM]
#pragma section @near {USART_IRAM}
#pragma section @tiny [USART_UZRAM]
#pragma section @tiny {USART_IZRAM}
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/** @addtogroup USART_Private_Functions
* @{
*/
/**
* @}
*/
/* Public functions ----------------------------------------------------------*/
/** @}
* @addtogroup USART_Public_Functions
* @{
*/
/**
* @brief Clears the USART's pending flags.
* @par To be checked
* - Check with designer the Clearing Procedure for TC and TXE flags
* @par Full description:
* Clears the USART's pending flags.
* @param[in] USARTx can be only USART
* @param[in] USART_FLAG specifies the flag to clear
* This parameter can be one of the following values:
* - USART_FLAG_LBD
* - USART_FLAG_TXE
* - USART_FLAG_TC
* - USART_FLAG_RXNE
* - USART_FLAG_IDLE
* - USART_FLAG_ORE
* - USART_FLAG_NE
* - USART_FLAG_FE
* - USART_FLAG_PE
* - USART_FLAG_SBK
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Clear the USART TC flag
* @code
* USART_ClearFlag(USART, USART_FLAG_TC);
* @endcode
*/
void USART_ClearFlag(USART_TypeDef* USARTx,USART_Flag_TypeDef USART_FLAG)
{
u8 dummy= (u8)(0xAA);
assert(IS_USART_FLAG_VALUE_OK(USART_FLAG));
switch (USART_FLAG)
{
/*< Clear the Transmit Register Empty flag */
case USART_FLAG_TXE:
USARTx->DR = dummy;
break;
/*< Clear the Transmission Complete flag */
case USART_FLAG_TC:
USARTx->SR |= USART_SR_TC; /*TBD*/
USARTx->SR |= USART_SR_TC;
USARTx->DR = dummy;
break;
/*< Clear the Receive Register Not Empty flag */
case USART_FLAG_RXNE:
/*< Clear the Idle Detection flag */
case USART_FLAG_IDLE:
/*< Clear the Overrun Error flag */
case USART_FLAG_ORE:
/*< Clear the Noise Error flag */
case USART_FLAG_NE:
/*< Clear the Framing Error flag */
case USART_FLAG_FE:
/*< Clear the Parity Error flag */
case USART_FLAG_PE:
dummy = USARTx->SR; /*< Read Status Register */ /*TBD*/
dummy = USARTx->SR; /*< Read Status Register */
dummy = USARTx->DR; /*< Read Data Register */
break;
/*< Clear the LIN Break Detection flag */
case USART_FLAG_LBD:
USARTx->CR4 &= (u8)~(USART_CR4_LBDF);
break;
case USART_FLAG_SBK:
USARTx->CR2 &= (u8)~(USART_CR2_SBK);
break;
default:
break;
}
}
/**
* @brief Enable the USART peripheral.
* @par Full description:
* Enable the USART peripheral.
* @param[in] USARTx can be only USART.
* @param[in] NewState new state of the USART Communication.
* This parameter can be: ENABLE or DISABLE.
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Enable USART peripheral.
* @code
* USART_Cmd(USART, ENABLE);
* @endcode
*/
void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Test if the USART is Enabled */
/* if(!(USART->CR1&USART_CR1_USARTD)) */
/* Wait for no Transmition before modifying the USART Enable bit */
/* while(!(USART->SR&(USART_SR_TC|USART_SR_TC))); */
if (NewState)
{
USARTx->CR1 &= (u8)(~USART_CR1_USARTD); /**< USART Enable */
}
else
{
USARTx->CR1 |= USART_CR1_USARTD; /**< USART Disable (for low power consumption) */
}
}
/**
* @brief Deinitializes the USART peripheral.
* @par Full description:
* Set the USART peripheral registers to their default reset values.
* @param[in] USARTx can be only USART.
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Deinitialize USART peripheral.
* @code
* USART_DeInit(USART);
* @endcode
*/
void USART_DeInit(USART_TypeDef* USARTx)
{
u8 dummy;
/*! Wait for no Transmition before DeInit process */
/*! while(!(USARTx->SR&(USART_SR_TC|USART_SR_TC))); */
/*< Clear the Idle Line Detected bit in the status rerister by a read
to the USART_SR register followed by a Read to the USART_DR register */
dummy = USARTx->SR;
dummy = USARTx->DR;
USARTx->BRR2 = USART_BRR2_RESET_VALUE; /*< Set USART_BRR2 to reset value 0x00 */
USARTx->BRR1 = USART_BRR1_RESET_VALUE; /*< Set USART_BRR1 to reset value 0x00 */
USARTx->CR1 = USART_CR1_RESET_VALUE; /*< Set USART_CR1 to reset value 0x00 */
USARTx->CR2 = USART_CR2_RESET_VALUE; /*< Set USART_CR2 to reset value 0x00 */
USARTx->CR3 = USART_CR3_RESET_VALUE; /*< Set USART_CR3 to reset value 0x00 */
USARTx->CR4 = USART_CR4_RESET_VALUE; /*< Set USART_CR4 to reset value 0x00 */
USARTx->CR5 = USART_CR5_RESET_VALUE; /*< Set USART_CR5 to reset value 0x00 */
USARTx->GT = USART_GT_RESET_VALUE;
USARTx->PSCR= USART_PSCR_RESET_VALUE;
}
/**
* @brief Checks whether the specified USART flag is set or not.
* @par Full description:
* Checks whether the specified USART flag is set or not.
* @param[in] USARTx can be only USART.
* @param[in] USART_FLAG specifies the flag to check.
* This parameter can be one of the following values:
* - USART_FLAG_LBD
* - USART_FLAG_TXE
* - USART_FLAG_TC
* - USART_FLAG_RXNE
* - USART_FLAG_IDLE
* - USART_FLAG_ORE
* - USART_FLAG_NE
* - USART_FLAG_FE
* - USART_FLAG_PE
* - USART_FLAG_SBK
* @retval FlagStatus (SET or RESET)
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Check the status of TC flag.
* @code
* FlagStatus TC_flag;
* TC_Flag = USART_GetFlagStatus(USART, USART_FLAG_TC);
* @endcode
*/
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, USART_Flag_TypeDef USART_FLAG)
{
assert(IS_USART_FLAG_VALUE_OK(USART_FLAG));
switch (USART_FLAG)
{
/*< Clear the Transmit Data Register Empty flag */
case USART_FLAG_TXE:
/*< Returns the Transmission Complete flag state */
case USART_FLAG_TC:
/*< Returns the Read Data Not Empty flag state */
case USART_FLAG_RXNE:
/*< Returns the IDLE Detection flag state */
case USART_FLAG_IDLE:
/*< Returns the Overrun Error flag state */
case USART_FLAG_ORE:
/*< Returns the Noise Error flag state */
case USART_FLAG_NE:
/*< Returns the Framing Error flag state */
case USART_FLAG_FE:
/*< Returns the Parity Error flag state */
case USART_FLAG_PE:
if (USARTx->SR&(u8)USART_FLAG)
{
return SET;
}
else
{
return RESET;
}
break;
/*< Returns the LIN Break Detection flag state */
case USART_FLAG_LBD:
if (USARTx->CR4&USART_CR4_LBDF)
{
return SET;
}
else
{
return RESET;
}
break;
/*< Returns the Send Break flag state */
case USART_FLAG_SBK:
if (USARTx->CR2 & USART_CR2_SBK)
{
return SET;
}
else
{
return RESET;
}
break;
default:
return RESET;
break;
}
}
/**
* @brief Checks whether the specified USART interrupt has occurred or not.
* @par Full description:
* Checks whether the specified USART interrupt has occurred or not.
* @param[in] USARTx can be only USART.
* @param[in] USART_IT specifies the USART interrupt source to check.
* This parameter can be one of the following values:
* - USART_IT_PIEN
* - USART_IT_TCIEN
* - USART_IT_RIEN
* - USART_IT_ILIEN
* - USART_IT_RIEN
* - USART_IT_LBDIEN
* @retval ITStatus - The new state of USART_IT (SET or RESET).
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* @code
* IT_Status PIEN_ITStatus;
* PIEN_ITStatus = USART_GetITStatus(USART, USART_IT_PIEN;
* @endcode
*/
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT)
{
assert(IS_USART_IT_VALUE_OK(USART_IT));
switch (USART_IT)
{
/*< Returns the Parity Interrupt Enable bit state */
case USART_IT_PIEN:
if (USARTx->CR1 & USART_CR1_PIEN)
{
return SET;
}
else
{
return RESET;
}
break;
/*< Returns the Transmitter Interrupt Enable bit state */
case USART_IT_TIEN:
if (USARTx->CR2 & USART_CR2_TIEN)
{
return SET;
}
else
{
return RESET;
}
break;
/*< Returns the Transmission Complete Interrupt Enable bit state */
case USART_IT_TCIEN:
if (USARTx->CR2 & USART_CR2_TCIEN)
{
return SET;
}
else
{
return RESET;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -