You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 line
7.5KB

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