Add FAT16 library
[rmslog.git] / FAT16 / examples / FAT16_ReadExample / applet / FAT16_ReadExample.cpp
1 /*
2 FAT16 ReadFile Example
3 SparkFun Electronics
4 Written by Ryan Owens
5 3/16/2010
6
7 Code Description: Uses an Arduino Duemillanove or Arduino Pro to read the file contents of each file on an SD card.
8
9 Circuit Description: Uses the SparkFun microSD shield. (http://www.sparkfun.com/commerce/product_info.php?products_id=9520)
10
11 Attributions: Special thanks to Roland Riegel for providing an open source FAT library 
12 for AVR microcontrollers. See more of his projects here:
13 http://www.roland-riegel.de/
14
15 This code is provided under the Creative Commons Attribution License. More information can be found here:
16 http://creativecommons.org/licenses/by/3.0/
17
18 (Use our code freely! Please just remember to give us credit where it's due. Thanks!)
19 */
20
21 //Add libraries to support FAT16 on the SD Card.
22 //(Note: If you already have these libraries installed in the directory, they'll have to remove in order to compile this.)
23 #include <byteordering.h>
24 #include <fat.h>
25 #include <fat_config.h>
26 #include <partition.h>
27 #include <partition_config.h>
28 #include <sd-reader_config.h>
29 #include <sd_raw.h>
30 #include <sd_raw_config.h>
31
32 //Define the pin numbers
33 #define CS    8
34 #define MOSI    11
35 #define MISO    12
36 #define SCK    13
37
38 //This is the amount of data to be fetched from the SD card for each read.
39 #define BUFFERSIZE      256
40
41 #include "WProgram.h"
42 void setup();
43 void loop();
44 uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry);
45 struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name);
46 char init_filesystem(void);
47 char get_next_filename(struct fat_dir_struct* cur_dir, char * new_file);
48 unsigned char buffer[BUFFERSIZE];
49 char file_name[30];
50
51 struct fat_dir_struct* dd;              //FAT16 directory
52 struct fat_dir_entry_struct dir_entry;  //FAT16 directory entry (A.K.A. a file)
53
54 struct fat_fs_struct* fs;               //FAT16 File System
55 struct partition_struct* partition;     //FAT16 Partition
56
57 struct fat_file_struct * file_handle;   //FAT16 File Handle
58
59 void setup()
60 {
61     //Set up the pins for the Serial communication
62     pinMode(0, INPUT);
63     pinMode(1, OUTPUT);
64     Serial.begin(9600);
65  
66     //Set up the pins for the microSD shield
67     pinMode(CS, OUTPUT);
68     pinMode(MOSI, OUTPUT);
69     pinMode(MISO, INPUT);
70     pinMode(SCK, OUTPUT);
71     pinMode(10, OUTPUT);
72 }
73
74 void loop()
75 {
76     int bytes_read=0; //Keeps track of how many bytes are read when accessing a file on the SD card.
77
78     init_filesystem();  //Initialize the FAT16 file system on the SD card.
79   
80     //Get the next file in the directory
81     while(get_next_filename(dd, file_name)){
82         //Open the file 
83         file_handle=open_file_in_dir(fs, dd, file_name);
84         //Read up to 512 bytes from the file
85         bytes_read = fat_read_file(file_handle, buffer, BUFFERSIZE);
86         //Print whatever we just got from the file
87         Serial.println((const char *)buffer);
88         //Keep reading from the file until we reach the end (nothing more is read from the file)
89         while(bytes_read > 0){
90             //If there's more to be read from the file, go get it.
91             bytes_read = fat_read_file(file_handle, buffer, BUFFERSIZE);
92             //Print the contents that have been read
93             Serial.println((const char *)buffer);
94         }
95         //Close the file before moving on to the next one.
96         fat_close_file(file_handle);
97         delay(1000);
98         Serial.println("Next File...");
99     }
100     while(1);
101 }
102
103 uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry)
104 {
105         fat_reset_dir(dd);      //Make sure to start from the beginning of the directory!
106     while(fat_read_dir(dd, dir_entry))
107     {
108         if(strcmp(dir_entry->long_name, name) == 0)
109         {
110             //fat_reset_dir(dd);
111             return 1;
112         }
113     }
114
115     return 0;
116 }
117
118 struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name)
119 {
120     struct fat_dir_entry_struct file_entry;
121     if(!find_file_in_dir(fs, dd, name, &file_entry))
122         return 0;
123
124     return fat_open_file(fs, &file_entry);
125 }
126
127 char init_filesystem(void)
128 {
129         //setup sd card slot 
130         if(!sd_raw_init())
131         {
132                 return 0;
133         }
134
135         //open first partition
136         partition = partition_open(sd_raw_read,
137                                                                         sd_raw_read_interval,
138 #if SD_RAW_WRITE_SUPPORT
139                                                                         sd_raw_write,
140                                                                         sd_raw_write_interval,
141 #else
142                                                                         0,
143                                                                         0,
144 #endif
145                                                                         0
146                                                            );
147
148         if(!partition)
149         {
150                 //If the partition did not open, assume the storage device
151                 //is a "superfloppy", i.e. has no MBR.
152                 partition = partition_open(sd_raw_read,
153                                                                    sd_raw_read_interval,
154 #if SD_RAW_WRITE_SUPPORT
155                                                                    sd_raw_write,
156                                                                    sd_raw_write_interval,
157 #else
158                                                                    0,
159                                                                    0,
160 #endif
161                                                                    -1
162                                                                   );
163                 if(!partition)
164                 {
165                         return 0;
166                 }
167         }
168
169         //Open file system
170         fs = fat_open(partition);
171         if(!fs)
172         {
173                 return 0;
174         }
175
176         //Open root directory
177         fat_get_dir_entry_of_path(fs, "/", &dir_entry);
178         dd=fat_open_dir(fs, &dir_entry);
179         
180         if(!dd)
181         {
182                 return 0;
183         }
184         return 1;
185 }
186
187 char get_next_filename(struct fat_dir_struct* cur_dir, char * new_file)
188 {
189     //'dir_entry' is a global variable of type directory_entry_struct
190
191     //Get the next file from the root directory
192     if(fat_read_dir(cur_dir, &dir_entry))
193     {
194         sprintf(new_file, "%s", dir_entry.long_name);
195         Serial.println((const char *)new_file);
196         return 1;
197     }
198     //If another file isn't found, return 0
199     return 0;
200 }
201
202
203
204 int main(void)
205 {
206         init();
207
208         setup();
209     
210         for (;;)
211                 loop();
212         
213         return 0;
214 }
215