📄 73x_uart.c
字号:
/******************** (C) COPYRIGHT 2005 STMicroelectronics ********************
* File Name : 73x_uart.c
* Author : MCD Application Team
* Date First Issued : 09/27/2005 : V1.0
* Description : This file provides all the UART software functions.
**********************************************************************************
* History:
* 09/27/2005 : V1.0
**********************************************************************************
* 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.
*********************************************************************************/
/* Standard include ----------------------------------------------------------*/
#include "73x_uart.h"
#include "73x_prccu.h"
#include "73x_cfg.h"
/* Include of other module interface headers ---------------------------------*/
/* Local includes ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Interface functions -------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/******************************************************************************
* Function Name : UART_DeInit
* Description : Deinitializes the UARTx peripheral registers to their default
* : reset values.
* Input : - UARTx: where x can be 0..3 to select the UARTx peripheral.
* Output : None
* Return : None
******************************************************************************/
void UART_DeInit(UART_TypeDef *UARTx)
{
if( UARTx == UART0)
{
CFG_PeripheralClockConfig(CFG_CLK_UART0,DISABLE);
CFG_PeripheralClockConfig(CFG_CLK_UART0,ENABLE);
}
else if ( UARTx == UART1)
{
CFG_PeripheralClockConfig(CFG_CLK_UART1,DISABLE);
CFG_PeripheralClockConfig(CFG_CLK_UART1,ENABLE);
}
else if ( UARTx == UART2)
{
CFG_PeripheralClockConfig(CFG_CLK_UART2,DISABLE);
CFG_PeripheralClockConfig(CFG_CLK_UART2,ENABLE);
}
else if ( UARTx == UART3)
{
CFG_PeripheralClockConfig(CFG_CLK_UART3,DISABLE);
CFG_PeripheralClockConfig(CFG_CLK_UART3,ENABLE);
}
}
/******************************************************************************
* Function Name : UART_StructInit
* Description : Fills in a UART_InitTypeDef structure with the reset value of
* : each parameter.
* Input : - UART_InitStruct : pointer to a UART_InitTypeDef structure
which will be initialized.
* Output : None
* Return : None
******************************************************************************/
void UART_StructInit(UART_InitTypeDef* UART_InitStruct)
{
/*Initializes BaudRate*/
UART_InitStruct->UART_BaudRate = 9600;
/*Initializes Mode*/
UART_InitStruct->UART_Mode = 0x000;
/*Initializes Stop Bits*/
UART_InitStruct->UART_StopBits = UART_StopBits_0_5;
/*Initializes Parity*/
UART_InitStruct->UART_Parity = UART_Parity_Even;
/*Enable standard transmit/recieve mode*/
UART_InitStruct->UART_Loop_Standard = UART_Standard;
/*Disable yhe Receive mode*/
UART_InitStruct->UART_Rx = UART_Rx_Disable;
/*Disable the FOFO mode*/
UART_InitStruct->UART_FIFO = UART_FIFO_Disable;
}
/******************************************************************************
* Function Name : UART_Init
* Description : Initializes the UARTx peripheral according to the specified
* parameters in the UART_InitTypeDef structure.
* Input : - UARTx: where x can be 0..3 to select the UARTx peripheral.
* - UART_InitStruct: pointer to a UART_InitTypeDef structure that
contains the configuration information for the specified UART
peripheral.
* Output : None
* Return : None
******************************************************************************/
void UART_Init(UART_TypeDef *UARTx, UART_InitTypeDef* UART_InitStruct)
{
u32 tmpBaudRate = 0;
/*Initialize struct*/
UARTx->BR = UART_BR_MASK;
UARTx->CR &= UART_CR_MASK;
/*Configure BaudRate*/
tmpBaudRate = (u32)((PRCCU_GetFrequencyValue(PRCCU_CLOCK_MCLK) * 10) / (16*UART_InitStruct->UART_BaudRate));
/*Search the reload value (Integer)*/
if(tmpBaudRate -((tmpBaudRate/10)*10) < 5)
{
UARTx->BR = tmpBaudRate /10;
}
else
{
UARTx->BR = (tmpBaudRate /10)+1;
}
/*Choose Mode*/
UARTx->CR = (UARTx->CR & 0xFFF8) | (u16)UART_InitStruct->UART_Mode;
/*Choose Stop Bits*/
UARTx->CR = (UARTx->CR & 0xFFE7) | (u16)UART_InitStruct->UART_StopBits;
/*Choose Parity*/
UARTx->CR |= UART_InitStruct->UART_Parity;
/*Choose loopback or standard transmit/recieve mode*/
UARTx->CR |= UART_InitStruct->UART_Loop_Standard;
/*Enable or disable receive mode*/
UARTx->CR |= UART_InitStruct->UART_Rx;
/*Enable or disable FIFO mode*/
UARTx->CR |= UART_InitStruct->UART_FIFO;
}
/*******************************************************************************
* Function Name : UART_ByteBufferSend
* Description : Transmits data from a user defined buffer and returns the
* status of transmission.
* Input : - UARTx: where x can be 0..3 to select the UARTx peripheral.
* - PtrToBuffer : A pointer to the data to send
* - NbOfBytes: The data length in bytes
* Output : None
* Return : None
*******************************************************************************/
void UART_ByteBufferSend(UART_TypeDef *UARTx, u8* PtrToBuffer, u8 NbOfBytes)
{
if ((UARTx->CR & UART_FIFO_Enable) == UART_FIFO_Enable)
{
/*FIFO is enabled*/
while(NbOfBytes!=0)
{
NbOfBytes--;
/* while the UART_TxFIFO contain 16 characters.*/
while((UARTx->SR & UART_Flag_TxFull) == UART_Flag_TxFull);
UARTx->TxBUFR = *PtrToBuffer;
PtrToBuffer++;
}
}
else
{
/*FIFO is disabled*/
while(NbOfBytes!=0 )
{
NbOfBytes--;
/* while the UART_TxFIFO contain one character.*/
while ((UARTx->SR & UART_Flag_TxEmpty) != UART_Flag_TxEmpty);
UARTx->TxBUFR = *PtrToBuffer;
PtrToBuffer++;
}
}
}
/*******************************************************************************
* Function Name : UART_9BitByteBufferSend
* Description : Transmits 9 bits data from a user defined buffer and returns the
* status of transmission.
* Input : - UARTx: where x can be 0..3 to select the UARTx peripheral.
* - PtrToBuffer : A pointer to the data to send
* - NbOfBytes: The data length in bytes
* Output : None
* Return : None
*******************************************************************************/
void UART_9BitByteBufferSend(UART_TypeDef *UARTx, u16* PtrToBuffer, u8 NbOfBytes)
{
if ((UARTx->CR & UART_FIFO_Enable) == UART_FIFO_Enable)
{
/*FIFO is enable*/
while(NbOfBytes!=0)
{
NbOfBytes--;
/* while the UART_TxFIFO contain 16 characters.*/
while((UARTx->SR & UART_Flag_TxFull) == UART_Flag_TxFull)
UARTx->TxBUFR = *PtrToBuffer;
PtrToBuffer++;
}
}
else
{
/*FIFO is disable*/
while(NbOfBytes!=0 )
{
NbOfBytes--;
/* while the UART_TxFIFO contain one character.*/
while((UARTx->SR & UART_Flag_TxEmpty) != UART_Flag_TxEmpty);
UARTx->TxBUFR = *PtrToBuffer;
PtrToBuffer++;
}
}
}
/*******************************************************************************
* Function Name : UART_StringSend
* Description : Transmits a string from a user defined string and returns the
* status of transmission.
* Input : - UARTx: where x can be 0..3 to select the UARTx peripheral.
* - PtrToString : A pointer to the sting to send
* Output : None
* Return : None
*******************************************************************************/
void UART_StringSend(UART_TypeDef *UARTx, u8 *PtrToString)
{
if((UARTx->CR & UART_FIFO_Enable)==UART_FIFO_Enable)
{
/*FIFO is enable*/
while(*PtrToString != '\0')
{
/*while the UART_TxFIFO contains 16 characters */
while((UARTx->SR & UART_Flag_TxFull) == UART_Flag_TxFull);
UARTx->TxBUFR = *PtrToString;
PtrToString++;
}
while((UARTx->SR & UART_Flag_TxFull) == UART_Flag_TxFull);
}
else
{
/*FIFO is disable*/
while(*PtrToString != '\0')
{
/*while the UART_TxFIFO contain one character.*/
while ((UARTx->SR & UART_Flag_TxEmpty) != UART_Flag_TxEmpty);
UARTx->TxBUFR = *PtrToString;
PtrToString++;
}
while ((UARTx->SR & UART_Flag_TxEmpty) != UART_Flag_TxEmpty);
}
UARTx->TxBUFR = '\0';
}
/*******************************************************************************
* Function Name : UART_ByteBufferReceive
* Description : Receives number of data words (Byte), stores them in user defined
* area and returns the status of the reception.
* Input : - UARTx: where x can be 0..3 to select the UARTx peripheral.
* - PtrToBuffer : A pointer to the buffer where the data will be stored.
* - NbOfBytes : The data length.
* Output : None
* Return : An ErrorStatus enumuration value:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -