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

Communication between nrf52840 and nrf9160 on thingy 91

Hei

I am trying to make SPI or UART communication between nrf9160 and nrf52840 on thingy 91....... i need to send data from nrf52840 to nrf9160(i will use and print the data)
what is the best way to do it? I tried connectivity bridge, but with no hope......


Another small question....i cannot get any thing out on putty when i write printk("hello world"); in the main function in connectivity bridge code ...how to fix it?

  • Thanks for your answer 
    now i dont get the same feil when i use uart0_dev = device_get_binding("UART_0");

    but i get another feil (Failed to set callback) 


    the code is:

    static void async(const struct device *uart0_dev)
    {
    uint8_t txbuf[5] = {1, 2, 3, 4, 5};
    int err;
    uint8_t *buf;

    err = k_mem_slab_alloc(&uart_slab, (void **)&buf, K_NO_WAIT);
    __ASSERT(err == 0, "Failed to alloc slab");

    err = uart_callback_set(uart0_dev, uart_callback, (void *)uart0_dev);
    __ASSERT(err == 0, "Failed to set callback");  

    err = uart_rx_enable(uart0_dev, buf, BUF_SIZE, 10);
    __ASSERT(err == 0, "Failed to enable RX");

    while (1) {
    err = uart_tx(uart0_dev, txbuf, sizeof(txbuf), 10);
    __ASSERT(err == 0, "Failed to initiate transmission");

    k_sleep(K_MSEC(500));

    uart_poll_out(uart0_dev, txbuf[0]);
    k_sleep(K_MSEC(100));
    }
    }


    main function:

    void main(void)
    {
    LOG_INF("Asset tracker started");


    const struct device *uart0_dev;

    k_msleep(1000);
    uart0_dev = device_get_binding("UART_0");
    //lpuart = device_get_binding("LPUART");
    __ASSERT(uart0_dev, "Failed to get the device");

    if (IS_ENABLED(CONFIG_NRF_SW_LPUART_INT_DRIVEN)) {
    interrupt_driven(uart0_dev);
    } else {
    async(uart0_dev);
    }


    I think the feil is from my overlay file:

    overlay file:

    &uart0 {
    compatible = "nordic,nrf-uarte";
    status = "okay";
    rx-pin = <19>;
    tx-pin = <20>;
    baudrate = <1000000>;
    /delete-property/ rts-pin;
    /delete-property/ cts-pin;
    /delete-property/ hw-flow-control;
    };


    prj.conf:

    i added to the original prj file

    CONFIG_UART_ASYNC_API=y

    thanks for your help 

    Mohammad





  • Hamada888 said:
    but i get another feil (Failed to set callback) 

    Have you defined a callback function in main.c, as shown below from the LPUART sample?

    static void uart_callback(const struct device *dev,
    			  struct uart_event *evt,
    			  void *user_data)
    {
    	struct device *uart = user_data;
    	int err;
    
    	switch (evt->type) {
    	case UART_TX_DONE:
    		LOG_INF("Tx sent %d bytes", evt->data.tx.len);
    		break;
    
    	case UART_TX_ABORTED:
    		LOG_ERR("Tx aborted");
    		break;
    
    	case UART_RX_RDY:
    		LOG_INF("Received data %d bytes", evt->data.rx.len);
    		break;
    
    	case UART_RX_BUF_REQUEST:
    	{
    		uint8_t *buf;
    
    		err = k_mem_slab_alloc(&uart_slab, (void **)&buf, K_NO_WAIT);
    		__ASSERT(err == 0, "Failed to allocate slab");
    
    		err = uart_rx_buf_rsp(uart, buf, BUF_SIZE);
    		__ASSERT(err == 0, "Failed to provide new buffer");
    		break;
    	}
    
    	case UART_RX_BUF_RELEASED:
    		k_mem_slab_free(&uart_slab, (void **)&evt->data.rx_buf.buf);
    		break;
    
    	case UART_RX_DISABLED:
    		break;
    
    	case UART_RX_STOPPED:
    		break;
    	}
    }

    Cheers!

    Markus


  • #define BUF_SIZE 64
    static K_MEM_SLAB_DEFINE(uart_slab, BUF_SIZE, 3, 4);


    static void uart_irq_handler(const struct device *dev, void *context)
    {
    uint8_t buf[] = {1, 2, 3, 4, 5};

    if (uart_irq_tx_ready(dev)) {
    (void)uart_fifo_fill(dev, buf, sizeof(buf));
    uart_irq_tx_disable(dev);
    }

    if (uart_irq_rx_ready(dev)) {
    uint8_t buf[10];
    int len = uart_fifo_read(dev, buf, sizeof(buf));

    if (len) {
    printk("read %d bytes\n", len);
    }
    }
    }

    static void interrupt_driven(const struct device *dev)
    {
    uint8_t c = 0xff;

    uart_irq_callback_set(dev, uart_irq_handler);
    uart_irq_rx_enable(dev);
    while (1) {
    uart_irq_tx_enable(dev);
    k_sleep(K_MSEC(500));

    uart_poll_out(dev, c);
    k_sleep(K_MSEC(100));
    }
    }

    static void uart_callback(const struct device *dev,
    struct uart_event *evt,
    void *user_data)
    {
    struct device *uart = user_data;
    int err;

    switch (evt->type) {
    case UART_TX_DONE:
    LOG_INF("Tx sent %d bytes", evt->data.tx.len);
    break;

    case UART_TX_ABORTED:
    LOG_ERR("Tx aborted");
    break;

    case UART_RX_RDY:
    LOG_INF("Received data %d bytes", evt->data.rx.len);
    break;

    case UART_RX_BUF_REQUEST:
    {
    uint8_t *buf;

    err = k_mem_slab_alloc(&uart_slab, (void **)&buf, K_NO_WAIT);
    __ASSERT(err == 0, "Failed to allocate slab");

    err = uart_rx_buf_rsp(uart, buf, BUF_SIZE);
    __ASSERT(err == 0, "Failed to provide new buffer");
    break;
    }

    case UART_RX_BUF_RELEASED:
    k_mem_slab_free(&uart_slab, (void **)&evt->data.rx_buf.buf);
    break;

    case UART_RX_DISABLED:
    break;

    case UART_RX_STOPPED:
    break;
    }
    }


    static void async(const struct device *uart0_dev)
    {
    uint8_t txbuf[5] = {1, 2, 3, 4, 5};
    int err;
    uint8_t *buf;

    err = k_mem_slab_alloc(&uart_slab, (void **)&buf, K_NO_WAIT);
    __ASSERT(err == 0, "Failed to alloc slab");

    err = uart_callback_set(uart0_dev, uart_callback, (void *)uart0_dev);
    __ASSERT(err == 0, "Failed to set callback");

    err = uart_rx_enable(uart0_dev, buf, BUF_SIZE, 10);
    __ASSERT(err == 0, "Failed to enable RX");

    while (1) {
    err = uart_tx(uart0_dev, txbuf, sizeof(txbuf), 10);
    __ASSERT(err == 0, "Failed to initiate transmission");

    k_sleep(K_MSEC(500));

    uart_poll_out(uart0_dev, txbuf[0]);
    k_sleep(K_MSEC(100));
    }
    }

  • Thanks for you answer, Mohammad! I’m sorry for the delay, but we have a lot of cases right now. I will come back to you as soon as possible.

    Cheers!

    Markus

Related