changeset 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
files flash.c flash.h tempctrl.c
diffstat 3 files changed, 33 insertions(+), 8 deletions(-) [+]
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;
 }
--- a/flash.h	Sun Apr 14 22:52:21 2013 +0930
+++ b/flash.h	Sun Apr 14 22:53:50 2013 +0930
@@ -9,7 +9,8 @@
 void		flashwrite(uint32_t addr, uint8_t data);
 void		flashwait(void);
 int		flashreadblock(uint32_t addr, uint32_t len, void *_data);
-void		flashwriteblock(uint32_t addr, uint32_t len, void *_data);
+int		flashwriteblock(uint32_t addr, uint32_t len, void *_data);
+uint32_t	flashcrcblock(uint32_t addr, uint32_t len);
 
 /* Streaming read/write */
 void		flashstartread(uint32_t addr);
--- a/tempctrl.c	Sun Apr 14 22:52:21 2013 +0930
+++ b/tempctrl.c	Sun Apr 14 22:53:50 2013 +0930
@@ -120,7 +120,7 @@
 /* Local function prototypes */
 static void		tempctrl_load_or_init_settings(void);
 static void		tempctrl_default_settings(void);
-static void		tempctrl_write_settings(void);
+static int		tempctrl_write_settings(void);
 static void		setstate(char state);
 static const char *	state2long(char s);
 static int		fmttemp(char *buf, const char *name, int tmp, const char *trailer);
@@ -342,7 +342,8 @@
     if (!flashreadblock(TEMPCTRL_FLASH_ADDRESS, sizeof(tc_settings), &tc_settings)) {
 	fputs("CRC fails, loading defaults\n", stdout);
 	tempctrl_default_settings();
-	tempctrl_write_settings();
+	if (tempctrl_write_settings())
+	    fputs("Failed to write settings\n", stdout);
     }
 }
 
@@ -353,9 +354,9 @@
 }
 
 /* Write the current settings out to SPI flash */
-static void
+static int
 tempctrl_write_settings(void) {
-    flashwriteblock(TEMPCTRL_FLASH_ADDRESS, sizeof(tc_settings), &tc_settings);
+    return flashwriteblock(TEMPCTRL_FLASH_ADDRESS, sizeof(tc_settings), &tc_settings);
 }
 
 /* Set the relays to match the desired state */
@@ -440,7 +441,8 @@
     }
 	
     if (!strcasecmp(argv[0], "save")) {
-	tempctrl_write_settings();
+	if (tempctrl_write_settings())
+	    fputs("Failed to write settings\n", stdout);
 	return;
     }
     if (!strcasecmp(argv[0], "load")) {