进阶:J-Flash 量产工具与 SystemView 实时分析

知识库
知识库文档
/tech-stacks/segger-jlink/examples/进阶:J-Flash 量产工具与 SystemView 实时分析.md

文档

SEGGER J-Link 进阶:J-Flash 量产工具与 SystemView 分析

目标

掌握 J-Flash 批量编程的多功能配置(序列号烧录、选项字节、保护位),以及 SystemView 的 RTOS 实时事件分析。

一、J-Flash 批量量产配置

1. 创建 J-Flash 工程

J-Flash → File → New Project
→ Target Device: STM32F407VG
→ Target Interface: SWD, Speed: 4000 kHz
→ Flash Bank: 自动检测

2. 配置自动序列号

Options → Project Settings → Production
☑ Program Serial Number

Address:       0x0800E000   (Flash 末尾专用区域)
Length:        4 bytes
Start Value:   100000
Increment:     1
Format:        ASCII / Binary

3. 解锁 + 编程 + 锁定流程

Options → Project Settings → Production → Actions
执行顺序:
  1. Unsecure Chip (解除保护)
  2. Erase Sectors (擦除)
  3. Program (编程)
  4. Verify (校验)
  5. Secure Chip (重新写保护 Level 1)
  6. Start Application (运行)

4. 命令行调用(无头模式)

# 单次烧录
JFlash -openprj production.jflash \
       -open firmware.hex \
       -auto \
       -exit

# 批量烧录(脚本循环)
for i in {1..100}; do
    JFlash -openprj production.jflash \
           -open "firmware_v1.2.hex" \
           -auto \
           -exit
    echo "设备 #$i 完成"
done

5. 序列号 CSV 定制

# serial_numbers.csv
Chip_ID,Serial_Number,Calibration_Value
1,SN-24001-A,0x3F2A
2,SN-24002-A,0x3F15
3,SN-24003-A,0x3F48

配合 J-Flash 的 Custom Init Steps 宏调用 CSV 写入每个设备。

二、SystemView — RTOS 实时可视化

1. 集成到 FreeRTOS 工程

/* 下载 SystemView 源码:https://www.segger.com/downloads/systemview/ */
/* 将 SEGGER_SYSVIEW*.c/h 加入工程 */

#include "SEGGER_SYSVIEW.h"
#include "SEGGER_SYSVIEW_FreeRTOS.h"

int main(void) {
    HAL_Init();
    SystemClock_Config();

    /* ── 初始化 SystemView ── */
    SEGGER_SYSVIEW_Conf();
    SEGGER_SYSVIEW_Start();
    SEGGER_SYSVIEW_PrintfHost("SystemView 初始化完成\n");

    /* 注册 FreeRTOS 事件 */
    SEGGER_SYSVIEW_FreeRTOS_Init();

    /* 应用事件 */
    SEGGER_SYSVIEW_RecordVoid(0);  // 事件 ID 0: 系统启动

    // ... 创建任务 ...
    vTaskStartScheduler();
}

/* 在任务中标记事件 */
void sensor_task(void *pv) {
    while (1) {
        SEGGER_SYSVIEW_RecordEnterISR();  // 标记任务开始
        // 采集传感器数据
        SEGGER_SYSVIEW_RecordExitISR();

        // 命名事件
        SEGGER_SYSVIEW_OnUserStart(1);  // 用户事件 1: 数据处理
        process_data();
        SEGGER_SYSVIEW_OnUserStop(1);

        vTaskDelay(pdMS_TO_TICKS(100));
    }
}

2. SystemView 分析

# 启动 SystemView
SystemView

# 连接 J-Link → 选择设备 → 开始录制
# 分析面板:
#   - Timeline: 任务/中断时间线视图
#   - CPU Load: 各任务 CPU 占用率
#   - Context: 上下文切换次数
#   - Events: 用户自定义事件

3. 典型分析场景

问题 SystemView 发现
任务饥饿 低优先级任务长期不执行
中断轰炸 ISR 执行时间占比异常高
优先级翻转 高优任务被意外阻塞
内存泄漏 配合 Heap 监控发现
时序抖动 周期性任务的执行时间波动

三、J-Link Commander 诊断命令

JLinkExe -device STM32F407VG -if SWD -speed 4000

# 常用命令
J-Link>; connect
J-Link>; mem32 0x08000000, 16         # 读 Flash 前 64 字节
J-Link>; mem32 0x20000000, 16         # 读 SRAM
J-Link>; regs                          # 读 CPU 寄存器
J-Link>; setpc 0x08000400              # 设置 PC
J-Link>; w4 0x20000010, 0xDEADBEEF    # 写 32bit 到内存
J-Link>; erase                          # 擦除 Flash
J-Link>; loadbin firmware.bin, 0x08000000  # 烧录 .bin
J-Link>; r                              # 复位
J-Link>; g                              # 运行
J-Link>; halt                           # 暂停
J-Link>; sleep 1000                     # 延时
J-Link>; exit

关键点

  • J-Flash 命令行模式适合 CI/CD 产线自动化
  • SystemView 需要 RTT 或 SEGGER 协议支持,比 J-Scope 提供更丰富的系统级信息
  • 自定义事件 ID 0-31 为 SystemView 保留,用户事件从 32 开始
  • J-Link Commander 是底层诊断利器,适合排查硬件连接问题

信息

路径
/tech-stacks/segger-jlink/examples/进阶:J-Flash 量产工具与 SystemView 实时分析.md
更新时间
2026/5/31