Mbed os does support ADC - DMA?

Does Mbed support DMA? Currently i’m write code, shown below -

Mbed_app.json
"target_overrides": {

  "NUCLEO_F103RB": {

    "target.printf_lib": "minimal-printf",

    "target.components_add": ["SD"],

    "platform.minimal-printf-enable-floating-point": false,

    "platform.stdio-minimal-console-only": true

  }

}

}

main.cpp

#include “mbed.h”

#include <stdio.h>

#include <errno.h>

#include

#include

#define BUFFER_MAX_LEN 10

#define FORCE_REFORMAT true

#include “SDBlockDevice.h”

BlockDevice *sd = SDBlockDevice::get_default_instance();

#include “FATFileSystem.h”

FATFileSystem fs(“fs”);

volatile uint16_t fileBuffer = NULL;

int bufferPtr = 0;

AnalogIn adcz(A0);

Ticker ticker;

Timer fileOpenTimer;

#define bufferSize 4096

uint16_t sensorReading[bufferSize];

unsigned int readPointer = 0;

volatile unsigned int writePointer = 0; // volatile so that the main loop knows to check for changes.

// opens the next unused file name in the format set.

// This can be a little slow the first time if there are already lots of log files

// since it tries each number in turn but the second call on will be fairly quick.

int counter = 0;

FILE *nextLogFile(void)

{

static unsigned int fileNumber = 0;

char fileName[32];

FILE *filePtr = NULL;

do {

    if (filePtr != NULL)

        fclose(filePtr);

    sprintf(fileName,"/fs/log%04u.txt",counter++);

    filePtr = fopen(fileName,"r");

} while (filePtr != NULL);

return fopen( fileName,"w");

}

void onSampleTick(void)

{

sensorReading[writePointer++] = adcz.read_u16(); 

if (writePointer == bufferSize)

    writePointer = 0;

if (writePointer == readPointer) {

    //Overflow

}

}

int main()

{

int err = fs.mount(sd);

if (err || FORCE_REFORMAT) {

    printf("formatting... ");

    fflush(stdout);

    err = fs.reformat(sd);

    if (err) {

        printf("badreform");

        error("error: %s (%d)\n", strerror(-err), err);

        return 1;

    }

}

FILE *myLogFile;

myLogFile = nextLogFile();

if (!myLogFile) {

    printf("notlog");

    // ERROR failed to open the first log file for writing.

    // The SD card is missing, not working, read only or full?



    return 1; // probably want to exit the program in this situation

}

fileOpenTimer.start();



ticker.attach(&onSampleTick,0.0001);

while(true)

{

    while (writePointer != readPointer) { // write any waiting data to the SD card

        fprintf(myLogFile,"%hu\r\n",sensorReading[readPointer++]);

        if (readPointer == bufferSize)

            readPointer = 0;

    }

    

    if (std::chrono::duration_cast<std::chrono::milliseconds>(fileOpenTimer.elapsed_time()).count() > 1000*60) {

        fclose(myLogFile); // close the current file

        printf("done");

        myLogFile = nextLogFile(); // open a new file

        if (!myLogFile) {

            // ERROR failed to open the log file for writing.

            break; // exit the while(true) loop

        }

        fileOpenTimer.reset(); // restart the timer

    }

}

}

How can i increase ADC speed and get more samples per second into SD-Card(For example read ~16Khz analogIn)? I know that i can use DMA in cubeMX(But i cannot implement interface with sd-card, so i want 2 to do it on Mbed), but how can i implement it in MBED? Can you please give me example, does MODDMA implement DMA?

Hello Vadim,

Can you please give me example, does MODDMA implement DMA?

Yes, the MODDMA library implements DMA and you can use it to transfer data from ADC to other peripheral or to RAM. However, it supports only the LPC1768 target. You can find an Mbed OS 6 version along with an example here. But I’m afraid that if you’d like to use DMA and ADC on STM targets you have to first create the base frame of your project with the STM32CubeIDE and set up ADC and DMA there. After that you can port it to Mbed and add more APIs. You can find an example here.

Best regards, Zoltan