選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CopyFromSD.ino 3.5KB

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