BK7258 原生 Windows 开发(二)用串口心跳确认 CP 和 AP 均已启动

用串口心跳确认 CP 和 AP 均已启动,附完整工程源码、构建记录和实板串口日志。

本文目录

刚接手一块 BK7258 时,串口偶尔只来得及显示一段启动日志。这个例程在 AP 上启动一个常驻线程,每隔约一秒打印计数和实际经过的毫秒数。即使串口助手开得晚,也能从连续两行输出看出应用是否还在运行。

工程从哪里启动

工程名为 hello_heartbeat,以博通 Armino SMP release/v3.1.1.10 的 projects/app 为底板模板,对应提交 398cab28。BK7258 的 CPU0 运行 CP 镜像。CP 在 main() 中先用 rtos_set_user_app_entry() 登记启动回调,再调用 bk_init()。回调里通过 bk_pm_module_vote_boot_cp1_ctrl() 请求启动运行 AP 镜像的处理器。AP 上的 main() 完成自己的初始化后才创建心跳线程。

工程中的文件 在这个例程里的用途
hello_heartbeat/cp/cp_main.c 登记 CP 回调,打印 CP 启动提示,启动 AP
hello_heartbeat/ap/ap_main.c 初始化 AP、创建线程、统计相邻两次采样间隔
hello_heartbeat/ap/CMakeLists.txt 将 ap_main.c 加入 AP 组件,声明 bk_init 和 bk_rtos 依赖
hello_heartbeat/partitions/bk7258/ 保存本工程使用的 Flash 分区和 RAM 区域定义

在 Windows 编译

本文的目录约定为 SDK 和工具在 D:\BK,示例工程和构建入口在 E:\Code\BK7258_demo。环境使用 Armino Bash 0.3.0 中的 Arm GCC 10.3.1、GNU Make 4.4.1、Ninja 1.13.2 和 Python 3.12.10,另装 Windows CMake 3.28.6。单独复制下面的 C 文件时,还需要保留同版 SDK 的 AP、CP 配置和分区目录,以及工程根目录的构建入口。

在 PowerShell 运行以下命令。

1
2
3
4
5
Set-Location E:\Code\BK7258_demo
.\build.bat hello_heartbeat
Get-Content .\hello_heartbeat\build\build.log -Tail 8
Get-Item .\hello_heartbeat\build\bk7258\hello_heartbeat\package\all-app.bin |
    Select-Object FullName, Length, LastWriteTime

脚本依次构建 AP、CP,然后打包。验收时检查本次日志末尾的 EXIT=0,并核对 all-app.bin 的大小和更新时间。日后修改心跳周期,重新在同一目录运行同一条构建命令即可。

AP 如何测量一秒

rtos_create_thread() 的参数依次是线程句柄输出地址、优先级、线程名、入口、栈大小和用户参数。这里使用 BEKEN_APPLICATION_PRIORITY 和 2048 字节栈。心跳工作在独立线程中,因此 AP 入口完成创建后即可返回。修改 HEARTBEAT_PERIOD_MS 可以调整等待时间。

线程先取一次 rtos_get_time()。每次 rtos_delay_milliseconds() 返回后再读一次,做无符号 32 位减法,把两次读数的差打印为 elapsed_ms。只要相邻两次采样间隔小于一个完整的 32 位计时周期,毫秒计数器跨越零点时仍能得到正确间隔。这个 SDK 的实现从 RTOS tick 换算毫秒,所以调度、日志输出和 tick 分辨率都会影响相邻两行的间隔。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "bk_private/bk_init.h"
#include <components/log.h>
#include <os/os.h>
#include <stdint.h>

#define HEARTBEAT_PERIOD_MS 1000U
#define WORKER_STACK_BYTES  2048U

static char tag[] = "hello_heartbeat";

static void heartbeat_task(beken_thread_arg_t arg)
{
    (void)arg;
    uint32_t count = 0;
    uint32_t previous = rtos_get_time();

    BK_LOGI(tag, "READY: heartbeat period_ms=%u\r\n", HEARTBEAT_PERIOD_MS);
    for (;;) {
        rtos_delay_milliseconds(HEARTBEAT_PERIOD_MS);
        uint32_t now = rtos_get_time();
        ++count;
        BK_LOGI(tag, "count=%lu elapsed_ms=%lu\r\n",
                (unsigned long)count, (unsigned long)(now - previous));
        previous = now;
    }
}

int main(void)
{
    bk_err_t err = bk_init();
    if (err != BK_OK) {
        BK_LOGE(tag, "AP initialization failed (%d)\r\n", err);
        return err;
    }

    beken_thread_t worker;
    err = rtos_create_thread(&worker, BEKEN_APPLICATION_PRIORITY, "heartbeat",
                             heartbeat_task, WORKER_STACK_BYTES, NULL);
    if (err != BK_OK) {
        BK_LOGE(tag, "heartbeat task creation failed (%d)\r\n", err);
    }
    return err;
}

CP 完整入口

CP 的 READY 行在启动 AP 的请求发出前打印。因此它能帮助定位程序执行到了哪一步。若启动请求返回错误,BK_LOG_ON_ERR 会记录返回值。心跳计数由 AP 产生。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include "bk_private/bk_init.h"
#include <components/log.h>
#include <driver/pwr_clk.h>
#include <os/os.h>

extern void rtos_set_user_app_entry(beken_thread_function_t entry);

static void start_application_processor(beken_thread_arg_t arg)
{
    (void)arg;
    BK_LOGI("hello_heartbeat", "READY: BK7258 CP startup\r\n");
    BK_LOG_ON_ERR(bk_pm_module_vote_boot_cp1_ctrl(PM_BOOT_CP1_MODULE_NAME_APP,
                                                  PM_POWER_MODULE_STATE_ON));
}

