mirror of
https://gitee.com/beecue/fastbee.git
synced 2026-02-05 16:45:56 +08:00
添加智能灯固件代码
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(uart_echo)
|
||||
@@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := uart_echo
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
# UART Echo Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This example demonstrates how to utilize UART interfaces of ESP32 by echoing back to the sender any data received on
|
||||
UART1.
|
||||
|
||||
## How to use example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
The example can be run on any ESP32 development board connected to a PC with a single USB cable for flashing and
|
||||
monitoring. The external interface should have 3.3V outputs. You may use e.g. 3.3V compatible USB-to-Serial dongle.
|
||||
|
||||
### Setup the Hardware
|
||||
|
||||
Connect the external serial interface to the ESP32 board as follows.
|
||||
|
||||
| ESP32 Interface | #define | ESP32 Pin | External UART Pin |
|
||||
| --- | --- | --- | --- |
|
||||
| Transmit Data (TxD) | ECHO_TEST_TXD | GPIO4 | RxD |
|
||||
| Receive Data (RxD) | ECHO_TEST_RXD | GPIO5 | TxD |
|
||||
| Ground | n/a | GND | GND |
|
||||
|
||||
Optionally, you can set-up and use a serial interface that has RTS and CTS signals in order to verify that the
|
||||
hardware control flow works. Connect the extra signals according to the following table, configure both extra pins in
|
||||
the example code by replacing existing `UART_PIN_NO_CHANGE` macros with the appropriate pin numbers and configure
|
||||
UART1 driver to use the hardware flow control by setting `.flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS` and adding
|
||||
`.rx_flow_ctrl_thresh = 122`.
|
||||
|
||||
| ESP32 Interface | #define | ESP32 Pin | External UART Pin |
|
||||
| --- | --- | --- | --- |
|
||||
| Request to Send (RTS) | ECHO_TEST_RTS | GPIO18 | CTS |
|
||||
| Clear to Send (CTS) | ECHO_TEST_CTS | GPIO19 | RTS |
|
||||
|
||||
### Configure the project
|
||||
|
||||
```
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
idf.py -p PORT flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
Type some characters in the terminal connected to the external serial interface. As result you should see echo in the
|
||||
terminal which is used for flashing and monitoring. You can verify if the echo indeed comes from ESP32 by
|
||||
disconnecting either `TxD` or `RxD` pin: no characters will appear when typing.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
You are not supposed to see the echo in the terminal which is used for flashing and monitoring, but in the other one
|
||||
which is connected to UART1.
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "uart_echo_example_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,3 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
||||
@@ -0,0 +1,64 @@
|
||||
/* 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
|
||||
*/
|
||||
|
||||
#define ECHO_TEST_TXD (GPIO_NUM_4)
|
||||
#define ECHO_TEST_RXD (GPIO_NUM_5)
|
||||
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
|
||||
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
|
||||
|
||||
#define BUF_SIZE (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);
|
||||
// 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", 1024, NULL, 10, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user