DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
此页面由 AI 自动翻译。查看英文原版
DepthAI 教程
DepthAI API 参考

本页目录

  • 演示
  • 设置
  • 源代码

闪烁 USB 恢复引导头

此脚本将特殊的 USB“恢复头”闪烁到设备的板载闪存中。某些设备,例如 OAK-FFC 4P,可以闪烁引导加载程序。通过写入 USB 恢复头,设备将不再使用引导加载程序,而是像标准 USB 设备一样启动(类似于 OAK-1)。这在您想要执行以下操作的场景中可能很有益:
  • 消除引导加载程序在空闲模式下的额外功耗
  • 恢复到简单的仅 USB 设置,移除高级引导加载程序功能

演示

示例脚本可能会产生类似以下的输出:
Command Line
1~/depthai-python/examples$ python3 flash_boot_header.py USB_RECOVERY
2Found device with name: 1.1
3Successfully flashed boot header!

设置

请运行 安装脚本 以下载所有必需的依赖项。请注意,此脚本必须在 git 上下文中运行,因此您必须先下载 depthai-python 存储库,然后运行脚本
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
有关更多信息,请遵循 安装指南

源代码

Python

Python
GitHub
1import sys
2from typing import Callable, Tuple
3from enum import Enum
4import depthai
5
6FLASH = depthai.DeviceBootloader.Memory.FLASH
7
8def main():
9    if len(sys.argv) < 2:
10        print(f"Usage: {sys.argv[0]} [GPIO_MODE/USB_RECOVERY/NORMAL/FAST] [params]")
11        print("\tOptions:")
12        print(f"\t\t{sys.argv[0]} GPIO_MODE gpioModeNum")
13        print(f"\t\t{sys.argv[0]} USB_RECOVERY")
14        print(f"\t\t{sys.argv[0]} NORMAL [frequency] [location] [dummyCycles] [offset]")
15        print(f"\t\t{sys.argv[0]} FAST [frequency] [location] [dummyCycles] [offset]")
16        return 0
17
18    mode = sys.argv[1].lower()
19    flash_func = None
20
21    if mode == "gpio_mode":
22        # gpio mode header
23        if len(sys.argv) < 3:
24            print(f"Usage: {sys.argv[0]} GPIO_MODE gpioModeNum")
25            return 0
26        
27        gpio_mode = int(sys.argv[2])
28        def flash_gpio(bl: 'depthai.DeviceBootloader') -> Tuple[bool, str]:
29            return bl.flashGpioModeBootHeader(FLASH, gpio_mode)
30        flash_func = flash_gpio
31
32    elif mode == "usb_recovery":
33        # usb recovery header
34        def flash_usb(bl: 'depthai.DeviceBootloader') -> Tuple[bool, str]:
35            return bl.flashUsbRecoveryBootHeader(FLASH)
36        flash_func = flash_usb
37
38    elif mode in ("normal", "fast"):
39        if len(sys.argv) != 2 and len(sys.argv) != 3 and len(sys.argv) <= 3:
40            print(f"Usage: {sys.argv[0]} NORMAL/FAST [frequency] [location] [dummyCycles] [offset]")
41            print(f"Usage: {sys.argv[0]} NORMAL/FAST [frequency]")
42            return 0
43
44        offset = -1
45        location = -1
46        dummy_cycles = -1
47        frequency = -1
48
49        if len(sys.argv) > 3:
50            if len(sys.argv) >= 3:
51                offset = int(sys.argv[2])
52            if len(sys.argv) >= 4:
53                location = int(sys.argv[3])
54            if len(sys.argv) >= 5:
55                dummy_cycles = int(sys.argv[4])
56            if len(sys.argv) >= 6:
57                frequency = int(sys.argv[5])
58        elif len(sys.argv) == 3:
59            frequency = int(sys.argv[2])
60
61        if mode == "normal":
62            def flash_normal(bl: 'depthai.DeviceBootloader') -> Tuple[bool, str]:
63                return bl.flashBootHeader(FLASH, frequency, location, dummy_cycles, offset)
64            flash_func = flash_normal
65        elif mode == "fast":
66            def flash_fast(bl: 'depthai.DeviceBootloader') -> Tuple[bool, str]:
67                return bl.flashFastBootHeader(FLASH, frequency, location, dummy_cycles, offset)
68            flash_func = flash_fast
69
70    # Find device and flash specified boot header
71    success, info = depthai.DeviceBootloader.getFirstAvailableDevice()
72    
73    if success:
74        print(f"Found device with name: {info.name}")
75        bl = depthai.DeviceBootloader(info)
76
77        # Flash the specified boot header
78        if flash_func:
79            success, error_msg = flash_func(bl)
80            if success:
81                print("Successfully flashed boot header!")
82            else:
83                print(f"Couldn't flash boot header: {error_msg}")
84        else:
85            print("Invalid boot option header specified")
86    else:
87        print("No devices found")
88
89    return 0
90
91if __name__ == "__main__":
92    main()

