cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with Consecutive USB Transmissions in EXTI Callback

Shreayas_Acharaya
Associate III

 

Hello ST Community,

I am currently working on a project using an STM32 microcontroller, where I have implemented a feature to transmit the current date and time over USB when a user button is pressed. The USB transmission is handled using the CDC class.

In my code, when the user button is pressed, an EXTI callback function (HAL_GPIO_EXTI_Callback) is triggered. This function reads the current time and date from the RTC and then sends two separate messages over USB using the CDC_Transmit_FS function.

Here is the relevant part of my code:

Screenshot 2024-06-15 122414.png

The problem I am encountering is that only the first message (containing the current time) is transmitted successfully. The second message (containing the current date) does not get transmitted.

I suspect that this issue might be related to the state of the USB CDC interface, potentially being busy when trying to send the second message. However, I am not entirely sure how to handle this properly.

Could someone please help me understand why the second message is not being transmitted and how I can ensure both messages are sent successfully?

Thank you for your assistance!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Andrew Neil
Evangelist III

Remember that the the Callback is still running in the EXTI Interrupt handler context,  so probably not a good idea to be doing all that stuff effectively within the Interrupt Service Routine ... ?

Better to just have your EXTI Callback just set a flag to indicate that the button has been pressed, and then do the processing in your main code.

Also, remember that mechanical buttons bounce ...

 

Please use this button to properly post source code - not as an image:

AndrewNeil_0-1718441431551.png

 

 

View solution in original post

2 REPLIES 2
Andrew Neil
Evangelist III

Remember that the the Callback is still running in the EXTI Interrupt handler context,  so probably not a good idea to be doing all that stuff effectively within the Interrupt Service Routine ... ?

Better to just have your EXTI Callback just set a flag to indicate that the button has been pressed, and then do the processing in your main code.

Also, remember that mechanical buttons bounce ...

 

Please use this button to properly post source code - not as an image:

AndrewNeil_0-1718441431551.png

 

 

Hi Andrew Neil, 

This approach avoids issues with the USB stack being busy and also handles button debouncing more effectively.

Additionally, I would appreciate some help understanding the CDC_Transmit_FS function. Specifically, how does it manage the transmission state, and what are the best practices to ensure data is transmitted reliably?

Thank you for your assistance!