Copyright © https://mongoose-os.com

Mongoose OS Forum

frame
ATTENTION! This forum has moved to:

https://community.mongoose-os.com

Do not post any new messages.

Need help building a SD Card library

Hi

For my commercial project, I need to read sound files from a SD Card (Flash is too small of course).

I've spent the past 5 weeks desperately trying to get the library to compile, with Javascript support, and the application to be able to use it. No success so far. I'm totally desperate for help.

Disclaimer: although I'm a former developper (javascript, Java) I'm totally Noob in C, C++ and micro-controller. I'm doing my best to learn fast but I'm certainly making atrocious mistakes that I'm not aware of.

I know a few people around here are hoping for a SD card library and even if I'm not the best around for this task I tried to get to it because I need it.
If someone can help me unlock this it would be a great addiction to the mongoose libs.

I've based my SD card library code on this https://github.com/pcbreflux/espressif/blob/master/esp32/app/ESP32_sd_card/main/sd_card_example_main.c, it's based on ESP-IDF native libraries.

So far I've been able to get the library itself to compile, with a lot of sweat and a few hacks. https://github.com/briancouchman/SD

But I'm still unable to get the simplest demo application to build or run with it.
I have tried using this
* in mos.yml

libs:
       - name: SD

* command : mos build --local --clean --platform esp32 --lib SD:/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/libs/SD
but I get this
AR /Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a
make[1]: Leaving directory '/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp
Target 'component-mosapp-build' responsible for '/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(SD.o):(.literal._ZN2SD4initEiiii+0x24): undefined reference to 'sdmmc_card_print_info'
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(SD.o):(.literal._ZN2SD9listFilesEv+0x14): undefined reference to 'opendir(char const*)'
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(SD.o):(.literal._ZN2SD9listFilesEv+0x18): undefined reference to 'readdir(DIR*)'
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(SD.o):(.literal._ZN2SD9listFilesEv+0x1c): undefined reference to 'closedir(DIR*)'
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(SD.o): In function 'SD::init(int, int, int, int)':
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/libs/SD/src/SD.cpp:55: undefined reference to 'sdmmc_card_print_info'
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(SD.o): In function 'SD::listFiles()':
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/libs/SD/src/SD.cpp:115: undefined reference to 'opendir(char const*)'
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/libs/SD/src/SD.cpp:121: undefined reference to 'readdir(DIR*)'
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/libs/SD/src/SD.cpp:127: undefined reference to 'closedir(DIR*)'
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(diskio_sdmmc.o):(.text.ff_sdmmc_read+0xc): undefined reference to 'sdmmc_read_sectors'
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(diskio_sdmmc.o): In function 'ff_sdmmc_read':
/Users/bcouchman/esp/esp-idf/components/fatfs/src/diskio_sdmmc.c:37: undefined reference to 'sdmmc_read_sectors'
/Users/bcouchman/esp/esp-idf/components/fatfs/src/diskio_sdmmc.c:41: undefined reference to 'sdmmc_write_sectors'
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(diskio_sdmmc.o): In function 'ff_sdmmc_write':
/Users/bcouchman/esp/esp-idf/components/fatfs/src/diskio_sdmmc.c:48: undefined reference to 'sdmmc_write_sectors'
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(vfs_fat_sdmmc.o):(.text.esp_vfs_fat_sdmmc_mount+0x20): undefined reference to 'sdmmc_card_init'
/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(vfs_fat_sdmmc.o): In function 'esp_vfs_fat_sdmmc_mount':
/Users/bcouchman/esp/esp-idf/components/fatfs/src/vfs_fat_sdmmc.c:41: undefined reference to 'sdmmc_card_init'
collect2: error: ld returned 1 exit status
/opt/Espressif/esp-idf/make/project.mk:388: recipe for target '/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/firmware.elf' failed
make: *** [/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/firmware.elf] Error 1

like if a c file was missing (reminder the library itself is compiling fine when I go in the directory of the library and run mos build --local --clean --platform esp32)

I have tried using - origin: https://github.com/briancouchman/SD instead in the mos.yml file
command: mos build --local --clean --platform esp32 --libs-update-interval 0m1s
And I get the same stack trace

I have tried using directly the compiled library instead
mos.yml

binary_libs:
 - /Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/libs/SD/build/lib.a

and this time I can build the application successfully, but there is no trace of the api_SD.js that is defined in the library, and therefore the application cannot really use the SD library.

mos.yml file of the application attached. Library code is here

