USART+DMA+循环队列接收不定长数据

AI摘要

本文介绍了基于STM32的串口USART结合DMA的循环模式,利用IDLE中断实现接收不定长数据的方法。通过设置循环队列缓冲区,即使在接收过程中CPU未介入,也能避免数据丢失。IDLE中断用于判断数据包的完整性,循环模式确保DMA自动启动新的接收任务,而无需每次手动配置。

简介

这篇文章介绍了串口+DMA的发送过程:记群内因串口DMA发送而引发的讨论_哈士奇去买菜的博客-优快云博客

本文将介绍基于串口+DMA循环模式+循环队列的接收过程。其最大的优点是每次接收完成后不需要禁止MDA通道然后重新配置接收长度。并且还支持接收不定长数据。

USART的IDLE中断

我已经写过IDLE中断的解释,请参考:GD32F130之USART串口通信_哈士奇去买菜的博客-优快云博客_gd32f130串口

这里需要补充强调的是,当本机串口检测到IDLE中断时,只是说明对方已经结束了一串连续的串口帧发送过程,而不一定说明对方已经完成了一包应用层数据的发送。首先串口通信本身没有规定串口帧之间的间隔时间,可以没有间隔,或者可以有任意长间隔。其次,对方在发送一包应用层数据的时候,可能在发送过程中被中断打断,导致发送产生间隔,从而会让本机产生IDLE中断。

在DMA接收方式下,IDLE中断用于告诉应用程序,对方已经完整发送了一包数据,或者,对方只发送了一包数据的一部分。因此,在IDLE中断发生时,应该检查收到数据的完整性,只有收到了完整的数据,才去分析处理这个数据。最好的做法是在应用层设计

