点灯程序
- 创建好 hello_world 模板后,在根目录下创建 comm 文件夹,用于放组件
- comm 中创建一个 CMakeLists.txt,创建一个 led 文件夹
led 文件夹中创建 led.c 和 led.h
目录结构如下:
├─comm │ │ CMakeLists.txt │ │ │ └─led │ led.c │ led.hcomm/CmakeLists.txt:
set(src_dirs LED) set(include_dirs LED) set(requires driver) idf_component_register( SRC_DIRS ${src_dirs} INCLUDE_DIRS ${include_dirs} REQUIRES ${requires} ) component_compile_options(-ffast-math -o3 -Wno-error=format=-Wno-format)comm/led/led.c
#include "led.h" #include "driver/gpio.h" /* 可配置的宏 */ #ifndef LED_GPIO #define LED_GPIO 2 // GPIO2 #endif #ifndef LED_LEVEL_ON #define LED_LEVEL_ON 0 // 0=低电平点亮 #endif void led_init(void) { gpio_config_t io_conf = { .pin_bit_mask = (1ULL << LED_GPIO), .mode = GPIO_MODE_OUTPUT, }; gpio_config(&io_conf); gpio_set_level(LED_GPIO, !LED_LEVEL_ON); // 初始熄灭 } void led_set(bool on) { gpio_set_level(LED_GPIO, on ? LED_LEVEL_ON : !LED_LEVEL_ON); } void led_toggle(void) { static bool s_state = false; s_state = !s_state; led_set(s_state); }comm/led/led.h
#ifndef LED_H #define LED_H #include <stdbool.h> /** @brief 初始化 LED 引脚 */ void led_init(void); /** @brief 控制 LED 开关 @param on true 点亮,false 熄灭 */ void led_set(bool on); /** @brief 翻转 LED 状态 */ void led_toggle(void); #endif /* LED_H */增加 main.c 文件
main/maim.c:
#include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "led.h" void app_main(void) { led_init(); while (true) { led_toggle(); vTaskDelay(pdMS_TO_TICKS(500)); } }main文件夹 CMakeLists.txt 更改启动文件,增加 comm 依赖
main 文件夹中的 CMakeLists.txt 添加
REQUIRES comm
main/CMakeLists.txt:idf_component_register( SRCS "main.c" PRIV_REQUIRES spi_flash INCLUDE_DIRS "" REQUIRES comm )根目录 CMakeLists.txt 增加 comm 依赖
增加
set(EXTRA_COMPONENT_DIRS comm)语句# 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.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) set(EXTRA_COMPONENT_DIRS comm) # "Trim" the build. Include the minimal set of components, main, and anything it depends on. idf_build_set_property(MINIMAL_BUILD ON) project(Demo1)
编译运行。done!
评论 (0)