cancel
Showing results for 
Search instead for 
Did you mean: 

Which function can detect STM32F413 USB(CDC) connected or disconnected

June Zhou
Associate II
Posted on September 26, 2017 at 17:04

Hello,

I use STM32F413ZG MCU and STM32CubeMx to create the Keil project.

Now, I need USB as communication Device Class(Virtual Port Com) to transmit data to computer(Master). But I do not know which function in USB library can detect USB cable plugged in the computer or unplugged in?  I tried to set up breakpoints in

function  USBD_LL_DevConnected(USBD_HandleTypeDef *pdev)

 and USBD_LL_DevDisconnected(USBD_HandleTypeDef *pdev)

in USB_core.c.

 but program never went to here when I plugging in USB or taking USB out.

Note, the computer can find USB in and get the serial port (COM6) and I can sending and receiving data via USB port (COM6).

Do you know the function or a interrupt can detect USB connection or disconnection?

Thanks a lot.

June

#how-to-detect-stm32-usb-cdc-connection-or-disconnection
8 REPLIES 8
Ben K
Senior III
Posted on September 26, 2017 at 17:30

https://community.st.com/0D50X00009XkdkdSAB

To detect disconnection the simplestway is to check if VBUS voltage is present with a GPIO EXTI channel. For a pure software solution you could also consider the Suspend callback, although it isintended to power down the USB device when there is no bus transaction (StartOfFrame) for > 3ms.

Posted on September 26, 2017 at 17:57

Hello!

Evaluate (hUsbDeviceXS.dev_state == USBD_STATE_CONFIGURED) to know if device is connected.

hUsbDeviceXS is the handle of the USB device.

Regards

vf

June Zhou
Associate II
Posted on September 26, 2017 at 17:58

Hi Ben,

Thank you so much for your reply !

But in our hardware board, doesn't use VBUS line, so I think I cannot use '

connected callbacks' function and check GPIO EXTI. 

As the link you give me, you suggest me '

start sending data when the USB device state has changed to 'configured'', could you tell me which function tell me USB device configured when USB cable (type C) plugging in or not?

When our device power on, firmware initialize USB 

void MX_USB_DEVICE_Init(void)

{

/* Init Device Library,Add Supported Class and Start the library*/

   USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);

   USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC);

   USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS);

   USBD_Start(&hUsbDeviceFS);

}

And then waiting for USB plugging in, then send/receive data. When usb pulg in, which function can tell me the USB device state changed to 'Configured'

Note: our application is: our device need to transmit data with computer once it find USB plugged in, and when it find USB taken out, it will stop send data and do other things. And we have Bluetooth and RS232, STM32F413 have to know the connection is USB or Bluetooth or RS232, and it is able to decide which Serial port the data sent to.

So I think I have to find out how to know USB plugged in/out. 

Thanks.

June

Posted on September 26, 2017 at 18:11

Hi vf,

thanks a lot ! could you tell me how to use' 

hUsbDeviceXS.dev_state == USBD_STATE_CONFIGURED' to check our device is connected or not?

I have to use inquiry mode: i.e, every 1s to check if ' 

USBD_STATE_CONFIGURED' 

Is there any way like interrupt, i.e, once USB plugged in, create a interrupt?

It is the first time to write USB codes and USB looks complex, sorry,  maybe ask you guys some basic question.

Thank you and looking forward to your reply

Posted on September 27, 2017 at 11:06

By looking at your code you should have this structure:

USBD_CDC_ItfTypeDef USBD_CDC_fops_FS = {
 CDC_Itf_Init,
 CDC_Itf_DeInit,
 CDC_Itf_Control,
 CDC_Itf_Receive
};�?�?�?�?�?�?�?�?�?�?�?�?

The CDC_Itf_Init() function will be called when the device enters CONFIGURED state, so you can edit it to start the communication there.

As for the disconnection, you can add your code toHAL_PCD_SuspendCallback to stop communication. (This will only be sufficient if the host never suspends the connected device, otherwise also add communication start to

HAL_PCD_ResumeCallback, after checking if the device state is CONFIGURED.)

Posted on September 27, 2017 at 15:03

Hi Ben,

Very useful help ! really appreciate it !

Thanks so much ! 

Posted on September 27, 2017 at 18:53

Hi again

sorry for the late answer.

When wrote evaluate i meant something like this

if(hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED)

{

// device is connected. do something

}

else

{

//disconnected.do someting else.

}

It is thread safe .  You can call it also from inside an Interrupt Service Routine.

Inside

http://www.st.com/content/ccc/resource/technical/document/user_manual/cf/38/e5/b5/dd/1d/4c/09/DM00108129.pdf/files/DM00108129.pdf/jcr:content/translations/en.DM00108129.pdf

you can find all the details about USB Device

Regards

vf

Posted on September 28, 2017 at 18:55

Hi vf,

Sorry to reply later because our office internet breakdown. 

Thank you so much for that in detail. I already tried your codes in interrupt routines, it works.

Thanks again !

June