UART RTOS Receive status kStatus_UART_RxHardwareOverrun

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

UART RTOS Receive status kStatus_UART_RxHardwareOverrun

9,212 Views
higor
Contributor II

My English is not very good.

I’m new with freertos, I’m having a hard time solving the following situation:
The UART_RTOS_Receive function is returning the status kStatus_UART_RxHardwareOverrun I am not able to solve it, I created a modbus code to communicate with the computer.

It could help/assist me. I'll leave my code below:

 

///*
// * modbus_freertos.c
// *
// * Created on: 26 de dez de 2022
// */
//
#include "modbus_freertos.h"
//
#define UART_MODBUS_RX_BUFFER_SIZE 256
#define UART_MODBUS_TX_BUFFER_SIZE 260


typedef enum{
read_multiple_register = 3,
write_single_register = 6,
write_multiple_register = 16
}function_modbus_t;

typedef struct {
uint8_t rx_buffer[UART_MODBUS_RX_BUFFER_SIZE];
uint8_t tx_buffer[UART_MODBUS_TX_BUFFER_SIZE];
size_t rx_nBytes;
size_t tx_nBytes;
}modbus_t;

modbus_t modbus;

uint8_t get_size_request(uint8_t* buffer);
unsigned short CRC16Table256(int i);
unsigned short update_crc_16( unsigned short crc, char c );
unsigned short CRC16(unsigned char *ptr,int npts);
void CopySwapBuf(unsigned char *copy_buffer, unsigned char *paste_buffer, uint8_t init_buffer, uint8_t size);
status_t compare_crc(uint16_t crc_calculate, uint16_t crc_receive_high, uint16_t crc_receive_low);
void get_multiple_registers(uint8_t* buffer);
void set_single_register(uint8_t* buffer);
void set_multiple_register(uint8_t* buffer);


/* ------------------------------------ Calcular CRC ------------------------------------------*/
unsigned short CRC16Table256(int i){
unsigned short crc,c,j;
crc = 0;
c = (unsigned short) i;
for (j=0; j<8; j++) {
if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ 0xA001;
else crc = crc >> 1;
c = c >> 1;
}
return crc;
}

