This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to communicate an external module through UART in nRF9160DK?

I want to let nRF9160DK communicate with an external module through UART in nRF9160DK

Then I need to assign rx and tx on two pins.

I will use UART0 for printk function so P0.26, 27, 28, and 29 are reserved.

I read page18 in nRF91_DK_User_Guide_v07.pdf and realized that all the pins are already connected to some modules in the DK.

Is it possible to assign some pins as UART tx and rx and connect to an external module? And if possibel, how can yo do it?

Thank you in advance.

Parents
  • Hi,

     

    Yes, you can override the UART0 pinout using a .overlay file, as explained here:

    https://devzone.nordicsemi.com/f/nordic-q-a/44114/continuous-wave-for-nrf9160/174022#174022

     

    Note that this .overlay file must be done in both the secure_boot and the non-secure application.

     

    Kind regards,

    Håkon

  • Thank you for your reply.

    I want to use UARTs other than UART0, because UART0 is used for printk func. So how do you let nRF9160 communicate with an external module through UART1, or 2?

  • your advice is helpful.

    >Have you connected another UART to the GPIOs?

    Yes.

    Here is my code. very simple. Any advice is helpful for me.

    <main.c>
    #include <nrf9160.h>
    #include <zephyr.h>
    #include <misc/printk.h>
    #include <uart.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    static u8_t uart_buf[1024];
    static struct device *uart_wifi;
    
    void uart_cb(struct device *_uart)
    {
    	uart_irq_update(_uart); // Start processing interrupts in ISR.
    	int data_length = 0;
    
    	if (uart_irq_rx_ready(_uart)) { // Check if UART RX buffer has a received char.
    		data_length = uart_fifo_read(_uart, uart_buf, sizeof(uart_buf)); // Read data from FIFO.
    		uart_buf[data_length] = 0;
    	}
    	printk("%s\r\n", uart_buf);
    }
    
    void uart_send_str(struct device *uart, char *str){
        printk("callback!\r\n");
        u32_t len = strlen(str);
        while (len--) {
            uart_poll_out(uart, *str++);
        }
    }
    
    void main(void)
    {
            char *command = "AT,test\r\n";
            printk("Hello, World!\r\n");
    
            uart_wifi = device_get_binding("UART_2"); // "UART_1" works fine. "UART_2" does not work out.
            if (!uart_wifi) {
    		printk("error\r\n");
    	}
            uart_irq_rx_disable(uart_wifi);
            uart_irq_callback_set(uart_wifi, uart_cb); // Set UART interrupt callback
            uart_irq_rx_enable(uart_wifi);
    
    	while (1) {
                    printk("loop head\r\n");
                    uart_send_str(uart_wifi, command);
                    printk("enter sleep\r\n");
                    __WFI();
    	}
    }
    

    <prj.conf>
    
    CONFIG_SERIAL=y
    CONFIG_TRUSTED_EXECUTION_NONSECURE=y
    CONFIG_UART_INTERRUPT_DRIVEN=y
    CONFIG_UART_1_NRF_UARTE=y
    CONFIG_UART_2_NRF_UARTE=y
    CONFIG_BSD_LIBRARY_TRACE_ENABLED=n
    

    <nrf9160_pca10090.dts>
    
    /dts-v1/;
    #include <nordic/nrf9160_xxaa.dtsi>
    
    / {
    	model = "Nordic PCA10090 Dev Kit";
    	compatible = "nordic,pca10090-dk", "nordic,nrf9160-xxaa";
    
    	chosen {
    		zephyr,console = &uart0;
                    zephyr,debug = &uart1;
                    zephyr,uart-wifi = &uart2;
    		zephyr,sram = &sram0;
    		zephyr,flash = &flash0;
    	};
    
    	aliases {
    		led0 = &led0;
    		led1 = &led1;
    		led2 = &led2;
    		led3 = &led3;
    		sw0 = &button2;
    		sw1 = &button3;
    		sw2 = &button0;
    		sw3 = &button1;
    	};
    
    	leds {
    		compatible = "gpio-leds";
    		led0: led_0 {
    			gpios = <&gpio0 2 GPIO_INT_ACTIVE_LOW>;
    			label = "Green LED 0";
    		};
    		led1: led_1 {
    			gpios = <&gpio0 3 GPIO_INT_ACTIVE_LOW>;
    			label = "Green LED 1";
    		};
    		led2: led_2 {
    			gpios = <&gpio0 4 GPIO_INT_ACTIVE_LOW>;
    			label = "Green LED 2";
    		};
    		led3: led_3 {
    			gpios = <&gpio0 5 GPIO_INT_ACTIVE_LOW>;
    			label = "Green LED 3";
    		};
    	};
    
    	buttons {
    		compatible = "gpio-keys";
    		button0: button_0 {
    			gpios = <&gpio0 8 GPIO_PUD_PULL_UP>;
    			label = "Switch 1";
    		};
    		button1: button_1 {
    			gpios = <&gpio0 9 GPIO_PUD_PULL_UP>;
    			label = "Switch 2";
    		};
    		button2: button_2 {
    			gpios = <&gpio0 6 GPIO_PUD_PULL_UP>;
    			label = "Push button 1";
    		};
    		button3: button_3 {
    			gpios = <&gpio0 7 GPIO_PUD_PULL_UP>;
    			label = "Push button 2";
    		};
    	};
    
    	sram0_bsd: memory@20010000 {
    		device_type = "memory";
    		compatible = "mmio-sram";
    	};
    };
    
    &uart0 {
    	status = "ok";
    	current-speed = <115200>;
    	tx-pin = <29>;
    	rx-pin = <28>;
    	rts-pin = <27>;
    	cts-pin = <26>;
    };
    
    &uart1 {
    	status = "ok";
    	current-speed = <115200>;
    	tx-pin = <1>;
    	rx-pin = <0>;
    	rts-pin = <14>;
    	cts-pin = <15>;
    };
    
    &uart2 {
    	status = "ok";
    	current-speed = <115200>;
    	tx-pin = <10>;
    	rx-pin = <11>;
    };
    
    ...

  • nRF9160 UART logic level is originally 1.8V. I thought 3.3V. 

    Anyway, I disconnected P10(Tx) and saw the waveform, but always 1.8V. No signal. I have no idea of why UART signal does not come...

  • I thought UART_2 module or GPIO might be broken as I input higher voltage(1.8v vs 3.3v), but it doesn't seem to be broken.

    UART_1 works fine. So I replaced UART_1 with UART_2 with the same pin assignment. It works.

    <.dts file>
    
    zephyr,console = &uart2;
    
    &uart2 {
    	status = "ok";
    	current-speed = <115200>;
    	tx-pin = <1>;
    	rx-pin = <0>;
    	rts-pin = <14>;
    	cts-pin = <15>;
    };

    Also, I toggled the voltage in P10~20. all the pins but P17,18,19 work fine. I'm using P10 for Tx and P11 for Rx so that's fine.

    #include <nrf9160.h>
    #include <zephyr.h>
    #include <misc/printk.h>
    #include <uart.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include <gpio.h>
    
    struct device *gpio_test;
    int cnt = 0;
    
    void main(void)
    {
            printk("Hello, World!\r\n");
            gpio_test = device_get_binding("GPIO_0");
            if (!gpio_test) {
    		printk("error gpio_test\r\n");
    	}
            gpio_pin_configure(gpio_test, 10, GPIO_DIR_OUT);
            gpio_pin_configure(gpio_test, 11, GPIO_DIR_OUT);
            gpio_pin_configure(gpio_test, 12, GPIO_DIR_OUT);
            gpio_pin_configure(gpio_test, 13, GPIO_DIR_OUT);
            gpio_pin_configure(gpio_test, 16, GPIO_DIR_OUT);
            gpio_pin_configure(gpio_test, 17, GPIO_DIR_OUT);
            gpio_pin_configure(gpio_test, 18, GPIO_DIR_OUT);
            gpio_pin_configure(gpio_test, 19, GPIO_DIR_OUT);
            gpio_pin_configure(gpio_test, 20, GPIO_DIR_OUT);
    
            while(1){
                    printk("cnt = %i\r\n", cnt);
                    gpio_pin_write(gpio_test, 10, cnt % 2);
                    gpio_pin_write(gpio_test, 11, cnt % 2);
                    gpio_pin_write(gpio_test, 12, cnt % 2);
                    gpio_pin_write(gpio_test, 13, cnt % 2);
                    gpio_pin_write(gpio_test, 16, cnt % 2);
                    gpio_pin_write(gpio_test, 17, cnt % 2);
                    gpio_pin_write(gpio_test, 18, cnt % 2); // aloways undefined
                    gpio_pin_write(gpio_test, 19, cnt % 2); // always high
                    gpio_pin_write(gpio_test, 20, cnt % 2); // always low
                    cnt++;
                    k_sleep(1000);
            }
    }

    Tx is always H and releases no signal. I really appreciate it if you give me any advice.

  • Did you manage to get uart2 working?  I need an external uart from the Arduino header while leaving the USB uarts alone.

Reply Children
No Data
Related