Skip to content

Conversation

@joelguittet
Copy link
Contributor

@joelguittet joelguittet commented May 12, 2025

Add support for UART bitbang driver. The initial purpose of this driver is to support specific modes, such as 9-bits mode on target that doesn't support such feature (esp32 for example). It can also be used to have more UART interfaces.

It supports RS485 communication with Driver Enable output.
It supports none, odd, even, mark and space parity configuration and it supports 0.5, 1, 1.5 and 2 stop bits configuration.
It is using 2 counters to generate Tx data and sample Rx line. In the case of Half-Duplex communications the timer can be the same instance.

It has been developed on STM32L4A6 Nucleo board, and the UART passthrough sample is updated to demonstrate the usage of the driver.

Unfortunately for the moment it doesn't support ESP32 targets (my initial expectation) because of the lack of counter driver feature on Espressif port (counter set_top_value API is used to setup periodic interrupts and for the moment this API is not available on every hardware). I may update this Pull-Request later or with a new Pull-Request when it's done, @sylvioalves already looking at this topic.

Regarding to performances, I successfully tested the communications up to 62500 baud. I have regularly errors at 115200. By nature this driver has no synchronization signal and is sensitive to latencies on the target. Having parity/checksum is a good mitigation to detect errors when using this driver.

I'm open to any remark/suggestions, please do not hesitate

@github-actions github-actions bot added area: Samples Samples area: UART Universal Asynchronous Receiver-Transmitter area: Devicetree labels May 12, 2025
@joelguittet joelguittet force-pushed the feature/drivers-uart-bitbang branch 2 times, most recently from 37730cb to e7b242b Compare May 12, 2025 20:17
@kartben kartben requested a review from Copilot May 12, 2025 20:20

This comment was marked as off-topic.

@joelguittet joelguittet force-pushed the feature/drivers-uart-bitbang branch from e7b242b to 530366f Compare May 12, 2025 20:27
@dcpleung
Copy link
Member

To be honest, there are too many variables affecting the performance of this. Bitbanging UART data lines need to be done with strict timing control. It would work on baremetal programs as they have total control of the hardware. With an OS, it can lock interrupts and you may miss the tiny time window to send/receive a bit. This will be very hard to debug, and will be very confusing to any user of this.

Any particular reasons that you must have this?

@joelguittet joelguittet force-pushed the feature/drivers-uart-bitbang branch from 530366f to 2249f69 Compare May 12, 2025 20:39
@joelguittet
Copy link
Contributor Author

joelguittet commented May 12, 2025

@dcpleung yes sure, this is what I have introduced in the first message of the PR. Having specific modes on some platforms that doesn't support it, eg 9-bits mode is not supported on ESP32 UART peripherals (without the cost of extra hardware - SPI or I2C to UART controller for instance).

Of course people using this must take care of this limitation. In case interrupts are disabled during a frame transfert then... as you said may be difficult to debug.

I'm using this for a development using an RS485 interface at 62500 baud. I have parity and checksum so detection of errors is easy and I can send again when this occurs (not seen for the moment but the project is just beginning so not so much stuff in the app).

@joelguittet
Copy link
Contributor Author

@dcpleung I can add "select EXPERIMENTAL" in the Kconfig if you want.

@joelguittet
Copy link
Contributor Author

Note: I don't know why some twister tests fail. This seems not related to my modifications...

@dcpleung
Copy link
Member

If you are not strict on transferring raw bytes on serial lines, encapsulating the data in another format would properly work better. 8-bit at 115200 should yield faster overall speed than 9-bit at 62500 even accounting for software overhead to en-/de-code data. AFAIK, the UARTs on ESP32 do support RS485 (according to their technical manual).

@joelguittet
Copy link
Contributor Author

This is not my purpose, the protocol I'm using is not my own design. This is an existing protocol and I'm interfacing with existing devices.
The protocol I'm using is xpressnet, from Lenz. It' used to control hobbyist railroad layouts.

So, instead of keeping a custom implementation for me, I have designed this driver and I'm sharing with the community. The xpressnet protocol part itself has no interest in zephyr of course, but the uart driver can be reused, knowing limitations we have shared above. We know that due to the nature of the uart hardware (no clock signal), it is not possible to do better than relying on the frequency of interrupts, just cross fingers the deviation is enough small (actually on my prototype I'm monitoring this and I'm always under 1% deviation).

Either it's interesting in zephyr and here is the PR, else if we think it has no value, I can close it. Tell me.

@dcpleung
Copy link
Member

It would be great if you can add some warnings on the kconfig help about the limitations and downfalls.

There is no need for EXPERIMENTAL as it is for features, not for drivers.

@joelguittet joelguittet force-pushed the feature/drivers-uart-bitbang branch 4 times, most recently from 66a5212 to 11f0fa4 Compare May 15, 2025 20:37
@joelguittet
Copy link
Contributor Author

Information: I tested on ESP32 and this is working as well, but it depends on #90300 and #89932. I will add an esp32-devkitc board configuration to the passthrough sample if both mentioned PR are integrated.

@dcpleung
Copy link
Member

In that case, I think we can wait for those PRs to be merged first.

Initial support for uart bitbang driver.

Signed-off-by: Joel Guittet <joelguittet@gmail.com>
Add sample for uart bitbang driver.

Signed-off-by: Joel Guittet <joelguittet@gmail.com>
@joelguittet joelguittet force-pushed the feature/drivers-uart-bitbang branch from 11f0fa4 to 05c1958 Compare May 30, 2025 12:22
@sonarqubecloud
Copy link

@joelguittet
Copy link
Contributor Author

joelguittet commented Jun 3, 2025

@dcpleung dependencies for esp32 have been merged and I added an overlay to the sample, so it's ready for review.

Question for you: I'm actually thinking to an improvement to have much better timings and avoid the deviation, even if it's low. But this will require 2 additional GPIOs and 2 logic gates. I haven't seen such kind of "requirement" on other drivers. Is it acceptable? Maybe conditioned to an additional configuration option ? Or maybe it should simply be another driver ?

@dcpleung
Copy link
Member

dcpleung commented Jun 3, 2025

That would depend on how complex the additions would be. If these are simple enough, I think it's okay to add into the same, and guard them being kconfigs. Personally, I would use readability to gauge whether this is a good idea. Others will be reading the code in the future. The question would be if they, who only know UART but without your background knowledge, are able to follow the logic in code .

@joelguittet
Copy link
Contributor Author

Let's say for the moment I keep this implementation. May be a future improvement with a new build option.

@github-actions
Copy link

github-actions bot commented Aug 5, 2025

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Aug 5, 2025
@joelguittet
Copy link
Contributor Author

@dcpleung any news here ? Thanks

@github-actions github-actions bot removed the Stale label Aug 10, 2025
@henrikbrixandersen
Copy link
Member

Retriggered CI. Previous run was 81 days ago.

@zephyrbot zephyrbot requested a review from mbolivar August 19, 2025 14:17
@sonarqubecloud
Copy link

@kartben kartben merged commit 820aa7a into zephyrproject-rtos:main Aug 19, 2025
52 checks passed
@joelguittet joelguittet deleted the feature/drivers-uart-bitbang branch August 30, 2025 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Devicetree area: Samples Samples area: UART Universal Asynchronous Receiver-Transmitter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants