STM32 serial port DMA problem detailed

Last evening, I got stuck trying to figure out the STM32 serial DMA issue, so this morning I decided to write a blog post to share my understanding of serial DMA transmission. In this article, I will discuss three main topics: 1. What is a serial port DMA request? 2. A brief review of the serial port. 3. The process of serial DMA transmission. ### 1. What is a Serial Port DMA Request (Battleship STM32 Development Board)? Before diving into the topic, let me briefly recall what DMA is. According to a PPT from the atomic brother: DMA stands for Direct Memory Access, which allows data to be transferred directly between memory and peripherals without involving the CPU. Once the CPU initializes a DMA transfer, the transfer is handled entirely by the DMA controller. STM32 has two DMA controllers: DMA1 (with 7 channels) and DMA2 (with 5 channels, available only in high-capacity devices). Each channel manages access requests from one or more peripherals to memory. There's also an arbiter that handles the priority of DMA requests. The main purpose of DMA is to reduce the CPU's workload. ![DMA Channels](http://i.bosscdn.com/blog/16/19/43/41V-0.png) DMA requests from peripherals like TIMx, ADCx, SPIx, I2Cx, and USARTx are logically OR'ed into the DMA controller. This means only one request can be active at a time. Each peripheral’s DMA request can be enabled or disabled independently via control bits in its respective registers. Now, focusing on the serial port DMA request: when using the serial port to send or receive data, the DMA can handle the data transfer instead of the CPU. For example, if you want to send data from the STM32 to a PC, normally the CPU would write the data through the APB bus and then send it. But with DMA, the data is copied directly from memory to the serial port without CPU intervention. So, the definition of a serial port DMA request is simply when the serial port asks the DMA to handle the sending or receiving of data. ### 2. Brief Review of the Serial Port (Library Function) I previously shared a post about redirecting `printf` and semi-host mode. You can find it [here](http://?mod=viewthread&tid=44609&highlight=printf). Let me quickly recap some key points about the serial port on the Battleship STM32 board. For instance, if you want to receive data from a PC via the serial port and then echo it back, the process involves enabling the receive interrupt. When data arrives, the serial port generates an interrupt, and the interrupt service routine checks if the reception is complete. If it is, it sets a flag. Then, in the main function, we continuously check that flag. Once it’s set, we loop through the received data and send it back via the serial port by writing to the `USART_DR` register. The serial port then automatically sends the data, which is displayed in the serial debugging assistant. ### 3. Serial DMA Transmission Process (Library Function) In a Battleship experiment, pressing KEY0 triggers a DMA transfer to USART1, and progress is shown on the TFTLCD module. Starting from the main function, we first allocate a section of SRAM (like 5200 bytes) as a buffer (`SendBuff[]`) to store the data to be sent. The larger the buffer, the slower the DMA transfer. We then fill the `TEXT_TO_SEND[]` array into `SendBuff[]`. The way the Battleship code fills the buffer is quite clever and worth learning. Once `SendBuff[]` is filled, we check if KEY0 is pressed. If it is, we enable the serial port’s DMA transmission by setting the `DMAT` bit in `USART_CR3`, and then start the DMA transfer by enabling DMA1 Channel 4. This is done using functions like `DMA_Cmd()` from `stm32f10x_dma.c`. Regarding transmission time, it depends heavily on the baud rate. At 9600 bps, the serial port transfers about 1200 bytes per second. With a 5200-byte buffer, the transfer would take roughly 4 seconds. This concludes my thoughts on serial DMA transmission. Understanding how DMA works with the serial port can greatly improve system efficiency and reduce CPU load.

Carbon Fiber Insulation And Heat Insulating Board

Carbon Fiber Insulation And Heat Insulating Board,Carbon Fiber Refractory Material,Octagonal Carbon Fiber Board,Civilian Carbon Fiber Vacuum Furnace

HuNan MTR New Material Technology Co.,Ltd , https://www.hnmtr.com

Posted on