1 | /* Arduino FAT16 Library
|
---|
2 | * Copyright (C) 2008 by William Greiman
|
---|
3 | *
|
---|
4 | * This file is part of the Arduino FAT16 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 Fat16 Library. If not, see
|
---|
18 | * <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | /**
|
---|
22 | \mainpage Arduino Fat16 Library
|
---|
23 | <CENTER>Copyright © 2008 by William Greiman
|
---|
24 | </CENTER>
|
---|
25 |
|
---|
26 | \section Intro Introduction
|
---|
27 | The Arduino Fat16 Library is a minimal implementation of the FAT16 file system
|
---|
28 | on standard SD flash memory cards. Fat16 supports read, write, file
|
---|
29 | creation, deletion, and truncation.
|
---|
30 |
|
---|
31 | The Fat16 class only supports access to files in the root directory and only
|
---|
32 | supports short 8.3 names. Directory time and date fields for creation
|
---|
33 | and modification can be maintained by providing a date/time callback
|
---|
34 | function \link Fat16::dateTimeCallback() dateTimeCallback()\endlink
|
---|
35 | or calling \link Fat16::timestamp() timestamp()\endlink.
|
---|
36 |
|
---|
37 | Fat16 was designed to use the Arduino Print class which
|
---|
38 | allows files to be written with \link Print::print() print() \endlink and
|
---|
39 | \link Print::println() println()\endlink.
|
---|
40 |
|
---|
41 | \section comment Bugs and Comments
|
---|
42 |
|
---|
43 | If you wish to report bugs or have comments, send email to fat16lib@sbcglobal.net.
|
---|
44 |
|
---|
45 |
|
---|
46 | \section SDcard SD Cards
|
---|
47 |
|
---|
48 | Arduinos access SD cards using the cards SPI protocol. PCs, Macs, and
|
---|
49 | most consumer devices use the 4-bit parallel SD protocol. A card that
|
---|
50 | functions well on A PC or Mac may not work well on the Arduino.
|
---|
51 |
|
---|
52 | Most cards have good SPI read performance but cards vary widely in SPI
|
---|
53 | write performance. Write performance is limited by how efficiently the
|
---|
54 | card manages internal erase/remapping operations. The Arduino cannot
|
---|
55 | optimize writes to reduce erase operations because of its limit RAM.
|
---|
56 |
|
---|
57 | SanDisk cards generally have good write performance. They seem to have
|
---|
58 | more internal RAM buffering than other cards and therefore can limit
|
---|
59 | the number of flash erase operations that the Arduino forces due to its
|
---|
60 | limited RAM.
|
---|
61 |
|
---|
62 | Some Dane-Elec cards have a write speed that is only 20% as fast as
|
---|
63 | a good SanDisk card.
|
---|
64 |
|
---|
65 |
|
---|
66 | \section Hardware Hardware Configuration
|
---|
67 | Fat16 was developed using an <A HREF = "http://www.adafruit.com/"> Adafruit Industries</A>
|
---|
68 | <A HREF = "http://ladyada.net/make/gpsshield/modules.html"> GPS Shield</A>.
|
---|
69 |
|
---|
70 | The hardware interface to the SD card should not use a resistor based level
|
---|
71 | shifter. SdCard::init() sets the SPI bus frequency to 8 MHz which results in
|
---|
72 | signal rise times that are too slow for the edge detectors in many newer SD card
|
---|
73 | controllers when resistor voltage dividers are used.
|
---|
74 |
|
---|
75 | The 5 to 3.3 V level shifter for 5 V arduinos should be IC based like the
|
---|
76 | 74HC4050N based circuit shown in the file SdLevel.png. The Adafruit Wave Shield
|
---|
77 | uses a 74AHC125N. Gravitech sells SD and MicroSD Card Adapters based on the
|
---|
78 | 74LCX245.
|
---|
79 |
|
---|
80 | If you are using a resistor based level shifter and are having problems try
|
---|
81 | setting the SPI bus frequency to 4 MHz. This can be done by using
|
---|
82 | card.init(true) to initialize the SD card.
|
---|
83 |
|
---|
84 |
|
---|
85 | \section Fat16Class Fat16 Usage
|
---|
86 |
|
---|
87 | The class Fat16 is a minimal implementation of FAT16 on standard SD cards.
|
---|
88 | High Capacity SD cards, SDHC, are not supported. It should work on all
|
---|
89 | standard cards from 8MB to 2GB formatted with a FAT16 file system.
|
---|
90 |
|
---|
91 | \note
|
---|
92 | The Arduino Print class uses character
|
---|
93 | at a time writes so it was necessary to use a \link Fat16::sync() sync() \endlink
|
---|
94 | function to control when data is written to the SD card.
|
---|
95 |
|
---|
96 | \par
|
---|
97 | An application which writes to a file using \link Print::print() print()\endlink,
|
---|
98 | \link Print::println() println() \endlink
|
---|
99 | or \link Fat16::write write() \endlink must call \link Fat16::sync() sync() \endlink
|
---|
100 | at the appropriate time to force data and directory information to be written
|
---|
101 | to the SD Card. Data and directory information are also written to the SD card
|
---|
102 | when \link Fat16::close() close() \endlink is called.
|
---|
103 |
|
---|
104 | \par
|
---|
105 | Applications must use care calling \link Fat16::sync() sync() \endlink
|
---|
106 | since 2048 bytes of I/O is required to update file and
|
---|
107 | directory information. This includes writing the current data block, reading
|
---|
108 | the block that contains the directory entry for update, writing the directory
|
---|
109 | block back and reading back the current data block.
|
---|
110 |
|
---|
111 | Fat16 only supports access to files in the root directory and only supports
|
---|
112 | short 8.3 names.
|
---|
113 |
|
---|
114 | It is possible to open a file with two or more instances of Fat16. A file may
|
---|
115 | be corrupted if data is written to the file by more than one instance of Fat16.
|
---|
116 |
|
---|
117 | Short names are limited to 8 characters followed by an optional period (.)
|
---|
118 | and extension of up to 3 characters. The characters may be any combination
|
---|
119 | of letters and digits. The following special characters are also allowed:
|
---|
120 |
|
---|
121 | $ % ' - _ @ ~ ` ! ( ) { } ^ # &
|
---|
122 |
|
---|
123 | Short names are always converted to upper case and their original case
|
---|
124 | value is lost.
|
---|
125 |
|
---|
126 | Fat16 uses a slightly restricted form of short names.
|
---|
127 | Only printable ASCII characters are supported. No characters with code point
|
---|
128 | values greater than 127 are allowed. Space is not allowed even though space
|
---|
129 | was allowed in the API of early versions of DOS.
|
---|
130 |
|
---|
131 | Fat16 has been optimized for The Arduino ATmega168. Minimizing RAM use is the
|
---|
132 | highest priority goal followed by flash use and finally performance.
|
---|
133 | Most SD cards only support 512 byte block write operations so a 512 byte
|
---|
134 | cache buffer is used by Fat16. This is the main use of RAM. A small
|
---|
135 | amount of RAM is used to store key volume and file information.
|
---|
136 | Flash memory usage can be controlled by selecting options in Fat16Config.h.
|
---|
137 |
|
---|
138 | \section HowTo How to format SD Cards as FAT16 Volumes
|
---|
139 |
|
---|
140 | Microsoft operating systems support removable media formatted with a
|
---|
141 | Master Boot Record, MBR, or formatted as a super floppy with a FAT Boot Sector
|
---|
142 | in block zero.
|
---|
143 |
|
---|
144 | Microsoft operating systems expect MBR formatted removable media
|
---|
145 | to have only one partition. The first partition should be used.
|
---|
146 |
|
---|
147 | Microsoft operating systems do not support partitioning SD flash cards.
|
---|
148 | If you erase an SD card with a program like KillDisk, Most versions of
|
---|
149 | Windows will format the card as a super floppy.
|
---|
150 |
|
---|
151 | The best way to restore an SD card's MBR is to use SDFormatter
|
---|
152 | which can be downloaded from:
|
---|
153 |
|
---|
154 | http://www.sdcard.org/consumers/formatter/
|
---|
155 |
|
---|
156 | SDFormatter does not have an option for FAT type so it may format
|
---|
157 | small cards as FAT12.
|
---|
158 |
|
---|
159 | After the MBR is restored by SDFormatter you may need to reformat small
|
---|
160 | cards that have been formatted FAT12 to force the volume type to be FAT16.
|
---|
161 |
|
---|
162 | The FAT type, FAT12, FAT16, or FAT32, is determined by the count
|
---|
163 | of clusters on the volume and nothing else.
|
---|
164 |
|
---|
165 | Microsoft published the following code for determining FAT type:
|
---|
166 |
|
---|
167 | \code
|
---|
168 | if (CountOfClusters < 4085) {
|
---|
169 | // Volume is FAT12
|
---|
170 | }
|
---|
171 | else if (CountOfClusters < 65525) {
|
---|
172 | // Volume is FAT16
|
---|
173 | }
|
---|
174 | else {
|
---|
175 | // Volume is FAT32
|
---|
176 | }
|
---|
177 |
|
---|
178 | \endcode
|
---|
179 | If you format a FAT volume with an OS utility , choose a cluster size that
|
---|
180 | will result in:
|
---|
181 |
|
---|
182 | 4084 < CountOfClusters && CountOfClusters < 65525
|
---|
183 |
|
---|
184 | The volume will then be FAT16.
|
---|
185 |
|
---|
186 | If you are formatting an SD card on OS X or Linux, be sure to use the first
|
---|
187 | partition. Format this partition with a cluster count in above range.
|
---|
188 |
|
---|
189 | \section References References
|
---|
190 |
|
---|
191 | The Arduino site:
|
---|
192 |
|
---|
193 | http://www.arduino.cc/
|
---|
194 |
|
---|
195 | For more information about FAT file systems see:
|
---|
196 |
|
---|
197 | http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
|
---|
198 |
|
---|
199 | For information about using SD cards as SPI devices see:
|
---|
200 |
|
---|
201 | http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
|
---|
202 |
|
---|
203 | The ATmega328 datasheet:
|
---|
204 |
|
---|
205 | http://www.atmel.com/dyn/resources/prod_documents/doc8161.pdf
|
---|
206 |
|
---|
207 |
|
---|
208 | */
|
---|