嵌入式串口屏通信框架设计 状态机DMA与变量映射表的工程实践Embedded Serial Display Communication Framework: State Machine, DMA and Variable Mapping Table in Practice


AI free token/AI免费积分

嵌入式串口屏通信框架设计 状态机DMA与变量映射表的工程实践

本文来源:http://www.lpqz.cn/news/161721
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

做嵌入式这么多年串口屏项目我接过不下二十个从最早的迪文 DGUS 用到后来的淘晶驰、大彩踩坑踩到最后问题基本都集中在同一个地方不是屏不会用而是主控端和串口屏之间的通信代码越写越乱换个屏、换个项目就要推倒重来。后来我花几个周末把这些收发逻辑抽出来整理成一套可复用的 HMI串口屏框架才发现原来这活可以干得这么清爽。这篇东西不聊屏厂官方的点灯教程聊的是屏之外的那层“壳”主控端怎么设计通信协议层、怎么把串口收包做成状态机、怎么把迪文和淘晶驰的指令差异关在同一个接口后面以及框架里每一块代码到底是为什么存在的。适合正在用 STM32 等 MCU 接串口屏做项目的嵌入式工程师也适合刚接触 HMI 串口屏、想从一开始就把代码组织得有条理的初学者。我把这套框架从设计思路到落地代码完整拆给你看你照着搭一遍后面再做串口屏项目会顺非常多。1. 为什么串口屏项目需要一套框架1.1 裸调串口屏的三宗罪我见过太多项目代

码里满屏都是HAL_UART_Transmit(huart1, buf, len, 100);后面跟着一堆魔法数0x5A 0xA5 0x05 0x82之类注释也不写读起来全靠猜。这种写法的第一个问题是不可读。两个月后你回来看代码根本不知道 0x0082 这个地址对应的是温度显示还是转速表盘改一个数值变量得把整个文件翻个底朝天。第二个问题是不可靠。串口是异步的屏不是所有时候都等着回你数据裸调的时候经常遇到“发过去没回应、读回来的数据对不上”的情况一旦没有超时重传机制整个系统就僵在那里等看门狗咬人。第三个问题是不可换。今天用迪文明天客户指定淘晶驰指令集完全不一样业务代码全得跟着改几千行的改动成本没人想背。这三个问题本质上不是串口屏本身的毛病而是主控端缺少一层统一的软件抽象。你仔细观察就会发现串口屏的协议虽然各家不同但做的事情高度相似都是“我要往某个控件或地址写什么数据”“我想知道某个变量的当前

值”“用户按了哪个按钮需要触发什么动作”。把这些共性抽出来就是框架的第一动力。很多朋友觉得串口屏开发就是发指令、收指令没必要小题大做但真等你接手一个几千行的裸调工程时你就会明白当初省下的那点设计时间后面都会加倍还回去。1.2 框架到底管了哪些事我设计的这套框架职责分三层。最底层是硬件驱动层管的是串口外设的初始化、DMA 收发、环形缓冲这一层跟屏没关系换个屏这层一行都不用动中间是协议层负责组指令、解析应答、校验拆包这一层跟屏强相关但改动被严格限制在一个文件里最上面是应用层业务代码只跟“变量表”打交道比如hmi_write_u16(温度, 36)至于这条指令是5A A5 05 82开头还是淘晶驰的print开头应用层完全不用关心。这样分层的价值在于每一层都可以单独测试。我用串口调试助手先验协议层再把协议层和硬件层连起来跑回环最后才接真屏调应用。不要把屏一插上就开始写业务代码那是赌运气。真正需要

花心思打磨的是中间那一层也就是帧的设计与解析。帧不隔层、不越层这一层做稳了上层加多少页面、多少个变量都不会心虚。反过来说如果这一层写得稀烂后面每加一个功能都要重新跟协议搏斗一遍项目越做越痛苦。2. 框架的核心模块与通信协议设计2.1 串口底层的环形缓冲与DMA收包串口屏的返回数据不是连续的屏收到指令后隔几毫秒才回一帧中间还夹杂着触摸事件主动上报的数据。如果每收到一个字节就调用一次回调会把上层逻辑活活打断。所以我第一件事就是做一个环形缓冲区配合串口空闲中断或者 DMA 接收把数据先囤下来在主循环里统一消化。以 STM32 HAL 库为例我一般用HAL_UARTEx_ReceiveToIdle_DMA让硬件在串口空闲时自己产生中断DMA 把一串数据搬到内存里然后在中断回调里把数据写进环形缓冲。代码大致长这样#define RING_BUF_SIZE 256 typedef struct { u

