STM32F030C8T6串列埠接收不定長資料
阿新 • • 發佈:2019-01-24
usart.c檔案程式碼。
#include "usart.h"
#include "gpio.h"
UART_HandleTypeDef huart1;
/* USART1 init function */
void MX_USART1_UART_Init(void)
{
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;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT | UART_ADVFEATURE_DMADISABLEONERROR_INIT;
huart1.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;
huart1.AdvancedInit.DMADisableonRxError = UART_ADVFEATURE_DMA_DISABLEONRXERROR;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(uartHandle->Instance == USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* USART1 clock enable */
__HAL_RCC_USART1_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.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 3, 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 */
}
}
/*
** Rewrite fputc function and make printf function work
**/
int fputc(int ch, FILE* file)
{
huart1.Instance->TDR = ch & 0xFF;
while((huart1.Instance->ISR & UART_FLAG_TC) == RESET);
return ch;
}
/*
** Rewrite fgetc function and make scanf function work
**/
int fgetc(FILE* file)
{
while((huart1.Instance->ISR & UART_FLAG_RXNE) == RESET);
return huart1.Instance->RDR;
}
usart.h程式碼:
#ifndef __usart_H
#define __usart_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"
#include <stdbool.h>
/* Log enable/disable switch */
#define ENABLE_LOG 1
#if ENABLE_LOG
#define Log(format, ...) printf(format, ##__VA_ARGS__)
#else
#define Log(format, ...)
#endif
typedef struct Data
{
uint8_t Data[256];
uint16_t Size;
bool ReceiveFlag;
}ReceiveUARTData_t;
void MX_USART1_UART_Init(void);
#ifdef __cplusplus
}
#endif
#endif /*__ usart_H */
中斷函式程式碼:
uint8_t TempData[260] = {0};
uint16_t TempSize = 0;
void USART1_IRQHandler(void)
{
/* Handle read data register not empty interruption */
if((__HAL_UART_GET_IT(&huart1, UART_IT_RXNE) != RESET))
{
__HAL_UART_CLEAR_IT(&huart1, UART_IT_RXNE);
if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET)
{
__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
TempData[TempSize++] = huart1.Instance->RDR;
}
}
/* Handle idle interruption */
if((__HAL_UART_GET_IT(&huart1, UART_IT_IDLE) != RESET))
{
__HAL_UART_CLEAR_IT(&huart1, UART_IT_IDLE);
if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET)
{
__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_IDLE);
UARTData.Size = TempSize;
memcpy(UARTData.Data, TempData, UARTData.Size);
memset(TempData, 0, TempSize);
TempSize = 0;
UARTData.ReceiveFlag = true;
}
}
}
main.c程式碼:
#include "stm32f0xx_hal.h"
#include "usart.h"
#include "gpio.h"
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
ReceiveUARTData_t UARTData= {{0}, 0, false};
void CharPrint(const uint8_t* Data, size_t Size);
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_USART1_UART_Init();
Log("\r\n>>>>>>>>>>>>>>>>>>>> UART data transmit receive test <<<<<<<<<<<<<<<<<<<<\r\n");
while (1)
{
if(UARTData.ReceiveFlag)
{
UARTData.ReceiveFlag = false;
CharPrint(UARTData.Data, UARTData.Size);
memset(UARTData.Data, 0, UARTData.Size);
BluetoothData.Size = 0;
}
}
}
/*
** Brief: Print char data
**/
void CharPrint(const uint8_t* Data, size_t Size)
{
size_t DataIndex = 0;
for(DataIndex = 0; DataIndex < Size; DataIndex++)
{
if(isprint(Data[DataIndex]))
{
Log("%c", Data[DataIndex]);
}
}
}
可以注意到在中斷處理的時候,我採用了雙緩衝機制,保證了超長資料丟失問題,自己測試了可以正常使用。