[beken-72xx] Add UF2 uploading based on bk7231tools
This commit is contained in:
@@ -22,7 +22,13 @@
|
||||
],
|
||||
"upload": {
|
||||
"maximum_ram_size": 262144,
|
||||
"flash_size": 2097152
|
||||
"flash_size": 2097152,
|
||||
"require_upload_port": true,
|
||||
"speed": 921600,
|
||||
"protocol": "uart",
|
||||
"protocols": [
|
||||
"uart"
|
||||
]
|
||||
},
|
||||
"doc": {
|
||||
"params": {
|
||||
|
||||
@@ -61,6 +61,8 @@ def env_uf2upload(env, target):
|
||||
"${UF2OUT}",
|
||||
"uart",
|
||||
"${UPLOAD_PORT}",
|
||||
"-b",
|
||||
"${UPLOAD_SPEED}",
|
||||
]
|
||||
actions = [
|
||||
env.VerboseAction(env.AutodetectUploadPort, "Looking for upload port..."),
|
||||
|
||||
@@ -111,6 +111,11 @@
|
||||
"optional": true,
|
||||
"owner": "platformio",
|
||||
"version": "~2.1100.0"
|
||||
},
|
||||
"tool-bk7231tools": {
|
||||
"type": "uploader",
|
||||
"optional": true,
|
||||
"version": "https://github.com/libretuya/bk7231tools"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,6 +109,10 @@ class LibretuyaPlatform(PlatformBase):
|
||||
if "arduino" in framework:
|
||||
self.packages["framework-arduino-api"]["optional"] = False
|
||||
|
||||
# require bk7231tools
|
||||
if "beken-72xx" in framework:
|
||||
self.packages["tool-bk7231tools"]["optional"] = False
|
||||
|
||||
# mark framework SDK as required
|
||||
package_obj["optional"] = False
|
||||
|
||||
|
||||
57
tools/soc/uf2_bk72xx.py
Normal file
57
tools/soc/uf2_bk72xx.py
Normal file
@@ -0,0 +1,57 @@
|
||||
# Copyright (c) Kuba Szczodrzyński 2022-06-23.
|
||||
|
||||
import sys
|
||||
|
||||
try:
|
||||
from platformio.package.manager.tool import ToolPackageManager
|
||||
|
||||
manager = ToolPackageManager()
|
||||
pkg = manager.get_package("tool-bk7231tools")
|
||||
sys.path.append(pkg.path)
|
||||
from bk7231tools.serial import BK7231Serial
|
||||
except (ImportError, AttributeError):
|
||||
print("You need PlatformIO and tool-bk7231tools package to run this program.")
|
||||
exit(1)
|
||||
|
||||
|
||||
from tools.upload.ctx import UploadContext
|
||||
|
||||
|
||||
def upload_uart(
|
||||
ctx: UploadContext,
|
||||
port: str,
|
||||
baud: int = None,
|
||||
**kwargs,
|
||||
) -> bool:
|
||||
prefix = "| |--"
|
||||
# connect to chip
|
||||
bk = BK7231Serial(port=port, baudrate=baud or ctx.baudrate or 115200)
|
||||
|
||||
# collect continuous blocks of data
|
||||
parts = ctx.collect(ota_idx=1)
|
||||
# write blocks to flash
|
||||
for offs, data in parts.items():
|
||||
length = len(data.getvalue())
|
||||
data.seek(0)
|
||||
print(prefix, f"Writing {length} bytes to 0x{offs:06x}")
|
||||
try:
|
||||
bk.program_flash(
|
||||
data,
|
||||
length,
|
||||
offs,
|
||||
verbose=False,
|
||||
crc_check=True,
|
||||
dry_run=False,
|
||||
really_erase=True,
|
||||
)
|
||||
except ValueError as e:
|
||||
print(prefix, f"Writing failed: {e.args[0]}")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def upload(ctx: UploadContext, protocol: str, **kwargs) -> bool:
|
||||
if protocol == "uart":
|
||||
return upload_uart(ctx, **kwargs)
|
||||
print(f"Unknown upload protocol - {protocol}")
|
||||
return False
|
||||
@@ -53,6 +53,12 @@ class UploadContext:
|
||||
return None
|
||||
return datetime.fromtimestamp(letoint(self.uf2.tags[Tag.BUILD_DATE]))
|
||||
|
||||
@property
|
||||
def baudrate(self) -> int:
|
||||
if not self.board_manifest:
|
||||
self.board_manifest = get_board_manifest(self.board)
|
||||
return get(self.board_manifest, "upload.speed")
|
||||
|
||||
def get_offset(self, part: str, offs: int) -> int:
|
||||
if not self.board_manifest:
|
||||
self.board_manifest = get_board_manifest(self.board)
|
||||
@@ -87,7 +93,8 @@ class UploadContext:
|
||||
part1 = block.tags.get(Tag.LT_PART_1, None)
|
||||
part2 = block.tags.get(Tag.LT_PART_2, None)
|
||||
|
||||
if part1 and part2:
|
||||
if part1 is not None and part2 is not None:
|
||||
# decode empty tags too
|
||||
self.part1 = part1.decode()
|
||||
self.part2 = part2.decode()
|
||||
elif part1 or part2:
|
||||
|
||||
@@ -40,7 +40,12 @@ if __name__ == "__main__":
|
||||
|
||||
args = dict(args._get_kwargs())
|
||||
if uf2.family.code == "ambz":
|
||||
from uf2_rtltool import upload
|
||||
from tools.soc.uf2_rtltool import upload
|
||||
|
||||
if not upload(ctx, **args):
|
||||
exit(1)
|
||||
elif uf2.family.parent_code == "bk72xx":
|
||||
from tools.soc.uf2_bk72xx import upload
|
||||
|
||||
if not upload(ctx, **args):
|
||||
exit(1)
|
||||
|
||||
Reference in New Issue
Block a user