Memory-to-peripheral transfer moves data from memory to a device like a screen or sensor. It helps send information without making the CPU do all the work.
Memory-to-peripheral transfer in Embedded C
DMA_Channel->CCR = DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_EN; DMA_Channel->CPAR = (uint32_t)&PERIPHERAL_DATA_REGISTER; DMA_Channel->CMAR = (uint32_t)memory_address; DMA_Channel->CNDTR = data_length;
This example shows how to set up a DMA channel for memory-to-peripheral transfer.
DMA_CCR_DIR bit set means data moves from memory to peripheral.
DMA1_Channel3->CCR = DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_EN; DMA1_Channel3->CPAR = (uint32_t)&USART1->DR; DMA1_Channel3->CMAR = (uint32_t)tx_buffer; DMA1_Channel3->CNDTR = sizeof(tx_buffer);
DMA2_Channel5->CCR = DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_EN;
DMA2_Channel5->CPAR = (uint32_t)&SPI2->DR;
DMA2_Channel5->CMAR = (uint32_t)spi_tx_data;
DMA2_Channel5->CNDTR = 100;This program sets up a DMA channel to send the string "Hello DMA!" from memory to the USART1 peripheral automatically. The CPU does not need to send each character.
#include <stdint.h> #include "stm32f10x.h" // Example MCU header uint8_t message[] = "Hello DMA!"; void setup_dma_memory_to_peripheral(void) { // Enable clock for DMA1 RCC->AHBENR |= RCC_AHBENR_DMA1EN; // Disable DMA channel before configuration DMA1_Channel4->CCR &= ~DMA_CCR_EN; // Set peripheral address (e.g., USART1 data register) DMA1_Channel4->CPAR = (uint32_t)&USART1->DR; // Set memory address DMA1_Channel4->CMAR = (uint32_t)message; // Set number of data to transfer DMA1_Channel4->CNDTR = sizeof(message) - 1; // exclude null terminator // Configure DMA channel: // Memory increment mode enabled // Direction: memory to peripheral // Enable transfer complete interrupt (optional) DMA1_Channel4->CCR = DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_TCIE; // Enable DMA channel DMA1_Channel4->CCR |= DMA_CCR_EN; } int main(void) { // Setup USART1 here (not shown for simplicity) setup_dma_memory_to_peripheral(); while (1) { // Main loop can do other tasks } }
Memory-to-peripheral transfer is often done using DMA (Direct Memory Access) controllers.
Make sure the peripheral is ready to receive data before starting the transfer.
Always disable the DMA channel before changing its settings.
Memory-to-peripheral transfer moves data from memory to a device without CPU intervention.
DMA controllers handle this transfer efficiently.
Setup involves configuring source, destination, data size, and enabling the DMA channel.