Skip to content

Commit

Permalink
Merge pull request #468 from flit/feature/region_algos
Browse files Browse the repository at this point in the history
Connecting flash algos to memory regions
  • Loading branch information
flit authored Dec 8, 2018
2 parents 061e90a + 43d20db commit 0575286
Show file tree
Hide file tree
Showing 77 changed files with 1,285 additions and 791 deletions.
16 changes: 12 additions & 4 deletions pyocd/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ def flatten_args(args):
"""! @brief Converts a list of lists to a single list."""
return [item for sublist in args for item in sublist]

def int_base_0(x):
return int(x, base=0)

class PyOCDTool(object):
"""! @brief Main class for the pyocd tool and subcommands.
"""
Expand Down Expand Up @@ -203,13 +206,13 @@ def build_parser(self):
help="Program an image to device flash.")
flashParser.add_argument("-e", "--erase", choices=ERASE_OPTIONS.keys(), default='auto',
help="Choose flash erase method. Default is auto.")
flashParser.add_argument("-a", "--base-address", metavar="ADDR",
flashParser.add_argument("-a", "--base-address", metavar="ADDR", type=int_base_0,
help="Base address used for the address where to flash a binary. Defaults to start of flash.")
flashParser.add_argument("--trust-crc", action="store_true",
help="Use only the CRC of each page to determine if it already has the same data.")
flashParser.add_argument("--format", choices=("bin", "hex", "elf"),
help="File format. Default is to use the file's extension.")
flashParser.add_argument("--skip", metavar="BYTES", default=0,
flashParser.add_argument("--skip", metavar="BYTES", default=0, type=int_base_0,
help="Skip programming the first N bytes. This can only be used with binary files.")
flashParser.add_argument("file", metavar="PATH",
help="File to program into flash.")
Expand Down Expand Up @@ -369,8 +372,13 @@ def do_flash(self):
if session is None:
sys.exit(1)
with session:
programmer = loader.FileProgrammer(session, self._args)
programmer.program(self._args.file)
programmer = loader.FileProgrammer(session,
chip_erase=ERASE_OPTIONS[self._args.erase],
trust_crc=self._args.trust_crc)
programmer.program(self._args.file,
base_address=self._args.base_address,
skip=self._args.skip,
format=self._args.format)

def do_erase(self):
self._increase_logging(["pyocd.tools.loader", "pyocd"])
Expand Down
4 changes: 1 addition & 3 deletions pyocd/board/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
limitations under the License.
"""

from ..target import (TARGET, FLASH)
from ..target import TARGET
import logging
import six

Expand All @@ -37,11 +37,9 @@ def __init__(self, session, target=None):
try:
log.info("Target type is %s", self._target_type)
self.target = TARGET[self._target_type](session)
self.flash = FLASH[self._target_type](self.target)
except KeyError as exc:
log.error("target '%s' not recognized", self._target_type)
six.raise_from(KeyError("target '%s' not recognized" % self._target_type), exc)
self.target.flash = self.flash
self._inited = False

## @brief Initialize the board.
Expand Down
49 changes: 40 additions & 9 deletions pyocd/core/coresight_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"""

from .target import Target
from .memory_map import MemoryType
from ..flash.loader import FlashEraser
from ..coresight import (dap, cortex_m, rom_table)
from ..debug.svd import (SVDFile, SVDLoader)
from ..debug.context import DebugContext
Expand All @@ -26,6 +28,12 @@
from ..utility.sequencer import CallSequence
import logging

# inspect.getargspec is deprecated in Python 3.
try:
from inspect import getfullargspec as getargspec
except ImportError:
from inspect import getargspec

##
# @brief Represents a chip that uses CoreSight debug infrastructure.
#
Expand Down Expand Up @@ -109,6 +117,7 @@ def add_core(self, core):
def create_init_sequence(self):
seq = CallSequence(
('load_svd', self.load_svd),
('create_flash', self.create_flash),
('dp_init', self.dp.init),
('power_up', self.dp.power_up_debug),
('find_aps', self.dp.find_aps),
Expand All @@ -126,6 +135,34 @@ def init(self):
seq = self.create_init_sequence()
seq.invoke()

def create_flash(self):
"""! @brief Instantiates flash objects for memory regions.
This init task iterates over flash memory regions and for each one creates the Flash
instance. It uses the flash_algo and flash_class properties of the region to know how
to construct the flash object.
"""
for region in self.memory_map.get_regions_of_type(MemoryType.FLASH):
# If the constructor of the region's flash class takes the flash_algo arg, then we
# need the region to have a flash algo dict to pass to it. Otherwise we assume the
# algo is built-in.
klass = region.flash_class
argspec = getargspec(klass.__init__)
if 'flash_algo' in argspec.args:
if region.flash_algo is not None:
obj = klass(self, region.flash_algo)
else:
logging.warning("flash region '%s' has no flash algo" % region.name)
continue
else:
obj = klass(self)

# Set the region in the flash instance.
obj.region = region

# Store the flash object back into the memory region.
region.flash = obj

def _create_component(self, cmpid):
cmp = cmpid.factory(cmpid.ap, cmpid, cmpid.address)
cmp.init()
Expand Down Expand Up @@ -162,12 +199,9 @@ def resume(self):
return self.selected_core.resume()

def mass_erase(self):
if self.flash is not None:
self.flash.init()
self.flash.erase_all()
return True
else:
return False
# The default mass erase implementation is to simply perform a chip erase.
FlashEraser(self.session, FlashEraser.Mode.CHIP).erase()
return True

def write_memory(self, addr, value, transfer_size=32):
return self.selected_core.write_memory(addr, value, transfer_size)
Expand Down Expand Up @@ -235,9 +269,6 @@ def set_target_state(self, state):
def get_state(self):
return self.selected_core.get_state()

def get_memory_map(self):
return self.memory_map

def set_vector_catch(self, enableMask):
return self.selected_core.set_vector_catch(enableMask)

Expand Down
Loading

0 comments on commit 0575286

Please sign in to comment.