diff flash.c @ 70:aaf0603d7f88

Add routine to CRC a block of flash. Use it to verify a flash block write.
author Daniel O'Connor <darius@dons.net.au>
date Sun, 14 Apr 2013 22:53:50 +0930
parents cf9eb08b8b23
children 778adff8b569
line wrap: on
line diff
--- a/flash.c	Sun Apr 14 22:52:21 2013 +0930
+++ b/flash.c	Sun Apr 14 22:53:50 2013 +0930
@@ -369,10 +369,25 @@
 	return 0;
 }
 
-void
+uint32_t
+flashcrcblock(uint32_t addr, uint32_t len) {
+    assert(len % 4 == 0);
+
+   CRC_ResetDR();
+
+    flashstartread(addr);
+    for (int i = len; i > 0; i--)
+	CRC_CalcCRC(flashreadbyte());
+
+    flashstopread();
+
+    return CRC_GetCRC();
+}
+
+int
 flashwriteblock(uint32_t addr, uint32_t len, void *_data) {
     uint16_t	*data = _data;
-    uint32_t	crc;
+    uint32_t	crc, vcrc;
     
     printf("Writing %u bytes to 0x%06x\r\n", (uint)len, (uint)addr);
     
@@ -413,4 +428,11 @@
    flashwriteword(crc >> 16);
    
    flashstopwrite();
+
+   /* Read back and check CRC */
+   vcrc = flashcrcblock(addr, len);
+   if (vcrc != crc)
+       return 1;
+   else
+       return 0;
 }