Device tree : how to get uart characteristics from DTS?

I'm struggling with SDKConnect and the device tree stuff... and finding it hard to get good examples or docs TBH

So, I have the sample dts files for the nRF5340DK, which defines the config for the 'uart0' node:

&uart0 {
    status = "okay";
    current-speed = <115200>;
    pinctrl-0 = <&uart0_default>;
    pinctrl-1 = <&uart0_sleep>;
    pinctrl-names = "default", "sleep";
};
and the pinctrl mappings:
uart0_default: uart0_default {
        group1 {
            psels = <NRF_PSEL(UART_TX, 0, 20)>,
                <NRF_PSEL(UART_RTS, 0, 19)>;
        };
        group2 {
            psels = <NRF_PSEL(UART_RX, 0, 22)>,
                <NRF_PSEL(UART_CTS, 0, 21)>;
            bias-pull-up;
        };
    };

    uart0_sleep: uart0_sleep {
        group1 {
            psels = <NRF_PSEL(UART_TX, 0, 20)>,
                <NRF_PSEL(UART_RX, 0, 22)>,
                <NRF_PSEL(UART_RTS, 0, 19)>,
                <NRF_PSEL(UART_CTS, 0, 21)>;
            low-power-enable;
        };
    };
aliases {
    console = &uart0;
}
Given this, how to I get the port/pins for my console in my C code? and switch between 'default' and 'sleep' cases easily - given they are in 2 groups for one case and 1 group for the other?
I want to use the nrfx uarte driver, not the zephyr driver, so getting the struct device (via get_device_info()) doesn't help?
I was hoping to be able to do something like:
uint8_t rxd_pin = NRFX_PSEL_UART_RX_PORT(DT_PROP(DT_PROP(DT_ALIAS(console), group1)),psel) << 4 | NRFX_PSEL_UART_RX_PIN(DT_PROP(DT_PROP(DT_ALIAS(console), group1)),
(since I saw similar code to get the pins for an I2C node... NRFX_PSEL_SDA_PORT/PIN macros)
but no such macro seems to exist for uart? I can get the 'current-speed' property ok...
Any pointers on using the pinctrl macros to get NRF_PSEL components? I will confess to being a newbie on the whole device tree config stuff...
Maybe I'm looking in the wrong place - but honestly there are SO many files to keep in mind for the dts stuff....
thanks!
  • BrianW said:
    I'm still stuck with 1 point: I would like to pass the alias name ('myconsole') to the function so that I can call it for multiple uart configurations...  however, the use of the string "myconsole" in the macros makes this impossible (resolved at compile time). Am I right in thinking there is no way to find nodes in the DTS dynamically (ie with the node label, name or alias only known at run time)?

    I am a little confused. How is the alias defined at runtime? In Zephyr, everything on the Devicetree is defined in compile time. Dynamic update of the Devicetree is not supported.

    It is not clear to me what "myconsole" is in your application. If it is the console that is printk() get routed to by default, then you could just use DT_CHOSEN(zephyr_console) like in my sample code.

  • In Zephyr, everything on the Devicetree is defined in compile time. Dynamic update of the Devicetree is not supported.

    I think this is what I was not really grasping, the concept that the DTS is that part you update per board and is then static for that particular hardware. I was thinking of a 'config file' that would be dynamic and  change at run time to suit the specific device environment (eg select different uart configs based on a device name..). 

  • BrianW said:
    I think this is what I was not really grasping, the concept that the DTS is that part you update per board and is then static for that particular hardware.

    I believe that you have gotten it correct now though Slight smile

    What I have heard in passing, but not verified, is the Devicetree solution does support dynamic changes. However, this is only in Linux, and not in Zephyr.

Related