C++

1#include <algorithm>
2#include <cstdint>
3#include <cstdio>
4#include <fstream>
5#include <string>
6
7#include "depthai/depthai.hpp"
8
9int main(int argc, char** argv) {
10    if(argc < 2) {
11        std::cout << "Usage: " << argv[0] << " [GPIO_MODE/USB_RECOVERY/NORMAL/FAST] [params]" << std::endl;
12        std::cout << "\tOptions:\n";
13        std::cout << "\t\t" << argv[0] << " GPIO_MODE gpioModeNum" << std::endl;
14        std::cout << "\t\t" << argv[0] << " USB_RECOVERY" << std::endl;
15        std::cout << "\t\t" << argv[0] << " NORMAL [frequency] [location] [dummyCycles] [offset]" << std::endl;
16        std::cout << "\t\t" << argv[0] << " FAST [frequency] [location] [dummyCycles] [offset]" << std::endl;
17        return 0;
18    }
19    std::string mode{argv[1]};
20    std::transform(mode.begin(), mode.end(), mode.begin(), ::tolower);
21    std::function<std::tuple<bool, std::string>(dai::DeviceBootloader&)> flash = nullptr;
22
23    if(mode == "gpio_mode") {
24        // gpio mode header
25        if(argc < 3) {
26            std::cout << "Usage: " << argv[0] << " GPIO_MODE gpioModeNum" << std::endl;
27            return 0;
28        }
29        int gpioMode = std::stoi(std::string(argv[2]));
30
31        flash = [gpioMode](dai::DeviceBootloader& bl) { return bl.flashGpioModeBootHeader(dai::DeviceBootloader::Memory::FLASH, gpioMode); };
32    } else if(mode == "usb_recovery") {
33        // usb recovery header
34        flash = [](dai::DeviceBootloader& bl) { return bl.flashUsbRecoveryBootHeader(dai::DeviceBootloader::Memory::FLASH); };
35    } else if(mode == "normal" || mode == "fast") {
36        if(argc != 2 && argc != 3 && argc <= 3) {
37            std::cout << "Usage: " << argv[0] << " NORMAL/FAST [frequency] [location] [dummyCycles] [offset]" << std::endl;
38            std::cout << "Usage: " << argv[0] << " NORMAL/FAST [frequency]" << std::endl;
39            return 0;
40        }
41        int64_t offset = -1;
42        int64_t location = -1;
43        int32_t dummyCycles = -1;
44        int32_t frequency = -1;
45        if(argc > 3) {
46            if(argc >= 3) {
47                offset = std::stoi(std::string(argv[2]));
48            }
49            if(argc >= 4) {
50                location = std::stoi(std::string(argv[3]));
51            }
52            if(argc >= 5) {
53                dummyCycles = std::stoi(std::string(argv[4]));
54            }
55            if(argc >= 6) {
56                frequency = std::stoi(std::string(argv[5]));
57            }
58        } else if(argc == 3) {
59            frequency = std::stoi(std::string(argv[2]));
60        }
61
62        if(mode == "normal") {
63            flash = [offset, location, dummyCycles, frequency](dai::DeviceBootloader& bl) {
64                return bl.flashBootHeader(dai::DeviceBootloader::Memory::FLASH, frequency, location, dummyCycles, offset);
65            };
66        } else if(mode == "fast") {
67            flash = [offset, location, dummyCycles, frequency](dai::DeviceBootloader& bl) {
68                return bl.flashFastBootHeader(dai::DeviceBootloader::Memory::FLASH, frequency, location, dummyCycles, offset);
69            };
70        }
71    }
72
73    // Find device and flash specified boot header
74    bool res = false;
75    dai::DeviceInfo info;
76    std::tie(res, info) = dai::DeviceBootloader::getFirstAvailableDevice();
77
78    if(res) {
79        std::cout << "Found device with name: " << info.name << std::endl;
80        dai::DeviceBootloader bl(info);
81        // Flash the specified boot header
82        if(flash) {
83            bool success;
84            std::string errorMsg;
85            std::tie(success, errorMsg) = flash(bl);
86            if(success) {
87                std::cout << "Successfully flashed boot header!" << std::endl;
88            } else {
89                std::cout << "Couldn't flash boot header: " << errorMsg << std::endl;
90            }
91        } else {
92            std::cout << "Invalid boot option header specified" << std::endl;
93        }
94    } else {
95        std::cout << "No devices found" << std::endl;
96    }
97
98    return 0;
99}

需要帮助?

请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。