Browse Source

Flash LPC8xx chips so they can actually run

Previously, I didn't realize that lpc21isp calculated the checksum in
the interrupt table, so I didn't make Alpaca ISP do that.  This caused a
few hours of confusion, followed by a quick change to make Alpaca ISP
calculate the checksum and insert it into the right part of sector 0.
Now programs flashed with Alpaca ISP actually run, so that's good.

A few other changes have been made as well.  Flash sectors are now
written in the order that lpc21isp uses, first counting up from sector 1
then ending with sector 0.  The first 64 bytes are not compared when
verifying flash.  If verification fails, an exception is raised, and if
the package was run as a program, it exits before launching the code on
the microcontroller.
Clara Hobbs 6 years ago
parent
commit
5f27e82d12
2 changed files with 22 additions and 5 deletions
  1. 4
    1
      alpaca_isp/__init__.py
  2. 18
    4
      alpaca_isp/lpc.py

+ 4
- 1
alpaca_isp/__init__.py View File

@@ -135,7 +135,10 @@ def main():
135 135
         except ISPError as e:
136 136
             if e.args[0] == ReturnCode.CODE_READ_PROTECTION_ENABLED:
137 137
                 print("Error: code read protection is enabled")
138
-                print("Unalbe to verify, continuing")
138
+                print("Unable to verify, continuing")
139
+            else:
140
+                print("Flash verification failed, exiting")
141
+                sys.exit(3)
139 142
 
140 143
     # Start the program if we haven't been asked not to
141 144
     if not args.no_start:

+ 18
- 4
alpaca_isp/lpc.py View File

@@ -245,7 +245,7 @@ class LPC:
245 245
         self._send_command("C {} {} {}\r\n".format(flash, ram, count).encode(
246 246
             "utf-8"))
247 247
 
248
-    def go(self, address=0, mode=b"T"):
248
+    def go(self, address=0, mode="T"):
249 249
         """Jump to the given address, in the given mode of execution
250 250
 
251 251
         Of course, this function generally causes the ISP command handler to
@@ -366,15 +366,23 @@ class LPC:
366 366
                 self.prepare_write(sector)
367 367
                 self.erase(sector)
368 368
 
369
-        # Write sectors in reversed order to make sure sector 0 is invalid
370
-        # until the end
369
+        # Write sectors, with 0 at the end
371 370
         ss = self._chip.sector_segments
372
-        for sector in reversed(sectors_used):
371
+        if 0 in sectors_used:
372
+            sectors_used.remove(0)
373
+            sectors_used.append(0)
374
+        for sector in sectors_used:
373 375
             if verbose:
374 376
                 print("Writing sector {}".format(sector))
375 377
             # Get the data we'll be writing
376 378
             secdat = ihex.tobinstr(start=ss[sector][0], end=ss[sector][1]-1)
377 379
 
380
+            # If we're writing sector 0, set the checksum
381
+            if sector == 0:
382
+                iv = struct.unpack('<7I', secdat[0:28])
383
+                cs = struct.pack('<i', -sum(iv))
384
+                secdat = secdat[:28] + cs + secdat[32:]
385
+
378 386
             # Write data to RAM
379 387
             bs = 0x100
380 388
             for i in range(0, len(secdat), bs):
@@ -408,8 +416,14 @@ class LPC:
408 416
                 raise
409 417
 
410 418
             # Verify the sector
419
+            if sector == 0:
420
+                # Avoid comparing first 64 bytes of sector 0 because these are
421
+                # remapped to flash boot sector
422
+                secdat = secdat[64:]
423
+                flash = flash[64:]
411 424
             if secdat != flash:
412 425
                 print("failed!")
426
+                raise ISPError("Flash verification failed")
413 427
             else:
414 428
                 print()
415 429
 

Loading…
Cancel
Save