My rx_fifo buffer shows only 1 byte, but the other side is sending more. Am I using the APIs correctly?
#include "uart.h"
#define UART_RX_FIFO_SIZE 512
#define UART_RX_BLOCK_SIZE 1
static uint8_t rx_fifo[UART_RX_FIFO_SIZE];
static volatile uint16_t head = 0;
static volatile uint16_t tail = 0;
static void (*user_rx_callback)(void) = NULL;
static inline bool uart_buffer_full(void){
uint16_t next_head = (head + UART_RX_BLOCK_SIZE) % UART_RX_FIFO_SIZE;
return (next_head == tail);
}
static inline bool uart_buffer_empty(void){
return head == tail;
}
bool uart_read_byte(uint8_t *byte){
if (uart_buffer_empty()) {
return false;
}
*byte = rx_fifo[tail];
tail = (tail + 1) % UART_RX_FIFO_SIZE;
return true;
}
bool uart_init(void){
status_t status = UART_Init(&uart_pal1_instance, &uart_pal1_Config0);
if (status == STATUS_SUCCESS) {
status = UART_ReceiveData(&uart_pal1_instance, &rx_fifo[head], UART_RX_BLOCK_SIZE);
if (status == STATUS_SUCCESS) {
return true;
}
}
return false;
}
bool uart_send_data(const uint8_t *data, uint32_t length){
if (data == NULL || length == 0) {
return false;
}
status_t status = UART_SendData(&uart_pal1_instance, data, length);
return (status == STATUS_SUCCESS);
}
bool uart_receive_data(uint8_t *data, uint32_t length){
if (data == NULL || length == 0) {
return false;
}
uint32_t i;
for (i = 0; i < length; i++) {
if (!uart_read_byte(&data[i])) {
break;
}
}
return (i > 0);
}
void uart_register_rx_callback(void (*callback)(void)){
user_rx_callback = callback;
}
void uart0_rx_callback(void *driverState, uart_event_t event, void *userData){
(void)driverState;
(void)userData;
switch(event) {
case UART_EVENT_RX_FULL: {
uint16_t next_head = (head + UART_RX_BLOCK_SIZE) % UART_RX_FIFO_SIZE;
UART_SetRxBuffer(&uart_pal1_instance, &rx_fifo[next_head], UART_RX_BLOCK_SIZE);
head = next_head;
if (head == tail) {
tail = (tail + UART_RX_BLOCK_SIZE) % UART_RX_FIFO_SIZE;
}
if (user_rx_callback != NULL) {
user_rx_callback();
}
}
break;
case UART_EVENT_END_TRANSFER:
break;
case UART_EVENT_ERROR:
head = tail = 0;
UART_SetRxBuffer(&uart_pal1_instance, &rx_fifo[head], UART_RX_BLOCK_SIZE);
break;
default:
break;
}
}
Hello @shishir-dey
While reviewing your code, I noticed that in the functions "UART_SetBuffer(&uart_pal1_instance, &rx_fifo[next_head], UART_RX_BLOCK_SIZE);" and "UART_ReceiveData(&uart_pal1_instance, &rx_fifo[head], UART_RX_BLOCK_SIZE);" you are passing as the length of the buffer "UART_RX_BLOCK_SIZE" which is defined as 1, as a result, your receiving buffer is always configured to receive 1 byte only, to receive more data you should increase the value of UART_RX_BLOCK_SIZE to match the expected payload size.
Please let me know if this information was helpful to solve your issue.
- RomanVR.
Hi @shishir-dey.
Could you please share your project so I can further analyze the issue? Additionally, please refer to the following community post where an RX Overrun issue was solved by moving the "SendData" function into the RxCallback function: uart_pal_s32k144w status = STATUS_UART_RX_OVERRUN.
Let me know if the solution works for you and I will be waiting for your answer.
- RomanVR.