How to set UART Baudrate ?

I'm referring to the sample code : ncs \ v1.9.1 \ nrf \ samples \ bluetooth \ peripheral_uart

I'm not happy at all with the samples, the documentation is quite poor in many ways and the online help not much better

The sample itself is working and I was able to find out (using a scope) that it is using UART settings 115200-8n1 - would you mind to explain me why a UART sample does not contain a single line of comment telling me what the communication settings are ?

Now, I need to switch to 57600-8n1 ... I have no clue how to do this, there is nothing in the sample code, neither in the online help, nor anything available in the Kconfig.

And browsing through devzone, it seems I'm not the only one with this issue.

Can you please tell me how to change the baudrate ?

thanks, Matthias

Parents Reply Children
  • that does not work - all the "preset" things are undeclared

  • It was just a sample. You should study "struct"

    struct uart_config {
    	uint32_t baudrate;
    	uint8_t parity;
    	uint8_t stop_bits;
    	uint8_t data_bits;
    	uint8_t flow_ctrl;
    };
    
    /** @brief Parity modes */
    enum uart_config_parity {
    	UART_CFG_PARITY_NONE,
    	UART_CFG_PARITY_ODD,
    	UART_CFG_PARITY_EVEN,
    	UART_CFG_PARITY_MARK,
    	UART_CFG_PARITY_SPACE,
    };
    
    /** @brief Number of stop bits. */
    enum uart_config_stop_bits {
    	UART_CFG_STOP_BITS_0_5,
    	UART_CFG_STOP_BITS_1,
    	UART_CFG_STOP_BITS_1_5,
    	UART_CFG_STOP_BITS_2,
    };
    
    /** @brief Number of data bits. */
    enum uart_config_data_bits {
    	UART_CFG_DATA_BITS_5,
    	UART_CFG_DATA_BITS_6,
    	UART_CFG_DATA_BITS_7,
    	UART_CFG_DATA_BITS_8,
    	UART_CFG_DATA_BITS_9,
    };
    
    /**
     * @brief Hardware flow control options.
     *
     * With flow control set to none, any operations related to flow control
     * signals can be managed by user with uart_line_ctrl functions.
     * In other cases, flow control is managed by hardware/driver.
     */
    enum uart_config_flow_control {
    	UART_CFG_FLOW_CTRL_NONE,
    	UART_CFG_FLOW_CTRL_RTS_CTS,
    	UART_CFG_FLOW_CTRL_DTR_DSR,
    };

    just enter according parameter

    const struct uart_config uart_cfg = {
                                    .baudrate  = 115200,
                                    .parity    = UART_CFG_PARITY_NONE,
                                    .stop_bits = UART_CFG_STOP_BITS_1,
                                    .data_bits = UART_CFG_DATA_BITS_8,
                                    .flow_ctrl = UART_CFG_FLOW_CTRL_NONE
                    };
    uart_configure(uart, &uart_cfg);

  • Hi,

    Just like how @Vasiliy mentioned, you can change the UART baudrate dynamically by creating a variable of type uart_config. For more details you can take a look at our Dev academy.

    Kind Regards,

    Priyanka

Related