STM32标准库 UART FIFO收发例程
时间: 2023-08-25 10:06:15 浏览: 130
STM32实现串口转发功能,使用UART DMA方式不定长接收,FIFO接收与发送,数据零拷贝
好的,以下是一个基于STM32标准库的UART FIFO收发例程:
```c
#include "stm32f4xx.h"
#include <string.h>
#define UART_BUFFER_SIZE 128
uint8_t uart_rx_buffer[UART_BUFFER_SIZE];
uint8_t uart_tx_buffer[UART_BUFFER_SIZE];
uint8_t uart_rx_data = 0;
uint8_t uart_rx_data_available = 0;
void USART2_IRQHandler(void)
{
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
// Check if receive buffer is full
if (uart_rx_data_available < UART_BUFFER_SIZE)
{
uart_rx_buffer[uart_rx_data_available++] = USART_ReceiveData(USART2);
}
else
{
uart_rx_data_available = 0;
}
}
if (USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
{
if (uart_tx_buffer[0] != 0)
{
USART_SendData(USART2, uart_tx_buffer[0]);
for (uint8_t i = 0; i < UART_BUFFER_SIZE - 1; i++)
{
uart_tx_buffer[i] = uart_tx_buffer[i + 1];
}
uart_tx_buffer[UART_BUFFER_SIZE - 1] = 0;
}
else
{
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
}
}
}
void UART_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART2, ENABLE);
}
void UART_Send(const uint8_t *data, uint16_t size)
{
if (size > UART_BUFFER_SIZE)
{
size = UART_BUFFER_SIZE;
}
memcpy(uart_tx_buffer, data, size);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
}
uint8_t UART_Read()
{
if (uart_rx_data_available == 0)
{
return 0;
}
uint8_t data = uart_rx_buffer[0];
uart_rx_data_available--;
for (uint8_t i = 0; i < uart_rx_data_available; i++)
{
uart_rx_buffer[i] = uart_rx_buffer[i + 1];
}
return data;
}
void UART_ReadBuffer(uint8_t *buffer, uint16_t size)
{
if (uart_rx_data_available < size)
{
size = uart_rx_data_available;
}
for (uint16_t i = 0; i < size; i++)
{
buffer[i] = uart_rx_buffer[i];
}
uart_rx_data_available -= size;
for (uint16_t i = 0; i < uart_rx_data_available; i++)
{
uart_rx_buffer[i] = uart_rx_buffer[i + size];
}
}
int main(void)
{
UART_Init();
while (1)
{
uint8_t data = UART_Read();
if (data != 0)
{
// Handle received data
}
// Do other stuff
}
}
```
这个例程同样使用了UART2,可以根据需要修改。数据接收和发送都使用了中断,相对于轮询模式,可以更快地响应数据,不会占用过多CPU时间。UART_Send函数使用了memcpy函数,也可以使用for循环逐个拷贝实现,但是memcpy函数更加高效。
阅读全文