summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhoekstar <hoekstar@cb376a5e-1013-0410-a455-b6b1f9ac8223>2008-10-28 17:12:51 +0000
committerhoekstar <hoekstar@cb376a5e-1013-0410-a455-b6b1f9ac8223>2008-10-28 17:12:51 +0000
commita3f68d5499ee456a341f9909a96486f2f8883291 (patch)
tree3070b2aeb71d238ec37954f9ecd629e4a4be0149
parentd7b28a963231c2985a627818a5c797cfbe675250 (diff)
downloadreprap-backup-a3f68d5499ee456a341f9909a96486f2f8883291.tar.gz
reprap-backup-a3f68d5499ee456a341f9909a96486f2f8883291.zip
woot. read and write now both work!
git-svn-id: https://reprap.svn.sourceforge.net/svnroot/reprap@2181 cb376a5e-1013-0410-a455-b6b1f9ac8223
-rwxr-xr-xtrunk/users/hoeken/sanguino-motherboard/RepRapSDCard/RepRapSDCard.cpp19
-rwxr-xr-xtrunk/users/hoeken/sanguino-motherboard/RepRapSDCard/RepRapSDCard.h4
-rwxr-xr-xtrunk/users/hoeken/sanguino-motherboard/RepRapSDCard/fat16.cpp174
-rwxr-xr-xtrunk/users/hoeken/sanguino-motherboard/RepRapSDCard/sd_raw.cpp2
-rwxr-xr-xtrunk/users/hoeken/sanguino-motherboard/sd_card_test/sd_card_test.pde42
5 files changed, 195 insertions, 46 deletions
diff --git a/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/RepRapSDCard.cpp b/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/RepRapSDCard.cpp
index 6d712069..474cb07f 100755
--- a/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/RepRapSDCard.cpp
+++ b/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/RepRapSDCard.cpp
@@ -124,12 +124,21 @@ uint8_t RepRapSDCard::create_file(char *name)
struct fat16_dir_entry_struct file_entry;
return fat16_create_file(dd, name, &file_entry);
}
+
+uint8_t RepRapSDCard::reset_file(File f)
+{
+ return fat16_seek_file(f, 0, FAT16_SEEK_SET);
+}
-/*
-uint8_t RepRapSDCard::seek_file(File fd, int32_t *offset, uint8_t whence) {
- return fat16_seek_file(fd, offset, whence);
-}*/
-
+uint8_t RepRapSDCard::seek_file(File f, int32_t *offset, uint8_t whence)
+{
+ return fat16_seek_file(f, offset, whence);
+}
+
+uint16_t RepRapSDCard::read_file(File f, uint8_t* buffer, uint16_t buffer_len)
+{
+ return fat16_read_file(f, buffer, buffer_len);
+}
uint8_t RepRapSDCard::write_file(File f, uint8_t *buff, uint8_t siz)
{
diff --git a/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/RepRapSDCard.h b/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/RepRapSDCard.h
index 071dce6e..2b776290 100755
--- a/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/RepRapSDCard.h
+++ b/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/RepRapSDCard.h
@@ -33,8 +33,10 @@ class RepRapSDCard
File open_file(char *name);
void close_file(File f);
uint8_t create_file(char *name);
- uint8_t write_file(File f, uint8_t *b, uint8_t num);
uint8_t seek_file(File fd, int32_t *offset, uint8_t whence);
+ uint8_t reset_file(File f);
+ uint16_t read_file(File f, uint8_t* buffer, uint16_t buffer_len);
+ uint8_t write_file(File f, uint8_t *b, uint8_t num);
};
#endif
diff --git a/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/fat16.cpp b/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/fat16.cpp
index 6e7a715b..48203848 100755
--- a/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/fat16.cpp
+++ b/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/fat16.cpp
@@ -15,15 +15,32 @@
#include "sd-reader_config.h"
#include <string.h>
-#include <HardwareSerial.h>
#if USE_DYNAMIC_MEMORY
#include <stdlib.h>
-#endif
+#endif
+
+static uint8_t fat16_read_header(struct fat16_fs_struct* fs);
+static uint16_t fat16_get_next_cluster(const struct fat16_fs_struct* fs, uint16_t cluster_num);
+static uint16_t fat16_append_clusters(const struct fat16_fs_struct* fs, uint16_t cluster_num, uint16_t count);
+static uint8_t fat16_free_clusters(const struct fat16_fs_struct* fs, uint16_t cluster_num);
+static uint8_t fat16_terminate_clusters(const struct fat16_fs_struct* fs, uint16_t cluster_num);
+static uint8_t fat16_clear_cluster(const struct fat16_fs_struct* fs, uint16_t cluster_num);
+static uint16_t fat16_clear_cluster_callback(uint8_t* buffer, uint32_t offset, void* p);
+static uint32_t fat16_cluster_offset(const struct fat16_fs_struct* fs, uint16_t cluster_num);
+static uint8_t fat16_dir_entry_read_callback(uint8_t* buffer, uint32_t offset, void* p);
+static uint8_t fat16_interpret_dir_entry(struct fat16_dir_entry_struct* dir_entry, const uint8_t* raw_entry);
+static uint32_t fat16_find_offset_for_dir_entry(const struct fat16_fs_struct* fs, const struct fat16_dir_struct* parent, const struct fat16_dir_entry_struct* dir_entry);
+static uint8_t fat16_write_dir_entry(const struct fat16_fs_struct* fs, struct fat16_dir_entry_struct* dir_entry);
+
+//static uint8_t fat16_get_fs_free_callback(uint8_t* buffer, uint32_t offset, void* p);
+
+//static void fat16_set_file_modification_date(struct fat16_dir_entry_struct* dir_entry, uint16_t year, uint8_t month, uint8_t day);
+//static void fat16_set_file_modification_time(struct fat16_dir_entry_struct* dir_entry, uint8_t hour, uint8_t min, uint8_t sec);
struct fat16_fs_struct fat16_fs_handlers[FAT16_FILE_COUNT];
-struct fat16_file_struct fat16_file_handlers[FAT16_FILE_COUNT];
-struct fat16_dir_struct fat16_dir_handlers[FAT16_DIR_COUNT];
+ struct fat16_file_struct fat16_file_handlers[FAT16_FILE_COUNT];
+ struct fat16_dir_struct fat16_dir_handlers[FAT16_DIR_COUNT];
/**
* \addtogroup fat16 FAT16 support
@@ -901,11 +918,6 @@ struct fat16_file_struct* fat16_open_file(struct fat16_fs_struct* fs, const str
++fd;
}
- if(i >= FAT16_FILE_COUNT) {
- //todo: see if this works?
- //putstring_nl("no more handlers\n\r");
- return 0;
- }
#endif
memcpy(&fd->dir_entry, dir_entry, sizeof(*dir_entry));
@@ -931,7 +943,102 @@ void fat16_close_file(struct fat16_file_struct* fd)
#else
fd->fs = 0;
#endif
-}
+}
+
+/**
+ * \ingroup fat16_file
+ * Reads data from a file.
+ *
+ * The data requested is read from the current file location.
+ *
+ * \param[in] fd The file handle of the file from which to read.
+ * \param[out] buffer The buffer into which to write.
+ * \param[in] buffer_len The amount of data to read.
+ * \returns The number of bytes read, 0 on end of file, or -1 on failure.
+ * \see fat16_write_file
+ */
+int16_t fat16_read_file(struct fat16_file_struct* fd, uint8_t* buffer, uint16_t buffer_len)
+{
+ /* check arguments */
+ if(!fd || !buffer || buffer_len < 1)
+ return -1;
+
+ /* determine number of bytes to read */
+ if(fd->pos + buffer_len > fd->dir_entry.file_size)
+ buffer_len = fd->dir_entry.file_size - fd->pos;
+ if(buffer_len == 0)
+ return 0;
+
+ uint16_t cluster_size = fd->fs->header.cluster_size;
+ uint16_t cluster_num = fd->pos_cluster;
+ uint16_t buffer_left = buffer_len;
+ uint16_t first_cluster_offset = fd->pos % cluster_size;
+
+ /* find cluster in which to start reading */
+ if(!cluster_num)
+ {
+ cluster_num = fd->dir_entry.cluster;
+
+ if(!cluster_num)
+ {
+ if(!fd->pos)
+ return 0;
+ else
+ return -1;
+ }
+
+ if(fd->pos)
+ {
+ uint32_t pos = fd->pos;
+ while(pos >= cluster_size)
+ {
+ pos -= cluster_size;
+ cluster_num = fat16_get_next_cluster(fd->fs, cluster_num);
+ if(!cluster_num)
+ return -1;
+ }
+ }
+ }
+
+ /* read data */
+ do
+ {
+ /* calculate data size to copy from cluster */
+ uint32_t cluster_offset = fat16_cluster_offset(fd->fs, cluster_num) + first_cluster_offset;
+ uint16_t copy_length = cluster_size - first_cluster_offset;
+ if(copy_length > buffer_left)
+ copy_length = buffer_left;
+
+ /* read data */
+ if(!fd->fs->partition->device_read(cluster_offset, buffer, copy_length))
+ return buffer_len - buffer_left;
+
+ /* calculate new file position */
+ buffer += copy_length;
+ buffer_left -= copy_length;
+ fd->pos += copy_length;
+
+ if(first_cluster_offset + copy_length >= cluster_size)
+ {
+ /* we are on a cluster boundary, so get the next cluster */
+ if((cluster_num = fat16_get_next_cluster(fd->fs, cluster_num)))
+ {
+ first_cluster_offset = 0;
+ }
+ else
+ {
+ fd->pos_cluster = 0;
+ return buffer_len - buffer_left;
+ }
+ }
+
+ fd->pos_cluster = cluster_num;
+
+ } while(buffer_left > 0); /* check if we are done */
+
+ return buffer_len;
+}
+
/**
* \ingroup fat16_file
@@ -1091,8 +1198,6 @@ int16_t fat16_write_file(struct fat16_file_struct* fd, const uint8_t* buffer, ui
* \returns 0 on failure, 1 on success.
*/
- /* We could use this to append to the log but we just make a new one!
-
uint8_t fat16_seek_file(struct fat16_file_struct* fd, int32_t* offset, uint8_t whence)
{
if(!fd || !offset)
@@ -1123,8 +1228,6 @@ uint8_t fat16_seek_file(struct fat16_file_struct* fd, int32_t* offset, uint8_t w
*offset = new_pos;
return 1;
}
-*/
-
/**
* \ingroup fat16_dir
@@ -1490,12 +1593,9 @@ uint8_t fat16_write_dir_entry(const struct fat16_fs_struct* fs, struct fat16_dir
uint8_t fat16_create_file(struct fat16_dir_struct* parent, const char* file, struct fat16_dir_entry_struct* dir_entry)
{
#if FAT16_WRITE_SUPPORT
- if(!parent || !file || !file[0] || !dir_entry)
- {
- Serial.println("faila1");
+ if(!parent || !file || !file[0] || !dir_entry)
return 0;
- }
-
+
/* check if the file already exists */
while(1)
{
@@ -1504,9 +1604,7 @@ uint8_t fat16_create_file(struct fat16_dir_struct* parent, const char* file, str
if(strcmp(file, dir_entry->long_name) == 0)
{
- fat16_reset_dir(parent);
- Serial.println("faila2");
-
+ fat16_reset_dir(parent);
return 0;
}
}
@@ -1518,23 +1616,16 @@ uint8_t fat16_create_file(struct fat16_dir_struct* parent, const char* file, str
strncpy(dir_entry->long_name, file, sizeof(dir_entry->long_name) - 1);
/* find place where to store directory entry */
- if(!(dir_entry->entry_offset = fat16_find_offset_for_dir_entry(fs, parent, dir_entry)))
- {
- Serial.println("faila3");
+ if(!(dir_entry->entry_offset = fat16_find_offset_for_dir_entry(fs, parent, dir_entry)))
return 0;
- }
+
/* write directory entry to disk */
if(!fat16_write_dir_entry(fs, dir_entry))
- {
- Serial.println("faila4");
- return 0;
- }
+ return 0;
return 1;
-#else
- Serial.println("faila5");
-
+#else
return 0;
#endif
}
@@ -1618,4 +1709,19 @@ uint8_t fat16_get_fs_free_callback(uint8_t* buffer, uint32_t offset, void* p)
return 1;
}
-
+
+/**
+ * \ingroup fat16_fs
+ * Calculates the offset of the specified cluster.
+ *
+ * \param[in] fs The filesystem on which to operate.
+ * \param[in] cluster_num The cluster whose offset to calculate.
+ * \returns The cluster offset.
+ */
+uint32_t fat16_cluster_offset(const struct fat16_fs_struct* fs, uint16_t cluster_num)
+{
+ if(!fs || cluster_num < 2)
+ return 0;
+
+ return fs->header.cluster_zero_offset + (uint32_t) (cluster_num - 2) * fs->header.cluster_size;
+}
diff --git a/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/sd_raw.cpp b/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/sd_raw.cpp
index 581d93e8..c39d2401 100755
--- a/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/sd_raw.cpp
+++ b/trunk/users/hoeken/sanguino-motherboard/RepRapSDCard/sd_raw.cpp
@@ -586,7 +586,7 @@ uint8_t sd_raw_write(uint32_t offset, const uint8_t* buffer, uint16_t length)
if(get_pin_locked())
{
- Serial.println("locked.");
+ //Serial.println("locked.");
return 0;
}
diff --git a/trunk/users/hoeken/sanguino-motherboard/sd_card_test/sd_card_test.pde b/trunk/users/hoeken/sanguino-motherboard/sd_card_test/sd_card_test.pde
index 7f9a8b5a..675e6cea 100755
--- a/trunk/users/hoeken/sanguino-motherboard/sd_card_test/sd_card_test.pde
+++ b/trunk/users/hoeken/sanguino-motherboard/sd_card_test/sd_card_test.pde
@@ -48,7 +48,7 @@ void setup()
error = 6;
}
- open_file();
+ open_new_file();
if (error == 0)
{
@@ -62,13 +62,15 @@ void setup()
card.write_file(f, (uint8_t *) buffer, bufferIndex);
card.close_file(f);
}
+
+ read_first_file();
}
void loop()
{
}
-void open_file()
+void open_new_file()
{
strcpy(buffer, "RRJOB00.TXT");
for (buffer[5] = '0'; buffer[5] <= '9'; buffer[5]++)
@@ -86,7 +88,7 @@ void open_file()
if(!card.create_file(buffer))
{
- Serial.println("couldnt create: ");
+ Serial.print("couldnt create: ");
Serial.println(buffer);
error = 7;
}
@@ -95,14 +97,44 @@ void open_file()
f = card.open_file(buffer);
if (!f)
{
- Serial.println("error opening: ");
+ Serial.print("error opening: ");
Serial.println(buffer);
error = 8;
}
else
{
- Serial.println("writing to: ");
+ Serial.print("writing to: ");
Serial.println(buffer);
}
}
}
+
+void read_first_file()
+{
+ strcpy(buffer, "RRJOB00.TXT");
+ f = card.open_file(buffer);
+
+ if (!f)
+ {
+ Serial.print("error opening: ");
+ Serial.println(buffer);
+ error = 8;
+ }
+ else
+ {
+ Serial.print("reading from: ");
+ Serial.println(buffer);
+
+ uint8_t result = card.read_file(f, (uint8_t *)buffer, 8);
+
+ if (result >0)
+ Serial.print(buffer);
+ else if (result == 0)
+ Serial.println("end of file.");
+ else
+ {
+ Serial.print("read error: ");
+ Serial.println(result, DEC);
+ }
+ }
+}