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.

90 lines
2.0KB

  1. /*
  2. SD card basic directory list example
  3. This example shows how to create and destroy an SD card file
  4. The circuit:
  5. * SD card attached to SPI bus as follows:
  6. ** MOSI - pin 11, pin 7 on Teensy with audio board
  7. ** MISO - pin 12
  8. ** CLK - pin 13, pin 14 on Teensy with audio board
  9. ** CS - pin 4, pin 10 on Teensy with audio board
  10. created Nov 2010
  11. by David A. Mellis
  12. modified 9 Apr 2012
  13. by Tom Igoe
  14. This example code is in the public domain.
  15. */
  16. #include <SD.h>
  17. // change this to match your SD shield or module;
  18. // Teensy 2.0: pin 0
  19. // Teensy++ 2.0: pin 20
  20. // Wiz820+SD board: pin 4
  21. // Teensy audio board: pin 10
  22. // Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD
  23. const int chipSelect = 10;
  24. void setup()
  25. {
  26. //Uncomment these lines for Teensy 3.x Audio Shield (Rev C)
  27. //SPI.setMOSI(7); // Audio shield has MOSI on pin 7
  28. //SPI.setSCK(14); // Audio shield has SCK on pin 14
  29. // Open serial communications and wait for port to open:
  30. Serial.begin(9600);
  31. while (!Serial) {
  32. ; // wait for serial port to connect.
  33. }
  34. Serial.print("Initializing SD card...");
  35. if (!SD.begin(chipSelect)) {
  36. Serial.println("initialization failed!");
  37. return;
  38. }
  39. Serial.println("initialization done.");
  40. File root = SD.open("/");
  41. printDirectory(root, 0);
  42. Serial.println("done!");
  43. }
  44. void loop()
  45. {
  46. // nothing happens after setup finishes.
  47. }
  48. void printDirectory(File dir, int numSpaces) {
  49. while(true) {
  50. File entry = dir.openNextFile();
  51. if (! entry) {
  52. //Serial.println("** no more files **");
  53. break;
  54. }
  55. printSpaces(numSpaces);
  56. Serial.print(entry.name());
  57. if (entry.isDirectory()) {
  58. Serial.println("/");
  59. printDirectory(entry, numSpaces+2);
  60. } else {
  61. // files have sizes, directories do not
  62. printSpaces(48 - numSpaces - strlen(entry.name()));
  63. Serial.print(" ");
  64. Serial.println(entry.size(), DEC);
  65. }
  66. entry.close();
  67. }
  68. }
  69. void printSpaces(int num) {
  70. for (int i=0; i < num; i++) {
  71. Serial.print(" ");
  72. }
  73. }