Skip to content

Commit 18eb8aa

Browse files
committed
daplink_flash: Address PR #173 review comments.
1 parent c4cbf56 commit 18eb8aa

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

lib/daplink_flash/daplink_flash/device.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ def clear_config(self):
182182
self._write_cmd(CMD_CLEAR_CONFIG)
183183
sleep_ms(100)
184184
self._wait_busy()
185+
err = self._error()
186+
if err:
187+
raise OSError("DAPLink config clear error: 0x{:02X}".format(err))
185188

186189
def write_config(self, data, offset=0):
187190
"""Write data to the config zone at the given offset.
@@ -196,12 +199,18 @@ def write_config(self, data, offset=0):
196199
if isinstance(data, str):
197200
data = data.encode()
198201
length = len(data)
202+
if offset < 0 or offset >= CONFIG_SIZE:
203+
raise ValueError("offset out of range: {}".format(offset))
204+
if offset + length > CONFIG_SIZE:
205+
raise ValueError("data exceeds config zone boundary")
206+
# Frame: cmd(1) + offset(2) + len(1) + data(N) + padding
199207
buf = bytearray(MAX_WRITE_CHUNK + 2)
208+
max_payload = len(buf) - 4
200209
buf[0] = CMD_WRITE_CONFIG
201210
pos = 0
202211
while pos < length:
203212
self._wait_busy()
204-
chunk_len = min(MAX_WRITE_CHUNK - 3, length - pos)
213+
chunk_len = min(max_payload, length - pos)
205214
cur_offset = offset + pos
206215
buf[1] = (cur_offset >> 8) & 0xFF
207216
buf[2] = cur_offset & 0xFF
@@ -213,6 +222,9 @@ def write_config(self, data, offset=0):
213222
sleep_ms(50)
214223
pos += chunk_len
215224
self._wait_busy()
225+
err = self._error()
226+
if err:
227+
raise OSError("DAPLink config write error: 0x{:02X}".format(err))
216228

217229
def read_config(self):
218230
"""Read config zone content.

lib/daplink_flash/examples/config_zone.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def read_config(offset=0):
5454

5555

5656
def write_config(offset, data):
57+
if len(data) > 28:
58+
raise ValueError("data too long (max 28 bytes per frame)")
5759
wait_busy()
5860
payload = bytearray([0x30, offset >> 8, offset & 0xFF, len(data)]) + data
5961
while len(payload) < 32:

tests/scenarios/daplink_flash.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ tests:
321321
action: hardware_script
322322
script: |
323323
from time import sleep_ms
324+
dev.clear_config()
324325
dev.write_config("preserved")
325326
dev.clear_flash()
326327
sleep_ms(500)
@@ -332,6 +333,7 @@ tests:
332333
action: hardware_script
333334
script: |
334335
from time import sleep_ms
336+
dev.clear_config()
335337
dev.write_config('{"cal": true}')
336338
dev.set_filename("TEST", "CSV")
337339
dev.clear_flash()

0 commit comments

Comments
 (0)