You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
277 lines
7.1 KiB
277 lines
7.1 KiB
/**
|
|
******************************************************************************
|
|
* @file usart.c
|
|
* @brief This file provides code for the configuration
|
|
* of the USART instances.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* <h2><center>© Copyright (c) 2022 STMicroelectronics.
|
|
* All rights reserved.</center></h2>
|
|
*
|
|
* This software component is licensed by ST under Ultimate Liberty license
|
|
* SLA0044, the "License"; You may not use this file except in compliance with
|
|
* the License. You may obtain a copy of the License at:
|
|
* www.st.com/SLA0044
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "usart.h"
|
|
|
|
/* USER CODE BEGIN 0 */
|
|
#if EN_USART1_RX //如果使能了接收
|
|
//串口1中断服务程序
|
|
//注意,读取USARTx->SR能避免莫名其妙的错误
|
|
uint8_t USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.
|
|
//接收状态
|
|
//bit15, 接收完成标志
|
|
//bit14, 接收到0x0d
|
|
//bit13~0, 接收到的有效字节数目
|
|
uint16_t USART_RX_STA=0; //接收状态标记
|
|
|
|
uint8_t aRxBuffer[RXBUFFERSIZE];//HAL库使用的串口接收缓冲
|
|
#endif
|
|
|
|
//加入以下代码,支持printf函数,而不需要选择use MicroLIB
|
|
//#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
|
|
#include "stdio.h"
|
|
#if 1
|
|
__asm (".global __use_no_semihosting\n\t");
|
|
//标准库需要的支持函数
|
|
//struct __FILE
|
|
//{
|
|
// int handle;
|
|
//};
|
|
|
|
FILE __stdout;
|
|
//定义_sys_exit()以避免使用半主机模式
|
|
void _sys_exit(int x)
|
|
{
|
|
x = x;
|
|
}
|
|
//重定义fputc函数
|
|
int fputc(int ch, FILE *f)
|
|
{
|
|
USART1->SR;//读USARTx->SR能够避免首字节丢失的情况
|
|
USART1->DR = (uint8_t) ch;
|
|
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
|
|
return 0;
|
|
}
|
|
|
|
/*********************USART Send one byte**********************/
|
|
void USART_SendByte( char Data)
|
|
{
|
|
USART1->SR;//读USARTx->SR能够避免首字节丢失的情况
|
|
USART1->DR = (uint8_t) Data;
|
|
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
|
|
}
|
|
/*********************USART SendString**********************/
|
|
void USART_SendString(char *nbuff)
|
|
{
|
|
while(*nbuff!='\0')
|
|
{
|
|
USART_SendByte(*(nbuff++));
|
|
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
|
|
}
|
|
}
|
|
|
|
#endif
|
|
/* USER CODE END 0 */
|
|
|
|
UART_HandleTypeDef huart1;
|
|
|
|
/* USART1 init function */
|
|
|
|
void MX_USART1_UART_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN USART1_Init 0 */
|
|
|
|
/* USER CODE END USART1_Init 0 */
|
|
|
|
/* USER CODE BEGIN USART1_Init 1 */
|
|
|
|
/* USER CODE END USART1_Init 1 */
|
|
huart1.Instance = USART1;
|
|
huart1.Init.BaudRate = 115200;
|
|
huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
|
huart1.Init.StopBits = UART_STOPBITS_1;
|
|
huart1.Init.Parity = UART_PARITY_NONE;
|
|
huart1.Init.Mode = UART_MODE_TX_RX;
|
|
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
if (HAL_UART_Init(&huart1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN USART1_Init 2 */
|
|
HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE);//该函数会开启接收中断:标志位UART_IT_RXNE,并且设置接收缓冲以及接收缓冲接收最大数据量
|
|
/* USER CODE END USART1_Init 2 */
|
|
|
|
}
|
|
|
|
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
|
{
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
if(uartHandle->Instance==USART1)
|
|
{
|
|
/* USER CODE BEGIN USART1_MspInit 0 */
|
|
|
|
/* USER CODE END USART1_MspInit 0 */
|
|
/* USART1 clock enable */
|
|
__HAL_RCC_USART1_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
/**USART1 GPIO Configuration
|
|
PA9 ------> USART1_TX
|
|
PA10 ------> USART1_RX
|
|
*/
|
|
GPIO_InitStruct.Pin = GPIO_PIN_9;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_10;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/* USART1 interrupt Init */
|
|
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
|
|
HAL_NVIC_EnableIRQ(USART1_IRQn);
|
|
/* USER CODE BEGIN USART1_MspInit 1 */
|
|
|
|
/* USER CODE END USART1_MspInit 1 */
|
|
}
|
|
}
|
|
|
|
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
|
|
{
|
|
|
|
if(uartHandle->Instance==USART1)
|
|
{
|
|
/* USER CODE BEGIN USART1_MspDeInit 0 */
|
|
|
|
/* USER CODE END USART1_MspDeInit 0 */
|
|
/* Peripheral clock disable */
|
|
__HAL_RCC_USART1_CLK_DISABLE();
|
|
|
|
/**USART1 GPIO Configuration
|
|
PA9 ------> USART1_TX
|
|
PA10 ------> USART1_RX
|
|
*/
|
|
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
|
|
|
|
/* USART1 interrupt Deinit */
|
|
HAL_NVIC_DisableIRQ(USART1_IRQn);
|
|
/* USER CODE BEGIN USART1_MspDeInit 1 */
|
|
|
|
/* USER CODE END USART1_MspDeInit 1 */
|
|
}
|
|
}
|
|
|
|
/* USER CODE BEGIN 1 */
|
|
|
|
#if HAL_USART_ISR_EN //是否使用HAL库自带的串口接收中断函数
|
|
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
{
|
|
if(huart->Instance==USART1)//如果是串口1
|
|
{
|
|
if((USART_RX_STA&0x8000)==0)//接收未完成
|
|
{
|
|
if(USART_RX_STA&0x4000)//接收到了0x0d
|
|
{
|
|
if(aRxBuffer[0]!=0x0a)USART_RX_STA=0;//接收错误,重新开始
|
|
else USART_RX_STA|=0x8000; //接收完成了
|
|
}
|
|
else //还没收到0X0D
|
|
{
|
|
if(aRxBuffer[0]==0x0d)USART_RX_STA|=0x4000;
|
|
else
|
|
{
|
|
USART_RX_BUF[USART_RX_STA&0X3FFF]=aRxBuffer[0] ;
|
|
USART_RX_STA++;
|
|
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
//串口1中断服务程序
|
|
//使用此函数需要屏蔽HAL库自带的中断服务子函数stm32f1xx_it.c
|
|
void USART1_IRQHandler(void)
|
|
{
|
|
uint32_t timeout=0;
|
|
uint32_t maxDelay=0x1FFFF;
|
|
#if SYSTEM_SUPPORT_OS //使用OS
|
|
OSIntEnter();
|
|
#endif
|
|
|
|
HAL_UART_IRQHandler(&huart1); //调用HAL库中断处理公用函数
|
|
|
|
timeout=0;
|
|
while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)//等待就绪
|
|
{
|
|
timeout++;////超时处理
|
|
if(timeout>maxDelay) break;
|
|
}
|
|
|
|
timeout=0;
|
|
while(HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)//一次处理完成之后,重新开启中断并设置RxXferCount为1
|
|
{
|
|
timeout++; //超时处理
|
|
if(timeout>maxDelay) break;
|
|
}
|
|
#if SYSTEM_SUPPORT_OS //使用OS
|
|
OSIntExit();
|
|
#endif
|
|
}
|
|
#else
|
|
/*下面代码我们直接把中断控制逻辑写在中断服务函数内部。
|
|
说明:采用HAL库处理逻辑,效率不高。
|
|
使用此函数需要屏蔽HAL库自带的中断服务子函数stm32f1xx_it.c
|
|
*/
|
|
//串口1中断服务程序
|
|
void USART1_IRQHandler(void)
|
|
{
|
|
uint8_t Res;
|
|
#if SYSTEM_SUPPORT_OS //使用OS
|
|
OSIntEnter();
|
|
#endif
|
|
if((__HAL_UART_GET_FLAG(&huart1,UART_FLAG_RXNE)!=RESET)) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
|
|
{
|
|
Res=USART1->DR;
|
|
if((USART_RX_STA&0x8000)==0)//接收未完成
|
|
{
|
|
if(USART_RX_STA&0x4000)//接收到了0x0d
|
|
{
|
|
if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始
|
|
else USART_RX_STA|=0x8000; //接收完成了
|
|
}
|
|
else //还没收到0X0D
|
|
{
|
|
if(Res==0x0d)USART_RX_STA|=0x4000;
|
|
else
|
|
{
|
|
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
|
|
USART_RX_STA++;
|
|
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
HAL_UART_IRQHandler(&huart1);
|
|
#if SYSTEM_SUPPORT_OS //使用OS
|
|
OSIntExit();
|
|
#endif
|
|
}
|
|
#endif
|
|
/* USER CODE END 1 */
|
|
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
|
|