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.48k stars 6.41k forks source link

Unstable SD Card performance on Teensy 4.1 #43326

Closed EmbedWise closed 2 years ago

EmbedWise commented 2 years ago

Describe the bug No deterministic behavior when using the SD card, mounting, reading and writing

The Teensy 4.1 does not have a card present gpio detection and no power control. So using a dummy GPIO.

Using SD Card formatter 5.0.2 Type: SDHC Capacity: 3.96 GB Overwrite Format

Is recognized by MacOs as MS-DOS (FAT32)

7F18E168-A89E-48BE-9CD3-874F13FC88DC

SD CARD Panasonic MicroSD HC I 4GB

To Reproduce Using SD card sample code to mount and lsdir

Expected behavior Constant behavior of mounting and listing dir.

Logs and console output First run:

[00:00:05.052,000] <inf> usdhc: SD inserted!
[00:00:05.135,000] <inf> sdhc: Block count 7741440
[00:00:05.136,000] <inf> sdhc: Sector size 512
[00:00:05.136,000] <inf> sdhc: Memory Size(MB) 3780        
[00:00:05.137,000] <err> fs: fs mount error (-30)        
[00:00:05.137,000] <err> sdhc: Error mounting disk.

Second Run:

[00:00:05.052,000] <inf> usdhc: SD inserted!
[00:00:05.133,000] <inf> sdhc: Block count 7741440
[00:00:05.133,000] <inf> sdhc: Sector size 512
[00:00:05.133,000] <inf> sdhc: Memory Size(MB) 3780
[00:00:05.134,000] <err> fs: fs mount error (-5)
[00:00:05.134,000] <err> sdhc: Error mounting disk.

After 5 times it can lsdir

[00:00:05.052,000] <inf> usdhc: SD inserted!
[00:00:05.135,000] <inf> sdhc: Block count 7741440
[00:00:05.135,000] <inf> sdhc: Sector size 512
[00:00:05.135,000] <inf> sdhc: Memory Size(MB) 3780
[00:00:05.137,000] <inf> sdhc: Disk mounted.
[00:00:05.137,000] <inf> sdhc: Listing dir /SD: ...
[00:00:05.138,000] <inf> sdhc: [DIR] .Spotlight-V100
[00:00:05.138,000] <inf> sdhc: [DIR] .fseventsd

When finally mounting and listing works the next step was to write, which then also returns -30, showing it is mounted readonly. After enough power off power on it can create a file write. (lucky shot)

Environment (please complete the following information):

EmbedWise commented 2 years ago

Additionally, It is for me unclear what level I should debug. Hardware / Driver / Filesystem.

Also tried a different SD Card SanDisk Edge 4GB HC I, similar behavior.

danieldegrasse commented 2 years ago

@EmbedWise can you run the this test with CONFIG_SDMMC_LOG_LEVEL_DBG=y? Also, do you have a sample that creates files on the card?

The mount error with a return value of -5 is probably occurring at the driver layer, in the usdhc.c driver. The mount error with a return value of -30 may be due to a corrupted read from the SD card, which is likely an issue at the driver layer resulting from incorrect initialization.

EmbedWise commented 2 years ago

@danieldegrasse thanks for your reply. Running with the following config:

# SDHC
CONFIG_FILE_SYSTEM=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_FS_FATFS_EXFAT=y
CONFIG_FS_FATFS_LFN=y # support long filenames
CONFIG_FS_FATFS_MOUNT_MKFS=y
CONFIG_SDMMC_USDHC=y
CONFIG_DISK_DRIVER_SDMMC=y
CONFIG_SDMMC_LOG_LEVEL_DBG=y
CONFIG_FILE_SYSTEM_SHELL=y
CONFIG_HEAP_MEM_POOL_SIZE=32768

After power off and on

 <inf> usdhc: SD inserted!
[00:00:05.132,000] <inf> sdhc: Block count 7741440
[00:00:05.132,000] <inf> sdhc: Sector size 512
[00:00:05.132,000] <inf> sdhc: Memory Size(MB) 3780
[00:00:05.132,000] <dbg> usdhc.disk_usdhc_access_read: sector=0 count=1
[00:00:05.133,000] <dbg> usdhc.disk_usdhc_access_read: sector=8192 count=1
[00:00:05.134,000] <err> fs: fs mount error (-30)
[00:00:05.134,000] <err> sdhc: Error mounting disk.

2nd time

[00:00:05.052,000] <inf> usdhc: SD inserted!
[00:00:05.132,000] <err> usdhc: CMD17 Polling ERROR
[00:00:05.132,000] <err> usdhc: USDHC bus device initalization failed!
[00:00:05.132,000] <err> sdhc: Storage init ERROR!

3rd

[00:00:05.052,000] <inf> usdhc: SD inserted!
[00:00:05.133,000] <inf> sdhc: Block count 7741440
[00:00:05.133,000] <inf> sdhc: Sector size 512
[00:00:05.133,000] <inf> sdhc: Memory Size(MB) 3780
[00:00:05.133,000] <dbg> usdhc.disk_usdhc_access_read: sector=0 count=1
[00:00:05.134,000] <dbg> usdhc.disk_usdhc_access_read: sector=8192 count=1
[00:00:05.134,000] <err> fs: fs mount error (-30)
[00:00:05.134,000] <err> sdhc: Error mounting disk.

4th

[00:00:05.052,000] <inf> usdhc: SD inserted!
[00:00:05.131,000] <inf> sdhc: Block count 7741440
[00:00:05.131,000] <inf> sdhc: Sector size 512
[00:00:05.131,000] <inf> sdhc: Memory Size(MB) 3780
[00:00:05.131,000] <dbg> usdhc.disk_usdhc_access_read: sector=0 count=1
[00:00:05.132,000] <dbg> usdhc.disk_usdhc_access_read: sector=8192 count=1
[00:00:05.132,000] <err> usdhc: CMD17 Polling ERROR
[00:00:05.132,000] <err> fs: fs mount error (-30)
[00:00:05.132,000] <err> sdhc: Error mounting disk.

Note that this was tested still under Zephyr 2.7.1. with defining a virtual cd-gpio

I will conduct the test also with the newer version of Zephyr 3.0.xx.

EmbedWise commented 2 years ago

@danieldegrasse Conducted the test with the newest Zephyr (zephyr-v3.0.0-666-gfeffe639a3f8)

detect-dat3

[00:00:05.000,000] <inf> usdhc: USDHC detection other than GPIO
[00:00:05.000,000] <inf> usdhc: USDHC detection using DAT3 pull
[00:00:05.001,000] <err> sdhc: Storage init ERROR!
[00:00:05.001,000] <err> fs: requested file system type not registered!!

Phony cd-gpio (cd-gpios = < &gpio3 21 GPIO_ACTIVE_HIGH>;)

[00:00:05.001,000] <err> usdhc: NO SD inserted!
[00:00:05.001,000] <err> sdhc: Storage init ERROR!
[00:00:05.001,000] <err> fs: requested file system type not registered!!

Moreover tested the same hardware with the Teensyduino environment that one is working flawlessly with the same SD card. Arduino also supports the fat32 and I wrote 2000 files and read them back with no issues also after multiple power off and power on sequences. My Mac can also read these files with and sdcard reader.

danieldegrasse commented 2 years ago

@EmbedWise from the logs you have sent, it looks like data reads are failing (CMD17 corresponds to single block reads). There were some changes between 3.0 and 2.7 regarding CMD17, specifically #40522 which polls the card with CMD13 before sending data. I've opened #43414, which drops the detect-dat3 flag from the USDHC configuration on the teensy 4.1, and changes the USDHC driver to assume the card is present if no detection method is configured. Can you try that PR and see if the FATFS sample works for you?

EmbedWise commented 2 years ago

@danieldegrasse I used 3280e385506c474112f175d6bb6bee09d30aa755 of your respository made sure USB is working in the FATFS sample:

CONFIG_DISK_ACCESS=y
CONFIG_LOG=y
CONFIG_FILE_SYSTEM=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_PRINTK=y
CONFIG_MAIN_STACK_SIZE=2048

## ADDED these below for USB and logging

CONFIG_SDMMC_LOG_LEVEL_DBG=y

# USB
CONFIG_USB_DEVICE_STACK=y
#CONFIG_USB_UART_CONSOLE=y NOT necessary from Zephyr 3.0.0 onwards
CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
CONFIG_USB_DEVICE_PRODUCT="TEENSY USB CONSOLE"
CONFIG_USB_DEVICE_VID=0x2FE4
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_UART_LINE_CTRL=y

