What is the best way to detect attaching and detaching of an usb cable between RT1020 (device) and a host computer?

cancel
Showing results for 
Search instead for 
Did you mean: 

What is the best way to detect attaching and detaching of an usb cable between RT1020 (device) and a host computer?

391 Views
Senior Contributor I

Hello,

we are using RT1020 USB OTG in device mode. What is the best way to detect attaching and detaching of an usb cable between RT1020 (device) and a host computer?

What I want to do:

1. Attach USB cable to host computer

2. Attach USB cable to RT1020

3. RT1020 detects that an USB cable got connected and fires an interrupt which enables USB peripherals and clocks and starts the USB stack

4. RT1020 communicates with host computer

5. Detach USB cable on either side

6. RT1020 detects that the cable got disconnected and fires an interrupt which stops the USB stack and USB peripherals/clocks

Is it sufficient to just poll the bit VBUS_VALID of USB_ANALOG_USB1_VBUS_DETECT_STAT register? Is there a way to fire an interrupt if this bit changes to 1?

I have found a chapter "USB Plugged-In Detector" in RT1020 reference manual. Would it be better to use this? The chapter describes what it does but not how I can use it in software.

Kind regards,

Stefan

Tags (2)
0 Kudos
6 Replies

128 Views
NXP TechSupport
NXP TechSupport

Hello Stefan,

 

I am sorry for the late reply. I am not sure what SDK project are you referring but I will take dev_cdc_vcom_bm as an example.

 

First you need to enable detach feature by setting the following define in usb_device_config.h file.

/*! @brief Whether the device detached feature is enabled or not. */
#define USB_DEVICE_CONFIG_DETACH_ENABLE (1U)

This will enable notification to the device callback through USB_DeviceAttachNotification and USB_DeviceDetachNotification functions.

#if USB_DEVICE_CONFIG_DETACH_ENABLE
        case kUSB_DeviceNotifyDetach:
            status = USB_DeviceDetachNotification(handle, message);
            break;
        case kUSB_DeviceNotifyAttach:
            status = USB_DeviceAttachNotification(handle, message);
            break;
#endif

Now you can implement what you like to do in USB_DeviceCallback when this events occurred. I just print message in console.

usb_status_t USB_DeviceCallback(usb_device_handle handle, uint32_t event, void *param)
{
 usb_status_t error = kStatus_USB_Error;
 uint16_t *temp16   = (uint16_t *)param;
 uint8_t *temp8     = (uint8_t *)param;

 switch (event)
 {
 case kUSB_DeviceEventAttach:
  usb_echo("USB device attached\r\n");
  break;
 case kUSB_DeviceEventDetach:
  usb_echo("USB device detached\r\n");
  break;

pastedImage_11.png

I hope this helps!

 

Have a great day,

Felipe

-------------------------------------------------------------------------------

Note:

- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored.

Please open a new thread and refer to the closed one, if you have a related question at a later point in time.

------------------------------------------------------------------------------ 

0 Kudos

128 Views
Senior Contributor I

Hello Felipe,

thank you for your answer!

We are not using the NXP USB device stack because of the documentation but with your code I found where to look. I now have some more questions.

  1. In the function USB_DeviceEhciIsrFunction the bit BSV will be tested to know if the device is attached or detached.
    if (ehciState->registerBase->OTGSC & USBHS_OTGSC_BSV_MASK)
            {
                /* Device is connected to a host. */
                message.code = kUSB_DeviceNotifyAttach;
    Could you please explain to me what the B Session and the A Session are and how to change them if needed?
  2. At the moment I'm using the VBUS_VALID flag of USB_ANALOG which is polled every second
    if( USB_ANALOG->INSTANCE[0].VBUS_DETECT_STAT    & 
      USB_ANALOG_VBUS_DETECT_STAT_VBUS_VALID_MASK ) == 
    USB_ANALOG_VBUS_DETECT_STAT_VBUS_VALID_MASK‍‍‍)
    {
      // device attached
    }
    Is this correct too or would it be better to use the B Session Valid Bit instead? What is the difference to the VBUS_VALID bit and do I need to enable anything to be able to use BSV?

Kind regards,

Stefan

0 Kudos

128 Views
Specialist V

Hello Stefan

Maybe you could just use a GPIO to detect the 5V USB bus voltage from the host?

This would then allow you to completely power down the USB interface when not needed, rather than use a small part of it for the detection.

Regards

Mark

[uTasker project developer for Kinetis and i.MX RT]

0 Kudos

128 Views
Senior Contributor I

Hello Mark,

thank you for you suggestion. Unfortunately we do not have any spare GPIOs therefore we need to use the provided mechanism.

At the moment it seems that my method is working reliable but it would still be good to know what the A and B sessions are and how to use them. The reference manual does not really explain them.

Kind regards,

Stefan

0 Kudos

128 Views
Specialist V

Hi Stefan

The A and B sessions refer to the OTG mode - the A device (according to OTG specification) is the end (presently) acting as a host and the B device is the end (presently) acting as a device.

The USB controller defaults to OTG mode and so, as device not supplying power, the B session would be the one that is relevant.

Since the manual doesn't explain the OTG specification itself is is best to reference that to get a better understanding of the terms used.

Regards

Mark

[uTasker project developer for Kinetis and i.MX RT]

128 Views
Senior Contributor I

Thank you very much Mark! That information helps a lot!

0 Kudos