Browse Source

Raise exceptions from _send_command

Now the ISPError is raised from the _send_command method, not each
caller.  There will be a few cases where I need that to not be an
exception, but I can just wrap it in a try block then, so there's no
problem.
Clara Hobbs 7 years ago
parent
commit
3e7b54daae
1 changed files with 10 additions and 22 deletions
  1. 10
    22
      alpaca_isp/__init__.py

+ 10
- 22
alpaca_isp/__init__.py View File

@@ -76,6 +76,8 @@ class LPC:
76 76
         rr = self._send_command_raw(cmd)
77 77
         lr = [int(n) for n in rr.split()]
78 78
         lr[0] = ReturnCode(lr[0])
79
+        if lr[0] != ReturnCode.CMD_SUCCESS:
80
+            raise ISPError(lr)
79 81
         return lr
80 82
 
81 83
     def enter_isp(self, delay=0.01):
@@ -127,9 +129,7 @@ class LPC:
127 129
 
128 130
     def unlock(self, code="23130"):
129 131
         """Unlock the flash write, erase, and go commands"""
130
-        r = self._send_command("U {}\r\n".format(code).encode("utf-8"))
131
-        if r[0] != ReturnCode.CMD_SUCCESS:
132
-            raise ISPError(r)
132
+        self._send_command("U {}\r\n".format(code).encode("utf-8"))
133 133
 
134 134
     @property
135 135
     def baudrate(self):
@@ -138,11 +138,9 @@ class LPC:
138 138
 
139 139
     @baudrate.setter
140 140
     def baudrate(self, br):
141
-        r = self._send_command("B {} {}\r\n".format(br,
141
+        self._send_command("B {} {}\r\n".format(br,
142 142
             self._uart.stopbits).encode("utf-8"))
143 143
         # Update the baud rate for our UART
144
-        if r[0] != ReturnCode.CMD_SUCCESS:
145
-            raise ISPError(r)
146 144
         self._uart.baudrate = br
147 145
 
148 146
     @property
@@ -152,11 +150,9 @@ class LPC:
152 150
 
153 151
     @stopbits.setter
154 152
     def stopbits(self, sb):
155
-        r = self._send_command("B {} {}\r\n".format(self._uart.baudrate,
153
+        self._send_command("B {} {}\r\n".format(self._uart.baudrate,
156 154
             sb).encode("utf-8"))
157 155
         # Update the number of stop bits for our UART
158
-        if r[0] != ReturnCode.CMD_SUCCESS:
159
-            raise ISPError(r)
160 156
         self._uart.stopbits = sb
161 157
 
162 158
     @property
@@ -167,9 +163,7 @@ class LPC:
167 163
     @echo.setter
168 164
     def echo(self, setting):
169 165
         setting = bool(setting)
170
-        r = self._send_command("A {}\r\n".format(int(setting)).encode("utf-8"))
171
-        if r[0] != ReturnCode.CMD_SUCCESS:
172
-            raise ISPError(r)
166
+        self._send_command("A {}\r\n".format(int(setting)).encode("utf-8"))
173 167
         self._echo = setting
174 168
 
175 169
     def write_ram(self, start, data, count=None):
@@ -182,13 +176,10 @@ class LPC:
182 176
         if count is None:
183 177
             count = len(data)
184 178
         # Ask to write data
185
-        r = self._send_command("W {} {}\r\n".format(start, count).encode(
186
-            "utf-8"))
187
-        if r[0] != ReturnCode.CMD_SUCCESS:
188
-            raise ISPError(r)
189
-        # If the MCU is okay with what we intend to do, send the data
179
+        self._send_command("W {} {}\r\n".format(start, count).encode("utf-8"))
180
+        # Send the data
190 181
         # NOTE: this is right for LPC8xx chips, not others
191
-        ok = self._writeline(data[:count], plain=True)
182
+        self._writeline(data[:count], plain=True)
192 183
         return
193 184
 
194 185
     def read_memory(self, start, count):
@@ -196,10 +187,7 @@ class LPC:
196 187
 
197 188
         Start and count must be multiples of four.
198 189
         """
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)
190
+        self._send_command("R {} {}\r\n".format(start, count).encode("utf-8"))
203 191
         return self._uart.read(count)
204 192
 
205 193
 

Loading…
Cancel
Save