Skip to content

Commit 6a00172

Browse files
authored
[ISSUE-242] Un-conditionally Halt the CPU (#244)
In some instances, `.halt()` not being called can lead to un-expected behaviour when attempting to flash or erase a target as the CPU may still be running. This patch addresses that by removing the `.halted()` check to ensure that `.halt()` is called un-conditionally.
1 parent 122d34b commit 6a00172

File tree

2 files changed

+17
-55
lines changed

2 files changed

+17
-55
lines changed

pylink/jlink.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,8 +2157,7 @@ def erase(self):
21572157
try:
21582158
# This has to be in a try-catch, as the device may not be in a
21592159
# state where it can halt, but we still want to try and erase.
2160-
if not self.halted():
2161-
self.halt()
2160+
self.halt()
21622161
except errors.JLinkException:
21632162
# Can't halt, so just continue to erasing.
21642163
pass
@@ -2209,9 +2208,8 @@ def flash(self, data, addr, on_progress=None, power_on=False, flags=0):
22092208

22102209
try:
22112210
# Stop the target before flashing. This is required to be in a
2212-
# try-catch as the 'halted()' check may fail with an exception.
2213-
if not self.halted():
2214-
self.halt()
2211+
# try-catch as the device may not be in a state where it can halt.
2212+
self.halt()
22152213
except errors.JLinkException:
22162214
pass
22172215

@@ -2264,9 +2262,8 @@ def flash_file(self, path, addr, on_progress=None, power_on=False):
22642262

22652263
try:
22662264
# Stop the target before flashing. This is required to be in a
2267-
# try-catch as the 'halted()' check may fail with an exception.
2268-
if not self.halted():
2269-
self.halt()
2265+
# try-catch as the device may not be in a state where it can halt.
2266+
self.halt()
22702267
except errors.JLinkException:
22712268
pass
22722269

tests/unit/test_jlink.py

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,16 +2639,14 @@ def test_jlink_erase_failed_to_halt(self):
26392639
Returns:
26402640
``None``
26412641
"""
2642-
self.jlink.halted = mock.Mock()
2643-
self.jlink.halted.side_effect = JLinkException(-1)
2644-
26452642
self.jlink.halt = mock.Mock()
2643+
self.jlink.halt.side_effect = JLinkException(-1)
26462644

26472645
self.dll.JLINK_EraseChip.return_value = 0
26482646
self.assertEqual(0, self.jlink.erase())
26492647

26502648
self.assertEqual(1, self.dll.JLINK_EraseChip.call_count)
2651-
self.assertEqual(0, self.jlink.halt.call_count)
2649+
self.assertEqual(1, self.jlink.halt.call_count)
26522650

26532651
def test_jlinK_erase_failed(self):
26542652
"""Tests the J-Link ``erase()`` method when it fails to erase.
@@ -2659,18 +2657,16 @@ def test_jlinK_erase_failed(self):
26592657
Returns:
26602658
``None``
26612659
"""
2662-
self.jlink.halted = mock.Mock()
2663-
self.jlink.halted.side_effect = JLinkException(-1)
2664-
26652660
self.jlink.halt = mock.Mock()
2661+
self.jlink.halt.side_effect = JLinkException(-1)
26662662

26672663
self.dll.JLINK_EraseChip.return_value = -1
26682664

26692665
with self.assertRaises(JLinkException):
26702666
self.jlink.erase()
26712667

26722668
self.assertEqual(1, self.dll.JLINK_EraseChip.call_count)
2673-
self.assertEqual(0, self.jlink.halt.call_count)
2669+
self.assertEqual(1, self.jlink.halt.call_count)
26742670

26752671
def test_jlink_erase_success(self):
26762672
"""Tests a successful erase of the target.
@@ -2681,9 +2677,6 @@ def test_jlink_erase_success(self):
26812677
Returns:
26822678
``None``
26832679
"""
2684-
self.jlink.halted = mock.Mock()
2685-
self.jlink.halted.return_value = False
2686-
26872680
self.jlink.halt = mock.Mock()
26882681

26892682
self.dll.JLINK_EraseChip.return_value = 1
@@ -2718,13 +2711,11 @@ def test_jlink_flash_fail_to_flash(self):
27182711
"""
27192712
self.dll.JLINKARM_WriteMem.return_value = 0
27202713

2714+
self.jlink.halt = mock.Mock()
27212715
self.jlink.power_on = mock.Mock()
27222716
self.jlink.erase = mock.Mock()
27232717
self.jlink.memory_write = mock.Mock()
27242718

2725-
self.jlink.halted = mock.Mock()
2726-
self.jlink.halted.return_value = True
2727-
27282719
# EndDownload failing
27292720
self.dll.JLINKARM_EndDownload.return_value = -1
27302721
with self.assertRaises(JLinkException):
@@ -2747,8 +2738,6 @@ def test_jlink_flash_success(self):
27472738
self.jlink.memory_write = mock.Mock()
27482739

27492740
self.jlink.halt = mock.Mock()
2750-
self.jlink.halted = mock.Mock()
2751-
self.jlink.halted.return_value = True
27522741

27532742
# With a progress callback.
27542743
self.assertEqual(0, self.jlink.flash([0], 0, util.noop))
@@ -2761,20 +2750,10 @@ def test_jlink_flash_success(self):
27612750
self.assertEqual(0, self.jlink.flash([0], 0))
27622751
self.dll.JLINK_SetFlashProgProgressCallback.assert_called_with(0)
27632752

2764-
# Halted exception
2765-
self.jlink.halted.side_effect = JLinkException(-1)
2753+
# Halt exception
2754+
self.jlink.halt.side_effect = JLinkException(-1)
27662755
self.assertEqual(0, self.jlink.flash([0], 0))
2767-
self.jlink.halted.side_effect = None
2768-
2769-
# Not halted
2770-
self.jlink.halted.return_value = False
2771-
self.assertEqual(0, self.jlink.flash([0], 0))
2772-
self.jlink.halt.assert_called_once()
2773-
2774-
# Halted
2775-
self.jlink.halted.return_value = True
2776-
self.assertEqual(0, self.jlink.flash([0], 0))
2777-
self.jlink.halt.assert_called_once()
2756+
self.jlink.halt.side_effect = None
27782757

27792758
# Without power by default
27802759
self.jlink.power_on = mock.Mock()
@@ -2802,8 +2781,7 @@ def test_jlink_flash_file_fail_to_flash(self):
28022781
"""
28032782
self.dll.JLINK_DownloadFile.return_value = -1
28042783

2805-
self.jlink.halted = mock.Mock()
2806-
self.jlink.halted.return_value = True
2784+
self.jlink.halt = mock.Mock()
28072785

28082786
self.jlink.power_on = mock.Mock()
28092787
self.jlink.erase = mock.Mock()
@@ -2822,9 +2800,6 @@ def test_jlink_flash_file_success(self):
28222800
"""
28232801
self.dll.JLINK_DownloadFile.return_value = 0
28242802

2825-
self.jlink.halted = mock.Mock()
2826-
self.jlink.halted.return_value = True
2827-
28282803
self.jlink.halt = mock.Mock()
28292804
self.jlink.power_on = mock.Mock()
28302805
self.jlink.erase = mock.Mock()
@@ -2840,20 +2815,10 @@ def test_jlink_flash_file_success(self):
28402815
self.assertEqual(0, self.jlink.flash_file('path', 0))
28412816
self.dll.JLINK_SetFlashProgProgressCallback.assert_called_with(0)
28422817

2843-
# Halted exception
2844-
self.jlink.halted.side_effect = JLinkException(-1)
2845-
self.assertEqual(0, self.jlink.flash_file('path', 0))
2846-
self.jlink.halted.side_effect = None
2847-
2848-
# Not halted
2849-
self.jlink.halted.return_value = False
2850-
self.assertEqual(0, self.jlink.flash_file('path', 0))
2851-
self.jlink.halt.assert_called_once()
2852-
2853-
# Halted
2854-
self.jlink.halted.return_value = True
2818+
# Halt exception
2819+
self.jlink.halt.side_effect = JLinkException(-1)
28552820
self.assertEqual(0, self.jlink.flash_file('path', 0))
2856-
self.jlink.halt.assert_called_once()
2821+
self.jlink.halt.side_effect = None
28572822

28582823
# With power
28592824
self.jlink.power_on = mock.Mock()

0 commit comments

Comments
 (0)