2 years, 2 months ago.

Using SD card with mDot even possible?


For my application, I'm trying to use an SD card for data logging. I've been working on it for a few weeks now but haven't gotten much success.

Is it even possible to use an SD card with the mDot?

I'm using this SD card breakout: https://www.sparkfun.com/products/12941 Using a variety of SD cards ranging from 1GB to 32GB. Using Sandisk cards SDHC. 1GB card is SD

I've come upon the error of not being able to initialize my SD card at all, or if it does, it either gets stuck in mounting or any subsequent file call function using mbed's SDBlockDevice and FATFileSystem libraries. It has worked a couple of times before but for some reason, it just stopped working.

I've tried my SD breakout board in Arduino and I am able to initialize my cards but unable to read FAT32 partitions even though I've formatted them multiple times after each error. Have also tried multiple breakout boards and same results on both Arduino and mDot. It has worked before, and consistently with certain cards as well, but lately has been failing similarly

Below is my attempt at getting theSD card to work.

I always get error = -5005 when initializing fails and -5 when mounting. I've double checked every connection and continuity between wires and soldering pins and even faulty SD cards.

I am currently using libmdot-dev-mbed5 version 3.1.0 with mbed version 5.7.7

Which my next question is, what am I doing wrong?

Thank you very much.


SDBlockDevice sd(D11, D12, D13, D10,400000); // mosi, miso, sclk, cs
                                             // 400kHz because apparent frequency problems
                                             // Just trying some values, to no successes
FATFileSystem fs("sd"); 

//.........my code.........

int err = sd.init();
   if(err == 0)
        pc.printf("Init success \n\r");
    else pc.printf("Init failed: %d \n\r", err);

err = fs.mount(&sd);
    pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
    if(err) pc.printf("Error: %d\n\r", err);
    pc.printf("Error for mounting was %d\n\r", err);
        return err;
    // Open the file.
    pc.printf("Opening file '/sd/mytest/testfile.txt'... ");
    fp = fopen("/sd/mytest/testfile.txt", "w+");
    pc.printf("%s\r\n", (!fp ? "Failed :(\r\n" : "OK\r\n"));
    if (!fp)
        // Check whether directory '/sd/mytest' exists.
        pc.printf("\r\nChecking directory '/sd/mytest'...\r\n");
        struct stat info;
        err = stat("/sd/mytest", &info);
        if (err)
            pc.printf("Directory '/sd/mytest' does not exist.\r\n");
            pc.printf("Trying to create it...");
            err = mkdir("/sd/mytest", 0777);
            pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
        // Create a new 'testfile.txt' file.
        pc.printf("File not found, creating a new one...\r\n");
        fp = fopen("/sd/mytest/testfile.txt", "w+");
        pc.printf("%s\r\n", (!fp ? "Failed :(" : "OK"));
        if (!fp)
            error("error: %s (%d)\r\n", strerror(errno), -errno);
    for (int i = 0; i < 10; i++)
        pc.printf("Writing numbers (%d/%d)... ", i, 10);
        err = fprintf(fp, "    %d\r\n", i);
        if (err < 0)
            pc.printf("Fail :(\r\n");
            error("error: %s (%d)\r\n", strerror(errno), -errno);
    pc.printf("Writing numbers (%d/%d)... OK\r\n\r\n", 10, 10);
    err = fclose(fp);
    pc.printf("Closing file '/sd/mytest/testfile.txt'... ");
    pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));
    pc.printf("Unmounting as well\n\r");
    err = fs.unmount();
    pc.printf("%s\r\n", (err ? "Failed :(\r\n" : "OK\r\n"));

Question relating to:

The MultiConnect® mDot™ offers significantly longer range and improved radio performance compared to traditional wireless solutions—resulting in greater transmission range and reduced capital expense.

What are your electrical connections like? Long/short wires? (a photo is good if possible, different people have different definitions of good wiring.)

What's the power source to the SD card?

Flaky, intermittent or varying behavior could be a sign of electrical problems rather than software issues.

posted by Andy A 19 Apr 2018

I am following this guide. CMD is MOSI, D0 is MISO, CLK is Clock, and D3 is CS/NSSI /media/uploads/SDesign2018/full_sd_with_5v_bb.jpg /media/uploads/SDesign2018/20180419_130037.jpg I took out the Vss and GND pins so it's clearer it see which wires go to which pins on the mDot dev board. Vss is +3.3V coming from the dev board.


posted by Denwis La 19 Apr 2018

The wiring isn't great but should be ok as long as you keep the speed low. Two suggestions: Firstly if you are using that level shiftier board shown in the first diagram then get rid of it. Everything is 3.3V IO so it's only possible contribution is to distort signals and make things less reliable. Secondly with wires like that you'll have a lot of inductance on your SD card power supply connections. Add a capacitor (anything from 1uF to 0.1uF is fine, you can go a little larger/smaller if you have to) between VCC and GND as close to the SD card as possible. Either soldered on the SD board or plugged in right next to it.

posted by Andy A 20 Apr 2018

Thank you very much for your suggestion. Power was intentionally taken out in the picture to better see the wires used for SPI connection. Everything seems to be working fine after adding a 0.1uF decoupling capacitor for the SD board. Another problem that was happening was the breadboard dropping the voltage at power enough to affect the board's performance. The breadboard dropped the voltage to around 3.15V from 3.3V, so I just took the 3.3V straight from the dev board to the SD power pins and that solved everything.

posted by Denwis La 22 Apr 2018

1 Answer

2 years, 1 month ago.

Hi, the SDFileSystem_HelloWorld example worked for me:

  1. include "mbed.h"
  2. include "SDFileSystem.h"
  3. include <string> filenames are stored in a vector string
  4. include <vector> filenames are stored in a vector string

SDFileSystem sd(p5, p6, p7, p8, "sd"); the pinout on the mbed Cool Components workshop board SDFileSystem sd(PA_7, PA_6, PA_5, PA_1, "sd"); the pinout on the mDot vector<string> filenames; filenames are stored in a vector string

void write1() { printf("write1() Write to SD card\r\n"); mkdir("/sd/mydir", 0777); FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); if(fp == NULL) { error("\tCould not open file for write\r\n"); } else { printf("\twrite1() SUCCESS\r\n"); } fprintf(fp, "Hello fun SD Card World!"); fclose(fp); }

void write2() { sd.mount(); const char *filePath = "/sd/mydir/sdtest.txt"; printf("write2() Write to SD card: %s\r\n", filePath);

FILE *file = fopen("/sd/mydir/sdtest.txt", "w"); if (file == NULL) { printf("\tFailed writeFile()"); } else { fprintf(file, "0,1,2,3,4,5,6,7,8,9\n"); fclose(file); printf("\twrite2() SUCCESS\r\n"); } should free card to be removed sd.unmount(); }

void read() { unsigned char c; a single byte buffer sd.mount(); printf("\r\nread() fopen /sd/mydir/sdtest.txt"); FILE *file = fopen("/sd/mydir/sdtest.txt", "r"); if (file == NULL) { printf("\tFailed fopen(...)"); } else { printf("\tSuccess fopen(...)"); }

printf("\r\nread() while (c != '
n') /sd/mydir/sdtest.txt\r\n"); while (c != '\n') { c = getc(file); printf("%c\r\n",c); }

fclose(file); sd.unmount(); }

int main() { printf("\r\nStarting...\n\r");

write1(); wait(0.1); write2(); wait(0.1); read(); wait(0.1);

printf("\r\nGoodbye World!\n"); }