Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CopyFromSD.ino 3.7KB

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