TeensyRtcTimestamp.ino 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <TimeLib.h>
  5. #include "SdFat.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 max SPI clock for an SD. Reduce SPI_CLOCK if errors occur.
  25. #define SPI_CLOCK SD_SCK_MHZ(50)
  26. // Try to select the best SD card configuration.
  27. #if HAS_SDIO_CLASS
  28. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  29. #elif ENABLE_DEDICATED_SPI
  30. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_CLOCK)
  31. #else // HAS_SDIO_CLASS
  32. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SPI_CLOCK)
  33. #endif // HAS_SDIO_CLASS
  34. #if SD_FAT_TYPE == 0
  35. SdFat sd;
  36. File file;
  37. #elif SD_FAT_TYPE == 1
  38. SdFat32 sd;
  39. File32 file;
  40. #elif SD_FAT_TYPE == 2
  41. SdExFat sd;
  42. ExFile file;
  43. #elif SD_FAT_TYPE == 3
  44. SdFs sd;
  45. FsFile file;
  46. #else // SD_FAT_TYPE
  47. #error Invalid SD_FAT_TYPE
  48. #endif // SD_FAT_TYPE
  49. //------------------------------------------------------------------------------
  50. // Call back for file timestamps. Only called for file create and sync().
  51. void dateTime(uint16_t* date, uint16_t* time, uint8_t* ms10) {
  52. // Return date using FS_DATE macro to format fields.
  53. *date = FS_DATE(year(), month(), day());
  54. // Return time using FS_TIME macro to format fields.
  55. *time = FS_TIME(hour(), minute(), second());
  56. // Return low time bits in units of 10 ms.
  57. *ms10 = second() & 1 ? 100 : 0;
  58. }
  59. //------------------------------------------------------------------------------
  60. time_t getTeensy3Time() { return Teensy3Clock.get(); }
  61. //------------------------------------------------------------------------------
  62. void printField(Print* pr, char sep, uint8_t v) {
  63. if (sep) {
  64. pr->write(sep);
  65. }
  66. if (v < 10) {
  67. pr->write('0');
  68. }
  69. pr->print(v);
  70. }
  71. //------------------------------------------------------------------------------
  72. void printNow(Print* pr) {
  73. pr->print(year());
  74. printField(pr, '-', month());
  75. printField(pr, '-', day());
  76. printField(pr, ' ', hour());
  77. printField(pr, ':', minute());
  78. printField(pr, ':', second());
  79. }
  80. //------------------------------------------------------------------------------
  81. void setup() {
  82. // set the Time library to use Teensy 3.0's RTC to keep time
  83. setSyncProvider(getTeensy3Time);
  84. Serial.begin(9600);
  85. while (!Serial) {
  86. yield();
  87. }
  88. Serial.println(F("Type any character to begin"));
  89. while (!Serial.available()) {
  90. yield();
  91. }
  92. if (timeStatus() != timeSet) {
  93. Serial.println("Unable to sync with the RTC");
  94. return;
  95. }
  96. Serial.print(F("DateTime::now "));
  97. printNow(&Serial);
  98. Serial.println();
  99. // Set callback
  100. FsDateTime::setCallback(dateTime);
  101. if (!sd.begin(SD_CONFIG)) {
  102. sd.initErrorHalt(&Serial);
  103. }
  104. // Remove old version to set create time.
  105. if (sd.exists("RtcTest.txt")) {
  106. sd.remove("RtcTest.txt");
  107. }
  108. if (!file.open("RtcTest.txt", FILE_WRITE)) {
  109. Serial.println(F("file.open failed"));
  110. return;
  111. }
  112. // Print current date time to file.
  113. file.print(F("Test file at: "));
  114. printNow(&file);
  115. file.println();
  116. file.close();
  117. // List files in SD root.
  118. sd.ls(LS_DATE | LS_SIZE);
  119. Serial.println(F("Done"));
  120. }
  121. //------------------------------------------------------------------------------
  122. void loop() {}