bench.ino 7.6 KB

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