@@ -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.
0 commit comments