当前位置:网站首页>STM32 DMA receives serial port data
STM32 DMA receives serial port data
2022-07-29 02:15:00 【Mubai 001】
- adopt DMA, No need to interrupt , Receive serial data with irregular length
describe :
When multiple data transmission through serial port ,CPU Multiple interrupts will be generated to receive serial data , This will greatly reduce CPU efficiency , At the same time, we need CPU To do something more important , How should we optimize ?
Like a four-axis aircraft , When continuously acquiring the attitude control direction , To receive serial port data .
answer : Use DMA, There is no need to CPU Interrupt can receive serial port data
1.DMA Introduce
DMA, Its full name is : Direct Memory Access, Direct memory access , DMA There is no need for transmission CPU direct
Control transmission , Through hardware for RAM And I/O The device opens up a path for direct data transmission , Can make CPU Greatly improved the efficiency of .
2 stay main() Call the serial port configuration function , After initializing the serial port , Then enable UART1_RX Of DMA receive
2.1 stay main() Function , Use the following function to call the configuration function :
uart_init(115200); // The serial port is initialized to 115200
2.2 uart_init() Function as follows :
void uart_init(u32 bound){
//GPIO Port settings
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); // Can make USART1,GPIOA The clock
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Multiplexing push pull output
GPIO_Init(GPIOA, &GPIO_InitStructure);// initialization GPIOA.9
//USART1_RX GPIOA.10 initialization
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;// Floating input
GPIO_Init(GPIOA, &GPIO_InitStructure);// initialization GPIOA.10
USART_InitStructure.USART_BaudRate = bound;// Serial port baud rate
USART_InitStructure.USART_WordLength = USART_WordLength_8b;// The word is 8 Bit data format
USART_InitStructure.USART_StopBits = USART_StopBits_1;// A stop bit
USART_InitStructure.USART_Parity = USART_Parity_No;// No parity bit
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;// No hardware data flow control
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // Transceiver mode
USART_Init(USART1, &USART_InitStructure); // Initialize serial port 1
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); // Enable serial port 1 Of DMA send out
}
3. stay main() Call in DMA Configuration function , Then initialize DMA1 Of UART1_RX After the passage , Then enable serial port 1 and DMA
3.1 As shown in the figure below ,UART1_RX be located DMA1 passageway 5:

