Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

267 lines
7.3KB

  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. #if HAS_UNUSED_STACK
  130. cout << F("FreeStack: ") << FreeStack() << endl;
  131. #endif // HAS_UNUSED_STACK
  132. if (!sd.begin(SD_CONFIG)) {
  133. sd.initErrorHalt(&Serial);
  134. }
  135. if (sd.fatType() == FAT_TYPE_EXFAT) {
  136. cout << F("Type is exFAT") << endl;
  137. } else {
  138. cout << F("Type is FAT") << int(sd.fatType()) << endl;
  139. }
  140. cout << F("Card size: ") << sd.card()->sectorCount()*512E-9;
  141. cout << F(" GB (GB = 1E9 bytes)") << endl;
  142. cidDmp();
  143. // open or create file - truncate existing file.
  144. if (!file.open("bench.dat", O_RDWR | O_CREAT | O_TRUNC)) {
  145. error("open failed");
  146. }
  147. // fill buf with known data
  148. if (BUF_SIZE > 1) {
  149. for (size_t i = 0; i < (BUF_SIZE - 2); i++) {
  150. buf[i] = 'A' + (i % 26);
  151. }
  152. buf[BUF_SIZE-2] = '\r';
  153. }
  154. buf[BUF_SIZE-1] = '\n';
  155. cout << F("FILE_SIZE_MB = ") << FILE_SIZE_MB << endl;
  156. cout << F("BUF_SIZE = ") << BUF_SIZE << F(" bytes\n");
  157. cout << F("Starting write test, please wait.") << endl << endl;
  158. // do write test
  159. uint32_t n = FILE_SIZE/BUF_SIZE;
  160. cout <<F("write speed and latency") << endl;
  161. cout << F("speed,max,min,avg") << endl;
  162. cout << F("KB/Sec,usec,usec,usec") << endl;
  163. for (uint8_t nTest = 0; nTest < WRITE_COUNT; nTest++) {
  164. file.truncate(0);
  165. if (PRE_ALLOCATE) {
  166. if (!file.preAllocate(FILE_SIZE)) {
  167. error("preAllocate failed");
  168. }
  169. }
  170. maxLatency = 0;
  171. minLatency = 9999999;
  172. totalLatency = 0;
  173. skipLatency = SKIP_FIRST_LATENCY;
  174. t = millis();
  175. for (uint32_t i = 0; i < n; i++) {
  176. uint32_t m = micros();
  177. if (file.write(buf, BUF_SIZE) != BUF_SIZE) {
  178. error("write failed");
  179. }
  180. m = micros() - m;
  181. totalLatency += m;
  182. if (skipLatency) {
  183. // Wait until first write to SD, not just a copy to the cache.
  184. skipLatency = file.curPosition() < 512;
  185. } else {
  186. if (maxLatency < m) {
  187. maxLatency = m;
  188. }
  189. if (minLatency > m) {
  190. minLatency = m;
  191. }
  192. }
  193. }
  194. file.sync();
  195. t = millis() - t;
  196. s = file.fileSize();
  197. cout << s/t <<',' << maxLatency << ',' << minLatency;
  198. cout << ',' << totalLatency/n << endl;
  199. }
  200. cout << endl << F("Starting read test, please wait.") << endl;
  201. cout << endl <<F("read speed and latency") << endl;
  202. cout << F("speed,max,min,avg") << endl;
  203. cout << F("KB/Sec,usec,usec,usec") << endl;
  204. // do read test
  205. for (uint8_t nTest = 0; nTest < READ_COUNT; nTest++) {
  206. file.rewind();
  207. maxLatency = 0;
  208. minLatency = 9999999;
  209. totalLatency = 0;
  210. skipLatency = SKIP_FIRST_LATENCY;
  211. t = millis();
  212. for (uint32_t i = 0; i < n; i++) {
  213. buf[BUF_SIZE-1] = 0;
  214. uint32_t m = micros();
  215. int32_t nr = file.read(buf, BUF_SIZE);
  216. if (nr != BUF_SIZE) {
  217. error("read failed");
  218. }
  219. m = micros() - m;
  220. totalLatency += m;
  221. if (buf[BUF_SIZE-1] != '\n') {
  222. error("data check error");
  223. }
  224. if (skipLatency) {
  225. skipLatency = false;
  226. } else {
  227. if (maxLatency < m) {
  228. maxLatency = m;
  229. }
  230. if (minLatency > m) {
  231. minLatency = m;
  232. }
  233. }
  234. }
  235. s = file.fileSize();
  236. t = millis() - t;
  237. cout << s/t <<',' << maxLatency << ',' << minLatency;
  238. cout << ',' << totalLatency/n << endl;
  239. }
  240. cout << endl << F("Done") << endl;
  241. file.close();
  242. }