ExFatLogger.ino 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // Example to demonstrate write latency for preallocated exFAT files.
  2. // I suggest you write a PC program to convert very large bin files.
  3. //
  4. // If an exFAT SD is required, the ExFatFormatter example will format
  5. // smaller cards with an exFAT file system.
  6. //
  7. // The maximum data rate will depend on the quality of your SD,
  8. // the size of the FIFO, and using dedicated SPI.
  9. #include "SdFat.h"
  10. #include "FreeStack.h"
  11. #include "ExFatLogger.h"
  12. //------------------------------------------------------------------------------
  13. // This example was designed for exFAT but will support FAT16/FAT32.
  14. // Note: Uno will not support SD_FAT_TYPE = 3.
  15. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  16. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  17. #define SD_FAT_TYPE 2
  18. //------------------------------------------------------------------------------
  19. // Interval between data records in microseconds.
  20. // Try 250 with Teensy 3.6, Due, or STM32.
  21. // Try 2000 with AVR boards.
  22. // Try 4000 with SAMD Zero boards.
  23. const uint32_t LOG_INTERVAL_USEC = 2000;
  24. // Set USE_RTC nonzero for file timestamps.
  25. // RAM use will be marginal on Uno with RTClib.
  26. // 0 - RTC not used
  27. // 1 - DS1307
  28. // 2 - DS3231
  29. // 3 - PCF8523
  30. #define USE_RTC 0
  31. #if USE_RTC
  32. #include "RTClib.h"
  33. #endif // USE_RTC
  34. // LED to light if overruns occur.
  35. #define ERROR_LED_PIN -1
  36. /*
  37. Change the value of SD_CS_PIN if you are using SPI and
  38. your hardware does not use the default value, SS.
  39. Common values are:
  40. Arduino Ethernet shield: pin 4
  41. Sparkfun SD shield: pin 8
  42. Adafruit SD shields and modules: pin 10
  43. */
  44. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  45. #ifndef SDCARD_SS_PIN
  46. const uint8_t SD_CS_PIN = SS;
  47. #else // SDCARD_SS_PIN
  48. // Assume built-in SD is used.
  49. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  50. #endif // SDCARD_SS_PIN
  51. // FIFO SIZE - 512 byte sectors. Modify for your board.
  52. #ifdef __AVR_ATmega328P__
  53. // Use 512 bytes for 328 boards.
  54. #define FIFO_SIZE_SECTORS 1
  55. #elif defined(__AVR__)
  56. // Use 2 KiB for other AVR boards.
  57. #define FIFO_SIZE_SECTORS 4
  58. #else // __AVR_ATmega328P__
  59. // Use 8 KiB for non-AVR boards.
  60. #define FIFO_SIZE_SECTORS 16
  61. #endif // __AVR_ATmega328P__
  62. // Preallocate 1GiB file.
  63. const uint32_t PREALLOCATE_SIZE_MiB = 1024UL;
  64. // Select the fastest interface. Assumes no other SPI devices.
  65. #if ENABLE_DEDICATED_SPI
  66. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
  67. #else // ENABLE_DEDICATED_SPI
  68. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
  69. #endif // ENABLE_DEDICATED_SPI
  70. // Save SRAM if 328.
  71. #ifdef __AVR_ATmega328P__
  72. #include "MinimumSerial.h"
  73. MinimumSerial MinSerial;
  74. #define Serial MinSerial
  75. #endif // __AVR_ATmega328P__
  76. //==============================================================================
  77. // Replace logRecord(), printRecord(), and ExFatLogger.h for your sensors.
  78. void logRecord(data_t* data, uint16_t overrun) {
  79. if (overrun) {
  80. // Add one since this record has no adc data. Could add overrun field.
  81. overrun++;
  82. data->adc[0] = 0X8000 | overrun;
  83. } else {
  84. for (size_t i = 0; i < ADC_COUNT; i++) {
  85. data->adc[i] = analogRead(i);
  86. }
  87. }
  88. }
  89. //------------------------------------------------------------------------------
  90. void printRecord(Print* pr, data_t* data) {
  91. static uint32_t nr = 0;
  92. if (!data) {
  93. pr->print(F("LOG_INTERVAL_USEC,"));
  94. pr->println(LOG_INTERVAL_USEC);
  95. pr->print(F("rec#"));
  96. for (size_t i = 0; i < ADC_COUNT; i++) {
  97. pr->print(F(",adc"));
  98. pr->print(i);
  99. }
  100. pr->println();
  101. nr = 0;
  102. return;
  103. }
  104. if (data->adc[0] & 0X8000) {
  105. uint16_t n = data->adc[0] & 0X7FFF;
  106. nr += n;
  107. pr->print(F("-1,"));
  108. pr->print(n);
  109. pr->println(F(",overuns"));
  110. } else {
  111. pr->print(nr++);
  112. for (size_t i = 0; i < ADC_COUNT; i++) {
  113. pr->write(',');
  114. pr->print(data->adc[i]);
  115. }
  116. pr->println();
  117. }
  118. }
  119. //==============================================================================
  120. const uint64_t PREALLOCATE_SIZE = (uint64_t)PREALLOCATE_SIZE_MiB << 20;
  121. // Max length of file name including zero byte.
  122. #define FILE_NAME_DIM 40
  123. // Max number of records to buffer while SD is busy.
  124. const size_t FIFO_DIM = 512*FIFO_SIZE_SECTORS/sizeof(data_t);
  125. #if SD_FAT_TYPE == 0
  126. typedef SdFat sd_t;
  127. typedef File file_t;
  128. #elif SD_FAT_TYPE == 1
  129. typedef SdFat32 sd_t;
  130. typedef File32 file_t;
  131. #elif SD_FAT_TYPE == 2
  132. typedef SdExFat sd_t;
  133. typedef ExFile file_t;
  134. #elif SD_FAT_TYPE == 3
  135. typedef SdFs sd_t;
  136. typedef FsFile file_t;
  137. #else // SD_FAT_TYPE
  138. #error Invalid SD_FAT_TYPE
  139. #endif // SD_FAT_TYPE
  140. sd_t sd;
  141. file_t binFile;
  142. file_t csvFile;
  143. // You may modify the filename. Digits before the dot are file versions.
  144. char binName[] = "ExFatLogger00.bin";
  145. //------------------------------------------------------------------------------
  146. #if USE_RTC
  147. #if USE_RTC == 1
  148. RTC_DS1307 rtc;
  149. #elif USE_RTC == 2
  150. RTC_DS3231 rtc;
  151. #elif USE_RTC == 3
  152. RTC_PCF8523 rtc;
  153. #else // USE_RTC == type
  154. #error USE_RTC type not implemented.
  155. #endif // USE_RTC == type
  156. // Call back for file timestamps. Only called for file create and sync().
  157. void dateTime(uint16_t* date, uint16_t* time, uint8_t* ms10) {
  158. DateTime now = rtc.now();
  159. // Return date using FS_DATE macro to format fields.
  160. *date = FS_DATE(now.year(), now.month(), now.day());
  161. // Return time using FS_TIME macro to format fields.
  162. *time = FS_TIME(now.hour(), now.minute(), now.second());
  163. // Return low time bits in units of 10 ms.
  164. *ms10 = now.second() & 1 ? 100 : 0;
  165. }
  166. #endif // USE_RTC
  167. //------------------------------------------------------------------------------
  168. #define error(s) sd.errorHalt(&Serial, F(s))
  169. #define dbgAssert(e) ((e) ? (void)0 : error("assert " #e))
  170. //-----------------------------------------------------------------------------
  171. // Convert binary file to csv file.
  172. void binaryToCsv() {
  173. uint8_t lastPct = 0;
  174. uint32_t t0 = millis();
  175. data_t binData[FIFO_DIM];
  176. if (!binFile.seekSet(512)) {
  177. error("binFile.seek faile");
  178. }
  179. uint32_t tPct = millis();
  180. printRecord(&csvFile, nullptr);
  181. while (!Serial.available() && binFile.available()) {
  182. int nb = binFile.read(binData, sizeof(binData));
  183. if (nb <= 0 ) {
  184. error("read binFile failed");
  185. }
  186. size_t nr = nb/sizeof(data_t);
  187. for (size_t i = 0; i < nr; i++) {
  188. printRecord(&csvFile, &binData[i]);
  189. }
  190. if ((millis() - tPct) > 1000) {
  191. uint8_t pct = binFile.curPosition()/(binFile.fileSize()/100);
  192. if (pct != lastPct) {
  193. tPct = millis();
  194. lastPct = pct;
  195. Serial.print(pct, DEC);
  196. Serial.println('%');
  197. csvFile.sync();
  198. }
  199. }
  200. if (Serial.available()) {
  201. break;
  202. }
  203. }
  204. csvFile.close();
  205. Serial.print(F("Done: "));
  206. Serial.print(0.001*(millis() - t0));
  207. Serial.println(F(" Seconds"));
  208. }
  209. //------------------------------------------------------------------------------
  210. void clearSerialInput() {
  211. uint32_t m = micros();
  212. do {
  213. if (Serial.read() >= 0) {
  214. m = micros();
  215. }
  216. } while (micros() - m < 10000);
  217. }
  218. //-------------------------------------------------------------------------------
  219. void createBinFile() {
  220. binFile.close();
  221. while (sd.exists(binName)) {
  222. char* p = strchr(binName, '.');
  223. if (!p) {
  224. error("no dot in filename");
  225. }
  226. while (true) {
  227. p--;
  228. if (p < binName || *p < '0' || *p > '9') {
  229. error("Can't create file name");
  230. }
  231. if (p[0] != '9') {
  232. p[0]++;
  233. break;
  234. }
  235. p[0] = '0';
  236. }
  237. }
  238. if (!binFile.open(binName, O_RDWR | O_CREAT)) {
  239. error("open binName failed");
  240. }
  241. Serial.println(binName);
  242. if (!binFile.preAllocate(PREALLOCATE_SIZE)) {
  243. error("preAllocate failed");
  244. }
  245. Serial.print(F("preAllocated: "));
  246. Serial.print(PREALLOCATE_SIZE_MiB);
  247. Serial.println(F(" MiB"));
  248. }
  249. //-------------------------------------------------------------------------------
  250. bool createCsvFile() {
  251. char csvName[FILE_NAME_DIM];
  252. if (!binFile.isOpen()) {
  253. Serial.println(F("No current binary file"));
  254. return false;
  255. }
  256. // Create a new csvFile.
  257. binFile.getName(csvName, sizeof(csvName));
  258. char* dot = strchr(csvName, '.');
  259. if (!dot) {
  260. error("no dot in filename");
  261. }
  262. strcpy(dot + 1, "csv");
  263. if (!csvFile.open(csvName, O_WRONLY | O_CREAT | O_TRUNC)) {
  264. error("open csvFile failed");
  265. }
  266. clearSerialInput();
  267. Serial.print(F("Writing: "));
  268. Serial.print(csvName);
  269. Serial.println(F(" - type any character to stop"));
  270. return true;
  271. }
  272. //-------------------------------------------------------------------------------
  273. void logData() {
  274. int32_t delta; // Jitter in log time.
  275. int32_t maxDelta = 0;
  276. uint32_t maxLogMicros = 0;
  277. uint32_t maxWriteMicros = 0;
  278. size_t maxFifoUse = 0;
  279. size_t fifoCount = 0;
  280. size_t fifoHead = 0;
  281. size_t fifoTail = 0;
  282. uint16_t overrun = 0;
  283. uint16_t maxOverrun = 0;
  284. uint32_t totalOverrun = 0;
  285. uint32_t fifoBuf[128*FIFO_SIZE_SECTORS];
  286. data_t* fifoData = (data_t*)fifoBuf;
  287. // Write dummy sector to start multi-block write.
  288. dbgAssert(sizeof(fifoBuf) >= 512);
  289. memset(fifoBuf, 0, sizeof(fifoBuf));
  290. if (binFile.write(fifoBuf, 512) != 512) {
  291. error("write first sector failed");
  292. }
  293. clearSerialInput();
  294. Serial.println(F("Type any character to stop"));
  295. // Wait until SD is not busy.
  296. while (sd.card()->isBusy()) {}
  297. // Start time for log file.
  298. uint32_t m = millis();
  299. // Time to log next record.
  300. uint32_t logTime = micros();
  301. while (true) {
  302. // Time for next data record.
  303. logTime += LOG_INTERVAL_USEC;
  304. // Wait until time to log data.
  305. delta = micros() - logTime;
  306. if (delta > 0) {
  307. Serial.print(F("delta: "));
  308. Serial.println(delta);
  309. error("Rate too fast");
  310. }
  311. while (delta < 0) {
  312. delta = micros() - logTime;
  313. }
  314. if (fifoCount < FIFO_DIM) {
  315. uint32_t m = micros();
  316. logRecord(fifoData + fifoHead, overrun);
  317. m = micros() - m;
  318. if (m > maxLogMicros) {
  319. maxLogMicros = m;
  320. }
  321. fifoHead = fifoHead < (FIFO_DIM - 1) ? fifoHead + 1 : 0;
  322. fifoCount++;
  323. if (overrun) {
  324. if (overrun > maxOverrun) {
  325. maxOverrun = overrun;
  326. }
  327. overrun = 0;
  328. }
  329. } else {
  330. totalOverrun++;
  331. overrun++;
  332. if (overrun > 0XFFF) {
  333. error("too many overruns");
  334. }
  335. if (ERROR_LED_PIN >= 0) {
  336. digitalWrite(ERROR_LED_PIN, HIGH);
  337. }
  338. }
  339. // Save max jitter.
  340. if (delta > maxDelta) {
  341. maxDelta = delta;
  342. }
  343. // Write data if SD is not busy.
  344. if (!sd.card()->isBusy()) {
  345. size_t nw = fifoHead > fifoTail ? fifoCount : FIFO_DIM - fifoTail;
  346. // Limit write time by not writing more than 512 bytes.
  347. const size_t MAX_WRITE = 512/sizeof(data_t);
  348. if (nw > MAX_WRITE) nw = MAX_WRITE;
  349. size_t nb = nw*sizeof(data_t);
  350. uint32_t usec = micros();
  351. if (nb != binFile.write(fifoData + fifoTail, nb)) {
  352. error("write binFile failed");
  353. }
  354. usec = micros() - usec;
  355. if (usec > maxWriteMicros) {
  356. maxWriteMicros = usec;
  357. }
  358. fifoTail = (fifoTail + nw) < FIFO_DIM ? fifoTail + nw : 0;
  359. if (fifoCount > maxFifoUse) {
  360. maxFifoUse = fifoCount;
  361. }
  362. fifoCount -= nw;
  363. if (Serial.available()) {
  364. break;
  365. }
  366. }
  367. }
  368. Serial.print(F("\nLog time: "));
  369. Serial.print(0.001*(millis() - m));
  370. Serial.println(F(" Seconds"));
  371. binFile.truncate();
  372. binFile.sync();
  373. Serial.print(("File size: "));
  374. // Warning cast used for print since fileSize is uint64_t.
  375. Serial.print((uint32_t)binFile.fileSize());
  376. Serial.println(F(" bytes"));
  377. Serial.print(F("totalOverrun: "));
  378. Serial.println(totalOverrun);
  379. Serial.print(F("FIFO_DIM: "));
  380. Serial.println(FIFO_DIM);
  381. Serial.print(F("maxFifoUse: "));
  382. Serial.println(maxFifoUse);
  383. Serial.print(F("maxLogMicros: "));
  384. Serial.println(maxLogMicros);
  385. Serial.print(F("maxWriteMicros: "));
  386. Serial.println(maxWriteMicros);
  387. Serial.print(F("Log interval: "));
  388. Serial.print(LOG_INTERVAL_USEC);
  389. Serial.print(F(" micros\nmaxDelta: "));
  390. Serial.print(maxDelta);
  391. Serial.println(F(" micros"));
  392. }
  393. //------------------------------------------------------------------------------
  394. void openBinFile() {
  395. char name[FILE_NAME_DIM];
  396. clearSerialInput();
  397. Serial.println(F("Enter file name"));
  398. if (!serialReadLine(name, sizeof(name))) {
  399. return;
  400. }
  401. if (!sd.exists(name)) {
  402. Serial.println(name);
  403. Serial.println(F("File does not exist"));
  404. return;
  405. }
  406. binFile.close();
  407. if (!binFile.open(name, O_RDONLY)) {
  408. Serial.println(name);
  409. Serial.println(F("open failed"));
  410. return;
  411. }
  412. Serial.println(F("File opened"));
  413. }
  414. //-----------------------------------------------------------------------------
  415. void printData() {
  416. if (!binFile.isOpen()) {
  417. Serial.println(F("No current binary file"));
  418. return;
  419. }
  420. // Skip first dummy sector.
  421. if (!binFile.seekSet(512)) {
  422. error("seek failed");
  423. }
  424. clearSerialInput();
  425. Serial.println(F("type any character to stop\n"));
  426. delay(1000);
  427. printRecord(&Serial, nullptr);
  428. while (binFile.available() && !Serial.available()) {
  429. data_t record;
  430. if (binFile.read(&record, sizeof(data_t)) != sizeof(data_t)) {
  431. error("read binFile failed");
  432. }
  433. printRecord(&Serial, &record);
  434. }
  435. }
  436. //------------------------------------------------------------------------------
  437. void printUnusedStack() {
  438. #if HAS_UNUSED_STACK
  439. Serial.print(F("\nUnused stack: "));
  440. Serial.println(UnusedStack());
  441. #endif // HAS_UNUSED_STACK
  442. }
  443. //------------------------------------------------------------------------------
  444. bool serialReadLine(char* str, size_t size) {
  445. size_t n = 0;
  446. while(!Serial.available()) {
  447. yield();
  448. }
  449. while (true) {
  450. int c = Serial.read();
  451. if (c < ' ') break;
  452. str[n++] = c;
  453. if (n >= size) {
  454. Serial.println(F("input too long"));
  455. return false;
  456. }
  457. uint32_t m = millis();
  458. while (!Serial.available() && (millis() - m) < 100){}
  459. if (!Serial.available()) break;
  460. }
  461. str[n] = 0;
  462. return true;
  463. }
  464. //------------------------------------------------------------------------------
  465. void testSensor() {
  466. const uint32_t interval = 200000;
  467. int32_t diff;
  468. data_t data;
  469. clearSerialInput();
  470. Serial.println(F("\nTesting - type any character to stop\n"));
  471. delay(1000);
  472. printRecord(&Serial, nullptr);
  473. uint32_t m = micros();
  474. while (!Serial.available()) {
  475. m += interval;
  476. do {
  477. diff = m - micros();
  478. } while (diff > 0);
  479. logRecord(&data, 0);
  480. printRecord(&Serial, &data);
  481. }
  482. }
  483. //------------------------------------------------------------------------------
  484. void setup() {
  485. if (ERROR_LED_PIN >= 0) {
  486. pinMode(ERROR_LED_PIN, OUTPUT);
  487. digitalWrite(ERROR_LED_PIN, HIGH);
  488. }
  489. Serial.begin(9600);
  490. // Wait for USB Serial
  491. while (!Serial) {
  492. SysCall::yield();
  493. }
  494. delay(1000);
  495. Serial.println(F("Type any character to begin"));
  496. while (!Serial.available()) {
  497. yield();
  498. }
  499. FillStack();
  500. #if !ENABLE_DEDICATED_SPI
  501. Serial.println(F(
  502. "\nFor best performance edit SdFatConfig.h\n"
  503. "and set ENABLE_DEDICATED_SPI nonzero"));
  504. #endif // !ENABLE_DEDICATED_SPI
  505. Serial.print(FIFO_DIM);
  506. Serial.println(F(" FIFO entries will be used."));
  507. // Initialize SD.
  508. if (!sd.begin(SD_CONFIG)) {
  509. sd.initErrorHalt(&Serial);
  510. }
  511. #if USE_RTC
  512. if (!rtc.begin()) {
  513. error("rtc.begin failed");
  514. }
  515. if (!rtc.isrunning()) {
  516. // Set RTC to sketch compile date & time.
  517. // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  518. error("RTC is NOT running!");
  519. }
  520. // Set callback
  521. FsDateTime::setCallback(dateTime);
  522. #endif // USE_RTC
  523. }
  524. //------------------------------------------------------------------------------
  525. void loop() {
  526. printUnusedStack();
  527. // Read any Serial data.
  528. clearSerialInput();
  529. if (ERROR_LED_PIN >= 0) {
  530. digitalWrite(ERROR_LED_PIN, LOW);
  531. }
  532. Serial.println();
  533. Serial.println(F("type: "));
  534. Serial.println(F("b - open existing bin file"));
  535. Serial.println(F("c - convert file to csv"));
  536. Serial.println(F("l - list files"));
  537. Serial.println(F("p - print data to Serial"));
  538. Serial.println(F("r - record data"));
  539. Serial.println(F("t - test without logging"));
  540. while(!Serial.available()) {
  541. SysCall::yield();
  542. }
  543. char c = tolower(Serial.read());
  544. Serial.println();
  545. if (c == 'b') {
  546. openBinFile();
  547. } else if (c == 'c') {
  548. if (createCsvFile()) {
  549. binaryToCsv();
  550. }
  551. } else if (c == 'l') {
  552. Serial.println(F("ls:"));
  553. sd.ls(&Serial, LS_DATE | LS_SIZE);
  554. } else if (c == 'p') {
  555. printData();
  556. } else if (c == 'r') {
  557. createBinFile();
  558. logData();
  559. } else if (c == 't') {
  560. testSensor();
  561. } else {
  562. Serial.println(F("Invalid entry"));
  563. }
  564. }