I am working with UART and I want to do something but could not achieve myself.

I want to enter an interrupt when the data receive stops, i.e when I unplug the cable, do something only once and then just continue. When I plug the cable again, it should will continue receiving values.

I am using UART in DMA mode.

I looked some about IDLE interrupt but I cannot use it as i want, where should its interrupt be placed exactly? When i use USART2_IRQHandler() func. it both enters when I un-plug Rx cable and also re-plug the rx.

So i am confused about how an IDLE interrupt can/should be used?

Thanks in advance.

The board is : STM32F4-DISC

  • \$\begingroup\$ Which exact STM32 model it is? Does it have IDLE interrupt capability? But it won't help much if your data has gaps, it only works when continuous stream of data has a gap. \$\endgroup\$
    – Justme
    Jul 19, 2019 at 7:52
  • \$\begingroup\$ I know in for example the MIDI protocol, a message is sent every x ms, to see if the connection is still ok, so if you can change the protocol, use this 'alive' message to detect when a cable is connected or not. On MIDI it is called 'active sensing' . \$\endgroup\$ Jul 19, 2019 at 9:04
  • 1
    \$\begingroup\$ @MichelKeijzers hey, thanks for the answer. I will have a look at that protocol \$\endgroup\$
    – muyustan
    Jul 19, 2019 at 10:52

1 Answer 1


Use a timer, in the UART receive interrupt you write the timer update flag, TIMx_EGR->UG.

Enable TIMx_CR1->URS, update request source, to disable interrupt generation when software triggers an update event via the UG bit.

When you write UG (write only bit) the timer is reloaded with TIMx_ARR, auto reload value, which is your maximum communication timeout.
When the timer expires, as in it over or underflows, it fires the timer interrupt telling you the communication has expired.
All timers can do this, little math required to compute ARR with the basic timers since these have no countdown mode.

You could also use the Windowed Watchdog Timer for this.

Other option is to look at the signal levels after the RS-232 driver, or use the hardware flow control signals.
Note that TTL level uart is not intended for operation over a cable.

  • \$\begingroup\$ hey, thanks for the answer. By saying "timer is reloaded with ARR" did you consider the downcounting mode? For upcounting it will make timer counter equals 0 right? Also didn't get what you mean with the sentence about basic timers. Does it make a difference to use up or down counting modes? Anyway, as general I think I understood how your method works. I will try it. \$\endgroup\$
    – muyustan
    Jul 19, 2019 at 11:01
  • \$\begingroup\$ Also, you said "in the UART receive interrupt"; is "HAL_UART_RxCpltCallback" function in the HAL library an equivalent to what you intended ? \$\endgroup\$
    – muyustan
    Jul 19, 2019 at 11:38
  • \$\begingroup\$ @muyustan ST has three timer versions, basic, general purpose and advanced-control timers. Not all have countdown. ARR can be set to anything, reload should still work I think for basic timers. Also yes, the place where you handle the received characters before the DMA. \$\endgroup\$
    – Jeroen3
    Jul 19, 2019 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.