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.

136 line
3.6KB

  1. // Test of time-stamp callback with Teensy 3/4.
  2. // The upload time will be used to set the RTC.
  3. // You must arrange for syncing the RTC.
  4. #include "SdFat.h"
  5. #include <TimeLib.h>
  6. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  7. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  8. #define SD_FAT_TYPE 3
  9. /*
  10. Change the value of SD_CS_PIN if you are using SPI and
  11. your hardware does not use the default value, SS.
  12. Common values are:
  13. Arduino Ethernet shield: pin 4
  14. Sparkfun SD shield: pin 8
  15. Adafruit SD shields and modules: pin 10
  16. */
  17. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  18. #ifndef SDCARD_SS_PIN
  19. const uint8_t SD_CS_PIN = SS;
  20. #else // SDCARD_SS_PIN
  21. // Assume built-in SD is used.
  22. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  23. #endif // SDCARD_SS_PIN
  24. // Try to select the best SD card configuration.
  25. #if HAS_SDIO_CLASS
  26. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  27. #elif ENABLE_DEDICATED_SPI
  28. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
  29. #else // HAS_SDIO_CLASS
  30. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
  31. #endif // HAS_SDIO_CLASS
  32. #if SD_FAT_TYPE == 0
  33. SdFat sd;
  34. File file;
  35. #elif SD_FAT_TYPE == 1
  36. SdFat32 sd;
  37. File32 file;
  38. #elif SD_FAT_TYPE == 2
  39. SdExFat sd;
  40. ExFile file;
  41. #elif SD_FAT_TYPE == 3
  42. SdFs sd;
  43. FsFile file;
  44. #else // SD_FAT_TYPE
  45. #error Invalid SD_FAT_TYPE
  46. #endif // SD_FAT_TYPE
  47. //------------------------------------------------------------------------------
  48. // Call back for file timestamps. Only called for file create and sync().
  49. void dateTime(uint16_t* date, uint16_t* time, uint8_t* ms10) {
  50. // Return date using FS_DATE macro to format fields.
  51. *date = FS_DATE(year(), month(), day());
  52. // Return time using FS_TIME macro to format fields.
  53. *time = FS_TIME(hour(), minute(), second());
  54. // Return low time bits in units of 10 ms.
  55. *ms10 = second() & 1 ? 100 : 0;
  56. }
  57. //------------------------------------------------------------------------------
  58. time_t getTeensy3Time()
  59. {
  60. return Teensy3Clock.get();
  61. }
  62. //------------------------------------------------------------------------------
  63. void printField(Print* pr, char sep, uint8_t v) {
  64. if (sep) {
  65. pr->write(sep);
  66. }
  67. if (v < 10) {
  68. pr->write('0');
  69. }
  70. pr->print(v);
  71. }
  72. //------------------------------------------------------------------------------
  73. void printNow(Print* pr) {
  74. pr->print(year());
  75. printField(pr, '-', month());
  76. printField(pr, '-', day());
  77. printField(pr, ' ', hour());
  78. printField(pr, ':', minute());
  79. printField(pr, ':', second());
  80. }
  81. //------------------------------------------------------------------------------
  82. void setup() {
  83. // set the Time library to use Teensy 3.0's RTC to keep time
  84. setSyncProvider(getTeensy3Time);
  85. Serial.begin(9600);
  86. while (!Serial) {
  87. yield();
  88. }
  89. Serial.println(F("Type any character to begin"));
  90. while (!Serial.available()) {
  91. yield();
  92. }
  93. if (timeStatus()!= timeSet) {
  94. Serial.println("Unable to sync with the RTC");
  95. return;
  96. }
  97. Serial.print(F("DateTime::now "));
  98. printNow(&Serial);
  99. Serial.println();
  100. // Set callback
  101. FsDateTime::setCallback(dateTime);
  102. if (!sd.begin(SD_CONFIG)) {
  103. sd.initErrorHalt(&Serial);
  104. }
  105. // Remove old version to set create time.
  106. if (sd.exists("RtcTest.txt")) {
  107. sd.remove("RtcTest.txt");
  108. }
  109. if (!file.open("RtcTest.txt", FILE_WRITE)) {
  110. Serial.println(F("file.open failed"));
  111. return;
  112. }
  113. // Print current date time to file.
  114. file.print(F("Test file at: "));
  115. printNow(&file);
  116. file.println();
  117. file.close();
  118. // List files in SD root.
  119. sd.ls(LS_DATE | LS_SIZE);
  120. Serial.println(F("Done"));
  121. }
  122. //------------------------------------------------------------------------------
  123. void loop() {
  124. }