def main():
args = parser.parse_args()
setup_logging(args)
# Sanity checks before attaching to board
if args.format == 'hex' and not intelhex_available:
print("Unable to program hex file")
print("Module 'intelhex' must be installed first")
exit()
if args.list_all:
MbedBoard.listConnectedBoards()
else:
board_selected = MbedBoard.chooseBoard(board_id=args.board_id, target_override=args.target_override,
frequency=args.frequency)
with board_selected as board:
flash = board.flash
transport = board.transport
# Boost speed with deferred transfers
transport.setDeferredTransfer(True)
progress = print_progress
if args.hide_progress:
progress = None
chip_erase = None
if args.chip_erase:
chip_erase = True
elif args.sector_erase:
chip_erase = False
# Binary file format
if args.format == 'bin':
# If no address is specified use the start of rom
if args.address is None:
args.address = board.flash.getFlashInfo().rom_start
with open(args.file, "rb") as f:
f.seek(args.skip, 0)
data = f.read()
args.address += args.skip
data = unpack(str(len(data)) + 'B', data)
flash.flashBlock(args.address, data, chip_erase=chip_erase, progress_cb=progress,
fast_verify=args.fast_program)
# Intel hex file format
if args.format == 'hex':
hex = IntelHex(args.file)
addresses = hex.addresses()
addresses.sort()
flash_builder = flash.getFlashBuilder()
data_list = list(ranges(addresses))
for start, end in data_list:
size = end - start + 1
data = list(hex.tobinarray(start=start, size=size))
flash_builder.addData(start, data)
flash_builder.program(chip_erase=chip_erase, progress_cb=progress, fast_verify=args.fast_program)
开发者ID:geky,项目名称:pyOCD,代码行数:60,代码来源:flash_tool.py
示例4: getAdapter
def getAdapter():
'''
Get a CMSIS DAP debug adapter
'''
interfaces = INTERFACE[usb_backend].getAllConnectedInterface(VID, PID)
if interfaces == None:
print "Not find a mbed interface"
sys.exit(1)
first_interface = interfaces[0]
adapter = MbedBoard("target_lpc1768", "flash_lpc1768", first_interface)
adapter.init()
return adapter
def _launchPyOCDGDBServer(msg_queue):
logger.info('starting PyOCD gdbserver...')
# ignore Ctrl-C, so we don't interrupt the GDB server when the
# being-debugged program is being stopped:
signal.signal(signal.SIGINT, _ignoreSignal);
from pyOCD.gdbserver import GDBServer
from pyOCD.board import MbedBoard
gdb = None
try:
board_selected = MbedBoard.chooseBoard()
with board_selected as board:
gdb = GDBServer(
board, 3333, {
'break_at_hardfault': True,
'step_into_interrupt': False,
'break_on_reset': False,
}
)
if gdb.isAlive():
msg_queue.put('alive')
while gdb.isAlive():
gdb.join(timeout = 0.5)
# check for a "kill" message from the parent process:
try:
msg = msg_queue.get(False)
if msg == 'kill':
gdb.stop()
break
except Queue.Empty:
pass
except Exception as e:
if gdb != None:
gdb.stop()
raise
msg_queue.put('dead')
def search_and_lock(board_id):
"""Repeatedly lock a board with the given ID"""
for _ in range(0, 20):
device = DAPAccess.get_device(board_id)
device.open()
device.close()
with MbedBoard.chooseBoard(board_id=board_id):
pass
def list_boards(self):
self.disable_logging()
try:
all_mbeds = MbedBoard.getAllConnectedBoards(close=True, blocking=False)
status = 0
error = ""
except Exception as e:
all_mbeds = []
status = 1
error = str(e)
if not self.args.output_json:
raise
if self.args.output_json:
boards = []
obj = {
'pyocd_version' : __version__,
'version' : { 'major' : 1, 'minor' : 0 },
'status' : status,
'boards' : boards,
}
if status != 0:
obj['error'] = error
for mbed in all_mbeds:
d = {
'unique_id' : mbed.unique_id,
'info' : mbed.getInfo(),
'board_name' : mbed.getBoardName(),
'target' : mbed.getTargetType(),
'vendor_name' : '',
'product_name' : '',
}
# Reopen the link so we can access the USB vendor and product names from the inteface.
# If it's not a USB based link, then we don't attempt this.
if isinstance(mbed.link, DAPAccessUSB):
try:
mbed.link.open()
d['vendor_name'] = mbed.link._interface.vendor_name
d['product_name'] = mbed.link._interface.product_name
mbed.link.close()
except Exception:
pass
boards.append(d)
print json.dumps(obj, indent=4) #, sys.stdout)
else:
index = 0
if len(all_mbeds) > 0:
for mbed in all_mbeds:
print("%d => %s boardId => %s" % (index, mbed.getInfo().encode('ascii', 'ignore'), mbed.unique_id))
index += 1
else:
print("No available boards are connected")
def validate_boards(data):
did_pass = True
print 'boards',
p = data.has_key('boards') and type(data['boards']) is list
if p:
b = data['boards']
if p:
print "PASSED"
else:
did_pass = False
print"FAILED"
try:
all_mbeds = MbedBoard.getAllConnectedBoards(close=True, blocking=False)
p = len(all_mbeds) == len(b)
matching_boards = 0
if p:
for mbed in all_mbeds:
for brd in b:
if mbed.unique_id == brd['unique_id']:
matching_boards += 1
p = brd.has_key('info') and brd.has_key('target') and brd.has_key('board_name')
if not p:
break
if not p:
break
p = matching_boards == len(all_mbeds)
if p:
print "PASSED"
else:
did_pass = False
print"FAILED"
except Exception:
print "FAILED"
did_pass = False
return did_pass
def main():
args = parser.parse_args()
setup_logging(args)
# Sanity checks before attaching to board
if args.format == 'hex' and not intelhex_available:
print("Unable to program hex file")
print("Module 'intelhex' must be installed first")
exit()
if args.list_all:
MbedBoard.listConnectedBoards()
else:
board_selected = MbedBoard.chooseBoard(board_id=args.board_id, target_override=args.target_override,
frequency=args.frequency)
with board_selected as board:
flash = board.flash
transport = board.transport
# Boost speed with deferred transfers
transport.setDeferredTransfer(True)
progress = print_progress
if args.hide_progress:
progress = None
has_file = args.file is not None
chip_erase = None
if args.chip_erase:
chip_erase = True
elif args.sector_erase:
chip_erase = False
if not has_file:
if chip_erase:
print("Erasing chip...")
flash.init()
flash.eraseAll()
print("Done")
elif args.sector_erase and args.address is not None:
flash.init()
page_addr = args.address
for i in range(args.count):
page_info = flash.getPageInfo(page_addr)
if not page_info:
break
# Align page address on first time through.
if i == 0:
delta = page_addr % page_info.size
if delta:
print("Warning: sector address 0x%08x is unaligned" % page_addr)
page_addr -= delta
print("Erasing sector 0x%08x" % page_addr)
flash.erasePage(page_addr)
page_addr += page_info.size
else:
print("No operation performed")
return
# If no format provided, use the file's extension.
if not args.format:
args.format = os.path.splitext(args.file)[1][1:]
# Binary file format
if args.format == 'bin':
# If no address is specified use the start of rom
if args.address is None:
args.address = board.flash.getFlashInfo().rom_start
with open(args.file, "rb") as f:
f.seek(args.skip, 0)
data = f.read()
args.address += args.skip
data = unpack(str(len(data)) + 'B', data)
flash.flashBlock(args.address, data, chip_erase=chip_erase, progress_cb=progress,
fast_verify=args.fast_program)
# Intel hex file format
elif args.format == 'hex':
hex = IntelHex(args.file)
addresses = hex.addresses()
addresses.sort()
flash_builder = flash.getFlashBuilder()
data_list = list(ranges(addresses))
for start, end in data_list:
size = end - start + 1
data = list(hex.tobinarray(start=start, size=size))
flash_builder.addData(start, data)
flash_builder.program(chip_erase=chip_erase, progress_cb=progress, fast_verify=args.fast_program)
else:
print("Unknown file format '%s'" % args.format)
from time import sleep
from random import randrange
import math
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
import pyOCD
from pyOCD.board import MbedBoard
import logging
logging.basicConfig(level=logging.INFO)
print "\r\n\r\n------ Test attaching to locked board ------"
for i in range(0, 10):
with MbedBoard.chooseBoard() as board:
# Erase and then reset - This locks Kinetis devices
board.flash.init()
board.flash.eraseAll()
board.target.reset()
print "\r\n\r\n------ Testing Attaching to board ------"
for i in range(0, 100):
with MbedBoard.chooseBoard() as board:
board.target.halt()
sleep(0.01)
board.target.resume()
sleep(0.01)
print "\r\n\r\n------ Flashing new code ------"
with MbedBoard.chooseBoard() as board:
请发表评论