# Logic Analyzer

当需要用 Saleae 逻辑分析仪在线采集数字波形并解码 I2C/SPI/UART/CAN 时使用，通过 Logic 2 自动化服务驱动硬件采集、添加协议解码器并导出数据。

- Repository: https://github.com/LeoKemp223/embed-ai-tool (1055 stars)
- Category: Engineering
- License: see repository
- Last updated: 2026-08-20
- Runs scripts: yes, review before use

## Install

Claude Code: `npx skills add LeoKemp223/embed-ai-tool --skill logic-analyzer -g -a claude-code -y`
Codex: `npx skills add LeoKemp223/embed-ai-tool --skill logic-analyzer -g -a codex -y`
Cursor: `npx skills add LeoKemp223/embed-ai-tool --skill logic-analyzer -g -a cursor -y`
Gemini CLI: `npx skills add LeoKemp223/embed-ai-tool --skill logic-analyzer -g -a gemini-cli -y`

## Description

当需要用 Saleae 逻辑分析仪在线采集数字波形并解码 I2C/SPI/UART/CAN 时使用，通过 Logic 2 自动化服务驱动硬件采集、添加协议解码器并导出数据。

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# 逻辑分析仪采集（Saleae）

## 适用场景

- 嵌入式设备的 I2C/SPI/UART 时序需要用逻辑分析仪验证。
- 需要在线触发 Saleae 设备采集指定通道的数字波形。
- 需要对采集到的波形添加协议解码器，导出解码后的数据表（CSV）。
- 需要导出原始通道波形（CSV）做进一步分析。

## 必要输入

- Logic 2 桌面软件已运行，且开启 automation server（Settings/Preferences → Automation，或底部状态栏 Automation 按钮；也可命令行 `Logic.exe --automation` 启动）。
- Saleae 设备已连接（或用 Logic 2 的仿真设备测试，`--include-sim`）。
- 采集通道、采样率、持续时间。
- 解码协议时：每个解码器的协议类型与通道映射。

## 依赖

- `logic2-automation`（pip install logic2-automation）
- Logic 2 桌面软件（提供 automation server，默认端口 10430）

## 执行步骤

1. 先阅读 [references/usage.md](references/usage.md)，确认操作参数与解码器设置键。
2. 探测环境（检查包、端口、Logic 2 连通性与设备）：
   ```bash
   python scripts/la_tool.py --detect
   ```
3. 列出设备：
   ```bash
   python scripts/la_tool.py --devices
   ```
4. 采集与解码。推荐用可重复的 `--analyzer`，一次采集可挂多个解码器，各自导出独立 CSV：
   ```bash
   # 单协议：I2C（SCL=CH0, SDA=CH1）
   python scripts/la_tool.py --capture --channels 0-1 --samplerate 4M --duration 5 \
       --analyzer "i2c:scl=0,sda=1"

   # 多协议同时解码：I2C + UART + SPI
   python scripts/la_tool.py --capture --channels 0-6 --samplerate 16M --duration 5 \
       --analyzer "i2c:scl=0,sda=1" \
       --analyzer "uart:rx=2,baud=115200" \
       --analyzer "spi:clk=3,mosi=4,miso=5,cs=6"

   # CAN（单线，500kbps）
   python scripts/la_tool.py --capture --channels 0 --samplerate 4M --duration 5 \
       --analyzer "can:can=0,baud=500000"
   ```
   `--analyzer` 格式为 `协议:键=值,...`：
   - `i2c`：`scl`、`sda`（均必填）
   - `spi`：`clk`（必填），`mosi`、`miso`、`cs`（可选，未接的省略）
   - `uart`：`rx`（必填）、`baud`（默认 115200）
   - `can`：`can`（必填，CAN 信号通道）、`baud`（默认 500000）

   同协议出现多次会自动按 `i2c`、`i2c2` 区分导出文件名。

5. 输出位置：默认存到工程根 `.captures/logic-analyzer/`，文件名带时间戳防覆盖（如 `i2c_20260625_201048.csv`）。
   - 可用环境变量 `LA_CAPTURE_DIR` 覆盖默认目录。
   - 单解码器时可用 `--output 路径` 指定文件；相对路径相对默认目录，绝对路径原样。
   - 数值进制用 `--radix hex|dec|bin|ascii`（默认 hex）。

### 自然语言提示词模板

让 AI 调用本 skill 时，**通道映射是必须说清的物理接线信息**（AI 无法探测），SCL/SDA、CLK 等谁接哪个通道要明确。模板（填空即用）：

The rest: https://github.com/LeoKemp223/embed-ai-tool/blob/HEAD/skills/logic-analyzer/SKILL.md
