General Category > Discussion - Software

VNC2: Handling USB Device connect/disconnect as a host

(1/2) > >>

KTrenholm:
Hello!

I'm looking to devise a way to handle connection and disconnection of a USB Flash Drive using the UART2DSC project as a base.  I wanted to ask a couple questions regarding what would be the "clean" way to handle such a thing?

My thought is to use a timer and essentially poll the USB host connection status every 250ms or so and act upon that.  The device configuration and timer thread looks like this:


--- Code: ---vos_tcb_t *tcbPOLL_USB;
VOS_HANDLE hTIMER; //USB Poll Timer

void POLL_USB();

void main(void){
    // USB Host configuration context
    usbhost_context_t usbhostContext;
    //TIMER Configuration context
    tmr_context_t timerContext;

    // Initialise FAT File System Driver
    fatdrv_init(VOS_DEV_FAT_FILE_SYSTEM);

    // Initialise BOMS Device Driver
    boms_init(VOS_DEV_BOMS);

    // Initialise USB Host
    usbhostContext.if_count = 8;
    usbhostContext.ep_count = 16;
    usbhostContext.xfer_count = 2;
    usbhostContext.iso_xfer_count = 2;
    usbhost_init(VOS_DEV_USBHOST_1, -1, &usbhostContext);

    //Initialise Timer0 - USB Poll Device Status - Connect/Disconnect
    timerContext.timer_identifier = TIMER_0;
    tmr_init(TIMER_0, &timerContext);
    tcbPOLL_USB = vos_create_thread_ex(24, 256, POLL_USB, "PollUSB", 0);

    /*Snipped - Other Device Config*/

    vos_start_scheduler();
}

//POLL_USB TIMER THREAD
void POLL_USB(){
    tmr_ioctl_cb_t tmr_iocb;
    uint8_t usb_connstate;
    static uint8_t usb_connstate_prev = PORT_STATE_DISCONNECTED;

    hTIMER = vos_dev_open(TIMER0);

    tmr_iocb.ioctl_code = VOS_IOCTL_TIMER_SET_TICK_SIZE;
    tmr_iocb.param = TIMER_TICK_MS;
    vos_dev_ioctl(hTIMER,&tmr_iocb);

    tmr_iocb.ioctl_code = VOS_IOCTL_TIMER_SET_COUNT;
    tmr_iocb.param = 250;
    vos_dev_ioctl(hTIMER,&tmr_iocb);

    vos_iocb.ioctl_code = VOS_IOCTL_TIMER_SET_MODE;
    tmr_iocb.param = TIMER_MODE_CONTINUOUS;
    vos_dev_ioctl(hTIMER,&tmr_iocb);

    tmr_iocb.ioctl_code = VOS_IOCTL_TIMER_START;
    vos_dev_ioctl(hTIMER,&tmr_iocb);

    while (1){
        tmr_iocb.ioctl_code = VOS_IOCTL_TIMER_WAIT_ON_COMPLETE;
vos_dev_ioctl(hTIMER, &tmr_iocb);

usb_connstate = usbhost_connect_state(hUSBHOST_1);

if (usb_connstate == PORT_STATE_DISCONNECTED){

    if (usb_connstate_prev != PORT_STATE_DISCONNECTED){
                //Connected -> Disconnected --Detach BOMS and FAT File System
        boms_detach(hBOMS);
fat_detach(hFAT_FILE_SYSTEM);

    }

}

if (usb_connstat == PORT_STATE_ENUMERATED){

    if (usb_connstate_prev != PORT_STATE_ENUMERATED){
                //Un-enumerated -> Enumerated --Attach BOMS and FAT File system
hBOMS = boms_attach(hUSBHOST_1, VOS_DEV_BOMS);
hFAT_FILE_SYSTEM = fat_attach(hBOMS, VOS_DEV_FAT_FILE_SYSTEM);

//attach the stdio file system to the FAT file system
fsAttach(hFAT_FILE_SYSTEM);
     }

}

        usb_connstate_prev = usb_connstate;
    }

}

--- End code ---

Would it all be just as simple at this above?  Do I have to do anything extra to cleanly detach the BOMS and FAT when the device has been removed?  Would I need to re-initialize them via their respective init_ functions or is detach/attach functions adequate after the initial init_ calls at the start of main()?  Is the call to fsAttach necessary after a disconnect/reconnect? or should I only call that once on initialization?

FTDI Community:
Hello,

please refer to the following documents:

https://ftdichip.com/wp-content/uploads/2020/08/AN_163_Vinculum-II_USB_Slave_Detecting_Disconnect.pdf

this application note describes how to detect when a USB slave device has been disconnected from the VNC2.

https://ftdichip.com/wp-content/uploads/2020/08/AN_172_Vinculum_II_Using_the_USB_Slave_Driver.pdf

this application note describes the VNC2 slave driver interface, which controls the USB slave ports on the device.

these should help you with your application.

please let us know if you have anymore questions.

Best regards,

FTDI community

KTrenholm:
Hi FTDI Community,

I think there's a misunderstanding what I'm trying to do?  Those AppNotes are for using the VNC2 as a USB Device(Slave) while I am looking to use the VNC2 as a USB Host.  I'm looking to detect and handle the connection/disconnection of a USB Flash Drive to the VNC2 USB Host. 
The AN and sample firmware I am using as a base is:
https://ftdichip.com/wp-content/uploads/2020/08/AN_187_Vinculum-II_UART_to_USB_Memory_Bridge.pdf

This firmware however only detects connection of the USB Memory device on VNC2 power-up and will loop looking for a USB device until one is detected and enumerated.  It does not support removal or reconnection of the USB Memory device after it has been removed.  That is the functionality I'm trying to add.  There does not appear to be a specific AppNote for this case.

I'm thinking I can use a timer to periodically check for connect/disconnect, but I am unclear on what exactly must be re-initialized/un-initialized when a USB Memory device is reconnected/disconnected.  Is just Attaching/Detaching the BOMS and FAT enough?  Do BOMS and FAT need to be completely shut down and reinitialized?

FTDI Community:
Hello,

Sorry for the misunderstanding.

what version of the VNC2 toolchain are you using?

version V2.0.2-SP2 fixes a known connect/disconnect issue.

Download Vinculum II Toolchain V2.0.2-SP2

let me know if you are already using this version.

Best regards

FTDI community

KTrenholm:

--- Quote from: FTDI Community on November 03, 2021, 11:42:51 AM ---Hello,

Sorry for the misunderstanding.

what version of the VNC2 toolchain are you using?

version V2.0.2-SP2 fixes a known connect/disconnect issue.

Download Vinculum II Toolchain V2.0.2-SP2

let me know if you are already using this version.

Best regards

FTDI community

--- End quote ---

I am on v2.0.2 SP3 (received from FTDI support last week).

Navigation

[0] Message Index

[#] Next page

Go to full version