Manually control USB MSC initialization timing

In my project (SDK 2.9.1), I have configured a USB Mass Storage (MSC) device that uses a QSPI flash, like the one on the 52840DK board.

CONFIG_USB_MASS_STORAGE=y
CONFIG_MASS_STORAGE_DISK_NAME="NAND"

My system goes into sleep mode when a button is pressed, but before that it powers down some peripherals to reduce current consumption. One of these peripherals is the flash memory itself. However, when the system wakes up, the flash is not yet active, so the MSC initialization fails. This is because the MSC is initialized before my application has a chance to start and activate the flash. I was wondering if it's possible to delay the MSC initialization and decide when to trigger it myself. Is that possible?

Parents
  • Hello,

    Does using CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n help?

    With this configuration, you can call the USB initialization functions (such as usb_enable()) at the appropriate time in your application, after ensuring the flash is active and ready for use by the MSC class.

    Kenneth

  • No, I’ve already enabled that configuration and I’m calling usb_enable() only at the end of my initialization.

    ################################################
    ### SYSTEM CONFIGURATION #######################
    ################################################
    CONFIG_LOG=y
    CONFIG_LOG_BUFFER_SIZE=2048
    CONFIG_LOG_MAX_LEVEL=2
    CONFIG_USE_SEGGER_RTT=y
    CONFIG_RTT_CONSOLE=y
    CONFIG_RING_BUFFER=y
    CONFIG_HEAP_MEM_POOL_SIZE=8192
    CONFIG_REBOOT=y
    CONFIG_KERNEL_BIN_NAME="XXXXX"
    # CONFIG_DISABLE_FLASH_PATCH=y
    CONFIG_POWEROFF=y
    CONFIG_PM_DEVICE=y
    CONFIG_THREAD_NAME=y
    # CONFIG_THREAD_ANALYZER=y
    # CONFIG_THREAD_ANALYZER_AUTO=y
    
    # printf floats support
    CONFIG_CBPRINTF_LIBC_SUBSTS=y
    CONFIG_CBPRINTF_FP_SUPPORT=y
    
    # time library support
    CONFIG_NEWLIB_LIBC=y
    CONFIG_POSIX_API=y
    
    ################################################
    ### SOC'S PERIPHERALS CONFIGURATION ############
    ################################################
    CONFIG_GPIO=y
    CONFIG_PWM=y
    CONFIG_LED=y
    CONFIG_I2C=y
    CONFIG_SPI=y
    CONFIG_USB_DEVICE_STACK=y
    CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
    CONFIG_NRFX_SPIM2=y
    # CONFIG_NRFX_TIMER1=y
    CONFIG_NRFX_GPIOTE0=y
    CONFIG_NRFX_GPIOTE1=y
    CONFIG_NRFX_PPI=y
    CONFIG_NRFX_QSPI=y                                  
    CONFIG_NORDIC_QSPI_NOR=y                            
    CONFIG_NORDIC_QSPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096  
    CONFIG_USB_DEVICE_MANUFACTURER="XXXXX"
    CONFIG_USB_DEVICE_PRODUCT="XXXXX"
    CONFIG_USB_DEVICE_SN="XXXXX"
    CONFIG_USB_DEVICE_PID=0x0002
    CONFIG_USB_DEVICE_VID=0xAD5A
    
    ################################################
    ### NVS MEMORY CONFIGURATION ###################
    ################################################
    CONFIG_NVS=y
    
    ################################################
    ### FLASH MEMORY SUPPORT CONFIGURATION #########
    ################################################
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_MPU_ALLOW_FLASH_WRITE=y
    CONFIG_FLASH_MAP=y
    
    ################################################
    ### BLE CONFIGURATION ##########################
    ################################################
    CONFIG_BT=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_DEVICE_NAME="XXXXX"
    # CONFIG_BT_SMP=y
    CONFIG_BT_DEVICE_APPEARANCE=1345
    CONFIG_BT_MAX_CONN=1
    CONFIG_BT_LL_SOFTDEVICE=y
    CONFIG_BT_BUF_ACL_RX_SIZE=502
    CONFIG_BT_ATT_PREPARE_COUNT=2
    # CONFIG_BT_L2CAP_TX_BUF_COUNT=10
    CONFIG_BT_L2CAP_TX_MTU=498
    # CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
    CONFIG_BT_CONN_TX_MAX=10
    CONFIG_BT_BUF_ACL_TX_COUNT=10
    CONFIG_BT_BUF_ACL_TX_SIZE=502
    CONFIG_BT_CTLR_DATA_LENGTH_MAX=251
    CONFIG_BT_CTLR_PHY_2M=y
    CONFIG_BT_CTLR_RX_BUFFERS=2
    CONFIG_BT_CTLR_SDC_MAX_CONN_EVENT_LEN_DEFAULT=4000000
    CONFIG_BT_CTLR_ADVANCED_FEATURES=y
    CONFIG_BT_CTLR_CONN_RSSI=y
    
    CONFIG_BT_USER_DATA_LEN_UPDATE=y
    CONFIG_BT_USER_PHY_UPDATE=y
    CONFIG_BT_PERIPHERAL_PREF_MIN_INT=6
    CONFIG_BT_PERIPHERAL_PREF_MAX_INT=80
    CONFIG_BT_PERIPHERAL_PREF_LATENCY=0
    CONFIG_BT_PERIPHERAL_PREF_TIMEOUT=400
    CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n
    
    CONFIG_BT_ATT_TX_COUNT=10
    CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y
    CONFIG_BT_BUF_EVT_RX_COUNT=11
    
    ################################################
    ### MASS STORAGE CONFIGURATION #################
    ################################################
    CONFIG_USB_MASS_STORAGE=y
    CONFIG_MASS_STORAGE_DISK_NAME="NAND"
    
    ################################################
    ### FILE SYSTEMFS CONFIGURATION ################
    ################################################
    CONFIG_DISK_DRIVER_FLASH=y
    CONFIG_FILE_SYSTEM=y
    CONFIG_FAT_FILESYSTEM_ELM=y
    CONFIG_FS_FATFS_LFN=y
    
    ################################################
    ### DFU CONFIGURATION ##########################
    ################################################
    CONFIG_NET_BUF=y
    CONFIG_ZCBOR=y
    CONFIG_CRC=y
    CONFIG_MCUMGR=y
    CONFIG_STREAM_FLASH=y
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
    CONFIG_MAIN_STACK_SIZE=4096
    CONFIG_THREAD_MONITOR=y
    CONFIG_MCUMGR_GRP_OS_TASKSTAT=y
    CONFIG_STATS=y
    CONFIG_STATS_NAMES=y
    CONFIG_IMG_MANAGER=y
    CONFIG_MCUMGR_GRP_IMG=y
    CONFIG_MCUMGR_GRP_OS=y
    CONFIG_MCUMGR_GRP_STAT=y
    CONFIG_MCUBOOT_UTIL_LOG_LEVEL_WRN=y
    CONFIG_SERIAL=y
    CONFIG_UART_LINE_CTRL=y
    CONFIG_CONSOLE=y
    CONFIG_MCUMGR_TRANSPORT_UART=y
    CONFIG_BASE64=y

  • But then it seems you already have control over this, and can add appropriate delays if needed? What am I missing here?

    Kenneth

  • Sorry for the late reply. Let me try to better explain my problem. My application runs on custom hardware. The nRF52840 I'm using has a pin that enables power to some peripherals that I want to completely turn off when going to sleep and reactivate when waking up. As a result, this signal that powers the peripherals is activated during the initialization of my main thread, which is called App_Thread. Until that moment, the peripherals I turned off remain powered down. Among these peripherals is also the Flash memory I’m using for MSC.

    The issue is that, during startup, it seems the MSC is being initialized before the App_Thread starts and thus before the Flash memory is powered. In fact, during boot, the nRF52840 outputs the following:

    <err> qspi_nor: JEDEC id [ff ff ff] expect [20 bb 20]
    *** Booting My Application v0.0.1-3822453fa6ec ***
    *** Using nRF Connect SDK v2.9.1-60d0d6c8d42d ***
    *** Using Zephyr OS v3.7.99-ca954a6216c9 ***
    <err> flashdisk: Flash area 0 open error -19
    <err> usb_msc: Storage init ERROR !!!! - Aborting USB init
    

    Mybe I'm worng but the first line suggests that before my code runs, MSC initialization is attempted and fails because the Flash is still unpowered.

    So, my question is whether it is possible to control the MSC initialization manually — that is, to initialize it only when I choose to, after powering the Flash.

Reply
  • Sorry for the late reply. Let me try to better explain my problem. My application runs on custom hardware. The nRF52840 I'm using has a pin that enables power to some peripherals that I want to completely turn off when going to sleep and reactivate when waking up. As a result, this signal that powers the peripherals is activated during the initialization of my main thread, which is called App_Thread. Until that moment, the peripherals I turned off remain powered down. Among these peripherals is also the Flash memory I’m using for MSC.

    The issue is that, during startup, it seems the MSC is being initialized before the App_Thread starts and thus before the Flash memory is powered. In fact, during boot, the nRF52840 outputs the following:

    <err> qspi_nor: JEDEC id [ff ff ff] expect [20 bb 20]
    *** Booting My Application v0.0.1-3822453fa6ec ***
    *** Using nRF Connect SDK v2.9.1-60d0d6c8d42d ***
    *** Using Zephyr OS v3.7.99-ca954a6216c9 ***
    <err> flashdisk: Flash area 0 open error -19
    <err> usb_msc: Storage init ERROR !!!! - Aborting USB init
    

    Mybe I'm worng but the first line suggests that before my code runs, MSC initialization is attempted and fails because the Flash is still unpowered.

    So, my question is whether it is possible to control the MSC initialization manually — that is, to initialize it only when I choose to, after powering the Flash.

Children
No Data
Related