RTEMS | dev/serial/zynq: Improve baud calculation and simplify code (!199)

Sebastian Huber (@sebhub) gitlab at rtems.org
Tue Sep 17 03:38:11 UTC 2024




Sebastian Huber commented on a discussion on bsps/shared/dev/serial/zynq-uart-polled.c: https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/199#note_112141

 > +  uint32_t  desired_baud,
 > +  uint32_t  mode_clks,
 > +  uint32_t *cd_ptr,
 > +  uint32_t *bdiv_ptr
 > +)
 > +{
 > +  uint32_t best_error = UINT32_MAX;
 > +  uint32_t best_cd;
 > +  uint32_t best_bdiv_plus_one;
 > +  uint32_t bdiv_plus_one;
 > +  uint32_t selected_clock;
 > +
 > +  _Assert((mode_clks & ~ZYNQ_UART_MODE_CLKS) == 0);
 > +  selected_clock = zynq_uart_input_clock() / (1U << (3 * mode_clks));
 > +
 > +  for (bdiv_plus_one = 5; bdiv_plus_one <= 256; ++bdiv_plus_one) {

The previous code just selected the best error. In the new code we have this:

```
    /*
     * The procedure to detect a start bit uses three samples in the middle of
     * an RX-bit.  If the sample set is too small, there may be a sample in
     * another bit in case the baud setting is not accurate.  Most noise is in
     * the form of small peaks, if the sample rate is too high, then noise may
     * get detected as a bit.
     *
     * Prefer an sample set of around 16 per RX-bit.
     */
    if (error < best_error || (bdiv_plus_one <= 20 && error <= best_error)) {
      best_error = error;
      best_cd = cd;
      best_bdiv_plus_one = bdiv_plus_one;
    }
```

In my tests I got this on the ZCU104:

```
desired baud: 9600, actual baud: 9600, error 0, max error: 288, cd: 651, bdiv: 15
desired baud: 19200, actual baud: 19201, error 1, max error: 576, cd: 372, bdiv: 13
desired baud: 38400, actual baud: 38402, error 2, max error: 1152, cd: 186, bdiv: 13
desired baud: 57600, actual baud: 57603, error 3, max error: 1728, cd: 124, bdiv: 13
desired baud: 115200, actual baud: 115207, error 7, max error: 3456, cd: 62, bdiv: 13
```

-- 
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/199#note_112141
You're receiving this email because of your account on gitlab.rtems.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/bugs/attachments/20240917/d5e0fc92/attachment-0001.htm>


More information about the bugs mailing list