FTDI Community

Please login or register.

Login with username, password and session length.
Advanced Search  

News:

Welcome to the FTDI Community!

Please read our Welcome Note

Technical Support enquires
please contact the team
@ FTDI Support


New Bridgetek Community is now open

Please note that we have created the Bridgetek Community to discuss all Bridgetek products e.g. EVE, MCU.

Please follow this link and create a new user account to get started.

Bridgetek Community

Author Topic: VNC2: Handling USB Device connect/disconnect as a host  (Read 11668 times)

KTrenholm

  • Newbie
  • *
  • Posts: 12
    • View Profile
VNC2: Handling USB Device connect/disconnect as a host
« on: November 01, 2021, 07:31:33 PM »

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: [Select]
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;
    }

}

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?
« Last Edit: November 01, 2021, 08:39:10 PM by KTrenholm »
Logged

FTDI Community

  • Administrator
  • Hero Member
  • *****
  • Posts: 891
    • View Profile
Re: VNC2: Handling USB Device connect/disconnect as a host
« Reply #1 on: November 02, 2021, 01:09:24 PM »

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
Logged

KTrenholm

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: VNC2: Handling USB Device connect/disconnect as a host
« Reply #2 on: November 02, 2021, 03:40:34 PM »

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?
« Last Edit: November 02, 2021, 03:48:14 PM by KTrenholm »
Logged

FTDI Community

  • Administrator
  • Hero Member
  • *****
  • Posts: 891
    • View Profile
Re: VNC2: Handling USB Device connect/disconnect as a host
« Reply #3 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
Logged

KTrenholm

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: VNC2: Handling USB Device connect/disconnect as a host
« Reply #4 on: November 03, 2021, 02:46:44 PM »

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

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

FTDI Community

  • Administrator
  • Hero Member
  • *****
  • Posts: 891
    • View Profile
Re: VNC2: Handling USB Device connect/disconnect as a host
« Reply #5 on: November 04, 2021, 10:08:31 AM »

Hello,

Ok that's fine then.

using the BOMS and FAT files to detect connection/removal won't work. As you say the UART2DSC doesn't do anything to detect removal or connection of USB devices. This is because it is a minimum project that is used to demonstrate how to write to a file on a flash disk. the procedure is as follows:

the USB host call to VOS_IOCTL_USBHOST_GET_CONNECT_STATE to see if there is a device connected and enumerated.
Search through the devices for the correct device.
Then open and attach the BOMS driver to the handle to the correct device.
After that attach the FAT driver to the BOMS driver.

Periodically poll the VOS_IOCTL_USBHOST_GET_CONNECT_STATE for disconnection, this checks that a device is connected to the root hub.
Also check the return value of any FAT or BOMS driver calls for FAT_NOT_FOUND or FAT_ERROR/FAT_FATAL_ERROR, MSI_NOT_FOUND or other errors. If there is an error again check VOS_IOCTL_USBHOST_GET_CONNECT_STATE to confirm or if there is a bub involved

On disconnection, detach FAT then BOMS drivers from the device.

I would recommend you use one of our other projects such as V2DAP, which has increased functionality and can be downloaded here: https://ftdichip.com/firmware/precompiled/.

Best Regards

FTDI Community
 
Logged

KTrenholm

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: VNC2: Handling USB Device connect/disconnect as a host
« Reply #6 on: November 05, 2021, 08:04:05 PM »

Hello,

Ok that's fine then.

using the BOMS and FAT files to detect connection/removal won't work. As you say the UART2DSC doesn't do anything to detect removal or connection of USB devices. This is because it is a minimum project that is used to demonstrate how to write to a file on a flash disk. the procedure is as follows:

the USB host call to VOS_IOCTL_USBHOST_GET_CONNECT_STATE to see if there is a device connected and enumerated.
Search through the devices for the correct device.
Then open and attach the BOMS driver to the handle to the correct device.
After that attach the FAT driver to the BOMS driver.

Periodically poll the VOS_IOCTL_USBHOST_GET_CONNECT_STATE for disconnection, this checks that a device is connected to the root hub.
Also check the return value of any FAT or BOMS driver calls for FAT_NOT_FOUND or FAT_ERROR/FAT_FATAL_ERROR, MSI_NOT_FOUND or other errors. If there is an error again check VOS_IOCTL_USBHOST_GET_CONNECT_STATE to confirm or if there is a bub involved

On disconnection, detach FAT then BOMS drivers from the device.

I would recommend you use one of our other projects such as V2DAP, which has increased functionality and can be downloaded here: https://ftdichip.com/firmware/precompiled/.

Best Regards

FTDI Community

Thanks for this!  I've been sidetracked to another project for the moment but will be sure to give this a try when I get the chance ;D.
Logged