Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

DMA - Code example
[DMA - Direct Memory Access]

 #include <otSerial.h>
 #include <otTimer.h>
 #include <otPrintfLite.h>
 #include <otMalloc.h>
 #include <otDMA.h>
 
 #define LEN 65536
 
 bool    done = false;
 U32     st, et;
 
 FAST_CODE_1 void dmaCallback()
 {
        et = TimerGetCounts(TIMER0);
        done = true;
 }
 
 void setup()
 {
        UartEnableSignals(UART0);
        UartSetBaud(UART0, 115200);
        setStdout(UART0); // printf output is now directed on UART0
        _initMalloc();
        TimerSetAsCounter(TIMER0);
        TimerEnable(TIMER0, true);
        DelayMs(2000);
 }
 
 void loop()
 {
        U32     * src, * dst;
        
        printf("Start DMA test\n");
        
        DmaSetInterruptService(DMA_CH_0, dmaCallback);
        
        src = (U32 *) _malloc(sizeof(U32) * LEN);
        dst = (U32 *) _malloc(sizeof(U32) * LEN);
        
        st = TimerGetCounts(TIMER0);    // Grab start time
        // Init src
        for(U32 i = 0; i < LEN; i++)
                src[i] = i;
        et = TimerGetCounts(TIMER0);    // Grab end time
        printf("Memory fill   elapsed time: %u uS\n", ElapsedTimeUs(st, et));
        
        st = TimerGetCounts(TIMER0);    // Grab start time
        DmaMove(DMA_CH_0, src, dst, LEN, DMA_DWORD);
        
        // Wait transfer complete
        while(!done) DelayUs(100);
                
        printf("DMA transfer  elapsed time: %u uS\n", ElapsedTimeUs(st, et));
        
        st = TimerGetCounts(TIMER0);    // Grab start time
        // Check the transfer
        for(U32 i = 0; i < LEN; i++)
                if(dst[i] != i)
                {
                        printf("Compare fail at [%d]\n", i);
                        goto end;
                }
        et = TimerGetCounts(TIMER0);    // Grab end time
        printf("Memory verify elapsed time: %u uS\n", ElapsedTimeUs(st, et));
                
        printf("Test OK\n");
        DmaEnableInterrupt(DMA_CH_0, false);
 end:
        DelayMs(10);
        IDLE;
 }
footer
otStudio - Library Reference - (C) 2020-23 Officina Turini, All Rights Reserved
Document built with Doxygen 1.4.0