comparison flash.c @ 69:cf9eb08b8b23

CRC a word at a time rather than the whole block (since we are reading/writing a word at a time).
author Daniel O'Connor <darius@dons.net.au>
date Sun, 14 Apr 2013 22:52:21 +0930
parents dcac5f08f87a
children aaf0603d7f88
comparison
equal deleted inserted replaced
68:cc998b0b2bae 69:cf9eb08b8b23
344 344
345 /* Must be a multiple of 4 due to CRC check */ 345 /* Must be a multiple of 4 due to CRC check */
346 assert(len % 4 == 0); 346 assert(len % 4 == 0);
347 347
348 flashstartread(addr); 348 flashstartread(addr);
349 CRC_ResetDR();
349 for (int i = len; i > 0; i--) { 350 for (int i = len; i > 0; i--) {
350 *data = flashreadbyte(); 351 *data = flashreadbyte();
352 CRC_CalcCRC(*data);
351 data++; 353 data++;
352 } 354 }
353 355
354 flashcrc = flashreadbyte(); 356 flashcrc = flashreadbyte();
355 flashcrc |= flashreadbyte() << 8; 357 flashcrc |= flashreadbyte() << 8;
356 flashcrc |= flashreadbyte() << 16; 358 flashcrc |= flashreadbyte() << 16;
357 flashcrc |= flashreadbyte() << 24; 359 flashcrc |= flashreadbyte() << 24;
358 360
359 flashstopread(); 361 flashstopread();
360 362
361 /* Calculate CRC */ 363 ramcrc = CRC_GetCRC();
362 CRC_ResetDR(); 364
363 ramcrc = CRC_CalcBlockCRC((uint32_t *)_data, len / 4); 365 /* printf("RAM CRC 0x%08x Flash CRC 0x%08x\r\n", (uint)ramcrc, (uint)flashcrc); */
364 366 if (ramcrc == flashcrc)
365 /* printf("RAM CRC 0x%08x Flash CRC 0x%08x\r\n", (uint)ramcrc, (uint)flashcrc); */ 367 return 1;
366 368 else
367 if (ramcrc == flashcrc) 369 return 0;
368 return 1;
369 else
370 return 0;
371 } 370 }
372 371
373 void 372 void
374 flashwriteblock(uint32_t addr, uint32_t len, void *_data) { 373 flashwriteblock(uint32_t addr, uint32_t len, void *_data) {
375 uint16_t *data = _data; 374 uint16_t *data = _data;
390 flashwritestatus(0x00); 389 flashwritestatus(0x00);
391 390
392 /* Erase sector */ 391 /* Erase sector */
393 flash4kerase(addr); 392 flash4kerase(addr);
394 393
394 CRC_ResetDR();
395
395 /* Write data */ 396 /* Write data */
396 for (uint i = 0; i < len / 2; i++) { 397 for (uint i = 0; i < len / 2; i++) {
397 if (i == 0) 398 if (i == 0)
398 flashstartwrite(addr, *data); 399 flashstartwrite(addr, *data);
399 else 400 else
400 flashwriteword(*data); 401 flashwriteword(*data);
402 CRC_CalcCRC(*data);
401 data++; 403 data++;
402 } 404 }
403 405
404 /* Calculate CRC */ 406 /* Calculate CRC */
405 CRC_ResetDR(); 407 crc = CRC_GetCRC();
406 crc = CRC_CalcBlockCRC((uint32_t *)_data, len / 4); 408
407 409 //printf("CRC is 0x%08x\r\n", (uint)crc);
408 printf("CRC is 0x%08x\r\n", (uint)crc);
409 410
410 /* Write CRC */ 411 /* Write CRC */
411 flashwriteword(crc); 412 flashwriteword(crc);
412 flashwriteword(crc >> 16); 413 flashwriteword(crc >> 16);
413 414