2.3 Setting an Appropriate DMA Priority

Once the DMA has been configured, it is important to set an appropriate priority to the DMA as described in the “System Arbiter” section of the “PIC18 CPU” chapter of the device data sheet. The system arbiter grants memory access to the DMA if the PRLOCKED bit is set. Setting and clearing this bit requires a special sequence as an extra precaution against unexpected changes.

Code Setting the DMA Priority Lock demonstrates setting the Priority Lock.

Setting the DMA Priority Lock

void DMA_SetPriority(void)
{
    // This function is dependant on the PR1WAY CONFIG bit

    // Unlock first (if locked)
    PRLOCK = 0x55;
    PRLOCK = 0xAA;
    PRLOCKbits.PRLOCKED = 0;

    // Change priority as needed
    DMA1PR = 7;     // RX_DMA has highest priority
    DMA2PR = 7;     // TX_DMA has next highest priority

    // Lock again
    PRLOCK = 0x55;
    PRLOCK = 0xAA;
    PRLOCKbits.PRLOCKED = 1;
}