Implementing FATFS and USB MSC SD card examples together to store/access data to/from the file

Dear ,

Thanks a lot for your time.

We currently have a project using nRF52840 dongle and nRF connect SDK, implementing FATFS and USB MSC SD card examples together.

The nRF52840 by itself is powered by the battery source. When turned on, it will acquire data from peripherals and store it in the file on the SD card that is created by the program. When a data acquisition session is finished and it is connected to a PC via USB, the data file can be accessed by PC or Mac just like an SD card reader.

I am not sure if I get it correctly, but to be able to achieve the function described above, do I need to introduce a certain mechanism to check whether it's been connected to a USB port and then call the function usb_enable(NULL) so that it can then appear as a disk drive in PC or Mac? 

Do I always call usb_enable(NULL) like a process of initialisation? or do I only call this function when it's been connected to a USB port.

Thanks again. Kind regards, Kevin

Parents
  • Hi,

    It should be sufficient to call usb_enable() in the initialization. The USB library will handle attachment and such for you. It may not be easy to detect when using the dongle, but if you were to build the USB  Mass Storage sample for the nRF52840 DK, you can plug and replug the nRF USB port to a computer multiple times and observe that the mass storage drive will enumerate on the computer, even though you have only a single call to usb_enable() in the code. If you do not call this function, nothing will happen when you connect the USB.

    For your application to know when you are connected to a PC or not, you can pass a callback to usb_enable(), to get events when it connects/disconnects, in case you need to know when you should write to the MSC/SD card or not.

    For instance like this:

    static void app_usb_status_cb(enum usb_dc_status_code status,
    							  const uint8_t *param)
    {
    	ARG_UNUSED(param);
    
    	/* Check the USB status and do needed action if required */
    	switch (status) {
    	case USB_DC_ERROR:
    		LOG_INF("USB device error");
    		break;
    	case USB_DC_RESET:
    		LOG_INF("USB device reset detected");
    		break;
    	case USB_DC_CONNECTED:
    		LOG_INF("USB device connected");
    		break;
    	case USB_DC_CONFIGURED:
    		LOG_INF("USB device configured");
    		break;
    	case USB_DC_DISCONNECTED:
    		LOG_INF("USB device disconnected");
    		break;
    	case USB_DC_SUSPEND:
    		LOG_INF("USB device supended");
    		break;
    	case USB_DC_RESUME:
    		LOG_INF("USB device resumed");
    		break;
    	case USB_DC_INTERFACE:
    		LOG_INF("USB interface selected");
    		break;
    	case USB_DC_SOF:
    		break;
    	case USB_DC_UNKNOWN:
    	default:
    		LOG_INF("USB unknown state");
    		break;
    	}
    }
    
    void main(void)
    {
    	int ret;
    
    	setup_disk();
    
    	ret = usb_enable(&app_usb_status_cb);
    	if (ret != 0) {
    		LOG_ERR("Failed to enable USB");
    		return;
    	}
    
    	LOG_INF("The device is put in USB mass storage mode.\n");
    }

    Best regards,
    Jørgen

  • Hi Jørgen,

    In addition to my last comments, are there any particular points or steps I need to follow to be able to create and write to a file that is created and stored in the SD card?

    Currently, I am using the Zephyr USB MSC mass example as a template. By adding the below declarations and function calls, I would like to see a file is created on the SPI wired SD card by the program.

    ...
    
    #define FILE_NAME ABCD.TXT
    
    struct fs_file_t file;
    
    ...
    
    static void setup_disk(void)
    {
    
    ...
    
    fs_t_file_t_init(&file);
    
    ... (after sucessfully called mount_app_fs(mp))
    
    rc = fs_open(&file,FILE_NAME,FS_O_CREATE | FS_O_RDWR | FS_O_APPEND);
    
    rc = fs_close(&file);
    
    ...
    
    }

    But the file is not created and appears in the drive when the dongle is connected to the PC via USB. While I am still trying to examine all the rc values, could you please kindly help check are there any steps I missed?

    Thanks a lot for your kind assistance.

     

    Kind regards, Kevin

Reply
  • Hi Jørgen,

    In addition to my last comments, are there any particular points or steps I need to follow to be able to create and write to a file that is created and stored in the SD card?

    Currently, I am using the Zephyr USB MSC mass example as a template. By adding the below declarations and function calls, I would like to see a file is created on the SPI wired SD card by the program.

    ...
    
    #define FILE_NAME ABCD.TXT
    
    struct fs_file_t file;
    
    ...
    
    static void setup_disk(void)
    {
    
    ...
    
    fs_t_file_t_init(&file);
    
    ... (after sucessfully called mount_app_fs(mp))
    
    rc = fs_open(&file,FILE_NAME,FS_O_CREATE | FS_O_RDWR | FS_O_APPEND);
    
    rc = fs_close(&file);
    
    ...
    
    }

    But the file is not created and appears in the drive when the dongle is connected to the PC via USB. While I am still trying to examine all the rc values, could you please kindly help check are there any steps I missed?

    Thanks a lot for your kind assistance.

     

    Kind regards, Kevin

Children
No Data
Related