STM32 HAL library CubeMX tutorial (vi) the DMA data transfer

STM32 HAL library CubeMX tutorial (vi) the DMA data transfer


STM32 HAL library CubeMX series of tutorials directory

About DMA

Direct memory access (DMA) is used to provide high speed data transfer between peripherals and memory or between a memory and a memory. Without CPU intervention , data can be moved by DMA quickly, which saves CPU resources to do other operations.

stm32 has two DMA controllers, a total of 12 channels (DMA1 seven channels, DMA2 five channels), each channel is used to manage a request from one or more peripheral memory access. There is also a priority arbiter to coordinate the respective DMA requests.

: A block diagram of DMA
Here Insert Picture Description
requests the DMA1 image for each channel:
Here Insert Picture Description
requesting each image channel DMA2:
Here Insert Picture Description
direction of DMA transfer data three: from peripheral to memory, from memory to peripheral, from memory to memory.

Generally refers to the peripheral data register peripherals, such as ADC, SPI, I2C data registers and other peripherals. Generally it refers to the memory chip SRAM, external memory, such as Flash chip. Summarizing the data is transmitted using DMA transfer without taking up CPU , the liberation of CPU, CPU to have enough time to deal with other things.

We can use the DMA transfer to transfer the data into the specified memory USART registers, and sent to the PC side, the serial debugging assistant display.

CubeMX Configuration

  1. And the same clock configuration prior to
  2. 1 serial multiplexed PB6, PB7, asynchronous mode, interrupt enable
    PA9 connected to the LED, the output mode
    Here Insert Picture Description
    Here Insert Picture Description
  3. DMA configuration
    Here Insert Picture Description

DMA initialization

Initialization DMA_ InitTypeDef structure:
Here Insert Picture Description

  1. The Direction : the direction of transmission select selectable peripheral to memory, memory to peripheral and memory to the memory
    device. [: 0 1] which sets the value of bit DMA_SxCR register DIR.
  2. PeripheralInc:如果配置为 DMA_PINC_ENABLE,使能外设地址自动递增功能,
    它设定 DMA_CCR 寄存器的 PINC 位的值;一般外设都是只有一个数据寄存器,
    所以一般不会使能该位。
  3. MemoryInc:如果配置为 DMA_MINC_ENABLE,使能存储器地址自动递增功能,
    它设定 DMA_CCR 寄存器的 MINC 位的值;我们自定义的存储区一般都是存放多
    个数据的,所以要使能存储器地址自动递增功能。
  4. PeriphDataAlignment:外设数据宽度,可选字节(8 位)、半字(16 位)和字(32 位),
    它设定 DMA_SxCR 寄存器的 PSIZE[1:0]位的值。 ADC 数据寄存器只有低 16 位数
    据有效,使用半字数据宽度。
  5. Mode: DMA 传输模式选择,可选一次传输或者循环传输,它设定 DMA_SxCR 寄
    存器的 CIRC 位的值。我们希望 ADC 采集是持续循环进行的,所以使用循环传输
    模式。
  6. 软件设置数据流的优先级,有 4 个可选优先级分别为非常高、高、中和低,它设
    定 DMA_SxCR 寄存器的 PL[1:0]位的值。 DMA 优先级只有在多个 DMA 数据流同
    时使用时才有意义,这里我们设置为高优先级就可以了。
    hdma_usart1_tx.Instance = DMA1_Channel4; //DAMAͨµÀ
    hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; //´«Êä·½Ïò
    hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE; //
    hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_usart1_tx.Init.Mode = DMA_CIRCULAR;
    hdma_usart1_tx.Init.Priority = DMA_PRIORITY_HIGH;

DMA-UART数据收发

我们使用DMA将数据从内部flash转移到 USART 寄存器内,并发送至 PC 端, 由串口调试助手显示。
主函数编写:

  1. Transmitting and receiving data buffer of the definition
/* USER CODE BEGIN 0 */
uint8_t aRxBuffer;                      
uint8_t aTxBuffer[SENDBUFF_SIZE];       
/* USER CODE END 0 */
  1. To define the data transmitted to the buffer, we usually defined constants are stored in the internal memory of the microcontroller
    while the serial port receive interrupt, DMA transfer start
/* USER CODE BEGIN 2 */
 for(uint16_t i=0;i<SENDBUFF_SIZE;i++)
  aTxBuffer[i]='h';
 HAL_UART_Receive_IT(&huart1,&aRxBuffer,1);
 HAL_UART_Transmit_DMA(&huart1,aTxBuffer,SENDBUFF_SIZE);
  /* USER CODE END 2 */
  1. Interrupt callback function
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  HAL_UART_Transmit(&huart1,&aRxBuffer,1,0);
  HAL_UART_Receive_IT(&huart1,&aRxBuffer,1);
}
  1. Order not to take the controller's CPU to verify the DMA transfer, while we in the LED blinking
while (1)
{
  HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_9);
  HAL_Delay(500);
}
  1. Serial Assistant continue to show 'h', and the LED blink
    Here Insert Picture Description
    because the DMA transfer rate soon, remember to click Clear to receive clear screen to prevent serial assistant stuck!

Reference material

  1. Hard Rock YS-F1Pro development board development manual (HAL library updated version 20170104) .pdf
  2. STM32F10xxx Reference Manual .pdf
  3. [Wildfire] "STM32 + HAL + library developers practical guidelines - based on the F103-MINI" .pdf
Published 14 original articles · won praise 18 · views 7668

Guess you like

Origin blog.csdn.net/weixin_43116606/article/details/104233238