示例 Applications

本页面记录了 Nexus 仓库中包含的示例应用程序。每个示例演示特定功能,并作为构建您自己应用程序的参考。

所有示例都位于仓库的 applications/ 目录中。

概述

Nexus 仓库包含以下示例应用程序:

示例 Applications

示例

目的

Key 特性

blinky

基本 GPIO 控制

LED 闪烁、GPIO 初始化、HAL 基础

config_demo

配置 management

配置存储、命名空间、导入/导出、UART 输出

freertos_demo

使用 OSAL 的多任务

任务、互斥锁、信号量、队列、生产者-消费者模式

shell_demo

命令行界面

Shell 命令、UART CLI、命令注册、行编辑

Blinky 示例

位置applications/blinky/

描述:一个简单的 LED 闪烁应用程序,演示基本的 GPIO 控制和 HAL 初始化。

特性

  • HAL 系统初始化

  • GPIO 配置为输出

  • LED 顺序闪烁

  • 使用 LED 指示器的错误处理

Hardware 要求

  • STM32F4 Discovery board (or compatible)

  • LEDs on PD12 (Green), PD13 (Orange), PD14 (Red), PD15 (Blue)

构建和运行

# Configure for STM32F4
CMake -B build-stm32f4 \
    -DCMAKE_TOOLCHAIN_FILE=CMake/toolchains/arm-none-eabi.CMake \
    -DNEXUS_PLATFORM=stm32f4

# Build
CMake --build build-stm32f4 --target blinky

# Flash
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg \
    -c "program build-stm32f4/applications/blinky/blinky.elf verify reset exit"

预期行为

STM32F4 Discovery 板上的所有四个 LED 按顺序闪烁:

  1. Green LED toggles (500ms)

  2. Orange LED toggles (500ms)

  3. Red LED toggles (500ms)

  4. Blue LED toggles (500ms)

  5. 重复

关键代码部分

LED 初始化

static hal_status_t led_init(void) {
    hal_gpio_config_t config = {
        .direction = HAL_GPIO_DIR_OUTPUT,
        .pull = HAL_GPIO_PULL_NONE,
        .output_mode = HAL_GPIO_OUTPUT_PP,
        .speed = HAL_GPIO_SPEED_LOW,
        .init_level = HAL_GPIO_LEVEL_LOW
    };

    /* Initialize all LEDs */
    hal_gpio_init(LED_GREEN_PORT, LED_GREEN_PIN, &config);
    hal_gpio_init(LED_ORANGE_PORT, LED_ORANGE_PIN, &config);
    hal_gpio_init(LED_RED_PORT, LED_RED_PIN, &config);
    hal_gpio_init(LED_BLUE_PORT, LED_BLUE_PIN, &config);

    return HAL_OK;
}

主循环

while (1) {
    hal_gpio_toggle(LED_GREEN_PORT, LED_GREEN_PIN);
    hal_delay_ms(BLINK_DELAY_MS);

    hal_gpio_toggle(LED_ORANGE_PORT, LED_ORANGE_PIN);
    hal_delay_ms(BLINK_DELAY_MS);

    hal_gpio_toggle(LED_RED_PORT, LED_RED_PIN);
    hal_delay_ms(BLINK_DELAY_MS);

    hal_gpio_toggle(LED_BLUE_PORT, LED_BLUE_PIN);
    hal_delay_ms(BLINK_DELAY_MS);
}

学习成果

学习此示例后,您将理解:

  • 如何初始化 HAL 子系统

  • 如何将 GPIO 引脚配置为输出

  • 如何控制 LED 状态

  • 如何使用 HAL 延迟函数

  • 基本错误处理模式

Config Demo 示例

位置applications/config_demo/

描述:演示用于存储和检索配置数据的配置管理器中间件。

特性

  • 配置存储和检索

  • Multiple data types (int32, uint32, float, bool, string, blob)

  • 命名空间隔离

  • 配置查询和枚举

  • JSON 导入/导出

  • 二进制导入/导出

  • 用于演示的 UART 输出

Hardware 要求

  • STM32F4 Discovery 板

  • USB-to-Serial adapter connected to UART2 (PA2/PA3)

  • 115200 波特率的串口终端

构建和运行

# Configure for STM32F4
CMake -B build-stm32f4 \
    -DCMAKE_TOOLCHAIN_FILE=CMake/toolchains/arm-none-eabi.CMake \
    -DNEXUS_PLATFORM=stm32f4

# Build
CMake --build build-stm32f4 --target config_demo

# Flash
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg \
    -c "program build-stm32f4/applications/config_demo/config_demo.elf verify reset exit"

预期行为

应用程序运行多个演示:

  1. 基本配置:存储和检索各种数据类型

  2. Namespaces: Demonstrates namespace isolation (WiFi and BLE configs)

  3. 查询:列出所有配置条目

  4. JSON 导出/导入:将配置导出为 JSON 并重新导入

  5. 二进制导出/导入:将配置导出为二进制格式并重新导入

