#include "driver/uart.h"
#include "driver/gpio.h"
#include <string.h>

TaskHandle_t Uart1Handle = NULL;
#define UART_TX_BUF_SIZE        (1024)        //Size of the TX ring buffer (if set uart_write_bytes() will block until all bytes sent)to 0 
#define UART_RX_BUF_SIZE        (1024)


//********************************
//********************************
//********** INITIALISE **********
//********************************
//********************************
	//----- INIT UART1 -----
	uart_config_t UartConfig1 = {
		.baud_rate = 115200,
		.data_bits = UART_DATA_8_BITS,
		.parity    = UART_PARITY_DISABLE,
		.stop_bits = UART_STOP_BITS_1,
		.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,	//UART_HW_FLOWCTRL_CTS
		.source_clk = UART_SCLK_APB,
		.rx_flow_ctrl_thresh = 100
	};

	uart_driver_install(UART_NUM_1, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE, 0, NULL, 0);
	uart_param_config(UART_NUM_1, &UartConfig1);
	uart_set_pin(UART_NUM_1, 17, 16, 0, 0);		//UART, TX, RX, RTS, CTS

Transmit

	char TxBuffer[] = {"AT\r\n"};
	uart_write_bytes(UART_NUM_1, TxBuffer, strlen(TxBuffer));	//Copies data to tx ring buffer. Will only st all if UART_TX_BUF_SIZE=0

Receive – Polling for data

	uint8_t RxData[128];
	int RxLength = 0;
	ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUM_1, (size_t*)&RxLength));
	if (RxLength > 0)
	{
		if (RxLength > sizeof(RxData))
			RxLength = sizeof(RxData);
		RxLength = uart_read_bytes(UART_NUM_1, RxData, RxLength, 100);		//Returns -1 on error, >=0 the number of bytes read from UART buffer
		ESP_LOGI(TAG, "RX %i bytes", (int)RxLength);
	}
	
	//If the data in the RX FIFO buffer is no longer needed you can use this:
	//uart_flush(UART_NUM_1);

Receive – Using an xTask

	xTaskCreate(UartRxTask, "UartRxTask", UART_RX_BUF_SIZE, NULL, 8, &Uart1Handle);		//Create task for reading data received

//*****************************
//*****************************
//********** UART RX **********
//*****************************
//*****************************
static void UartRxTask (void *arg)
{


}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *