DMA for LPC1768 with Mbed OS 6

Hello folks,

Those who are interested in using DMA (Direct Memory Access) on LPC1768 mbed boards with Mbed OS 6 please find a fork of Andy Kirkham’s MODDMA library here.

Below is an example of comparing DMA speed with memcpy.

#include "mbed.h"
#include "MODDMA.h"

DigitalOut  led1(LED1);
MODDMA      dma;
int         transferState = 0;
Timer       timer;


// Create a source buffer we are going to move.
static char src[16*1024-1] __attribute__((section("AHBSRAM0")));

// Create a buffer for the destination to copy to.
static char dst[sizeof(src)] __attribute__((section("AHBSRAM1")));

/**
 * @brief
 * @note
 * @param
 * @retval
 */
void myCallback(void)
{
    transferState = 1;
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
int main()
{
    timer.start();
    // Transfer/copy src content to dst using the memcpy function
    memcpy(dst, src, sizeof(src));
    timer.stop();
    printf("It took %lli us to transfer %i bytes using memcpy\r\n", timer.elapsed_time(), sizeof(src));

    // Create a MODDMA configuration object.
    MODDMA_Config*  conf = new MODDMA_Config;

    // Setup that configuration
    conf->channelNum(MODDMA::Channel_0);
    conf->srcMemAddr((uint32_t) & src);
    conf->dstMemAddr((uint32_t) & dst);
    conf->transferSize(sizeof(src));
    conf->transferType(MODDMA::m2m);
    conf->attach_tc(&myCallback);
    // config end

    // Pass the configuration to the controller
    dma.Setup(conf);

    timer.reset();
    timer.start();
    // Tell the controller to perform the DMA operation
    // defined by that configuration.
    dma.Enable(conf);
    while (transferState == 0); // wait for DMA transfer to be completed
    timer.stop();
    printf("It took %lli us to transfer %i bytes using DMA\r\n", timer.elapsed_time(), sizeof(src));

    while (1) {
        led1 = !led1;
        ThisThread::sleep_for(500ms);
    }
}

Printout:

It took 1204 us to transfer 16383 bytes using memcpy.
It took 96 us to transfer 16383 bytes using DMA.

3 Likes