Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

168 Zeilen
5.0KB

  1. /*
  2. * This sketch illustrates raw write functions in SdFat that
  3. * can be used for high speed data logging.
  4. *
  5. * This sketch simulates logging from a source that produces
  6. * data at a constant rate of one block every MICROS_PER_BLOCK.
  7. *
  8. * If a high quality SanDisk card is used with this sketch
  9. * no overruns occur and the maximum block write time is
  10. * under 2000 micros.
  11. *
  12. * Note: Apps should create a very large file then truncates it
  13. * to the length that is used for a logging. It only takes
  14. * a few seconds to erase a 500 MB file since the card only
  15. * marks the blocks as erased; no data transfer is required.
  16. */
  17. #include <SPI.h>
  18. #include <SdFat.h>
  19. #include <SdFatUtil.h>
  20. // SD chip select pin
  21. const uint8_t chipSelect = SS;
  22. // number of blocks in the contiguous file
  23. const uint32_t BLOCK_COUNT = 10000UL;
  24. // time to produce a block of data
  25. const uint32_t MICROS_PER_BLOCK = 10000;
  26. // file system
  27. SdFat sd;
  28. // test file
  29. SdFile file;
  30. // file extent
  31. uint32_t bgnBlock, endBlock;
  32. // Serial output stream
  33. ArduinoOutStream cout(Serial);
  34. //------------------------------------------------------------------------------
  35. // store error strings in flash to save RAM
  36. #define error(s) sd.errorHalt(F(s))
  37. //------------------------------------------------------------------------------
  38. // log of first overruns
  39. #define OVER_DIM 20
  40. struct {
  41. uint32_t block;
  42. uint32_t micros;
  43. } over[OVER_DIM];
  44. //------------------------------------------------------------------------------
  45. void setup(void) {
  46. Serial.begin(9600);
  47. while (!Serial) {} // wait for Leonardo
  48. }
  49. //------------------------------------------------------------------------------
  50. void loop(void) {
  51. while (Serial.read() >= 0) {}
  52. // pstr stores strings in flash to save RAM
  53. cout << pstr("Type any character to start\n");
  54. while (Serial.read() <= 0) {}
  55. delay(400); // catch Due reset problem
  56. cout << pstr("Free RAM: ") << FreeRam() << endl;
  57. // initialize the SD card at SPI_FULL_SPEED for best performance.
  58. // try SPI_HALF_SPEED if bus errors occur.
  59. if (!sd.begin(chipSelect, SPI_FULL_SPEED)) sd.initErrorHalt();
  60. // delete possible existing file
  61. sd.remove("RAW.TXT");
  62. // create a contiguous file
  63. if (!file.createContiguous(sd.vwd(), "RAW.TXT", 512UL*BLOCK_COUNT)) {
  64. error("createContiguous failed");
  65. }
  66. // get the location of the file's blocks
  67. if (!file.contiguousRange(&bgnBlock, &endBlock)) {
  68. error("contiguousRange failed");
  69. }
  70. //*********************NOTE**************************************
  71. // NO SdFile calls are allowed while cache is used for raw writes
  72. //***************************************************************
  73. // clear the cache and use it as a 512 byte buffer
  74. uint8_t* pCache = (uint8_t*)sd.vol()->cacheClear();
  75. // fill cache with eight lines of 64 bytes each
  76. memset(pCache, ' ', 512);
  77. for (uint16_t i = 0; i < 512; i += 64) {
  78. // put line number at end of line then CR/LF
  79. pCache[i + 61] = '0' + (i/64);
  80. pCache[i + 62] = '\r';
  81. pCache[i + 63] = '\n';
  82. }
  83. cout << pstr("Start raw write of ") << file.fileSize() << pstr(" bytes at\n");
  84. cout << 512000000UL/MICROS_PER_BLOCK << pstr(" bytes per second\n");
  85. cout << pstr("Please wait ") << (BLOCK_COUNT*MICROS_PER_BLOCK)/1000000UL;
  86. cout << pstr(" seconds\n");
  87. // tell card to setup for multiple block write with pre-erase
  88. if (!sd.card()->writeStart(bgnBlock, BLOCK_COUNT)) {
  89. error("writeStart failed");
  90. }
  91. // init stats
  92. uint16_t overruns = 0;
  93. uint32_t maxWriteTime = 0;
  94. uint32_t t = micros();
  95. uint32_t tNext = t;
  96. // write data
  97. for (uint32_t b = 0; b < BLOCK_COUNT; b++) {
  98. // write must be done by this time
  99. tNext += MICROS_PER_BLOCK;
  100. // put block number at start of first line in block
  101. uint32_t n = b;
  102. for (int8_t d = 5; d >= 0; d--){
  103. pCache[d] = n || d == 5 ? n % 10 + '0' : ' ';
  104. n /= 10;
  105. }
  106. // write a 512 byte block
  107. uint32_t tw = micros();
  108. if (!sd.card()->writeData(pCache)) error("writeData failed");
  109. tw = micros() - tw;
  110. // check for max write time
  111. if (tw > maxWriteTime) {
  112. maxWriteTime = tw;
  113. }
  114. // check for overrun
  115. if (micros() > tNext) {
  116. if (overruns < OVER_DIM) {
  117. over[overruns].block = b;
  118. over[overruns].micros = tw;
  119. }
  120. overruns++;
  121. // advance time to reflect overrun
  122. tNext = micros();
  123. }
  124. else {
  125. // wait for time to write next block
  126. while(micros() < tNext);
  127. }
  128. }
  129. // total write time
  130. t = micros() - t;
  131. // end multiple block write mode
  132. if (!sd.card()->writeStop()) error("writeStop failed");
  133. cout << pstr("Done\n");
  134. cout << pstr("Elapsed time: ") << setprecision(3)<< 1.e-6*t;
  135. cout << pstr(" seconds\n");
  136. cout << pstr("Max write time: ") << maxWriteTime << pstr(" micros\n");
  137. cout << pstr("Overruns: ") << overruns << endl;
  138. if (overruns) {
  139. uint8_t n = overruns > OVER_DIM ? OVER_DIM : overruns;
  140. cout << pstr("fileBlock,micros") << endl;
  141. for (uint8_t i = 0; i < n; i++) {
  142. cout << over[i].block << ',' << over[i].micros << endl;
  143. }
  144. }
  145. // close file for next pass of loop
  146. file.close();
  147. Serial.println();
  148. }