cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure interrupts and register a callback for USB CDC receive?

Anaik.11
Associate

My project involves a STM32F1 interfacing with a Linux machine as a USB CDCACM device, where the Linux machine will send some packets that need to be received/parsed on the MCU. I'd like to configure an interrupt for when the MCU receives the data packets.

I haven't found much material on this, except for UM1734 where I wasn't able to find anything about interrupts for CDC. I also found functions for USB_LP_CAN1_RX0_IRQn interrupt configuration (HAL_NVIC_SetPriority, HAL_NVIC_EnableIRQ) in the HAL user manual which get auto generated by CubeMX, but I'm not sure how to make use of these.

Any code or pointers to relevant reference material will be helpful.

11 REPLIES 11
taraben
Senior

HI Anaik,

here is my lib based on the latest STM32F4 libs.

you have to gnerate the USB CDC via CubeMx and then set on top that lib.

A receiver should check using the usb_cdc_status if there is pending incomming data.

The needed patches to usbd_cdc_if.c are described in the usb_cdc_stm32.c file.

HTH, Adib.

--

Hi Adib,

Thank you very much. Can you explain the setting of cubemx. Timeout.h and watchdog...h are missing

taraben
Senior

Hi Daisky,

basically timeout is based on milliseconds here taken from HAL_TickGet() and

watchdog is based on IIWDG which should trigger every 4sec.

I had to implement this wrapper when I came from AVR platform ...

HTH, Adib.

--

Daisky
Associate II

Thank you a lot. I did the instructions in the "usb_cdc_stm32.c". I called in the main : usb_cdc_init instead of "MX_USB_DEVICE_Init(0)"

Can i send and receive data via UART and terminal?

Best regards

Hello Daisky, yes , in my functions MX_USB_DEVICE_Init() must be replaced by usb_cdc_init().

I append my complete example here.

It runs on Olimex STM32-E407 board that has STM32F407. Which CPU do you have?

Basically CubeMX generated code runs outofthebox. That means if you are able to identify your device using CubeMX then you put my functions on_top.

You must correctly set Frequency, clocktree, USBlibs, etc first and verify.

That gives you buffered communication in USB CDC.

But if something on your setup does not work then my functions can not help either.

Regards, Adib.

Daisky
Associate II

Thank you a lot. i will check it now.

Best regards,

Daisky
Associate II

Dear Adib,

I am trying to send from PC using terminal tools like realTerm via USB. Will this work?

Thanks in advance and sorry for my many questions

Best regards,

taraben
Senior

Yes that should work.

  • windows recognizes new USB device
  • windows loads USB driver for CDC and display in devicemanager as COM port
  • Terminal program connects to that COM Port
  • terminal sends characters
  • application consumes character using usb_cdc_read()

You have to go through all steps and check success.

For me it works like this. The driver also has no dependency on HANDSHAKE signals. (The NXP application driver depends on DTS)

Which part does not work for you?

HTH, Adib.

Ons KOOLI
Senior III

Hi,

Please refer to the STM32CubeF1 under Applications/USB_Device/CDC_Standalone directory.

Best Regards,

Ons.