mirror of
https://github.com/DuRuofu/ESP32-Guide.git
synced 2026-10-02 22:22:53 +08:00
code: 添加现有程序
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
# For more information about build system see
|
||||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
||||
# The following five 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.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(main)
|
||||
@@ -0,0 +1,4 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
# target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
void Task_1(void *pvParameters)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
ESP_LOGI(TAG, "Task_1");
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void Task_2(void *pvParameters)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
ESP_LOGI(TAG, "Task_2");
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
UBaseType_t taskPriority_1 = 0;
|
||||
UBaseType_t taskPriority_2 = 0;
|
||||
TaskHandle_t taskHandle_1 = NULL;
|
||||
TaskHandle_t taskHandle_2 = NULL;
|
||||
|
||||
xTaskCreate(Task_1, "Task_1", 2048, NULL, 12, &taskHandle_1);
|
||||
taskPriority_1 = uxTaskPriorityGet(taskHandle_1);
|
||||
ESP_LOGI(TAG, "Task_1 优先级:%d", taskPriority_1);
|
||||
|
||||
xTaskCreate(Task_2, "Task_1", 2048, NULL, 12, &taskHandle_2);
|
||||
taskPriority_2 = uxTaskPriorityGet(taskHandle_2);
|
||||
ESP_LOGI(TAG, "Task_1 优先级:%d", taskPriority_2);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# This file was generated using idf.py save-defconfig. It can be edited manually.
|
||||
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
|
||||
#
|
||||
CONFIG_IDF_TARGET="esp32s3"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
|
||||
@@ -0,0 +1,8 @@
|
||||
# For more information about build system see
|
||||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
||||
# The following five 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.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(main)
|
||||
@@ -0,0 +1,4 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
# target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
void Task_1(void *pvParameters)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
ESP_LOGI(TAG, "Task_1");
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void Task_2(void *pvParameters)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
ESP_LOGI(TAG, "Task_2");
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
TaskHandle_t taskHandle_1 = NULL;
|
||||
TaskHandle_t taskHandle_2 = NULL;
|
||||
|
||||
xTaskCreate(Task_1, "Task_1", 2048, NULL, 12, &taskHandle_1);
|
||||
xTaskCreate(Task_2, "Task_1", 2048, NULL, 12, &taskHandle_2);
|
||||
|
||||
|
||||
// 输出任务列表
|
||||
static char cBuffer[512]={0};
|
||||
vTaskList(cBuffer);
|
||||
ESP_LOGI(TAG, "任务列表:\n%s", cBuffer);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
# This file was generated using idf.py save-defconfig. It can be edited manually.
|
||||
# Espressif IoT Development Framework (ESP-IDF) 5.3.2 Project Minimal Configuration
|
||||
#
|
||||
CONFIG_IDF_TARGET="esp32s3"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
|
||||
CONFIG_FREERTOS_USE_TRACE_FACILITY=y
|
||||
CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS=y
|
||||
@@ -0,0 +1,8 @@
|
||||
# For more information about build system see
|
||||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
||||
# The following five 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.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(main)
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
// 任务函数
|
||||
void myTask(void *pvParameters)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
ESP_LOGI(TAG, "myTask");
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// 创建一个 FreeRTOS 任务
|
||||
// 参数说明:
|
||||
// 1. 任务入口函数:myTask
|
||||
// 2. 任务名称:"myTask",用于调试时标识任务
|
||||
// 3. 任务堆栈大小:2048 字节(适当分配以避免栈溢出)
|
||||
// 4. 任务参数:NULL(未传递参数)
|
||||
// 5. 任务优先级:1(优先级较低,空闲任务优先级为 0)
|
||||
// 6. 任务句柄:NULL(不需要保存任务句柄)
|
||||
xTaskCreate(myTask, "myTask", 2048, NULL, 1, NULL);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# This file was generated using idf.py save-defconfig. It can be edited manually.
|
||||
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
|
||||
#
|
||||
CONFIG_IDF_TARGET="esp32s3"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
|
||||
@@ -0,0 +1,8 @@
|
||||
# For more information about build system see
|
||||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
||||
# The following five 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.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(main)
|
||||
@@ -0,0 +1,4 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
# target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
|
||||
@@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int Int;
|
||||
int Array[3];
|
||||
} MyStruct;
|
||||
|
||||
// 任务函数1:接收整型参数
|
||||
void Task_1(void *pvParameters)
|
||||
{
|
||||
int *pInt = (int *)pvParameters;
|
||||
ESP_LOGI(TAG, "取得整型参数为:%d", *pInt);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
// 任务函数2:接收数组参数
|
||||
void Task_2(void *pvParameters)
|
||||
{
|
||||
int *pArray = (int *)pvParameters;
|
||||
ESP_LOGI(TAG, "取得数组参数为:%d %d %d", *pArray, *(pArray + 1), *(pArray + 2));
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
// 任务函数3:接收结构体参数
|
||||
void Task_3(void *pvParameters)
|
||||
{
|
||||
MyStruct *pStruct = (MyStruct *)pvParameters;
|
||||
ESP_LOGI(TAG, "取得结构体参数为:%d %d %d %d", pStruct->Int, pStruct->Array[0], pStruct->Array[1], pStruct->Array[2]);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
// 任务函数4:接收字符串参数
|
||||
void Task_4(void *pvParameters)
|
||||
{
|
||||
char *pChar = (char *)pvParameters;
|
||||
ESP_LOGI(TAG, "取得字符串参数为:%s", pChar);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
int Parameters_1 = 1;
|
||||
int Parameters_2[3] = {1, 2, 3};
|
||||
MyStruct Parameters_3 = {1, {1, 2, 3}};
|
||||
static const char *Parameters_4 = "Hello World!";
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// 传递整形参数
|
||||
xTaskCreate(Task_1, "Task_1", 2048, (void *)&Parameters_1, 1, NULL);
|
||||
|
||||
// 传递数组参数
|
||||
xTaskCreate(Task_2, "Task_2", 2048, (void *)&Parameters_2, 1, NULL);
|
||||
|
||||
// 传递结构体参数
|
||||
xTaskCreate(Task_3, "Task_3", 3048, (void *)&Parameters_3, 1, NULL);
|
||||
|
||||
// 传递字符串参数(注意这里没有取地址符号&)
|
||||
xTaskCreate(Task_4, "Task_4", 3048, (void *)Parameters_4, 1, NULL);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# This file was generated using idf.py save-defconfig. It can be edited manually.
|
||||
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
|
||||
#
|
||||
CONFIG_IDF_TARGET="esp32s3"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
|
||||
@@ -0,0 +1,8 @@
|
||||
# For more information about build system see
|
||||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
||||
# The following five 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.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(main)
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
// 任务函数
|
||||
void myTask(void *pvParameters)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
ESP_LOGI(TAG, "myTask");
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// 任务句柄
|
||||
TaskHandle_t taskHandle = NULL;
|
||||
// 创建一个 FreeRTOS 任务
|
||||
xTaskCreate(myTask, "myTask", 2048, NULL, 1, &taskHandle);
|
||||
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
|
||||
// 删除任务
|
||||
if (taskHandle != NULL)
|
||||
{
|
||||
vTaskDelete(taskHandle);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# This file was generated using idf.py save-defconfig. It can be edited manually.
|
||||
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
|
||||
#
|
||||
CONFIG_IDF_TARGET="esp32s3"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
|
||||
@@ -0,0 +1,8 @@
|
||||
# For more information about build system see
|
||||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
||||
# The following five 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.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(main)
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
// 任务函数
|
||||
void myTask(void *pvParameters)
|
||||
{
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
ESP_LOGI(TAG, "myTask:1");
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
ESP_LOGI(TAG, "myTask:2");
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
ESP_LOGI(TAG, "myTask:3");
|
||||
|
||||
// 删除任务(如果传递 NULL,则删除当前任务)
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// 任务句柄
|
||||
TaskHandle_t taskHandle = NULL;
|
||||
// 创建一个 FreeRTOS 任务
|
||||
xTaskCreate(myTask, "myTask", 2048, NULL, 1, &taskHandle);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# This file was generated using idf.py save-defconfig. It can be edited manually.
|
||||
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
|
||||
#
|
||||
CONFIG_IDF_TARGET="esp32s3"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
|
||||
@@ -0,0 +1,8 @@
|
||||
# For more information about build system see
|
||||
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
||||
# The following five 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.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(main)
|
||||
@@ -0,0 +1,4 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
# target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/timers.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
// 定时器回调
|
||||
void TimerCallback_1(TimerHandle_t xTimer)
|
||||
{
|
||||
// 取到定时器的名字
|
||||
const char *timerName = pcTimerGetName(xTimer);
|
||||
// 取得定时器的ID
|
||||
void *timerID = pvTimerGetTimerID(xTimer);
|
||||
ESP_LOGI(TAG, "定时器名称:%s,定时器id:%d,定时器回调函数执行", timerName, (int)timerID);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// 创建定时器句柄
|
||||
TimerHandle_t xTimer_1;
|
||||
TimerHandle_t xTimer_2;
|
||||
xTimer_1 = xTimerCreate("timer1", pdMS_TO_TICKS(1000), pdTRUE, (void *)0, TimerCallback_1);
|
||||
xTimer_2 = xTimerCreate("timer2", pdMS_TO_TICKS(2000), pdTRUE, (void *)1, TimerCallback_1);
|
||||
|
||||
xTimerStart(xTimer_1, 0); // 启动定时器
|
||||
xTimerStart(xTimer_2, 0); // 启动定时器
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
//停止定时器
|
||||
xTimerStop(xTimer_1, 0);
|
||||
xTimerStop(xTimer_2, 0);
|
||||
}
|
||||
|
||||
|
||||
// 其他常用函数
|
||||
// xTimerChangePeriod():修改定时器周期
|
||||
// xTimerReset():重置定时器
|
||||
// xTimerDelete():删除定时器
|
||||
@@ -0,0 +1,7 @@
|
||||
# This file was generated using idf.py save-defconfig. It can be edited manually.
|
||||
# Espressif IoT Development Framework (ESP-IDF) 5.3.2 Project Minimal Configuration
|
||||
#
|
||||
CONFIG_IDF_TARGET="esp32s3"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
|
||||
CONFIG_FREERTOS_USE_TRACE_FACILITY=y
|
||||
CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS=y
|
||||
Reference in New Issue
Block a user