usart.c file code.
#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 code:
#ifndef __usart_H
#define __usart_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"
#include
/* 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
/*__usart_H */ #endif /*__usart_H */
Interrupt function code:
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 code:
#include "stm32f0xx_hal.h"
#include "usart.h"
#include "gpio.h"
#include
#include
#include
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(sprint(Data[DataIndex]))
{
Log("%c", Data[DataIndex]);
}
}
}
It can be noticed that when processing interrupts, I used a double buffering mechanism to ensure that the problem of excessive data loss is solved. I tested it myself and it works normally.
Previous article:STM32 internal flash operation details that must be known
Next article:STM32F030 cannot download FLASH using IAR+JLINK online debugging
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- I decided to quit my job and develop my operating system full-time (transferred)
- Design rules for four-layer PCB
- Understanding NB-IoT technology
- 3DH Model
- What kind of products need a dedicated shutdown discharge circuit?
- [First Round] Interview Questions for Embedded Engineers
- How to prevent PCB board from bending and warping during reflow oven
- Fully automatic high pressure steam sterilization controller
- ECG ten electrodes and 12 leads
- Why can't the P0 port of STC8A8K32S4A12 output a high level?