Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

80 lines
2.4KB

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