Skip to content

Commit

Permalink
Switch to read-modify-write flashing.
Browse files Browse the repository at this point in the history
This enables programming the chip without having to erase the entire contents.
Instead, JLink will erase where needed while retaining the data that's within
the same sectors but outside of the specified write area.

Closes square#154, square#148
  • Loading branch information
denravonska committed Nov 25, 2022
1 parent 5cbf339 commit 1147466
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
13 changes: 11 additions & 2 deletions pylink/jlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -2192,9 +2192,18 @@ def flash(self, data, addr, on_progress=None, power_on=False, flags=0):
except errors.JLinkException:
pass

res = self.flash_write(addr, data, flags=flags)
# Perform read-modify-write operation.
res = self._dll.JLINKARM_BeginDownload(flags=flags)
if res < 0:
raise errors.JLinkEraseException(res)

return res
bytes_flashed = self._dll.JLINKARM_WriteMem(addr, len(data), data)

res = self._dll.JLINKARM_EndDownload()
if res < 0:
raise errors.JLinkEraseException(res)

return bytes_flashed

@connection_required
def flash_file(self, path, addr, on_progress=None, power_on=False):
Expand Down
5 changes: 4 additions & 1 deletion pylink/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ class Library(object):
'JLINK_SWD_StoreRaw',
'JLINK_SWD_SyncBits',
'JLINK_SWD_SyncBytes',
'JLINK_SetFlashProgProgressCallback'
'JLINK_SetFlashProgProgressCallback',
'JLINKARM_BeginDownload',
'JLINKARM_EndDownload',
'JLINKARM_WriteMem'
]

# On Linux and macOS, represents the library file name prefix
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/test_jlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -2715,7 +2715,7 @@ def test_jlink_flash_fail_to_flash(self):
Returns:
``None``
"""
self.dll.JLINKARM_EndDownload.return_value = -1
self.dll.JLINKARM_WriteMem.return_value = 0

self.jlink.power_on = mock.Mock()
self.jlink.erase = mock.Mock()
Expand All @@ -2724,6 +2724,15 @@ def test_jlink_flash_fail_to_flash(self):
self.jlink.halted = mock.Mock()
self.jlink.halted.return_value = True

# BeginDownload failing
self.dll.JLINKARM_BeginDownload.return_value = -1
self.dll.JLINKARM_EndDownload.return_value = 0
with self.assertRaises(JLinkException):
self.jlink.flash([0], 0)

# EndDownload failing
self.dll.JLINKARM_BeginDownload.return_value = 0
self.dll.JLINKARM_EndDownload.return_value = -1
with self.assertRaises(JLinkException):
self.jlink.flash([0], 0)

Expand All @@ -2736,6 +2745,8 @@ def test_jlink_flash_success(self):
Returns:
``None``
"""
self.dll.JLINKARM_BeginDownload.return_value = 0
self.dll.JLINKARM_WriteMem.return_value = 0
self.dll.JLINKARM_EndDownload.return_value = 0

self.jlink.power_on = mock.Mock()
Expand Down

0 comments on commit 1147466

Please sign in to comment.