zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.88k stars 6.63k forks source link

SD Card init issue when CONFIG_SPEED_OPTIMIZATIONS=y #47238

Closed asarbs closed 2 years ago

asarbs commented 2 years ago

Describe the bug

When I'm turning on CONFIG_SPEED_OPTIMIZATIONS=y I'm getting sd:Card never left busy state error.

To Reproduce Steps to reproduce the behavior: prj.conf

CONFIG_STDOUT_CONSOLE=y

CONFIG_CPLUSPLUS=y
CONFIG_STD_CPP20=y
CONFIG_NEWLIB_LIBC=y
CONFIG_LIB_CPLUSPLUS=y

CONFIG_MAIN_STACK_SIZE=90000

CONFIG_DEBUG=y
CONFIG_PRINTK=y
CONFIG_LOG=y
CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y

CONFIG_DISK_ACCESS=y
CONFIG_FILE_SYSTEM=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_DISK_DRIVER_SDMMC=y
CONFIG_SPI=y

CONFIG_LOG_MODE_IMMEDIATE=y
CONFIG_PWM_LOG_LEVEL_DBG=y

CONFIG_FS_LITTLEFS_BLK_DEV=y
CONFIG_APP_LITTLEFS_STORAGE_BLK_SDMMC=y
CONFIG_FILE_SYSTEM_LITTLEFS=y

CONFIG_TIMING_FUNCTIONS=y

CONFIG_SPEED_OPTIMIZATIONS=y
CONFIG_FS_FATFS_NUM_FILES=10
            if (disk_access_init(disk_pdrv) != 0) {
                LOG_ERR("Storage init ERROR!");
                break;
            }
  1. west flash

I don't have this problem when i setup comment out CONFIG_SPEED_OPTIMIZATIONS=y from my config file.

Expected behavior I'm able to read from SD card files with CONFIG_SPEED_OPTIMIZATIONS=y

Impact I can't use CONFIG_SPEED_OPTIMIZATIONS=y

Logs and console output image

Environment (please complete the following information):

Additional context Add any other context that could be relevant to your issue, such as pin setting, target configuration, ...

asarbs commented 2 years ago
static int sdmmc_send_ocr(struct sd_card *card, int ocr_arg)
{
    struct sdhc_command cmd;
    int ret;
    int retries;

    cmd.opcode = SD_APP_SEND_OP_COND;
    cmd.arg = ocr_arg;
    cmd.response_type = (SD_RSP_TYPE_R3 | SD_SPI_RSP_TYPE_R1);
    cmd.timeout_ms = CONFIG_SD_CMD_TIMEOUT;
    /* Send initialization ACMD41 */
    for (retries = 0; retries < CONFIG_SD_OCR_RETRY_COUNT; retries++) {
        **LOG_INF("retries=%d", retries);**
        ret = sdmmc_app_command(card, 0U);
        if (ret == SD_RETRY) {
            /* Retry */
            continue;
        } else if (ret) {
            return ret;
        }
        ret = sdhc_request(card->sdhc, &cmd, NULL);
        if (ret) {
            /* OCR failed */
            return ret;
        }
        if (ocr_arg == 0) {
            /* Just probing, don't wait for card to exit busy state */
            return 0;
        }
        /*
         * Check to see if card is busy with power up. PWR_BUSY
         * flag will be cleared when card finishes power up sequence
         */
        if (card->host_props.is_spi) {
            if (!(cmd.response[0] & SD_SPI_R1IDLE_STATE)) {
                break;
            }
        } else {
            if ((cmd.response[0U] & SD_OCR_PWR_BUSY_FLAG)) {
                break;
            }
        }
        sd_delay(10);
    }
    if (retries >= CONFIG_SD_OCR_RETRY_COUNT) {
        /* OCR timed out */
        LOG_ERR("Card never left busy state");
        return -ETIMEDOUT;
    }
    LOG_DBG("SDMMC responded to ACMD41 after %d attempts", retries);
    if (!card->host_props.is_spi) {
        /* Save OCR */
        card->ocr = cmd.response[0U];
    }
    return 0;
}

After add of this LOG_INF line problem stop to occur.

carlescufi commented 2 years ago

@asarbs what board are you building for?

asarbs commented 2 years ago

I'm using mimxrt1020_evk

asarbs commented 2 years ago

I have do some more investigation and i have information that if i set CONFIG_SPEED_OPTIMIZATIONS=y, mount point flags suggest that this is READ ONLY File system and CONFIG_SPEED_OPTIMIZATIONS=n, mount point flags suggest that we can write.

asarbs commented 2 years ago

I have found solution mp.type = FS_FATFS; mp.fs_data = &fat_fs; mp.mnt_point = _disk_mount_pt; mp.flags = 0; if (fs_mount(&mp) != FR_OK) {

danieldegrasse commented 2 years ago

@asarbs Interesting that this fixes the issue- based on your initial description I expected the issue was that CMD55 (app command) was being sent to the card too soon after CMD8. If you replace the LOG_INF statement with k_busy_wait(100) is the issue also resolved?

asarbs commented 2 years ago

@danieldegrasse sorry last comment was my mistake, if we are talking about initial issue I still don't have solution.

danieldegrasse commented 2 years ago

@asarbs, sorry, missed this notification and was on PTO this past week. Will changing the LOG_INF statement out for k_busy_wait work to fix the issue?

asarbs commented 2 years ago

@danieldegrasse yes.