Page 1 of 1

UART SLOW READING

Posted: Wed Apr 28, 2021 6:55 am
by saigajul37
I am using esp-idf v3.3.
Esp32 clock frequency set to 240 MZ
In my code I have a serial task with priority 21 and stacksize of (1024*12) pinned on core zero, to read serial data from UART_NUM_1 using Pattern detect.
I receive approx 50 kb data in rx_buff in just 2 Sec in UART_NUM_1, as esp32 receives first pattern it start reading data
uart_read_bytes(UART_NUM_1, &dataSerial[0], sizeof(some_strct), 0);

Issue:
Data reading from rx_buffer is very slow. To read one data packet of approx size 23 bytes it takes approx 80 ms. i.e to read 2100 packets it takes 80 *2100 = 168 Sec approx . This is a very long delay and is not acceptable as I need real-time data.

Code: Select all

#define UART_NUM			(UART_NUM_1)
#define BAUD_RATE			(115200)
#define PATTERN_CHAR		'|'
#define UART_QUEUE_SIZE	(10000)
#define RX_BUF_SIZE			(100000)

void uart_Init()
{
	const uart_config_t uart_config = {
			.baud_rate	= BAUD_RATE,
			.data_bits	= UART_DATA_8_BITS,
			.parity		= UART_PARITY_DISABLE,
			.stop_bits	= UART_STOP_BITS_1,
			.flow_ctrl	= UART_HW_FLOWCTRL_DISABLE
	};

	uart_param_config(UART_NUM, &uart_config);

	uart_set_pin(UART_NUM, TX_PIN, RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

	esp_err_t ret = uart_driver_install(UART_NUM, RX_BUF_SIZE, TX_BUF_SIZE, UART_QUEUE_SIZE, &uartQueueHandle, 0);

	if (ret == ESP_OK) {

		//	Set UART pattern detect function.
		uart_enable_pattern_det_intr(UART_NUM, PATTERN_CHAR, PATTERN_CHR_NUM, 50000, 10, 10);

		//	Reset the pattern queue length to record at most 1024 pattern positions.
		ret = uart_pattern_queue_reset(UART_NUM,UART_QUEUE_SIZE);
		if (ret != ESP_OK) {
                        
		//ESP_LOGI(NIBLEWAY_INIT,"*uart_Init fail*");
		}
		//ESP_LOGI(NIBLEWAY_INIT,"*uart_Init done*");
	}
	uart_flush(UART_NUM_1);
}

void serial_read() 
{
uint8_t* dataSerial = (uint8_t*) malloc(dataSize);

int mid_size = 10000;

char * mid_str = (char *) heap_caps_malloc(mid_size, MALLOC_CAP_SPIRAM);
char dt[150] = {0};
size_t length, b_len;
int pac_cnt  = 0
int check = 0;

  while(1) {
  	
  	while ( uart_pattern_get_pos(UART_NUM) != -1 || pac_cnt < 100) {
  	       
		uart_read_bytes(UART_NUM, &dataSerial[0], 1, 0);
		
		switch(dataSerial[0]) {

					case MSG_IDLE :
					:
					:
					case DATA_PAC :
					:
					memset(dataSerial, 0, data_size);
					uart_read_bytes(UART_NUM, &dataSerial[1], data_size-1, 0);
					memcpy(data_struct, dataSerial, data_size );
					check = sprinft(dt, format_spec, data_struct.mem1,.....,data_struct.mem7);
					pac_cnt++;
					:
					break;
					default:
					break;
  	            }
				
				if (check > 0){
					check = 0;
					strcat(mid_str, dt);
				}
		}
		
		if (pac_cnt > 0) {
		
			>>>>
				ext_mem_malloc = (char *) heap_caps_malloc((mid_size), MALLOC_CAP_SPIRAM);
				memcpy(ext_mem_malloc, mid, strlen(mid));
				push ext_mem_malloc xQueueSend 
			>>>>
			bzero(mid_str, mid_size);
			pac_cnt = 0
		}
		if(xQueueReceive(uartQueueHandle, (void * )&event, (portTickType)1)) {
			case UART_DATA:
				break;
				:
				:
				:
			default:
			       break;
		}
		msDelay(1);
    }
    vTaskDelete(NULL);
}