Browse Source

Exceptions instead of return codes, implement R

Now instead of returning codes, the methods of LPC raise exceptions with
the information the chip returned.

The new read_memory method implements Python bindings for the R command.
Clara Hobbs 7 years ago
parent
commit
b98e9a3194
1 changed files with 19 additions and 5 deletions
  1. 19
    5
      alpaca_isp/__init__.py

+ 19
- 5
alpaca_isp/__init__.py View File

@@ -127,7 +127,9 @@ class LPC:
127 127
 
128 128
     def unlock(self, code="23130"):
129 129
         """Unlock the flash write, erase, and go commands"""
130
-        return self._send_command("U {}\r\n".format(code).encode("utf-8"))
130
+        r = self._send_command("U {}\r\n".format(code).encode("utf-8"))
131
+        if r[0] != ReturnCode.CMD_SUCCESS:
132
+            raise ISPError(r)
131 133
 
132 134
     @property
133 135
     def baudrate(self):
@@ -182,11 +184,23 @@ class LPC:
182 184
         # Ask to write data
183 185
         r = self._send_command("W {} {}\r\n".format(start, count).encode(
184 186
             "utf-8"))
187
+        if r[0] != ReturnCode.CMD_SUCCESS:
188
+            raise ISPError(r)
185 189
         # If the MCU is okay with what we intend to do, send the data
186
-        if r[0] == ReturnCode.CMD_SUCCESS:
187
-            # NOTE: this is right for LPC8xx chips, not others
188
-            ok = self._writeline(data[:count], plain=True)
189
-        return r
190
+        # NOTE: this is right for LPC8xx chips, not others
191
+        ok = self._writeline(data[:count], plain=True)
192
+        return
193
+
194
+    def read_memory(self, start, count):
195
+        """Read count bytes starting at the given address
196
+
197
+        Start and count must be multiples of four.
198
+        """
199
+        r = self._send_command("R {} {}\r\n".format(start, count).encode(
200
+            "utf-8"))
201
+        if r[0] != ReturnCode.CMD_SUCCESS:
202
+            raise ISPError(r)
203
+        return self._uart.read(count)
190 204
 
191 205
 
192 206
 class ReturnCode(Enum):

Loading…
Cancel
Save