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.

400 line
15KB

  1. /* Arduino SdFat Library
  2. * Copyright (C) 2012 by William Greiman
  3. *
  4. * This file is part of the Arduino SdFat Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino SdFat Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. \mainpage Arduino %SdFat Library
  22. <CENTER>Copyright &copy; 2012, 2013, 2014, 2015, 2016 by William Greiman
  23. </CENTER>
  24. \section Intro Introduction
  25. The Arduino %SdFat Library is a minimal implementation of FAT16 and FAT32
  26. file systems on SD flash memory cards. Standard SD and high capacity SDHC
  27. cards are supported.
  28. Experimental support for FAT12 can be enabled by setting FAT12_SUPPORT
  29. nonzero in SdFatConfig.h.
  30. The %SdFat library supports Long %File Names or short 8.3 names.
  31. Edit the SdFatConfig.h file to select short or long file names.
  32. The main classes in %SdFat are SdFat, SdFatEX, SdFatSoftSpi, SdFatSoftSpiEX,
  33. SdBaseFile, SdFile, File, StdioStream, \ref fstream, \ref ifstream,
  34. and \ref ofstream.
  35. The SdFat, SdFatEX, SdFatSoftSpi and SdFatSoftSpiEX classes maintain a
  36. FAT volume, a current working directory, and simplify initialization
  37. of other classes. The SdFat and SdFatEX classes uses a fast custom hardware SPI
  38. implementation. The SdFatSoftSpi and SdFatSoftSpiEX classes uses software SPI.
  39. the SdFatEX and SdFatSoftSpiEX use extended multi-block I/O for enhanced
  40. performance. These classes must have exclusive use of the SPI bus.
  41. The SdBaseFile class provides basic file access functions such as open(),
  42. binary read(), binary write(), close(), remove(), and sync(). SdBaseFile
  43. is the smallest file class.
  44. The SdFile class has all the SdBaseFile class functions plus the Arduino
  45. Print class functions.
  46. The File class has all the SdBaseFile functions plus the functions in
  47. the Arduino SD.h File class. This provides compatibility with the
  48. Arduino SD.h library.
  49. The StdioStream class implements functions similar to Linux/Unix standard
  50. buffered input/output.
  51. The \ref fstream class implements C++ iostreams for both reading and writing
  52. text files.
  53. The \ref ifstream class implements C++ iostreams for reading text files.
  54. The \ref ofstream class implements C++ iostreams for writing text files.
  55. The classes \ref ifstream, \ref ofstream, \ref istream, and \ref ostream
  56. follow the C++ \ref iostream standard when possible.
  57. There are many tutorials and much documentation about using C++ iostreams
  58. on the web.
  59. http://www.cplusplus.com/ is a good C++ site for learning iostreams.
  60. The classes \ref ibufstream and \ref obufstream format and parse character
  61. strings in memory buffers.
  62. the classes ArduinoInStream and ArduinoOutStream provide iostream functions
  63. for Serial, LiquidCrystal, and other devices.
  64. A number of example are provided in the %SdFat/examples folder. These were
  65. developed to test %SdFat and illustrate its use.
  66. \section Install Installation
  67. You must manually install SdFat by copying the SdFat folder from the download
  68. package to the Arduino libraries folder in your sketch folder.
  69. See the Manual installation section of this guide.
  70. http://arduino.cc/en/Guide/Libraries
  71. \section SDconfig SdFat Configuration
  72. Several configuration options may be changed by editing the SdFatConfig.h
  73. file in the %SdFat folder.
  74. Set USE_LONG_FILE_NAMES nonzero to enable Long %File Names. By default,
  75. Long %File Names are enabled. For the leanest fastest library disable
  76. Long %File Names. Long %File names require extra flash but no extra RAM.
  77. Opening Long %File Names can be slower than opening Short %File Names.
  78. Data read and write performance is not changed by the type of %File Name.
  79. If the symbol ENABLE_EXTENDED_TRANSFER_CLASS is nonzero, the class SdFatEX
  80. will be defined. If the symbol ENABLE_SOFTWARE_SPI_CLASS is also nonzero,
  81. the class SdFatSoftSpiEX will be defined.
  82. These classes used extended multi-block SD I/O for better performance.
  83. the SPI bus may not be shared with other devices in this mode.
  84. Set USE_STANDARD_SPI_LIBRARY and ENABLE_SOFTWARE_SPI_CLASS to
  85. enable various SPI options. set USE_STANDARD_SPI_LIBRARY to use the standard
  86. Arduino SPI library. set ENABLE_SOFTWARE_SPI_CLASS to enable the SdFatSoftSpi
  87. class which uses software SPI.
  88. To enable SD card CRC checking set USE_SD_CRC nonzero.
  89. Set FAT12_SUPPORT nonzero to enable use of FAT12 volumes.
  90. FAT12 has not been well tested and requires additional flash.
  91. \section SDPath Paths and Working Directories
  92. Relative paths in SdFat are resolved in a manner similar to Windows.
  93. Each instance of SdFat has a current directory. In SdFat this directory
  94. is called the volume working directory, vwd. Initially this directory is
  95. the root directory for the volume.
  96. The volume working directory is changed by calling SdFat::chdir(path).
  97. The call sd.chdir("/2014") will change the volume working directory
  98. for sd to "/2014", assuming "/2014" exists.
  99. Relative paths for SdFat member functions are resolved by starting at
  100. the volume working directory.
  101. For example, the call sd.mkdir("April") will create the directory
  102. "/2014/April" assuming the volume working directory is "/2014".
  103. SdFat has a current working directory, cwd, that is used to resolve paths
  104. for file.open() calls.
  105. For a single SD card the current working directory is always the volume
  106. working directory for that card.
  107. For multiple SD cards the current working directory is set to the volume
  108. working directory of a card by calling the SdFat::chvol() member function.
  109. The chvol() call is like the Windows \<drive letter>: command.
  110. The call sd2.chvol() will set the current working directory to the volume
  111. working directory for sd2.
  112. If the volume working directory for sd2 is "/music" the call
  113. file.open("BigBand.wav", O_READ);
  114. will then open "/music/BigBand.wav" on sd2.
  115. The following functions are used to change or get current directories.
  116. See the html documentation for more information.
  117. @code
  118. bool SdFat::chdir(bool set_cwd = false);
  119. bool SdFat::chdir(const char* path, bool set_cwd = false);
  120. void SdFat::chvol();
  121. SdBaseFile* SdFat::vwd();
  122. static SdBaseFile* SdBaseFile::cwd();
  123. @endcode
  124. \section SDcard SD\SDHC Cards
  125. Arduinos access SD cards using the cards SPI protocol. PCs, Macs, and
  126. most consumer devices use the 4-bit parallel SD protocol. A card that
  127. functions well on A PC or Mac may not work well on the Arduino.
  128. Most cards have good SPI read performance but cards vary widely in SPI
  129. write performance. Write performance is limited by how efficiently the
  130. card manages internal erase/remapping operations. The Arduino cannot
  131. optimize writes to reduce erase operations because of its limit RAM.
  132. SanDisk cards generally have good write performance. They seem to have
  133. more internal RAM buffering than other cards and therefore can limit
  134. the number of flash erase operations that the Arduino forces due to its
  135. limited RAM.
  136. \section Hardware Hardware Configuration
  137. %SdFat was developed using an
  138. <A HREF = "http://www.adafruit.com/"> Adafruit Industries</A>
  139. Data Logging Shield.
  140. The hardware interface to the SD card should not use a resistor based level
  141. shifter. %SdFat sets the SPI bus frequency to 8 MHz which results in signal
  142. rise times that are too slow for the edge detectors in many newer SD card
  143. controllers when resistor voltage dividers are used.
  144. The 5 to 3.3 V level shifter for 5 V Arduinos should be IC based like the
  145. 74HC4050N based circuit shown in the file SdLevel.png. The Adafruit Wave Shield
  146. uses a 74AHC125N. Gravitech sells SD and MicroSD Card Adapters based on the
  147. 74LCX245.
  148. If you are using a resistor based level shifter and are having problems try
  149. setting the SPI bus frequency to 4 MHz. This can be done by using
  150. card.init(SPI_HALF_SPEED) to initialize the SD card.
  151. A feature to use software SPI is available. Software SPI is slower
  152. than hardware SPI but allows any digital pins to be used. See
  153. SdFatConfig.h for software SPI definitions.
  154. \section comment Bugs and Comments
  155. If you wish to report bugs or have comments, send email to
  156. fat16lib@sbcglobal.net. If possible, include a simple program that illustrates
  157. the bug or problem.
  158. \section Trouble Troubleshooting
  159. The two example programs QuickStart, and SdInfo are useful for troubleshooting.
  160. A message like this from SdInfo with erorCode 0X1 indicates the SD card
  161. is not seen by SdFat. This is often caused by a wiring error and reformatting
  162. the card will not solve the problem.
  163. <PRE>
  164. cardBegin failed
  165. SD errorCode: 0X1
  166. SD errorData: 0XFF
  167. </PRE>
  168. Here is a similar message from QuickStart:
  169. <PRE>
  170. SD initialization failed.
  171. Do not reformat the card!
  172. Is the card correctly inserted?
  173. Is chipSelect set to the correct value?
  174. Does another SPI device need to be disabled?
  175. Is there a wiring/soldering problem?
  176. errorCode: 0x1, errorData: 0xff
  177. </PRE>
  178. Here is a message from QuickStart that indicates a formatting problem:
  179. <PRE>
  180. Card successfully initialized.
  181. Can't find a valid FAT16/FAT32 partition.
  182. Try reformatting the card. For best results use
  183. the SdFormatter program in SdFat/examples or download
  184. and use SDFormatter from www.sdcard.org/downloads.
  185. </PRE>
  186. The best source of recent information and help is the Arduino forum.
  187. http://arduino.cc/forum/
  188. Also search the Adafruit forum.
  189. http://forums.adafruit.com/
  190. If you are using a Teensy try.
  191. http://forum.pjrc.com/forum.php
  192. \section SdFatClass SdFat Usage
  193. SdFat supports Long File Names. Long names in SdFat are limited to 7-bit
  194. ASCII characters in the range 0X20 - 0XFE The following are reserved characters:
  195. <ul>
  196. <li>< (less than)
  197. <li>> (greater than)
  198. <li>: (colon)
  199. <li>" (double quote)
  200. <li>/ (forward slash)
  201. <li>\ (backslash)
  202. <li>| (vertical bar or pipe)
  203. <li>? (question mark)
  204. <li>* (asterisk)
  205. </ul>
  206. %SdFat uses a slightly restricted form of short names.
  207. Short names are limited to 8 characters followed by an optional period (.)
  208. and extension of up to 3 characters. The characters may be any combination
  209. of letters and digits. The following special characters are also allowed:
  210. $ % ' - _ @ ~ ` ! ( ) { } ^ # &
  211. Short names are always converted to upper case and their original case
  212. value is lost. Files that have a base-name where all characters have the
  213. same case and an extension where all characters have the same case will
  214. display properly. Examples this type name are UPPER.low, lower.TXT,
  215. UPPER.TXT, and lower.txt.
  216. An application which writes to a file using print(), println() or
  217. write() must close the file or call sync() at the appropriate time to
  218. force data and directory information to be written to the SD Card.
  219. Applications must use care calling sync() sync()
  220. since 2048 bytes of I/O is required to update file and
  221. directory information. This includes writing the current data block, reading
  222. the block that contains the directory entry for update, writing the directory
  223. block back and reading back the current data block.
  224. It is possible to open a file with two or more instances of a file object.
  225. A file may be corrupted if data is written to the file by more than one
  226. instance of a file object.
  227. \section HowTo How to format SD Cards as FAT Volumes
  228. The best way to restore an SD card's format on a PC or Mac is to use
  229. SDFormatter which can be downloaded from:
  230. http://www.sdcard.org/downloads
  231. A formatter program, SdFormatter.ino, is included in the
  232. %SdFat/examples/SdFormatter directory. This program attempts to
  233. emulate SD Association's SDFormatter.
  234. SDFormatter aligns flash erase boundaries with file
  235. system structures which reduces write latency and file system overhead.
  236. The PC/Mac SDFormatter does not have an option for FAT type so it may format
  237. very small cards as FAT12. Use the SdFat formatter to force FAT16
  238. formatting of small cards.
  239. Do not format the SD card with an OS utility, OS utilities do not format SD
  240. cards in conformance with the SD standard.
  241. You should use a freshly formatted SD card for best performance. FAT
  242. file systems become slower if many files have been created and deleted.
  243. This is because the directory entry for a deleted file is marked as deleted,
  244. but is not deleted. When a new file is created, these entries must be scanned
  245. before creating the file. Also files can become
  246. fragmented which causes reads and writes to be slower.
  247. \section ExampleFilder Examples
  248. A number of examples are provided in the SdFat/examples folder.
  249. See the html documentation for a list.
  250. To access these examples from the Arduino development environment
  251. go to: %File -> Examples -> %SdFat -> \<program Name\>
  252. Compile, upload to your Arduino and click on Serial Monitor to run
  253. the example.
  254. Here is a list:
  255. AnalogBinLogger - Fast AVR ADC logger - see the AnalogBinLoggerExtras folder.
  256. bench - A read/write benchmark.
  257. dataLogger - A simple modifiable data logger.
  258. DirectoryFunctions - Demo of chdir(), ls(), mkdir(), and rmdir().
  259. fgets - Demo of the fgets read line/string function.
  260. formating - Print a table with various formatting options.
  261. getline - Example of getline from section 27.7.1.3 of the C++ standard.
  262. LongFileName - Example use of openNext, printName, and open by index.
  263. LowLatencyLogger - A data logger for higher data rates. ADC version.
  264. LowLatencyLoggerADXL345 - A data logger for higher data rates. ADXL345 SPI.
  265. LowLatencyLoggerMPU6050 - A data logger for higher data rates. MPU6050 I2C.
  266. OpenNext - Open all files in the root dir and print their filename.
  267. PrintBenchmark - A simple benchmark for printing to a text file.
  268. QuickStart - A program to quickly test your SD card and SD shield/module.
  269. RawWrite - A test of raw write functions for contiguous files.
  270. ReadCsv - Function to read a CSV text file one field at a time.
  271. ReadCsvStream - Read a comma-separated value file using iostream extractors.
  272. ReadCsvArray - Read a two dimensional array from a CSV file.
  273. ReadWrite - Compatibility test of Arduino SD ReadWrite example.
  274. rename - A demo of SdFat::rename(old, new) and SdFile::rename(dirFile, newPath).
  275. SdFormatter - This program will format an SD or SDHC card.
  276. SoftwareSpi - Simple demonstration of the SdFatSoftSpi template class.
  277. SdInfo - Initialize an SD card and analyze its structure for trouble shooting.
  278. StdioBench - Demo and test of stdio style stream.
  279. Timestamp - Sets file create, modify, and access timestamps.
  280. TwoCards - Example using two SD cards.
  281. VolumeFreeSpace - Demonstrate the freeClusterCount() call.
  282. wipe - Example to wipe all data from an already formatted SD.
  283. */