#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 #include #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("rn"); } 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 #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_ */
上一篇:STM32F10x 学习笔记之USART实现串口通讯
下一篇:STM32的Flash缓冲区
推荐阅读最新更新时间:2024-11-13 08:40
设计资源 培训 开发板 精华推荐
- HV9930DB1,满足汽车要求的高亮度 LED 驱动器演示板
- NCP459FCT2GEVB:负载开关,4 A 评估板
- 具有附加迟滞的 MC34164 低压检测微处理器复位的典型应用电路
- Blackfin DSP 的高性能数字 MEMS 麦克风标准数字音频应用电路接口
- 具有集成 650V CoolMOS 和启动单元的 ICE3A2065ELJ 离线 SMPS 电流模式控制器的典型应用电路
- LT6656BIDC-2.048、2.048V 升压型输出电流电压基准的典型应用
- 11自由度机械手驱动+力反馈
- 基于AS5147P的ODrive配套磁编码器_pro
- AD8032ARM-EBZ,采用 8 引脚 SOIC 封装的双通道高速运算放大器评估板
- 具有迟滞功能的 NCV2904DR2G 比较器的典型应用