Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CopyFromSD.ino 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <SerialFlash.h>
  2. #include <SD.h>
  3. #include <SPI.h>
  4. const int SDchipSelect = 4;
  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. while (count < length) {
  64. char buf[256];
  65. unsigned int n;
  66. n = f.read(buf, 256);
  67. ff.write(buf, n);
  68. count = count + n;
  69. Serial.print(".");
  70. }
  71. ff.close();
  72. Serial.println();
  73. } else {
  74. Serial.println(" error opening freshly created file!");
  75. }
  76. } else {
  77. Serial.println(" unable to create file");
  78. }
  79. f.close();
  80. }
  81. rootdir.close();
  82. }
  83. bool compareFiles(File &file, SerialFlashFile &ffile) {
  84. file.seek(0);
  85. ffile.seek(0);
  86. unsigned long count = file.size();
  87. while (count > 0) {
  88. char buf1[128], buf2[128];
  89. unsigned long n = count;
  90. if (n > 128) n = 128;
  91. file.read(buf1, n);
  92. ffile.read(buf2, n);
  93. if (memcmp(buf1, buf2, n) != 0) return false; // differ
  94. count = count - n;
  95. }
  96. return true; // all data identical
  97. }
  98. void loop() {
  99. }
  100. void error(const char *message) {
  101. while (1) {
  102. Serial.println(message);
  103. delay(2500);
  104. }
  105. }