-1
\$\begingroup\$

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);
}
\$\endgroup\$
6
  • 2
    \$\begingroup\$ What does it matter which way your main code tells the MCU to communicate with peripherals? You can still send data and wait until you know it's been sent or wait if you get a response back or not. What aspect of using DMA or interrupts or blocking comms would affect your other code? \$\endgroup\$ Commented Aug 22 at 8:17
  • 2
    \$\begingroup\$ I don't get the question. Polling, interrupt or DMA is a local choice per node and does not affect other nodes. A communications protocol can't have an "interrupt mode", only local UART drivers can have that. Or are you using UART handshaking signals or something? Please clarify. \$\endgroup\$ Commented Aug 22 at 8:18
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 22 at 12:00
  • \$\begingroup\$ 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 */ } \$\endgroup\$ Commented Aug 22 at 12:01
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Aug 22 at 12:01

1 Answer 1

1
\$\begingroup\$

I typically have a low-level UART driver that operates in interrupt mode and simply moves bytes between the UART and software buffers for sending and receiving data. Then the UART driver has a software API that allows application layer tasks to poll whether data has been received (or sent). The data can stay in the UART driver's software buffers for as little or as long as necessary. This design decouples the UART interface from the application interface. The UART driver API could include functions such as:

uart_send_message(); // Copy message data into the TX buffer.
uart_is_tx_data_empty(); // Has the transmit data been sent (TX buffer empty)?
uart_is_rx_data_available(); // Has any data been received (RX buffer not empty)?
uart_receive_message(); // Get the next message from the RX buffer.
\$\endgroup\$
1
  • \$\begingroup\$ I solved the problem by writing a code like this : " if (HAL_GetTick() - lastTempReadTime >= 1000) { lastTempReadTime = HAL_GetTick(); NTC_Read_Temperatures(temperatures); if (!FanControl_GetManualMode()) { float maxTemp = NTC_GetMaxTemperature(); FanControl_Update(maxTemp); } }" \$\endgroup\$ Commented Aug 26 at 13:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.