rb-tail]; rb-tail (rb-tail 1) % RING_BUF_SIZE; } return cnt; }环形缓冲区的大小要按最大报文长度算我一般取最大帧长的 2 到 3 倍。迪文 DGUS 最长一条读指令回包大概十几个字节淘晶驰的文本回包可能到几十个字节256 字节足够用。如果项目里既有屏又有别的设备共用一块缓冲区那就再往上提别省这点内存。处理这个模块时还要注意 head 和 tail 必须加volatile修饰因为中断和主循环会同时访问这两个变量。提示DMA 接收模式一定要处理“数据恰好填满缓冲区”的边界情况否则 DMA 不会再触发空闲中断后续数据全部卡死。我的做法是在 DMA 传输完成回调里再手动调用一次接收函数让它继续候着这样既能处理满缓冲也不影响空闲中断的触发。2.2 报文解析状态机解决粘包和半包串口数据流是没边界的你无法保证一次收到一整帧。可能是半包可能是

两帧粘在一起还有可能中间混进屏端主动上报的乱序数据。所以协议层至少需要两种解析方式按定长时间切片或者按帧头结尾做状态机。我更推荐后者稳定性更好尤其是屏端指令带\xff\xff\xff这种特殊结尾时状态机远比“等 50ms 再读缓冲区”靠谱。我习惯用一个简单的四态机找帧头、读长度、读数据、校验。以迪文 DGUS 指令为例帧头是5A A5紧接着是数据长度后面是指令码和数据校验可选。实现如下typedef enum { ST_IDLE, ST_HEADER1, ST_HEADER2, ST_LEN, ST_DATA, ST_TAIL } frame_st_t; static uint8_t parse_state ST_IDLE; static uint8_t rx_buf[128]; static uint16_t rx_len 0; static uint16_t rx_cnt 0;

ase ST_TAIL: hmi_on_frame(rx_buf, rx_len); // 交给上层处理 parse_state ST_IDLE; break; default: parse_state ST_IDLE; break; } }注意几个细节。一是状态机的每个分支都必须有break否则状态会串这是新手最容易犯的错。二是缓冲区溢出要有保护代码里rx_cnt sizeof(rx_buf)就是为了防止屏端发来一条超长数据把内存写爆。三是收到完整帧后要立即把状态恢复到ST_IDLE不然下一帧的第一个字节会被当成当前帧的尾巴丢掉。四是不要在任何中断里直接调用hmi_on_frame做业务处理只负责把事件记下来具体逻辑放到主循环里跑否则中断反复嵌套很容易搞出死锁。2.3 指令封装层与品牌差异隔离状态机接收解决的是“怎么收”指令封装解决的是“怎么发”。这一层我给它起了个名叫hmi_po

rt里面只暴露四个接口写变量、读变量、刷新页面、发送原始帧。不同品牌的屏在这个文件里各自实现一份编译时用条件编译或者函数指针切换。比如迪文 DGUS 写一个 16 位变量协议是5A A5 05 82 地址高 地址低 数据高 数据低。淘晶驰则是通过串口发print 温度,n0加三个结束字节。大彩是AA 设备地址 长度 指令 数据 校验。这几套东西如果散落在业务代码里项目基本没法维护。放进hmi_port后业务层只调hmi_write_u16()换屏等于换一个.c文件这个收益是立竿见影的。有个细节值得重点说指令的“确认”机制。屏不是指令收发机有些指令发出去屏并不应答有些则回 ACK。我的框架里把指令分成两类一类是“发完即走”的比如清屏、切页发完不用管另一类是“必须等到应答”的比如读变量。第二类指令要挂进发送队列带超时和重试超过 100ms 没等到就重发重发三次仍无回应就报错给应用层。这套机制看

