In this lesson, you will learn how to configure your DMA controller to notify the CPU only when a significant event occurs, preventing unnecessary CPU interruptions during ongoing data movement.
When we offload data transfers to a DMA controller, the main goal is to let the CPU sleep or focus on complex processing while the hardware handles the "heavy lifting" of moving memory. If we configured the DMA to interrupt the CPU for every single byte or word moved, we would lose the performance benefits of using a DMA in the first place. Instead, we use interrupt flags to signal only when the "job" is finished or if the hardware encounters a critical failure.
Configuring Completion and Error Interrupts
The DMA controller typically maintains a status register containing specific flags. To manage this efficiently, you must enable two specific interrupt masks:
- Transfer Complete Interrupt (TC): This flag is raised only after the DMA has moved the final element of your pre-defined buffer size. This tells the CPU, "The entire data block is ready for processing."
- Transfer Error Interrupt (TE): This flag is triggered if the DMA encounters a bus error (e.g., trying to access protected memory or an invalid address). This is critical because it alerts the CPU that the data is corrupted or the transfer has halted.
By enabling only these two bits, your interrupt service routine (ISR) remains quiet for 99% of the transfer time.
// Conceptually configuring DMA interrupt masks
DMA_Controller->INTERRUPT_MASK |= (DMA_TC_ENABLE | DMA_TE_ENABLE);
void DMA_IRQHandler(void) {
if (DMA_Controller->STATUS & DMA_TC_FLAG) {
// Handle successful batch processing
DMA_Controller->CLEAR_FLAGS = DMA_TC_FLAG;
} else if (DMA_Controller->STATUS & DMA_TE_FLAG) {
// Handle critical hardware error
DMA_Controller->CLEAR_FLAGS = DMA_TE_FLAG;
}
}
By filtering these events at the hardware level, we ensure the CPU only consumes clock cycles when a large data block is ready for analysis or when immediate intervention is required to recover from a system fault.
Key Takeaway: Configure your DMA interrupts to trigger only on the "Transfer Complete" or "Transfer Error" flags to maximize CPU efficiency and ensure robust error detection during background data transfers.