Comments

  • I'm also interested in a SD card library as my Lolin32 Pro is on its way.
    I modified your library to make it compile and create the SD both in C and mJS: https://github.com/nliviu/SD

  • Thank you nliviu.
    I have been wondering how to cleanly include ESP native libraries and I couldn't figure it out. Great improvement.
    Have you been able to use the library in an application ? I still see errors when I include the library. It cannot find the implementations of opendir(char const*), readdir and closedir, like if the file vfs.c was missing.

    Target 'component-mosapp-build' responsible for '/Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a'
    /Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(SD.o):(.literal._ZN2SD9listFilesEv+0x14): undefined reference to 'opendir(char const*)'
    /Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(SD.o):(.literal._ZN2SD9listFilesEv+0x18): undefined reference to 'readdir(DIR*)'
    /Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(SD.o):(.literal._ZN2SD9listFilesEv+0x1c): undefined reference to 'closedir(DIR*)'
    /Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/firmware/build/objs/mosapp/libmosapp.a(SD.o): In function 'SD::listFiles()':
    /Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/libs/SD/src/SD.cpp:115: undefined reference to 'opendir(char const*)'
    /Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/libs/SD/src/SD.cpp:121: undefined reference to 'readdir(DIR*)'
    /Users/bcouchman/Documents/Perso/projects/meeio/electronics/mongooseos/libs/SD/src/SD.cpp:127: undefined reference to 'closedir(DIR*)'

    I included the library using this in mos.yml

    libs: 
      -name: SD

    and I'm not sure it's the right way to include a custom library.

    If I include your library on github directly with 'origin' I have 8 more function that have 'undefined reference'. I'm assuming I'm missing something at the application level to properly include the library.

    Thank you 1000 times for your help.

  • nliviunliviu Romania
    edited January 2018

    Example that compiles using mos build --local --clean
    main.c

    #include <mgos.h>
    #include "mgos_SD.h"
    
    enum mgos_app_init_result mgos_app_init(void)
    {
        SD* sd = mgos_sd_create();
        mgos_sd_begin(sd, 19, 23, 18, 5);
        mgos_sd_read(sd, "test1");
        mgos_sd_write(sd, "test2");
        mgos_sd_listFiles(sd);
        mgos_sd_close(sd);
    
        return MGOS_APP_INIT_SUCCESS;
    }
    

    Relevant parts of mos.yml


    libs_version: ${mos.version} modules_version: ${mos.version} mongoose_os_version: ${mos.version} platform: esp32 includes: - include # List of files / directories with C sources. No slashes at the end of dir names. sources: - src # List of dirs. Files from these dirs will be copied to the device filesystem filesystem: - fs config_schema: #debug - ["debug.level", 3] build_vars: # List of libraries used by this app, in order of initialisation libs: - origin: https://github.com/nliviu/SD

    LE. I'm using a similar directories layout and I include the local libraries in mos.yml. No need to add the --lib command line switch

      - origin: ../libs/SD
    
  • You are my hero! It compiles, and I can see the api_sd.js file now.
    I realize I really don't understand the subtleties and nuances in the various way there are in the different way to include a library. I picked your library from your github but don't understand what you changed to make it work.

    Now i'm going to work on the library itself to verify it can actually read and write from and to the SD card on my Huzzah 32

  • The most important changes are in mos.yml: removing all the include and source directories and adding

    build_vars:
      ESP_IDF_EXTRA_COMPONENTS: "${build_vars.ESP_IDF_EXTRA_COMPONENTS} fatfs wear_levelling sdmmc"
    

    Other changes are in mgos_SD.h to enable compiling in C:

    #ifdef __cplusplus
    #include "SD.h"
    #else
    typedef struct SDtag SD;
    #endif
    

    Moved all the #include ... from SD.h to SD.cpp (cosmetic).

    Corrected the ffi definitions in api_SD.js.

  • I completely overlooked the changes to enable C compiling in mgos_SD.h !
    Thank you, I think you have saved me many many many hours of pain, I'm so grateful.

    I have added proper read functions to the library and tested it in main.c with the precious help of a friend who is skilled in C:
    ` printf("In main.c - mgos_app_init_result\n");

    SD* sd = mgos_sd_create();
    // mgos_sd_begin(sd, 19, 23, 18, 5);
    mgos_sd_begin(sd, 19, 18, 5, 14);
    
    printf("In main.c - SD card mounted\n");
    
    
    mgos_sd_listFiles(sd);
    
    printf ("Reading FOO.TXT at once");
    uint8_t* buffer;
    int fileLength = mgos_sd_readFile(sd, "/sdcard/FOO.TXT", &buffer);
    printf("File length : %d\n", fileLength);
    printf("%s", buffer);
    free(buffer);
    
    
    
    printf ("Reading FOO.TXT in blocks");
    FILE* f = mgos_sd_openFile(sd, "/sdcard/FOO.TXT", "r");
    uint8_t buf[3];
    int bytesRead;
    while((bytesRead = mgos_sd_read(sd, f, buf, (size_t)sizeof(buf)))>0){
      printf("%.*s", bytesRead, (char *)buf);
      //feed buf to mext stage
    }
    printf("\n");
    mgos_sd_closeFile(sd, f);
    

    `
    These are the pins for my setup.

    I have pushed my latest changes to https://github.com/briancouchman/SD, do you want me to push the changes to your github too ?

  • Can we use SD card ESP-IDF SDK on mongoose OS?

Sign In or Register to comment.