Skip to content

Instantly share code, notes, and snippets.

@kekiefer
Last active February 10, 2019 00:46
Show Gist options
  • Save kekiefer/36cc5d9daac2565f8ec515d3868f3305 to your computer and use it in GitHub Desktop.
Save kekiefer/36cc5d9daac2565f8ec515d3868f3305 to your computer and use it in GitHub Desktop.
Program TinyFPGA BX flash metadata
# Copyright 2019 Kurt Kiefer
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# This program will (re)initialize the flash on a TinyFPGA BX.
# Use this at your own risk. If your metadata gets corrupted, you can still run the bootloader (and this script), but
# you will not be able to program userdata.
# Note that an earlier version of this script used the new style pointer to flash in the bootmeta region.
# This method doesn't work with older versions of tinyprog (like the one that's currently part of icestudio)
# so I've updated this to use the "old" method of putting all the bootmeta in protected region 2.
import tinyprog
import json
import uuid
import time
ports = tinyprog.get_ports("1d50:6130")
if len(ports) == 0:
print("No ports found")
exit(1)
port = ports[0]
prog = tinyprog.TinyProg(port)
pg1 = {
"boardmeta": {
"name": "TinyFPGA BX",
"fpga": "ice40lp8k-cm81",
"hver": "1.0.0",
"uuid": str(uuid.uuid4())
}
}
pg2 = {
"bootmeta": {
"bver": "1.0.1",
"bootloader": "TinyFPGA USB Bootloader",
"update": "https://tinyfpga.com/update/tinyfpga-bx",
"addrmap": {
"userimage": "0x28000-0x50000",
"bootloader": "0x000a0-0x28000",
"userdata": "0x50000-0x100000"
}
}
}
prog.erase_security_register_page(1)
prog.program_security_register_page(1, json.dumps(pg1).encode('utf8'))
prog.erase_security_register_page(2)
prog.program_security_register_page(2, json.dumps(pg2).encode('utf8'))
print("Programming done")
meta = tinyprog.TinyMeta(prog)
print(json.dumps(meta.root, indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment