Browse Source

Implement remaining commands

J, K, and N were implemented as read-only properties.
Clara Hobbs 6 years ago
parent
commit
f1213c462e
1 changed files with 52 additions and 2 deletions
  1. 52
    2
      alpaca_isp/__init__.py

+ 52
- 2
alpaca_isp/__init__.py View File

@@ -13,6 +13,7 @@
13 13
 # limitations under the License.
14 14
 
15 15
 from enum import Enum
16
+import struct
16 17
 import time
17 18
 
18 19
 import serial
@@ -244,11 +245,60 @@ class LPC:
244 245
         except ISPError as e:
245 246
             # Return a tuple for SECTOR_NOT_BLANK
246 247
             if e.args[0][0] == ReturnCode.SECTOR_NOT_BLANK:
247
-                offset = int(self._readline().strip())
248
-                value = int(self._readline().strip())
248
+                offset = int(self._readline())
249
+                value = int(self._readline())
249 250
                 return (offset, value)
250 251
             raise
251 252
 
253
+    @property
254
+    def part_id(self):
255
+        """The identification number for the part"""
256
+        self._send_command(b"J\r\n")
257
+        return int(self._readline())
258
+
259
+    @property
260
+    def boot_code_version(self):
261
+        """The boot code version number (major, minor)"""
262
+        self._send_command(b"K\r\n")
263
+        major = int(self._readline())
264
+        minor = int(self._readline())
265
+        return (major, minor)
266
+
267
+    def compare(self, addr1, addr2, count):
268
+        """Compart count bytes starting from the two addresses
269
+
270
+        Both addresses should be on word boundaries, and count should be a
271
+        multiple of four.
272
+
273
+        Returns None if the two blocks are equal, or the byte offset of the
274
+        first mismatched word if they are not.
275
+        """
276
+        try:
277
+            self._send_command("M {} {} {}\r\n".format(addr1, addr2,
278
+                count).encode("utf-8"))
279
+        except ISPError as e:
280
+            # Return an offset for COMPARE_ERROR
281
+            if e.args[0][0] == ReturnCode.COMPARE_ERROR:
282
+                return int(self._readline())
283
+            raise
284
+
285
+    @property
286
+    def uid(self):
287
+        """The microcontroller's unique ID, as bytes"""
288
+        self._send_command(b"N\r\n")
289
+        words = []
290
+        for _ in range(4):
291
+            words.append(int(self._readline()))
292
+        return struct.pack("<4I", *words)
293
+
294
+    def read_crc32(self, start, count):
295
+        """Compute the CRC checksum of a black of RAM or flash
296
+
297
+        Start must be on a word boundary, and count must be a multiple of four.
298
+        """
299
+        self._send_command("S {} {}\r\n".format(start, count).encode("utf-8"))
300
+        return int(self._readline())
301
+
252 302
 
253 303
 class ReturnCode(Enum):
254 304
     """LPC ISP return codes

Loading…
Cancel
Save