就绪时绿色 LED 点亮,完成时缓慢闪烁。

关键代码部分

Basic 配置 Storage:

/* Store different data types */
config_set_i32("app.timeout", 5000);
config_set_u32("app.retry", 3);
config_set_float("sensor.threshold", 25.5f);
config_set_bool("feature.enabled", true);
config_set_str("device.name", "Nexus-Demo");

/* Read values back */
int32_t timeout = 0;
config_get_i32("app.timeout", &timeout, 0);

命名空间使用

/* Open namespaces */
config_ns_handle_t wifi_ns, ble_ns;
config_open_namespace("wifi", &wifi_ns);
config_open_namespace("ble", &ble_ns);

/* Store WiFi settings */
config_ns_set_str(wifi_ns, "ssid", "MyNetwork");
config_ns_set_bool(wifi_ns, "auto_connect", true);

/* Close namespaces */
config_close_namespace(wifi_ns);
config_close_namespace(ble_ns);

JSON 导出

/* Export to JSON */
char buffer[1024];
size_t actual_size = 0;
config_status_t status = config_export(CONFIG_FORMAT_JSON, 0, buffer,
                                       sizeof(buffer), &actual_size);

学习成果

学习此示例后,您将理解:

  • 如何初始化和使用配置管理器

  • 如何存储和检索不同的数据类型

  • 如何使用命名空间进行配置隔离

  • 如何导出和导入配置

  • 如何查询配置条目

  • 如何使用 UART 进行调试输出

FreeRTOS Demo 示例

位置applications/freertos_demo/

描述:演示使用带有 FreeRTOS 后端的 OSAL 进行多任务处理。

特性

  • 多个并发任务

  • 任务优先级和调度

  • 用于资源保护的互斥锁

  • 用于任务同步的信号量

  • 用于任务间通信的消息队列

  • 生产者-消费者模式

  • 系统统计跟踪

Hardware 要求

  • 支持 FreeRTOS 的 STM32F4 Discovery 板

  • 用于视觉反馈的 LED

构建和运行

# Configure for STM32F4 with FreeRTOS
CMake -B build-stm32f4 \
    -DCMAKE_TOOLCHAIN_FILE=CMake/toolchains/arm-none-eabi.CMake \
    -DNEXUS_PLATFORM=stm32f4 \
    -DNEXUS_OSAL_BACKEND=FreeRTOS

# Build
CMake --build build-stm32f4 --target freertos_demo

# Flash
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg \
    -c "program build-stm32f4/applications/freertos_demo/freertos_demo.elf verify reset exit"

预期行为

应用程序创建四个任务:

  1. Producer Task: Generates sensor data every 100ms, sends to queue

  2. Consumer Task: Receives data from queue, processes it

  3. LED Task: Blinks green LED as heartbeat (500ms)

  4. Stats Task: Periodically reports system statistics (2s)

LED Indicators:

  • Green: Heartbeat (system running)

  • Orange: Consumer processing data

  • Blue: Producer sending data

  • Red: Queue overflow or error

关键代码部分

Task Creation:

/* Producer task - Normal priority */
osal_task_config_t producer_config = {
    .name = "Producer",
    .func = producer_task,
    .arg = NULL,
    .priority = OSAL_TASK_PRIORITY_NORMAL,
    .stack_size = TASK_STACK_SIZE
};
osal_task_create(&producer_config, &g_producer_task);

Message Queue:

/* Create message queue for sensor data */
osal_queue_create(sizeof(sensor_data_t), SENSOR_QUEUE_SIZE,
                 &g_sensor_queue);

/* Send to queue */
osal_queue_send(g_sensor_queue, &data, 10);

/* Receive from queue */
osal_queue_receive(g_sensor_queue, &data, OSAL_WAIT_FOREVER);

Mutex Protection:

/* Create mutex */
osal_mutex_create(&g_stats_mutex);

/* Lock mutex */
if (osal_mutex_lock(g_stats_mutex, 100) == OSAL_OK) {
    /* Critical section - update shared statistics */
    g_stats.samples_produced++;
    osal_mutex_unlock(g_stats_mutex);
}

Semaphore Signaling:

/* Create semaphore */
osal_sem_create_counting(SENSOR_QUEUE_SIZE, 0, &g_data_ready_sem);

/* Signal data ready */
osal_sem_give(g_data_ready_sem);

/* Wait for data */
osal_sem_take(g_data_ready_sem, 500);

学习成果

学习此示例后,您将理解:

  • How to initialize OSAL and create tasks

  • How to use task priorities

  • How to protect shared resources with mutexes

  • How to synchronize tasks with semaphores

  • How to pass messages between tasks with queues

  • How to implement producer-consumer patterns

  • How to start the RTOS scheduler

相关文档

Shell Demo 示例

位置applications/shell_demo/

Description: Demonstrates the Shell/CLI middleware for interactive command-line interfaces.

特性

  • Interactive command-line interface over UART

  • Custom command registration

  • Command parsing and execution

  • Command completion

  • Built-in commands (help, version, etc.)

  • LED control commands

  • System commands (tick, delay, reboot)

