本页目录

  • 演示
  • 源代码

烧录引导加载器

此脚本会将 出厂引导加载器 烧录到所连接的 OAK 相机。它还会清除 用户引导加载器(如果存在)。 引导加载器 只能烧录到具有板载闪存存储器的设备。

演示

示例脚本输出
Command Line
1~/depthai-python/examples$ python3 flash_bootloader.py
2    [0] 1844301041C83D0E00 [X_LINK_USB_VSC]
3    Which DepthAI device to flash bootloader for [0..0]: 0
4    Booting latest bootloader first, will take a tad longer...
5    Bootloader version to flash: 0.0.26
6    No config found, skipping erasing user bootloader
7    Flashing USB bootloader...
8    Flashing progress: 0.0%
9    Flashing progress: 18.8%
10    Flashing progress: 31.2%
11    Flashing progress: 48.2%
12    Flashing progress: 94.2%
13    Flashing progress: 100.0%
14    Flashing successful. Took 7.55600329185836 seconds
这个示例需要DepthAI v3 API,参见安装说明

源代码

Python

Python
GitHub
1#!/usr/bin/env python3
2
3import depthai as dai
4import sys
5import time
6
7blType = dai.DeviceBootloader.Type.AUTO
8if len(sys.argv) > 1:
9    if sys.argv[1] == 'usb':
10        blType = dai.DeviceBootloader.Type.USB
11    elif sys.argv[1] == 'network':
12        blType = dai.DeviceBootloader.Type.NETWORK
13    else:
14        print("Specify either 'usb' or 'network' bootloader type")
15        exit()
16
17deviceInfos = dai.DeviceBootloader.getAllAvailableDevices()
18if len(deviceInfos) == 0:
19    print("No device found to flash. Exiting.")
20    exit(-1)
21else:
22    for i, di in enumerate(deviceInfos):
23        print(f'[{i}] {di.getMxId()} [{di.protocol.name}]', end='')
24        if di.state == dai.XLinkDeviceState.X_LINK_BOOTLOADER:
25            with dai.DeviceBootloader(di) as bl:
26                print(f' current bootloader: {bl.getVersion()}', end='')
27        print()
28    selected = input(f'Which OAK device to flash factory bootloader for [0..{len(deviceInfos)-1}]: ')
29    info = deviceInfos[int(selected)]
30
31hasBootloader = (info.state == dai.XLinkDeviceState.X_LINK_BOOTLOADER)
32if hasBootloader:
33    print("Warning! Flashing factory bootloader can potentially soft brick your device and should be done with caution.")
34    print("Do not unplug your device while the bootloader is flashing.")
35    print("Type 'y' and press enter to proceed, otherwise exits: ")
36    if input() != 'y':
37        print("Prompt declined, exiting...")
38        exit(-1)
39
40# Open DeviceBootloader and allow flashing bootloader
41print(f"Booting latest bootloader first, will take a tad longer...")
42with dai.DeviceBootloader(info, allowFlashingBootloader=True) as bl:
43    print("Bootloader version to flash:", bl.getVersion())
44    currentBlType = bl.getType()
45
46    if blType == dai.DeviceBootloader.Type.AUTO:
47        blType = currentBlType
48
49    # Check if bootloader type is the same, if already booted by bootloader (not in USB recovery mode)
50    if currentBlType != blType and hasBootloader:
51        print(f"Are you sure you want to flash '{blType.name}' bootloader over current '{currentBlType.name}' bootloader?")
52        print(f"Type 'y' and press enter to proceed, otherwise exits: ")
53        if input() != 'y':
54            print("Prompt declined, exiting...")
55            exit(-1)
56
57    try:
58        # Clears out user bootloader
59        configJson = bl.readConfigData()
60        configJson["userBlSize"] = 0
61        bl.flashConfigData(configJson)
62    except:
63        print('No config found, skipping erasing user bootloader')
64
65    # Create a progress callback lambda
66    progress = lambda p : print(f'Flashing progress: {p*100:.1f}%')
67
68    print(f"Flashing {blType.name} bootloader...")
69    startTime = time.monotonic()
70    (res, message) = bl.flashBootloader(dai.DeviceBootloader.Memory.FLASH, blType, progress)
71    if res:
72        print("Flashing successful. Took", time.monotonic() - startTime, "seconds")
73    else:
74        print("Flashing failed:", message)

C++

