What is the correct way to use the SDCard on the Daisy Patch?

Hi,
I’m struggling with the Daisy Patch SD-Card (with a SanDisk Extreme micro SDHC 32GB formatted in FAT32); I don’t find any documentation, only snippets of code.

On some pages/code I find

SdmmcHandler::Config sd_cfg;
sd_cfg.Defaults();
sdcard.Init(sd_cfg);
...

on others:

dsy_fatfs_init();
// Mount SD Card
f_mount(&SDFatFS, SDPath, 1);
hw.DelayMs( 1000 );

On others I found that a USE_FATFS = 1 should be added to the Makefile

I tried the latter combination but:

f_mount(&SDFatFS, SDPath, 1)

returns error 3 FR_NOT_READY ?!?

What are the correct APIs (and Makefile configuration/defines) to

  1. initialize the SD-card hardware and prepare to read/write it (FAT32)
  2. open a file
  3. read some bytes from it
  4. close the file

?

Thank you in advance!

(I’ll put the info on the cheatsheet :wink: )

@algoritmarte This recently changed in v3.0.0

The reason for the change was to support USB drives as well as SD cards, and simultaneous mounting of both SD and USB media.

In order to setup SD card usage now, you can follow the following steps:

  1. Initialize the SD Card hardware using SdmmcHandler and SdmmcHandler::Config as shown in your first code block
  2. Link the FatFS filesystem to the desired hardware using FatFSInterface
  3. Use FatFS API directly as expected – (mount, read, write, etc.)
// . . . Global variables, or part of hardware class
SdmmcHandler sdcard;
FatFSInterface fsi;

// . . . in main() {

// Init the hardware
SdmmcHandler::Config sd_cfg;
sd_cfg.Defaults();
sdcard.Init(sd_cfg);

// Link hardware and FatFS
fsi.Init(FatFSInterface::Config::MEDIA_SD);

// Get a reference to the SD card file system
FATFS& fs = fsi.GetSDFileSystem();

// Mount using fatfs:
f_mount(&fs, "/", 1);

// you can now use FatFS as expected to read/write, etc.

hope that helps!

Thank you very much!

I still have some doubts: before reading your answer, I was able to read a file using the old combo sdcard.Configure/Init + dsy_fatfs_init() + f_mount; then I tried the new version; but the FatFSInterface is not present in the libDaisy that I’m using.

Then I found that the libDaisy linked to the DaisyExamples + submodules repository doesn’t contain any FatFSInterface definition/implementation .

Is it ok ?

ah, your repo may be a little bit out of date.

you should be able to git pull on DaisyExamples to get any updates, and if you’ve never manually changed your libDaisy files then it should update to the most recent.

Otherwise, you can either git submodule update and you should see libDaisy update to commit f99ff47, or you can manually do this by navigating to libDaisy and running git checkout f99ff47

Once you’ve done that you can rebuild the libraries, and you should be good to go with the above advice.

The seed/SDMMC should be able to be compiled and run. You can use that example as a reference as well.

Odd, because I redownloaded it from scratch using SmartGit and the repository link GitHub - electro-smith/DaisyExamples: Examples for the Daisy Platform . Tomorrow I’ll try again.

@shensley is the STM firmware for daisy seed deployed when updating a program binary? For some daisy seeds that I have, HAL_SD_ERROR_INVALID_VOLTRANGE is returned by SD_PowerON in DaisyExamples/libdaisy/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_sd.c when following your above instructions.

For other daisy seeds, which I think are older. SD_PowerOn completes without error.

Is there a way to check daisy seed firmware versions? Does anyone know if there was a regression or change that may be causing this behavior?

@algoritmarte @Rawyawmedia , @shensley

Update for me:
Are you still seeing the PowerOn issue?

The code seems to be getting stuck on this while statement:

while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))

My count is well over 51,000 and not sure why it does not just give up after that many counts.

Here is the loop where this is happening:

/* SD CARD */
  /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
  while((count < SDMMC_MAX_VOLT_TRIAL) && (validvoltage == 0U))
  {
    /* SEND CMD55 APP_CMD with RCA as 0 */
    errorstate = SDMMC_CmdAppCommand(hsd->Instance, 0);
    if(errorstate != HAL_SD_ERROR_NONE)
    {
      return errorstate;
    }

    /* Send CMD41 */
    errorstate = SDMMC_CmdAppOperCommand(hsd->Instance, SDMMC_VOLTAGE_WINDOW_SD | SDMMC_HIGH_CAPACITY | SD_SWITCH_1_8V_CAPACITY);
    if(errorstate != HAL_SD_ERROR_NONE)
    {
      return HAL_SD_ERROR_UNSUPPORTED_FEATURE;
    }

    /* Get command response */
    response = SDMMC_GetResponse(hsd->Instance, SDMMC_RESP1);

    /* Get operating voltage*/
    validvoltage = (((response >> 31U) == 1U) ? 1U : 0U);

    count++;
  }

I did swap out the Daisy device, but I may look to find a different one to see if that makes a difference… and also just change the code to abort after trying just a few times to see what happens.

Thanks,
Brett

Here’s why it hasn’t given up at 51,000:

#define SDMMC_MAX_VOLT_TRIAL        ((uint32_t)0x0000FFFFU)

@tele_player ,

Thank you for that.

Two things here:

  1. Does that imply that my SDCard is no longer working as expected? I ask because I used to be able to read the SDCard just fine and now I cannot. Note: the SDCard works fine in my PC and i’ve tired a few others just in case it might have been the SDCard.

  2. I would propose that this be a much lower number as this was running for many seconds before I decided to hit the “stop and debug” button in VSCode. I’m wondering if a shorter check number or a shorter time limit would be good here. :slight_smile:

Thanks,
Brett