开发案例

为 RV 核心写一个点亮 LED 的程序,在 RV 核启动时自动运行

1. LED 控制源码

新增 lichee/rtos/projects/t153_e907/demo/src/app_gpio_auto.c

/*
 * e907(RV 核) 开机自动运行示例:GPIO 定时翻转
 *
 * 参考:rtos/lichee/hal_v2/hal/examples/gpio/gpio_output_test/gpio_output_test.c
 *
 * 本文件提供一个可直接被 cpu0_app_entry() 调用的 init 函数,
 * 它创建一个 FreeRTOS 线程,循环翻转 GPIO 并打印日志。
 */
#include <stdio.h>
#include <sunxi_hal_common.h>
#include <hal_gpio.h>
#include <hal_thread.h>
#include "FreeRTOS.h"
#include "task.h"

/*
 * 默认用 GPIO:PA23, LED。
 * 注意:不要选 Linux 正在使用的脚(如 uart2 的 PB0/PB1、显示/触摸相关的 PD22/PA21 等)。
 */
#define APP_GPIO_PORT        SUNXI_GPIO_A        /* GPIO 组 */
#define APP_GPIO_PIN        PIN_23            /* 组内第几脚 */
#define APP_GPIO_PIN_NUM    SUNXI_GPA(23)        /* 绝对编号,set/get_data 用 */

#define APP_GPIO_BLINK_MS    500
#define APP_GPIO_STACK_SIZE    (4 * 1024)

static void app_gpio_thread(void *param)
{
    gpio_init_t gpio_initstruct;
    int level = GPIO_LOW;

    (void)param;

    /* 参考例程:配置为输出模式 */
    gpio_initstruct.port = APP_GPIO_PORT;
    gpio_initstruct.port_num = APP_GPIO_PIN;
    gpio_initstruct.mul_sel = PIN_MULTI_SEL_OUTPUT;
    gpio_initstruct.pull = PIN_PULL_DISABLE;
    gpio_initstruct.drv_level = PIN_MULTI_DRIVE_1;
    gpio_initstruct.data = GPIO_LOW;

    hal_gpio_init(&gpio_initstruct);
    printf("app_gpio: init done, start toggling\n");

    while (1) {
        level = !level;
        hal_gpio_set_data(APP_GPIO_PIN_NUM, level);

        /* FreeRTOS 延时,单位 tick;pdMS_TO_TICKS 把毫秒换成 tick */
        vTaskDelay(pdMS_TO_TICKS(APP_GPIO_BLINK_MS));
    }
}

/*
 * 开机自运行入口:由 projects/t153_e907/demo/src/main.c 的 cpu0_app_entry() 调用。
 * 平台代码在调度器启动后创建 "init-thread-0" 任务执行 cpu0_app_entry()。
 */
int app_gpio_auto_init(void)
{
    void *thread;

    thread = hal_thread_create(app_gpio_thread, NULL, "app_gpio",
                   APP_GPIO_STACK_SIZE, HAL_THREAD_PRIORITY_APP);
    if (thread == NULL) {
        printf("app_gpio: create thread failed\n");
        return -1;
    }

    hal_thread_start(thread);
    return 0;
}

2. 修改 Makefile

rtos/lichee/rtos/projects/t153_e907/demo/Makefile

obj-y += src/app_gpio_auto.o

3. 自动运行

rtos/lichee/rtos/projects/t153_e907/demo/src/main.c

#ifdef CONFIG_COMPONENTS_PM
extern int pm_init(int argc, char **argv);
#endif

/* 开机自动运行的应用程序入口(src/app_gpio_auto.c) */
extern int app_gpio_auto_init(void);

#ifdef CONFIG_GEN_DIGEST

...

#ifdef CONFIG_COMPONENT_CLI
    vCommandConsoleStart(0x1000, HAL_THREAD_PRIORITY_CLI, NULL);
#endif

    /* 开机自动运行的用户程序:创建线程后立即返回,不影响后续流程 */
    app_gpio_auto_init();

#ifdef CONFIG_COMPONENTS_CERTIFICATION_60730
extern void aw_run_check_start(void);
    aw_run_check_start();
#endif

4. 重新编译并烧录固件

# SDK 编译指令
./build.sh rtos
scp device/config/chips/t153/bin/amp_rv0.bin root@192.168.0.78:/lib/firmware/amp_rv0.bin

重新加载固件

echo stop > /sys/class/remoteproc/remoteproc0/state
echo amp_rv0.bin > /sys/class/remoteproc/remoteproc0/firmware
echo start > /sys/class/remoteproc/remoteproc0/state

测试: LED 闪烁

作者:Mai  创建时间:2026-08-28 17:57
最后编辑:陈柏林  更新时间:2026-11-05 14:56
上一篇:
下一篇: