Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

bench.ino 7.4KB

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