How to read data coming from an external board via UART with nrF5340?

Hi, I am new to the nordic world. I am interested in developing an application that allows me to send data via UART from an X board and read that data with the nordic nRF5340.

I am trying to follow the base explained in academy.nordicsemi.com/.../ however it is not clear to me how is the process to be able to use other GPIO pins apart from the ones that control the LEDs. I understand that an overlay file must be used but I still have doubts.

Could someone provide me with an example code (apart from the templates that come in nRF Connect for VS Code) with this configuration? I mean that I can receive data from an external board and read them with the nordic nrf5340.

Thanks in advance.
Best regards.

Translated with www.DeepL.com/Translator (free version)

  • Hello Miguel,

    If you followed this base example closely and it does not work, a likely problem is that the devicetree-overlay is not complete.

     

    &uart0 {
    		compatible = "nordic,nrf-uarte";
    		reg = <0x8000 0x1000>;
    		interrupts = <8 NRF_DEFAULT_IRQ_PRIORITY>;
    		status = "okay";
    		current-speed = <115200>;
    		pinctrl-0 = <&uart0_default>;
    		pinctrl-1 = <&uart0_sleep>;
    		pinctrl-names = "default", "sleep";
    };
    
    &uart0_default {
    		group1 {
    				psels = <NRF_PSEL(UART_TX, 0, 29)>,
    						<NRF_PSEL(UART_RTS, 0, 27)>;
    		};
    		group2 {
    				psels = <NRF_PSEL(UART_RX, 0, 28)>,
    						<NRF_PSEL(UART_CTS, 0, 26)>;
    				bias-pull-up;
    		};
    };
    &uart0_sleep {
    		group1 {
    				psels = <NRF_PSEL(UART_TX, 0, 29)>,
    						<NRF_PSEL(UART_RX, 0, 28)>,
    						<NRF_PSEL(UART_RTS, 0, 27)>,
    						<NRF_PSEL(UART_CTS, 0, 26)>;
    				low-power-enable;
    		};
    };

    It should look something like that. You can choose the UART-GPIOs with the ‘psels’ properties in the ‘uartX_default’ and the ‘uartX_sleep’ nodes which are referenced in the ‘uart0’ node. To activate the UART driver the ‘status’ property in the ‘uart0’ node must be set to “okay”.

    My process of creating the overlay is by looking up the needed nodes in the ‘Compiled devicetree output’ (which can be opened in the SDK) and referencing them in the overlay with ‘&LABEL’.

    Regards,

    Sandro

  • Hi Miguel,

    Welcome to the Nordic world. Regarding UART, there is echo_bot sample which can help you to get started with Nordic development. When building the sample for nRF52840dk_nrf52840 in VS Code, you can see UART configuration in nrf52840dk_nrf52840-pinctrl.dtsi and nrf52840dk_nrf52840.dts files: Here is relevant application part in VS Code:



    Similar to Sandro's answer, relevant (UART) configuration from nrf52840dk_nrf52840-pinctrl.dtsi file is shown below.

    /*
     * Copyright (c) 2022 Nordic Semiconductor
     * SPDX-License-Identifier: Apache-2.0
     */
    
    &pinctrl {
    	uart0_default: uart0_default {
    		group1 {
    			psels = <NRF_PSEL(UART_TX, 0, 6)>,
    				<NRF_PSEL(UART_RTS, 0, 5)>;
    		};
    		group2 {
    			psels = <NRF_PSEL(UART_RX, 0, 8)>,
    				<NRF_PSEL(UART_CTS, 0, 7)>;
    			bias-pull-up;
    		};
    	};
    
    	uart0_sleep: uart0_sleep {
    		group1 {
    			psels = <NRF_PSEL(UART_TX, 0, 6)>,
    				<NRF_PSEL(UART_RX, 0, 8)>,
    				<NRF_PSEL(UART_RTS, 0, 5)>,
    				<NRF_PSEL(UART_CTS, 0, 7)>;
    			low-power-enable;
    		};
    	};
    
    	uart1_default: uart1_default {
    		group1 {
    			psels = <NRF_PSEL(UART_RX, 1, 1)>;
    			bias-pull-up;
    		};
    		group2 {
    			psels = <NRF_PSEL(UART_TX, 1, 2)>;
    		};
    	};
    
    	uart1_sleep: uart1_sleep {
    		group1 {
    			psels = <NRF_PSEL(UART_RX, 1, 1)>,
    				<NRF_PSEL(UART_TX, 1, 2)>;
    			low-power-enable;
    		};
    	};
    ...
    };


    In the same echo_bot sample, there is a file called nrf52840dk_nrf52840.dts with UART configuration shown below.

    &uart0 {
    	compatible = "nordic,nrf-uarte";
    	status = "okay";
    	current-speed = <115200>;
    	pinctrl-0 = <&uart0_default>;
    	pinctrl-1 = <&uart0_sleep>;
    	pinctrl-names = "default", "sleep";
    };


    When building for nRF52840-DK board, both above-mentioned files are taken from <your_ncs_installation_folder>\zephyr\boards\arm\nrf52840dk_nrf52840 folder.

    You can read more about devicetree and pin control in the documentation.

    Best regards,
    Dejan


Related