From 4180abf106725d6f5054a361fcba2eada490eea8 Mon Sep 17 00:00:00 2001 From: Filip Jagodzinski Date: Wed, 23 Nov 2022 12:02:16 +0100 Subject: [PATCH] probe: stlink: Fix 1-byte transfers Internally the 1-byte transfers are handled in 3 phases: 1. read/write 8-bit chunks from every unaligned addresses until the first aligned address is reached, 2. read/write 32-bit chunks from all aligned addresses, 2. read/write 8-bit chunks from the remaining unaligned addresses. Size of the first unaligned read/write is set to the result of address (4-byte) alignment check and can be either 1, 2, or 3 bytes (the value of `unaligned_count` calculated as `addr & 0x3`). This is incorrect and every transfer with the requested size smaller than `unaligned_count` is terminated with the following error: Unhandled exception in handle_message (b'm'): result size (3) != requested size (1) [gdbserver] Skip the first unaligned transfer if the requested size is smaller than the result of the address alignment check. Handle the whole request in the second, unaligned read/write. --- pyocd/probe/stlink_probe.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyocd/probe/stlink_probe.py b/pyocd/probe/stlink_probe.py index 4eb36e2cd..f15d7c84d 100644 --- a/pyocd/probe/stlink_probe.py +++ b/pyocd/probe/stlink_probe.py @@ -330,7 +330,7 @@ def read_memory_block8(self, addr: int, size: int, **attrs: Any) -> Sequence[int # read leading unaligned bytes unaligned_count = addr & 3 - if (size > 0) and (unaligned_count > 0): + if (size >= unaligned_count) and (unaligned_count > 0): res += self._link.read_mem8(addr, unaligned_count, self._apsel, csw) size -= unaligned_count addr += unaligned_count @@ -356,7 +356,7 @@ def write_memory_block8(self, addr: int, data: Sequence[int], **attrs: Any) -> N # write leading unaligned bytes unaligned_count = addr & 3 - if (size > 0) and (unaligned_count > 0): + if (size >= unaligned_count) and (unaligned_count > 0): self._link.write_mem8(addr, data[:unaligned_count], self._apsel, csw) size -= unaligned_count addr += unaligned_count