# Vitis Python API 参考 (2025.2)

> 适用版本: Vitis ≥ 2020.x (推荐 2024.2+)
> 命令行: `vitis -s script.py`

## 导入

```python
import vitis
client = vitis.create_client()
```

## 工作空间

```python
# 设置工作空间
client.set_workspace(path="C:/project/vitis_workspace")

# 获取当前工作空间
client.get_workspace()
```

## 平台组件

```python
# 从 .xsa 创建平台
platform = client.create_platform_component(
    name="hw_platform",
    hw_design="path/to/system.xsa",
    os="standalone",
    processor="ps7_cortexa9_0"
)

# 获取平台
platform = client.get_component(name="hw_platform")

# 构建平台
platform = client.get_component(name="hw_platform")
status = platform.build()
```

## 应用组件

```python
# 创建应用
app = client.create_app_component(
    name="my_app",
    platform="hw_platform",
    template="empty_application"
)

# 获取应用
app = client.get_component(name="my_app")

# 构建应用
status = app.build()

# 清理
status = app.clean()
```

## 模板列表

```python
# 获取可用模板
templates = client.get_app_templates()
# ['empty_application', 'hello_world', 'memory_tests', 'zynq_fsbl', ...]
```

## BSP 配置

```python
# 获取平台的 domain
platform = client.get_component(name="hw_platform")

# 设置 BSP 属性
platform.set_property(
    prop_name="stdin",
    prop_value="ps7_uart_1"
)
platform.set_property(
    prop_name="stdout",
    prop_value="ps7_uart_1"
)

# 添加库
platform.add_library(lib_name="lwip211")
platform.add_library(lib_name="xilffs")

# 重新生成
platform.build()
```

## 系统工程

```python
# 创建系统工程
sys_proj = client.create_sys_project(
    name="system",
    platform="hw_platform",
    template="hello_world"
)

# 构建系统工程
sys_proj.build()
```

## 调试会话

```python
# 创建调试会话
debug = client.create_debug_session(
    name="debug_session",
    app="my_app"
)

# 连接到目标
debug.connect()

# 下载比特流
debug.program_device(bitstream="path/to/system.bit")

# 下载 ELF 并运行
debug.download()
debug.run()

# 暂停
debug.pause()

# 断开
debug.disconnect()
```

## 完整开发流程

```python
import vitis

# 1. 创建客户端
client = vitis.create_client()
client.set_workspace(path="C:/project/vitis_workspace")

# 2. 创建平台
platform = client.create_platform_component(
    name="hw",
    hw_design="export/system.xsa",
    os="standalone",
    processor="ps7_cortexa9_0"
)

# 3. 配置平台
platform = client.get_component(name="hw")
platform.set_property(prop_name="stdin", prop_value="ps7_uart_1")
platform.set_property(prop_name="stdout", prop_value="ps7_uart_1")
platform.build()

# 4. 创建应用
app = client.create_app_component(
    name="app",
    platform="hw",
    template="hello_world"
)

# 5. 构建
app = client.get_component(name="app")
app.build()
```

## 与 XSCT 对照

| XSCT (2018.3) | Vitis Python (2025.2) |
|---|---|
| `setws` | `client.set_workspace()` |
| `createhw -hwspec .sysdef` | `create_platform_component(hw_design=.xsa)` |
| `createbsp` | 平台创建时自动包含 |
| `createapp` | `create_app_component()` |
| `projects -build` | `component.build()` |
| `connect` + `fpga` + `dow` | `debug_session` API |
| `configbsp` | `platform.set_property()` |
| `regenbsp` | `platform.build()` |
