当前位置:网站首页>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
边栏推荐
- Qt 内存管理小技巧
- [circuit design] open collector OC output of triode
- Leetcode 242. valid anagram
- 【ONE·Data || 链式二叉树】
- Leetcode/ and continuous shortest subarray greater than or equal to target
- Add graceful annotations to latex formula; "Data science" interview questions collection of RI Gai; College Students' computer self-study guide; Personal firewall; Cutting edge materials / papers | sh
- Monadic linear function perceptron: Rosenblatt perceptron
- 2022.7.27-----leetcode.592
- mobile-picker.js
- How to write, load and unload plug-ins in QT
猜你喜欢

【ONE·Data || 链式二叉树】
![[circuit design] open collector OC output of triode](/img/48/5a111b81f0d99990fbcc5263313c07.jpg)
[circuit design] open collector OC output of triode

E-commerce keyword research helps data collection

Motionlayout -- realize animation in visual editor

Detailed explanation of IVX low code platform series -- Overview (II)

Verilog procedure assignment statements: blocking & non blocking

12. < tag dynamic programming and subsequence, subarray> lt.72. edit distance

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

Jetpack -- navigation realizes page Jump

Flexible layout single selection
随机推荐
解决使用ESlint时,和vetur冲突导致保存变双引号,结尾逗号等
2022.7.27-----leetcode.592
Promise解决异步
Leetcode exercise - Sword finger offer 45. arrange the array into the smallest number
Introduction to shared data center agent
druid. io index_ Realtime real-time query
Cookies and sessions
Try to understand the essence of low code platform design from another angle
2022.7.27-----leetcode.592
Mathematical modeling -- heat conduction of subgrade on Permafrost
autoware中ndtmatching功能加载点云图坐标系修正的问题
Random talk on distributed development
全志T3/A40i工业核心板,4核[email protected],国产化率达100%
awvs无法启动问题
Jetpack -- understand the use of ViewModel and livedata
12. < tag dynamic programming and subsequence, subarray> lt.72. edit distance
Anti crawler mechanism solution: JS code generates random strings locally
【MySQL】sql给表起别名
Lm13 morphological quantification momentum period analysis
基于C51控制蜂鸣器