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

Problem reading buffer in different functions

Hello!
me and a coworker are developing a library for communicating with a peripheral via UART. The main idea is that when we send a command, we should get the corresponding to acknowledge and, if needed, more data.

We are using Zephyr, and using the UART callback function to read the incoming data. After testing it, we have found that that ISR is reading one byte every time it is called, and then clearing the UART buffer. We want to store the data received after each command, and for that, we have declared a static array which is used as a buffer and a pointer to the next empty position in that buffer. Said buffer and pointer are declared in the header file of the library, and they are used by both the library and the ISR in the main file.

// Callback function in main.c

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;
	}

    // Considering it reads just one character per time
    // (consideration done after testing it)
	uint8_t read_char = uart_buf[0];
	read_buf[read_buf_ptr] = read_char;
    read_buf_ptr++;
	printk("%c", read_char);
}

// One of the functions in the library

int adafruit_VC0706_read_picture_length(struct device *uart){
    uint8_t msg[] = {header_send, serial_num, VC0706_GET_FBUF_LEN, 0x01, 0x00};
    int err = send_command(uart,msg,9);
    if (err < 0)
        return err;
    return ((read_buf[7] << 8) | read_buf[8]);
}

// Buffer initialized in the library's header file
static uint8_t read_buf[65536];
static uint32_t read_buf_ptr = 0;

In the main file (not only in the main function) everything works fine. However, using that buffer in the library does nothing. It always reads "0", and writing changes nothing (e.g., we cannot reset the pointer). Our supposition is that the OS is doing some kind of memory protection and, even though the buffer is initialized inside the library, it only allows functions in main.c to use it. We tried with other keywords such as global, but nothing worked (it did not even build). What could we do to access it from functions in both files?

PS: up to now, we are not using any explicit kernel calls or similar, we are just calling different functions inside main.

Parents Reply Children
Related