Skip to content

modbus: serial: Ignore received data when reception is disabled#87482

Merged
dkalowsk merged 1 commit intozephyrproject-rtos:mainfrom
msalau:modbus-ignore-rx-data-in-tx-mode
Jun 11, 2025
Merged

modbus: serial: Ignore received data when reception is disabled#87482
dkalowsk merged 1 commit intozephyrproject-rtos:mainfrom
msalau:modbus-ignore-rx-data-in-tx-mode

Conversation

@msalau
Copy link
Contributor

@msalau msalau commented Mar 21, 2025

A byte received when reception has been disabled
corrupts internal state of the server
(e.g. during transmission of a reply in server mode). The reponse packet is corrupted and its transmission is aborted and the data in the buffer is treated by the server as a new incoming packet. Since the buffer is corrupted CRC doesn't match and the following log message is printed:

modbus_serial: Calculated CRC does not match received CRC

This condition happens when uart_irq_rx_ready() returns true if there is a new byte in the receive FIFO even with disabled RX interrupt.

The issue has been discovered on a nucleo_u083rc board with a RS485 transceiver with the RI signal floating (a pull-down gives more stable reproduction). The pull-down ensures that RI is low during transmission which is seen as byte 0 with a framing error by the receiver. The byte is received by the MCU and corrupts the response. Similar effect can be achieved by not disabling the receiver during transmission (i.e. nRE is driven by the MCU and is fixed low).

The fix discards any data received when reception has been disabled.

@msalau
Copy link
Contributor Author

msalau commented Mar 21, 2025

I think the pull-request should fix #42366

@msalau
Copy link
Contributor Author

msalau commented Mar 24, 2025

Tested the change with RE=0
image
Responses are sent correctly and no warnings are present in the log.

A byte received when reception has been disabled
corrupts internal state of the server
(e.g. during transmission of a reply in server mode).
The reponse packet is corrupted and its transmission is aborted and the
data in the buffer is treated by the server as a new incoming packet.
Since the buffer is corrupted CRC doesn't match and the following log
message is printed:

  <wrn> modbus_serial: Calculated CRC does not match received CRC

This condition happens when uart_irq_rx_ready() returns true if there is
a new byte in the receive FIFO even with disabled RX interrupt.

The issue has been discovered on a nucleo_u083rc board with a RS485
transceiver with the RI signal floating (a pull-down gives more stable
reproduction). The pull-down ensures that RI is low during transmission
which is seen as byte 0 with a framing error by the receiver.
The byte is received by the MCU and corrupts the response.
Similar effect can be achieved by not disabling the receiver during
transmission (i.e. nRE is driven by the MCU and is fixed low).

The fix discards any data received when reception has been disabled.

Signed-off-by: Maksim Salau <msalau@iotecha.com>
@msalau msalau force-pushed the modbus-ignore-rx-data-in-tx-mode branch from 013b103 to 54da380 Compare April 14, 2025 10:00
@msalau
Copy link
Contributor Author

msalau commented Apr 14, 2025

Hello @jfischer-no
Thank you for feedback.

It seems I found a proper way to drain the RX FIFO before enabling reception.
I've added a function to drain the RX FIFO. It is used in RX handler when reception is disabled and in the TX complete handler right before enabling the RX interrupt.

Tested the change in 2 modes:

  • uart_irq_rx_ready() == true when FIFO is not empty but RX IRQ is disabled (e.g. stm32)
  • uart_irq_rx_ready() == false when RX IRQ is disabled (e.g. silabs, renesas ra)

Both modes were tested using an stm32 board. For the second one uart_stm32_irq_rx_ready() has been patched in the following way:

static int uart_stm32_irq_rx_ready(const struct device *dev)
{
	const struct uart_stm32_config *config = dev->config;
	/*
	 * On stm32 F4X, F1X, and F2X, the RXNE flag is affected (cleared) by
	 * the uart_err_check function call (on errors flags clearing)
	 */
-	return LL_USART_IsActiveFlag_RXNE(config->usart);
+	return LL_USART_IsActiveFlag_RXNE(config->usart) && LL_USART_IsEnabledIT_RXNE_RXFNE(config->usart);
}

@msalau msalau requested a review from jfischer-no April 14, 2025 10:36
jfischer-no
jfischer-no previously approved these changes Apr 26, 2025
@kartben kartben requested a review from Copilot May 2, 2025 08:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug where stale UART data corrupts the response packet when reception is disabled. Key changes include adding a function to drain the RX FIFO, updating RX enabling/disabling logic with atomic state tracking, and updating internal definitions to support the new state.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
subsys/modbus/modbus_serial.c Added a FIFO drain function and updated RX on/off and TX handlers
subsys/modbus/modbus_internal.h Defined MODBUS_STATE_RX_ENABLED to track reception state

