Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Usart driver enhancements #10

Merged
merged 16 commits into from
Mar 5, 2024
Merged

feat: Usart driver enhancements #10

merged 16 commits into from
Mar 5, 2024

Conversation

shadow578
Copy link
Owner

@shadow578 shadow578 commented Feb 28, 2024

this PR introduces various enhancements to the Usart driver.
enhancements listed in this PR are listed in the chronological order they were added in.

Usart clock divider and Oversampling auto-config

The Problem

currently, the Usart driver uses a fixed clock divider of 1 and 8x oversampling.
due to limitations in the usart peripheral baud rate generator, this configuration may cause sub-optimal internal baud rate generation.

given a PCLK1 clock of 50MHz and a target baud rate of 115200, the actual baud rate would be closer to ~114995 baud.
using a different configuration (clkdiv=4 and 16x oversampling) this error can be minimized.

additionally, with the current configuration baud rates <= 19200 baud are not possible.

Solution

to solve this limitation, an automatic configuration of the clock divider and oversampling setting are added.
this new function can be enabled using the USART_AUTO_CLKDIV_OS_CONFIG define.

at runtime, the algorithm calculates the actually achived baud rate according to the formulars given in the HC32F460 reference manual (ref 1.5, page 621, Table 25-9 and Table 25-10) for every clock divider and oversampling setting.
it then selects a configuration such that the error between target and achived baud rate is minimal.

in a real-world application (Marlin host-print with Octoprint), the serial error rate was reduced to below 0,4% with these changes.

25-9 25-10
excerpt of Table 25-9 excerpt of Table 25-10

Usart RX line noise filtering

the HC32 supports enabling noise filtering on the usarts RX line.
this option will now be enabled by default, but can be disabled by setting rxNoiseFilter=false in Usart::begin(uint32_t baud, const stc_usart_uart_init_t *config, const bool rxNoiseFilter).

Usart receive DMA support

when defining USART_RX_DMA_SUPPORT, the Usart driver can be configured to use DMA for receiving data.
to enable RX DMA, Usart::enableRxDma(M4_DMA_TypeDef *dma, en_dma_channel_t channel) must be called before calling Usart::begin.

in a real-world application (Marlin host-print with Octoprint), enabling receive DMA reduced the error rate to >0.01% (in preliminary testing no errors occured)

Usart error counters

when defining USART_RX_ERROR_COUNTERS_ENABLE, the Usart driver keeps track of receive errors.
the counters can be accessed using the Usart::getFramingErrorCount(), Usart::getParityErrorCount(), Usart::getOverrunErrorCount() and Usart::getDroppedDataErrorCount() functions.

Misc.

change to IRQ RX handler

the IRQ RX handler was updated to match the behaviour of the DMA RX handler.
previously, when the RX ring buffer was full and new data was received, the new data was discarded.
from now on, the new data will be pushed into the buffer and the oldest entry is discarded instead.

when data is discarded, a RxDataDropped is occured and the DroppedDataErrorCount counter is incremented.

added config for USART4 peripheral

the HC32 has four USART peripheral, but until now the core only included configurations for USART1 - 3.
with this PR, the configuration for USART4 was added.

`USART_RX_ERROR_COUNTERS_ENABLE` option
@shadow578 shadow578 changed the title feat: Usart driver enhancements feat: Usart driver enhancements Feb 29, 2024
cores/arduino/RingBuffer.h Outdated Show resolved Hide resolved
@shadow578 shadow578 marked this pull request as ready for review March 4, 2024 11:41
@shadow578 shadow578 merged commit 81e7019 into main Mar 5, 2024
4 checks passed
@shadow578 shadow578 deleted the add/usart-dma branch March 5, 2024 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant