这里的uart的代码我就不过多介绍了,因为sdk里面有很多的例程,然后也有很多大神的讲解,目前我在调试这个模块的时候遇到的问题:
1、硬件工程师画图的时候使用的是IO34和IO35作为uart,但是esp32的IO34到IO39是不可以作为输出口,即无法作为uart来使用。
代码如下(有点小改动):
/* 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"
/**
* This is an example which echos any data it receives on UART1 back to the sender,
* with hardware flow control turned off. It does not use UART driver event queue.
*
* - Port: UART1
* - Receive (Rx) buffer: on
* - Transmit (Tx) buffer: off
* - Flow control: off
* - Event queue: off
* - Pin assignment: see defines below
*/
//注意这里的mcu与esp32的tx和rx要相反
#define ECHO_TEST_TXD (GPIO_NUM_5)// tx->rx
#define ECHO_TEST_RXD (GPIO_NUM_4)// rx->tx
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
#define BUF_SIZE (1024)
uint8_t data[1024];
static void echo_task(void *arg)
{
/* Configure parameters of an UART driver,
* communication pins and install the driver */
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,
};
uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
uart_param_config(UART_NUM_1, &uart_config);
uart_set_pin(UART_NUM_1, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
// Configure a temporary buffer for the incoming data
//uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
while (1) {
// Read data from the UART
//测试发一个字节看看是否能正常接收
int len = uart_read_bytes(UART_NUM_1, data, BUF_SIZE, 20 / portTICK_RATE_MS);
if(len>0){
printf("data[0]:%x data[1]:%x len:%d\n", data[0],data[1],len);
}
// Write data back to the UART
//uart_write_bytes(UART_NUM_1, (const char *) data, len);
}
}
void app_main(void)
{
xTaskCreate(echo_task, "uart_echo_task", 2048, NULL, 10, NULL);
}