2018-04-11 02:56:00 -04:00
|
|
|
/* Uart Events Example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "driver/uart.h"
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
#include "esp_log.h"
|
2020-03-27 04:20:21 -04:00
|
|
|
#include "sdkconfig.h"
|
2018-04-11 02:56:00 -04:00
|
|
|
|
|
|
|
/**
|
2020-03-27 04:20:21 -04:00
|
|
|
* This is a example which echos any data it receives on UART back to the sender using RS485 interface in half duplex mode.
|
|
|
|
*/
|
|
|
|
#define TAG "RS485_ECHO_APP"
|
|
|
|
|
|
|
|
// Note: Some pins on target chip cannot be assigned for UART communication.
|
|
|
|
// Please refer to documentation for selected board and target to configure pins using Kconfig.
|
|
|
|
#define ECHO_TEST_TXD (CONFIG_ECHO_UART_TXD)
|
|
|
|
#define ECHO_TEST_RXD (CONFIG_ECHO_UART_RXD)
|
2018-04-11 02:56:00 -04:00
|
|
|
|
|
|
|
// RTS for RS485 Half-Duplex Mode manages DE/~RE
|
2020-03-27 04:20:21 -04:00
|
|
|
#define ECHO_TEST_RTS (CONFIG_ECHO_UART_RTS)
|
2018-04-11 02:56:00 -04:00
|
|
|
|
|
|
|
// CTS is not used in RS485 Half-Duplex Mode
|
2020-03-27 04:20:21 -04:00
|
|
|
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
|
2018-04-11 02:56:00 -04:00
|
|
|
|
|
|
|
#define BUF_SIZE (127)
|
2020-03-27 04:20:21 -04:00
|
|
|
#define BAUD_RATE (CONFIG_ECHO_UART_BAUD_RATE)
|
2018-04-11 02:56:00 -04:00
|
|
|
|
|
|
|
// Read packet timeout
|
2022-02-08 04:39:38 -05:00
|
|
|
#define PACKET_READ_TICS (100 / portTICK_PERIOD_MS)
|
2018-04-11 02:56:00 -04:00
|
|
|
#define ECHO_TASK_STACK_SIZE (2048)
|
|
|
|
#define ECHO_TASK_PRIO (10)
|
2020-03-27 04:20:21 -04:00
|
|
|
#define ECHO_UART_PORT (CONFIG_ECHO_UART_PORT_NUM)
|
2018-04-11 02:56:00 -04:00
|
|
|
|
2020-03-27 04:20:21 -04:00
|
|
|
// Timeout threshold for UART = number of symbols (~10 tics) with unchanged state on receive pin
|
|
|
|
#define ECHO_READ_TOUT (3) // 3.5T * 8 = 28 ticks, TOUT=3 -> ~24..33 ticks
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
static void echo_send(const int port, const char* str, uint8_t length)
|
|
|
|
{
|
2020-03-27 04:20:21 -04:00
|
|
|
if (uart_write_bytes(port, str, length) != length) {
|
|
|
|
ESP_LOGE(TAG, "Send data critical failure.");
|
|
|
|
// add your code to handle sending failure here
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
2018-06-27 10:48:14 -04:00
|
|
|
|
2018-04-11 02:56:00 -04:00
|
|
|
// An example of echo test with hardware flow control on UART
|
2019-07-30 00:53:48 -04:00
|
|
|
static void echo_task(void *arg)
|
2018-04-11 02:56:00 -04:00
|
|
|
{
|
|
|
|
const int uart_num = ECHO_UART_PORT;
|
|
|
|
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,
|
|
|
|
.rx_flow_ctrl_thresh = 122,
|
2019-04-17 08:19:44 -04:00
|
|
|
.source_clk = UART_SCLK_APB,
|
2018-04-11 02:56:00 -04:00
|
|
|
};
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2018-06-27 10:48:14 -04:00
|
|
|
// Set UART log level
|
|
|
|
esp_log_level_set(TAG, ESP_LOG_INFO);
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2018-06-27 10:48:14 -04:00
|
|
|
ESP_LOGI(TAG, "Start RS485 application test and configure UART.");
|
2018-04-11 02:56:00 -04:00
|
|
|
|
2019-04-17 08:19:44 -04:00
|
|
|
// Install UART driver (we don't need an event queue here)
|
|
|
|
// In this example we don't even use a buffer for sending data.
|
2020-03-27 04:20:21 -04:00
|
|
|
ESP_ERROR_CHECK(uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0));
|
|
|
|
|
2018-04-11 02:56:00 -04:00
|
|
|
// Configure UART parameters
|
2020-03-27 04:20:21 -04:00
|
|
|
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2018-06-27 10:48:14 -04:00
|
|
|
ESP_LOGI(TAG, "UART set pins, mode and install driver.");
|
2020-03-27 04:20:21 -04:00
|
|
|
|
|
|
|
// Set UART pins as per KConfig settings
|
|
|
|
ESP_ERROR_CHECK(uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));
|
2018-04-11 02:56:00 -04:00
|
|
|
|
|
|
|
// Set RS485 half duplex mode
|
2020-03-27 04:20:21 -04:00
|
|
|
ESP_ERROR_CHECK(uart_set_mode(uart_num, UART_MODE_RS485_HALF_DUPLEX));
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2020-03-27 04:20:21 -04:00
|
|
|
// Set read timeout of UART TOUT feature
|
|
|
|
ESP_ERROR_CHECK(uart_set_rx_timeout(uart_num, ECHO_READ_TOUT));
|
2018-04-11 02:56:00 -04:00
|
|
|
|
|
|
|
// Allocate buffers for UART
|
|
|
|
uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
|
|
|
|
|
2018-06-27 10:48:14 -04:00
|
|
|
ESP_LOGI(TAG, "UART start recieve loop.\r\n");
|
2020-03-27 04:20:21 -04:00
|
|
|
echo_send(uart_num, "Start RS485 UART test.\r\n", 24);
|
2018-04-11 02:56:00 -04:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
//Read data from UART
|
|
|
|
int len = uart_read_bytes(uart_num, data, BUF_SIZE, PACKET_READ_TICS);
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2018-04-11 02:56:00 -04:00
|
|
|
//Write data back to UART
|
|
|
|
if (len > 0) {
|
2020-03-27 04:20:21 -04:00
|
|
|
echo_send(uart_num, "\r\n", 2);
|
2018-04-11 02:56:00 -04:00
|
|
|
char prefix[] = "RS485 Received: [";
|
2020-03-27 04:20:21 -04:00
|
|
|
echo_send(uart_num, prefix, (sizeof(prefix) - 1));
|
2018-06-27 10:48:14 -04:00
|
|
|
ESP_LOGI(TAG, "Received %u bytes:", len);
|
|
|
|
printf("[ ");
|
2018-04-11 02:56:00 -04:00
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
printf("0x%.2X ", (uint8_t)data[i]);
|
2020-03-27 04:20:21 -04:00
|
|
|
echo_send(uart_num, (const char*)&data[i], 1);
|
2018-04-11 02:56:00 -04:00
|
|
|
// Add a Newline character if you get a return charater from paste (Paste tests multibyte receipt/buffer)
|
|
|
|
if (data[i] == '\r') {
|
2020-03-27 04:20:21 -04:00
|
|
|
echo_send(uart_num, "\n", 1);
|
2018-04-11 02:56:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("] \n");
|
2020-03-27 04:20:21 -04:00
|
|
|
echo_send(uart_num, "]\r\n", 3);
|
2018-04-11 02:56:00 -04:00
|
|
|
} else {
|
|
|
|
// Echo a "." to show we are alive while we wait for input
|
2020-03-27 04:20:21 -04:00
|
|
|
echo_send(uart_num, ".", 1);
|
|
|
|
ESP_ERROR_CHECK(uart_wait_tx_done(uart_num, 10));
|
2018-04-11 02:56:00 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 04:20:21 -04:00
|
|
|
vTaskDelete(NULL);
|
2018-04-11 02:56:00 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2018-04-11 02:56:00 -04:00
|
|
|
{
|
|
|
|
//A uart read/write example without event queue;
|
|
|
|
xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, ECHO_TASK_PRIO, NULL);
|
|
|
|
}
|