External Flash W25Q64 with USBD_MSC example on NRF52840

Hi All,

        I have an external flash W25Q64 on which I have to mount a file system. I am using USBD_MSC example of NRF52840. My main goal is to create a file inside the flash memory chip. And when USB is plugged into the device it should be seen as a drive on laptop/desktop and I should be able to access the file which was created in the flash chip.I have made the connections on the NRF52840 using the pin configurations below:-


#define SDC_SCK_PIN     (25) 
#define SDC_MOSI_PIN   (23) 
#define SDC_MISO_PIN   (24) 
#define SDC_CS_PIN       (15) 

I have also configured the details in nrf_serial_flash_params.c file. 

static const nrf_serial_flash_params_t m_sflash_params[] = {
    {    
        .read_id = { 0xEF,0x40,0x17 },
        .capabilities = 0x00,
        .size = 8 * 1024 * 1024,
        .erase_size = 2 * 1024,
        .program_size = 256,
    }
};

nrf_serial_flash_params_t const * nrf_serial_flash_params_get(const uint8_t * p_read_id)
{
    size_t i;

    for (i = 0; i < ARRAY_SIZE(m_sflash_params); ++i)
    {
        if (memcmp(m_sflash_params[i].read_id, p_read_id, sizeof(m_sflash_params[i].read_id)) == 0)
        {
            return &m_sflash_params[i];
        }
    }

    return NULL;
}

When I run the example , i get the following error:-

<info> app: Initializing disk 0 (QSPI)...

 <error> app: Disk initialization failed.

1) Will this example run for W25Q64 IC??

2) This example also includes FATfs files. Will it automatically create a file system on the external SPI W25Q64 flash memory??

3) Why am I getting "Disk initialization failed." error?

4) Inside sdk_config.h do I also need to enable pin configurations for Qspi??

5) How to test this application? Currently I am using NRF52840 DK and then connecting external spi flash to it. But for USB detection how will it work?

I have attached few files here to check for the configurations

04162.sdk_config.h

5287.W25Q64BV.PDF

Please help me to solve this issue. I need to solve this issue on an urgent basis. Your help will be appreciated. I have also attached W25Q64 datasheet for your reference.

Thanks & Regards,

Snehal

