DepthAI Tutorials
DepthAI API References

ON THIS PAGE

  • Flash Bootloader
  • Demo
  • Setup
  • Source code

Flash Bootloader

This script will flash factory bootloader to the connected OAK camera. It will also clear the user bootloader (if it exists). Bootloader can only be flashed to devices that have on-board flash memory.

Demo

Example script output
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

Setup

Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the script
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
For additional information, please follow the installation guide.

Source code

Python
C++

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)

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.