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:
#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);
char dest[strlen(uart_buf)];
// strncpy(dest,uart_buf,strlen(uart_buf));
strcpy(dest, uart_buf);
printk("%s", dest);
const char ch = ',';
char *ret;
ret = strchr(uart_buf, ch);
printk("%s", ret);
}
void main(void)
{
struct device *uart = device_get_binding("UART_1");
uart_irq_callback_set(uart, uart_cb);
uart_irq_rx_enable(uart);
printk("UART loopback start!\n");
while (1) {
k_cpu_idle();
}
}
Output:
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...