unsigned short update_crc_16(unsigned short crc, char c ) {
unsigned short tmp, short_c;
short_c = 0x00ff & (unsigned short) c;
tmp = crc ^ short_c;
crc = (crc >> ^ CRC16Table256((int)tmp & 0xff);
return crc;
}

unsigned short CRC16(unsigned char *ptr,int npts){
unsigned short crc;
int i;
crc=0xffff; // Valor inicial para CRC16modbus
for(i=0;i<npts;i++) {
crc=update_crc_16(crc,(char) *(ptr+i));
}
return crc;
}

status_t compare_crc(uint16_t crc_calculate, uint16_t crc_receive_high, uint16_t crc_receive_low){
if (((crc_calculate & 0x00FF) == crc_receive_high) && ((crc_calculate >> == crc_receive_low)) return kStatus_Success;
return kStatus_Fail;
}

/* ----------------------------------- Copia um buffer para outro ----------------------------------*/
void CopySwapBuf(unsigned char *copy_buffer, unsigned char *paste_buffer, uint8_t init_buffer, uint8_t size){
uint8_t inc_pst = init_buffer;
for(uint8_t inc_cpy = 0; inc_cpy < size; inc_cpy++){
paste_buffer[inc_pst] = copy_buffer[inc_cpy];
inc_pst++;
}
}

/* ----------------------------------- Tarefa modbus ------------ ----------------------------------*/
void modbus_task(void *param){
status_t status;
uint16_t crc;
uint8_t rxsize;
uint8_t process_buffer[UART_MODBUS_RX_BUFFER_SIZE]; 
while(1){
memset(process_buffer, 0, UART_MODBUS_RX_BUFFER_SIZE);
memset(modbus.rx_buffer, 0, UART_MODBUS_RX_BUFFER_SIZE);
rxsize = 2;
status = UART_RTOS_Receive(&UART_MODBUS_rtos_handle, modbus.rx_buffer, rxsize, &modbus.rx_nBytes);
if(status == kStatus_Success)
{
if( xSemaphoreTake( UART_MODBUS_rtos_handle.rxSemaphore, ( TickType_t ) 3 ) == pdTRUE ) //Tenta pegar o semáforo
{
CopySwapBuf((uint8_t *)&modbus.rx_buffer, (uint8_t *)&process_buffer, 0, rxsize); 
xSemaphoreGive(UART_MODBUS_rtos_handle.rxSemaphore);
}
else add_log(ModbusError);
rxsize = get_size_request(process_buffer); 
status = UART_RTOS_Receive(&UART_MODBUS_rtos_handle, modbus.rx_buffer, rxsize, &modbus.rx_nBytes);
if (status == kStatus_Success)
{
if( xSemaphoreTake( UART_MODBUS_rtos_handle.rxSemaphore, ( TickType_t ) 3 ) == pdTRUE ) 
{
CopySwapBuf((uint8_t *)&modbus.rx_buffer, (uint8_t *)&process_buffer, 2, rxsize); /
xSemaphoreGive(UART_MODBUS_rtos_handle.rxSemaphore);
}
else add_log(ModbusError);
if(process_buffer[0] == get_par_int16(iMB_ADDRESS)) 
{
crc = CRC16(process_buffer, rxsize); 
if (compare_crc(crc, process_buffer[rxsize], process_buffer[rxsize + 1]) == kStatus_Success) 
{
switch(process_buffer[1])
{
case read_multiple_register: get_multiple_registers(process_buffer);
break;
case write_single_register: set_single_register(process_buffer);
break;
case write_multiple_register: set_multiple_register(process_buffer);
break;
default: break;
}
}
}
}
}
}
}

uint8_t get_size_request(uint8_t* buffer){
uint8_t size = 0;
if(buffer[1] == 3) size = (7 + 1 - 2);
if(buffer[1] == 6) size = (7 + 1 - 2);
if(buffer[1] == 16) size = ((buffer[5] * 2) + 9 - 2);
return size;
}

/* -------------------------------------------- Funções modbus --------------------------------------------*/
void get_multiple_registers(uint8_t* buffer){
int cont_tx = 3;
uint16_t crc;
uint16_t addr_reg = ((buffer[2] << | buffer[3]);
uint16_t size_buffer = ((buffer[4] << | buffer[5]);

if ((addr_reg + size_buffer) < (SIZE_READ+SIZE_CFG+SIZE_CALIB)){ 
crc = 0;
modbus.tx_buffer[0] = buffer[0];
modbus.tx_buffer[1] = read_multiple_register; 
modbus.tx_buffer[2] = (size_buffer * 2); 

for (int i = addr_reg; i < size_buffer; i++) { 
modbus.tx_buffer[cont_tx] = (get_par_uint16_modbus(i) >> 8);
cont_tx++;
modbus.tx_buffer[cont_tx] = (get_par_uint16_modbus(i) & 0xff);
cont_tx++;
}
crc = CRC16(modbus.tx_buffer, cont_tx); 
modbus.tx_buffer[cont_tx++] = (crc & 0xff);
modbus.tx_buffer[cont_tx++] = (crc >> 8);
modbus.tx_nBytes = cont_tx;
UART_RTOS_Send(&UART_MODBUS_rtos_handle, modbus.tx_buffer, modbus.tx_nBytes);
}
}

void set_single_register(uint8_t* buffer){
uint16_t sinc_status;
uint16_t reg;
uint16_t value;
uint16_t crc;

sinc_status = get_par_int16(iSTATUS_SINC); 
reg = buffer[2] << 8; 
reg = reg + buffer[3];
value = buffer[4] << 8;
value = value + buffer[5];

if (sinc_status == Iddle || sinc_status == Testing) 
{
if(reg > SIZE_READ) 
set_par_uint16_modbus(reg, value);
}
if ((reg - SIZE_HEADER) == (iRECEIVE_CMD_MB - OFFSET_READ)) /
set_par_int16(iRECEIVE_CMD_MB, value);

/* Prepara para devolver a informação da modbus */
modbus.tx_buffer[0] = buffer[0];
modbus.tx_buffer[1] = buffer[1];
modbus.tx_buffer[2] = buffer[2];
modbus.tx_buffer[3] = buffer[3];
modbus.tx_buffer[4] = buffer[4];
modbus.tx_buffer[5] = buffer[5];
crc = CRC16(modbus.tx_buffer, 6); 
modbus.tx_buffer[6] = (crc & 0x00ff); 
modbus.tx_buffer[7] = (crc >> 8); 
modbus.tx_nBytes = 8;
UART_RTOS_Send(&UART_MODBUS_rtos_handle, modbus.tx_buffer, modbus.tx_nBytes);
}

void set_multiple_register(uint8_t* buffer){

}

0 Replies