bench.ino 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * This program is a simple binary write/read benchmark.
  3. */
  4. #include "FreeStack.h"
  5. #include "SdFat.h"
  6. #include "sdios.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 << uppercase << showbase << 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("\nRevision: ") << cid.prvN() << '.' << cid.prvM() << endl;
  90. cout << F("Serial number: ") << hex << cid.psn() << dec << endl;
  91. cout << F("Manufacturing date: ");
  92. cout << cid.mdtMonth() << '/' << cid.mdtYear() << endl;
  93. cout << endl;
  94. }
  95. //------------------------------------------------------------------------------
  96. void clearSerialInput() {
  97. uint32_t m = micros();
  98. do {
  99. if (Serial.read() >= 0) {
  100. m = micros();
  101. }
  102. } while (micros() - m < 10000);
  103. }
  104. //------------------------------------------------------------------------------
  105. void setup() {
  106. Serial.begin(9600);
  107. // Wait for USB Serial
  108. while (!Serial) {
  109. yield();
  110. }
  111. delay(1000);
  112. cout << F("\nUse a freshly formatted SD for best performance.\n");
  113. if (!ENABLE_DEDICATED_SPI) {
  114. cout << F(
  115. "\nSet ENABLE_DEDICATED_SPI nonzero in\n"
  116. "SdFatConfig.h for best SPI performance.\n");
  117. }
  118. // use uppercase in hex and use 0X base prefix
  119. cout << uppercase << showbase << endl;
  120. }
  121. //------------------------------------------------------------------------------
  122. void loop() {
  123. float s;
  124. uint32_t t;
  125. uint32_t maxLatency;
  126. uint32_t minLatency;
  127. uint32_t totalLatency;
  128. bool skipLatency;
  129. // Discard any input.
  130. clearSerialInput();
  131. // F() stores strings in flash to save RAM
  132. cout << F("Type any character to start\n");
  133. while (!Serial.available()) {
  134. yield();
  135. }
  136. #if HAS_UNUSED_STACK
  137. cout << F("FreeStack: ") << FreeStack() << endl;
  138. #endif // HAS_UNUSED_STACK
  139. if (!sd.begin(SD_CONFIG)) {
  140. sd.initErrorHalt(&Serial);
  141. }
  142. if (sd.fatType() == FAT_TYPE_EXFAT) {
  143. cout << F("Type is exFAT") << endl;
  144. } else {
  145. cout << F("Type is FAT") << int(sd.fatType()) << endl;
  146. }
  147. cout << F("Card size: ") << sd.card()->sectorCount() * 512E-9;
  148. cout << F(" GB (GB = 1E9 bytes)") << endl;
  149. cidDmp();
  150. // open or create file - truncate existing file.
  151. if (!file.open("bench.dat", O_RDWR | O_CREAT | O_TRUNC)) {
  152. error("open failed");
  153. }
  154. // fill buf with known data
  155. if (BUF_SIZE > 1) {
  156. for (size_t i = 0; i < (BUF_SIZE - 2); i++) {
  157. buf[i] = 'A' + (i % 26);
  158. }
  159. buf[BUF_SIZE - 2] = '\r';
  160. }
  161. buf[BUF_SIZE - 1] = '\n';
  162. cout << F("FILE_SIZE_MB = ") << FILE_SIZE_MB << endl;
  163. cout << F("BUF_SIZE = ") << BUF_SIZE << F(" bytes\n");
  164. cout << F("Starting write test, please wait.") << endl << endl;
  165. // do write test
  166. uint32_t n = FILE_SIZE / BUF_SIZE;
  167. cout << F("write speed and latency") << endl;
  168. cout << F("speed,max,min,avg") << endl;
  169. cout << F("KB/Sec,usec,usec,usec") << endl;
  170. for (uint8_t nTest = 0; nTest < WRITE_COUNT; nTest++) {
  171. file.truncate(0);
  172. if (PRE_ALLOCATE) {
  173. if (!file.preAllocate(FILE_SIZE)) {
  174. error("preAllocate failed");
  175. }
  176. }
  177. maxLatency = 0;
  178. minLatency = 9999999;
  179. totalLatency = 0;
  180. skipLatency = SKIP_FIRST_LATENCY;
  181. t = millis();
  182. for (uint32_t i = 0; i < n; i++) {
  183. uint32_t m = micros();
  184. if (file.write(buf, BUF_SIZE) != BUF_SIZE) {
  185. error("write failed");
  186. }
  187. m = micros() - m;
  188. totalLatency += m;
  189. if (skipLatency) {
  190. // Wait until first write to SD, not just a copy to the cache.
  191. skipLatency = file.curPosition() < 512;
  192. } else {
  193. if (maxLatency < m) {
  194. maxLatency = m;
  195. }
  196. if (minLatency > m) {
  197. minLatency = m;
  198. }
  199. }
  200. }
  201. file.sync();
  202. t = millis() - t;
  203. s = file.fileSize();
  204. cout << s / t << ',' << maxLatency << ',' << minLatency;
  205. cout << ',' << totalLatency / n << endl;
  206. }
  207. cout << endl << F("Starting read test, please wait.") << endl;
  208. cout << endl << F("read speed and latency") << endl;
  209. cout << F("speed,max,min,avg") << endl;
  210. cout << F("KB/Sec,usec,usec,usec") << endl;
  211. // do read test
  212. for (uint8_t nTest = 0; nTest < READ_COUNT; nTest++) {
  213. file.rewind();
  214. maxLatency = 0;
  215. minLatency = 9999999;
  216. totalLatency = 0;
  217. skipLatency = SKIP_FIRST_LATENCY;
  218. t = millis();
  219. for (uint32_t i = 0; i < n; i++) {
  220. buf[BUF_SIZE - 1] = 0;
  221. uint32_t m = micros();
  222. int32_t nr = file.read(buf, BUF_SIZE);
  223. if (nr != BUF_SIZE) {
  224. error("read failed");
  225. }
  226. m = micros() - m;
  227. totalLatency += m;
  228. if (buf[BUF_SIZE - 1] != '\n') {
  229. error("data check error");
  230. }
  231. if (skipLatency) {
  232. skipLatency = false;
  233. } else {
  234. if (maxLatency < m) {
  235. maxLatency = m;
  236. }
  237. if (minLatency > m) {
  238. minLatency = m;
  239. }
  240. }
  241. }
  242. s = file.fileSize();
  243. t = millis() - t;
  244. cout << s / t << ',' << maxLatency << ',' << minLatency;
  245. cout << ',' << totalLatency / n << endl;
  246. }
  247. cout << endl << F("Done") << endl;
  248. file.close();
  249. sd.end();
  250. }