Search code examples
cusbcdc

STM32 USB CDC Operation


I have created a project using STMCubeMX which includes a usbd driver configured as a virtual com port. I have it working and can receive data via the CDC_Receive_FS callback. My question is how is this callback called. Is it done at interrupt level, or is there some other mechanism. In particular, if I want to copy the data from the callback buffer into a queue which will be read by my main code, do I need some protection for concurrency (e.g. disable interrupts)?

Thanks.


Solution

  • It is called from an ISR. (Interrupt Service Routine)

    Most likely it is called from:

    OTG_HS_IRQHandler.  
    

    (with several levels of functions inbetween).

    Here is a copy of my stack inside of a breakpoint.

    CDC_Receive_HS() at usbd_cdc_if.c:456 0x801c758 
    USBD_CDC_DataOut() at usbd_cdc.c:699 0x8031592  
    USBD_LL_DataOutStage() at usbd_core.c:331 0x80318aa 
    HAL_PCD_DataOutStageCallback() at usbd_conf.c:249 0x801e486 
    HAL_PCD_IRQHandler() at stm32f7xx_hal_pcd.c:359 0x802d264   
    OTG_HS_IRQHandler() at stm32f7xx_it.c:288 0x801ab74 
    

    You most likely do NOT need to disable other interrupts just to copy this data to another buffer. I believe the buffer it uses should only be used by the usb receive. Copy the data to a separate buffer. The new buffer will need concurrency protection when used outside of this interrupt.

    If you are using FreeRTOS, I recommend using the "xQueue" type as a buffer. It is thread safe. You use xQueueSendToBackFromISR inside of interrupts and xQueueSendToBack outside of interrupts.