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

flash: fix some type errors, sort imports #1566

Merged
merged 1 commit into from
May 30, 2023
Merged
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
19 changes: 9 additions & 10 deletions pyocd/flash/file_programmer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pyOCD debugger
# Copyright (c) 2018-2020 Arm Limited
# Copyright (c) 2021 Chris Reed
# Copyright (c) 2021-2023 Chris Reed
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,26 +15,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import logging
import errno
import itertools
import logging
import os
from typing import (IO, TYPE_CHECKING, Any, Callable, Dict, Iterator, List, Optional, Tuple, Union)

from elftools.elf.elffile import ELFFile
from intelhex import IntelHex
import errno
from typing import (Any, Callable, Dict, IO, List, Optional, Tuple, TYPE_CHECKING, Union)

from .loader import (
FlashLoader,
ProgressCallback,
)
from ..core import exceptions
from .loader import (FlashLoader, ProgressCallback)

if TYPE_CHECKING:
from ..core.session import Session

LOG = logging.getLogger(__name__)

def ranges(i: List[int]) -> List[Tuple[int, int]]:
def ranges(i: List[int]) -> Iterator[Tuple[int, int]]:
"""Accepts a sorted list of byte addresses. Breaks the addresses into contiguous ranges.
Yields 2-tuples of the start and end address for each contiguous range.

Expand Down Expand Up @@ -184,6 +182,7 @@ def _program_bin(self, file_obj: IO[bytes], **kwargs: Any) -> None:
# If no base address is specified use the start of the boot memory.
address = kwargs.get('base_address', None)
if address is None:
assert self._session.target
boot_memory = self._session.target.memory_map.get_boot_memory()
if boot_memory is None:
raise exceptions.TargetSupportError("No boot memory is defined for this device")
Expand Down
33 changes: 14 additions & 19 deletions pyocd/flash/loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pyOCD debugger
# Copyright (c) 2018-2019 Arm Limited
# Copyright (c) 2021 Chris Reed
# Copyright (c) 2021-2023 Chris Reed
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,29 +15,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from dataclasses import dataclass
from __future__ import annotations

import logging
from pyocd.core.target import Target
from dataclasses import dataclass
from time import time
from typing import (Any, Callable, Dict, List, Optional, TYPE_CHECKING, Union)

from .builder import (
FlashBuilder,
MemoryBuilder,
ProgrammingInfo,
get_page_count,
get_sector_count,
)
from typing import (TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union, cast)

from ..core import exceptions
from ..core.target import Target
from ..utility.progress import print_progress
from .builder import (FlashBuilder, MemoryBuilder, ProgrammingInfo, get_page_count, get_sector_count)

if TYPE_CHECKING:
from ..core.memory_map import MemoryMap, MemoryRegion, RamRegion
from ..core.session import Session
from ..core.memory_map import (
MemoryRegion,
MemoryMap,
RamRegion,
)

LOG = logging.getLogger(__name__)

Expand All @@ -54,8 +46,9 @@ class RamBuilder(MemoryBuilder):
## Maximum number of bytes to write at once. This is primarily done so progress is updated occasionally.
_MAX_WRITE_SIZE = 4096

def __init__(self, session: "Session", region: "RamRegion") -> None:
def __init__(self, session: "Session", region: MemoryRegion) -> None:
"""@brief Constructor."""
assert region.is_writable, "Memory region passed to RamBuilder must be directly writable"
super().__init__()
self._session = session
self._region = region
Expand Down Expand Up @@ -247,7 +240,9 @@ def add_data(self, address, data):
region_builder = region.flash.get_flash_builder()
region_builder.log_performance = False
elif region.is_writable:
region_builder = RamBuilder(self._session, region)
# Casting to a RamRegion is technically not quite right, since we're only checking
# that the region is writable
region_builder = RamBuilder(self._session, cast(RamRegion, region))
else:
raise ValueError(f"memory region at address {address:#010x} is not writable")

Expand Down
Loading