Closed OmerGozderesi closed 4 months ago
Hi @OmerGozderesi! We appreciate you submitting your first issue for our open-source project. 🌟
Even though I'm a bot, I can assure you that the whole community is genuinely grateful for your time and effort. 🤖💙
Describe the bug
The esp32 UART rx FIFO threshold is fixed at 0x16 bytes, leading to premature end of frame detection and incorrect decoding of Modbus packets larger than 0x16 bytes.
When Modbus message size is greater than 0x16, callback in Modbus layer is called with 0x16 bytes of data and end of frame detection timer is started. But before the callback is called (due to rx event) again, the end of frame timer expires, leading to premature decoding attempt at the, now, incomplete modbus message. This is because the rest of the message does not trigger rx threshold interrupt to fire. Since the premature decoding attempt throws away the first 0x16 bytes of data, the attempt to decode the data remaining in the FIFO also fails.
This is demonstrated in the following example. A successful response for the following message is 25 bytes. esp32 UART stack reports first 22 (0x16) bytes of data via callback. Decoding it fails. And then UART timeout happens, and Modbus stack tries to decode the remaining 3 (25-22) bytes and it results in a frame length error as per https://github.com/zephyrproject-rtos/zephyr/blob/9ae5352372dd9965805980e0c712612f21ed2229/subsys/modbus/modbus_serial.c#L251-L255
To Reproduce
While developing on a custom board, encountering issues due to the fixed fifo threshold. Below is a snippet of the code:
Running the build and flash commands:
west build -b esp32c3_devkitm applatications/app && west flash && west espressif monitor
Expected behavior
Expected to be able to read holding registers up to the maximum quantity (127) as defined in the Modbus specifications.
Impact
Unable to handle Modbus buffers larger than 0x16 bytes, leading to errors during communication.
Logs and console output
Describe the solution you'd like
Introduce Kconfig options to define both the RX and TX FIFO thresholds for the ESP32 UART, allowing them to be configurable while keeping the default value as 1.
CONFIG_UART_ESP32_TX_FIFO_THRESH
CONFIG_UART_ESP32_RX_FIFO_THRESH
Environment
OS: (MacOS Sonoma 14.5) Toolchain: (Zephyr SDK 0.16.6) Zephyr : latest release