Parents Reply Children
  • Is there any example available for porting the code?? 

  • Just have to look at how e.g. sd-card or QSPI backend does it, and replicate that using the SPI driver instead.

  • Hi Sigurd,

    I tried to change 

    NRF_BLOCK_DEV_QSPI_DEFINE(
    m_block_dev_qspi,
    NRF_BLOCK_DEV_QSPI_CONFIG(
    512,
    NRF_BLOCK_DEV_QSPI_FLAG_CACHE_WRITEBACK,
    NRF_DRV_QSPI_DEFAULT_CONFIG
    ),
    NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "QSPI", "1.00")
    );

    to 

    NRF_BLOCK_DEV_SPI_DEFINE(
    m_block_dev_spi,
    NRF_BLOCK_DEV_SPI_CONFIG(
    512,
    NRF_BLOCK_DEV_SPI_FLAG_CACHE_WRITEBACK,
    NRF_DRV_SPI_DEFAULT_CONFIG
    ),
    NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "SPI", "1.00")
    );

    but I am getting compiling errors.

    probably because there is no file for SPI under sdk\components\libraries\block_dev. 

    How to solve?

  • Hi,

    sne_333 said:
    probably because there is no file for SPI under sdk\components\libraries\block_dev. 

    block_dev library have support for QSPI and sd-cards. you would need to implement support for SPI yourself. 

  • But on the same SPI lines I am having MAX3421e IC also. It might create a conflict. I am very tensed now.

    I saw the example of sd card, it uses SPI drivers internally. So I modified my file this way. 

    #include <stdint.h>
    #include <stdbool.h>
    #include <stddef.h>
    #include <inttypes.h>
    #include <stdlib.h>
    
    #include "nrf.h"
    #include "nrf_block_dev.h"
    #include "nrf_block_dev_ram.h"
    #include "nrf_block_dev_empty.h"
    #include "nrf_block_dev_qspi.h"
    #include "nrf_block_dev_sdc.h"
    #include "nrf_drv_usbd.h"
    #include "nrf_drv_clock.h"
    #include "nrf_gpio.h"
    #include "nrf_atomic.h"
    #include "nrf_drv_power.h"
    
    #include "ff.h"
    #include "diskio_blkdev.h"
    
    #include "app_usbd.h"
    #include "app_usbd_core.h"
    #include "app_usbd_string_desc.h"
    #include "app_usbd_msc.h"
    #include "app_error.h"
    #include "app_timer.h"
    
    #include "bsp.h"
    
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    
    bool g_test_open = false;
    uint32_t g_idle_s = 0;
    
    BYTE rx_buff[1024];
    uint16_t rx_index;
    
    #define USE_FATFS_SPI    1
    
    static void msc_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                    app_usbd_msc_user_event_t     event);
    
    
    #ifndef USBD_POWER_DETECTION
    #define USBD_POWER_DETECTION true
    #endif
    
    /**
     * @brief SD card enable/disable
     */
    #define USE_SD_CARD       1
    
    
    #define FR_FILE_OPENED 1
    #define FR_FILE_CLOSED 2
    
    const char my_filename[] = "test1.xls";
    
    
    
    
    
    #if USE_SD_CARD
    
    #define SDC_SCK_PIN     (25)        //(27)        ///< SDC serial clock (SCK) pin.
    #define SDC_MOSI_PIN    (23)        //(26)        ///< SDC serial data in (DI) pin.
    #define SDC_MISO_PIN    (24)        //(2)         ///< SDC serial data out (DO) pin.
    #define SDC_CS_PIN      (15)        //(32 + 15)   ///< SDC chip select (CS) pin.
    
    
    /**
     * @brief  SDC block device definition
     */
    NRF_BLOCK_DEV_SDC_DEFINE(
        m_block_dev_sdc,
        NRF_BLOCK_DEV_SDC_CONFIG(
            SDC_SECTOR_SIZE,
            APP_SDCARD_CONFIG(SDC_MOSI_PIN, SDC_MISO_PIN, SDC_SCK_PIN, SDC_CS_PIN)
         ),
         NFR_BLOCK_DEV_INFO_CONFIG("Nordic", "SDC", "1.00")
    );
    
    
    #define BLOCKDEV_LIST() (                                   \
        NRF_BLOCKDEV_BASE_ADDR(m_block_dev_sdc, block_dev)     \
    )
    
    #endif
    
    /**
     * @brief Endpoint list passed to @ref APP_USBD_MSC_GLOBAL_DEF
     */
    #define ENDPOINT_LIST() APP_USBD_MSC_ENDPOINT_LIST(1, 1)
    
    /**
     * @brief Mass storage class work buffer size
     */
    #define MSC_WORKBUFFER_SIZE (1024)
    
    /**
     * @brief Mass storage class instance
     */
    APP_USBD_MSC_GLOBAL_DEF(m_app_msc,
                            0,
                            msc_user_ev_handler,
                            ENDPOINT_LIST(),
                            BLOCKDEV_LIST(),
                            MSC_WORKBUFFER_SIZE);
    
    
    /**
     * @brief Events from keys
     */
    static nrf_atomic_u32_t m_key_events;
    
    /**
     * @brief  USB connection status
     */
    static bool m_usb_connected = false;
    
    
    #if USE_FATFS_SPI
    
    static FATFS m_filesystem;
    
    
    static void fatfs_mkfs(void)
    {
        FRESULT ff_result;
    
        if (m_usb_connected)
        {
            NRF_LOG_ERROR("Unable to operate on filesystem while USB is connected");
            return;
        }
    
        NRF_LOG_INFO("\r\nCreating filesystem...");
        static uint8_t buf[512];
        ff_result = f_mkfs("", FM_FAT, 1024, buf, sizeof(buf));
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("Mkfs failed.");
            return;
        }
    
        NRF_LOG_INFO("Mounting volume...");
        ff_result = f_mount(&m_filesystem, "", 1);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("Mount failed.");
            return;
        }
    
        NRF_LOG_INFO("Done");
    }
    
    
    
    
    static bool fatfs_init(void)
    {
        FRESULT ff_result;
        DSTATUS disk_state = STA_NOINIT;
    
        memset(&m_filesystem, 0, sizeof(FATFS));
    
        // Initialize FATFS disk I/O interface by providing the block device.
        static diskio_blkdev_t drives[] =
        {
            DISKIO_BLOCKDEV_CONFIG(NRF_BLOCKDEV_BASE_ADDR(m_block_dev_sdc, block_dev), NULL)
        };
    
        diskio_blockdev_register(drives, ARRAY_SIZE(drives));
    
        NRF_LOG_INFO("Initializing disk 0 (SPI)...");
        disk_state = disk_initialize(0);
        if (disk_state)
        {
            NRF_LOG_ERROR("Disk initialization failed.disk_state<%d>", disk_state);
            return false;
        }
        NRF_LOG_ERROR("disk_state <%d>", disk_state);
        NRF_LOG_INFO("Mounting volume...");
        ff_result = f_mount(&m_filesystem, "", 1);
        if (ff_result != FR_OK)
        {
            if (ff_result == FR_NO_FILESYSTEM)
            {
                NRF_LOG_ERROR("Mount failed. Filesystem not found. Please format device.");
                fatfs_mkfs();// flash is new, should execute this here once, and then it can be annotated 
            }
            else
            {
                NRF_LOG_ERROR("Mount failed: %u", ff_result);
            }
            return false;
        }
    
        return true;
    }
    
    
    
    static void fatfs_ls(void)
    {
        DIR dir;
        FRESULT ff_result;
        FILINFO fno;
    
        if (m_usb_connected)
        {
            NRF_LOG_ERROR("Unable to operate on filesystem while USB is connected");
            return;
        }
    
        NRF_LOG_INFO("\r\nListing directory: /");
        ff_result = f_opendir(&dir, "/");
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("Directory listing failed: %u", ff_result);
            return;
        }
    
        uint32_t entries_count = 0;
        do
        {
            ff_result = f_readdir(&dir, &fno);
            if (ff_result != FR_OK)
            {
                NRF_LOG_ERROR("Directory read failed: %u", ff_result);
                return;
            }
    
            if (fno.fname[0])
            {
                if (fno.fattrib & AM_DIR)
                {
                    NRF_LOG_RAW_INFO("   <DIR>   %s\r\n",(uint32_t)fno.fname);
                }
                else
                {
                    NRF_LOG_RAW_INFO("%9lu  %s\r\n", fno.fsize, (uint32_t)fno.fname);
                }
            }
    
            ++entries_count;
            NRF_LOG_FLUSH();
        } while (fno.fname[0]);
    
    
        NRF_LOG_RAW_INFO("Entries count: %u\r\n", entries_count);
    }
    
    
    
    
    static void fatfs_file_create(void)
    {
        FRESULT ff_result;
        FIL file;
    
    
        if (m_usb_connected)
        {
            NRF_LOG_ERROR("Unable to operate on filesystem while USB is connected");
            return;
        }
    
        NRF_LOG_RAW_INFO("Creating random file: %s ...", (uint32_t)my_filename);
        NRF_LOG_FLUSH();
    
        ff_result = f_open(&file, my_filename, FA_CREATE_ALWAYS | FA_WRITE);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("\r\nUnable to open or create file: %u", ff_result);
            NRF_LOG_FLUSH();
            return;
        }
    
    //write a string
    
        static BYTE test_string[] = "christophe\t1995\t0707\r\nsid\t1997\t1234\r\n";
        UINT data_len_written = 0;
        ff_result = f_write(&file, test_string, (UINT)strlen(test_string), &data_len_written);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("\r\nUnable to write data: %u", ff_result);
            NRF_LOG_FLUSH();
            return;
        }
    
    
        ff_result = f_close(&file);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("\r\nUnable to close file: %u", ff_result);
            NRF_LOG_FLUSH();
            return;
        }
        NRF_LOG_RAW_INFO("done\r\n");
    
    }
    
    
    static FRESULT my_file_open(FIL* my_file)
    {
        FRESULT ff_result;
        ret_code_t err_code;
    
        if (g_test_open)
        {
            return FR_FILE_OPENED;
        }
    
        ff_result = f_open(my_file, my_filename, FA_OPEN_ALWAYS | FA_WRITE | FA_OPEN_APPEND);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("\r\nUnable to open or create file: %u", ff_result);
            NRF_LOG_FLUSH();
            return ff_result;
        }
    
    #if CLOSE_FILE_BY_TIMER
        err_code = app_timer_start(file_close_timer_id, CLOSE_INTERVAL, NULL);
        APP_ERROR_CHECK(err_code);
    #endif
    
        g_test_open = true;
    
          return ff_result;
    }
    
    #if 0
    static void my_file_write(void)
    {
        FRESULT ff_result;
        FIL file;
    
        ff_result = f_write(&file, test, (UINT)strlen(test), &written);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("\r\nUnable to write data: %u", ff_result);
            NRF_LOG_FLUSH();
            return;
        }
    
    }
    #endif
    
    static FRESULT my_file_close(FIL * my_file)
    {
        FRESULT ff_result;
    
        if (g_test_open == false)
        {
            return FR_FILE_CLOSED;
           
        }
    
        ff_result = f_close(my_file);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("\r\nUnable to close file: %u", ff_result);
            NRF_LOG_FLUSH();
            return ff_result;
        }
        NRF_LOG_RAW_INFO("done\r\n");
    
        g_test_open = false;
    #if CLOSE_FILE_BY_TIMER
        //app_timer_stop(file_close_timer_id);
    #endif
        NRF_LOG_RAW_INFO("file closed!\r\n"); 
        NRF_LOG_FLUSH();
    
         return ff_result;
    
    }
    
    
    static void my_write_data(void)
    {
    
        FRESULT ff_result;
        FIL file;
    
        ff_result = f_open(&file, my_filename, FA_CREATE_ALWAYS | FA_WRITE);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("\r\nUnable to open or create file: %u", ff_result);
            NRF_LOG_FLUSH();
            return;
        }
    
    //write a string
    
    #if 1
    //    ff_result = f_lseek(&file, file.obj->fs.fsize));
    //    if (ff_result != FR_OK)
    //    {
    //        NRF_LOG_ERROR("\r\nUnable to write data: %u", ff_result);
    //        NRF_LOG_FLUSH();
    //        return;
    //    }
    
    
        static BYTE test[] = "1111\t2222\t3333\r\n4444\t5555\t6666\r\n";
        UINT written = 0;
        ff_result = f_write(&file, test, (UINT)strlen(test), &written);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("\r\nUnable to write data: %u", ff_result);
            NRF_LOG_FLUSH();
            return;
        }
    
        ff_result = f_close(&file);
        if (ff_result != FR_OK)
        {
            NRF_LOG_ERROR("\r\nUnable to close file: %u", ff_result);
            NRF_LOG_FLUSH();
            return;
        }
        NRF_LOG_RAW_INFO("done\r\n");
    
    }
    
    static void fatfs_uninit(void)
    {
        NRF_LOG_INFO("Un-initializing disk 0 (SPI)...");
        UNUSED_RETURN_VALUE(disk_uninitialize(0));
    }
    #else //USE_FATFS_SPI
    #define fatfs_init()        false
    #define fatfs_mkfs()        do { } while (0)
    #define fatfs_ls()          do { } while (0)
    #define fatfs_file_create() do { } while (0)
    #define fatfs_uninit()      do { } while (0)
    #endif
    
    /**
     * @brief Class specific event handler.
     *
     * @param p_inst    Class instance.
     * @param event     Class specific event.
     */
    static void msc_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                                    app_usbd_msc_user_event_t     event)
    {
        UNUSED_PARAMETER(p_inst);
        UNUSED_PARAMETER(event);
    }
    
    
    /**
     * @brief USBD library specific event handler.
     *
     * @param event     USBD library event.
     */
    static void usbd_user_ev_handler(app_usbd_event_type_t event)
    {
        switch (event)
        {
            case APP_USBD_EVT_DRV_SUSPEND:
                //bsp_board_led_off(LED_USB_RESUME);
                break;
            case APP_USBD_EVT_DRV_RESUME:
                //bsp_board_led_on(LED_USB_RESUME);
                break;
            case APP_USBD_EVT_STARTED:
                //bsp_board_led_on(LED_USB_START);
                break;
            case APP_USBD_EVT_STOPPED:
                UNUSED_RETURN_VALUE(fatfs_init());
                app_usbd_disable();
                //bsp_board_leds_off();
                break;
            case APP_USBD_EVT_POWER_DETECTED:
                NRF_LOG_INFO("USB power detected");
    
                if (!nrf_drv_usbd_is_enabled())
                {
                    fatfs_uninit();
                    app_usbd_enable();
                }
                break;
            case APP_USBD_EVT_POWER_REMOVED:
                NRF_LOG_INFO("USB power removed");
                app_usbd_stop();
                m_usb_connected = false;
                //memset(&m_rx, 0, sizeof(m_rx));
                break;
            case APP_USBD_EVT_POWER_READY:
                NRF_LOG_INFO("USB ready");
                app_usbd_start();
                m_usb_connected = true;
                break;
            default:
                break;
        }
    }
    
    
    #endif
    
    
    static void close_file_handler(void * p_context)
    {
        UNUSED_PARAMETER(p_context);
        g_idle_s++;
    
    }
    
    
    
    void usbd_msc_task(void)
    {
        FRESULT ff_result;
        FIL file;
    
        bsp_board_init(BSP_INIT_LEDS);
        
        ret_code_t ret;
        static const app_usbd_config_t usbd_config = {
            .ev_state_proc = usbd_user_ev_handler
        };
    
        if (fatfs_init())
            {
                  fatfs_ls();
                  fatfs_file_create();
            }
    
        ret = app_usbd_init(&usbd_config);
        APP_ERROR_CHECK(ret);
    
        app_usbd_class_inst_t const * class_inst_msc = app_usbd_msc_class_inst_get(&m_app_msc);
        ret = app_usbd_class_append(class_inst_msc);
        APP_ERROR_CHECK(ret);
    
        NRF_LOG_INFO("USBD MSC example started.");
        
        if (USBD_POWER_DETECTION)
        {
            ret = app_usbd_power_events_enable();
            APP_ERROR_CHECK(ret);
        }
        else
        {
            NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
    
            app_usbd_enable();
            app_usbd_start();
            m_usb_connected = true;
        }
    
        uint8_t write_cnt = 0;
    
    
       // memset(&m_rx, 0, sizeof(m_rx));
        NRF_LOG_INFO("New Flash to write.");
    
    #if 1 //read JEDEC ID
    
        nrf_qspi_cinstr_conf_t cinstr_cfg = {
            .opcode    = 0x9F,
            .length    = 4,
            .io2_level = true,
            .io3_level = true,
            .wipwait   = true,
            .wren      = true
        };
    
        uint8_t rdid_buf[3] = {0, 0, 0};
        ret = nrf_drv_qspi_cinstr_xfer(&cinstr_cfg, NULL, rdid_buf);
        if (ret != NRF_SUCCESS)
        {
            NRF_LOG_INFO("read eror.");
            //NRF_LOG_INST_ERROR(p_qspi_dev->p_log, "QSPI get 3 byte id error: %"PRIu32"", ret);
            //return ret;
        }
        NRF_LOG_INFO("rdid_buf [%02x %02x %02x]", rdid_buf[0], rdid_buf[1], rdid_buf[2]);
    #endif
    
                fatfs_mkfs();
    
                fatfs_file_create();
                fatfs_file_create();
                fatfs_file_create();
            
                fatfs_ls();
    
        while (true)
        {
    
            while (app_usbd_event_queue_process())
            {
                /* Nothing to do */
            }
           
          NRF_LOG_FLUSH();
        }
    
    }
    

    Will this work?

    Thanks & Regards,

    Snehal.

Related