SoftwareSpi.ino 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #if ENABLE_DEDICATED_SPI
  23. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(0), &softSpi)
  24. #else // ENABLE_DEDICATED_SPI
  25. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SD_SCK_MHZ(0), &softSpi)
  26. #endif // ENABLE_DEDICATED_SPI
  27. #if SD_FAT_TYPE == 0
  28. SdFat sd;
  29. File file;
  30. #elif SD_FAT_TYPE == 1
  31. SdFat32 sd;
  32. File32 file;
  33. #elif SD_FAT_TYPE == 2
  34. SdExFat sd;
  35. ExFile file;
  36. #elif SD_FAT_TYPE == 3
  37. SdFs sd;
  38. FsFile file;
  39. #else // SD_FAT_TYPE
  40. #error Invalid SD_FAT_TYPE
  41. #endif // SD_FAT_TYPE
  42. void setup() {
  43. Serial.begin(9600);
  44. // Wait for USB Serial
  45. while (!Serial) {
  46. yield();
  47. }
  48. Serial.println("Type any character to start");
  49. while (!Serial.available()) {
  50. yield();
  51. }
  52. if (!sd.begin(SD_CONFIG)) {
  53. sd.initErrorHalt();
  54. }
  55. if (!file.open("SoftSPI.txt", O_RDWR | O_CREAT)) {
  56. sd.errorHalt(F("open failed"));
  57. }
  58. file.println(F("This line was printed using software SPI."));
  59. file.rewind();
  60. while (file.available()) {
  61. Serial.write(file.read());
  62. }
  63. file.close();
  64. Serial.println(F("Done."));
  65. }
  66. //------------------------------------------------------------------------------
  67. void loop() {}
  68. #else // SPI_DRIVER_SELECT
  69. #error SPI_DRIVER_SELECT must be two in SdFat/SdFatConfig.h
  70. #endif // SPI_DRIVER_SELECT