Memory to Memory - Scatter/Gather
This sample uses the GDMA scatter/gather function to achieve memory-to-memory data transfer.
Users can modify some GDMA settings, including channel configuration and data transfer length, through related macro configurations. For detailed descriptions of specific macro configuration items, please refer to Configuration.
Requirements
For hardware requirements, please refer to the Requirements.
Configurations
The following macros can be used to configure whether to use the gather function or the scatter function.
#define USE_GATHER 0 /*< Set this macro to 1 to use gather function. */ #define USE_SCATTER 1 /*< Set this macro to 1 to use scatter function. */
The following macros can be used to configure the parameters for gather or scatter.
#define SCATTER_GATHER_BUFFER_SIZE (20) #define SCATTER_GATHER_COUNT (4) #define SCATTER_GATHER_INTERVAL (1)
The following macros represent the parameter configurations of the allocated GDMA channel.
#define SCATTER_GATHER_DMA_CHANNEL_NUM scatter_gather_dma_ch_num #define SCATTER_GATHER_DMA_CHANNEL DMA_CH_BASE(scatter_gather_dma_ch_num) #define SCATTER_GATHER_DMA_IRQ DMA_CH_IRQ(scatter_gather_dma_ch_num)
The entry function is as follows, call this function in
main()
to run this sample code. For more details, please refer to the Initialization.dma_scrgar_demo();
Building and Downloading
For building and downloading, please refer to the Building and Downloading.
Experimental Verification
Press the Reset button on the EVB.
After initialization is complete, the GDMA begins transferring data. Once the GDMA completes the data transfer, it will trigger an interrupt. Observe the data transfer completion message displayed in the Debug Analyzer.
scatter_gather_dma_handler: Data transmission completion!
Note
If an error is detected in the transferred data, observe the error information in the Debug Analyzer.
Code Overview
This section introduces the code and process description for initialization and corresponding function implementation in the sample.
Source Code Directory
The directory for project file and source code are as follows:
For project directory, please refer to Source Code Directory.
Source code directory:
sdk\src\sample\io_demo\gdma\scatter_gather\dma_scrgar_demo.c
.
Initialization
The initialization flow for peripherals can refer to Initialization Flow.
In this sample, data is transferred from memory to memory, so there is no need to set PAD and PINMUX.
Call
RCC_PeriphClockCmd()
to enable the GDMA clock.Call
GDMA_channel_request
to request a free GDMA channel and register the GDMA interrupt handler.Initialize the GDMA peripheral:
Define a
GDMA_InitTypeDef
typeGDMA_InitStruct
, and callGDMA_StructInit()
to pre-fillGDMA_InitStruct
with default values.Modify the
GDMA_InitStruct
parameters as needed. The initialization parameters for the GDMA channel are configured as shown in the table below.
GDMA Initialization Parameters GDMA Hardware Parameters
Setting in the
GDMA_InitStruct
GDMA
Channel Num
SCATTER_GATHER_DMA_CHANNEL_NUM
Transfer Direction
Buffer Size
SCATTER_GATHER_BUFFER_SIZE
Source Address Increment or Decrement
Destination Address Increment or Decrement
Source Data Size
Destination Data Size
Source Burst Transaction Length
Destination Burst Transaction Length
Source Address
GDMA_SendBuffer
Destination Address
GDMA_RecvBuffer
Scatter Enable
Scatter Count
SCATTER_GATHER_COUNT
Scatter Interval
SCATTER_GATHER_INTERVAL
Gather Enable
Gather Count
SCATTER_GATHER_COUNT
Gather Interval
SCATTER_GATHER_INTERVAL
Call
GDMA_Init()
to initialize the GDMA peripheral.Configure the GDMA total transfer completion interrupt:
GDMA_INT_Transfer
and NVIC. For NVIC-related configuration, refer to Interrupt Configuration.
Call
GDMA_Cmd()
to enable GDMA channel for transmission.
Functional Implementation
After enabling GDMA, GDMA begins transferring data from
GDMA_SendBuffer
toGDMA_RecvBuffer
.If
USE_SCATTER
is set to1
, GDMA will scatter the data from the GDMA_SendBuffer into the GDMA_RecvBuffer, as shown in the figure below.GDMA Scatter Function
Once the data transfer is completed, a GDMA total transfer completion interrupt
GDMA_INT_Transfer
interrupt is triggered. Within the interrupt handler, GDMA channel is closed, the GDMA channel interrupt flag is cleared, and it is checked whether the sent data is identical to the received data.uint32_t scatter_cnt = SCATTER_GATHER_BUFFER_SIZE / SCATTER_GATHER_COUNT; uint8_t send_value = 0; uint8_t recv_value = 0; for (uint32_t i = 0; i < scatter_cnt; i++) { for (uint32_t j = 0; j < SCATTER_GATHER_COUNT; j++) { send_value = GDMA_SendBuffer[i * SCATTER_GATHER_COUNT + j]; recv_value = GDMA_RecvBuffer[i * (SCATTER_GATHER_COUNT + SCATTER_GATHER_INTERVAL) + j]; if (send_value != recv_value) { IO_PRINT_ERROR4("scatter_gather_check_data: scatter check data failed i %d j %d source %d dest %d", i, j, send_value, recv_value); } } }
If
USE_GATHER
is set to1
, GDMA will gather the data scattered in the GDMA_SendBuffer into the GDMA_RecvBuffer, as shown in the figure below.GDMA Gather Function
Once the data transfer is completed, a GDMA total transfer completion interrupt
GDMA_INT_Transfer
interrupt is triggered. Within the interrupt handler, GDMA channel is closed, the GDMA channel interrupt flag is cleared, and it is checked whether the sent data is identical to the received data.uint32_t gather_cnt = SCATTER_GATHER_BUFFER_SIZE / SCATTER_GATHER_COUNT; uint8_t send_value = 0; uint8_t recv_value = 0; for (uint32_t i = 0; i < gather_cnt; i++) { for (uint32_t j = 0; j < SCATTER_GATHER_COUNT; j++) { send_value = GDMA_SendBuffer[i * (SCATTER_GATHER_COUNT + SCATTER_GATHER_INTERVAL) + j]; recv_value = GDMA_RecvBuffer[i * SCATTER_GATHER_COUNT + j]; if (send_value != recv_value) { IO_PRINT_ERROR4("scatter_gather_check_data: gather check data failed i %d j %d source %d dest %d", i, j, send_value, recv_value); } } }