着简单实际工程里能救你很多次尤其是屏固件版本不同导致个别指令没响应的时候有了超时重传系统至少不会永久卡死。2.4 变量映射表让业务代码不碰协议协议层搞定之后应用层还面临一个问题业务代码怎么知道“温度”这个逻辑量和屏上地址 0x0082 的对应关系我在这里用了一张静态配置表typedef struct { uint16_t addr; uint8_t type; // HMI_VAR_U8 / HMI_VAR_U16 / HMI_VAR_STR uint16_t max_len; } hmi_var_desc_t; static const hmi_var_desc_t var_tbl[] { { 0x0082, HMI_VAR_U16, 2 }, // 温度 { 0x0084, HMI_VAR_U16, 2 }, // 转速 { 0x0090, HMI_VAR_STR, 16 }, //

设备编号 };应用层写值的时候先按逻辑名查表拿到地址和类型再统一走hmi_port_write()。这样以后屏端工程里挪了变量地址只需要改这一张表业务代码一行不动。常见做法是把地址定义放到一个单独的头文件里或者干脆做成数组配合上位机工具批量生成。我在项目里就是拿 Excel 维护这份映射表再用脚本转成 C 数组非常省心变量多了以后这个优势尤其明显。变量映射表还有一个隐藏的好处它天然就成了项目文档。新人接手时不用去翻几百页的屏端组态截图看这张表就知道哪些变量在通信、什么类型、什么用途。我甚至会在表里加一列备注把协议指令样例也写进去团队协作的时候效率提升非常明显。3. 基于STM32的框架集成实操3.1 硬件连接与串口参数先泼一盆冷水很多人第一步就翻车不是因为代码而是因为电平。串口屏的串口分 TTL、RS232、RS485 三种STM32 的 UART 引脚是 3.3V TTL 电平。如果你

的屏是 RS232 接口直接拿杜邦线连 STM32 的 PA9、PA10多半不是乱码就是烧东西。我做项目前会先查屏的规格书确定电平是 TTL 还是需要外接 RS232 转换芯片再决定接线。TTL 屏要注意 TX 接 RX、RX 接 TX两边必须共地不然参考电平不一致数据收不回来。波特率我统一用 1152008 数据位、无校验、1 停止位也就是常说的 8N1。有些老式屏默认是 9600待机功耗低的场合 9600 也够用但项目维护周期长的话建议全部在屏端工程里设成 115200主控端和屏端保持严格一致。这里有个很容易踩的坑改完屏端波特率后屏要重新上电才生效不是点一下保存就行。我吃过一次亏改完设置以后直接发指令屏毫无反应排查了半天才发现是屏没重启。3.2 框架代码结构我的工程里与串口屏相关的文件固定是五个按模块分得清清楚楚文件职责ringbuf.c/h环形缓冲区管串口数据暂存hmi_uart.c

