nrf53 USB as mass storage with SDCARD enable ok but doesn't work


I am here, again! I am trying to use the USB of nfr5340dk or my custom board as mass storage with SDCard.

I am sure the SDCard works... in the same project I can read and write files without problems, but when I enable the usb, the USB_enable() function returns that there is not a problem, but from the PC I can't see the device, and no enumeration too.

The first question is: How does USB bind with SDCard? I saw that for internal flash, the overlay file contains these rows that I don't have:

/delete-node/ &storage_partition;

&mx25r64 {
	partitions {
		compatible = "fixed-partitions";
		#address-cells = <1>;
		#size-cells = <1>;

		storage_partition: partition@0 {
			label = "storage";
			reg = <0x00000000 0x00020000>;

/ {
	msc_disk0 {
		compatible = "zephyr,flash-disk";
		partition = <&storage_partition>;
		disk-name = "NAND";
		cache-size = <4096>;

second question: The application must exit from the main to works like in this other post?



  • A small update.

    After some tries with both dk and custom board, with DK now the PC start the enumeration but in a terminal I can read a fault message and the board reset.

    *** Booting Zephyr OS build v3.1.99-ncs1-1
    Mount /SD:: 0
    /SD:: bsize = 512 ; frsize = 32768 ; blocks = 238304 ; bfree = 236871
    /SD: opendir: 0
      D 0 EXAMPLE
    End of files
    [00:00:02.259,338] <inf> sd: Detected SD card
    [00:00:02.278,381] <inf> sd: Maximum SD clock is under 25MHz, using clock of 2000000Hz
    [00:00:02.364,013] <inf> main: The device is put in USB mass storage mode.
    [00:00:03.770,263] <err> os: ***** USAGE FAULT *****
    [00:00:03.770,294] <err> os:   Stack overflow (context area not valid)
    [00:00:03.770,294] <err> os: r0/a1:  0xfbd35e65  r1/a2:  0x247882c0  r2/a3:  0x0000a2d0
    [00:00:03.770,294] <err> os: r3/a4:  0x61000000 r12/ip:  0x20000224 r14/lr:  0x00000009
    [00:00:03.770,324] <err> os:  xpsr:  0x00000000
    [00:00:03.770,324] <err> os: Faulting instruction address (r15/pc): 0x20000224
    [00:00:03.770,355] <err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
    [00:00:03.770,355] <err> os: Current thread: 0x200009b0 (unknown)
    [00:00:03.828,186] <err> fatal_error: Resetting system

    It is the mass storage example and I don't touch anything! I just added the SPI configuration for the sd card.



  • Hi Marco

    Could you try to build the sample again with the following debugging options set?


    Then you should get a bit more info about where the issue happens. 

    Possibly you just need to increase the size of the main stack or the idle stack, which can be configured through the CONFIG_MAIN_STACK_SIZE or CONFIG_IDLE_STACK_SIZE parameters. 

    If you test it again with the debug options set can you share the log with me, and also the file from your build directory?

    The map file will show where the various stacks are located in memory, making it easier to identify the cause of the issue. 

    Best regards

  • Hi Torbjørn,

    I added the two options that you suggest me, but the log is the same.

    You can find the .map file in attach!



  • Hi Marco

    Most likely the issue is that your main stack is too small. 

    Could you try to increase the size of your main stack and see if the program will run without crashing? 

    For instance, try to set the stack size to 4096 or 8192 and see when it works (or not). 


    Best regards

Reply Children
  • Hi Torbjørn

    I have increased the main stack size and idle stack size to the following value:


    But the error is the same! nothing change!



  • Hi Marco 

    Does the error happen only if the SD card is connected, or will it happen also without an SD card in place?

    If it happens without the SD card present would you be able to share the project with me so I can try to reproduce the issue here?

    Best regards

  • Hi Torbjørn,

    whitout the SD card present, there isn't the fault. The log is:

    *** Booting Zephyr OS build v3.1.99-ncs1-1  ***
    [00:00:02.481,658] [1B][1;31m<err> sd: Card error on CMD0[1B][0m
    [00:00:02.481,689] [1B][1;31m<err> usb_msc: Storage init ERROR !!!! - Aborting USB init[1B][0m
    [00:00:04.485,412] [1B][1;31m<err> sd: Card error on CMD0[1B][0m
    [00:00:04.485,443] [1B][1;31m<err> fs: fs mount error (-5)[1B][0m
    [00:00:04.485,473] [1B][1;31m<err> main: Failed to mount filesystem[1B][0m
    [00:00:04.485,595] [1B][0m<inf> main: The device is put in USB mass storage mode.

    Anyway, in attach you can find the project.


  • Hi Marco

    I found a similar sounding case where the issue was the size of the mass storage stack size rather than the main or idle stacks. 

    Could you try one more time, but change CONFIG_MASS_STORAGE_STACK_SIZE instead? 

    Try 4096 or 8129 and see if it fixes the issue. 

    If you still can't make it work I will hook up an SD card to a DK so I can do some testing on my side. 

    Best regards

  • Hi Torbjørn,

    I added into the prj.conf file the following line 


    But the compiler reply in this way:

    d:/Workspace/vsc_ncs_2.1.2/usb_mass_test/prj.conf:29: warning: attempt to assign the value ' 8192' to the undefined symbol MASS_STORAGE_STACK_SIZE 
    Parsing D:/Workspace/vsc_ncs_2.1.2/usb_mass_test/Kconfig
    Loaded configuration 'D:/SDK/ncs/v2.1.2/zephyr/boards/arm/nrf5340dk_nrf5340/nrf5340dk_nrf5340_cpuapp_defconfig'
    Merged configuration 'd:/Workspace/vsc_ncs_2.1.2/usb_mass_test/prj.conf'
    error: Aborting due to Kconfig warnings
    CMake Error at D:/SDK/ncs/v2.1.2/zephyr/cmake/modules/kconfig.cmake:293 (message):
      command failed with return code: 1
    Call Stack (most recent call first):
      D:/SDK/ncs/v2.1.2/zephyr/cmake/modules/zephyr_default.cmake:121 (include)
      D:/SDK/ncs/v2.1.2/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:66 (include)
      D:/SDK/ncs/v2.1.2/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:92 (include_boilerplate)
      CMakeLists.txt:4 (find_package)
    -- Configuring incomplete, errors occurred!
    FATAL ERROR: command exited with status 1: 'd:\SDK\ncs\toolchains\v2.1.2\opt\bin\cmake.EXE' '-DWEST_PYTHON=d:\SDK\ncs\toolchains\v2.1.2\opt\bin\python.exe' '-Bd:\Workspace\vsc_ncs_2.1.2\usb_mass_test\build' -GNinja -DBOARD=nrf5340dk_nrf5340_cpuapp -DNCS_TOOLCHAIN_VERSION:STRING=NONE '-DBOARD_ROOT:STRING=d:/Workspace/vsc_ncs_2.1.2/prova_adc;d:/Workspace/vsc_ncs_2.1.2/usb_mass_test' -DDTC_OVERLAY_FILE:STRING=d:/Workspace/vsc_ncs_2.1.2/usb_mass_test/boards/nrf5340dk_nrf5340_cpuapp.overlay -DCONF_FILE:STRING=d:/Workspace/vsc_ncs_2.1.2/usb_mass_test/prj.conf '-Sd:\Workspace\vsc_ncs_2.1.2\usb_mass_test'


    To avoid this, I changed the value into Kconfig.msc... now the project compile, the stack overflow is disappered, but the PC doesn't see the USB and the SDCard. 

    I tried to go back to the previous value of stack size, but now the stack overflow is no more present! I'm going crazy!