CONFIG_SERIAL=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_UART_LINE_CTRL=y

created the overlay:

/ {
    chosen {
        zephyr,console = &cdc_acm_uart0;
        zephyr,shell-uart = &cdc_acm_uart0;
    };
};

&zephyr_udc0 {
    cdc_acm_uart0: cdc_acm_uart0 {
        compatible = "zephyr,cdc-acm-uart";
        label = "CDC_ACM_0";
    };
};
/*
 * Copyright (c) 2019 Tavish Naruka <tavishnaruka@gmail.com>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/* Sample which uses the filesystem API and SDHC driver */

#include <zephyr.h>
#include <device.h>
#include <usb/usb_device.h>
#include <storage/disk_access.h>
#include <logging/log.h>
#include <fs/fs.h>
#include <ff.h>

#define START_DELAY 2000

LOG_MODULE_REGISTER(main);

static int lsdir(const char *path);

static FATFS fat_fs;
/* mounting info */
static struct fs_mount_t mp = {
    .type = FS_FATFS,
    .fs_data = &fat_fs,
};

/*
*  Note the fatfs library is able to mount only strings inside _VOLUME_STRS
*  in ffconf.h
*/
static const char *disk_mount_pt = "/SD:";

void main(void)
{
    uint32_t version = sys_kernel_version_get();

    if (usb_enable(NULL)) {
        return;
    }

    k_msleep(START_DELAY);

    LOG_INF("Started zephyr %u.%u.%u on board %s/%s.", SYS_KERNEL_VER_MAJOR(version), SYS_KERNEL_VER_MINOR(version), SYS_KERNEL_VER_PATCHLEVEL(version), CONFIG_ARCH, CONFIG_BOARD);

    /* raw disk i/o */
    do {
        static const char *disk_pdrv = "SD";
        uint64_t memory_size_mb;
        uint32_t block_count;
        uint32_t block_size;

        if (disk_access_init(disk_pdrv) != 0) {
            LOG_ERR("Storage init ERROR!");
            break;
        }

        if (disk_access_ioctl(disk_pdrv,
                DISK_IOCTL_GET_SECTOR_COUNT, &block_count)) {
            LOG_ERR("Unable to get sector count");
            break;
        }
        LOG_INF("Block count %u", block_count);

        if (disk_access_ioctl(disk_pdrv,
                DISK_IOCTL_GET_SECTOR_SIZE, &block_size)) {
            LOG_ERR("Unable to get sector size");
            break;
        }
        printk("Sector size %u\n", block_size);

        memory_size_mb = (uint64_t)block_count * block_size;
        printk("Memory Size(MB) %u\n", (uint32_t)(memory_size_mb >> 20));
    } while (0);

    mp.mnt_point = disk_mount_pt;

    int res = fs_mount(&mp);

    if (res == FR_OK) {
        printk("Disk mounted.\n");
        lsdir(disk_mount_pt);
    } else {
        printk("Error mounting disk.\n");
    }

    while (1) {
        k_sleep(K_MSEC(1000));
    }
}

static int lsdir(const char *path)
{
    int res;
    struct fs_dir_t dirp;
    static struct fs_dirent entry;

    fs_dir_t_init(&dirp);

    /* Verify fs_opendir() */
    res = fs_opendir(&dirp, path);
    if (res) {
        printk("Error opening dir %s [%d]\n", path, res);
        return res;
    }

    printk("\nListing dir %s ...\n", path);
    for (;;) {
        /* Verify fs_readdir() */
        res = fs_readdir(&dirp, &entry);

        /* entry.name[0] == 0 means end-of-dir */
        if (res || entry.name[0] == 0) {
            break;
        }

        if (entry.type == FS_DIR_ENTRY_DIR) {
            printk("[DIR ] %s\n", entry.name);
        } else {
            printk("[FILE] %s (size = %zu)\n",
                entry.name, entry.size);
        }
    }

    /* Verify fs_closedir() */
    fs_closedir(&dirp);

    return res;
}

Following results

[00:00:00.545,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
[00:00:00.545,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
[00:00:00.545,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
[00:00:00.545,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
Sector size 512
Memory Size(MB) 3780
Disk mounted.

Listing dir /SD: ...
[DIR ] SPOTLI~1
[FILE] TEST.TXT (size = 36)
[DIR ] FSEVEN~1
[DIR ] TEST
[00:00:02.000,000] <inf> main: Started zephyr 3.0.99 on board arm/teensy41.
[00:00:02.000,000] <inf> usdhc: No SD detection method configured, assuming SD present
[00:00:02.086,000] <inf> main: Block count 7741440
[00:00:02.087,000] <dbg> usdhc: disk_usdhc_access_read: sector=0 count=1
[00:00:02.087,000] <dbg> usdhc: disk_usdhc_access_read: sector=8192 count=1
[00:00:02.088,000] <dbg> usdhc: disk_usdhc_access_read: sector=8193 count=1
[00:00:02.089,000] <dbg> usdhc: disk_usdhc_access_read: sector=16384 count=1
[00:00:01.282,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
[00:00:01.282,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
[00:00:01.282,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
[00:00:01.282,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
Sector size 512
Memory Size(MB) 3780
Error mounting disk.
[00:00:02.000,000] <inf> main: Started zephyr 3.0.99 on board arm/teensy41.
[00:00:02.000,000] <inf> usdhc: No SD detection method configured, assuming SD present
[00:00:02.087,000] <inf> main: Block count 7741440
[00:00:02.088,000] <dbg> usdhc: disk_usdhc_access_read: sector=0 count=1
[00:00:02.089,000] <dbg> usdhc: disk_usdhc_access_read: sector=8192 count=1
[00:00:02.089,000] <err> usdhc: CMD17 Polling ERROR
[00:00:02.089,000] <dbg> usdhc: disk_usdhc_access_write: sector=0 count=1
[00:00:02.089,000] <err> fs: fs mount error (-5)
[00:00:00.814,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
[00:00:00.814,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
[00:00:00.814,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
Sector size 512
Memory Size(MB) 3780
Disk mounted.

Listing dir /SD: ...
[00:00:02.000,000] <inf> main: Started zephyr 3.0.99 on board arm/teensy41.
[00:00:02.000,000] <inf> usdhc: No SD detection method configured, assuming SD present
[00:00:02.086,000] <inf> main: Block count 7741440
[00:00:02.087,000] <dbg> usdhc: disk_usdhc_access_read: sector=0 count=1
[00:00:02.088,000] <dbg> usdhc: disk_usdhc_access_read: sector=8192 count=1
[00:00:02.088,000] <dbg> usdhc: disk_usdhc_access_read: sector=8193 count=1
[00:00:02.090,000] <dbg> usdhc: disk_usdhc_access_read: sector=16384 count=1
[00:00:02.090,000] <err> usdhc: CMD17 Polling ERROR
[00:00:02.090,000] <err> fs: directory read error (-5)
[00:00:01.239,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
[00:00:01.239,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
[00:00:01.239,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
Sector size 512
Memory Size(MB) 3780
Disk mounted.

Listing dir /SD: ...
[DIR ] SPOTLI~1
[FILE] TEST.TXT (size = 36)
[DIR ] FSEVEN~1
[DIR ] TEST
[00:00:02.000,000] <inf> main: Started zephyr 3.0.99 on board arm/teensy41.
[00:00:02.000,000] <inf> usdhc: No SD detection method configured, assuming SD present
[00:00:02.085,000] <inf> main: Block count 7741440
[00:00:02.086,000] <dbg> usdhc: disk_usdhc_access_read: sector=0 count=1
[00:00:02.087,000] <dbg> usdhc: disk_usdhc_access_read: sector=8192 count=1
[00:00:02.088,000] <dbg> usdhc: disk_usdhc_access_read: sector=8193 count=1
[00:00:02.089,000] <dbg> usdhc: disk_usdhc_access_read: sector=16384 count=1
[00:00:00.967,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
[00:00:00.967,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
[00:00:00.967,000] <inf> usb_cdc_acm: Ring buffer full, drain buffer
Sector size 512
Memory Size(MB) 3780
Disk mounted.

Listing dir /SD: ...
[DIR ] SPOTLI~1
[FILE] TEST.TXT (size = 36)
[DIR ] FSEVEN~1
[DIR ] TEST
[00:00:02.000,000] <inf> main: Started zephyr 3.0.99 on board arm/teensy41.
[00:00:02.000,000] <inf> usdhc: No SD detection method configured, assuming SD present
[00:00:02.085,000] <inf> main: Block count 7741440
[00:00:02.086,000] <dbg> usdhc: disk_usdhc_access_read: sector=0 count=1
[00:00:02.086,000] <err> usdhc: CMD13 Polling ERROR
[00:00:02.086,000] <dbg> usdhc: usdhc_send_status: Send CMD13 failed with host error -5
[00:00:02.086,000] <dbg> usdhc: disk_usdhc_access_read: sector=8192 count=1
[00:00:02.087,000] <dbg> usdhc: disk_usdhc_access_read: sector=8193 count=1
[00:00:02.088,000] <dbg> usdhc: disk_usdhc_access_read: sector=16384 count=1

Note the different outputs after turning off waiting 5-10 seconds and turn it back on

danieldegrasse commented 2 years ago

Note the different outputs after turning off waiting 5-10 seconds and turn it back on To clarify, the card works if you wait a few seconds, but does not work if you immediately retry? Or the other way around?

I've tried to reproduce this on several of our RT series evaluation kits, without any luck. The core issue is probably either a timing difference between the teensy board and the evaluation kits, or a timing difference with the specific SD card you are using. Can you try running samples/subsys/fs/fat_fs using this PR: https://github.com/zephyrproject-rtos/zephyr/pull/43173? The SD driver has several timing issues, and that RFC has a rewrite of the driver.

EmbedWise commented 2 years ago

@danieldegrasse thanks for the suggestion.

I checked out #43173 with the commit 3c7fcabfa90ca8b45f2badc2054c4a948a50977f. However I miss the file fsl_usdhc.h at the building process:

zephyr/drivers/sdhc/imx_usdhc.c:21:10: fatal error: fsl_usdhc.h: No such file or directory
   21 | #include <fsl_usdhc.h>

Also checked out branch sdmmc_rfc from NXP and have the same issue.

Is there something I need to do after I have checked out the PR? like west update?

I tried with the following config:

CONFIG_DISK_ACCESS=y
CONFIG_LOG=y
CONFIG_FILE_SYSTEM=y
CONFIG_FAT_FILESYSTEM_ELM=y
CONFIG_PRINTK=y
CONFIG_MAIN_STACK_SIZE=2048

CONFIG_IMX_USDHC=y
CONFIG_SDMMC_SUBSYS=y
CONFIG_DISK_DRIVER_SDMMC=y
CONFIG_SDHC=y

CONFIG_SDMMC_LOG_LEVEL_DBG=y

# USB
CONFIG_USB_DEVICE_STACK=y
#CONFIG_USB_UART_CONSOLE=y NOT necessary from Zephyr 3.0.0 onwards
CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
CONFIG_USB_DEVICE_PRODUCT="TEENSY USB CONSOLE"
CONFIG_USB_DEVICE_VID=0x2FE4
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_UART_LINE_CTRL=y

CONFIG_SERIAL=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_UART_LINE_CTRL=y
danieldegrasse commented 2 years ago

@EmbedWise, yes you do need to run west update. Sorry for not mentioning that.

EmbedWise commented 2 years ago

@danieldegrasse I am most likely missing something very simple but when I conduct west update it shows:

=== updating zephyr (zephyr):
--- zephyr: fetching, need revision main
From github.com:EmbedWise/zephyr
 * branch                  main       -> FETCH_HEAD
HEAD is now at feffe639a3 soc: xtensa: adsp: Fix i.MX8 hw_cycles_per_sec definition
WARNING: left behind zephyr branch "sdmmc_rfc"; to switch back to it (fast forward):
  git -C . checkout sdmmc_rfc
=== updating nrfxlib (nrfxlib):
HEAD is now at dae7bfc bsdlib: version 0.7.5
=== updating mbedtls-nrf (mbedtls):
HEAD is now at 2a1d9332d Merge pull request #682 from ARMmbed/prepare-rc-2.16.6-updated
=== updating canopennode (modules/lib/canopennode):
HEAD is now at 53d3415 Merge pull request #5 from teslabs/module-name
=== updating civetweb (modules/lib/civetweb):
HEAD is now at 094aeb41 cmake: relax minimum version requirements
=== updating cmsis (modules/hal/cmsis):
HEAD is now at b0612c9 DSP: Integrate CMSIS-DSP 1.9.0 (CMSIS 5.8.0)
=== updating edtt (tools/edtt):
HEAD is now at 1fc2e6f Fix Database Hash value after GATT fix
=== updating fatfs (modules/fs/fatfs):
HEAD is now at 9237454 fat_fs: Add support for the "win" variable alignment in FATFS struct
=== updating hal_nordic (modules/hal/nordic):
HEAD is now at 1f9145e drivers: nrf_802154: Update the IEEE 802.15.4 component
=== updating hal_nxp (modules/hal/nxp):
HEAD is now at 9629f15 hal_cmake: Include system.c for MXRT6xx and MXRT5xx
=== updating hal_st (modules/hal/st):
HEAD is now at 575de9d sensor/stmemsc: Align stmemsc i/f to v2.00
=== updating libmetal (modules/hal/libmetal):
HEAD is now at f237c9d lib: update libmetal to release v2021.10.0
=== updating littlefs (modules/fs/littlefs):
HEAD is now at 7b2cf4b zephyr: move CMake glue code for building as a module into Zephyr
=== updating loramac-node (modules/lib/loramac-node):
HEAD is now at 12019623 zephyr: radio: sx127x: override buffer read/write functions
=== updating lvgl (modules/lib/gui/lvgl):
HEAD is now at aae1c111 zephyr: pull in the lvgl glue code from the zephyr tree
=== updating mbedtls (modules/crypto/mbedtls):
HEAD is now at 3e3e58a92 mbedtls: remove static keyword from certain function pointers
=== updating mcuboot (bootloader/mcuboot):
HEAD is now at 89936c3 Merge pull request #72 from nvlsianpu/fix/align_crc_api
=== updating mipi-sys-t (modules/debug/mipi-sys-t):
HEAD is now at d9da086 library: Add support for vprintf
=== updating net-tools (tools/net-tools):
HEAD is now at f49bd13 net-capture: Listen captured traffic and save to pcap file
=== updating nrf_hw_models (modules/bsim_hw_models/nrf_hw_models):
HEAD is now at b8cea37 Added overflow event handling and trigger overflow task
=== updating open-amp (modules/lib/open-amp):
HEAD is now at cfd050f lib: update open-amp lib to release v2021.10.0
=== updating openthread (modules/lib/openthread):
HEAD is now at 0e141a556 zephyr: Simplify zephyr integration of openthread.
=== updating segger (modules/debug/segger):
HEAD is now at 3a52ab2 config: Use zephyr provided options
=== updating tinycbor (modules/lib/tinycbor):
HEAD is now at 40daca9 zephyr: Remove TINYCBOR from interface libraries
=== updating tinycrypt (modules/crypto/tinycrypt):
HEAD is now at 3e9a49d cmake: Fix conditional in root CMakeLists.txt

Since it leaves the branch I conduct:

➜  zephyr git:(feffe639a3) git -C . checkout sdmmc_rfc

But I still miss the file:

zephyr/drivers/sdhc/imx_usdhc.c:21:10: fatal error: fsl_usdhc.h: No such file or directory
   21 | #include <fsl_usdhc.h>

When I search for them they are in modules/hal/nxp/mcux/mcux-sdk/drivers/usdhc

  1. What is the proper way to include these files and are these the correct ones?
  2. Which evaluation kit comes closest to what the Teensy looks like on SD card features?
  3. Is there an easy way to reduce the communication speed towards the SD card

It is directly connected to 3v3. image

danieldegrasse commented 2 years ago

That is very strange. The typical usage case I see for west update is to update the modules zephyr depends on, but that commandline seems to imply that when you run west update it also updates the branch of zephyr you are currently using. Could you checkout the PR, run git rev-parse HEAD and verify it returns 7621a728a909712f52d04005cc430bd1f326d647, then try west update?

Regarding your questions:

What is the proper way to include these files and are these the correct ones?

Those files are included via mcux/hal_nxp.cmake in the NXP HAL, using the include_driver_ifdef directive. The files you mentioned are the correct ones, yes.

Which evaluation kit comes closest to what the Teensy looks like on SD card features?

Probably the RT1060 EVK, provided that the user removes the chip detect and power control GPIOs from the devicetree. The SOC is the same, and without power or chip detect functionality the only significant difference is likely the signal routing on the RT1060 vs the teensy41.

The main difference I see between the teensy41 schematic and the RT1060 EVK is a lack of decoupling capacitors on the SD clock line, see the RT1060 EVK schematic here: image

Is there an easy way to reduce the communication speed towards the SD card

There is no direct support for this in the driver, but the card does change bus speeds several times. The driver is designed to negotiate a clock speed of 50MHz, but the card starts at 400KHz. The errors you are seeing do all seem to occur at that higher clock speed, however it looks like the Arduino teensy SD driver also will run the SD clock at 50MHz if the card supports it.

EmbedWise commented 2 years ago

@danieldegrasse Sorry for my late reply. This is the outcome before west update

zephyr git:(sdmmc_rfc) git rev-parse HEAD
7621a728a909712f52d04005cc430bd1f326d647
➜  zephyr git:(sdmmc_rfc) git status        
On branch sdmmc_rfc
Your branch is up to date with 'NXP/sdmmc_rfc'.

nothing to commit, working tree clean
➜  zephyr git:(sdmmc_rfc) 

Running west update inside the zephyr directory

west update
=== updating zephyr (zephyr):
--- zephyr: fetching, need revision main
From github.com:EmbedWise/zephyr
 * branch                  main       -> FETCH_HEAD
HEAD is now at feffe639a3 soc: xtensa: adsp: Fix i.MX8 hw_cycles_per_sec definition
WARNING: left behind zephyr branch "sdmmc_rfc"; to switch back to it (fast forward):
  git -C . checkout sdmmc_rfc
=== updating nrfxlib (nrfxlib):
HEAD is now at dae7bfc bsdlib: version 0.7.5
=== updating mbedtls-nrf (mbedtls):
HEAD is now at 2a1d9332d Merge pull request #682 from ARMmbed/prepare-rc-2.16.6-updated
=== updating canopennode (modules/lib/canopennode):
HEAD is now at 53d3415 Merge pull request #5 from teslabs/module-name
=== updating civetweb (modules/lib/civetweb):
HEAD is now at 094aeb41 cmake: relax minimum version requirements
=== updating cmsis (modules/hal/cmsis):
HEAD is now at b0612c9 DSP: Integrate CMSIS-DSP 1.9.0 (CMSIS 5.8.0)
=== updating edtt (tools/edtt):
HEAD is now at 1fc2e6f Fix Database Hash value after GATT fix
=== updating fatfs (modules/fs/fatfs):
HEAD is now at 9237454 fat_fs: Add support for the "win" variable alignment in FATFS struct
=== updating hal_nordic (modules/hal/nordic):
HEAD is now at 1f9145e drivers: nrf_802154: Update the IEEE 802.15.4 component
=== updating hal_nxp (modules/hal/nxp):
HEAD is now at 9629f15 hal_cmake: Include system.c for MXRT6xx and MXRT5xx
=== updating hal_st (modules/hal/st):
HEAD is now at 575de9d sensor/stmemsc: Align stmemsc i/f to v2.00
=== updating libmetal (modules/hal/libmetal):
HEAD is now at f237c9d lib: update libmetal to release v2021.10.0
=== updating littlefs (modules/fs/littlefs):
HEAD is now at 7b2cf4b zephyr: move CMake glue code for building as a module into Zephyr
=== updating loramac-node (modules/lib/loramac-node):
HEAD is now at 12019623 zephyr: radio: sx127x: override buffer read/write functions
=== updating lvgl (modules/lib/gui/lvgl):
HEAD is now at aae1c111 zephyr: pull in the lvgl glue code from the zephyr tree
=== updating mbedtls (modules/crypto/mbedtls):
HEAD is now at 3e3e58a92 mbedtls: remove static keyword from certain function pointers
=== updating mcuboot (bootloader/mcuboot):
HEAD is now at 89936c3 Merge pull request #72 from nvlsianpu/fix/align_crc_api
=== updating mipi-sys-t (modules/debug/mipi-sys-t):
HEAD is now at d9da086 library: Add support for vprintf
=== updating net-tools (tools/net-tools):
HEAD is now at f49bd13 net-capture: Listen captured traffic and save to pcap file
=== updating nrf_hw_models (modules/bsim_hw_models/nrf_hw_models):
HEAD is now at b8cea37 Added overflow event handling and trigger overflow task
=== updating open-amp (modules/lib/open-amp):
HEAD is now at cfd050f lib: update open-amp lib to release v2021.10.0
=== updating openthread (modules/lib/openthread):
HEAD is now at 0e141a556 zephyr: Simplify zephyr integration of openthread.
=== updating segger (modules/debug/segger):
HEAD is now at 3a52ab2 config: Use zephyr provided options
=== updating tinycbor (modules/lib/tinycbor):
HEAD is now at 40daca9 zephyr: Remove TINYCBOR from interface libraries
=== updating tinycrypt (modules/crypto/tinycrypt):
HEAD is now at 3e9a49d cmake: Fix conditional in root CMakeLists.txt

Conduct the step that was mentioned:

 git -C . checkout sdmmc_rfc
Previous HEAD position was feffe639a3 soc: xtensa: adsp: Fix i.MX8 hw_cycles_per_sec definition
Switched to branch 'sdmmc_rfc'
Your branch is up to date with 'NXP/sdmmc_rfc'.

Then building gives the error

workspace_zephyr_embedwise/zephyr/drivers/sdhc/imx_usdhc.c:21:10: fatal error: fsl_usdhc.h: No such file or directory
   21 | #include <fsl_usdhc.h>
      |          ^~~~~~~~~~~~~
compilation terminated.

I think also the capacitors you mentioned cannot be the issue since it mentions DNP (do not populate) so most likely they are not physically populated on the PCB.

EmbedWise commented 2 years ago

After checking more thoroughly I noticed not only the branch changes, with west update from zephyr sdmmc_rfc to zephyr feffe639a3, which I can put back by git -C . checkout sdmmc_rfc but also the nxp_hal stays behind.

After conducting: git -C . checkout sdmmc_rfc and manually copying the latest hal_nxp files to modules #da6c141

The test succeeded to list the files and write new files.

I will now make an endurance test to be sure this is working properly.

At least those fixes you made help with getting consistent outcomes after power off and power on. Also writing is possible.

danieldegrasse commented 2 years ago

@EmbedWise based on your logs I suspected something like that. At least in my development environment, west update does not change the branch of the zephyr repository itself, and will only checkout the submodules. You could check out the PR manually, and then change directories to the NXP HAL, and checkout the master branch there (after you run git fetch in that directory, this should be possible). That should ensure you have the required versions present on each branch

I'm glad to hear the test is working, and sorry for the confusion on the capacitors. It does look like the teensy4 should have similar routing for the SD card slot to the RT1060 EVK.

EmbedWise commented 2 years ago

@danieldegrasse I am trying to port your changes made for zephyr 3.0 back into 2.70 (LTS2). But get back:

[385/392] Linking C executable zephyr/zephyr_prebuilt.elf
/Applications/ARM/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.bfd: warning: orphan section `NonCacheable' from `modules/hal_nxp/hal_nxp/lib..__modules__hal__nxp.a(fsl_usdhc.c.obj)' being placed in section `NonCacheable'

[392/392] Linking C executable zephyr/zephyr.elf
/Applications/ARM/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.bfd: warning: orphan section `NonCacheable' from `modules/hal_nxp/hal_nxp/lib..__modules__hal__nxp.a(fsl_usdhc.c.obj)' being placed in section `NonCacheable'

It generates a hex file, not sure yet if it works but would like to ask if something changed in memory allocation for the hal which requires certain settings at the zephyr 2.7 side.

I saw the changes on power delay etc and requirements of DT parent etc.

Maybe it is a to broad question, but maybe it rings a bell.

Thanks once again for all the effort you are putting in this.

danieldegrasse commented 2 years ago

That issue is pretty easy to fix. The noncacheable section there is used by the USDHC HAL for booting from an SD card, but is not directly required by the SD subsystem (zephyr has its own internal noncacheable memory architecture). I just placed that section in normal SRAM, since it is not used by zephyr. See here for the linker file I added to do that

EmbedWise commented 2 years ago

That fixed it indeed together with the nocache.ld file. @danieldegrasse Thank you so much for the fast response!

danieldegrasse commented 2 years ago

@EmbedWise given that the new SD stack seems to fix your issue (and it sounds like you are going to backport it to 2.7) I'm going to go ahead to close this issue. Please reopen it if you encounter issues with the stack, or feel that using the new SD stack isn't a satisfactory solution for you (and I'd love feedback if you encounter bugs with the new stack, it is still in a beta state)