STM32—串口通信升级版(队列方式)
https://blog.csdn.net/wqx521/article/details/69191025
#ifndef _USART_QUEUE_H_
#define _USART_QUEUE_H_
#include "type.h"
typedef enum
{
USART_QUEUE_EMPTY = 0,
USART_QUEUE_FULL = 1,
USART_QUEUE_OK = 2,
} usart_queue_status_t;
#define USART_QUEUE_SIZE 1024
typedef struct
{
uint16_t front;
uint16_t rear;
uint16_t size;
char data[USART_QUEUE_SIZE];
} usart_queue_t;
extern usart_queue_t usart1_send, usart3_send;
void UsartQueueInit(usart_queue_t *q);
uint8_t UsartQueuePush(usart_queue_t *q, uint8_t data);
uint8_t UsartQueuePop(usart_queue_t *q, uint8_t *data);
#endif /* _USART_QUEUE_H_ */
#include "usart_queue.h"
usart_queue_t usart1_send, usart3_send;
void UsartQueueInit(usart_queue_t *q)
{
q->size = 0;
q->front = 0;
q->rear = 0;
}
uint8_t UsartQueuePush(usart_queue_t *q, uint8_t data)
{
if(((q->rear % USART_QUEUE_SIZE) == q->front) && (q->size == USART_QUEUE_SIZE))
{
return USART_QUEUE_FULL;
}
q->data[q->rear] = data;
q->rear = (q->rear + 1) % USART_QUEUE_SIZE;
q->size++;
return USART_QUEUE_OK;
}
uint8_t UsartQueuePop(usart_queue_t *q, uint8_t *data)
{
if((q->front == q->rear) && (q->size == 0))
{
return USART_QUEUE_EMPTY;
}
*data = q->data[q->front];
q->front = (q->front + 1) % USART_QUEUE_SIZE;
q->size--;
return USART_QUEUE_OK;
}
#ifndef _USART_H_
#define _USART_H_
#include "stm32f10x.h"
#include "type.h"
void UsartInit(USART_TypeDef* usart, uint32_t bound);
void DiagDump(const void *data, uint32_t length);
#endif /* _USART_H_ */
#include "usart.h"
#include "usart_queue.h"
#include "target.h"
#include "stm32f10x_iwdg.h"
#include <stdio.h>
#include <string.h>
#include "debug.h"
int fputc(int ch, FILE *f)
{
while (USART_GetFlagStatus(USART1_CHANNEL, USART_FLAG_TC) == RESET);
USART_SendData(USART1_CHANNEL, (unsigned char)ch);
return ch;
}
static void uart_gpio_init(USART_TypeDef *usart)
{
if(USART1_CHANNEL == usart)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART1_TX_CONFIG();
USART1_RX_CONFIG();
}
if(USART3_CHANNEL == usart)
{
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3 | RCC_APB2Periph_AFIO, ENABLE);
USART3_TX_CONFIG();
USART3_RX_CONFIG();
}
}
static void uart_nvic_init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//===================================================================================
void UsartInit(USART_TypeDef *usart, uint32_t bound)
{
USART_InitTypeDef USART_InitStructure;
uart_gpio_init(usart);
uart_nvic_init();
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(usart, &USART_InitStructure);
USART_ITConfig(usart, USART_IT_RXNE, ENABLE);
USART_Cmd(usart, ENABLE);
}
void DiagDump(const void *data, uint32_t length)
{
const uint8_t *buffer = data;
uint32_t i;
for(i = 0; i < length; ++i)
{
printf("%2x", buffer[i]);
printf(" ");
}
printf("\r\n");
}
void USART1_IRQHandler(void)
{
// static uint16_t usart_rx_status = 0;
uint8_t ret;
// receive interrupt, must be 0x0d 0x0a at the end;
if(USART_GetITStatus(USART1_CHANNEL, USART_IT_RXNE) != RESET)
{
ret = USART_ReceiveData(USART1_CHANNEL); // receive data (a byte)
UsartQueuePush(&usart1_send, ret);
}
}
void USART3_IRQHandler(void)
{
// static uint16_t usart_rx_status = 0;
uint8_t ret;
if(USART_GetITStatus(USART3_CHANNEL, USART_IT_RXNE) != RESET)
{
ret = USART_ReceiveData(USART3_CHANNEL);
UsartQueuePush(&usart3_send, ret);
}
}
#ifndef _DEBUG_H_
#define _DEBUG_H_
#include <stdio.h>
#include "usart.h"
#define CONFIG_UART
#ifndef CONFIG_UART
//#define CONFIG_LED
//#define CONFIG_KEY
//#define CONFIG_ADC
//#define CONFIG_CAN
//#define CONFIG_WATCHDOG
#define CONFIG_W5500
#endif
#ifdef CONFIG_UART
#define debug(fmt, args...) printf("[debug.h] "fmt, ##args)
#define debug_dump(buf, length) DiagDump(buf, length)
#else
#define debug(fmt, args...)
#define debug_dump(buf, length)
#endif /* CONFIG_DEBUG */
#endif /* _DEBUG_H_ */
STM32进阶之串口环形缓冲区实现(好文章)
https://blog.csdn.net/jiejiemcu/article/details/102576473
STM32CUBEIDE 学习笔记(八)FREERTOS+队列+多串口通信+CAN通信+多任务系统优先级设计
STM32CubeMX生成代码学习笔记(四)CAN总线配置及无法进入接收中断问题(已解决)
https://blog.csdn.net/weixin_44087298/article/details/112931851