Need for a clear Serial Port Example

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: barthess, RoccoMarco

Posts: 53
Joined: Mon Aug 10, 2015 6:57 am
Has thanked: 5 times
Been thanked: 3 times

Need for a clear Serial Port Example

Postby ceremcem » Tue Jun 25, 2019 8:39 am

I'm trying to prepare a clean example of serial port usage. I can't find the struct reference for the following example in testhal/STM32/STM32F4xx/RTC/main.c:

Code: Select all

static SerialConfig ser_cfg = {


  • Searching Google with the keywords "SerialConfig chibios" redirects to the page: ... onfig.html which displays 404 error page.
  • ChibiOS/HAL 6.1.0 documentation shows that there is only one field is required:

    Code: Select all

       68 typedef struct {
       69   /**
       70    * @brief Bit rate.
       71    */
       72   uint32_t                  speed;
       73   /* End of the mandatory fields.*/
       74 } SerialConfig;

    So where is the rest of the struct reference is documented?


  1. How can I configure the
    • Baudrate
    • Word length
    • Parity
    • Stop bits

    via SerialConfig? Where are the options written?
  2. How is UartConfig related with SerialConfig? Where can I find the differences?

User avatar
Posts: 635
Joined: Wed Apr 24, 2013 4:11 pm
Location: Salerno (Italy)
Has thanked: 77 times
Been thanked: 60 times

Re: Need for a clear Serial Port Example

Postby RoccoMarco » Tue Jun 25, 2019 2:49 pm

the documentation refers only to those fields which are common on every platform. Let me do a step back...

Each driver of ChibiOS/HAL is mainly configured on the xxxStart call which requires two parameters:
  • A pointer to the Driver instance
  • A pointer to the configuration structure

The configuration structure is composed of two parts:
  • A common part which is independent of the underlying hardware
  • Some fields strictly dependent on the underlying hardware

What you find in the documentation is the first one, while the second one basically could be retrieved directly from examples or in the proper header files.

For example, the STM32F4xx uses the USARTv1 LLD thus you could find the definition fo the configuration structure exploring the file

Where you will find this:

Code: Select all

 * @brief   STM32 Serial Driver configuration structure.
 * @details An instance of this structure must be passed to @p sdStart()
 *          in order to configure and start a serial driver operations.
 * @note    This structure content is architecture dependent, each driver
 *          implementation defines its own version and the custom static
 *          initializers.
typedef struct {
   * @brief Bit rate.
  uint32_t                  speed;
  /* End of the mandatory fields.*/
   * @brief Initialization value for the CR1 register.
  uint16_t                  cr1;
   * @brief Initialization value for the CR2 register.
  uint16_t                  cr2;
   * @brief Initialization value for the CR3 register.
  uint16_t                  cr3;
} SerialConfig;

Now, to answer your question the cr1, cr2, and c3 fields represent the value of the registers of the UART you are going to configure and through them, you could change all the configurations you were talking about.

Some further readings:
How is made HAL and how to identify which file you should explore for configurations structures:
An article about ChibiOS serial driver:
Some additional examples:>

Question 2:
UART and Serial are two different drivers based on the same peripheral. Let's say, UART is more about exposing IRQ to the user while Serial offers integrated IO buffering.

Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 4 guests