USART中DMA使用介绍 1 初始化代码分析 1.1 DMA初始化代码 作用是设置对应DMA通道的中断优先级; void MX_DMA_Init(void) { ​ /* DMA controller clock enable */ __HAL_RCC_DMA1_CLK_ENABLE(); ​ /* DMA interrupt init */ /* DMA1_Channel4_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA1_Channel... 阅读详情

相关推荐

STM32使用寄存器开发底层驱动学习(USART+DMA)

代码简洁明了,当然需要我们注释好,不然比标准库更难看懂。方便快捷,只需要一本《手册》就可以查看所有的寄存器,不需要像标准库那样记、找每个函数、每个参数的作用。

在知识与无知里反复横跳。 6019

STM32F103串口+DMA接收不定长数据,我用空闲中断+循环队列搞定了(附完整代码)

本文详细介绍了STM32F103串口DMA接收不定长数据的工程实践,结合空闲中断和循环队列技术,构建高效稳定的数据接收框架。通过DMA循环模式和空闲中断机制,有效解决了传统串口接收方案在高数据速率下的性能瓶颈问题,并提供了完整的代码实现和优化建议。

weixin_30735745的博客 399

stm32f103串口接收队列,DMA循环模式+空闲中断

串口通讯最主要的就是要不丢数据,不丢帧,基本设想就是建立一个大的串口缓冲区,串口接收到的数据使用循环队列的方式全部往这个缓冲区放,不过这种方式需要把串口缓冲区弄大一点,防止数据覆盖。在stm32中,利用DMA+空闲中断很容易做到这一点。只需要将DMA设置为循环模式,如下图: 也就是说,在循环模式下,当DMA传输了一个数据,DMA_CNDTRx传输数量寄存器相应减一,当DMA_CNDTRx传输数量寄存器减为0时,DMA_CNDTRx寄存器将恢复为相应的初始值,不用软件干预,那么,这不就是循环队列的方式吗?

qq_40831436的博客 1万+

GD32或STM32:DMA循环模式与普通模式(串口IDLE)

我的现象是:第一步收完后,关闭DMA,重置CNDTR,再开DMA,第二步接收,依然是从RxBuff下标0开始接收)Circle模式的DMA:初始化后,不需要再次配置,会以循环列表的形式往数组里面存数据;使用DMA减少了cpu的参与(数据搬移),IDLE(即帧与帧之间的空缺时间会触发idle)解决了频繁的串口中断,又可以处理不定长的数据帧;front 是当前正在处理的数组的index,rear 是下一个要存放数据的index。(2)获得rear的值,(它表示下一个要存储的下标);

weixin_39257775的博客 4117

避开HAL库串口中断的坑:从蓝桥杯板子出发,详解USART接收回调与数据解析

本文深入解析STM32 HAL库串口中断的常见问题,特别是数据拆分现象,并提供环形缓冲区、IDLE中断和DMA接收三种解决方案。针对蓝桥杯嵌入式开发中的USART接收回调与数据解析难题,详细介绍了实现步骤和优化技巧,帮助开发者提升串口通信的稳定性和效率。

weixin_33739646的博客 434

5个必知的脑网络图论指标:从聚类系数到小世界特性(附BCT工具包使用指南)

本文深度解析脑网络图论分析中五个核心指标:聚类系数、特征路径长度、全局效率、局部效率及节点效率,并详解其计算与解读。结合Brain Connectivity Toolbox工具包,提供从实战计算、常见报错解决到小世界特性统计检验的完整指南,帮助研究者跨越理论与实操鸿沟,让连接矩阵数据真正服务于神经科学研究。

blue42的博客 925

Ubuntu22.04+PX4+Gazebo:从零到一构建无人机仿真开发环境

本文详细介绍了在Ubuntu22.04系统下搭建PX4+Gazebo无人机仿真开发环境的完整流程。从基础环境配置、PX4固件安装到Gazebo仿真环境搭建,提供了实用命令和常见问题解决方案,帮助开发者快速构建高效的无人机开发环境。

weixin_29243279的博客 437

箱式连续提升机

箱式连续提升机,是对具有高度差较大(>1500mm)的输送线之间进行连接,将箱式物资进行连续输送的提升(或下降)的输送设备,可根据输送线的具体输送要求作相应的参数设计。目前的箱式连续提升机种类比较多,常用的形式主要有Z型和C型,工作原理相同,只是入口和出口分别在两侧和同侧。

月生沧溟 1249

2025数学建模国赛A题1-4问详解(论文+完整代码)

本文介绍了数学建模国赛A题的解题思路,重点分析了三维遮蔽模型的建立和运动学代码实现。相比第一题,第二题增加了未知参数求解,需要采用遗传算法等非线性优化方法。文章强调该模型由专业团队原创开发,代码可运行,并提醒参赛者警惕市场上的保奖班现象。整体内容聚焦于竞赛解题技巧和原创性保证。

Eloveeeee的博客 2799

青龙面板实战:用快手极速版脚本日赚XX元的核心参数解析

本文深入解析了青龙面板运行快手极速版脚本的核心参数配置,旨在帮助进阶用户提升收益稳定性。重点剖析了设备标识符(did)的获取原理、多账号Cookie的精准拼接规则,以及如何通过科学设置定时任务频率来量化并优化收益,实现细水长流的稳定收益。

weixin_29310605的博客 1075

Edge浏览器IE模式全攻略:从企业应用到老旧网站兼容(含常见问题解答)

本文详细解析了微软Edge浏览器的IE模式,这是解决企业遗留系统与老旧网站兼容性问题的关键技术。文章从核心原理出发,提供了从个人用户临时启用到企业级组策略部署的完整配置指南,并深入探讨了文档模式调优、ActiveX控件处理等高级技巧,最后汇总了常见问题排查与安全实践,帮助用户高效平衡兼容性与现代化浏览体验。

weixin_29227425的博客 610

SAP(ERP)工艺路线(Routing)业务视角解析

工艺路线(Routing)也是PP模块,即生产制造模块中最重要核心主数据之一,也是ERP(SAP)顾问,必需从业务与系统深入掌握的重要主数据,它也是整个生产计划与生产流程的重要基础。因为执行步骤只是操作层面的认知,业务顾问需要从业务本质上去理解它,要真正从业务视角理解工艺路线的业务价值,而不仅仅是操作层面的认知。因此将工艺路线理解为“有以上业务本质的理解,然后进到SAP系统中,就容易理解工艺路线(Routing)的每一组成部分结构与功能了,本质就是理解制造企业如何将设计意图转化为物理现实的编码规则。

m0_45197968的博客 1503

地球引擎初级教程:Python API 的安装和介绍

地球引擎的 Python API 提供了许多功能和工具,用于访问和处理地理空间数据。连接到地球引擎在编写地球引擎的 Python 脚本时,首先需要连接到地球引擎服务器。import ee # 初始化地球引擎 ee . Initialize()这将初始化地球引擎并建立与服务器的连接。加载和显示图像使用地球引擎的 Python API,您可以加载和显示卫星图像。import ee# 初始化地球引擎# 加载 Landsat 8 影像# 显示影像print('影像 URL:', url)

UePostscript的博客 1007

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: GD32F130之DMA
下一篇: vsnprintf函数使用
哈士奇上蔚
博客等级 码龄5年 282粉丝 35原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值