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.

404 lines
15KB

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