@msalau msalau force-pushed the modbus-ignore-rx-data-in-tx-mode branch from 54da380 to c7abbc7 Compare May 5, 2025 07:01
Copy link
Contributor

@jfischer-no jfischer-no left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a multiple review failure. A human ask review by trashpilot, trashpilot made a trash suggestion, and the humans blindly followed it.

@kartben
Copy link
Contributor

kartben commented May 7, 2025

Looks like a multiple review failure. A human ask review by trashpilot, trashpilot made a trash suggestion, and the humans blindly followed it.

And in what world is that calling for using such language?
Also, please do state clearly what is your request for change (e.g. going back to commit xyz). Throwing a random -1 is not helping.
https://docs.zephyrproject.org/latest/contribute/reviewer_expectations.html#:~:text=Reviewers%20shall%20be%20clear%20about%20what%20changes%20they%20are%20requesting%20when%20the%20%E2%80%9CRequest%20Changes%E2%80%9D%20option%20is%20used

@jfischer-no
Copy link
Contributor

Looks like a multiple review failure. A human ask review by trashpilot, trashpilot made a trash suggestion, and the humans blindly followed it.

And in what world is that calling for using such language?

That's fine in any world that values concise and honest communication. You seem to have a problem with this, please work on it.

Also, please do state clearly what is your request for change (e.g. going back to commit xyz). Throwing a random -1 is not helping. https://docs.zephyrproject.org/latest/contribute/reviewer_expectations.html#:~:text=Reviewers%20shall%20be%20clear%20about%20what%20changes%20they%20are%20requesting%20when%20the%20%E2%80%9CRequest%20Changes%E2%80%9D%20option%20is%20used

It is a line change between my reviews, which I am pretty sure is very easy to figure out. Also, before you blame me further, note that it was your actions that caused me to request changes.

@kartben
Copy link
Contributor

kartben commented May 7, 2025

Looks like a multiple review failure. A human ask review by trashpilot, trashpilot made a trash suggestion, and the humans blindly followed it.

And in what world is that calling for using such language?

I suggested that maybe the comment was valid, @msalau, who is in a position to actually test things, thought so too, how does that make either of us "humans blindly following the suggestions"?
To be perfectly clear, I would have more than likely made the exact same suggestion without Copilot -- so yeah sh** happens and sometimes reviewers make wrong suggestions, just like sometimes reviewers might miss things that others catch. That's the whole point of the review process, I think.

@kartben
Copy link
Contributor

kartben commented May 7, 2025

Also, please do state clearly what is your request for change (e.g. going back to commit xyz). Throwing a random -1 is not helping. https://docs.zephyrproject.org/latest/contribute/reviewer_expectations.html#:~:text=Reviewers%20shall%20be%20clear%20about%20what%20changes%20they%20are%20requesting%20when%20the%20%E2%80%9CRequest%20Changes%E2%80%9D%20option%20is%20used

It is a line change between my reviews, which I am pretty sure is very easy to figure out. Also, before you blame me further, note that it was your actions that caused me to request changes.

I guess I was simply hoping you would comment on the why it is wrong, since it's apparently so obvious to you the change is "trash" and yet it is not necessarily the case for at least 2 of the people involved in this PR who could maybe learn something for the future (plus anyone else stumbling open this PR in the future).

@msalau
Copy link
Contributor Author

msalau commented May 12, 2025

Hello @jfischer-no

I don't think that the change is unreasonable. Unfortunately I see discrepancies between documentation and actual behavior regularly and tend to be over-cautious in such cases.

The change decreases performance by making one additional syscall to confirm that FIFO is empty. I thought this is not a big deal.

I'll revert the change.

Thanks and regards
Maksim

@msalau msalau force-pushed the modbus-ignore-rx-data-in-tx-mode branch from c7abbc7 to 54da380 Compare May 12, 2025 11:03
@msalau msalau requested review from jfischer-no and kartben May 12, 2025 11:11
@sonarqubecloud
Copy link

@jfischer-no
Copy link
Contributor

I don't think that the change is unreasonable. Unfortunately I see discrepancies between documentation and actual behavior regularly and tend to be over-cautious in such cases.

It clearly is, no doubts. It does not matter what capabilities specific UART controller/driver has, the change was incorrect, not overcautious.

@dkalowsk dkalowsk merged commit c2fd84f into zephyrproject-rtos:main Jun 11, 2025
49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Comments