Skip to content
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
13 changes: 5 additions & 8 deletions pylink/jlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -2157,8 +2157,7 @@ def erase(self):
try:
# This has to be in a try-catch, as the device may not be in a
# state where it can halt, but we still want to try and erase.
if not self.halted():
self.halt()
self.halt()
except errors.JLinkException:
# Can't halt, so just continue to erasing.
pass
Expand Down Expand Up @@ -2209,9 +2208,8 @@ def flash(self, data, addr, on_progress=None, power_on=False, flags=0):

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

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

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

Expand Down
59 changes: 12 additions & 47 deletions tests/unit/test_jlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -2639,16 +2639,14 @@ def test_jlink_erase_failed_to_halt(self):
Returns:
``None``
"""
self.jlink.halted = mock.Mock()
self.jlink.halted.side_effect = JLinkException(-1)

self.jlink.halt = mock.Mock()
self.jlink.halt.side_effect = JLinkException(-1)

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

self.assertEqual(1, self.dll.JLINK_EraseChip.call_count)
self.assertEqual(0, self.jlink.halt.call_count)
self.assertEqual(1, self.jlink.halt.call_count)

def test_jlinK_erase_failed(self):
"""Tests the J-Link ``erase()`` method when it fails to erase.
Expand All @@ -2659,18 +2657,16 @@ def test_jlinK_erase_failed(self):
Returns:
``None``
"""
self.jlink.halted = mock.Mock()
self.jlink.halted.side_effect = JLinkException(-1)

self.jlink.halt = mock.Mock()
self.jlink.halt.side_effect = JLinkException(-1)

self.dll.JLINK_EraseChip.return_value = -1

with self.assertRaises(JLinkException):
self.jlink.erase()

self.assertEqual(1, self.dll.JLINK_EraseChip.call_count)
self.assertEqual(0, self.jlink.halt.call_count)
self.assertEqual(1, self.jlink.halt.call_count)

def test_jlink_erase_success(self):
"""Tests a successful erase of the target.
Expand All @@ -2681,9 +2677,6 @@ def test_jlink_erase_success(self):
Returns:
``None``
"""
self.jlink.halted = mock.Mock()
self.jlink.halted.return_value = False

self.jlink.halt = mock.Mock()

self.dll.JLINK_EraseChip.return_value = 1
Expand Down Expand Up @@ -2718,13 +2711,11 @@ def test_jlink_flash_fail_to_flash(self):
"""
self.dll.JLINKARM_WriteMem.return_value = 0

self.jlink.halt = mock.Mock()
self.jlink.power_on = mock.Mock()
self.jlink.erase = mock.Mock()
self.jlink.memory_write = mock.Mock()

self.jlink.halted = mock.Mock()
self.jlink.halted.return_value = True

# EndDownload failing
self.dll.JLINKARM_EndDownload.return_value = -1
with self.assertRaises(JLinkException):
Expand All @@ -2747,8 +2738,6 @@ def test_jlink_flash_success(self):
self.jlink.memory_write = mock.Mock()

self.jlink.halt = mock.Mock()
self.jlink.halted = mock.Mock()
self.jlink.halted.return_value = True

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

# Halted exception
self.jlink.halted.side_effect = JLinkException(-1)
# Halt exception
Copy link

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment should be "# Halt exception" to match the change from halted to halt, but it should be more descriptive. Consider "# Test halt exception handling" for clarity.

Suggested change
# Halt exception
# Test halt exception handling

Copilot uses AI. Check for mistakes.
self.jlink.halt.side_effect = JLinkException(-1)
self.assertEqual(0, self.jlink.flash([0], 0))
self.jlink.halted.side_effect = None

# Not halted
self.jlink.halted.return_value = False
self.assertEqual(0, self.jlink.flash([0], 0))
self.jlink.halt.assert_called_once()

# Halted
self.jlink.halted.return_value = True
self.assertEqual(0, self.jlink.flash([0], 0))
self.jlink.halt.assert_called_once()
self.jlink.halt.side_effect = None

# Without power by default
self.jlink.power_on = mock.Mock()
Expand Down Expand Up @@ -2802,8 +2781,7 @@ def test_jlink_flash_file_fail_to_flash(self):
"""
self.dll.JLINK_DownloadFile.return_value = -1

self.jlink.halted = mock.Mock()
self.jlink.halted.return_value = True
self.jlink.halt = mock.Mock()

self.jlink.power_on = mock.Mock()
self.jlink.erase = mock.Mock()
Expand All @@ -2822,9 +2800,6 @@ def test_jlink_flash_file_success(self):
"""
self.dll.JLINK_DownloadFile.return_value = 0

self.jlink.halted = mock.Mock()
self.jlink.halted.return_value = True

self.jlink.halt = mock.Mock()
self.jlink.power_on = mock.Mock()
self.jlink.erase = mock.Mock()
Expand All @@ -2840,20 +2815,10 @@ def test_jlink_flash_file_success(self):
self.assertEqual(0, self.jlink.flash_file('path', 0))
self.dll.JLINK_SetFlashProgProgressCallback.assert_called_with(0)

# Halted exception
self.jlink.halted.side_effect = JLinkException(-1)
self.assertEqual(0, self.jlink.flash_file('path', 0))
self.jlink.halted.side_effect = None

# Not halted
self.jlink.halted.return_value = False
self.assertEqual(0, self.jlink.flash_file('path', 0))
self.jlink.halt.assert_called_once()

# Halted
self.jlink.halted.return_value = True
# Halt exception
Copy link

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment should be more descriptive. Consider "# Test halt exception handling" for clarity.

Suggested change
# Halt exception
# Test halt exception handling during flash_file execution

Copilot uses AI. Check for mistakes.
self.jlink.halt.side_effect = JLinkException(-1)
self.assertEqual(0, self.jlink.flash_file('path', 0))
self.jlink.halt.assert_called_once()
self.jlink.halt.side_effect = None

# With power
self.jlink.power_on = mock.Mock()
Expand Down
Loading