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
         except ISPError as e:
135
         except ISPError as e:
136
             if e.args[0] == ReturnCode.CODE_READ_PROTECTION_ENABLED:
136
             if e.args[0] == ReturnCode.CODE_READ_PROTECTION_ENABLED:
137
                 print("Error: code read protection is enabled")
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
     # Start the program if we haven't been asked not to
143
     # Start the program if we haven't been asked not to
141
     if not args.no_start:
144
     if not args.no_start:

+ 18
- 4
alpaca_isp/lpc.py View File

245
         self._send_command("C {} {} {}\r\n".format(flash, ram, count).encode(
245
         self._send_command("C {} {} {}\r\n".format(flash, ram, count).encode(
246
             "utf-8"))
246
             "utf-8"))
247
 
247
 
248
-    def go(self, address=0, mode=b"T"):
248
+    def go(self, address=0, mode="T"):
249
         """Jump to the given address, in the given mode of execution
249
         """Jump to the given address, in the given mode of execution
250
 
250
 
251
         Of course, this function generally causes the ISP command handler to
251
         Of course, this function generally causes the ISP command handler to
366
                 self.prepare_write(sector)
366
                 self.prepare_write(sector)
367
                 self.erase(sector)
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
         ss = self._chip.sector_segments
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
             if verbose:
375
             if verbose:
374
                 print("Writing sector {}".format(sector))
376
                 print("Writing sector {}".format(sector))
375
             # Get the data we'll be writing
377
             # Get the data we'll be writing
376
             secdat = ihex.tobinstr(start=ss[sector][0], end=ss[sector][1]-1)
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
             # Write data to RAM
386
             # Write data to RAM
379
             bs = 0x100
387
             bs = 0x100
380
             for i in range(0, len(secdat), bs):
388
             for i in range(0, len(secdat), bs):
408
                 raise
416
                 raise
409
 
417
 
410
             # Verify the sector
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
             if secdat != flash:
424
             if secdat != flash:
412
                 print("failed!")
425
                 print("failed!")
426
+                raise ISPError("Flash verification failed")
413
             else:
427
             else:
414
                 print()
428
                 print()
415
 
429
 

Loading…
Cancel
Save