Appearance
43 结课项目 · STM32 串口中断接收与故障观察
沿着 USART2_IRQHandler → rx_push → rx_pop → decode 阅读。复用第 28 题的队列思想和第 42 题的校验协议。
源码
main.c
c
#include "stm32f407xx.h"
#include "rx.h"
static volatile RxQueue queue;
static volatile Decoder decoder;
static volatile unsigned uart_errors;
// 独立固件从复位默认时钟启动,PCLK1 为 16 MHz。
static void uart_init(void)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
(void)RCC->AHB1ENR;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
(void)RCC->APB1ENR;
GPIOA->MODER = (GPIOA->MODER & ~(3u << 6)) | (2u << 6);
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(15u << 12)) | (7u << 12);
GPIOA->PUPDR = (GPIOA->PUPDR & ~(3u << 6)) | (1u << 6);
USART2->CR1 = 0;
USART2->CR2 = 0;
USART2->CR3 = 0;
USART2->BRR = (16000000u + 115200u / 2u) / 115200u;
USART2->CR1 = USART_CR1_UE | USART_CR1_RE | USART_CR1_RXNEIE;
NVIC_EnableIRQ(USART2_IRQn);
}
void USART2_IRQHandler(void)
{
// 1. 先查状态再读数据,读取顺序同时用于清除接收错误。
uint32_t status = USART2->SR;
const uint32_t errors = USART_SR_ORE | USART_SR_FE | USART_SR_NE | USART_SR_PE;
if ((status & (errors | USART_SR_RXNE)) == 0u) return;
uint8_t byte = (uint8_t)USART2->DR;
// 2. 错误字节不入队,只记录缺口。下一次成功入队把缺口交给主循环。
if ((status & errors) != 0u) {
++uart_errors;
queue.gap_pending = true;
return;
}
// 3. 把有效字节送入队列。队列满时记录丢弃,ISR 随即返回。
rx_push(&queue, byte);
}
int main(void)
{
// 1. 初始化 USART2 和 NVIC,之后由 USART2_IRQHandler() 往队列交付字节。
uart_init();
for (;;) {
// 2. 暂停中断,原子地取走一个队列元素。恢复中断后仍可接收后续字节。
RxByte byte;
uint32_t mask = __get_PRIMASK();
__disable_irq();
bool ready = rx_pop(&queue, &byte);
__set_PRIMASK(mask);
// 3. 解码器只处理本地副本。缺口标记会让解码器丢弃上一段残帧。
if (ready) decode(&decoder, byte);
}
}rx.h
c
#ifndef RX_PIPELINE_H
#define RX_PIPELINE_H
#include <stdbool.h>
#include <stdint.h>
enum { RX_SLOTS = 16 };
typedef struct {
uint8_t value;
bool gap_before;
} RxByte;
typedef struct {
RxByte slots[RX_SLOTS];
unsigned read, write, depth, peak, dropped;
bool gap_pending;
} RxQueue;
typedef enum { WAIT_HEADER, WAIT_DATA, WAIT_CHECK } Phase;
typedef struct {
Phase phase;
uint8_t pending, last;
unsigned good, bad, gaps;
} Decoder;
/* Queue operations require a single context or external exclusion. */
void rx_push(volatile RxQueue *q, uint8_t value);
bool rx_pop(volatile RxQueue *q, RxByte *out);
void decode(volatile Decoder *d, RxByte byte);
#endifrx.c
c
#include "rx.h"
void rx_push(volatile RxQueue *q, uint8_t value)
{
if (q->depth == RX_SLOTS) {
++q->dropped;
q->gap_pending = true;
return;
}
q->slots[q->write].value = value;
q->slots[q->write].gap_before = q->gap_pending;
q->gap_pending = false;
q->write = (q->write + 1u) % RX_SLOTS;
++q->depth;
if (q->depth > q->peak) q->peak = q->depth;
}
bool rx_pop(volatile RxQueue *q, RxByte *out)
{
if (q->depth == 0) return false;
*out = q->slots[q->read];
q->read = (q->read + 1u) % RX_SLOTS;
--q->depth;
return true;
}
void decode(volatile Decoder *d, RxByte byte)
{
if (byte.gap_before) {
d->phase = WAIT_HEADER;
++d->gaps;
}
switch (d->phase) {
case WAIT_HEADER:
if (byte.value == 0xA5) d->phase = WAIT_DATA;
break;
case WAIT_DATA:
d->pending = byte.value;
d->phase = WAIT_CHECK;
break;
case WAIT_CHECK: {
uint8_t check = (uint8_t)(d->pending ^ 0xFFu);
if (byte.value == check) {
d->last = d->pending;
++d->good;
} else {
++d->bad;
}
d->phase = WAIT_HEADER;
break;
}
}
}host_main.c
c
#include "rx.h"
#include <stdio.h>
static void drain(RxQueue *queue, Decoder *decoder)
{
RxByte byte;
while (rx_pop(queue, &byte)) decode(decoder, byte);
}
int main(void)
{
RxQueue queue = {0};
Decoder decoder = {0};
uint8_t input[] = {0, 0xA5, 0x2A, 0xD5, 0xA5, 1, 0};
for (size_t i = 0; i < sizeof input; ++i) rx_push(&queue, input[i]);
drain(&queue, &decoder);
printf("good=%u bad=%u last=%u\n", decoder.good, decoder.bad, (unsigned)decoder.last);
for (unsigned i = 0; i < RX_SLOTS - 1u; ++i) rx_push(&queue, 0);
rx_push(&queue, 0xA5);
rx_push(&queue, 0x11); /* full, simulate a lost byte */
drain(&queue, &decoder);
rx_push(&queue, 0xA5);
rx_push(&queue, 0x07);
rx_push(&queue, 0xF8);
drain(&queue, &decoder);
printf("good=%u bad=%u last=%u peak=%u dropped=%u gaps=%u\n",
decoder.good, decoder.bad, (unsigned)decoder.last,
queue.peak, queue.dropped, decoder.gaps);
return 0;
}主机运行
在仓库根目录执行。Windows 可以将输出名写成 reading-43.exe,再从终端运行。
sh
gcc -std=c11 -Wall -Wextra -pedantic examples/c-reading/43/host_main.c examples/c-reading/43/rx.c -o reading-43
./reading-43先沿 main() 预测打印结果,再展开核对。
主机输出与手推
text
good=1 bad=1 last=42
good=2 bad=1 last=7 peak=16 dropped=1 gaps=1主机先送一帧正确数据和一帧坏数据。第二轮故意让队列装满并丢掉一个字节,下次入队带上缺口标记,解码器从新帧头恢复。
练习
画出 ISR 与主循环各自访问的字段,解释为什么仅在出队时进入临界区。再尝试分批发送和错误校验字节。
STM32 接线与构建
使用天空星 STM32F407VET6。3.3 V USB 转串口的 TX 接 PA3(USART2_RX,AF7),两端共地。串口设为 115200、8N1,以十六进制发送。固件只接收,已有模板中的 USART1 示例使用 PA9/PA10。
准备 GNU Arm、CMake、Ninja 和 STM32CubeF4 V1.28.3。把固件包路径替换为本机位置。
sh
cmake -S examples/c-reading/43 -B .work/sky-c-reading-43 -G Ninja -DCUBE_F4_ROOT="D:/SDK/STM32Cube_FW_F4_V1.28.3"
cmake --build .work/sky-c-reading-43复位后以十六进制发送 00 A5 2A D5 A5 01 00。在调试器中预期看到 decoder.good=1、decoder.bad=1 和 decoder.last=42。队列峰值 queue.peak 随实际时序变化。主机版可确定性复现队列溢出,queue.dropped 记录入队失败,uart_errors 记录串口外设错误。
芯片资料见 STM32F407 数据手册 和 RM0090 参考手册。
关联模板
验证记录
主机版使用 GCC 编译运行,输出与跟读材料逐项核对。 板端已构建 STM32F407 ELF,实板接线和收发结果待验证。