/* UART asynchronous example, that
uses separate RX and TX tasks
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 "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "string.h"
#include "driver/gpio.h"
static const int RX_BUF_SIZE = 1024;
#define UART1_TXD_PIN (GPIO_NUM_4)
#define UART1_RXD_PIN (GPIO_NUM_5)
#define UART2_TXD_PIN (GPIO_NUM_17)
#define UART2_RXD_PIN (GPIO_NUM_16)
#define TAG "MAIN"
void init_uart1(void) {
const
uart_config_t uart_config = {
.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,
.source_clk = UART_SCLK_APB,
};
// We won't use
a buffer for sending data.
uart_driver_install(UART_NUM_1, RX_BUF_SIZE * 2, 0, 0,
NULL, 0);
uart_param_config(UART_NUM_1, &uart_config);
// U1RXD:GPIO9
U1TXD->SD_DATA2:GPIO10->SD_DATA3
uart_set_pin(UART_NUM_1, UART1_TXD_PIN, UART1_RXD_PIN,
UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
}
void init_uart2(void) {
const
uart_config_t uart_config = {
.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,
.source_clk = UART_SCLK_APB,
};
// We won't use
a buffer for sending data.
uart_driver_install(UART_NUM_2, RX_BUF_SIZE * 2, 0, 0,
NULL, 0);
uart_param_config(UART_NUM_2, &uart_config);
// U2RXD:GPIO16
U2TXD:GPIO17
uart_set_pin(UART_NUM_2, UART2_TXD_PIN, UART2_RXD_PIN,
UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
// Don't work
//uart_set_pin(UART_NUM_2, UART_PIN_NO_CHANGE,
UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE,
UART_PIN_NO_CHANGE);
}
int sendData(uart_port_t port, char* data)
{
const int len =
strlen(data);
const int
txBytes = uart_write_bytes(port, data, len);
ESP_LOGI(pcTaskGetName(NULL), "Wrote %d bytes", txBytes);
return txBytes;
}
static void tx_uart(void *pvParameters)
{
uart_port_t
uart_num = (uart_port_t)pvParameters;
ESP_LOGI(pcTaskGetName(NULL), "Start uart_num=%d",
uart_num);
char data[64];
strcpy(data,
"Hello world");
while (1) {
sendData(uart_num, "Hello world");
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}
static void rx_uart(void *pvParameters)
{
uart_port_t
uart_num = (uart_port_t)pvParameters;
ESP_LOGI(pcTaskGetName(NULL), "Start uart_num=%d",
uart_num);
static const
char *RX_TASK_TAG = "RX_TASK";
esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);
uint8_t* data =
(uint8_t*) malloc(RX_BUF_SIZE+1);
while (1) {
const int rxBytes = uart_read_bytes(uart_num, data,
RX_BUF_SIZE, 1000 / portTICK_RATE_MS);
if (rxBytes > 0) {
data[rxBytes] = 0;
ESP_LOGI(pcTaskGetName(NULL), "Read %d bytes: '%s'",
rxBytes, data);
ESP_LOG_BUFFER_HEXDUMP(pcTaskGetName(NULL), data, rxBytes,
ESP_LOG_INFO);
}
}
free(data);
}
void app_main(void)
{
ESP_LOGI(TAG,
"UART_NUM_1=%d", UART_NUM_1);
init_uart1();
init_uart2();
// UART1 受信処理
xTaskCreate(rx_uart, "UART1_RX", 1024*2, (void
*)UART_NUM_1, 4, NULL);
// UART1 送信処理
xTaskCreate(tx_uart, "UART1_TX", 1024*2, (void
*)UART_NUM_1, 4, NULL);
// UART2 受信処理
xTaskCreate(rx_uart, "UART2_RX", 1024*2, (void
*)UART_NUM_2, 4, NULL);
// UART2 送信処理
xTaskCreate(tx_uart, "UART2_TX", 1024*2, (void
*)UART_NUM_2, 4, NULL);
while(1) {
vTaskDelay(1);
}
} |