How to read the file with f_read() function - USB Mass Storage class module

Hello guys,

I am currently playing with the usbd_msc example from SDK15.3.0 that use USB Class storage module.

I was able to drop some files from my PC to 8MB drive made of external QSPI flash memory on nRF52840-DK board. What confuses me now is how can I read those files by using f_read() function!?

FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br);

I see that the first parameter of f_read() function should be a pointer to the file object. How can I get the file object I want to open? Should I do it by opening directory (f_opendir()) and listing it with f_readdir() or should I use a filename of the file I want to open or something else?

I am aware of some examples that first create the file with f_open() function, write to the file with f_write(), close the file with f_close() and after that read the file with f_read(). They use the same file pointer for f_read() function like they used for creating the file (e.g. here). In my case, I know the file exist, I did not create it through the code and I want to open it. How can I do it?

Thanks in advance for your time and effort. It is really appreciated.

Sincerely,

Bojan.

  • By exploring this site I found more about FatFS.

    One way to reading a file would be to open it first with f_open() function by knowing the name of the file in advance.

                FRESULT fr;            
                FIL file; 
                uint16_t size;
                UINT bytesRead;
    
                fr = f_open(&file, "eyes2.bmp", FA_READ);
    
                if (fr == FR_OK){
                    NRF_LOG_INFO("File eyes2.bmp sucessfully opened!");
                    size = f_size(&file);
                    char * data = NULL;
    
                    data = malloc(size); /* allocate memory to store image data */
                    NRF_LOG_INFO("File size: %d bytes", size);
    
                    fr = f_read(&file, data, (UINT) size, &bytesRead);
                    if (fr == FR_OK){
                        NRF_LOG_INFO("File successfully read!");
                        NRF_LOG_INFO("%d bytes read", bytesRead);
                        for (int i=0; i < bytesRead; i++)
                        {
                            NRF_LOG_INFO("data[%d]: 0x%x", i, data[i]);
                        }
                    }
                    free(data); // free allocated memory when you don't need it
    
                    f_close(&file);
                }

    In the code above, I know there is a file called "eyes2.bmp" and I was able to open it and read it. What if I don't know the name of the file (the user can drop the files in folder from PC and the code should be able to read the files without having any idea about the file name)?

    Should I list the directory first with fatfs_ls() to find out how many files are there and after that open the particular file from the list (e.g. 5th file from the list of 10 files available in the folder)?

    Thanks once again.

    Cheers,

    Bojan.