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,

    Thanks a lot for your detailed explanation and it's very helpful.

    But in the meantime, I would also like to have one more thing to be checked.

    After I call usb_enable(), will the program automatically disable its capability of reading and writing from/to my SD card? OR it's just similar as spi_enable(), for example, which will not be activated and functioning until it's actually been using, i.e. connecting to a PC via USB?

    Thanks again.

    Kind regards, Kevin

Reply
  • Hi Jørgen,

    Thanks a lot for your detailed explanation and it's very helpful.

    But in the meantime, I would also like to have one more thing to be checked.

    After I call usb_enable(), will the program automatically disable its capability of reading and writing from/to my SD card? OR it's just similar as spi_enable(), for example, which will not be activated and functioning until it's actually been using, i.e. connecting to a PC via USB?

    Thanks again.

    Kind regards, Kevin

Children
No Data
Related