cancel
Showing results for 
Search instead for 
Did you mean: 

Mixing Polling Mode Modules with UART Interrupt Mode on STM32: Is It Safe?

durna
Senior

I'm developing a project using an STM32 Nucleo board. At the core of this project is a UART-based communication protocol running in interrupt (IT) mode — not polling or DMA. This protocol is responsible for controlling multiple subsystems such as a relay module and a fan speed controller.

However, most of these subsystem modules were previously written in polling mode.

My question is:
Can I integrate these polling-based modules into my current UART IT-mode communication system without causing issues? Or should I consider converting them to interrupt or DMA mode to prevent CPU blocking or communication delays?

Additionally:
What should I watch out for to ensure the CPU doesn't get blocked and UART interrupts don't get delayed in this mixed-mode setup?

Extra context:
There are 20 NTC temperature sensors in the system, and temperature from all of them is read once every second using ADC. The highest temperature value is selected and stored in a variable. Based on this value, the fan speed is adjusted accordingly.

Note: The polling-based modules do not contain any infinite loops like while(1) or other blocking code. For example, the ntc reader and fan control logic looks like:

 

 

NTC_Read_Temperatures(temps);
HAL_Delay(1000);


if(max_temp == 54.23f ){
speed_of_fan = mode_2;
hal_delay(1);
}

1 ACCEPTED SOLUTION

Accepted Solutions
durna
Senior

I solved

I should use like this code;


if (HAL_GetTick() - lastTempReadTime >= 1000)
{
lastTempReadTime = HAL_GetTick();
NTC_Read_Temperatures(temperatures);

if (!FanControl_GetManualMode())
{
float maxTemp = NTC_GetMaxTemperature();
FanControl_Update(maxTemp);
}
}

View solution in original post

8 REPLIES 8
mbarg.1
Senior III

At first look, I would suggest move UART to DMA and keep all program as it is.

Polling DMA result at intervals, allow to detect what happened on UART without disturbing other functions.

Remember that DMA must be stopped on error or timeout.

mike

Thank you. Yes, moving the communication module to DMA would at least ensure its functionality. But what would happen if I left it in IT mode as it is now? What potential dangers would there be in this case?

If you are playing only TX, no problems, but with RX you will need to manage rx buffer before oveflowing - you already bought a device with DMA, use it,it is free, and you will be much more reliable.

I think I didn’t fully explain my question clearly, so let me try again:


I’m working on a hobby project. In summary, based on commands sent by the user via the command line (received through a UART-based interrupt method), the fan speed changes, some relays turn on or off, or the user can view temperature sensor data on the terminal screen. There are a total of 20 temperature modules, and they are measured once every second. The highest temperature value is then stored in a variable, and the fan speed is adjusted based on this value. For the temperature measurement process, there’s the following code snippet in the main.c file:


while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
NTC_Read_Temperatures(temps);
HAL_Delay(1000);
/* USER CODE END 3 */
}


Does this code mean that the CPU will be occupied with only the temperature reading process for the entire 1 second? Or does it mean that the CPU is only busy for the duration of reading the 20 ADCs (~2 ms) every 1 second?


BR

This means only that you do not have any idea about what is a MCU ..

CPU will only execute what is in while loop - read temp, wait 1 sec, read temp .. -  where is uart call ? 

As you have uart rx requirements, DMA is preferred - of course you will have to read MCU reference manual, HAL functions manual, MCU errata and decide which are you requirements in term of time to reaction from data rx and data tx ...

Pavel A.
Super User

Can I integrate these polling-based modules into my current UART IT-mode communication system without causing issues?

Of course you can. This can be done with intermediate buffer between the polling stuff and interrupt driven routines.

The latter will store bytes in a ring buffer (or whatever), then the polling read function will fetch from the buffer. Same for DMA.

If you are new to STM32 and need help with coding, help is available here.

 

 

 

The HAL_Delay() will block for around 1 second, but other interrupt / background activity will continue as normal.

If you want to do multiple things in the loop, you could perhaps keep looping an do the 1 second activity when you've observed 1000 ms of time has elapsed. ie by using HAL_GetTick(), and subtracting out the starting point, or have SysTick, or TIM interrupts trigger a flagging event.

Initially I thought the question was about polling the UART and using interrupts for it. Which you can do, but don't create race conditions. It would be better to use interrupts, and buffer UART reception, and check the buffer for available data rather than the UART directly.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
durna
Senior

I solved

I should use like this code;


if (HAL_GetTick() - lastTempReadTime >= 1000)
{
lastTempReadTime = HAL_GetTick();
NTC_Read_Temperatures(temperatures);

if (!FanControl_GetManualMode())
{
float maxTemp = NTC_GetMaxTemperature();
FanControl_Update(maxTemp);
}
}