Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CopyFromSD.ino 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. File rootdir = SD.open("/");
  28. while (1) {
  29. // open a file from the SD card
  30. Serial.println();
  31. File f = rootdir.openNextFile();
  32. if (!f) break;
  33. const char *filename = f.name();
  34. Serial.print(filename);
  35. Serial.print(" ");
  36. unsigned long length = f.size();
  37. Serial.println(length);
  38. // check if this file is already on the Flash chip
  39. if (SerialFlash.exists(filename)) {
  40. Serial.println(" already exists on the Flash chip");
  41. SerialFlashFile ff = SerialFlash.open(filename);
  42. if (ff && ff.size() == f.size()) {
  43. Serial.println(" size is the same, comparing data...");
  44. if (compareFiles(f, ff) == true) {
  45. Serial.println(" files are identical :)");
  46. f.close();
  47. ff.close();
  48. continue; // advance to next file
  49. } else {
  50. Serial.println(" files are different");
  51. }
  52. } else {
  53. Serial.print(" size is different, ");
  54. Serial.print(ff.size());
  55. Serial.println(" bytes");
  56. }
  57. // delete the copy on the Flash chip, if different
  58. Serial.println(" delete file from Flash chip");
  59. SerialFlash.remove(filename);
  60. }
  61. // create the file on the Flash chip and copy data
  62. if (SerialFlash.create(filename, length)) {
  63. SerialFlashFile ff = SerialFlash.open(filename);
  64. if (ff) {
  65. Serial.print(" copying");
  66. // copy data loop
  67. unsigned long count = 0;
  68. unsigned char dotcount = 9;
  69. while (count < length) {
  70. char buf[256];
  71. unsigned int n;
  72. n = f.read(buf, 256);
  73. ff.write(buf, n);
  74. count = count + n;
  75. Serial.print(".");
  76. if (++dotcount > 100) {
  77. Serial.println();
  78. dotcount = 0;
  79. }
  80. }
  81. ff.close();
  82. if (dotcount > 0) Serial.println();
  83. } else {
  84. Serial.println(" error opening freshly created file!");
  85. }
  86. } else {
  87. Serial.println(" unable to create file");
  88. }
  89. f.close();
  90. }
  91. rootdir.close();
  92. delay(10);
  93. Serial.println("Finished All Files");
  94. }
  95. bool compareFiles(File &file, SerialFlashFile &ffile) {
  96. file.seek(0);
  97. ffile.seek(0);
  98. unsigned long count = file.size();
  99. while (count > 0) {
  100. char buf1[128], buf2[128];
  101. unsigned long n = count;
  102. if (n > 128) n = 128;
  103. file.read(buf1, n);
  104. ffile.read(buf2, n);
  105. if (memcmp(buf1, buf2, n) != 0) return false; // differ
  106. count = count - n;
  107. }
  108. return true; // all data identical
  109. }
  110. void loop() {
  111. }
  112. void error(const char *message) {
  113. while (1) {
  114. Serial.println(message);
  115. delay(2500);
  116. }
  117. }