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

Please help me modify this serial routine to resolve the problem of unstable data sending and receiving.

Use this routine to send and receive data from 4 different serials, receive and send data loss, please help me modify this sample bug.

sdk ncs v1.3
Path:D:\ncs\v1.3.0\nrf\samples\nrf9160\spm\prj.conf
Filename:prj.conf
Modify:CONFIG_SERIAL=n

Path:D:\ncs\v1.3.0\zephyr\boards\arm\nrf9160dk_nrf9160\nrf9160dk_nrf9160_common.dts
Filename:nrf9160dk_nrf9160_common.dts
Modify:Replace this dts file with the provided sample dtc file.    nrf9160dk_nrf9160_common.dts

Path:D:\ncs\v1.3.0\nrf\applications\21_uart_x4
Storage example:21_uart_x4

0160.4uart_example.rar

  • Hello,

    can you describe the problem you're experiencing? What do you mean by unstable data sending and receiving?

  • Hi HåkonHardy Lier,

    1. Have you ever tested your serial communication?
    2.Can you test the code I gave you?
    3. Is this kind of error easy to encounter?
    4. The most common errors are as follows:

    Kind regards. Peter.Min

  • In main.c you are using uart_buf for all the uart instances. Have you tried using a separate buffer for each of them?

  • Hi Håkon Hardy Lier,

    I try to use multiple bufs
    The test results are still consistent with the original phenomenon

    /*
    
    * Copyright (c) 2012-2014 Wind River Systems, Inc.
    
    *
    
    * SPDX-License-Identifier: Apache-2.0
    
    */
    #include <zephyr.h>
    #include <sys/printk.h>
    #include <drivers/uart.h>
    #include <string.h>
    #include <logging/log.h>
    LOG_MODULE_REGISTER(uart, 3);
    
    static u8_t uart_buf[4][256];
    struct device *uart0 = NULL;
    struct device *uart1 = NULL;
    struct device *uart2 = NULL;
    struct device *uart3 = NULL;
    
    void uart0_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[0], sizeof(uart_buf[0]));
            uart_buf[0][data_length] = 0;
            uart_fifo_fill( x, uart_buf[0], strlen(uart_buf[0]) );
        }
    }
    
    void uart1_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[1], sizeof(uart_buf[1]));
            uart_buf[1][data_length] = 0;
            uart_fifo_fill( x, uart_buf[1], strlen(uart_buf[1]) );
        }
    }
    
    void uart2_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[2], sizeof(uart_buf[2]));
            uart_buf[2][data_length] = 0;
            uart_fifo_fill( x, uart_buf[2], strlen(uart_buf[2]) );
        }
    }
    
    void uart3_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[3], sizeof(uart_buf[3]));
            uart_buf[3][data_length] = 0;
            uart_fifo_fill( x, uart_buf[3], strlen(uart_buf[3]) );
        }
    }
    
    void main(void)
    {
        uart0 = device_get_binding("UART_0");
        uart1 = device_get_binding("UART_1");
        uart2 = device_get_binding("UART_2");
        uart3 = device_get_binding("UART_3");
    
        uart_irq_callback_set(uart0, uart0_cb);
        uart_irq_rx_enable(uart0);
        
        uart_irq_callback_set(uart1, uart1_cb);
        uart_irq_rx_enable(uart1);
        
        uart_irq_callback_set(uart2, uart2_cb);
        uart_irq_rx_enable(uart2);
    
        uart_irq_callback_set(uart3, uart3_cb);
        uart_irq_rx_enable(uart3);
        
        LOG_INF("uart init");
        while (1) 
        {
            //k_cpu_idle();
            LOG_INF("Hello World!\n");
            k_sleep( Z_TIMEOUT_MS(8000) );
        }
    }
    

    Kind regards. Peter.Min

  • How do you test all four uarts at once? The nrf9160 has three VCOM ports, so how do you test the last uart?

Related