1#include <chrono>
2#include <string>
3
4#include "XLink/XLink.h"
5#include "depthai/depthai.hpp"
6#include "depthai/xlink/XLinkConnection.hpp"
7
8static const char* ProtocolToStr(XLinkProtocol_t val) {
9    switch(val) {
10        case X_LINK_USB_VSC:
11            return "X_LINK_USB_VSC";
12        case X_LINK_USB_CDC:
13            return "X_LINK_USB_CDC";
14        case X_LINK_PCIE:
15            return "X_LINK_PCIE";
16        case X_LINK_IPC:
17            return "X_LINK_IPC";
18        case X_LINK_TCP_IP:
19            return "X_LINK_TCP_IP";
20        case X_LINK_NMB_OF_PROTOCOLS:
21            return "X_LINK_NMB_OF_PROTOCOLS";
22        case X_LINK_ANY_PROTOCOL:
23            return "X_LINK_ANY_PROTOCOL";
24        default:
25            return "INVALID_ENUM_VALUE";
26            break;
27    }
28}
29
30int main(int argc, char** argv) {
31    using namespace std::chrono;
32
33    dai::DeviceBootloader::Type blType = dai::DeviceBootloader::Type::AUTO;
34    if(argc > 1) {
35        std::string blTypeStr(argv[1]);
36        if(blTypeStr == "usb") {
37            blType = dai::DeviceBootloader::Type::USB;
38        } else if(blTypeStr == "network") {
39            blType = dai::DeviceBootloader::Type::NETWORK;
40        } else {
41            std::cout << "Specify either 'usb' or 'network' bootloader type\n";
42            return 0;
43        }
44    }
45
46    dai::DeviceInfo info;
47    auto deviceInfos = dai::DeviceBootloader::getAllAvailableDevices();
48    if(deviceInfos.empty()) {
49        std::cout << "No device found to flash. Exiting." << std::endl;
50        return -1;
51    } else {
52        for(int i = 0; i < deviceInfos.size(); i++) {
53            const auto& devInfo = deviceInfos[i];
54            std::cout << "[" << i << "] " << devInfo.getMxId() << "[" << ProtocolToStr(devInfo.protocol) << "]";
55            if(devInfo.state == X_LINK_BOOTLOADER) {
56                dai::DeviceBootloader bl(devInfo);
57                std::cout << " current bootloader: " << bl.getVersion();
58            }
59            std::cout << std::endl;
60        }
61        int selected = 0;
62        std::cout << "Which DepthAI device to flash bootloader for [0.." << deviceInfos.size() - 1 << "]\n";
63        std::cin >> selected;
64        info = deviceInfos[selected];
65    }
66
67    bool hasBootloader = (info.state == X_LINK_BOOTLOADER);
68    if(hasBootloader) {
69        std::cout << "Warning! Flashing bootloader can potentially soft brick your device and should be done with caution." << std::endl;
70        std::cout << "Do not unplug your device while the bootloader is flashing." << std::endl;
71        std::cout << "Type 'y' and press enter to proceed, otherwise exits: ";
72        std::cin.ignore();
73        if(std::cin.get() != 'y') {
74            std::cout << "Prompt declined, exiting..." << std::endl;
75            return -1;
76        }
77    }
78
79    // Open DeviceBootloader and allow flashing bootloader
80    std::cout << "Booting latest bootloader first, will take a tad longer..." << std::endl;
81    dai::DeviceBootloader bl(info, true);
82    auto currentBlType = bl.getType();
83
84    if(blType == dai::DeviceBootloader::Type::AUTO) {
85        blType = currentBlType;
86    }
87
88    // Check if bootloader type is the same, if already booted by bootloader (not in USB recovery mode)
89    if(currentBlType != blType && hasBootloader) {
90        std::cout << "Are you sure you want to flash '" << blType << "' bootloader over current '" << currentBlType << "' bootloader?" << std::endl;
91        std::cout << "Type 'y' and press enter to proceed, otherwise exits: ";
92        std::cin.ignore();
93        if(std::cin.get() != 'y') {
94            std::cout << "Prompt declined, exiting..." << std::endl;
95            return -1;
96        }
97    }
98
99    // Create a progress callback lambda
100    auto progress = [](float p) { std::cout << "Flashing Progress..." << p * 100 << "%" << std::endl; };
101
102    std::cout << "Flashing " << blType << " bootloader..." << std::endl;
103    auto t1 = steady_clock::now();
104    bool success = false;
105    std::string message;
106    std::tie(success, message) = bl.flashBootloader(dai::DeviceBootloader::Memory::FLASH, blType, progress);
107    if(success) {
108        std::cout << "Flashing successful. Took " << duration_cast<milliseconds>(steady_clock::now() - t1).count() << "ms" << std::endl;
109    } else {
110        std::cout << "Flashing failed: " << message << std::endl;
111    }
112    return 0;
113}

需要帮助?

请前往 OAKChina 官网 获取技术支持或解答您的任何疑问。