You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.3KB

  1. #include <SerialFlash.h>
  2. #include <SD.h>
  3. #include <SPI.h>
  4. const int SDchipSelect = 4; // Audio Shield has SD card CS on pin 10
  5. const int FlashChipSelect = 6;
  6. void setup() {
  7. //uncomment these if using Teensy audio shield
  8. //SPI.setSCK(14); // Audio shield has SCK on pin 14
  9. //SPI.setMOSI(7); // Audio shield has MOSI on pin 7
  10. // wait up to 10 seconds for Arduino Serial Monitor
  11. unsigned long startMillis = millis();
  12. while (!Serial && (millis() - startMillis < 10000)) ;
  13. delay(100);
  14. Serial.println("Copy all files from SD Card to SPI Flash");
  15. if (!SD.begin(SDchipSelect)) {
  16. error("Unable to access SD card");
  17. }
  18. if (!SerialFlash.begin()) {
  19. error("Unable to access SPI Flash chip");
  20. }
  21. int count = 0;
  22. File rootdir = SD.open("/");
  23. while (1) {
  24. // open a file from the SD card
  25. Serial.println();
  26. File f = rootdir.openNextFile();
  27. if (!f) break;
  28. const char *filename = f.name();
  29. Serial.print(filename);
  30. Serial.print(" ");
  31. unsigned long length = f.size();
  32. Serial.println(length);
  33. // check if this file is already on the Flash chip
  34. if (SerialFlash.exists(filename)) {
  35. Serial.println(" already exists on the Flash chip");
  36. SerialFlashFile ff = SerialFlash.open(filename);
  37. if (ff && ff.size() == f.size()) {
  38. Serial.println(" size is the same, comparing data...");
  39. if (compareFiles(f, ff) == true) {
  40. Serial.println(" files are identical :)");
  41. f.close();
  42. ff.close();
  43. continue; // advance to next file
  44. } else {
  45. Serial.println(" files are different");
  46. }
  47. } else {
  48. Serial.print(" size is different, ");
  49. Serial.print(ff.size());
  50. Serial.println(" bytes");
  51. }
  52. // delete the copy on the Flash chip, if different
  53. Serial.println(" delete file from Flash chip");
  54. SerialFlash.remove(filename);
  55. }
  56. // create the file on the Flash chip and copy data
  57. if (SerialFlash.create(filename, length)) {
  58. SerialFlashFile ff = SerialFlash.open(filename);
  59. if (ff) {
  60. Serial.print(" copying");
  61. // copy data loop
  62. unsigned long count = 0;
  63. unsigned char dotcount = 9;
  64. while (count < length) {
  65. char buf[256];
  66. unsigned int n;
  67. n = f.read(buf, 256);
  68. ff.write(buf, n);
  69. count = count + n;
  70. Serial.print(".");
  71. if (++dotcount > 100) {
  72. Serial.println();
  73. dotcount = 0;
  74. }
  75. }
  76. ff.close();
  77. if (dotcount > 0) Serial.println();
  78. } else {
  79. Serial.println(" error opening freshly created file!");
  80. }
  81. } else {
  82. Serial.println(" unable to create file");
  83. }
  84. f.close();
  85. }
  86. rootdir.close();
  87. delay(10);
  88. Serial.println("Finished All Files");
  89. }
  90. bool compareFiles(File &file, SerialFlashFile &ffile) {
  91. file.seek(0);
  92. ffile.seek(0);
  93. unsigned long count = file.size();
  94. while (count > 0) {
  95. char buf1[128], buf2[128];
  96. unsigned long n = count;
  97. if (n > 128) n = 128;
  98. file.read(buf1, n);
  99. ffile.read(buf2, n);
  100. if (memcmp(buf1, buf2, n) != 0) return false; // differ
  101. count = count - n;
  102. }
  103. return true; // all data identical
  104. }
  105. void loop() {
  106. }
  107. void error(const char *message) {
  108. while (1) {
  109. Serial.println(message);
  110. delay(2500);
  111. }
  112. }