You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.9KB

  1. // This stress test will create and write files until the SD is full.
  2. #include <SPI.h>
  3. #include <SdFat.h>
  4. // SD chip select pin.
  5. const uint8_t SD_CS_PIN = SS;
  6. // Set write buffer size.
  7. #ifdef __arm__
  8. #ifndef CORE_TEENSY
  9. // Due
  10. const size_t BUF_SIZE = 32768;
  11. #else // CORE_TEENSY
  12. // Teensy 3.0
  13. const size_t BUF_SIZE = 8192;
  14. #endif // CORE_TEENSY
  15. #elif defined(RAMEND) && RAMEND > 5000
  16. // AVR with more than 4 KB RAM
  17. const size_t BUF_SIZE = 4096;
  18. #else // __arm__
  19. // other
  20. const size_t BUF_SIZE = 512;
  21. #endif // __arm__
  22. const size_t FILE_SIZE_KB = 10240;
  23. const uint16_t BUFS_PER_FILE = (1024L*FILE_SIZE_KB/BUF_SIZE);
  24. SdFat sd;
  25. SdFile file;
  26. uint8_t buf[BUF_SIZE];
  27. char name[13];
  28. //------------------------------------------------------------------------------
  29. void setup() {
  30. Serial.begin(9600);
  31. Serial.print("BUF_SIZE ");
  32. Serial.println(BUF_SIZE);
  33. Serial.println("Type any character to start");
  34. while (Serial.read() < 0) {}
  35. if (!sd.begin(SD_CS_PIN))sd.errorHalt("sd.begin");
  36. // Fill buf with known value.
  37. for (size_t i = 0; i < BUF_SIZE; i++) buf[i] = i;
  38. // Wait to begin.
  39. do {delay(10);} while (Serial.read() >= 0);
  40. Serial.println("Type any character to stop after next file");
  41. }
  42. //------------------------------------------------------------------------------
  43. void loop() {
  44. // Free KB on SD.
  45. uint32_t freeKB = sd.vol()->freeClusterCount()*sd.vol()->blocksPerCluster()/2;
  46. Serial.print("Free KB: ");
  47. Serial.println(freeKB);
  48. if (freeKB < 2*FILE_SIZE_KB) {
  49. Serial.println(" Done!");
  50. while(1);
  51. }
  52. sprintf(name, "%lu.DAT", freeKB);
  53. if (!file.open(name, O_WRITE | O_CREAT | O_TRUNC)) {
  54. sd.errorHalt("Open error!");
  55. }
  56. for (uint16_t i = 0; i < BUFS_PER_FILE; i++) {
  57. if (file.write(buf, BUF_SIZE) != BUF_SIZE) {
  58. sd.errorHalt("Write error!");
  59. }
  60. }
  61. file.close();
  62. if (Serial.available()) {
  63. Serial.println("Stopped!");
  64. while(1);
  65. }
  66. }