一、 PY32F003F18串口HAL库函数
HAL库函数本身写得很好,但非常难以理解。
1、PY32F003F18串口配置函数UART_SetConfig(UART_HandleTypeDef *huart)
//函数功能:将UART_HandleTypeDef型结构变量写入"串口控制寄存器"
static void UART_SetConfig(UART_HandleTypeDef *huart)
{
uint32_t tmpreg;
uint32_t pclk;
/* Check the parameters */
assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
assert_param(IS_UART_PARITY(huart->Init.Parity));
assert_param(IS_UART_MODE(huart->Init.Mode));
/*-------------------------- USART CR2 Configuration -----------------------*/
/* Configure the UART Stop Bits: Set STOP[13:12] bits
according to huart->Init.StopBits value */
MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
//使用huart->Init.StopBits的值修改"串口控制寄存器2(USART_CR2)中的STOP位",用来设置停止位的位数
/*-------------------------- USART CR1 Configuration -----------------------*/
/* Configure the UART Word Length, Parity and mode:
Set the M bits according to huart->Init.WordLength value
Set PCE and PS bits according to huart->Init.Parity value
Set TE and RE bits according to huart->Init.Mode value
Set OVER8 bit according to huart->Init.OverSampling value */
#if defined(USART_CR3_OVER8)
tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode;
//将"数据长度,奇偶校验,串口发送和接收模式合并为字,准备修改"串口控制寄存器1(USART_CR1)
MODIFY_REG(huart->Instance->CR1,
(uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
tmpreg);
tmpreg = (uint32_t) huart->Init.OverSampling;//准备修改"串口控制寄存器3(USART_CR3)中的OVER8位"
MODIFY_REG(huart->Instance->CR3,
(uint32_t)(USART_CR3_OVER8),
tmpreg);
#else
tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode;
MODIFY_REG(huart->Instance->CR1,
(uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
tmpreg);
#endif /* USART_CR3_OVER8 */
/*-------------------------- USART CR3 Configuration -----------------------*/
/* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */
MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE), huart->Init.HwFlowCtl);
//修改"串口控制寄存器3中的RTS和CTS位"
#if defined(USART_CR3_OVER8)
/* Check the Over Sampling */
if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
{
/*-------------------------- USART BRR Configuration ---------------------*/
pclk = HAL_RCC_GetPCLK1Freq();//读取PCLK1时钟频率
huart->Instance->BRR = UART_BRR_SAMPLING8(pclk, huart->Init.BaudRate);
//设置串口波特率
}
else
{
/*-------------------------- USART BRR Configuration ---------------------*/
pclk = HAL_RCC_GetPCLK1Freq();//读取PCLK1时钟频率
huart->Instance->BRR = UART_BRR_SAMPLING16(pclk, huart->Init.BaudRate);
//设置串口波特率
}
#else
/*-------------------------- USART BRR Configuration ---------------------*/
pclk = HAL_RCC_GetPCLK1Freq();
huart->Instance->BRR = UART_BRR_SAMPLING16(pclk, huart->Init.BaudRate);
#endif /* USART_CR3_OVER8 */
}
2、PY32F003F18串口初始化函数HAL_UART_Init(UART_HandleTypeDef *huart)
//函数功能:根据UART_HandleTypeDef型结构,将串口配置为"非半双工模式",使能串口
HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
{
/* Check the UART handle allocation */
if (huart == NULL)
{
return HAL_ERROR;
}
/* Check the parameters */
if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)//没有使用RTS和CTS流程控制
{
/* The hardware flow control is available only for USART1 and USART2 */
assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
}
else
{
assert_param(IS_UART_INSTANCE(huart->Instance));
}
assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
#if defined(USART_CR3_OVER8)
assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
#endif /* USART_CR3_OVER8 */
if (huart->gState == HAL_UART_STATE_RESET)//UART外设还没有被初始化
{
/* Allocate lock resource and initialize it */
huart->Lock = HAL_UNLOCKED;//解锁,为初始化串口做准备
#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
UART_InitCallbacksToDefault(huart);
if (huart->MspInitCallback == NULL)
{
huart->MspInitCallback = HAL_UART_MspInit;
}
/* Init the low level hardware */
huart->MspInitCallback(huart);
#else
/* Init the low level hardware : GPIO, CLOCK */
HAL_UART_MspInit(huart);
#endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
}
huart->gState = HAL_UART_STATE_BUSY;
/* Disable the peripheral */
__HAL_UART_DISABLE(huart);
//将"串口控制寄存器1(USART_CR1)中的UE位"置0,不使能串口
/* Set the UART Communication parameters */
UART_SetConfig(huart);
//将UART_HandleTypeDef型指针变量huart的所指向的结构数据写入"串口控制寄存器"
if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
{
UART_AdvFeatureConfig(huart);
}
/* In asynchronous mode, the following bits must be kept cleared:
- CLKEN bit in the USART_CR2 register,
- HDSEL bit in the USART_CR3 register.*/
CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
//清除"串口控制寄存器2(USART_CR2)中的CLKEN位",不使能USARTx_CK引脚
CLEAR_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
//清除"串口控制寄存器3(USART_CR3)中的HDSEL位",配置串口为"非半双工模式"
/* Enable the peripheral */
__HAL_UART_ENABLE(huart);//将"串口控制寄存器1(USART_CR1)中的UE位"置1,使能串口
/*记录的状态值,Initialize the UART state */
huart->ErrorCode = HAL_UART_ERROR_NONE;
huart->gState = HAL_UART_STATE_READY;//UART外设已经被初始化,可以使用UART
huart->RxState = HAL_UART_STATE_READY;//UART接收已经被初始化,可以使用UART接收数据
return HAL_OK;
}
二、增加的函数:
#include "MyUSART.h"
void USART_ITConfig(USART_TypeDef * USARTx, uint32_t USART_IT, FunctionalState NewState);
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint32_t USART_IT);
void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint32_t USART_IT);
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
uint16_t USART_ReceiveData(USART_TypeDef* USARTx);
void USARTx_SetConfig(USART_TypeDef* USARTx,UART_InitTypeDef *Init,UART_AdvFeatureInitTypeDef *AdvancedInit);
//函数功能:
//当NewState=ENABLE,USART_IT=UART_IT_RXNE,使能串口接收寄存器为非空时产生中断,即使能RXNEIE=1
//当NewState=ENABLE,USART_IT=UART_IT_PE,使能串口奇偶校验错误产生中断,即使能PEIE=1
//当NewState=ENABLE,USART_IT=UART_IT_ERR,使能帧错误、噪音错误和溢出错误时产生中断,即使能EIE=1
//当NewState=ENABLE,USART_IT=UART_IT_TXE时,使能串口发送寄存器为空产生中断,即使能TXEIE=1
//当NewState=ENABLE,USART_IT=UART_IT_TC时,使能发送完成产生中断,即使能TCIE=1
void USART_ITConfig(USART_TypeDef * USARTx, uint32_t USART_IT, FunctionalState NewState)
{
if(NewState==DISABLE) _UART_DISABLE_IT(USARTx,USART_IT);
else _HAL_UART_ENABLE_IT(USARTx,USART_IT);
}
//函数功能:读串口状态标志位
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
{
if( USART_FLAG & READ_REG(USARTx->SR) ) return SET;
else return RESET;
}
//函数功能:读串口中断标志位
ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint32_t USART_IT)
{
if( USART_IT & READ_REG(USARTx->SR) ) return SET;
else return RESET;
}
//函数功能:
//USART_IT=UART_IT_RXNE,清除"串口接收寄存器为非空时产生的中断标志位"
//USART_IT=UART_IT_PE,清除"串口奇偶校验错误产生的中断标志位"
//USART_IT=UART_IT_ERR,清除"帧错误、噪音错误和溢出错误时产生的中断标志位"
//USART_IT=UART_IT_TXE时,清除"串口发送寄存器为空产生的中断标志位"
//USART_IT=UART_IT_TC时,清除"发送完成产生的中断标志位"
void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint32_t USART_IT)
{
CLEAR_BIT(USARTx->SR,USART_IT);//将"状态寄存器USART_SR"的USART_IT
}
//函数功能:串口发送数据
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
{
USARTx->DR = (Data & (uint16_t)0x01FF);
}
//函数功能:串口接收数据
uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
{
return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
}
//函数功能:USARTx_CK引脚配置
void USART_CK_Pin_Config(USART_TypeDef* USARTx, FunctionalState NewState)
{
if(NewState==DISABLE)
{
CLEAR_BIT(USARTx->CR2, USART_CR2_CLKEN);
//清除"串口控制寄存器2(USART_CR2)中的CLKEN位",不使能USARTx_CK引脚
}
else
{
SET_BIT(USARTx->CR2, USART_CR2_CLKEN);
//设置"串口控制寄存器2(USART_CR2)中的CLKEN位",使能USARTx_CK引脚
}
}
//函数功能:USARTx半双工通讯配置
//NewState=DISABLE,配置串口为"非半双工模式"
//NewState=ENABLE,配置串口为"半双工模式"
void USART_Half_Duplex_Config(USART_TypeDef* USARTx, FunctionalState NewState)
{
if(NewState==DISABLE)
{
CLEAR_BIT(USARTx->CR3, USART_CR3_HDSEL);
//清除"串口控制寄存器3(USART_CR3)中的HDSEL位",配置串口为"非半双工模式"
}
else
{
SET_BIT(USARTx->CR3, USART_CR3_HDSEL);
//设置"串口控制寄存器3(USART_CR3)中的HDSEL位",配置串口为"半双工模式"
}
}
//函数工能:使用自动波特率配置
void USART_Automatic_Baudrate_Detection_Config(USART_TypeDef* USARTx, UART_AdvFeatureInitTypeDef *AdvancedInit)
{
/* Check whether the set of advanced features to configure is properly set */
assert_param(IS_UART_ADVFEATURE_INIT(AdvancedInit->AdvFeatureInit));
/* if required, configure auto Baud rate detection scheme */
if (HAL_IS_BIT_SET(AdvancedInit->AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
{
assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(USARTx));
assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(AdvancedInit->AutoBaudRateEnable));
MODIFY_REG(USARTx->CR3, USART_CR3_ABREN, AdvancedInit->AutoBaudRateEnable);
/* set auto Baudrate detection parameters if detection is enabled */
if (AdvancedInit->AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
{
assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(AdvancedInit->AutoBaudRateMode));
MODIFY_REG(USARTx->CR3, USART_CR3_ABRMODE, AdvancedInit->AutoBaudRateMode);
}
}
}
//函数功能:将UART_InitTypeDef型结构变量写入"串口控制寄存器"
void USARTx_SetConfig(USART_TypeDef* USARTx,UART_InitTypeDef *Init,UART_AdvFeatureInitTypeDef *AdvancedInit)
{
uint32_t tmpreg;
uint32_t pclk;
/* Check the parameters */
assert_param(IS_UART_BAUDRATE(Init->BaudRate));
assert_param(IS_UART_STOPBITS(Init->StopBits));
assert_param(IS_UART_PARITY(Init->Parity));
assert_param(IS_UART_MODE(Init->Mode));
_HAL_UART_DISABLE(USARTx);
//将"串口控制寄存器1(USART_CR1)中的UE位"置0,不使能串口
/*-------------------------- USART CR2 Configuration -----------------------*/
/* Configure the UART Stop Bits: Set STOP[13:12] bits
according to Init->StopBits value */
MODIFY_REG(USARTx->CR2, USART_CR2_STOP, Init->StopBits);
//使用Init->StopBits的值修改"串口控制寄存器2(USART_CR2)中的STOP位",用来设置停止位的位数
/*-------------------------- USART CR1 Configuration -----------------------*/
/* Configure the UART Word Length, Parity and mode:
Set the M bits according to Init->WordLength value
Set PCE and PS bits according to Init->Parity value
Set TE and RE bits according to Init->Mode value
Set OVER8 bit according to Init->OverSampling value */
#if defined(USART_CR3_OVER8)
tmpreg = (uint32_t)Init->WordLength | Init->Parity | Init->Mode;
//将"数据长度,奇偶校验,串口发送和接收模式合并为字,准备修改"串口控制寄存器1(USART_CR1)
MODIFY_REG(USARTx->CR1,
(uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
tmpreg);
tmpreg = (uint32_t) Init->OverSampling;//准备修改"串口控制寄存器3(USART_CR3)中的OVER8位"
MODIFY_REG(USARTx->CR3,
(uint32_t)(USART_CR3_OVER8),
tmpreg);
#else
tmpreg = (uint32_t)Init->WordLength | Init->Parity | Init->Mode;
MODIFY_REG(USARTx->CR1,
(uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
tmpreg);
#endif /* USART_CR3_OVER8 */
/*-------------------------- USART CR3 Configuration -----------------------*/
/* Configure the UART HFC: Set CTSE and RTSE bits according to Init->HwFlowCtl value */
MODIFY_REG(USARTx->CR3, (USART_CR3_RTSE | USART_CR3_CTSE), Init->HwFlowCtl);
//修改"串口控制寄存器3中的RTS和CTS位"
#if defined(USART_CR3_OVER8)
/* Check the Over Sampling */
if(Init->OverSampling == UART_OVERSAMPLING_8)
{
/*-------------------------- USART BRR Configuration ---------------------*/
pclk = HAL_RCC_GetPCLK1Freq();//读取PCLK1时钟频率
USARTx->BRR = UART_BRR_SAMPLING8(pclk, Init->BaudRate);
//设置串口波特率
}
else
{
/*-------------------------- USART BRR Configuration ---------------------*/
pclk = HAL_RCC_GetPCLK1Freq();//读取PCLK1时钟频率
USARTx->BRR = UART_BRR_SAMPLING16(pclk, Init->BaudRate);
//设置串口波特率
}
#else
/*-------------------------- USART BRR Configuration ---------------------*/
pclk = HAL_RCC_GetPCLK1Freq();
USARTx->BRR = UART_BRR_SAMPLING16(pclk, Init->BaudRate);
#endif /* USART_CR3_OVER8 */
if( AdvancedInit->AdvFeatureInit != UART_ADVFEATURE_NO_INIT )
{//使用自动波特率配置
USART_Automatic_Baudrate_Detection_Config(USARTx,AdvancedInit);
}
USART_CK_Pin_Config(USARTx,DISABLE);//不使用USARTx_CK引脚配置
USART_Half_Duplex_Config(USARTx,DISABLE);//配置串口为"非半双工模式"
_HAL_UART_ENABLE(USARTx);//使能串口
}
#ifndef __MyUSART_H
#define __MyUSART_H
#include "py32f0xx_hal.h"
#define _HAL_UART_ENABLE(__INSTANCE__) ((__INSTANCE__)->CR1 |= USART_CR1_UE)
//将"串口控制寄存器1(USART_CR1)中的UE位"置1,使能串口
#define _HAL_UART_DISABLE(__INSTANCE__) ((__INSTANCE__)->CR1 &= ~USART_CR1_UE)
//将"串口控制寄存器1(USART_CR1)中的UE位"置0,不使能串口
#define _HAL_UART_ENABLE_IT(__INSTANCE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28U) == UART_CR1_REG_INDEX)? ((__INSTANCE__)->CR1 |= ((__INTERRUPT__) & UART_IT_MASK)): \
(((__INTERRUPT__) >> 28U) == UART_CR2_REG_INDEX)? ((__INSTANCE__)->CR2 |= ((__INTERRUPT__) & UART_IT_MASK)): \
((__INSTANCE__)->CR3 |= ((__INTERRUPT__) & UART_IT_MASK)))
//使能串口中断
#define _UART_DISABLE_IT(__INSTANCE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28U) == UART_CR1_REG_INDEX)? ((__INSTANCE__)->CR1 &= ~((__INTERRUPT__) & UART_IT_MASK)): \
(((__INTERRUPT__) >> 28U) == UART_CR2_REG_INDEX)? ((__INSTANCE__)->CR2 &= ~((__INTERRUPT__) & UART_IT_MASK)): \
((__INSTANCE__)->CR3 &= ~ ((__INTERRUPT__) & UART_IT_MASK)))
//不使能串口中断
extern void USART_ITConfig(USART_TypeDef * USARTx, uint32_t USART_IT, FunctionalState NewState);
extern FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
extern ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint32_t USART_IT);
extern void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint32_t USART_IT);
extern void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);
extern uint16_t USART_ReceiveData(USART_TypeDef* USARTx);
extern void USARTx_SetConfig(USART_TypeDef* USARTx,UART_InitTypeDef *Init,UART_AdvFeatureInitTypeDef *AdvancedInit);
#endif /* __MyUSART_H */
为了实际使用,增加了自己的库函数。