So use variables in library functions DMA1_Channel5 To configure the UART1_RX.
3.2 stay main() Function , Define a receive array , Use the following 3 Parameters to call the configuration function :
u8 USART_RX_BUF[35]; // Receive buffer , Maximum USART_REC_LEN Bytes . The last byte is a newline character MYDMA_Config(DMA1_Channel5,(u32)&USART1->DR,(u32)USART_RX_BUF,35);//DMA1 passageway 5, Peripherals are serial ports 1, The memory is SendBuff, length 35,
3.3 MYDMA_Config() Function as follows , In the end, it will call MYDMA_Enable() Start once DMA transmission !:
void MYDMA_Config(DMA_Channel_TypeDef* DMA_CHx,u32 cpar,u32 cmar,u16 cndtr)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // Can make DMA transmission
DMA_DeInit(DMA_CHx); // take DMA The passage of 1 Register reset to default
DMA1_MEM_LEN=cndtr;
DMA_InitStructure.DMA_PeripheralBaseAddr = cpar; //DMA Peripheral base address
DMA_InitStructure.DMA_MemoryBaseAddr = cmar; //DMA Memory base address
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // Data transmission direction , Read from memory and send to peripheral
DMA_InitStructure.DMA_BufferSize = cndtr; //DMA The tunnel DMA Cache size
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // The peripheral address register remains unchanged
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // The memory address register is incremented
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; // Data width is 8 position
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; // Data width is 8 position
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // Working in normal mode
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //DMA passageway x Have medium priority
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA passageway x Not set to memory to memory transfer
DMA_Init(DMA_CHx, &DMA_InitStructure); // according to DMA_InitStruct The parameter specified in DMA The passage of USART1_Rx_DMA Register identified
USART_Cmd(USART1, ENABLE); // Enable serial port 1
DMA_Cmd(DMA_CHx, ENABLE); // Can make USART1 TX DMA1 Indicated channel
MYDMA_Enable(DMA1_Channel5);// Start once DMA transmission !
}
3.4 MYDMA_Enable() function
as follows :
void MYDMA_Enable(DMA_Channel_TypeDef*DMA_CHx)
{
DMA_Cmd(DMA_CHx, DISABLE ); // close USART1 TX DMA1 Indicated channel
DMA_SetCurrDataCounter(DMA_CHx,DMA1_MEM_LEN);// Set the buffer size again , Pointing to the array 0
DMA_Cmd(DMA_CHx, ENABLE); // Can make USART1 TX DMA1 Indicated channel
} 4. Then when USART_RX_BUF[0] There are data , Appropriate delay 10ms, Give Way UCOS Continue to operate other processes , You can receive all the data of variable length
The code is as follows ( It can also be placed in a non operating system while in ):
if(USART_RX_BUF[0]) // Array 0 Have the data , explain DMA Start receiving a piece of data
{
delay_ms(10); // Time delay 10ms, Give Way DMA While continuing to receive the following data , Can also run other processes
printf("1:%s\r\n",USART_RX_BUF); // Print
memset(USART_RX_BUF,0,35); // Empty array
MYDMA_Enable(DMA1_Channel5);// Start once DMA transmission !
}
Delay in the above code 10ms, How much data can you accept ?
At baud rate 115200 Next ,1S Can accept 115200 position bit, Then a byte is 8 position bit, Plus a stop bit , So it's acceptable 12800 Data .
that 10ms, Acceptable 128 Data , If the data array is large , The delay time can be appropriately increased
5. The test results
As shown in the figure below , Echo as much as you enter , It shows that it has been successful , Here I set the receiving array size to 35, If you need longer data , Just change the size of the array

summary :1 , Enable serial reception
2, Enable serial port DMA
3, Enabling channel
4, Start transmitting
边栏推荐
- Navigation -- realize data transmission and data sharing between fragments
- Promise solves asynchrony
- [circuit design] open collector OC output of triode
- 2022年编程语言排名,官方数据来了,让人大开眼界
- 第十四天:续第十三天标签相关知识
- Click back to the top JS
- druid. io index_ Realtime real-time query
- 2022.7.28-----leetcode.1331
- druid. io kill -9 index_ Realtime traceability task
- Force deduction brush question (1): sum of two numbers
猜你喜欢

第十四天:续第十三天标签相关知识

数学建模——永冻土层上关于路基热传导问题

Force deduction brush question (2): sum of three numbers

Have you ever encountered the situation that the IP is blocked when crawling web pages?

Mathematical modeling -- cold proof simulation of low temperature protective clothing with phase change materials

数学建模——带相变材料的低温防护服御寒仿真模拟

Lm13 morphological quantification momentum period analysis

Establish an engineering template based on STM32 in keil -- detailed steps

弹性布局 单选

Mathematical modeling -- bus scheduling optimization
随机推荐
mobile-picker.js
Mathematical modeling -- heat conduction of subgrade on Permafrost
leetcode/乘积小于K 的连续子数组的个数
TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(2)
druid. The performance of IO + tranquility real-time tasks is summarized with the help of 2020 double 11
Mathematical modeling -- cold proof simulation of low temperature protective clothing with phase change materials
Force deduction brush question (1): sum of two numbers
QT source code analysis -- QObject (4)
Read the recent trends of okaleido tiger and tap the value and potential behind it
解决使用ESlint时,和vetur冲突导致保存变双引号,结尾逗号等
webview攻击
Understand the working principle of timer in STM32 in simple terms
Using local cache + global cache to realize user rights management of small systems
基于C51控制蜂鸣器
Feynman learning method (symbol table)
E-commerce keyword research helps data collection
Basic working principle and LTSpice simulation of 6T SRAM
What is browser fingerprint recognition
Leetcode/ and continuous shortest subarray greater than or equal to target
Understand the clock tree in STM32 in simple terms