Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

263 lines
7.2KB

  1. /*
  2. * This program is a simple binary write/read benchmark.
  3. */
  4. #include "SdFat.h"
  5. #include "sdios.h"
  6. #include "FreeStack.h"
  7. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  8. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  9. #define SD_FAT_TYPE 0
  10. /*
  11. Change the value of SD_CS_PIN if you are using SPI and
  12. your hardware does not use the default value, SS.
  13. Common values are:
  14. Arduino Ethernet shield: pin 4
  15. Sparkfun SD shield: pin 8
  16. Adafruit SD shields and modules: pin 10
  17. */
  18. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  19. #ifndef SDCARD_SS_PIN
  20. const uint8_t SD_CS_PIN = SS;
  21. #else // SDCARD_SS_PIN
  22. // Assume built-in SD is used.
  23. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  24. #endif // SDCARD_SS_PIN
  25. // Try to select the best SD card configuration.
  26. #if HAS_SDIO_CLASS
  27. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  28. #elif ENABLE_DEDICATED_SPI
  29. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
  30. #else // HAS_SDIO_CLASS
  31. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
  32. #endif // HAS_SDIO_CLASS
  33. // Set PRE_ALLOCATE true to pre-allocate file clusters.
  34. const bool PRE_ALLOCATE = true;
  35. // Set SKIP_FIRST_LATENCY true if the first read/write to the SD can
  36. // be avoid by writing a file header or reading the first record.
  37. const bool SKIP_FIRST_LATENCY = true;
  38. // Size of read/write.
  39. const size_t BUF_SIZE = 512;
  40. // File size in MB where MB = 1,000,000 bytes.
  41. const uint32_t FILE_SIZE_MB = 5;
  42. // Write pass count.
  43. const uint8_t WRITE_COUNT = 2;
  44. // Read pass count.
  45. const uint8_t READ_COUNT = 2;
  46. //==============================================================================
  47. // End of configuration constants.
  48. //------------------------------------------------------------------------------
  49. // File size in bytes.
  50. const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB;
  51. // Insure 4-byte alignment.
  52. uint32_t buf32[(BUF_SIZE + 3)/4];
  53. uint8_t* buf = (uint8_t*)buf32;
  54. #if SD_FAT_TYPE == 0
  55. SdFat sd;
  56. File file;
  57. #elif SD_FAT_TYPE == 1
  58. SdFat32 sd;
  59. File32 file;
  60. #elif SD_FAT_TYPE == 2
  61. SdExFat sd;
  62. ExFile file;
  63. #elif SD_FAT_TYPE == 3
  64. SdFs sd;
  65. FsFile file;
  66. #else // SD_FAT_TYPE
  67. #error Invalid SD_FAT_TYPE
  68. #endif // SD_FAT_TYPE
  69. // Serial output stream
  70. ArduinoOutStream cout(Serial);
  71. //------------------------------------------------------------------------------
  72. // Store error strings in flash to save RAM.
  73. #define error(s) sd.errorHalt(&Serial, F(s))
  74. //------------------------------------------------------------------------------
  75. void cidDmp() {
  76. cid_t cid;
  77. if (!sd.card()->readCID(&cid)) {
  78. error("readCID failed");
  79. }
  80. cout << F("\nManufacturer ID: ");
  81. cout << hex << int(cid.mid) << dec << endl;
  82. cout << F("OEM ID: ") << cid.oid[0] << cid.oid[1] << endl;
  83. cout << F("Product: ");
  84. for (uint8_t i = 0; i < 5; i++) {
  85. cout << cid.pnm[i];
  86. }
  87. cout << F("\nVersion: ");
  88. cout << int(cid.prv_n) << '.' << int(cid.prv_m) << endl;
  89. cout << F("Serial number: ") << hex << cid.psn << dec << endl;
  90. cout << F("Manufacturing date: ");
  91. cout << int(cid.mdt_month) << '/';
  92. cout << (2000 + cid.mdt_year_low + 10 * cid.mdt_year_high) << endl;
  93. cout << endl;
  94. }
  95. //------------------------------------------------------------------------------
  96. void setup() {
  97. Serial.begin(9600);
  98. // Wait for USB Serial
  99. while (!Serial) {
  100. SysCall::yield();
  101. }
  102. delay(1000);
  103. cout << F("\nUse a freshly formatted SD for best performance.\n");
  104. if (!ENABLE_DEDICATED_SPI) {
  105. cout << F(
  106. "\nSet ENABLE_DEDICATED_SPI nonzero in\n"
  107. "SdFatConfig.h for best SPI performance.\n");
  108. }
  109. // use uppercase in hex and use 0X base prefix
  110. cout << uppercase << showbase << endl;
  111. }
  112. //------------------------------------------------------------------------------
  113. void loop() {
  114. float s;
  115. uint32_t t;
  116. uint32_t maxLatency;
  117. uint32_t minLatency;
  118. uint32_t totalLatency;
  119. bool skipLatency;
  120. // Discard any input.
  121. do {
  122. delay(10);
  123. } while (Serial.available() && Serial.read() >= 0);
  124. // F() stores strings in flash to save RAM
  125. cout << F("Type any character to start\n");
  126. while (!Serial.available()) {
  127. SysCall::yield();
  128. }
  129. cout << F("FreeStack: ") << FreeStack() << endl;
  130. if (!sd.begin(SD_CONFIG)) {
  131. sd.initErrorHalt(&Serial);
  132. }
  133. if (sd.fatType() == FAT_TYPE_EXFAT) {
  134. cout << F("Type is exFAT") << endl;
  135. } else {
  136. cout << F("Type is FAT") << int(sd.fatType()) << endl;
  137. }
  138. cout << F("Card size: ") << sd.card()->sectorCount()*512E-9;
  139. cout << F(" GB (GB = 1E9 bytes)") << endl;
  140. cidDmp();
  141. // open or create file - truncate existing file.
  142. if (!file.open("bench.dat", O_RDWR | O_CREAT | O_TRUNC)) {
  143. error("open failed");
  144. }
  145. // fill buf with known data
  146. for (uint16_t i = 0; i < (BUF_SIZE-2); i++) {
  147. buf[i] = 'A' + (i % 26);
  148. }
  149. buf[BUF_SIZE-2] = '\r';
  150. buf[BUF_SIZE-1] = '\n';
  151. cout << F("FILE_SIZE_MB = ") << FILE_SIZE_MB << endl;
  152. cout << F("BUF_SIZE = ") << BUF_SIZE << F(" bytes\n");
  153. cout << F("Starting write test, please wait.") << endl << endl;
  154. // do write test
  155. uint32_t n = FILE_SIZE/BUF_SIZE;
  156. cout <<F("write speed and latency") << endl;
  157. cout << F("speed,max,min,avg") << endl;
  158. cout << F("KB/Sec,usec,usec,usec") << endl;
  159. for (uint8_t nTest = 0; nTest < WRITE_COUNT; nTest++) {
  160. file.truncate(0);
  161. if (PRE_ALLOCATE) {
  162. if (!file.preAllocate(FILE_SIZE)) {
  163. error("preAllocate failed");
  164. }
  165. }
  166. maxLatency = 0;
  167. minLatency = 9999999;
  168. totalLatency = 0;
  169. skipLatency = SKIP_FIRST_LATENCY;
  170. t = millis();
  171. for (uint32_t i = 0; i < n; i++) {
  172. uint32_t m = micros();
  173. if (file.write(buf, BUF_SIZE) != BUF_SIZE) {
  174. error("write failed");
  175. }
  176. m = micros() - m;
  177. totalLatency += m;
  178. if (skipLatency) {
  179. // Wait until first write to SD, not just a copy to the cache.
  180. skipLatency = file.curPosition() < 512;
  181. } else {
  182. if (maxLatency < m) {
  183. maxLatency = m;
  184. }
  185. if (minLatency > m) {
  186. minLatency = m;
  187. }
  188. }
  189. }
  190. file.sync();
  191. t = millis() - t;
  192. s = file.fileSize();
  193. cout << s/t <<',' << maxLatency << ',' << minLatency;
  194. cout << ',' << totalLatency/n << endl;
  195. }
  196. cout << endl << F("Starting read test, please wait.") << endl;
  197. cout << endl <<F("read speed and latency") << endl;
  198. cout << F("speed,max,min,avg") << endl;
  199. cout << F("KB/Sec,usec,usec,usec") << endl;
  200. // do read test
  201. for (uint8_t nTest = 0; nTest < READ_COUNT; nTest++) {
  202. file.rewind();
  203. maxLatency = 0;
  204. minLatency = 9999999;
  205. totalLatency = 0;
  206. skipLatency = SKIP_FIRST_LATENCY;
  207. t = millis();
  208. for (uint32_t i = 0; i < n; i++) {
  209. buf[BUF_SIZE-1] = 0;
  210. uint32_t m = micros();
  211. int32_t nr = file.read(buf, BUF_SIZE);
  212. if (nr != BUF_SIZE) {
  213. error("read failed");
  214. }
  215. m = micros() - m;
  216. totalLatency += m;
  217. if (buf[BUF_SIZE-1] != '\n') {
  218. error("data check error");
  219. }
  220. if (skipLatency) {
  221. skipLatency = false;
  222. } else {
  223. if (maxLatency < m) {
  224. maxLatency = m;
  225. }
  226. if (minLatency > m) {
  227. minLatency = m;
  228. }
  229. }
  230. }
  231. s = file.fileSize();
  232. t = millis() - t;
  233. cout << s/t <<',' << maxLatency << ',' << minLatency;
  234. cout << ',' << totalLatency/n << endl;
  235. }
  236. cout << endl << F("Done") << endl;
  237. file.close();
  238. }