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

nRF9160 DK send/receive data from UART1

Dear, 

I am using nRF9160 DK, 

I can re-define pin for UART0 and UART1, from UART0, i can receive data from UART0/UART1. 

#include <zephyr.h>
#include <misc/printk.h>
#include <uart.h>

static u8_t uart_buf[1024];

void uart_cb(struct device *x)
{
	uart_irq_update(x);
	int data_length = 0;

	if (uart_irq_rx_ready(x)) {
		data_length = uart_fifo_read(x, uart_buf, sizeof(uart_buf));
		uart_buf[data_length] = 0;
	}
	printk("%s", uart_buf);
}

void main(void)
{
	struct device *uart0 = device_get_binding("UART_0");
        struct device *uart1 = device_get_binding("UART_1");

	uart_irq_callback_set(uart0, uart_cb);
	uart_irq_rx_enable(uart0);
    uart_irq_callback_set(uart1, uart_cb);
	uart_irq_rx_enable(uart1);
	printk("UART loopback start!\n");
	while (1) {
		k_cpu_idle();
                
	}
}

Now, I want to send data to UART1.

Plz help me

Regards,

Thanks

Parents
  • Hi Hng,
    I am sorry you feel that way, it is my fault for misunderstanding. 

    It would be fantastic if you could share with the community the solution to your issue (?) Smile

    Best regards, 

    Martin L.

  • Hi 

    I just tried:

    void uart_sendCOM(struct device *x,  u8_t *Cont)
    {
    	       
             u16_t len = strlen(Cont); 
             uart_fifo_fill(x, Cont,len );
             uart_irq_tx_enable(x);
             
        
    }
    void uart_cb(struct device *x)
    {
    	uart_irq_update(x);
    	int data_length = 0;
    
    	if (uart_irq_rx_ready(x)) {
                    data_length = uart_fifo_read(x, uart_buf, sizeof(uart_buf));
    		uart_buf[data_length] = 0;
    	}
    	printk("%s", uart_buf);
    
           	if (uart_irq_tx_ready(x)) {
                   
            	struct uart_data_t *buf =
    			k_fifo_get(&fifo_uart_tx_data, K_NO_WAIT);
    		u16_t written = 0;
    
    		/* Nothing in the FIFO, nothing to send */
    		if (!buf) {
    			uart_irq_tx_disable(x);
    			return;
    		}
    
    		while (buf->len > written) {
    			written += uart_fifo_fill(x,
    						  &buf->data[written],
    						  buf->len - written);
    		}
    
    		while (!uart_irq_tx_complete(x)) {
    			/* Wait for the last byte to get
    			 * shifted out of the module
    			 */
    		}
    
    		if (k_fifo_is_empty(&fifo_uart_tx_data)) {
    			uart_irq_tx_disable(x);
    		}
    
    		k_free(buf);
    	
    		
    	}
    
           
    }
    
    void main(void)
    {
    	
            struct device *uart1 = device_get_binding("UART_1");
           
            uart_irq_rx_enable(uart1);
            uart_irq_callback_set(uart1, uart_cb);
            uart_sendCOM(uart1, "Hello the world!");
            k_sleep(3000);
            
    }
            

    Hope it is correct!

    Thanks

    Regards, 

  • HI hoang,
    I'm practically trying to do the same thing.

    I did not understand what is this fifo fifo_uart_tx_data,

    did you create it ? (trying this code i get fifo_uart_tx_data undeclared error)

Reply Children
Related