Browse Source

Implement --verify option

Now Alpaca ISP can verify that the flash was successfully written.  It
does a full comparison of the data, which is perhaps a bit heavy-handed
given that we're mostly worried about random bit errors.  Maybe I'll
eventually implement a CRC32-based verify routine for faster but less
perfect verification.
Clara Hobbs 6 years ago
parent
commit
7acc9904e4
2 changed files with 40 additions and 3 deletions
  1. 12
    3
      alpaca_isp/__init__.py
  2. 28
    0
      alpaca_isp/lpc.py

+ 12
- 3
alpaca_isp/__init__.py View File

@@ -114,23 +114,32 @@ def main():
114 114
         lpc.prepare_write()
115 115
         lpc.erase()
116 116
 
117
-    # Write the files to flash
117
+    # Load the files
118 118
     ih = intelhex.IntelHex()
119 119
     for f in args.file:
120 120
         ih.fromfile(f, format="hex")
121 121
 
122
+    # Write the files to flash
122 123
     try:
123 124
         lpc.flash_hex(ih, verbose=True)
124 125
     except ISPError as e:
125 126
         if e.args[0] == ReturnCode.CODE_READ_PROTECTION_ENABLED:
126 127
             print("Error: code read protection is enabled")
128
+            print("Unable to flash, exiting")
127 129
         sys.exit(2)
128 130
 
129
-    # TODO: Verify that the flash has been written correctly if we've been
130
-    # asked to
131
+    # Verify that the flash has been written correctly if we've been asked to
132
+    if args.verify:
133
+        try:
134
+            lpc.verify(ih, verbose=True)
135
+        except ISPError as e:
136
+            if e.args[0] == ReturnCode.CODE_READ_PROTECTION_ENABLED:
137
+                print("Error: code read protection is enabled")
138
+                print("Unalbe to verify, continuing")
131 139
 
132 140
     # Start the program if we haven't been asked not to
133 141
     if not args.no_start:
142
+        print("Starting program")
134 143
         lpc.go()
135 144
 
136 145
     lpc.close()

+ 28
- 0
alpaca_isp/lpc.py View File

@@ -385,6 +385,34 @@ class LPC:
385 385
             self.copy_ram_to_flash(ss[sector][0], self._chip.ram_base,
386 386
                     len(secdat))
387 387
 
388
+    def verify(self, ihex, verbose=False):
389
+        """Verify that the data in an IntelHex object is stored in flash"""
390
+        # Get the sectors we're verifying
391
+        sectors_used = self._chip.sectors_used(ihex.segments())
392
+
393
+        # Verify segments
394
+        ss = self._chip.sector_segments
395
+        for sector in sectors_used:
396
+            if verbose:
397
+                print("Verifying sector {}... ".format(sector), end="")
398
+            # Get the data we're verifying
399
+            secdat = ihex.tobinstr(start=ss[sector][0], end=ss[sector][1]-1)
400
+
401
+            # Read data from flash
402
+            try:
403
+                flash = self.read_memory(ss[sector][0],
404
+                        self._chip.sectors[sector])
405
+            except ISPError as e:
406
+                if e.args[0] == ReturnCode.CODE_READ_PROTECTION_ENABLED:
407
+                    print()
408
+                raise
409
+
410
+            # Verify the sector
411
+            if secdat != flash:
412
+                print("failed!")
413
+            else:
414
+                print()
415
+
388 416
 
389 417
 class LPC8xx(LPC):
390 418
     """Interface for LPC8xx in-system programming"""

Loading…
Cancel
Save