Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

CopyFromSD.ino 3.6KB

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