int main(void)
{
    rtos_set_user_app_entry(start_application_processor);
    return bk_init();
}

怎样看串口

UART0 串口使用 115200、8 位数据、无校验、1 位停止、无流控。先打开串口,再复位开发板,可从启动信息开始连续采集。

实板日志将标签截断为 hello_he。筛选 count= 或 hello_he 可以找到心跳并排除网络后台日志。如果出现 heartbeat task creation failed,说明 AP 已完成初始化,下一步应查任务栈和任务控制块可用的堆空间。如果只能看到 CP 的启动行,先检查后续是否有 AP 启动请求或 AP 初始化的错误,并核对打包的完整镜像来自 hello_heartbeat 目录。如果只有连续的 count,通常是串口助手打开时启动提示已经过去。

源码与相关篇目

下载五个工程的完整源码 ZIP。源码包 SHA-256 为 2bbcb691ee9a178bfa7da3c6ff0721109dbbf990378f2cc62d2b5a473e399086。

系列其他篇目:在原生 Windows 上搭建环境并编译 AP、CP 双镜像固件 · 用 RTOS 队列在两个线程之间传递消息 · 扫描周围热点,读取信道与 RSSI · 创建开放热点并查询 SoftAP 状态 · 接入小智服务,验证应用层握手。

本次实板验证

实板复位后,CP 和 AP 都打印了 READY。随后连续记录到 count=1 至 15,每条 elapsed_ms 都是 1000。

验证时间为 2026-12-02T01:35:10.902+08:00,板卡为 立创·实战派 BK7258,串口为 COM10。本次所用 all-app.bin 共 2,095,008 字节,SHA-256 为 aa3b96774550fdf8052a11ef31550b33044be1fb6f3940a6a81252e6cc2f1f4f。

下载完整实板串口日志,已脱敏 · 下载完整编译日志,已脱敏 · 下载完整烧录日志 · 下载主机操作时间记录

下面逐行摘自主记录。

ap0:hello_he:I(1351):count=1 elapsed_ms=1000
ap0:hello_he:I(2351):count=2 elapsed_ms=1000
ap0:hello_he:I(3351):count=3 elapsed_ms=1000
hello_he:I(116):READY: BK7258 CP startup
ap1:hello_he:I(351):READY: heartbeat period_ms=1000
ap0:hello_he:I(1351):count=1 elapsed_ms=1000
ap0:hello_he:I(2351):count=2 elapsed_ms=1000
ap0:hello_he:I(3351):count=3 elapsed_ms=1000
ap0:hello_he:I(4351):count=4 elapsed_ms=1000
ap0:hello_he:I(5351):count=5 elapsed_ms=1000
ap0:hello_he:I(6351):count=6 elapsed_ms=1000
ap0:hello_he:I(7351):count=7 elapsed_ms=1000
展开本次完整串口日志(已脱敏)
ap0:hello_he:I(1351):count=1 elapsed_ms=1000
ap0:hello_he:I(2351):count=2 elapsed_ms=1000
ap0:hello_he:I(3351):count=3 elapsed_ms=1000
reboot

se ef:I(6):ENV start address is 0x007FA000, size is 8192 bytes.
ef:I(8):EasyFlash V4.1.0 is initialize success.
I(11):driver_init: psram init ok
sensor:W(11):uncali sdmadc value:[个体校准值]
rwnx_cal_set_rfconfig(0x102) phy on; rf off
cal:W(19):temp in otp is:[个体校准值]
cal:E(112):load polar tab magic code error 0xffffffff
xtal_cali:[个体校准值]
cal:I(113):idx:[个体校准值],r:[个体校准值],xtal:[个体校准值],pwr_gain:[个体校准值]
wifi:I(114):wifi inited(1) ret(0)
hello_he:I(116):READY: BK7258 CP startup
ap1:ef:I(0):ENV start address is 0x007FC000, size is 8192 bytes.
ap0:ef:I(170):EasyFlash V4.1.0 is initialize success.
ap1:WDRV:W(258):wdrv_tx_cmd_buffer_init, addr[0]=0x28011d38, pattern_addr=0x28011d34
ap1:WDRV:W(258):wdrv_tx_cmd_buffer_init, addr[1]=0x28012058, pattern_addr=0x28012054
ap1:WDRV:W(258):wdrv_tx_cmd_buffer_init, addr[2]=0x28012378, pattern_addr=0x28012374

$wakeup
bluetoot:W(386):bk_bluetooth_init bluetooth already initialised
wakeup
ap1:hello_he:I(351):READY: heartbeat period_ms=1000
ap0:hello_he:I(1351):count=1 elapsed_ms=1000
ap0:hello_he:I(2351):count=2 elapsed_ms=1000
ap0:hello_he:I(3351):count=3 elapsed_ms=1000
ap0:hello_he:I(4351):count=4 elapsed_ms=1000
ap0:hello_he:I(5351):count=5 elapsed_ms=1000
ap0:hello_he:I(6351):count=6 elapsed_ms=1000
ap0:hello_he:I(7351):count=7 elapsed_ms=1000
ap0:hello_he:I(8351):count=8 elapsed_ms=1000
ap0:hello_he:I(9351):count=9 elapsed_ms=1000
ap0:hello_he:I(10351):count=10 elapsed_ms=1000
ap0:hello_he:I(11351):count=11 elapsed_ms=1000
ap0:hello_he:I(12351):count=12 elapsed_ms=1000
ap0:hello_he:I(13351):count=13 elapsed_ms=1000
ap0:hello_he:I(14351):count=14 elapsed_ms=1000
ap0:hello_he:I(15351):count=15 elapsed_ms=1000
世界是你们
使用 Hugo 构建
主题 Stack 由 Jimmy 设计