# HG changeset patch # User Daniel O'Connor # Date 1365945830 -34200 # Node ID aaf0603d7f88e468be0557b59f7b61068ef8a952 # Parent cf9eb08b8b23d1a0f8f42c8e9539bc74847e8fd8 Add routine to CRC a block of flash. Use it to verify a flash block write. diff -r cf9eb08b8b23 -r aaf0603d7f88 flash.c --- 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; } diff -r cf9eb08b8b23 -r aaf0603d7f88 flash.h --- 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); diff -r cf9eb08b8b23 -r aaf0603d7f88 tempctrl.c --- 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")) {