Hardware 要求

  • STM32F4 Discovery 板

  • USB-to-Serial adapter connected to UART2 (PA2/PA3)

  • 115200 波特率的串口终端

构建和运行

# Configure for STM32F4
CMake -B build-stm32f4 \
    -DCMAKE_TOOLCHAIN_FILE=CMake/toolchains/arm-none-eabi.CMake \
    -DNEXUS_PLATFORM=stm32f4

# Build
CMake --build build-stm32f4 --target shell_demo

# Flash
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg \
    -c "program build-stm32f4/applications/shell_demo/shell_demo.elf verify reset exit"

预期行为

After flashing, open a serial terminal and you'll see:

========================================
  Nexus Shell Demo v1.0.0
  STM32F4 Discovery Board
  Type 'help' for available commands
========================================
nexus>

可用 Commands:

  • help - Show available commands

  • led <color> <on|off|toggle> - Control LEDs (green, orange, red, blue, all)

  • button - Read user button status

  • tick - Show system tick count

  • delay <ms> - Delay for specified milliseconds

  • reboot - Reboot the system

示例 Session:

nexus> help
Available commands:
  help     - Show this help
  led      - Control LEDs on the board
  button   - Read user button status
  tick     - Show system tick count
  delay    - Delay for specified milliseconds
  reboot   - Reset system

nexus> led green on
LED green ON

nexus> tick
System tick: 12345 ms

nexus> button
User button: RELEASED

关键代码部分

Command Handler:

static int cmd_led(int argc, char* argv[]) {
    if (argc < 3) {
        shell_printf("Usage: led <color> <on|off|toggle>\n");
        return 1;
    }

    const char* color = argv[1];
    const char* action = argv[2];

    /* Determine which LED and perform action */
    if (strcmp(action, "on") == 0) {
        hal_gpio_write(port, pin, HAL_GPIO_LEVEL_HIGH);
        shell_printf("LED %s ON\n", color);
    }

    return 0;
}

Command Registration:

static const shell_command_t cmd_led_def = {
    .name = "led",
    .handler = cmd_led,
    .help = "Control LEDs on the board",
    .usage = "led <color> <on|off|toggle>",
    .completion = led_color_completion
};

/* Register command */
shell_register_command(&cmd_led_def);

Shell Initialization:

/* Configure shell */
shell_config_t config = {
    .prompt = "nexus> ",
    .cmd_buffer_size = 128,
    .history_depth = 16,
    .max_commands = 32
};

/* Initialize shell */
shell_init(&config);

/* Set UART backend */
shell_set_backend(&shell_uart_backend);

/* Register commands */
shell_register_builtin_commands();
shell_register_command(&cmd_led_def);

学习成果

学习此示例后,您将理解:

  • How to initialize the Shell framework

  • How to register custom commands

  • How to parse command arguments

  • How to implement command handlers

  • How to use UART for interactive CLI

  • How to provide command completion

  • How to integrate shell with your application

相关文档

Building All 示例

To build all examples at once:

# Configure
CMake -B build-stm32f4 \
    -DCMAKE_TOOLCHAIN_FILE=CMake/toolchains/arm-none-eabi.CMake \
    -DNEXUS_PLATFORM=stm32f4

# Build all examples
CMake --build build-stm32f4 --target all

Example binaries will be located in:

  • build-stm32f4/applications/blinky/blinky.elf

  • build-stm32f4/applications/config_demo/config_demo.elf

  • build-stm32f4/applications/freertos_demo/freertos_demo.elf

  • build-stm32f4/applications/shell_demo/shell_demo.elf

Modifying 示例

All examples are designed to be easily modified and extended:

  1. Copy the Example: Copy the example directory to create your own application

  2. Modify CMakeLists.txt: Update project name and dependencies

  3. Modify main.c: Add your custom functionality

  4. Build and Test: Build and flash to your hardware

Example: Creating a Custom Application from Blinky

# Copy blinky example
cp -r applications/blinky applications/my_app

# Edit CMakeLists.txt
# Change: project(blinky C)
# To:     project(my_app C)

# Edit main.c with your custom code

# Build
CMake --build build-stm32f4 --target my_app

故障排除

Example doesn't build:

  • Ensure you have the correct toolchain installed

  • Check that the platform is set correctly (-DNEXUS_PLATFORM=stm32f4)

  • Verify all dependencies are available

Example doesn't run after flashing:

  • Check that you flashed the correct .elf file

  • Verify the board is powered correctly

  • Try resetting the board

  • Check LED indicators for error states

UART examples show no output:

  • Verify UART wiring (TX/RX crossed correctly)

  • Check baud rate matches (115200)

  • Ensure correct COM port is selected in terminal

  • Try a different USB-to-Serial adapter

FreeRTOS example crashes:

  • Increase task stack sizes if needed

  • Check for stack overflow

  • Verify FreeRTOS is configured correctly

下一步