Memory to Memory-Single Block
This sample uses the GDMA single block function to achieve memory-to-memory data transfer.
The maximum data transfer size supported by the single block function is 65,535. If this data size is exceeded, Multi block transfer is required. For details on the multi block sample, please refer to Memory to Memory - Multi Block.
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 configured to modify the GDMA data transfer length.
#define GDMA_TRANSFER_SIZE 100 -
The following macros represent the parameter configurations of the allocated GDMA channel.
#define DEMO_DMA_CHANNEL_NUM mem_to_mem_dma_ch_num #define DEMO_DMA_CHANNEL DMA_CH_BASE(mem_to_mem_dma_ch_num) #define DEMO_DMA_IRQ DMA_CH_IRQ(mem_to_mem_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.gdma_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.
demo_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\memtomem\gdma_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_requestto request a free GDMA channel and register the GDMA interrupt handler.-
Initialize the GDMA peripheral:
Define a
GDMA_InitTypeDeftypeGDMA_InitStruct, and callGDMA_StructInit()to pre-fillGDMA_InitStructwith default values.Modify the
GDMA_InitStructparameters 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_InitStructGDMA
Channel Num
DEMO_DMA_CHANNEL_NUMTransfer Direction
Buffer Size
GDMA_TRANSFER_SIZESource 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_SendBufferDestination Address
GDMA_RecvBufferCall
GDMA_Initto initialize the GDMA peripheral.Configure the GDMA total transfer completion interrupt:
GDMA_INT_Transferand NVIC. For NVIC-related configuration, refer to Interrupt Configuration.
Call
GDMA_Cmdto enable GDMA channel for transmission.
Functional Implementation
-
After enabling GDMA, GDMA begins transferring data from
GDMA_SendBuffertoGDMA_RecvBuffer. Once the data transfer is completed, a GDMA total transfer completion interruptGDMA_INT_Transferinterrupt 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.GDMA_ClearAllTypeINT(DEMO_DMA_CHANNEL_NUM); /* Compare whether the destination data which transported by GDMA is equal to the source data*/ for (uint32_t i = 0; i < GDMA_TRANSFER_SIZE; i++) { if (GDMA_SendBuffer[i] != GDMA_RecvBuffer[i]) { IO_PRINT_ERROR3("demo_dma_handler: Data transmission error! index %d GDMA_SendBuffer = %d, GDMA_RecvBuffer = %d", i, GDMA_SendBuffer[i], GDMA_RecvBuffer[i]); } } IO_PRINT_INFO0("demo_dma_handler: Data transmission completion!");