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

UART on nrf9160dk

Hello,

I am using UART1 to communicate with sensor. It is working fine. I can receive the data. Now I want to modify this data. When I use strcpy() function, it works properly. But when I use functions like strchr(), there is error. I will paste the code and error as well.

To modify the received data from sensor, I am modifying the UART callback function. So is this the reason for the error and what should I do to overcome this problem?

Code:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <zephyr.h>
#include <misc/printk.h>
#include <uart.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.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);
// k_sleep(1000);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
UART loopback start!
$Exception occurred in Secure State
***** HARD FAULT *****
Fault escalation (see below)
***** BUS FAULT *****
Precise data bus error
BFAR Address: 0x50008120
***** Hardware exception *****
Current thread ID = 0x200200b0
Faulting instruction address = 0xe608
Fatal fault in ISR! Spinning...
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • The FIND_AND_NUL macro does not look very robust, and seems error prone.
    If the character is not found by strchr(), strchr() returns a null pointer. And then this macro starts to do pointer operation on a NULL pointer. Maybe you could create a function instead, that is able to handle strchr() returning NULL

  • Yes you were right. The hard fault was because of FIND_AND_NUL macro. I found the solution for that. Now it is working properly. I can separate all the data continuously. Thank you so much for your help.

1 2