/hSTM32 串口外设初始化与 DMA 收发hmi_protocol.c/h状态机解析、帧校验、粘包拆包hmi_port.c/h品牌指令封装对外统一接口hmi_app.c/h变量表、业务回调、周期刷新任务为什么这样拆因为每个文件都能单独测试。ringbuf不依赖任何硬件可以在 PC 上跑单元测试hmi_protocol只需要喂字节流用串口助手也能验证真正绑定 STM32 的只有hmi_uart一个文件。你把硬件相关的东西压缩到最小范围将来移植到别的 MCU 或者从裸机换 RTOS成本都极低。我刚把框架从 STM32F103 搬到 GD32F407 的时候只改了hmi_uart.c其余四个文件一行没动两天完成移植。3.3 核心代码实现以 STM32 HAL 为例串口初始化这样写void hmi_uart_init(void) { MX_USART1_UART_Init(); // 1152

00 8N1 HAL_UARTEx_ReceiveToIdle_DMA(huart1, dma_buf, DMA_BUF_LEN); } void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t size) { if (huart-Instance USART1) { ringbuf_push(rb, dma_buf, size); // 重新开启 DMA 接收循环待命 HAL_UARTEx_ReceiveToIdle_DMA(huart1, dma_buf, DMA_BUF_LEN); } }主循环里再跑一个轻量任务把缓冲区里的字节一个个喂给状态机void hmi_task(void) { uint8_t byte; while (ringbuf_pop(rb, byte, 1)) { hmi_protoco

Embedded Serial Display Communication Framework: State Machine, DMA and Variable Mapping Table in Practice

本文来源:http://www.lpqz.cn/news/161721
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

Over the years I've worked on no fewer than twenty serial-screen (UART HMI) projects, from the early DWIN DGUS displays to later ones from Taojingchi and Dacai. After stepping into pitfall after pitfall, I found the problems almost always converged in the same place: it wasn't that the screen was hard to use, but that the communication code between the host MCU and the serial screen got messier the more you wrote it. Switch to a different screen or a different project and you had to tear everything down and start over. Later I spent a few weekends extracting that send/receive logic and organizing it into a reusable HMI serial-screen framework — only then did I realize this job could be done so cleanly. This article isn't about the vendor's official "blink an LED" tutorials; it's about the "shell" outside the screen: how the host side designs a communication protocol layer, how to turn serial packet reception into a state machine, how to hide the command-set differences between DWIN and Taojingchi behind a single interface, and why every piece of code in the framework exists. It's for embedded engineers currently using an STM32 or other MCU to drive a serial screen, and for beginners new to HMI serial screens who want to organize their code properly from day one. I'll break the whole framework down for you, from design thinking to working code; follow along and rebuild it yourself, and your future serial-screen projects will go far more smoothly. 1. Why serial-screen projects need a framework 1.1 The three cardinal sins of bare-metal serial-screen calls I've seen far too many projects whose code

is littered with HAL_UART_Transmit(huart1, buf, len, 100); followed by a bunch of magic numbers like 0x5A 0xA5 0x05 0x82, with no comments — you have to guess your way through it. The first problem with this style is readability. Come back two months later and you won't have a clue whether address 0x0082 is the temperature display or the RPM gauge; changing one numeric value means turning the whole file upside down. The second problem is reliability. UART is asynchronous and the screen isn't always waiting to reply, so with bare calls you constantly hit situations like "sent it, no response" or "the data I read back doesn't match." Once there's no timeout-and-retry mechanism, the whole system just freezes there waiting for the watchdog to bite. The third problem is swap-ability. Use DWIN today and tomorrow the client specifies Taojingchi, whose command set is completely different — all the application code has to change with it, and nobody wants to carry the cost of changing thousands of lines. Fundamentally, these three issues aren't defects of the serial screen itself; they come from the host side lacking a unified software abstraction. Look closely and you'll see that although serial-screen protocols differ from vendor to vendor, what they do is highly similar: "I want to write some data to a certain widget or address," "I want to know the current

value," and "which button did the user press and what action should it trigger." Extracting these commonalities is the first motivation for a framework. Many people think serial-screen development is just sending and receiving commands, no need to make a mountain out of a molehill — but when you actually take over a few-thousand-line bare-call project, you'll understand that the design time you saved back then gets paid back with interest later. 1.2 What exactly does the framework handle? The framework I designed has three layers of responsibility. The bottom layer is the hardware driver layer, handling initialization of the UART peripheral, DMA transmit/receive, and the ring buffer; this layer has nothing to do with the screen — swap the screen and you don't touch a single line here. In the middle is the protocol layer, responsible for building commands, parsing responses, checksumming, and splitting packets; this layer is tightly coupled to the screen, but changes are strictly confined to one file. On top is the application layer, where business code only deals with the "variable table" — for example hmi_write_u16(temperature, 36). Whether that command starts with 5A A5 05 82 or with Taojingchi's print, the application layer doesn't care at all. The value of this layering is that every layer can be tested independently. I first validate the protocol layer with a serial-port debug assistant, then connect the protocol layer to the hardware layer and run a loopback, and only at the end hook up the real screen to tune the application. Don't start writing business code the moment you plug the screen in — that's gambling on luck. What really needs

care and polishing is the middle layer — the design and parsing of frames. Frames must not leak across layers or jump layers; once this layer is solid, you can add as many pages and variables as you like to the upper layers without a guilty conscience. Conversely, if this layer is written badly, every new feature later means another bout with the protocol, and the project gets more painful as it grows. 2. Core modules of the framework and communication protocol design 2.1 Ring buffer and DMA packet reception at the serial bottom layer A serial screen's return data isn't continuous: after receiving a command, the screen waits a few milliseconds before replying with a frame, and in between there may be unsolicited touch-event reports mixed in. If you invoke a callback on every byte received, you'll hammer the upper-layer logic to death. So the first thing I do is build a ring buffer, paired with the UART idle interrupt or DMA receive, to stage the data first and consume it uniformly in the main loop. Taking the STM32 HAL library as an example, I usually use HAL_UARTEx_ReceiveToIdle_DMA so the hardware generates an interrupt when the line goes idle, DMA moves a batch of data into memory, and then the interrupt callback writes the data into the ring buffer. The code roughly looks like this: #define RING_BUF_SIZE 256 typedef struct { u

rb-tail]; rb-tail = (rb-tail + 1) % RING_BUF_SIZE; } return cnt; } The ring buffer size should be calculated from the maximum message length; I generally take two to three times the maximum frame length. The longest read-command reply from DWIN DGUS is roughly a dozen bytes, and a Taojingchi text reply might reach a few dozen bytes, so 256 bytes is plenty. If the screen shares one buffer with other devices in the project, increase it further — don't skimp on this bit of memory. Another thing to watch in this module: head and tail must be declared volatile, because the interrupt and the main loop access both variables concurrently. Tip: in DMA receive mode you must handle the edge case where data exactly fills the buffer; otherwise DMA won't trigger the idle interrupt again and all subsequent data gets stuck. My approach is to manually call the receive function once more in the DMA transfer-complete callback so it keeps waiting — this handles a full buffer without affecting idle-interrupt triggering. 2.2 A packet-parsing state machine to solve stuck and partial packets Serial data has no boundaries; you can't guarantee receiving a whole frame in one go. It might be a partial frame, or

