?? 71x_uart.c
字號:
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name : 71x_uart.c
* Author : MCD Application Team
* Version : V4.0
* Date : 10/09/2007
* Description : This file provides all the UART firmware functions
********************************************************************************
* 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.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "71x_uart.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : UART_Init
* Description : This function initializes the selected UART registers to
* their reset values.
* Input : - UARTx: selects the UART to be configured UART
* (x can be 0,1, 2 or 3).
* Output : None.
* Return : None.
*******************************************************************************/
void UART_Init(UART_TypeDef *UARTx)
{
UARTx->CR = 0x0000;
UARTx->IER = 0x0000;
(void)UARTx->RxBUFR;
UARTx->RxRSTR = 0xFFFF;
UARTx->TxRSTR = 0xFFFF;
UARTx->TOR = 0x0000;
UARTx->GTR = 0x0000;
}
/*******************************************************************************
* Function Name : UART_ModeConfig
* Description : This function configures the mode of the selected UART.
* Input : - UARTx: selects the UART to be configured
* (x can be 0,1, 2 or 3).
* - UART_Mode: selects the UART modeThe UART mode,
* it can be one of the following parameters:
* - UARTM_8D for 8-bit data format
* - UARTM_7D_P for 7-bit data + parity format
* - UART_9D for 9-bit data format
* - UART_8D_W for 8-bit data + wake-up bit format
* - UART_8D_P for 8-bit data + parity bit format
* Output : None.
* Return : None.
*******************************************************************************/
void UART_ModeConfig(UART_TypeDef *UARTx, UARTMode_TypeDef UART_Mode)
{
UARTx->CR = (UARTx->CR & 0xFFF8) | (u16)UART_Mode;
}
/*******************************************************************************
* Function Name : UART_BaudRateConfig
* Description : This function configures the baud rate of the selected UART.
* Input : - UARTx: selects the UART to be configured
* (x can be 0,1, 2 or 3).
* - BaudRate: The baudrate value in bps.
* Output : None.
* Return : None.
*******************************************************************************/
void UART_BaudRateConfig(UART_TypeDef *UARTx, u32 BaudRate)
{
u32 tmpBaudRate = 0;
/* Configure BaudRate */
tmpBaudRate = (u32)((RCCU_FrequencyValue(RCCU_PCLK1) * 10) / (16 * BaudRate));
/*Search the reload value (Integer)*/
if (tmpBaudRate - ((tmpBaudRate / 10) * 10) < 5)
{
UARTx->BR = tmpBaudRate / 10;
}
else
{
UARTx->BR = (tmpBaudRate / 10) + 1;
}
}
/*******************************************************************************
* Function Name : UART_ParityConfig
* Description : This function configures the data parity of the selected UART.
* Input : - UARTx: the selected UART (x can be 0,1, 2 or 3).
* - Parity: the parity type, it can be:
* - UART_EVEN_PARITY for even parity configuration.
* - UART_ODD_PARITY for odd parity configuration.
* - UART_NO_PARITY for no parity configuration.
* Output : None.
* Return : None.
*******************************************************************************/
void UART_ParityConfig(UART_TypeDef *UARTx, UARTParity_TypeDef Parity)
{
UARTx->CR = (UARTx->CR & 0xFFDF) | (u16)Parity;
}
/*******************************************************************************
* Function Name : UART_StopBitsConfig
* Description : This function configures the number of stop bits of the
* selected UART.
* Input : - UARTx: selects the UART to be configured.
* (x can be 0,1, 2 or 3).
* - StopBits: the number of stop bits, it can be:
* - UART_0_5_StopBits for 0.5 stop bit.
* - UART_1_StopBits for 1 stop bit.
* - UART_1_5_StopBits for 1.5 stop bits.
* - UART_2_StopBits for 2 stop bits.
* Output : None.
* Return : None.
*******************************************************************************/
void UART_StopBitsConfig(UART_TypeDef *UARTx, UARTStopBits_TypeDef StopBits)
{
UARTx->CR = (UARTx->CR & 0xFFE7) | (u16)StopBits;
}
/*******************************************************************************
* Function Name : UART_Config
* Description : This function configures the Baud rate, parity mode,
* the number of stop bits and the UART mode of the selected
* UART.
* Input : - UARTx: the selected UART (x can be 0,1, 2 or 3).
* - BaudRate: the baudrate value in bps.
* - Parity: selects the parity type, it can be:
- UART_EVEN_PARITY for even parity configuration.
* - UART_ODD_PARITY for odd parity configuration.
* - UART_NO_PARITY for no parity configuration.
* - StopBits: selects the number of the stop bits, it can be:
* - UART_0_5_StopBits for 0.5 stop bit.
* - UART_1_StopBits for 1 stop bit.
* - UART_1_5_StopBits for 1.5 stop bits.
* - UART_2_StopBits for 2 stop bits.
* - Mode: selects the UART mode, it can be one of the following
* parameters:
* - UARTM_8D for 8-bit data format.
* - UARTM_7D_P for 7-bit data + parity format.
* - UART_9D for 9-bit data format.
* - UART_8D_W for 8-bit data + wake-up bit format.
* - UART_8D_P for 8-bit data + parity bit format.
* Output : None.
* Return : None.
*******************************************************************************/
void UART_Config(UART_TypeDef *UARTx, u32 BaudRate, UARTParity_TypeDef Parity,
UARTStopBits_TypeDef StopBits, UARTMode_TypeDef Mode)
{
UART_ModeConfig(UARTx, Mode);
UART_BaudRateConfig(UARTx, BaudRate);
UART_ParityConfig(UARTx, Parity);
UART_StopBitsConfig(UARTx, StopBits);
}
/*******************************************************************************
* Function Name : UART_ItConfig
* Description : This function enables or disables one or several interrupt
* sources of the selected UART.
* Input : - UARTx: the selected UART (x can be 0,1, 2 or 3).
* - UART_Flag: selects one or several UART interrupt source.
* - NewState: specifies whether the interrupt source is
* enabled or disabled (ENABLE or DISABLE).
* Output : None.
* Return : None.
* Note : The UART interrupt flags are listed in the file uart.h
* except UART_TxFull flag will have no effect when using
* this function.
*******************************************************************************/
void UART_ItConfig(UART_TypeDef *UARTx, u16 UART_Flag, FunctionalState NewState)
{
if (NewState == ENABLE)
{
UARTx->IER |= UART_Flag;
}
else
{
UARTx->IER &= ~UART_Flag;
}
}
/*******************************************************************************
* Function Name : UART_FifoConfig
* Description : This function enables or disables the Rx and Tx FIFOs of
* the selected UART.
* Input : - UARTx: the selected UART (x can be 0,1, 2 or 3).
* - NewState: specifies whether the FIFOs are enabled or
* disabled (ENABLE or DISABLE).
* Output : None.
* Return : None.
*******************************************************************************/
void UART_FifoConfig(UART_TypeDef *UARTx, FunctionalState NewState)
{
if (NewState == ENABLE)
{
UARTx->CR |= 0x0400;
}
else
{
UARTx->CR &= ~0x0400;
}
}
/*******************************************************************************
* Function Name : UART_FifoReset
* Description : This function resets the Rx and the Tx FIFOs of the
* selected UART.
* Input : - UARTx: the selected UART (x can be 0,1, 2 or 3).
* - FIFO: Selects the FIFO to reset, it can be:
* - UART_RxFIFO
* - UART_TxFIFO
* Output : None.
* Return : None.
*******************************************************************************/
void UART_FifoReset(UART_TypeDef *UARTx, UARTFIFO_TypeDef FIFO)
{
if (FIFO == UART_RxFIFO)
{
UARTx->RxRSTR = 0xFFFF;
}
else
{
UARTx->TxRSTR = 0xFFFF;
}
}
/*******************************************************************************
* Function Name : UART_LoopBackConfig
* Description : This function enables or disables the loop back mode of
* the selected UART.
* Input : - UARTx: the selected UART (x can be 0,1, 2 or 3).
* - NewState: specifies whether the loop back is enabled
* or disabled (ENABLE or DISABLE).
* Output : None.
* Return : None.
*******************************************************************************/
void UART_LoopBackConfig(UART_TypeDef *UARTx, FunctionalState NewState)
{
if (NewState == ENABLE)
{
UARTx->CR |= 0x0040;
}
else
{
UARTx->CR &= ~0x0040;
}
}
/*******************************************************************************
* Function Name : UART_TimeOutPeriodConfig
* Description : This function configures the UART Time Out Period of the
* selected UART.
* Input : - UARTx: the selected UART (x can be 0,1, 2 or 3).
* - TimeOutPeriod: the time-out period value.
* Output : None.
* Return : None.
*******************************************************************************/
void UART_TimeOutPeriodConfig(UART_TypeDef *UARTx, u16 TimeOutPeriod)
{
UARTx->TOR = TimeOutPeriod;
}
/*******************************************************************************
* Function Name : UART_GuardTimeConfig
* Description : This function configures the UART Guard Time.
* Input : - UARTx: the selected UART (x can be 0,1, 2 or 3).
* - GuardTime: the guard time value.
* Output : None.
* Return : None.
*******************************************************************************/
void UART_GuardTimeConfig(UART_TypeDef *UARTx, u16 GuardTime)
{
UARTx->GTR = GuardTime;
}
/*******************************************************************************
* Function Name : UART_RxConfig
* Description : This function enables or disables the selected UART data
* reception.
* Input : - UARTx: selects the UART to be configured
* (x can be 0,1, 2 or 3).
* - NewState: specifies whether the reception is enabled or
* disabled (ENABLE or DISABLE).
* Output : None.
* Return : None.
*******************************************************************************/
void UART_RxConfig(UART_TypeDef *UARTx, FunctionalState NewState)
{
if (NewState == ENABLE)
{
UARTx->CR |= 0x0100;
}
else
{
UARTx->CR &= ~0x0100;
}
}
/*******************************************************************************
* Function Name : UART_OnOffConfig
* Description : This function sets On/Off the selected UART.
* Input : - UARTx: selects the UART to be configured
* (x can be 0,1, 2 or 3).
* - NewState: ENABLE or DISABLE.
* Output : None.
* Return : None.
*******************************************************************************/
void UART_OnOffConfig(UART_TypeDef *UARTx, FunctionalState NewState)
{
if (NewState == ENABLE)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -