当前位置:网站首页>Also on STM32 using dma+ serial port to obtain variable length data
Also on STM32 using dma+ serial port to obtain variable length data
2022-06-13 00:18:00 【csaaa2005】
The latest one STM SCM development needs to connect a module , Serial communication .
STM32 SCM is a typical F103Cx, It is a simple operation in this paper , I didn't expect to do it 2 God . There are many posts on the Internet, including various existing routines , Use RXNE I won't say if I interrupt the reception , Single chip computers with heavy load are not recommended . about DMA Use , Most articles use DMA+ Serial port is free IDLE sign , Get variable length data , The usual way is :
1. To configure DMA, Use single pass mode
2. Configure serial port , Interrupt flag IDLE,
3 Receive data in the serial port interrupt , Reset DMA buffer
Found in actual use :
The serial port data does not spit smoothly ,IDLE Interrupts occur frequently , The buffer read by multiple interrupts needs to be spliced together to be the complete data , So I set DMA Buffers are basically wasteful . But also need to add splicing code . The use effect is not ideal .
Since the nature of the module itself cannot be changed , Can only solve their own software problems . In the observation of serial port data . The data interval of each frame exceeds 500ms, The amount of data per frame is 400-800byte Unequal . So we have the following improvements :
The general principle is : Give Way DMA Go to buffer Fill in the data , When one frame is finished , There was a time when there was no growth in data , Check this period of time , Judge the end of the frame , I also see people using timers to detect this period of time , The effect is the same , But I will definitely skip a timer , Just count directly .
Methods and steps :
1. To configure DMA, The buffer is set to be greater than the number of frame bytes (1000), Use continuous mode ( Data exceeding the buffer will overwrite the previous )
2. Configure serial port without interruption ( Because the single-chip microcomputer has display timing requirements , Do not use interrupts at will , Will cause display disorder )
3. stay while The process of querying the number of buffers in CheckDMA();
#define RXIDLE 500
#define USART1_MAX_RECV_LEN 1000
void CheckDMA(void){
u16 temp;
static u16 lastcnt,idletime;
if(DMA1_Channel5->CNDTR<USART1_MAX_RECV_LEN){// There's data coming in
if(DMA1_Channel5->CNDTR==lastcnt){// Have new data arrived
if(++idletime>RXIDLE){// No new data has arrived over time
temp = USART1_MAX_RECV_LEN -DMA1_Channel5->CNDTR;// Number of data read
USART1_RX_BUF[temp]=0;// Terminator
DATA_Analysis((u8*)USART1_RX_BUF);
DMA_Restart();// Buffer reset , Although it is a continuous mode , But you want every frame to be written from the header .
}
}else idletime=0;
lastcnt=DMA1_Channel5->CNDTR;
}
}
void DMA_Restart(void)
{
DMA_Cmd(DMA1_Channel5, DISABLE ); // close USART1 TX DMA1 Indicated channel
DMA_SetCurrDataCounter(DMA1_Channel5,USART1_MAX_RECV_LEN);//DMA The tunnel DMA Cache size
DMA_Cmd(DMA1_Channel5, ENABLE); // Can make USART1 TX DMA1 Indicated channel
}
After the above transformation , It realizes the reading of serial port data of irregular data sending module , By adjusting the idle time just in the right range ( No more than the frame interval , wide ), The above procedure can work stably and reliably , And get buffer The data is continuous and accurate . because STM32 Of IDLE The time of the sign cannot be adjusted , Therefore, the above method is an effective method to solve this problem . At the same time, due to the adoption of DMA+ How to query , Avoiding other problems caused by interruptions won't be right CPU Bring extra burden
边栏推荐
- PLC peut également faire des jeux - - codesys écrit des jeux de devinettes numériques
- Memory address mapping of u-boot
- 分公司能与员工签劳动合同么
- Context of go concurrency mode
- 哲學和文學的區別
- 【Matlab】符号计算
- [hcie discussion] STP-A
- 3、 Storage system
- How to gracefully solve the offset problem of Baidu and Gaode maps in leaflet
- Go custom collation
猜你喜欢

USTC of China University of science and technology: Minrui Wang | distribution network voltage stabilization based on transformer Multi-Agent Reinforcement Learning

【Matlab】符号计算

PMP training organization

PMP renewal | PDU specific operation diagram

Vscode实现PHP在浏览器实时预览

Is the revised PMP worth testing?

PLC也能制作小游戏----Codesys编写猜数字小游戏

Video tracker error troubleshooting

PLC也能制作小遊戲----Codesys編寫猜數字小遊戲
![[vscode]todo tree a to-do plug-in](/img/52/c977bc9cd021ca6fd12bcc22ae9f78.jpg)
[vscode]todo tree a to-do plug-in
随机推荐
APISpace 空号检测API接口 免费好用
[LeetCode]28. Implement strstr()
2022施工员-设备方向-通用基础(施工员)操作证考试题及模拟考试
Pipelines in go concurrency mode
Basics of network security (1)
63. 不同路径 II
[LeetCode]13. Roman numerals to integers thirty
[C] Inverts the binary of a decimal number and outputs it
How to visit a website
[matlab] two dimensional curve
What occupation is suitable for PMP?
PMP training organization
【Matlab】符号计算
How SAP ui5 uses manifest JSON file defines third-party library dependencies
[matlab] 3D curve and 3D surface
6.824 Lab 4B: Sharded Key/Value Service
PLC也能制作小遊戲----Codesys編寫猜數字小遊戲
MAYA建模六
A detailed explanation of synchronized
[hcie discussion] multicast igmp-a