two frames stuck together, or even out-of-order data from unsolicited screen reports mixed in between. So the protocol layer needs at least two parsing approaches: slicing by a fixed timeout, or a state machine keyed on frame header and terminator. I prefer the latter — it's more robust, especially when the screen's commands end with a special sequence like \xff\xff\xff, where a state machine is far more dependable than "wait 50 ms then read the buffer." I like to use a simple four-state machine: find frame header, read length, read data, verify checksum. Taking a DWIN DGUS command as an example, the header is 5A A5, followed by the data length, then the command code and data, with an optional checksum. The implementation looks like this: typedef enum { ST_IDLE, ST_HEADER1, ST_HEADER2, ST_LEN, ST_DATA, ST_TAIL } frame_st_t; static uint8_t parse_state = ST_IDLE; static uint8_t rx_buf[128]; static uint16_t rx_len = 0; static uint16_t rx_cnt = 0;

ase ST_TAIL: hmi_on_frame(rx_buf, rx_len); // hand off to the upper layer parse_state = ST_IDLE; break; default: parse_state = ST_IDLE; break; } } A few details matter. First, every branch of the state machine must have a break, otherwise states bleed into one another — the most common beginner mistake. Second, guard against buffer overflow; the rx_cnt >= sizeof(rx_buf) check in the code is there to stop an oversized frame from the screen from blowing up memory. Third, as soon as a complete frame arrives, reset the state to ST_IDLE immediately, or the first byte of the next frame will be treated as the tail of the current one and dropped. Fourth, never call hmi_on_frame directly inside an interrupt to do business processing — just record the event there and run the actual logic in the main loop, or nested interrupts will easily deadlock. 2.3 The command-encapsulation layer and isolating brand differences The receive state machine solves "how to receive"; command encapsulation solves "how to send." I gave this layer a name: hmi_po

rt — it exposes only four interfaces: write variable, read variable, refresh page, and send a raw frame. Each screen brand gets its own implementation in this file, switched at compile time with conditional compilation or function pointers. For example, writing a 16-bit variable on DWIN DGUS uses 5A A5 05 82 addr_high addr_low data_high data_low. Taojingchi instead sends print temperature,n0 over UART followed by three terminator bytes. Dacai uses AA device_addr length command data checksum. If these several schemes are scattered through the business code, the project is basically unmaintainable. Move them into hmi_port and the business layer only calls hmi_write_u16(); swapping screens then equals swapping one .c file — the payoff is immediate. One detail deserves special attention: the command "acknowledgement" mechanism. A screen isn't a command transceiver — some commands get no reply at all, while others return an ACK. In my framework, commands are split into two kinds: "fire-and-forget," such as clear screen or switch page, which need no follow-up; and "must wait for a response," such as reading a variable. The second kind goes into a transmit queue with timeout and retry — if no response within 100 ms, resend; if three resends still get no reply, report an error to the application layer. This mechanism may look

