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 | #ifndef Fat16_h
|
---|
21 | #define Fat16_h
|
---|
22 | /**
|
---|
23 | * \file
|
---|
24 | * Fat16 class
|
---|
25 | */
|
---|
26 | #include <string.h>
|
---|
27 | #include <avr/pgmspace.h>
|
---|
28 | #include <Print.h>
|
---|
29 | #include <SdCard.h>
|
---|
30 | #include <FatStructs.h>
|
---|
31 | #include <Fat16Config.h>
|
---|
32 | //------------------------------------------------------------------------------
|
---|
33 | /** Fat16 version YYYYMMDD */
|
---|
34 | #define FAT16_VERSION 20111205
|
---|
35 | //------------------------------------------------------------------------------
|
---|
36 | // flags for ls()
|
---|
37 | /** ls() flag to print modify date */
|
---|
38 | uint8_t const LS_DATE = 1;
|
---|
39 | /** ls() flag to print file size */
|
---|
40 | uint8_t const LS_SIZE = 2;
|
---|
41 |
|
---|
42 | // use the gnu style oflags
|
---|
43 | /** open for reading */
|
---|
44 | uint8_t const O_READ = 0X01;
|
---|
45 | /** same as O_READ */
|
---|
46 | uint8_t const O_RDONLY = O_READ;
|
---|
47 | /** open for write */
|
---|
48 | uint8_t const O_WRITE = 0X02;
|
---|
49 | /** same as O_WRITE */
|
---|
50 | uint8_t const O_WRONLY = O_WRITE;
|
---|
51 | /** open for reading and writing */
|
---|
52 | uint8_t const O_RDWR = O_READ | O_WRITE;
|
---|
53 | /** mask for access modes */
|
---|
54 | uint8_t const O_ACCMODE = O_READ | O_WRITE;
|
---|
55 | /** The file offset shall be set to the end of the file prior to each write. */
|
---|
56 | uint8_t const O_APPEND = 0X04;
|
---|
57 | /** synchronous writes - call sync() after each write */
|
---|
58 | uint8_t const O_SYNC = 0X08;
|
---|
59 | /** create the file if nonexistent */
|
---|
60 | uint8_t const O_CREAT = 0X10;
|
---|
61 | /** If O_CREAT and O_EXCL are set, open() shall fail if the file exists */
|
---|
62 | uint8_t const O_EXCL = 0X20;
|
---|
63 | /** truncate the file to zero length */
|
---|
64 | uint8_t const O_TRUNC = 0X40;
|
---|
65 |
|
---|
66 | // flags for timestamp
|
---|
67 | /** set the file's last access date */
|
---|
68 | uint8_t const T_ACCESS = 1;
|
---|
69 | /** set the file's creation date and time */
|
---|
70 | uint8_t const T_CREATE = 2;
|
---|
71 | /** Set the file's write date and time */
|
---|
72 | uint8_t const T_WRITE = 4;
|
---|
73 |
|
---|
74 | /** date field for FAT directory entry */
|
---|
75 | static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) {
|
---|
76 | return (year - 1980) << 9 | month << 5 | day;
|
---|
77 | }
|
---|
78 | /** year part of FAT directory date field */
|
---|
79 | static inline uint16_t FAT_YEAR(uint16_t fatDate) {
|
---|
80 | return 1980 + (fatDate >> 9);
|
---|
81 | }
|
---|
82 | /** month part of FAT directory date field */
|
---|
83 | static inline uint8_t FAT_MONTH(uint16_t fatDate) {
|
---|
84 | return (fatDate >> 5) & 0XF;
|
---|
85 | }
|
---|
86 | /** day part of FAT directory date field */
|
---|
87 | static inline uint8_t FAT_DAY(uint16_t fatDate) {
|
---|
88 | return fatDate & 0X1F;
|
---|
89 | }
|
---|
90 | /** time field for FAT directory entry */
|
---|
91 | static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) {
|
---|
92 | return hour << 11 | minute << 5 | second >> 1;
|
---|
93 | }
|
---|
94 | /** hour part of FAT directory time field */
|
---|
95 | static inline uint8_t FAT_HOUR(uint16_t fatTime) {
|
---|
96 | return fatTime >> 11;
|
---|
97 | }
|
---|
98 | /** minute part of FAT directory time field */
|
---|
99 | static inline uint8_t FAT_MINUTE(uint16_t fatTime) {
|
---|
100 | return(fatTime >> 5) & 0X3F;
|
---|
101 | }
|
---|
102 | /** second part of FAT directory time field */
|
---|
103 | static inline uint8_t FAT_SECOND(uint16_t fatTime) {
|
---|
104 | return 2*(fatTime & 0X1F);
|
---|
105 | }
|
---|
106 | /** Default date for file timestamps is 1 Jan 2000 */
|
---|
107 | uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
|
---|
108 | /** Default time for file timestamp is 1 am */
|
---|
109 | uint16_t const FAT_DEFAULT_TIME = (1 << 11);
|
---|
110 | //------------------------------------------------------------------------------
|
---|
111 | /**
|
---|
112 | * \typedef fat_t
|
---|
113 | *
|
---|
114 | * \brief Type for FAT16 entry
|
---|
115 | */
|
---|
116 | typedef uint16_t fat_t;
|
---|
117 | /**
|
---|
118 | * \union cache16_t
|
---|
119 | *
|
---|
120 | * \brief Cache buffer data type
|
---|
121 | *
|
---|
122 | */
|
---|
123 | union cache16_t {
|
---|
124 | /** Used to access cached file data blocks. */
|
---|
125 | uint8_t data[512];
|
---|
126 | /** Used to access cached FAT entries. */
|
---|
127 | fat_t fat[256];
|
---|
128 | /** Used to access cached directory entries. */
|
---|
129 | dir_t dir[16];
|
---|
130 | /** Used to access a cached Master Boot Record. */
|
---|
131 | mbr_t mbr;
|
---|
132 | /** Used to access to a cached FAT16 boot sector. */
|
---|
133 | fbs_t fbs;
|
---|
134 | };
|
---|
135 | //------------------------------------------------------------------------------
|
---|
136 | /** \class Fat16
|
---|
137 | * \brief Fat16 implements a minimal Arduino FAT16 Library
|
---|
138 | *
|
---|
139 | * Fat16 does not support subdirectories or long file names.
|
---|
140 | */
|
---|
141 | class Fat16 : public Print {
|
---|
142 | public:
|
---|
143 | /*
|
---|
144 | * Public functions
|
---|
145 | */
|
---|
146 | /** create with file closed */
|
---|
147 | Fat16(void) : flags_(0) {}
|
---|
148 | /** \return The current cluster number. */
|
---|
149 | fat_t curCluster(void) const {return curCluster_;}
|
---|
150 | uint8_t close(void);
|
---|
151 | /** \return The count of clusters in the FAT16 volume. */
|
---|
152 | static fat_t clusterCount(void) {return clusterCount_;}
|
---|
153 | /** \return The number of 512 byte blocks in a cluster */
|
---|
154 | static uint8_t clusterSize(void) {return blocksPerCluster_;}
|
---|
155 | /** \return The current file position. */
|
---|
156 | uint32_t curPosition(void) const {return curPosition_;}
|
---|
157 | /**
|
---|
158 | * Set the date/time callback function
|
---|
159 | *
|
---|
160 | * \param[in] dateTime The user's callback function. The callback
|
---|
161 | * function is of the form:
|
---|
162 | *
|
---|
163 | * \code
|
---|
164 | * void dateTime(uint16_t* date, uint16_t* time) {
|
---|
165 | * uint16_t year;
|
---|
166 | * uint8_t month, day, hour, minute, second;
|
---|
167 | *
|
---|
168 | * // User gets date and time from GPS or real-time clock here
|
---|
169 | *
|
---|
170 | * // return date using FAT_DATE macro to format fields
|
---|
171 | * *date = FAT_DATE(year, month, day);
|
---|
172 | *
|
---|
173 | * // return time using FAT_TIME macro to format fields
|
---|
174 | * *time = FAT_TIME(hour, minute, second);
|
---|
175 | * }
|
---|
176 | * \endcode
|
---|
177 | *
|
---|
178 | * Sets the function that is called when a file is created or when
|
---|
179 | * a file's directory entry is modified by sync(). All timestamps,
|
---|
180 | * access, creation, and modify, are set when a file is created.
|
---|
181 | * sync() maintains the last access date and last modify date/time.
|
---|
182 | *
|
---|
183 | * See the timestamp() function.
|
---|
184 | */
|
---|
185 | static void dateTimeCallback(
|
---|
186 | void (*dateTime)(uint16_t* date, uint16_t* time)) {
|
---|
187 | dateTime_ = dateTime;
|
---|
188 | }
|
---|
189 | /**
|
---|
190 | * Cancel the date/time callback function.
|
---|
191 | */
|
---|
192 | static void dateTimeCallbackCancel(void) {dateTime_ = NULL;}
|
---|
193 | uint8_t dirEntry(dir_t* dir);
|
---|
194 |
|
---|
195 | /** \return The file's size in bytes. */
|
---|
196 | uint32_t fileSize(void) const {return fileSize_;}
|
---|
197 | static uint8_t init(SdCard* dev, uint8_t part);
|
---|
198 | /**
|
---|
199 | * Initialize a FAT16 volume.
|
---|
200 | *
|
---|
201 | * First try partition 1 then try super floppy format.
|
---|
202 | *
|
---|
203 | * \param[in] dev The SdCard where the volume is located.
|
---|
204 | *
|
---|
205 | * \return The value one, true, is returned for success and
|
---|
206 | * the value zero, false, is returned for failure. reasons for
|
---|
207 | * failure include not finding a valid FAT16 file system, a call
|
---|
208 | * to init() after a volume has been successful initialized or
|
---|
209 | * an I/O error.
|
---|
210 | *
|
---|
211 | */
|
---|
212 | static uint8_t init(SdCard* dev) {
|
---|
213 | return init(dev, 1) ? true : init(dev, 0);
|
---|
214 | }
|
---|
215 | /**
|
---|
216 | * Checks the file's open/closed status for this instance of Fat16.
|
---|
217 | * \return The value true if a file is open otherwise false;
|
---|
218 | */
|
---|
219 | uint8_t isOpen(void) const {return (flags_ & O_ACCMODE) != 0;}
|
---|
220 | static void ls(uint8_t flags = 0);
|
---|
221 | uint8_t open(const char* fileName, uint8_t oflag);
|
---|
222 | uint8_t open(uint16_t entry, uint8_t oflag);
|
---|
223 | static void printDirName(const dir_t& dir, uint8_t width);
|
---|
224 | static void printFatDate(uint16_t fatDate);
|
---|
225 | static void printFatTime(uint16_t fatTime);
|
---|
226 | static void printTwoDigits(uint8_t v);
|
---|
227 | int16_t read(void);
|
---|
228 | int16_t read(void* buf, uint16_t nbyte);
|
---|
229 | static uint8_t readDir(dir_t* dir, uint16_t* index,
|
---|
230 | uint8_t skip = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY));
|
---|
231 |
|
---|
232 | uint8_t remove(void);
|
---|
233 | static uint8_t remove(const char* fileName);
|
---|
234 | /** Sets the file's current position to zero. */
|
---|
235 | void rewind(void) {curPosition_ = curCluster_ = 0;}
|
---|
236 | /** \return The number of entries in the root directory. */
|
---|
237 | static uint16_t rootDirEntryCount(void) {return rootDirEntryCount_;}
|
---|
238 | /** Seek to current position plus \a pos bytes. See Fat16::seekSet(). */
|
---|
239 | uint8_t seekCur(uint32_t pos) {return seekSet(curPosition_ + pos);}
|
---|
240 | /** Seek to end of file. See Fat16::seekSet(). */
|
---|
241 | uint8_t seekEnd(void) {return seekSet(fileSize_);}
|
---|
242 | uint8_t seekSet(uint32_t pos);
|
---|
243 | uint8_t sync(void);
|
---|
244 | uint8_t timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
|
---|
245 | uint8_t hour, uint8_t minute, uint8_t second);
|
---|
246 | uint8_t truncate(uint32_t size);
|
---|
247 | /** Fat16::writeError is set to true if an error occurs during a write().
|
---|
248 | * Set Fat16::writeError to false before calling print() and/or write() and check
|
---|
249 | * for true after calls to write() and/or print().
|
---|
250 | */
|
---|
251 | bool writeError;
|
---|
252 | int16_t write(const void *buf, uint16_t nbyte);
|
---|
253 | #if ARDUINO < 100
|
---|
254 | void write(uint8_t b);
|
---|
255 | void write(const char* str);
|
---|
256 | #else // ARDUINO < 100
|
---|
257 | size_t write(uint8_t b);
|
---|
258 | int16_t write(const char* str);
|
---|
259 | #endif // ARDUINO < 100
|
---|
260 | void write_P(PGM_P str);
|
---|
261 | void writeln_P(PGM_P str);
|
---|
262 | //------------------------------------------------------------------------------
|
---|
263 | #if FAT16_DEBUG_SUPPORT
|
---|
264 | /** For debug only. Do not use in applications. */
|
---|
265 | static cache16_t* dbgBufAdd(void) {return &cacheBuffer_;}
|
---|
266 | /** For debug only. Do not use in applications. */
|
---|
267 | static void dbgSetDev(SdCard* dev) {rawDev_ = dev;}
|
---|
268 | /** For debug only. Do not use in applications. */
|
---|
269 | static uint8_t* dbgCacheBlock(uint32_t blockNumber) {
|
---|
270 | return cacheRawBlock(blockNumber) ? cacheBuffer_.data : 0; }
|
---|
271 | /** For debug only. Do not use in applications. */
|
---|
272 | static dir_t* dbgCacheDir(uint16_t index) {
|
---|
273 | return cacheDirEntry(index);}
|
---|
274 | #endif // FAT16_DEBUG_SUPPORT
|
---|
275 | //------------------------------------------------------------------------------
|
---|
276 | #if ALLOW_DEPRECATED_FUNCTIONS
|
---|
277 | // Deprecated functions - suppress cpplint messages with NOLINT comment
|
---|
278 | public:
|
---|
279 | /**
|
---|
280 | * Deprecated - Use:
|
---|
281 | * static void Fat16::dateTimeCallback(
|
---|
282 | * void (*dateTime)(uint16_t* date, uint16_t* time));
|
---|
283 | */
|
---|
284 | static void dateTimeCallback(
|
---|
285 | void (*dateTime)(uint16_t& date, uint16_t& time)) { // NOLINT
|
---|
286 | oldDateTime_ = dateTime;
|
---|
287 | dateTime_ = dateTime ? oldToNew : 0;
|
---|
288 | }
|
---|
289 | /** Deprecated - Use: uint8_t Fat16::dirEntry(dir_t* dir); */
|
---|
290 | uint8_t dirEntry(dir_t& dir) { // NOLINT
|
---|
291 | return dirEntry(&dir);
|
---|
292 | }
|
---|
293 | /** Deprecated - Use: static uint8_t Fat16::init(SdCard *dev); */
|
---|
294 | static uint8_t init(SdCard& dev) {return init(&dev);} // NOLINT
|
---|
295 |
|
---|
296 | /** Deprecated - Use: static uint8_t Fat16::init(SdCard *dev, uint8_t part) */
|
---|
297 | static uint8_t init(SdCard& dev, uint8_t part) { // NOLINT
|
---|
298 | return init(&dev, part);
|
---|
299 | }
|
---|
300 | /**
|
---|
301 | * Deprecated - Use:
|
---|
302 | * uint8_t Fat16::readDir(dir_t* dir, uint16_t* index, uint8_t skip);
|
---|
303 | */
|
---|
304 | static uint8_t readDir(dir_t& dir, uint16_t& index, // NOLINT
|
---|
305 | uint8_t skip = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY)) {
|
---|
306 | return readDir(&dir, &index, skip);
|
---|
307 | }
|
---|
308 | //------------------------------------------------------------------------------
|
---|
309 | private:
|
---|
310 | static void (*oldDateTime_)(uint16_t& date, uint16_t& time); // NOLINT
|
---|
311 | static void oldToNew(uint16_t *date, uint16_t *time) {
|
---|
312 | uint16_t d;
|
---|
313 | uint16_t t;
|
---|
314 | oldDateTime_(d, t);
|
---|
315 | *date = d;
|
---|
316 | *time = t;
|
---|
317 | }
|
---|
318 | #endif // ALLOW_DEPRECATED_FUNCTIONS
|
---|
319 | //------------------------------------------------------------------------------
|
---|
320 | private:
|
---|
321 | // Volume info
|
---|
322 | static uint8_t volumeInitialized_; // true if volume has been initialized
|
---|
323 | static uint8_t fatCount_; // number of FATs
|
---|
324 | static uint8_t blocksPerCluster_; // must be power of 2
|
---|
325 | static uint16_t rootDirEntryCount_; // should be 512 for FAT16
|
---|
326 | static fat_t blocksPerFat_; // number of blocks in one FAT
|
---|
327 | static fat_t clusterCount_; // total clusters in volume
|
---|
328 | static uint32_t fatStartBlock_; // start of first FAT
|
---|
329 | static uint32_t rootDirStartBlock_; // start of root dir
|
---|
330 | static uint32_t dataStartBlock_; // start of data clusters
|
---|
331 |
|
---|
332 | // block cache
|
---|
333 | static uint8_t const CACHE_FOR_READ = 0; // cache a block for read
|
---|
334 | static uint8_t const CACHE_FOR_WRITE = 1; // cache a block and set dirty
|
---|
335 | static SdCard *rawDev_; // Device
|
---|
336 | static cache16_t cacheBuffer_; // 512 byte cache for raw blocks
|
---|
337 | static uint32_t cacheBlockNumber_; // Logical number of block in the cache
|
---|
338 | static uint8_t cacheDirty_; // cacheFlush() will write block if true
|
---|
339 | static uint32_t cacheMirrorBlock_; // mirror block for second FAT
|
---|
340 |
|
---|
341 | // callback function for date/time
|
---|
342 | static void (*dateTime_)(uint16_t* date, uint16_t* time);
|
---|
343 |
|
---|
344 | // define fields in flags_
|
---|
345 | static uint8_t const F_OFLAG = O_ACCMODE | O_APPEND | O_SYNC;
|
---|
346 | static uint8_t const F_FILE_DIR_DIRTY = 0X80; // require sync directory entry
|
---|
347 |
|
---|
348 | uint8_t flags_; // see above for bit definitions
|
---|
349 | int16_t dirEntryIndex_; // index of directory entry for open file
|
---|
350 | fat_t firstCluster_; // first cluster of file
|
---|
351 | uint32_t fileSize_; // fileSize
|
---|
352 | fat_t curCluster_; // current cluster
|
---|
353 | uint32_t curPosition_; // current byte offset
|
---|
354 |
|
---|
355 | // private functions for cache
|
---|
356 | static uint8_t blockOfCluster(uint32_t position) {
|
---|
357 | // depends on blocks per cluster being power of two
|
---|
358 | return (position >> 9) & (blocksPerCluster_ - 1);
|
---|
359 | }
|
---|
360 | static uint16_t cacheDataOffset(uint32_t position) {return position & 0X1FF;}
|
---|
361 | static dir_t* cacheDirEntry(uint16_t index, uint8_t action = 0);
|
---|
362 | static uint8_t cacheRawBlock(uint32_t blockNumber, uint8_t action = 0);
|
---|
363 | static uint8_t cacheFlush(void);
|
---|
364 | static void cacheSetDirty(void) {cacheDirty_ |= CACHE_FOR_WRITE;}
|
---|
365 | static uint32_t dataBlockLba(fat_t cluster, uint8_t blockOfCluster) {
|
---|
366 | return dataStartBlock_ + (uint32_t)(cluster - 2) * blocksPerCluster_
|
---|
367 | + blockOfCluster;
|
---|
368 | }
|
---|
369 | static uint8_t fatGet(fat_t cluster, fat_t* value);
|
---|
370 | static uint8_t fatPut(fat_t cluster, fat_t value);
|
---|
371 | // end of chain test
|
---|
372 | static uint8_t isEOC(fat_t cluster) {return cluster >= 0XFFF8;}
|
---|
373 | // allocate a cluster to a file
|
---|
374 | uint8_t addCluster(void);
|
---|
375 | // free a cluster chain
|
---|
376 | uint8_t freeChain(fat_t cluster);
|
---|
377 | };
|
---|
378 | #endif // Fat16_h
|
---|