VolumeFreeSpace.ino 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * This program demonstrates the freeClusterCount() call.
  3. */
  4. #include <SPI.h>
  5. #include "SdFat.h"
  6. #include "sdios.h"
  7. /*
  8. * SD chip select pin. Common values are:
  9. *
  10. * Arduino Ethernet shield, pin 4.
  11. * SparkFun SD shield, pin 8.
  12. * Adafruit Datalogging shield, pin 10.
  13. * Default SD chip select is the SPI SS pin.
  14. */
  15. const uint8_t chipSelect = SS;
  16. #define TEST_FILE "Cluster.test"
  17. // file system
  18. SdFat sd;
  19. // test file
  20. SdFile file;
  21. // Serial output stream
  22. ArduinoOutStream cout(Serial);
  23. //------------------------------------------------------------------------------
  24. void printFreeSpace() {
  25. cout << F("freeClusterCount() call time: ");
  26. uint32_t m = micros();
  27. uint32_t volFree = sd.vol()->freeClusterCount();
  28. cout << micros() - m << F(" micros\n");
  29. cout << F("freeClusters: ") << volFree << setprecision(3) << endl;
  30. float fs = 0.000512*volFree*sd.vol()->blocksPerCluster();
  31. cout << F("freeSpace: ") << fs << F(" MB (MB = 1,000,000 bytes)\n\n");
  32. }
  33. //------------------------------------------------------------------------------
  34. void setup() {
  35. Serial.begin(9600);
  36. // Wait for USB Serial
  37. while (!Serial) {
  38. yield();
  39. }
  40. if (!MAINTAIN_FREE_CLUSTER_COUNT) {
  41. cout << F("Please edit SdFatConfig.h and set\n");
  42. cout << F("MAINTAIN_FREE_CLUSTER_COUNT nonzero for\n");
  43. cout << F("maximum freeClusterCount() performance.\n\n");
  44. }
  45. // F stores strings in flash to save RAM
  46. cout << F("Type any character to start\n");
  47. while (!Serial.available()) {
  48. yield();
  49. }
  50. // Initialize at the highest speed supported by the board that is
  51. // not over 50 MHz. Try a lower speed if SPI errors occur.
  52. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  53. sd.initErrorHalt();
  54. }
  55. // Insure no TEST_FILE.
  56. sd.remove(TEST_FILE);
  57. cout << F("\nFirst call to freeClusterCount scans the FAT.\n\n");
  58. printFreeSpace();
  59. cout << F("Create and write to ") << TEST_FILE << endl;
  60. if (!file.open(TEST_FILE, O_WRONLY | O_CREAT)) {
  61. sd.errorHalt(F("Create failed"));
  62. }
  63. file.print(F("Cause a cluster to be allocated"));
  64. file.close();
  65. cout << F("\nSecond freeClusterCount call is faster if\n");
  66. cout << F("MAINTAIN_FREE_CLUSTER_COUNT is nonzero.\n\n");
  67. printFreeSpace();
  68. cout << F("Remove ") << TEST_FILE << endl << endl;
  69. sd.remove(TEST_FILE);
  70. printFreeSpace();
  71. cout << F("Done") << endl;
  72. }
  73. //------------------------------------------------------------------------------
  74. void loop() {}