Sending characters via uart1

Hi guys,

I want to send 'a' characters via the uart1 of the nRF52840-DK. I cut the short on the SB6 jumper to disconnect LED 2 from P0.14, which also happens to be TX of uart1.

#include <zephyr/kernel.h>
#include <zephyr/zephyr.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/uart.h>

void main(void)
{
	struct device *gpio0_dev = DEVICE_DT_GET(DT_NODELABEL(gpio0));
	gpio_pin_configure(gpio0_dev, 14, GPIO_OUTPUT);

	struct device *uart_dev = DEVICE_DT_GET(DT_NODELABEL(uart1));
	while (true) {
		uart_poll_out(uart_dev, 'a');
	}
}

According to my protocol analyzer, P0.14 is low all the time.

Thanks for your help in advance!

Parents
  • Hi 

    Have you made a device tree overlay to reassign P0.14 from the LED to the uart1 TX pin? 

    If so, could you share your overlay file with me so I can see if it looks OK?

    It shouldn't be necessary to manually configure P0.14 to an output if you are planning to use it for the UART.

    Rather you should remove the "led1" node from your device tree, and ensure that the uart1 TX pin is changed to 14. 

    Also, to check for possible configuration errors you should check the value of uart_dev after you use DEVICE_DT_GET(..). If uart_dev == 0 it means that something went wrong in the DEVICE_DT_GET(..) call, and at this point you have not been able to get a valid device. 

    Best regards
    Torbjørn

  • Thanks for your quick reply, Torbjørn!

    Given that uart_dev is nonzero, I assumed that uart1 should work, and I haven't changed the device tree.

    I can see that in nrf52840.dtsi, the status of uart1 is disabled. Now, I removed the references to led1 in nrf52840dk_nrf52840.dts and created the following device tree overlay, but uart1 still doesn't transmit anything.

    &uart1 {
        status = "okay";
    };

    As far as I know, uart1 should use pin 14 for TX by default, so I don't understand why it should be explicitly defined. The uart1 definition, however, doesn't mention pin 14 explicitly in nrf52840.dtsi:

    		uart1: uart@40028000 {
    			compatible = "nordic,nrf-uarte";
    			reg = <0x40028000 0x1000>;
    			interrupts = <40 NRF_DEFAULT_IRQ_PRIORITY>;
    			status = "disabled";
    		};

    Any further ideas?

Reply
  • Thanks for your quick reply, Torbjørn!

    Given that uart_dev is nonzero, I assumed that uart1 should work, and I haven't changed the device tree.

    I can see that in nrf52840.dtsi, the status of uart1 is disabled. Now, I removed the references to led1 in nrf52840dk_nrf52840.dts and created the following device tree overlay, but uart1 still doesn't transmit anything.

    &uart1 {
        status = "okay";
    };

    As far as I know, uart1 should use pin 14 for TX by default, so I don't understand why it should be explicitly defined. The uart1 definition, however, doesn't mention pin 14 explicitly in nrf52840.dtsi:

    		uart1: uart@40028000 {
    			compatible = "nordic,nrf-uarte";
    			reg = <0x40028000 0x1000>;
    			interrupts = <40 NRF_DEFAULT_IRQ_PRIORITY>;
    			status = "disabled";
    		};

    Any further ideas?

Children
  • Hi 

    Which version of the nRF Connect SDK are you using?

    I checked in v2.1.2, and I can't find any reference to uart1 using P0.14 by default. This would cause a conflict since P0.14 is already used as a LED. 

    The UARTE peripheral defaults to all pins being disconnected, and there is no standard configuration for UART1 printed on the nRF52840DK, so I am not sure where you read about the P0.14 assignment?

    Rather, if you look at the nrf52840dk_nrf52840-pinctrl.dtsi you can see that the uart1 pinctrl configuration uses P1.02 for TX and P1.01 for RX. 

    And while it is true that uart1 is disabled in the nrf52840.dtsi file this gets overwritten by the nrf52840dk_nrf52840.dts file, where the status of uart1 is set to "ok". 
    As you can see by the "arduino_serial" nodelabel uart1 is connected to the standard UART pins on the Arduino headers by default, in case you hook up an Arduino shield that needs UART. By using uart1 for this you can still use uart0 for logging, console etc. 

    Changing the board files, such as nrf52840dk_nrf52840.dts, is not recommended. Rather you should use a device tree overlay, or copy all the board files and make your own board. 

    Using an overlay is by far the easiest, and an overlay file that removes the led1 node and overwrites the default pinctrl pin assignments for uart1 to move the TX pin to P0.14 would look like this:

    /{
    	/delete-node/ led1;
    };
    
    &pinctrl {
    	uart1_default: uart1_default {
    		group1 {
    			psels = <NRF_PSEL(UART_RX, 1, 1)>;
    			bias-pull-up;
    		};
    		group2 {
    			psels = <NRF_PSEL(UART_TX, 0, 14)>;
    		};
    	};
    
    	uart1_sleep: uart1_sleep {
    		group1 {
    			psels = <NRF_PSEL(UART_RX, 1, 1)>,
    				<NRF_PSEL(UART_TX, 0, 14)>;
    			low-power-enable;
    		};
    	};
    };

    Could you try to apply this overlay to your project and see if it works better?
    Just make sure to reverse any changes you made to the original board files. 

    Best regards
    Torbjørn

  • Hi Torbjørn,

    It turns out that I was mistaken about UART pin controls. I come from other microcontrollers where there are one or two UARTs with fixed pins, but now I can see that the UARTs of nRF ICs can be mapped to any GPIO. In a forum discussion, I read that someone wanted to map uart1:tx to pin 14, and I assumed that it was the only possible pin for this function.

    Thanks for the overlay file! uart1 works with it as expected, and now I have a better idea of pin mappings.

    (I used the nRF Connect SDK version 2.1.0 and upgraded to 2.1.2 in the meantime.)

    Thanks for everything! Your support is great, and I greatly appreciate it.

    - Laci

  • Hi Laci

    I have worked with Nordic for such a long time now I forget that this is not a feature in all devices Wink

    Good to hear that you got it working, and thanks for the kind words. The best of luck with your project Slight smile

    Best regards
    Torbjørn

Related