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
2.0KB

  1. // An example of the SoftSpiDriver template class.
  2. // This example is for an old Adafruit Data Logging Shield on a Mega.
  3. // Software SPI is required on Mega since this shield connects to pins 10-13.
  4. // This example will also run on an Uno and other boards using software SPI.
  5. //
  6. #include "SdFat.h"
  7. #if SPI_DRIVER_SELECT == 2 // Must be set in SdFat/SdFatConfig.h
  8. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  9. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  10. #define SD_FAT_TYPE 0
  11. //
  12. // Chip select may be constant or RAM variable.
  13. const uint8_t SD_CS_PIN = 10;
  14. //
  15. // Pin numbers in templates must be constants.
  16. const uint8_t SOFT_MISO_PIN = 12;
  17. const uint8_t SOFT_MOSI_PIN = 11;
  18. const uint8_t SOFT_SCK_PIN = 13;
  19. // SdFat software SPI template
  20. SoftSpiDriver<SOFT_MISO_PIN, SOFT_MOSI_PIN, SOFT_SCK_PIN> softSpi;
  21. // Speed argument is ignored for software SPI.
  22. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(0), &softSpi)
  23. #if SD_FAT_TYPE == 0
  24. SdFat sd;
  25. File file;
  26. #elif SD_FAT_TYPE == 1
  27. SdFat32 sd;
  28. File32 file;
  29. #elif SD_FAT_TYPE == 2
  30. SdExFat sd;
  31. ExFile file;
  32. #elif SD_FAT_TYPE == 3
  33. SdFs sd;
  34. FsFile file;
  35. #else // SD_FAT_TYPE
  36. #error Invalid SD_FAT_TYPE
  37. #endif // SD_FAT_TYPE
  38. void setup() {
  39. Serial.begin(9600);
  40. // Wait for USB Serial
  41. while (!Serial) {
  42. SysCall::yield();
  43. }
  44. Serial.println("Type any character to start");
  45. while (!Serial.available()) {
  46. SysCall::yield();
  47. }
  48. if (!sd.begin(SD_CONFIG)) {
  49. sd.initErrorHalt();
  50. }
  51. if (!file.open("SoftSPI.txt", O_RDWR | O_CREAT)) {
  52. sd.errorHalt(F("open failed"));
  53. }
  54. file.println(F("This line was printed using software SPI."));
  55. file.rewind();
  56. while (file.available()) {
  57. Serial.write(file.read());
  58. }
  59. file.close();
  60. Serial.println(F("Done."));
  61. }
  62. //------------------------------------------------------------------------------
  63. void loop() {}
  64. #else // SPI_DRIVER_SELECT
  65. #error SPI_DRIVER_SELECT must be two in SdFat/SdFatConfig.h
  66. #endif //SPI_DRIVER_SELECT