simple, but in real engineering it can save you many times — especially when a different screen firmware version leaves a particular command unresponsive; with timeout-and-retry the system at least won't hang forever. 2.4 A variable mapping table that keeps business code away from the protocol Once the protocol layer is done, the application layer still faces a problem: how does business code know that the logical quantity "temperature" maps to address 0x0082 on the screen? I use a static configuration table: typedef struct { uint16_t addr; uint8_t type; // HMI_VAR_U8 / HMI_VAR_U16 / HMI_VAR_STR uint16_t max_len; } hmi_var_desc_t; static const hmi_var_desc_t var_tbl[] = { { 0x0082, HMI_VAR_U16, 2 }, // temperature { 0x0084, HMI_VAR_U16, 2 }, // RPM { 0x0090, HMI_VAR_STR, 16 }, //

device ID }; When the application layer writes a value, it first looks up the table by logical name to get the address and type, then goes uniformly through hmi_port_write(). That way, if a variable address is moved later in the screen project, you only change this one table and the business code stays untouched. A common practice is to put the address definitions in a separate header file, or make them an array generated in bulk by a PC-side tool. In my projects I maintain this mapping table in Excel and use a script to convert it into a C array — very worry-free, and the advantage becomes especially obvious once the variable count grows. The mapping table has a hidden benefit too: it naturally becomes the project documentation. When a newcomer takes over, they don't need to dig through hundreds of pages of screen configuration screenshots — one look at this table tells them which variables are in communication, their types, and their purposes. I even add a remarks column with sample protocol commands in it; in team collaboration the efficiency gain is very noticeable. 3. Hands-on framework integration on STM32 3.1 Hardware connections and UART parameters A splash of cold water first: many people crash at step one, not because of code but because of logic levels. Serial screens come with TTL, RS232, or RS485 UART interfaces, while an STM32's UART pins are 3.3V TTL. If your

screen has an RS232 interface and you connect it directly to an STM32's PA9/PA10 with jumper wires, you'll most likely get either garbage or a burnt part. Before starting a project I check the screen's datasheet to confirm whether the level is TTL or needs an external RS232 converter chip, and only then decide the wiring. With a TTL screen, remember TX to RX and RX to TX, and the two sides must share a common ground — otherwise the reference levels differ and you won't receive data. I standardize the baud rate at 115200, 8 data bits, no parity, 1 stop bit — the classic 8N1. Some older screens default to 9600; in low-standby-power scenarios 9600 is fine, but for a project with a long maintenance cycle I recommend setting everything to 115200 in the screen project and keeping host and screen strictly identical. There's an easy pitfall here: after changing the screen's baud rate, the screen must be power-cycled for it to take effect — clicking save isn't enough. I got burned once: after changing the settings I sent commands and got no response at all, and only after a long investigation found the screen hadn't been restarted. 3.2 Framework code structure In my projects the serial-screen-related files are always five, cleanly separated by module. File responsibilities: ringbuf.c/h — ring buffer for staging serial data; hmi_uart.c

/h — STM32 UART peripheral initialization and DMA transmit/receive; hmi_protocol.c/h — state-machine parsing, frame checking, stuck/partial packet splitting; hmi_port.c/h — brand command encapsulation and a unified external interface; hmi_app.c/h — variable table, business callbacks, periodic refresh task. Why split this way? Because every file can be tested alone: ringbuf has no hardware dependency and can run unit tests on a PC; hmi_protocol only needs a byte stream fed in and can even be verified with a serial assistant; the only file truly bound to STM32 is hmi_uart. By squeezing hardware-dependent code into the smallest possible area, porting to another MCU or moving from bare metal to an RTOS costs almost nothing. When I first moved the framework from STM32F103 to GD32F407, I only changed hmi_uart.c — the other four files weren't touched at all, and the port took two days. 3.3 Core code implementation Taking STM32 HAL as an example, UART initialization goes like this: void hmi_uart_init(void) { MX_USART1_UART_Init(); // 1152

00 8N1 HAL_UARTEx_ReceiveToIdle_DMA(huart1, dma_buf, DMA_BUF_LEN); } void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t size) { if (huart-Instance USART1) { ringbuf_push(rb, dma_buf, size); // restart DMA receive and keep waiting HAL_UARTEx_ReceiveToIdle_DMA(huart1, dma_buf, DMA_BUF_LEN); } } Then run a lightweight task in the main loop to feed the bytes from the buffer into the state machine one by one: void hmi_task(void) { uint8_t byte; while (ringbuf_pop(rb, byte, 1)) { hmi_protoco

暂无评论,快来发表第一条评论吧!No comments yet. Be the first!

📮 需求咨询📮 Inquiry