Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pdietl/add srec file programming support #1740

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions pyocd/flash/file_programmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import io
import os
import logging
import itertools
import bincopy
from elftools.elf.elffile import ELFFile
from intelhex import IntelHex
import errno
Expand Down Expand Up @@ -57,6 +59,7 @@ class FileProgrammer(object):
- Binary (.bin)
- Intel Hex (.hex)
- ELF (.elf or .axf)
- Motorola S-record (.srec)
"""
def __init__(self,
session: "Session",
Expand Down Expand Up @@ -99,10 +102,11 @@ def __init__(self,
self._loader = None

self._format_handlers: Dict[str, Callable[..., None]] = {
'axf': self._program_elf,
'bin': self._program_bin,
'elf': self._program_elf,
'hex': self._program_hex,
'axf': self._program_elf,
'bin': self._program_bin,
'elf': self._program_elf,
'hex': self._program_hex,
'srec': self._program_srec,
}

def program(self, file_or_path: Union[str, IO[bytes]], file_format: Optional[str] = None, **kwargs: Any):
Expand Down Expand Up @@ -160,8 +164,8 @@ def program(self, file_or_path: Union[str, IO[bytes]], file_format: Optional[str
# Open the file if a path was provided.
if is_path:
mode = 'rb'
if file_format == 'hex':
# hex file must be read as plain text file
if file_format == 'hex' or file_format == 'srec':
# hex and srec files must be read as plain text file
mode = 'r'
assert isinstance(file_or_path, str)
file_obj = open(file_or_path, mode)
Expand All @@ -177,6 +181,14 @@ def program(self, file_or_path: Union[str, IO[bytes]], file_format: Optional[str
if is_path and file_obj is not None:
file_obj.close()

def _program_srec(self, file_obj: IO[bytes], **kwargs: Any) -> None:
"""@brief SREC file format loader"""
assert self._loader

bf = bincopy.BinFile()
bf.add_srec(file_obj.read())
self._program_hex(io.StringIO(bf.as_ihex()), **kwargs)

def _program_bin(self, file_obj: IO[bytes], **kwargs: Any) -> None:
"""@brief Binary file format loader"""
assert self._loader
Expand Down
4 changes: 2 additions & 2 deletions pyocd/subcommands/load_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LoadSubcommand(SubcommandBase):

NAMES = ['load', 'flash']
HELP = "Load one or more images into target device memory."
EPILOG = "Supported file formats are: binary, Intel hex, and ELF32."
EPILOG = "Supported file formats are: binary (.bin), ELF32/AXF (.elf and .axf), Intel hex (.hex), and Motorola S-record (.srec)."
DEFAULT_LOG_LEVEL = logging.WARNING

## @brief Valid erase mode options.
Expand All @@ -57,7 +57,7 @@ def get_args(cls) -> List[argparse.ArgumentParser]:
"Only allowed if a single binary file is being loaded.")
parser_options.add_argument("--trust-crc", action="store_true",
help="Use only the CRC of each page to determine if it already has the same data.")
parser_options.add_argument("--format", choices=("bin", "hex", "elf"),
parser_options.add_argument("--format", choices=("axf", "bin", "elf", "hex", "srec"),
help="File format. Default is to use the file's extension. If multiple files are provided, then "
"all must be of this type.")
parser_options.add_argument("--skip", metavar="BYTES", default=0, type=int_base_0,
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ python_requires = >=3.7.0
# importlib_resources is used instead of stdlib importlib.resources because we
# want the selectable entry_points API, which is not present until Python 3.10.
install_requires =
bincopy>=20.0,<21.0
capstone>=4.0,<5.0
cmsis-pack-manager>=0.5.2,<1.0
colorama<1.0
Expand Down