Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SdFatmainpage.h 14KB

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