|
| 1 | +# Copyright (c) 2024 MASSDRIVER EI (massdriver.space) |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +'''Runner for the Official Bouffalo Lab open source command-line flash tool (bflb-mcu-tool)''' |
| 6 | + |
| 7 | +import shutil |
| 8 | + |
| 9 | +from runners.core import RunnerCaps, ZephyrBinaryRunner |
| 10 | + |
| 11 | +DEFAULT_PORT = '/dev/ttyUSB0' |
| 12 | +DEFAULT_SPEED = '115200' |
| 13 | +DEFAULT_CHIP = 'bl602' |
| 14 | +BFLB_SDK_MODULE_NAME = 'hal_bouffalolab' |
| 15 | +DEFAULT_EXECUTABLE = "bflb-mcu-tool" |
| 16 | + |
| 17 | + |
| 18 | +class BlFlashCommandBinaryRunner(ZephyrBinaryRunner): |
| 19 | + '''Runner front-end for bflb-mcu-tool.''' |
| 20 | + |
| 21 | + def __init__( |
| 22 | + self, cfg, port=DEFAULT_PORT, baudrate=DEFAULT_SPEED, chipname=DEFAULT_CHIP, erase=False |
| 23 | + ): |
| 24 | + super().__init__(cfg) |
| 25 | + self.port = port |
| 26 | + self.baudrate = baudrate |
| 27 | + self.chipname = chipname |
| 28 | + self.erase = bool(erase) |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def name(cls): |
| 32 | + return 'bflb_mcu_tool' |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def capabilities(cls): |
| 36 | + return RunnerCaps(commands={'flash'}, erase=True) |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def do_add_parser(cls, parser): |
| 40 | + parser.add_argument( |
| 41 | + '-p', |
| 42 | + '--port', |
| 43 | + default=DEFAULT_PORT, |
| 44 | + help='serial port to use, default is ' + str(DEFAULT_PORT), |
| 45 | + ) |
| 46 | + parser.add_argument( |
| 47 | + '-b', |
| 48 | + '--baudrate', |
| 49 | + default=DEFAULT_SPEED, |
| 50 | + help='serial port speed to use, default is ' + DEFAULT_SPEED, |
| 51 | + ) |
| 52 | + parser.add_argument( |
| 53 | + '-ch', |
| 54 | + '--chipname', |
| 55 | + default=DEFAULT_CHIP, |
| 56 | + help='chip model, default is ' + DEFAULT_CHIP, |
| 57 | + choices=['bl602', 'bl606p', 'bl616', 'bl702', 'bl702l', 'bl808'], |
| 58 | + ) |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def do_create(cls, cfg, args): |
| 62 | + print(args) |
| 63 | + if shutil.which(DEFAULT_EXECUTABLE) is None: |
| 64 | + raise ValueError( |
| 65 | + "Couldnt find bflb-mcu-tool in PATH, please install with pip install \ |
| 66 | +bflb-mcu-tool" |
| 67 | + ) |
| 68 | + return BlFlashCommandBinaryRunner( |
| 69 | + cfg, port=args.port, baudrate=args.baudrate, chipname=args.chipname, erase=args.erase |
| 70 | + ) |
| 71 | + |
| 72 | + def do_run(self, command, **kwargs): |
| 73 | + self.ensure_output('bin') |
| 74 | + cmd_flash = [ |
| 75 | + DEFAULT_EXECUTABLE, |
| 76 | + '--interface', |
| 77 | + 'uart', |
| 78 | + '--port', |
| 79 | + self.port, |
| 80 | + '--baudrate', |
| 81 | + self.baudrate, |
| 82 | + '--chipname', |
| 83 | + self.chipname, |
| 84 | + '--firmware', |
| 85 | + self.cfg.bin_file, |
| 86 | + ] |
| 87 | + if self.erase is True: |
| 88 | + cmd_flash.append("--erase") |
| 89 | + self.check_call(cmd_flash) |
0 commit comments