Skip to contentSkip to conversation

Fix flashing with pyocd

PATCH New Wojciech Zmuda 1 participant 2 messages Apr 6, 2021

lore
Share & tools
lore
#0 lore raw

cover Fix flashing with pyocd

From: Wojciech Zmuda <[email protected]>

I'm submitting this patch with the consent of the original author Zbigniew Bodek <[email protected]>

The flashing process occurred to contain an undefined behavior, resulting in flashing garbage data to the connected board. It rarely occured with small images (e.g. zephyr-hello-world or zephyr-philosophers samples) but became visible with custom applications.

The exact mechanism is described in the commit log of the patch.

Zbigniew Bodek (1):
zephyr-flash-pyocd.bbclass: Fix problems with flashing particular
boards

 classes/zephyr-flash-pyocd.bbclass | 10 +++++++++-
 conf/machine/include/nrf52832.inc  |  3 +++
 2 files changed, 12 insertions(+), 1 deletion(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 zephyr-flash-pyocd.bbclass: Fix problems with flashing particular boards

From: Zbigniew Bodek <[email protected]>

By default, pyocd uses generic target type called "cortex_m" which should be able to connect and debug but not flash the memory. Normally pyocd would warn us of using default target instead of proper one but this message wasn't displayed.

Despite not providing target type, flashing process succeeded but results were undefined. On Nitrogen, sometimes it worked (especially for small images) and sometimes the programmed device crashed miserably.

Fix flashing operation by providing pyocd target type acquired from the conditional PYOCD_TARGET variable declared for each machine (chip family).

---
 classes/zephyr-flash-pyocd.bbclass | 10 +++++++++-
 conf/machine/include/nrf52832.inc  |  3 +++
 2 files changed, 12 insertions(+), 1 deletion(-)
classes/zephyr-flash-pyocd.bbclass +9 -1
index a873be4..6517945 100644
@@ -27,6 +27,9 @@ python do_flash_usb() {
         if not ids:
             bb.fatal("No probe requested for programming. Make sure PYOCD_FLASH_IDS is set.")
 
+    # Fetch target type to pass to the ConnectHelper
+    target = d.getVar('PYOCD_TARGET')
+
     # Program each ID
     for id in ids:
         bb.plain(f"Attempting to flash {os.path.basename(image)} to board {d.getVar('BOARD')} [{id}]")
@@ -35,7 +38,12 @@ python do_flash_usb() {
         now = 0
         step = 3
         while True:
-            session = ConnectHelper.session_with_chosen_probe(blocking=False, return_first=True, unique_id=id)
+            if target is not None:
+                session = ConnectHelper.session_with_chosen_probe(blocking=False, return_first=True, unique_id=id, target_override=target)
+            else:
+                bb.warn(f"Target type not provided. Flashing may fail or result in an undefined behavior.")
+                session = ConnectHelper.session_with_chosen_probe(blocking=False, return_first=True, unique_id=id)
+
             if session:
                 break
             if now >= timeout:
conf/machine/include/nrf52832.inc +3 -0
index 73e628a..e938aa6 100644
@@ -8,3 +8,6 @@ require conf/machine/include/tune-cortexm4.inc
 MACHINEOVERRIDES =. "nordic:"
 
 TUNE_FEATURES = "armv7m cortexm4"
+
+# Target type for this machine used by Pyocd
+PYOCD_TARGET = "nrf52"
Signature · 1 line
2.25.1