您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

298 行
7.9KB

  1. // SD Card Test
  2. //
  3. // Check if the SD card on the Audio Shield is working,
  4. // and perform some simple speed measurements to gauge
  5. // its ability to play 1, 2, 3 and 4 WAV files at a time.
  6. //
  7. // Requires the audio shield:
  8. // http://www.pjrc.com/store/teensy3_audio.html
  9. //
  10. // Data files to put on your SD card can be downloaded here:
  11. // http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html
  12. //
  13. // This example code is in the public domain.
  14. #include <SD.h>
  15. #include <SPI.h>
  16. // Use these with the Teensy Audio Shield
  17. #define SDCARD_CS_PIN 10
  18. #define SDCARD_MOSI_PIN 7
  19. #define SDCARD_SCK_PIN 14
  20. // Use these with the Teensy 3.5 & 3.6 SD card
  21. //#define SDCARD_CS_PIN BUILTIN_SDCARD
  22. //#define SDCARD_MOSI_PIN 11 // not actually used
  23. //#define SDCARD_SCK_PIN 13 // not actually used
  24. // Use these for the SD+Wiz820 or other adaptors
  25. //#define SDCARD_CS_PIN 4
  26. //#define SDCARD_MOSI_PIN 11
  27. //#define SDCARD_SCK_PIN 13
  28. void setup() {
  29. Sd2Card card;
  30. SdVolume volume;
  31. File f1, f2, f3, f4;
  32. char buffer[512];
  33. boolean status;
  34. unsigned long usec, usecMax;
  35. elapsedMicros usecTotal, usecSingle;
  36. int i, type;
  37. float size;
  38. // wait for the Arduino Serial Monitor to open
  39. while (!Serial) ;
  40. delay(50);
  41. // Configure SPI
  42. SPI.setMOSI(SDCARD_MOSI_PIN);
  43. SPI.setSCK(SDCARD_SCK_PIN);
  44. Serial.begin(9600);
  45. Serial.println("SD Card Test");
  46. Serial.println("------------");
  47. // First, detect the card
  48. status = card.init(SPI_FULL_SPEED, SDCARD_CS_PIN);
  49. if (status) {
  50. Serial.println("SD card is connected :-)");
  51. } else {
  52. Serial.println("SD card is not connected or unusable :-(");
  53. return;
  54. }
  55. type = card.type();
  56. if (type == SD_CARD_TYPE_SD1 || type == SD_CARD_TYPE_SD2) {
  57. Serial.println("Card type is SD");
  58. } else if (type == SD_CARD_TYPE_SDHC) {
  59. Serial.println("Card type is SDHC");
  60. } else {
  61. Serial.println("Card is an unknown type (maybe SDXC?)");
  62. }
  63. // Then look at the file system and print its capacity
  64. status = volume.init(card);
  65. if (!status) {
  66. Serial.println("Unable to access the filesystem on this card. :-(");
  67. return;
  68. }
  69. size = volume.blocksPerCluster() * volume.clusterCount();
  70. size = size * (512.0 / 1e6); // convert blocks to millions of bytes
  71. Serial.print("File system space is ");
  72. Serial.print(size);
  73. Serial.println(" Mbytes.");
  74. // Now open the SD card normally
  75. status = SD.begin(SDCARD_CS_PIN);
  76. if (status) {
  77. Serial.println("SD library is able to access the filesystem");
  78. } else {
  79. Serial.println("SD library can not access the filesystem!");
  80. Serial.println("Please report this problem, with the make & model of your SD card.");
  81. Serial.println(" http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports");
  82. }
  83. // Open the 4 sample files. Hopefully they're on the card
  84. f1 = SD.open("SDTEST1.WAV");
  85. f2 = SD.open("SDTEST2.WAV");
  86. f3 = SD.open("SDTEST3.WAV");
  87. f4 = SD.open("SDTEST4.WAV");
  88. // Speed test reading a single file
  89. if (f1) {
  90. Serial.println();
  91. Serial.println("Reading SDTEST1.WAV:");
  92. if (f1.size() >= 514048) {
  93. usecMax = 0;
  94. usecTotal = 0;
  95. for (i=0; i < 1000; i++) {
  96. usecSingle = 0;
  97. f1.read(buffer, 512);
  98. usec = usecSingle;
  99. if (usec > usecMax) usecMax = usec;
  100. }
  101. reportSpeed(1, 1000, usecTotal, usecMax);
  102. } else {
  103. Serial.println("SDTEST1.WAV is too small for speed testing");
  104. }
  105. } else {
  106. Serial.println("Unable to find SDTEST1.WAV on this card");
  107. return;
  108. }
  109. // Speed test reading two files
  110. if (f2) {
  111. Serial.println();
  112. Serial.println("Reading SDTEST1.WAV & SDTEST2.WAV:");
  113. if (f2.size() >= 514048) {
  114. f1.seek(0);
  115. usecMax = 0;
  116. usecTotal = 0;
  117. for (i=0; i < 1000; i++) {
  118. usecSingle = 0;
  119. f1.read(buffer, 512);
  120. f2.read(buffer, 512);
  121. usec = usecSingle;
  122. if (usec > usecMax) usecMax = usec;
  123. }
  124. reportSpeed(2, 1000, usecTotal, usecMax);
  125. Serial.println();
  126. Serial.println("Reading SDTEST1.WAV & SDTEST2.WAV staggered:");
  127. f1.seek(0);
  128. f2.seek(0);
  129. f1.read(buffer, 512);
  130. usecMax = 0;
  131. usecTotal = 0;
  132. for (i=0; i < 1000; i++) {
  133. usecSingle = 0;
  134. f1.read(buffer, 512);
  135. f2.read(buffer, 512);
  136. usec = usecSingle;
  137. if (usec > usecMax) usecMax = usec;
  138. }
  139. reportSpeed(2, 1000, usecTotal, usecMax);
  140. } else {
  141. Serial.println("SDTEST2.WAV is too small for speed testing");
  142. }
  143. } else {
  144. Serial.println("Unable to find SDTEST2.WAV on this card");
  145. return;
  146. }
  147. // Speed test reading three files
  148. if (f3) {
  149. Serial.println();
  150. Serial.println("Reading SDTEST1.WAV, SDTEST2.WAV, SDTEST3.WAV:");
  151. if (f3.size() >= 514048) {
  152. f1.seek(0);
  153. f2.seek(0);
  154. usecMax = 0;
  155. usecTotal = 0;
  156. for (i=0; i < 1000; i++) {
  157. usecSingle = 0;
  158. f1.read(buffer, 512);
  159. f2.read(buffer, 512);
  160. f3.read(buffer, 512);
  161. usec = usecSingle;
  162. if (usec > usecMax) usecMax = usec;
  163. }
  164. reportSpeed(3, 1000, usecTotal, usecMax);
  165. Serial.println();
  166. Serial.println("Reading SDTEST1.WAV, SDTEST2.WAV, SDTEST3.WAV staggered:");
  167. f1.seek(0);
  168. f2.seek(0);
  169. f3.seek(0);
  170. f1.read(buffer, 512);
  171. f1.read(buffer, 512);
  172. f2.read(buffer, 512);
  173. usecMax = 0;
  174. usecTotal = 0;
  175. for (i=0; i < 1000; i++) {
  176. usecSingle = 0;
  177. f1.read(buffer, 512);
  178. f2.read(buffer, 512);
  179. f3.read(buffer, 512);
  180. usec = usecSingle;
  181. if (usec > usecMax) usecMax = usec;
  182. }
  183. reportSpeed(3, 1000, usecTotal, usecMax);
  184. } else {
  185. Serial.println("SDTEST3.WAV is too small for speed testing");
  186. }
  187. } else {
  188. Serial.println("Unable to find SDTEST3.WAV on this card");
  189. return;
  190. }
  191. // Speed test reading four files
  192. if (f4) {
  193. Serial.println();
  194. Serial.println("Reading SDTEST1.WAV, SDTEST2.WAV, SDTEST3.WAV, SDTEST4.WAV:");
  195. if (f4.size() >= 514048) {
  196. f1.seek(0);
  197. f2.seek(0);
  198. f3.seek(0);
  199. usecMax = 0;
  200. usecTotal = 0;
  201. for (i=0; i < 1000; i++) {
  202. usecSingle = 0;
  203. f1.read(buffer, 512);
  204. f2.read(buffer, 512);
  205. f3.read(buffer, 512);
  206. f4.read(buffer, 512);
  207. usec = usecSingle;
  208. if (usec > usecMax) usecMax = usec;
  209. }
  210. reportSpeed(4, 1000, usecTotal, usecMax);
  211. Serial.println();
  212. Serial.println("Reading SDTEST1.WAV, SDTEST2.WAV, SDTEST3.WAV, SDTEST4.WAV staggered:");
  213. f1.seek(0);
  214. f2.seek(0);
  215. f3.seek(0);
  216. f4.seek(0);
  217. f1.read(buffer, 512);
  218. f1.read(buffer, 512);
  219. f1.read(buffer, 512);
  220. f2.read(buffer, 512);
  221. f2.read(buffer, 512);
  222. f3.read(buffer, 512);
  223. usecMax = 0;
  224. usecTotal = 0;
  225. for (i=0; i < 1000; i++) {
  226. usecSingle = 0;
  227. f1.read(buffer, 512);
  228. f2.read(buffer, 512);
  229. f3.read(buffer, 512);
  230. f4.read(buffer, 512);
  231. usec = usecSingle;
  232. if (usec > usecMax) usecMax = usec;
  233. }
  234. reportSpeed(4, 1000, usecTotal, usecMax);
  235. } else {
  236. Serial.println("SDTEST4.WAV is too small for speed testing");
  237. }
  238. } else {
  239. Serial.println("Unable to find SDTEST4.WAV on this card");
  240. return;
  241. }
  242. }
  243. unsigned long maximum(unsigned long a, unsigned long b,
  244. unsigned long c, unsigned long d)
  245. {
  246. if (b > a) a = b;
  247. if (c > a) a = c;
  248. if (d > a) a = d;
  249. return a;
  250. }
  251. void reportSpeed(unsigned int numFiles, unsigned long blockCount, unsigned long usecTotal, unsigned long usecMax)
  252. {
  253. float bytesPerSecond = (float)(blockCount * 512 * numFiles) / usecTotal;
  254. Serial.print(" Overall speed = ");
  255. Serial.print(bytesPerSecond);
  256. Serial.println(" Mbyte/sec");
  257. Serial.print(" Worst block time = ");
  258. Serial.print((float)usecMax / 1000.0);
  259. Serial.println(" ms");
  260. Serial.print(" ");
  261. Serial.print( (float)usecMax / 29.01333);
  262. Serial.println("% of audio frame time");
  263. }
  264. void loop(void) {
  265. // do nothing after the test
  266. }