CardBusyTest.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "SdFat.h"
  2. #ifdef __AVR__
  3. const uint32_t FILE_SIZE_MiB = 10UL;
  4. #else // __AVR__
  5. const uint32_t FILE_SIZE_MiB = 100UL;
  6. #endif
  7. bool waitBusy = true;
  8. #define SD_CONFIG SdSpiConfig(SS, DEDICATED_SPI)
  9. //#define SD_CONFIG SdSpiConfig(SS, SHARED_SPI)
  10. // Config for Teensy 3.5/3.6 buit-in SD.
  11. //#define SD_CONFIG SdSpiConfig(SDCARD_SS_PIN, DEDICATED_SPI)
  12. //#define SD_CONFIG SdioConfig(FIFO_SDIO)
  13. //------------------------------------------------------------------------------
  14. const uint64_t FILE_SIZE = (uint64_t)FILE_SIZE_MiB << 20;
  15. SdFs sd;
  16. FsFile file;
  17. uint8_t buf[512];
  18. #define error(s) sd.errorHalt(&Serial, F(s))
  19. //------------------------------------------------------------------------------
  20. void clearSerialInput() {
  21. uint32_t m = micros();
  22. do {
  23. if (Serial.read() >= 0) {
  24. m = micros();
  25. }
  26. } while (micros() - m < 10000);
  27. }
  28. //------------------------------------------------------------------------------
  29. void setup() {
  30. Serial.begin(9600);
  31. // Wait for USB Serial
  32. while (!Serial) {
  33. yield();
  34. }
  35. delay(1000);
  36. //------------------------------------------------------------------------------
  37. }
  38. void loop() {
  39. clearSerialInput();
  40. Serial.println(F("\nType any character to start\n"));
  41. while (!Serial.available()) {
  42. yield();
  43. }
  44. // Initialize the SD card.
  45. if (!sd.begin(SD_CONFIG)) {
  46. sd.initErrorHalt();
  47. }
  48. if (!file.open("SdBusyTest.bin", O_RDWR | O_CREAT |O_TRUNC)) {
  49. error("file open failed");
  50. }
  51. if (!file.preAllocate(FILE_SIZE)) {
  52. error("preallocate failed");
  53. }
  54. Serial.print(F("Starting write of "));
  55. Serial.print(FILE_SIZE_MiB);
  56. Serial.println(F(" MiB."));
  57. uint32_t maxWrite = 0;
  58. uint32_t minWrite = 99999999;
  59. uint32_t ms = millis();
  60. uint32_t maxBusy = 0;
  61. uint32_t minBusy = UINT32_MAX;
  62. // Write a dummy sector to start a multi-sector write.
  63. if(file.write(buf, sizeof(buf)) != sizeof(buf)) {
  64. error("write failed for first sector");
  65. }
  66. while (file.position() < FILE_SIZE) {
  67. uint32_t m = micros();
  68. if (waitBusy) {
  69. m = micros();
  70. while (sd.card()->isBusy()) {}
  71. m = micros() - m;
  72. if (m < minBusy) {
  73. minBusy = m;
  74. }
  75. if (m > maxBusy) {
  76. maxBusy = m;
  77. }
  78. }
  79. m = micros();
  80. if (file.write(buf, sizeof(buf)) != sizeof(buf)) {
  81. error("write failed");
  82. }
  83. m = micros() - m;
  84. if (m < minWrite) {
  85. minWrite = m;
  86. }
  87. if (m > maxWrite) {
  88. maxWrite = m;
  89. }
  90. }
  91. file.close();
  92. ms = millis() - ms;
  93. Serial.println(F("\nTimes in micros"));
  94. if (waitBusy) {
  95. Serial.print(F("minBusy: "));
  96. Serial.println(minBusy);
  97. Serial.print(F("maxBusy: "));
  98. Serial.println(maxBusy);
  99. }
  100. Serial.print(F("minWrite: "));
  101. Serial.println(minWrite);
  102. Serial.print(F("maxWrite: "));
  103. Serial.println(maxWrite);
  104. Serial.print(1e-3*ms);
  105. Serial.println(F(" Seconds"));
  106. Serial.print(1.0*FILE_SIZE/ms);
  107. Serial.println(F(" KB/sec"));
  108. }