2023-07-20 10:17:11 +08:00
|
|
|
/* UART Echo 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 "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "driver/uart.h"
|
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "uart.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is an example which echos any data it receives on configured UART back to the sender,
|
|
|
|
* with hardware flow control turned off. It does not use UART driver event queue.
|
|
|
|
*
|
|
|
|
* - Port: configured UART
|
|
|
|
* - Receive (Rx) buffer: on
|
|
|
|
* - Transmit (Tx) buffer: off
|
|
|
|
* - Flow control: off
|
|
|
|
* - Event queue: off
|
|
|
|
* - Pin assignment: see defines below (See Kconfig)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define RS485_TXD_PIN 17
|
|
|
|
#define RS485_RXD_PIN 18
|
|
|
|
#define RS485_RTS_PIN 8
|
|
|
|
#define RS485_CTS_PIN (-1)
|
|
|
|
|
|
|
|
// #define MCU_TXD_PIN 17
|
|
|
|
// #define MCU_RXD_PIN 18
|
|
|
|
|
|
|
|
|
|
|
|
#define PRINT_TXD_PIN 16
|
|
|
|
#define PRINT_RXD_PIN 15
|
|
|
|
#define PRINT_RTS_PIN (-1)
|
|
|
|
#define PRINT_CTS_PIN 3
|
|
|
|
|
|
|
|
|
|
|
|
#define RS485_UART_PORT_NUM (0)
|
|
|
|
#define RS485_UART_BAUD_RATE (115200)
|
|
|
|
#define RS485_TASK_STACK_SIZE (1024)
|
|
|
|
|
|
|
|
#define PRINT_UART_PORT_NUM (1)
|
|
|
|
#define PRINT_UART_BAUD_RATE (115200)
|
|
|
|
#define PRINT_TASK_STACK_SIZE (1024)
|
|
|
|
|
2024-01-20 17:44:50 +08:00
|
|
|
// static const char *TAG = "UART";
|
2023-07-20 10:17:11 +08:00
|
|
|
|
|
|
|
#define BUF_SIZE (1024)
|
|
|
|
|
|
|
|
void RS485_init(void)
|
|
|
|
{
|
|
|
|
uart_config_t uart_config = {
|
|
|
|
.baud_rate = RS485_UART_BAUD_RATE,
|
|
|
|
.data_bits = UART_DATA_8_BITS,
|
|
|
|
.parity = UART_PARITY_DISABLE,
|
|
|
|
.stop_bits = UART_STOP_BITS_1,
|
|
|
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
|
|
|
.source_clk = UART_SCLK_APB,
|
|
|
|
};
|
|
|
|
int intr_alloc_flags = 0;
|
|
|
|
|
|
|
|
#if CONFIG_UART_ISR_IN_IRAM
|
|
|
|
intr_alloc_flags = ESP_INTR_FLAG_IRAM;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(uart_driver_install(RS485_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
|
|
|
|
ESP_ERROR_CHECK(uart_param_config(RS485_UART_PORT_NUM, &uart_config));
|
|
|
|
ESP_ERROR_CHECK(uart_set_pin(RS485_UART_PORT_NUM, RS485_TXD_PIN, RS485_RXD_PIN, RS485_RTS_PIN, RS485_CTS_PIN));
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_init(void)
|
|
|
|
{
|
|
|
|
uart_config_t uart_config = {
|
|
|
|
.baud_rate = PRINT_UART_BAUD_RATE,
|
|
|
|
.data_bits = UART_DATA_8_BITS,
|
|
|
|
.parity = UART_PARITY_DISABLE,
|
|
|
|
.stop_bits = UART_STOP_BITS_1,
|
|
|
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
|
|
|
.source_clk = UART_SCLK_APB,
|
|
|
|
};
|
|
|
|
int intr_alloc_flags = 0;
|
|
|
|
|
|
|
|
#if CONFIG_UART_ISR_IN_IRAM
|
|
|
|
intr_alloc_flags = ESP_INTR_FLAG_IRAM;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK(uart_driver_install(PRINT_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
|
|
|
|
ESP_ERROR_CHECK(uart_param_config(PRINT_UART_PORT_NUM, &uart_config));
|
|
|
|
ESP_ERROR_CHECK(uart_set_pin(PRINT_UART_PORT_NUM, PRINT_TXD_PIN, PRINT_RXD_PIN, PRINT_RTS_PIN, PRINT_CTS_PIN));
|
|
|
|
}
|