From: Olivier Matz
Date: Thu, 26 Jun 2014 18:27:39 +0000 (+0200)
Subject: fat: whitespace-cleanup on all files
X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=de6cbbfa24c5af2030ab1a46ee94bcc8b35910b9;p=protos%2Fimu.git
fat: whitespace-cleanup on all files
---
diff --git a/byteordering.c b/byteordering.c
index d1c67cd..46ff7d8 100644
--- a/byteordering.c
+++ b/byteordering.c
@@ -61,7 +61,7 @@ uint32_t swap32(uint32_t i)
uint16_t read16(const uint8_t* p)
{
return (((uint16_t) p[1]) << 8) |
- (((uint16_t) p[0]) << 0);
+ (((uint16_t) p[0]) << 0);
}
/**
@@ -73,9 +73,9 @@ uint16_t read16(const uint8_t* p)
uint32_t read32(const uint8_t* p)
{
return (((uint32_t) p[3]) << 24) |
- (((uint32_t) p[2]) << 16) |
- (((uint32_t) p[1]) << 8) |
- (((uint32_t) p[0]) << 0);
+ (((uint32_t) p[2]) << 16) |
+ (((uint32_t) p[1]) << 8) |
+ (((uint32_t) p[0]) << 0);
}
/**
diff --git a/byteordering.h b/byteordering.h
index 12fa571..824c70f 100644
--- a/byteordering.h
+++ b/byteordering.h
@@ -31,13 +31,13 @@ extern "C"
*/
#define SWAP16(val) ((((uint16_t) (val)) << 8) | \
- (((uint16_t) (val)) >> 8) \
- )
+ (((uint16_t) (val)) >> 8) \
+ )
#define SWAP32(val) (((((uint32_t) (val)) & 0x000000ff) << 24) | \
- ((((uint32_t) (val)) & 0x0000ff00) << 8) | \
- ((((uint32_t) (val)) & 0x00ff0000) >> 8) | \
- ((((uint32_t) (val)) & 0xff000000) >> 24) \
- )
+ ((((uint32_t) (val)) & 0x0000ff00) << 8) | \
+ ((((uint32_t) (val)) & 0x00ff0000) >> 8) | \
+ ((((uint32_t) (val)) & 0xff000000) >> 24) \
+ )
#if LITTLE_ENDIAN || __AVR__
#define SWAP_NEEDED 0
diff --git a/fat.c b/fat.c
index e62f4cf..367d489 100644
--- a/fat.c
+++ b/fat.c
@@ -1,5 +1,5 @@
-/*
+/*
* Copyright (c) 2006-2012 by Roland Riegel
*
* This file is free software; you can redistribute it and/or modify
@@ -24,7 +24,7 @@
* \addtogroup fat FAT support
*
* This module implements FAT16/FAT32 read and write access.
- *
+ *
* The following features are supported:
* - File names up to 31 characters long.
* - Unlimited depth of subdirectories.
@@ -33,7 +33,7 @@
* - Reading and writing from and to files.
* - File resizing.
* - File sizes of up to 4 gigabytes.
- *
+ *
* @{
*/
/**
@@ -106,7 +106,7 @@
* LFN entry 2
* LFN entry 1
* 8.3 entry (see above)
- *
+ *
* lfn entry:
* ==========
* offset length description
@@ -128,7 +128,7 @@
* 26 2 cluster (unused, always 0)
* 28 2 unicode character 12
* 30 2 unicode character 13
- *
+ *
* The ordinal field contains a descending number, from n to 1.
* For the n'th lfn entry the ordinal field is or'ed with 0x40.
* For deleted lfn entries, the ordinal field is set to 0xe5.
@@ -242,24 +242,24 @@ struct fat_fs_struct* fat_open(struct partition_struct* partition)
0
#endif
)
- return 0;
+ return 0;
#if USE_DYNAMIC_MEMORY
struct fat_fs_struct* fs = malloc(sizeof(*fs));
if(!fs)
- return 0;
+ return 0;
#else
struct fat_fs_struct* fs = fat_fs_handles;
uint8_t i;
for(i = 0; i < FAT_FS_COUNT; ++i)
{
- if(!fs->partition)
- break;
+ if(!fs->partition)
+ break;
- ++fs;
+ ++fs;
}
if(i >= FAT_FS_COUNT)
- return 0;
+ return 0;
#endif
memset(fs, 0, sizeof(*fs));
@@ -268,13 +268,13 @@ struct fat_fs_struct* fat_open(struct partition_struct* partition)
if(!fat_read_header(fs))
{
#if USE_DYNAMIC_MEMORY
- free(fs);
+ free(fs);
#else
- fs->partition = 0;
+ fs->partition = 0;
#endif
- return 0;
+ return 0;
}
-
+
return fs;
}
@@ -291,7 +291,7 @@ struct fat_fs_struct* fat_open(struct partition_struct* partition)
void fat_close(struct fat_fs_struct* fs)
{
if(!fs)
- return;
+ return;
#if USE_DYNAMIC_MEMORY
free(fs);
@@ -310,11 +310,11 @@ void fat_close(struct fat_fs_struct* fs)
uint8_t fat_read_header(struct fat_fs_struct* fs)
{
if(!fs)
- return 0;
+ return 0;
struct partition_struct* partition = fs->partition;
if(!partition)
- return 0;
+ return 0;
/* read fat parameters */
#if FAT_FAT32_SUPPORT
@@ -324,7 +324,7 @@ uint8_t fat_read_header(struct fat_fs_struct* fs)
#endif
offset_t partition_offset = (offset_t) partition->offset * 512;
if(!partition->device_read(partition_offset + 0x0b, buffer, sizeof(buffer)))
- return 0;
+ return 0;
uint16_t bytes_per_sector = read16(&buffer[0x00]);
uint16_t reserved_sectors = read16(&buffer[0x03]);
@@ -341,54 +341,54 @@ uint8_t fat_read_header(struct fat_fs_struct* fs)
if(sector_count == 0)
{
- if(sector_count_16 == 0)
- /* illegal volume size */
- return 0;
- else
- sector_count = sector_count_16;
+ if(sector_count_16 == 0)
+ /* illegal volume size */
+ return 0;
+ else
+ sector_count = sector_count_16;
}
#if FAT_FAT32_SUPPORT
if(sectors_per_fat != 0)
- sectors_per_fat32 = sectors_per_fat;
+ sectors_per_fat32 = sectors_per_fat;
else if(sectors_per_fat32 == 0)
- /* this is neither FAT16 nor FAT32 */
- return 0;
+ /* this is neither FAT16 nor FAT32 */
+ return 0;
#else
if(sectors_per_fat == 0)
- /* this is not a FAT16 */
- return 0;
+ /* this is not a FAT16 */
+ return 0;
#endif
/* determine the type of FAT we have here */
uint32_t data_sector_count = sector_count
- - reserved_sectors
+ - reserved_sectors
#if FAT_FAT32_SUPPORT
- - sectors_per_fat32 * fat_copies
+ - sectors_per_fat32 * fat_copies
#else
- - (uint32_t) sectors_per_fat * fat_copies
+ - (uint32_t) sectors_per_fat * fat_copies
#endif
- - ((max_root_entries * 32 + bytes_per_sector - 1) / bytes_per_sector);
+ - ((max_root_entries * 32 + bytes_per_sector - 1) / bytes_per_sector);
uint32_t data_cluster_count = data_sector_count / sectors_per_cluster;
if(data_cluster_count < 4085)
- /* this is a FAT12, not supported */
- return 0;
+ /* this is a FAT12, not supported */
+ return 0;
else if(data_cluster_count < 65525)
- /* this is a FAT16 */
- partition->type = PARTITION_TYPE_FAT16;
+ /* this is a FAT16 */
+ partition->type = PARTITION_TYPE_FAT16;
else
- /* this is a FAT32 */
- partition->type = PARTITION_TYPE_FAT32;
+ /* this is a FAT32 */
+ partition->type = PARTITION_TYPE_FAT32;
/* fill header information */
struct fat_header_struct* header = &fs->header;
memset(header, 0, sizeof(*header));
-
+
header->size = (offset_t) sector_count * bytes_per_sector;
header->fat_offset = /* jump to partition */
- partition_offset +
- /* jump to fat */
- (offset_t) reserved_sectors * bytes_per_sector;
+ partition_offset +
+ /* jump to fat */
+ (offset_t) reserved_sectors * bytes_per_sector;
header->fat_size = (data_cluster_count + 2) * (partition->type == PARTITION_TYPE_FAT16 ? 2 : 4);
header->sector_size = bytes_per_sector;
@@ -398,25 +398,25 @@ uint8_t fat_read_header(struct fat_fs_struct* fs)
if(partition->type == PARTITION_TYPE_FAT16)
#endif
{
- header->root_dir_offset = /* jump to fats */
- header->fat_offset +
- /* jump to root directory entries */
- (offset_t) fat_copies * sectors_per_fat * bytes_per_sector;
-
- header->cluster_zero_offset = /* jump to root directory entries */
- header->root_dir_offset +
- /* skip root directory entries */
- (offset_t) max_root_entries * 32;
+ header->root_dir_offset = /* jump to fats */
+ header->fat_offset +
+ /* jump to root directory entries */
+ (offset_t) fat_copies * sectors_per_fat * bytes_per_sector;
+
+ header->cluster_zero_offset = /* jump to root directory entries */
+ header->root_dir_offset +
+ /* skip root directory entries */
+ (offset_t) max_root_entries * 32;
}
#if FAT_FAT32_SUPPORT
else
{
- header->cluster_zero_offset = /* jump to fats */
- header->fat_offset +
- /* skip fats */
- (offset_t) fat_copies * sectors_per_fat32 * bytes_per_sector;
+ header->cluster_zero_offset = /* jump to fats */
+ header->fat_offset +
+ /* skip fats */
+ (offset_t) fat_copies * sectors_per_fat32 * bytes_per_sector;
- header->root_dir_cluster = cluster_root_dir;
+ header->root_dir_cluster = cluster_root_dir;
}
#endif
@@ -438,41 +438,41 @@ uint8_t fat_read_header(struct fat_fs_struct* fs)
cluster_t fat_get_next_cluster(const struct fat_fs_struct* fs, cluster_t cluster_num)
{
if(!fs || cluster_num < 2)
- return 0;
+ return 0;
#if FAT_FAT32_SUPPORT
if(fs->partition->type == PARTITION_TYPE_FAT32)
{
- /* read appropriate fat entry */
- uint32_t fat_entry;
- if(!fs->partition->device_read(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
- return 0;
-
- /* determine next cluster from fat */
- cluster_num = ltoh32(fat_entry);
-
- if(cluster_num == FAT32_CLUSTER_FREE ||
- cluster_num == FAT32_CLUSTER_BAD ||
- (cluster_num >= FAT32_CLUSTER_RESERVED_MIN && cluster_num <= FAT32_CLUSTER_RESERVED_MAX) ||
- (cluster_num >= FAT32_CLUSTER_LAST_MIN && cluster_num <= FAT32_CLUSTER_LAST_MAX))
- return 0;
+ /* read appropriate fat entry */
+ uint32_t fat_entry;
+ if(!fs->partition->device_read(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
+ return 0;
+
+ /* determine next cluster from fat */
+ cluster_num = ltoh32(fat_entry);
+
+ if(cluster_num == FAT32_CLUSTER_FREE ||
+ cluster_num == FAT32_CLUSTER_BAD ||
+ (cluster_num >= FAT32_CLUSTER_RESERVED_MIN && cluster_num <= FAT32_CLUSTER_RESERVED_MAX) ||
+ (cluster_num >= FAT32_CLUSTER_LAST_MIN && cluster_num <= FAT32_CLUSTER_LAST_MAX))
+ return 0;
}
else
#endif
{
- /* read appropriate fat entry */
- uint16_t fat_entry;
- if(!fs->partition->device_read(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
- return 0;
-
- /* determine next cluster from fat */
- cluster_num = ltoh16(fat_entry);
-
- if(cluster_num == FAT16_CLUSTER_FREE ||
- cluster_num == FAT16_CLUSTER_BAD ||
- (cluster_num >= FAT16_CLUSTER_RESERVED_MIN && cluster_num <= FAT16_CLUSTER_RESERVED_MAX) ||
- (cluster_num >= FAT16_CLUSTER_LAST_MIN && cluster_num <= FAT16_CLUSTER_LAST_MAX))
- return 0;
+ /* read appropriate fat entry */
+ uint16_t fat_entry;
+ if(!fs->partition->device_read(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
+ return 0;
+
+ /* determine next cluster from fat */
+ cluster_num = ltoh16(fat_entry);
+
+ if(cluster_num == FAT16_CLUSTER_FREE ||
+ cluster_num == FAT16_CLUSTER_BAD ||
+ (cluster_num >= FAT16_CLUSTER_RESERVED_MIN && cluster_num <= FAT16_CLUSTER_RESERVED_MAX) ||
+ (cluster_num >= FAT16_CLUSTER_LAST_MIN && cluster_num <= FAT16_CLUSTER_LAST_MAX))
+ return 0;
}
return cluster_num;
@@ -493,7 +493,7 @@ cluster_t fat_get_next_cluster(const struct fat_fs_struct* fs, cluster_t cluster
cluster_t fat_append_clusters(struct fat_fs_struct* fs, cluster_t cluster_num, cluster_t count)
{
if(!fs)
- return 0;
+ return 0;
device_read_t device_read = fs->partition->device_read;
device_write_t device_write = fs->partition->device_write;
@@ -508,116 +508,116 @@ cluster_t fat_append_clusters(struct fat_fs_struct* fs, cluster_t cluster_num, c
uint8_t is_fat32 = (fs->partition->type == PARTITION_TYPE_FAT32);
if(is_fat32)
- cluster_count = fs->header.fat_size / sizeof(fat_entry32);
+ cluster_count = fs->header.fat_size / sizeof(fat_entry32);
else
#endif
- cluster_count = fs->header.fat_size / sizeof(fat_entry16);
+ cluster_count = fs->header.fat_size / sizeof(fat_entry16);
fs->cluster_free = 0;
for(cluster_t cluster_left = cluster_count; cluster_left > 0; --cluster_left, ++cluster_current)
{
- if(cluster_current < 2 || cluster_current >= cluster_count)
- cluster_current = 2;
+ if(cluster_current < 2 || cluster_current >= cluster_count)
+ cluster_current = 2;
#if FAT_FAT32_SUPPORT
- if(is_fat32)
- {
- if(!device_read(fat_offset + (offset_t) cluster_current * sizeof(fat_entry32), (uint8_t*) &fat_entry32, sizeof(fat_entry32)))
- return 0;
- }
- else
-#endif
- {
- if(!device_read(fat_offset + (offset_t) cluster_current * sizeof(fat_entry16), (uint8_t*) &fat_entry16, sizeof(fat_entry16)))
- return 0;
- }
+ if(is_fat32)
+ {
+ if(!device_read(fat_offset + (offset_t) cluster_current * sizeof(fat_entry32), (uint8_t*) &fat_entry32, sizeof(fat_entry32)))
+ return 0;
+ }
+ else
+#endif
+ {
+ if(!device_read(fat_offset + (offset_t) cluster_current * sizeof(fat_entry16), (uint8_t*) &fat_entry16, sizeof(fat_entry16)))
+ return 0;
+ }
#if FAT_FAT32_SUPPORT
- if(is_fat32)
- {
- /* check if this is a free cluster */
- if(fat_entry32 != HTOL32(FAT32_CLUSTER_FREE))
- continue;
-
- /* If we don't need this free cluster for the
- * current allocation, we keep it in mind for
- * the next time.
- */
- if(count_left == 0)
- {
- fs->cluster_free = cluster_current;
- break;
- }
-
- /* allocate cluster */
- if(cluster_next == 0)
- fat_entry32 = HTOL32(FAT32_CLUSTER_LAST_MAX);
- else
- fat_entry32 = htol32(cluster_next);
-
- if(!device_write(fat_offset + (offset_t) cluster_current * sizeof(fat_entry32), (uint8_t*) &fat_entry32, sizeof(fat_entry32)))
- break;
- }
- else
-#endif
- {
- /* check if this is a free cluster */
- if(fat_entry16 != HTOL16(FAT16_CLUSTER_FREE))
- continue;
-
- /* If we don't need this free cluster for the
- * current allocation, we keep it in mind for
- * the next time.
- */
- if(count_left == 0)
- {
- fs->cluster_free = cluster_current;
- break;
- }
-
- /* allocate cluster */
- if(cluster_next == 0)
- fat_entry16 = HTOL16(FAT16_CLUSTER_LAST_MAX);
- else
- fat_entry16 = htol16((uint16_t) cluster_next);
-
- if(!device_write(fat_offset + (offset_t) cluster_current * sizeof(fat_entry16), (uint8_t*) &fat_entry16, sizeof(fat_entry16)))
- break;
- }
-
- cluster_next = cluster_current;
- --count_left;
+ if(is_fat32)
+ {
+ /* check if this is a free cluster */
+ if(fat_entry32 != HTOL32(FAT32_CLUSTER_FREE))
+ continue;
+
+ /* If we don't need this free cluster for the
+ * current allocation, we keep it in mind for
+ * the next time.
+ */
+ if(count_left == 0)
+ {
+ fs->cluster_free = cluster_current;
+ break;
+ }
+
+ /* allocate cluster */
+ if(cluster_next == 0)
+ fat_entry32 = HTOL32(FAT32_CLUSTER_LAST_MAX);
+ else
+ fat_entry32 = htol32(cluster_next);
+
+ if(!device_write(fat_offset + (offset_t) cluster_current * sizeof(fat_entry32), (uint8_t*) &fat_entry32, sizeof(fat_entry32)))
+ break;
+ }
+ else
+#endif
+ {
+ /* check if this is a free cluster */
+ if(fat_entry16 != HTOL16(FAT16_CLUSTER_FREE))
+ continue;
+
+ /* If we don't need this free cluster for the
+ * current allocation, we keep it in mind for
+ * the next time.
+ */
+ if(count_left == 0)
+ {
+ fs->cluster_free = cluster_current;
+ break;
+ }
+
+ /* allocate cluster */
+ if(cluster_next == 0)
+ fat_entry16 = HTOL16(FAT16_CLUSTER_LAST_MAX);
+ else
+ fat_entry16 = htol16((uint16_t) cluster_next);
+
+ if(!device_write(fat_offset + (offset_t) cluster_current * sizeof(fat_entry16), (uint8_t*) &fat_entry16, sizeof(fat_entry16)))
+ break;
+ }
+
+ cluster_next = cluster_current;
+ --count_left;
}
do
{
- if(count_left > 0)
- break;
-
- /* We allocated a new cluster chain. Now join
- * it with the existing one (if any).
- */
- if(cluster_num >= 2)
- {
+ if(count_left > 0)
+ break;
+
+ /* We allocated a new cluster chain. Now join
+ * it with the existing one (if any).
+ */
+ if(cluster_num >= 2)
+ {
#if FAT_FAT32_SUPPORT
- if(is_fat32)
- {
- fat_entry32 = htol32(cluster_next);
+ if(is_fat32)
+ {
+ fat_entry32 = htol32(cluster_next);
- if(!device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry32), (uint8_t*) &fat_entry32, sizeof(fat_entry32)))
- break;
- }
- else
+ if(!device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry32), (uint8_t*) &fat_entry32, sizeof(fat_entry32)))
+ break;
+ }
+ else
#endif
- {
- fat_entry16 = htol16((uint16_t) cluster_next);
+ {
+ fat_entry16 = htol16((uint16_t) cluster_next);
- if(!device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry16), (uint8_t*) &fat_entry16, sizeof(fat_entry16)))
- break;
- }
- }
+ if(!device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry16), (uint8_t*) &fat_entry16, sizeof(fat_entry16)))
+ break;
+ }
+ }
- return cluster_next;
+ return cluster_next;
} while(0);
@@ -651,82 +651,82 @@ cluster_t fat_append_clusters(struct fat_fs_struct* fs, cluster_t cluster_num, c
uint8_t fat_free_clusters(struct fat_fs_struct* fs, cluster_t cluster_num)
{
if(!fs || cluster_num < 2)
- return 0;
+ return 0;
offset_t fat_offset = fs->header.fat_offset;
#if FAT_FAT32_SUPPORT
if(fs->partition->type == PARTITION_TYPE_FAT32)
{
- uint32_t fat_entry;
- while(cluster_num)
- {
- if(!fs->partition->device_read(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
- return 0;
-
- /* get next cluster of current cluster before freeing current cluster */
- uint32_t cluster_num_next = ltoh32(fat_entry);
-
- if(cluster_num_next == FAT32_CLUSTER_FREE)
- return 1;
- if(cluster_num_next == FAT32_CLUSTER_BAD ||
- (cluster_num_next >= FAT32_CLUSTER_RESERVED_MIN &&
- cluster_num_next <= FAT32_CLUSTER_RESERVED_MAX
- )
- )
- return 0;
- if(cluster_num_next >= FAT32_CLUSTER_LAST_MIN && cluster_num_next <= FAT32_CLUSTER_LAST_MAX)
- cluster_num_next = 0;
-
- /* We know we will free the cluster, so remember it as
- * free for the next allocation.
- */
- if(!fs->cluster_free)
- fs->cluster_free = cluster_num;
-
- /* free cluster */
- fat_entry = HTOL32(FAT32_CLUSTER_FREE);
- fs->partition->device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry));
-
- /* We continue in any case here, even if freeing the cluster failed.
- * The cluster is lost, but maybe we can still free up some later ones.
- */
-
- cluster_num = cluster_num_next;
- }
+ uint32_t fat_entry;
+ while(cluster_num)
+ {
+ if(!fs->partition->device_read(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
+ return 0;
+
+ /* get next cluster of current cluster before freeing current cluster */
+ uint32_t cluster_num_next = ltoh32(fat_entry);
+
+ if(cluster_num_next == FAT32_CLUSTER_FREE)
+ return 1;
+ if(cluster_num_next == FAT32_CLUSTER_BAD ||
+ (cluster_num_next >= FAT32_CLUSTER_RESERVED_MIN &&
+ cluster_num_next <= FAT32_CLUSTER_RESERVED_MAX
+ )
+ )
+ return 0;
+ if(cluster_num_next >= FAT32_CLUSTER_LAST_MIN && cluster_num_next <= FAT32_CLUSTER_LAST_MAX)
+ cluster_num_next = 0;
+
+ /* We know we will free the cluster, so remember it as
+ * free for the next allocation.
+ */
+ if(!fs->cluster_free)
+ fs->cluster_free = cluster_num;
+
+ /* free cluster */
+ fat_entry = HTOL32(FAT32_CLUSTER_FREE);
+ fs->partition->device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry));
+
+ /* We continue in any case here, even if freeing the cluster failed.
+ * The cluster is lost, but maybe we can still free up some later ones.
+ */
+
+ cluster_num = cluster_num_next;
+ }
}
else
#endif
{
- uint16_t fat_entry;
- while(cluster_num)
- {
- if(!fs->partition->device_read(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
- return 0;
-
- /* get next cluster of current cluster before freeing current cluster */
- uint16_t cluster_num_next = ltoh16(fat_entry);
-
- if(cluster_num_next == FAT16_CLUSTER_FREE)
- return 1;
- if(cluster_num_next == FAT16_CLUSTER_BAD ||
- (cluster_num_next >= FAT16_CLUSTER_RESERVED_MIN &&
- cluster_num_next <= FAT16_CLUSTER_RESERVED_MAX
- )
- )
- return 0;
- if(cluster_num_next >= FAT16_CLUSTER_LAST_MIN && cluster_num_next <= FAT16_CLUSTER_LAST_MAX)
- cluster_num_next = 0;
-
- /* free cluster */
- fat_entry = HTOL16(FAT16_CLUSTER_FREE);
- fs->partition->device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry));
-
- /* We continue in any case here, even if freeing the cluster failed.
- * The cluster is lost, but maybe we can still free up some later ones.
- */
-
- cluster_num = cluster_num_next;
- }
+ uint16_t fat_entry;
+ while(cluster_num)
+ {
+ if(!fs->partition->device_read(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
+ return 0;
+
+ /* get next cluster of current cluster before freeing current cluster */
+ uint16_t cluster_num_next = ltoh16(fat_entry);
+
+ if(cluster_num_next == FAT16_CLUSTER_FREE)
+ return 1;
+ if(cluster_num_next == FAT16_CLUSTER_BAD ||
+ (cluster_num_next >= FAT16_CLUSTER_RESERVED_MIN &&
+ cluster_num_next <= FAT16_CLUSTER_RESERVED_MAX
+ )
+ )
+ return 0;
+ if(cluster_num_next >= FAT16_CLUSTER_LAST_MIN && cluster_num_next <= FAT16_CLUSTER_LAST_MAX)
+ cluster_num_next = 0;
+
+ /* free cluster */
+ fat_entry = HTOL16(FAT16_CLUSTER_FREE);
+ fs->partition->device_write(fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry));
+
+ /* We continue in any case here, even if freeing the cluster failed.
+ * The cluster is lost, but maybe we can still free up some later ones.
+ */
+
+ cluster_num = cluster_num_next;
+ }
}
return 1;
@@ -749,7 +749,7 @@ uint8_t fat_free_clusters(struct fat_fs_struct* fs, cluster_t cluster_num)
uint8_t fat_terminate_clusters(struct fat_fs_struct* fs, cluster_t cluster_num)
{
if(!fs || cluster_num < 2)
- return 0;
+ return 0;
/* fetch next cluster before overwriting the cluster entry */
cluster_t cluster_num_next = fat_get_next_cluster(fs, cluster_num);
@@ -758,23 +758,23 @@ uint8_t fat_terminate_clusters(struct fat_fs_struct* fs, cluster_t cluster_num)
#if FAT_FAT32_SUPPORT
if(fs->partition->type == PARTITION_TYPE_FAT32)
{
- uint32_t fat_entry = HTOL32(FAT32_CLUSTER_LAST_MAX);
- if(!fs->partition->device_write(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
- return 0;
+ uint32_t fat_entry = HTOL32(FAT32_CLUSTER_LAST_MAX);
+ if(!fs->partition->device_write(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
+ return 0;
}
else
#endif
{
- uint16_t fat_entry = HTOL16(FAT16_CLUSTER_LAST_MAX);
- if(!fs->partition->device_write(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
- return 0;
+ uint16_t fat_entry = HTOL16(FAT16_CLUSTER_LAST_MAX);
+ if(!fs->partition->device_write(fs->header.fat_offset + (offset_t) cluster_num * sizeof(fat_entry), (uint8_t*) &fat_entry, sizeof(fat_entry)))
+ return 0;
}
/* free remaining clusters */
if(cluster_num_next)
- return fat_free_clusters(fs, cluster_num_next);
+ return fat_free_clusters(fs, cluster_num_next);
else
- return 1;
+ return 1;
}
#endif
@@ -792,18 +792,18 @@ uint8_t fat_terminate_clusters(struct fat_fs_struct* fs, cluster_t cluster_num)
uint8_t fat_clear_cluster(const struct fat_fs_struct* fs, cluster_t cluster_num)
{
if(cluster_num < 2)
- return 0;
+ return 0;
offset_t cluster_offset = fat_cluster_offset(fs, cluster_num);
uint8_t zero[16];
memset(zero, 0, sizeof(zero));
return fs->partition->device_write_interval(cluster_offset,
- zero,
- fs->header.cluster_size,
- fat_clear_cluster_callback,
- 0
- );
+ zero,
+ fs->header.cluster_size,
+ fat_clear_cluster_callback,
+ 0
+ );
}
#endif
@@ -829,7 +829,7 @@ uintptr_t fat_clear_cluster_callback(uint8_t* buffer, offset_t offset, void* p)
offset_t fat_cluster_offset(const struct fat_fs_struct* fs, cluster_t cluster_num)
{
if(!fs || cluster_num < 2)
- return 0;
+ return 0;
return fs->header.cluster_zero_offset + (offset_t) (cluster_num - 2) * fs->header.cluster_size;
}
@@ -849,10 +849,10 @@ offset_t fat_cluster_offset(const struct fat_fs_struct* fs, cluster_t cluster_nu
uint8_t fat_get_dir_entry_of_path(struct fat_fs_struct* fs, const char* path, struct fat_dir_entry_struct* dir_entry)
{
if(!fs || !path || path[0] == '\0' || !dir_entry)
- return 0;
+ return 0;
if(path[0] == '/')
- ++path;
+ ++path;
/* begin with the root directory */
memset(dir_entry, 0, sizeof(*dir_entry));
@@ -860,56 +860,56 @@ uint8_t fat_get_dir_entry_of_path(struct fat_fs_struct* fs, const char* path, st
while(1)
{
- if(path[0] == '\0')
- return 1;
-
- struct fat_dir_struct* dd = fat_open_dir(fs, dir_entry);
- if(!dd)
- break;
-
- /* extract the next hierarchy we will search for */
- const char* sub_path = strchr(path, '/');
- uint8_t length_to_sep;
- if(sub_path)
- {
- length_to_sep = sub_path - path;
- ++sub_path;
- }
- else
- {
- length_to_sep = strlen(path);
- sub_path = path + length_to_sep;
- }
-
- /* read directory entries */
- while(fat_read_dir(dd, dir_entry))
- {
- /* check if we have found the next hierarchy */
- if((strlen(dir_entry->long_name) != length_to_sep ||
- strncmp(path, dir_entry->long_name, length_to_sep) != 0))
- continue;
-
- fat_close_dir(dd);
- dd = 0;
-
- if(path[length_to_sep] == '\0')
- /* we iterated through the whole path and have found the file */
- return 1;
-
- if(dir_entry->attributes & FAT_ATTRIB_DIR)
- {
- /* we found a parent directory of the file we are searching for */
- path = sub_path;
- break;
- }
-
- /* a parent of the file exists, but not the file itself */
- return 0;
- }
-
- fat_close_dir(dd);
+ if(path[0] == '\0')
+ return 1;
+
+ struct fat_dir_struct* dd = fat_open_dir(fs, dir_entry);
+ if(!dd)
+ break;
+
+ /* extract the next hierarchy we will search for */
+ const char* sub_path = strchr(path, '/');
+ uint8_t length_to_sep;
+ if(sub_path)
+ {
+ length_to_sep = sub_path - path;
+ ++sub_path;
+ }
+ else
+ {
+ length_to_sep = strlen(path);
+ sub_path = path + length_to_sep;
+ }
+
+ /* read directory entries */
+ while(fat_read_dir(dd, dir_entry))
+ {
+ /* check if we have found the next hierarchy */
+ if((strlen(dir_entry->long_name) != length_to_sep ||
+ strncmp(path, dir_entry->long_name, length_to_sep) != 0))
+ continue;
+
+ fat_close_dir(dd);
+ dd = 0;
+
+ if(path[length_to_sep] == '\0')
+ /* we iterated through the whole path and have found the file */
+ return 1;
+
+ if(dir_entry->attributes & FAT_ATTRIB_DIR)
+ {
+ /* we found a parent directory of the file we are searching for */
+ path = sub_path;
+ break;
+ }
+
+ /* a parent of the file exists, but not the file itself */
+ return 0;
+ }
+
+ fat_close_dir(dd);
}
-
+
return 0;
}
@@ -925,26 +925,26 @@ uint8_t fat_get_dir_entry_of_path(struct fat_fs_struct* fs, const char* path, st
struct fat_file_struct* fat_open_file(struct fat_fs_struct* fs, const struct fat_dir_entry_struct* dir_entry)
{
if(!fs || !dir_entry || (dir_entry->attributes & FAT_ATTRIB_DIR))
- return 0;
+ return 0;
#if USE_DYNAMIC_MEMORY
struct fat_file_struct* fd = malloc(sizeof(*fd));
if(!fd)
- return 0;
+ return 0;
#else
struct fat_file_struct* fd = fat_file_handles;
uint8_t i;
for(i = 0; i < FAT_FILE_COUNT; ++i)
{
- if(!fd->fs)
- break;
+ if(!fd->fs)
+ break;
- ++fd;
+ ++fd;
}
if(i >= FAT_FILE_COUNT)
- return 0;
+ return 0;
#endif
-
+
memcpy(&fd->dir_entry, dir_entry, sizeof(*dir_entry));
fd->fs = fs;
fd->pos = 0;
@@ -965,14 +965,14 @@ void fat_close_file(struct fat_file_struct* fd)
if(fd)
{
#if FAT_DELAY_DIRENTRY_UPDATE
- /* write directory entry */
- fat_write_dir_entry(fd->fs, &fd->dir_entry);
+ /* write directory entry */
+ fat_write_dir_entry(fd->fs, &fd->dir_entry);
#endif
#if USE_DYNAMIC_MEMORY
- free(fd);
+ free(fd);
#else
- fd->fs = 0;
+ fd->fs = 0;
#endif
}
}
@@ -980,7 +980,7 @@ void fat_close_file(struct fat_file_struct* fd)
/**
* \ingroup fat_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.
@@ -993,14 +993,14 @@ intptr_t fat_read_file(struct fat_file_struct* fd, uint8_t* buffer, uintptr_t bu
{
/* check arguments */
if(!fd || !buffer || buffer_len < 1)
- return -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;
+ buffer_len = fd->dir_entry.file_size - fd->pos;
if(buffer_len == 0)
- return 0;
-
+ return 0;
+
uint16_t cluster_size = fd->fs->header.cluster_size;
cluster_t cluster_num = fd->pos_cluster;
uintptr_t buffer_left = buffer_len;
@@ -1009,62 +1009,62 @@ intptr_t fat_read_file(struct fat_file_struct* fd, uint8_t* buffer, uintptr_t bu
/* 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 = fat_get_next_cluster(fd->fs, cluster_num);
- if(!cluster_num)
- return -1;
- }
- }
+ 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 = fat_get_next_cluster(fd->fs, cluster_num);
+ if(!cluster_num)
+ return -1;
+ }
+ }
}
-
+
/* read data */
do
{
- /* calculate data size to copy from cluster */
- offset_t cluster_offset = fat_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 = fat_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;
+ /* calculate data size to copy from cluster */
+ offset_t cluster_offset = fat_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 = fat_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 */
@@ -1075,7 +1075,7 @@ intptr_t fat_read_file(struct fat_file_struct* fd, uint8_t* buffer, uintptr_t bu
/**
* \ingroup fat_file
* Writes data to a file.
- *
+ *
* The data is written to the current file location.
*
* \param[in] fd The file handle of the file to which to write.
@@ -1088,9 +1088,9 @@ intptr_t fat_write_file(struct fat_file_struct* fd, const uint8_t* buffer, uintp
{
/* check arguments */
if(!fd || !buffer || buffer_len < 1)
- return -1;
+ return -1;
if(fd->pos > fd->dir_entry.file_size)
- return -1;
+ return -1;
uint16_t cluster_size = fd->fs->header.cluster_size;
cluster_t cluster_num = fd->pos_cluster;
@@ -1100,83 +1100,83 @@ intptr_t fat_write_file(struct fat_file_struct* fd, const uint8_t* buffer, uintp
/* find cluster in which to start writing */
if(!cluster_num)
{
- cluster_num = fd->dir_entry.cluster;
-
- if(!cluster_num)
- {
- if(!fd->pos)
- {
- /* empty file */
- fd->dir_entry.cluster = cluster_num = fat_append_clusters(fd->fs, 0, 1);
- if(!cluster_num)
- return 0;
- }
- else
- {
- return -1;
- }
- }
-
- if(fd->pos)
- {
- uint32_t pos = fd->pos;
- cluster_t cluster_num_next;
- while(pos >= cluster_size)
- {
- pos -= cluster_size;
- cluster_num_next = fat_get_next_cluster(fd->fs, cluster_num);
- if(!cluster_num_next)
- {
- if(pos != 0)
- return -1; /* current file position points beyond end of file */
-
- /* the file exactly ends on a cluster boundary, and we append to it */
- cluster_num_next = fat_append_clusters(fd->fs, cluster_num, 1);
- if(!cluster_num_next)
- return 0;
- }
-
- cluster_num = cluster_num_next;
- }
- }
+ cluster_num = fd->dir_entry.cluster;
+
+ if(!cluster_num)
+ {
+ if(!fd->pos)
+ {
+ /* empty file */
+ fd->dir_entry.cluster = cluster_num = fat_append_clusters(fd->fs, 0, 1);
+ if(!cluster_num)
+ return 0;
+ }
+ else
+ {
+ return -1;
+ }
+ }
+
+ if(fd->pos)
+ {
+ uint32_t pos = fd->pos;
+ cluster_t cluster_num_next;
+ while(pos >= cluster_size)
+ {
+ pos -= cluster_size;
+ cluster_num_next = fat_get_next_cluster(fd->fs, cluster_num);
+ if(!cluster_num_next)
+ {
+ if(pos != 0)
+ return -1; /* current file position points beyond end of file */
+
+ /* the file exactly ends on a cluster boundary, and we append to it */
+ cluster_num_next = fat_append_clusters(fd->fs, cluster_num, 1);
+ if(!cluster_num_next)
+ return 0;
+ }
+
+ cluster_num = cluster_num_next;
+ }
+ }
}
-
+
/* write data */
do
{
- /* calculate data size to write to cluster */
- offset_t cluster_offset = fat_cluster_offset(fd->fs, cluster_num) + first_cluster_offset;
- uint16_t write_length = cluster_size - first_cluster_offset;
- if(write_length > buffer_left)
- write_length = buffer_left;
-
- /* write data which fits into the current cluster */
- if(!fd->fs->partition->device_write(cluster_offset, buffer, write_length))
- break;
-
- /* calculate new file position */
- buffer += write_length;
- buffer_left -= write_length;
- fd->pos += write_length;
-
- if(first_cluster_offset + write_length >= cluster_size)
- {
- /* we are on a cluster boundary, so get the next cluster */
- cluster_t cluster_num_next = fat_get_next_cluster(fd->fs, cluster_num);
- if(!cluster_num_next && buffer_left > 0)
- /* we reached the last cluster, append a new one */
- cluster_num_next = fat_append_clusters(fd->fs, cluster_num, 1);
- if(!cluster_num_next)
- {
- fd->pos_cluster = 0;
- break;
- }
-
- cluster_num = cluster_num_next;
- first_cluster_offset = 0;
- }
-
- fd->pos_cluster = cluster_num;
+ /* calculate data size to write to cluster */
+ offset_t cluster_offset = fat_cluster_offset(fd->fs, cluster_num) + first_cluster_offset;
+ uint16_t write_length = cluster_size - first_cluster_offset;
+ if(write_length > buffer_left)
+ write_length = buffer_left;
+
+ /* write data which fits into the current cluster */
+ if(!fd->fs->partition->device_write(cluster_offset, buffer, write_length))
+ break;
+
+ /* calculate new file position */
+ buffer += write_length;
+ buffer_left -= write_length;
+ fd->pos += write_length;
+
+ if(first_cluster_offset + write_length >= cluster_size)
+ {
+ /* we are on a cluster boundary, so get the next cluster */
+ cluster_t cluster_num_next = fat_get_next_cluster(fd->fs, cluster_num);
+ if(!cluster_num_next && buffer_left > 0)
+ /* we reached the last cluster, append a new one */
+ cluster_num_next = fat_append_clusters(fd->fs, cluster_num, 1);
+ if(!cluster_num_next)
+ {
+ fd->pos_cluster = 0;
+ break;
+ }
+
+ cluster_num = cluster_num_next;
+ first_cluster_offset = 0;
+ }
+
+ fd->pos_cluster = cluster_num;
} while(buffer_left > 0); /* check if we are done */
@@ -1184,23 +1184,23 @@ intptr_t fat_write_file(struct fat_file_struct* fd, const uint8_t* buffer, uintp
if(fd->pos > fd->dir_entry.file_size)
{
#if !FAT_DELAY_DIRENTRY_UPDATE
- uint32_t size_old = fd->dir_entry.file_size;
+ uint32_t size_old = fd->dir_entry.file_size;
#endif
- /* update file size */
- fd->dir_entry.file_size = fd->pos;
+ /* update file size */
+ fd->dir_entry.file_size = fd->pos;
#if !FAT_DELAY_DIRENTRY_UPDATE
- /* write directory entry */
- if(!fat_write_dir_entry(fd->fs, &fd->dir_entry))
- {
- /* We do not return an error here since we actually wrote
- * some data to disk. So we calculate the amount of data
- * we wrote to disk and which lies within the old file size.
- */
- buffer_left = fd->pos - size_old;
- fd->pos = size_old;
- }
+ /* write directory entry */
+ if(!fat_write_dir_entry(fd->fs, &fd->dir_entry))
+ {
+ /* We do not return an error here since we actually wrote
+ * some data to disk. So we calculate the amount of data
+ * we wrote to disk and which lies within the old file size.
+ */
+ buffer_left = fd->pos - size_old;
+ fd->pos = size_old;
+ }
#endif
}
@@ -1236,7 +1236,7 @@ intptr_t fat_write_file(struct fat_file_struct* fd, const uint8_t* buffer, uintp
}
// file_pos now contains the absolute file position
\endcode
- *
+ *
* \param[in] fd The file decriptor of the file on which to seek.
* \param[in,out] offset A pointer to the new offset, as affected by the \c whence
* parameter. The function writes the new absolute offset
@@ -1247,22 +1247,22 @@ intptr_t fat_write_file(struct fat_file_struct* fd, const uint8_t* buffer, uintp
uint8_t fat_seek_file(struct fat_file_struct* fd, int32_t* offset, uint8_t whence)
{
if(!fd || !offset)
- return 0;
+ return 0;
uint32_t new_pos = fd->pos;
switch(whence)
{
- case FAT_SEEK_SET:
- new_pos = *offset;
- break;
- case FAT_SEEK_CUR:
- new_pos += *offset;
- break;
- case FAT_SEEK_END:
- new_pos = fd->dir_entry.file_size + *offset;
- break;
- default:
- return 0;
+ case FAT_SEEK_SET:
+ new_pos = *offset;
+ break;
+ case FAT_SEEK_CUR:
+ new_pos += *offset;
+ break;
+ case FAT_SEEK_END:
+ new_pos = fd->dir_entry.file_size + *offset;
+ break;
+ default:
+ return 0;
}
if(new_pos > fd->dir_entry.file_size
@@ -1270,7 +1270,7 @@ uint8_t fat_seek_file(struct fat_file_struct* fd, int32_t* offset, uint8_t whenc
&& !fat_resize_file(fd, new_pos)
#endif
)
- return 0;
+ return 0;
fd->pos = new_pos;
fd->pos_cluster = 0;
@@ -1302,7 +1302,7 @@ uint8_t fat_seek_file(struct fat_file_struct* fd, int32_t* offset, uint8_t whenc
uint8_t fat_resize_file(struct fat_file_struct* fd, uint32_t size)
{
if(!fd)
- return 0;
+ return 0;
cluster_t cluster_num = fd->dir_entry.cluster;
uint16_t cluster_size = fd->fs->header.cluster_size;
@@ -1310,68 +1310,68 @@ uint8_t fat_resize_file(struct fat_file_struct* fd, uint32_t size)
do
{
- if(cluster_num == 0 && size_new == 0)
- /* the file stays empty */
- break;
-
- /* seek to the next cluster as long as we need the space */
- while(size_new > cluster_size)
- {
- /* get next cluster of file */
- cluster_t cluster_num_next = fat_get_next_cluster(fd->fs, cluster_num);
- if(cluster_num_next)
- {
- cluster_num = cluster_num_next;
- size_new -= cluster_size;
- }
- else
- {
- break;
- }
- }
-
- if(size_new > cluster_size || cluster_num == 0)
- {
- /* Allocate new cluster chain and append
- * it to the existing one, if available.
- */
- cluster_t cluster_count = (size_new + cluster_size - 1) / cluster_size;
- cluster_t cluster_new_chain = fat_append_clusters(fd->fs, cluster_num, cluster_count);
- if(!cluster_new_chain)
- return 0;
-
- if(!cluster_num)
- {
- cluster_num = cluster_new_chain;
- fd->dir_entry.cluster = cluster_num;
- }
- }
-
- /* write new directory entry */
- fd->dir_entry.file_size = size;
- if(size == 0)
- fd->dir_entry.cluster = 0;
- if(!fat_write_dir_entry(fd->fs, &fd->dir_entry))
- return 0;
-
- if(size == 0)
- {
- /* free all clusters of file */
- fat_free_clusters(fd->fs, cluster_num);
- }
- else if(size_new <= cluster_size)
- {
- /* free all clusters no longer needed */
- fat_terminate_clusters(fd->fs, cluster_num);
- }
+ if(cluster_num == 0 && size_new == 0)
+ /* the file stays empty */
+ break;
+
+ /* seek to the next cluster as long as we need the space */
+ while(size_new > cluster_size)
+ {
+ /* get next cluster of file */
+ cluster_t cluster_num_next = fat_get_next_cluster(fd->fs, cluster_num);
+ if(cluster_num_next)
+ {
+ cluster_num = cluster_num_next;
+ size_new -= cluster_size;
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ if(size_new > cluster_size || cluster_num == 0)
+ {
+ /* Allocate new cluster chain and append
+ * it to the existing one, if available.
+ */
+ cluster_t cluster_count = (size_new + cluster_size - 1) / cluster_size;
+ cluster_t cluster_new_chain = fat_append_clusters(fd->fs, cluster_num, cluster_count);
+ if(!cluster_new_chain)
+ return 0;
+
+ if(!cluster_num)
+ {
+ cluster_num = cluster_new_chain;
+ fd->dir_entry.cluster = cluster_num;
+ }
+ }
+
+ /* write new directory entry */
+ fd->dir_entry.file_size = size;
+ if(size == 0)
+ fd->dir_entry.cluster = 0;
+ if(!fat_write_dir_entry(fd->fs, &fd->dir_entry))
+ return 0;
+
+ if(size == 0)
+ {
+ /* free all clusters of file */
+ fat_free_clusters(fd->fs, cluster_num);
+ }
+ else if(size_new <= cluster_size)
+ {
+ /* free all clusters no longer needed */
+ fat_terminate_clusters(fd->fs, cluster_num);
+ }
} while(0);
/* correct file position */
if(size < fd->pos)
{
- fd->pos = size;
- fd->pos_cluster = 0;
+ fd->pos = size;
+ fd->pos_cluster = 0;
}
return 1;
@@ -1390,26 +1390,26 @@ uint8_t fat_resize_file(struct fat_file_struct* fd, uint32_t size)
struct fat_dir_struct* fat_open_dir(struct fat_fs_struct* fs, const struct fat_dir_entry_struct* dir_entry)
{
if(!fs || !dir_entry || !(dir_entry->attributes & FAT_ATTRIB_DIR))
- return 0;
+ return 0;
#if USE_DYNAMIC_MEMORY
struct fat_dir_struct* dd = malloc(sizeof(*dd));
if(!dd)
- return 0;
+ return 0;
#else
struct fat_dir_struct* dd = fat_dir_handles;
uint8_t i;
for(i = 0; i < FAT_DIR_COUNT; ++i)
{
- if(!dd->fs)
- break;
+ if(!dd->fs)
+ break;
- ++dd;
+ ++dd;
}
if(i >= FAT_DIR_COUNT)
- return 0;
+ return 0;
#endif
-
+
memcpy(&dd->dir_entry, dir_entry, sizeof(*dir_entry));
dd->fs = fs;
dd->entry_cluster = dir_entry->cluster;
@@ -1433,9 +1433,9 @@ void fat_close_dir(struct fat_dir_struct* dd)
{
if(dd)
#if USE_DYNAMIC_MEMORY
- free(dd);
+ free(dd);
#else
- dd->fs = 0;
+ dd->fs = 0;
#endif
}
@@ -1451,7 +1451,7 @@ void fat_close_dir(struct fat_dir_struct* dd)
uint8_t fat_read_dir(struct fat_dir_struct* dd, struct fat_dir_entry_struct* dir_entry)
{
if(!dd || !dir_entry)
- return 0;
+ return 0;
/* get current position of directory handle */
struct fat_fs_struct* fs = dd->fs;
@@ -1463,13 +1463,13 @@ uint8_t fat_read_dir(struct fat_dir_struct* dd, struct fat_dir_entry_struct* dir
if(cluster_offset >= cluster_size)
{
- /* The latest call hit the border of the last cluster in
- * the chain, but it still returned a directory entry.
- * So we now reset the handle and signal the caller the
- * end of the listing.
- */
- fat_reset_dir(dd);
- return 0;
+ /* The latest call hit the border of the last cluster in
+ * the chain, but it still returned a directory entry.
+ * So we now reset the handle and signal the caller the
+ * end of the listing.
+ */
+ fat_reset_dir(dd);
+ return 0;
}
/* reset callback arguments */
@@ -1481,67 +1481,67 @@ uint8_t fat_read_dir(struct fat_dir_struct* dd, struct fat_dir_entry_struct* dir
if(cluster_num == 0)
{
#if FAT_FAT32_SUPPORT
- if(fs->partition->type == PARTITION_TYPE_FAT32)
- cluster_num = header->root_dir_cluster;
- else
+ if(fs->partition->type == PARTITION_TYPE_FAT32)
+ cluster_num = header->root_dir_cluster;
+ else
#endif
- cluster_size = header->cluster_zero_offset - header->root_dir_offset;
+ cluster_size = header->cluster_zero_offset - header->root_dir_offset;
}
/* read entries */
uint8_t buffer[32];
while(!arg.finished)
{
- /* read directory entries up to the cluster border */
- uint16_t cluster_left = cluster_size - cluster_offset;
- offset_t pos = cluster_offset;
- if(cluster_num == 0)
- pos += header->root_dir_offset;
- else
- pos += fat_cluster_offset(fs, cluster_num);
-
- arg.bytes_read = 0;
- if(!fs->partition->device_read_interval(pos,
- buffer,
- sizeof(buffer),
- cluster_left,
- fat_dir_entry_read_callback,
- &arg)
- )
- return 0;
-
- cluster_offset += arg.bytes_read;
-
- if(cluster_offset >= cluster_size)
- {
- /* we reached the cluster border and switch to the next cluster */
-
- /* get number of next cluster */
- if((cluster_num = fat_get_next_cluster(fs, cluster_num)) != 0)
- {
- cluster_offset = 0;
- continue;
- }
-
- /* we are at the end of the cluster chain */
- if(!arg.finished)
- {
- /* directory entry not found, reset directory handle */
- fat_reset_dir(dd);
- return 0;
- }
- else
- {
- /* The current execution of the function has been successful,
- * so we can not signal an end of the directory listing to
- * the caller, but must wait for the next call. So we keep an
- * invalid cluster offset to mark this directory handle's
- * traversal as finished.
- */
- }
-
- break;
- }
+ /* read directory entries up to the cluster border */
+ uint16_t cluster_left = cluster_size - cluster_offset;
+ offset_t pos = cluster_offset;
+ if(cluster_num == 0)
+ pos += header->root_dir_offset;
+ else
+ pos += fat_cluster_offset(fs, cluster_num);
+
+ arg.bytes_read = 0;
+ if(!fs->partition->device_read_interval(pos,
+ buffer,
+ sizeof(buffer),
+ cluster_left,
+ fat_dir_entry_read_callback,
+ &arg)
+ )
+ return 0;
+
+ cluster_offset += arg.bytes_read;
+
+ if(cluster_offset >= cluster_size)
+ {
+ /* we reached the cluster border and switch to the next cluster */
+
+ /* get number of next cluster */
+ if((cluster_num = fat_get_next_cluster(fs, cluster_num)) != 0)
+ {
+ cluster_offset = 0;
+ continue;
+ }
+
+ /* we are at the end of the cluster chain */
+ if(!arg.finished)
+ {
+ /* directory entry not found, reset directory handle */
+ fat_reset_dir(dd);
+ return 0;
+ }
+ else
+ {
+ /* The current execution of the function has been successful,
+ * so we can not signal an end of the directory listing to
+ * the caller, but must wait for the next call. So we keep an
+ * invalid cluster offset to mark this directory handle's
+ * traversal as finished.
+ */
+ }
+
+ break;
+ }
}
dd->entry_cluster = cluster_num;
@@ -1564,7 +1564,7 @@ uint8_t fat_read_dir(struct fat_dir_struct* dd, struct fat_dir_entry_struct* dir
uint8_t fat_reset_dir(struct fat_dir_struct* dd)
{
if(!dd)
- return 0;
+ return 0;
dd->entry_cluster = dd->dir_entry.cluster;
dd->entry_offset = 0;
@@ -1577,13 +1577,13 @@ uint8_t fat_reset_dir(struct fat_dir_struct* dd)
*
* Interprets a raw directory entry and puts the contained
* information into a fat_dir_entry_struct structure.
- *
+ *
* For a single file there may exist multiple directory
* entries. All except the last one are lfn entries, which
* contain parts of the long filename. The last directory
* entry is a traditional 8.3 style one. It contains all
* other information like size, cluster, date and time.
- *
+ *
* \param[in] buffer A pointer to 32 bytes of raw data.
* \param[in] offset The absolute offset of the raw data.
* \param[in,out] p An argument structure controlling operation.
@@ -1601,111 +1601,111 @@ uint8_t fat_dir_entry_read_callback(uint8_t* buffer, offset_t offset, void* p)
if(buffer[0] == FAT_DIRENTRY_DELETED || !buffer[0])
{
#if FAT_LFN_SUPPORT
- arg->checksum = 0;
+ arg->checksum = 0;
#endif
- return 1;
+ return 1;
}
#if !FAT_LFN_SUPPORT
/* skip lfn entries */
if(buffer[11] == 0x0f)
- return 1;
+ return 1;
#endif
char* long_name = dir_entry->long_name;
#if FAT_LFN_SUPPORT
if(buffer[11] == 0x0f)
{
- /* checksum validation */
- if(arg->checksum == 0 || arg->checksum != buffer[13])
- {
- /* reset directory entry */
- memset(dir_entry, 0, sizeof(*dir_entry));
-
- arg->checksum = buffer[13];
- dir_entry->entry_offset = offset;
- }
-
- /* lfn supports unicode, but we do not, for now.
- * So we assume pure ascii and read only every
- * second byte.
- */
- uint16_t char_offset = ((buffer[0] & 0x3f) - 1) * 13;
- const uint8_t char_mapping[] = { 1, 3, 5, 7, 9, 14, 16, 18, 20, 22, 24, 28, 30 };
- for(uint8_t i = 0; i <= 12 && char_offset + i < sizeof(dir_entry->long_name) - 1; ++i)
- long_name[char_offset + i] = buffer[char_mapping[i]];
-
- return 1;
+ /* checksum validation */
+ if(arg->checksum == 0 || arg->checksum != buffer[13])
+ {
+ /* reset directory entry */
+ memset(dir_entry, 0, sizeof(*dir_entry));
+
+ arg->checksum = buffer[13];
+ dir_entry->entry_offset = offset;
+ }
+
+ /* lfn supports unicode, but we do not, for now.
+ * So we assume pure ascii and read only every
+ * second byte.
+ */
+ uint16_t char_offset = ((buffer[0] & 0x3f) - 1) * 13;
+ const uint8_t char_mapping[] = { 1, 3, 5, 7, 9, 14, 16, 18, 20, 22, 24, 28, 30 };
+ for(uint8_t i = 0; i <= 12 && char_offset + i < sizeof(dir_entry->long_name) - 1; ++i)
+ long_name[char_offset + i] = buffer[char_mapping[i]];
+
+ return 1;
}
else
#endif
{
#if FAT_LFN_SUPPORT
- /* if we do not have a long name or the previous lfn does not match, take the 8.3 name */
- if(long_name[0] == '\0' || arg->checksum != fat_calc_83_checksum(buffer))
-#endif
- {
- /* reset directory entry */
- memset(dir_entry, 0, sizeof(*dir_entry));
- dir_entry->entry_offset = offset;
-
- uint8_t i;
- for(i = 0; i < 8; ++i)
- {
- if(buffer[i] == ' ')
- break;
- long_name[i] = buffer[i];
-
- /* Windows NT and later versions do not store lfn entries
- * for 8.3 names which have a lowercase basename, extension
- * or both when everything else is uppercase. They use two
- * extra bits to signal a lowercase basename or extension.
- */
- if((buffer[12] & 0x08) && buffer[i] >= 'A' && buffer[i] <= 'Z')
- long_name[i] += 'a' - 'A';
- }
- if(long_name[0] == 0x05)
- long_name[0] = (char) FAT_DIRENTRY_DELETED;
-
- if(buffer[8] != ' ')
- {
- long_name[i++] = '.';
-
- uint8_t j = 8;
- for(; j < 11; ++j)
- {
- if(buffer[j] == ' ')
- break;
- long_name[i] = buffer[j];
-
- /* See above for the lowercase 8.3 name handling of
- * Windows NT and later.
- */
- if((buffer[12] & 0x10) && buffer[j] >= 'A' && buffer[j] <= 'Z')
- long_name[i] += 'a' - 'A';
-
- ++i;
- }
- }
-
- long_name[i] = '\0';
- }
-
- /* extract properties of file and store them within the structure */
- dir_entry->attributes = buffer[11];
- dir_entry->cluster = read16(&buffer[26]);
+ /* if we do not have a long name or the previous lfn does not match, take the 8.3 name */
+ if(long_name[0] == '\0' || arg->checksum != fat_calc_83_checksum(buffer))
+#endif
+ {
+ /* reset directory entry */
+ memset(dir_entry, 0, sizeof(*dir_entry));
+ dir_entry->entry_offset = offset;
+
+ uint8_t i;
+ for(i = 0; i < 8; ++i)
+ {
+ if(buffer[i] == ' ')
+ break;
+ long_name[i] = buffer[i];
+
+ /* Windows NT and later versions do not store lfn entries
+ * for 8.3 names which have a lowercase basename, extension
+ * or both when everything else is uppercase. They use two
+ * extra bits to signal a lowercase basename or extension.
+ */
+ if((buffer[12] & 0x08) && buffer[i] >= 'A' && buffer[i] <= 'Z')
+ long_name[i] += 'a' - 'A';
+ }
+ if(long_name[0] == 0x05)
+ long_name[0] = (char) FAT_DIRENTRY_DELETED;
+
+ if(buffer[8] != ' ')
+ {
+ long_name[i++] = '.';
+
+ uint8_t j = 8;
+ for(; j < 11; ++j)
+ {
+ if(buffer[j] == ' ')
+ break;
+ long_name[i] = buffer[j];
+
+ /* See above for the lowercase 8.3 name handling of
+ * Windows NT and later.
+ */
+ if((buffer[12] & 0x10) && buffer[j] >= 'A' && buffer[j] <= 'Z')
+ long_name[i] += 'a' - 'A';
+
+ ++i;
+ }
+ }
+
+ long_name[i] = '\0';
+ }
+
+ /* extract properties of file and store them within the structure */
+ dir_entry->attributes = buffer[11];
+ dir_entry->cluster = read16(&buffer[26]);
#if FAT_FAT32_SUPPORT
- dir_entry->cluster |= ((cluster_t) read16(&buffer[20])) << 16;
+ dir_entry->cluster |= ((cluster_t) read16(&buffer[20])) << 16;
#endif
- dir_entry->file_size = read32(&buffer[28]);
+ dir_entry->file_size = read32(&buffer[28]);
#if FAT_DATETIME_SUPPORT
- dir_entry->modification_time = read16(&buffer[22]);
- dir_entry->modification_date = read16(&buffer[24]);
+ dir_entry->modification_time = read16(&buffer[22]);
+ dir_entry->modification_date = read16(&buffer[24]);
#endif
- arg->finished = 1;
- return 0;
+ arg->finished = 1;
+ return 0;
}
}
@@ -1722,7 +1722,7 @@ uint8_t fat_calc_83_checksum(const uint8_t* file_name_83)
{
uint8_t checksum = file_name_83[0];
for(uint8_t i = 1; i < 11; ++i)
- checksum = ((checksum >> 1) | (checksum << 7)) + file_name_83[i];
+ checksum = ((checksum >> 1) | (checksum << 7)) + file_name_83[i];
return checksum;
}
@@ -1741,7 +1741,7 @@ uint8_t fat_calc_83_checksum(const uint8_t* file_name_83)
offset_t fat_find_offset_for_dir_entry(struct fat_fs_struct* fs, const struct fat_dir_struct* parent, const struct fat_dir_entry_struct* dir_entry)
{
if(!fs || !dir_entry)
- return 0;
+ return 0;
/* search for a place where to write the directory entry to disk */
#if FAT_LFN_SUPPORT
@@ -1759,88 +1759,88 @@ offset_t fat_find_offset_for_dir_entry(struct fat_fs_struct* fs, const struct fa
if(cluster_num == 0)
{
#if FAT_FAT32_SUPPORT
- if(is_fat32)
- {
- cluster_num = fs->header.root_dir_cluster;
- }
- else
-#endif
- {
- /* we read/write from the root directory entry */
- offset = fs->header.root_dir_offset;
- offset_to = fs->header.cluster_zero_offset;
- dir_entry_offset = offset;
- }
+ if(is_fat32)
+ {
+ cluster_num = fs->header.root_dir_cluster;
+ }
+ else
+#endif
+ {
+ /* we read/write from the root directory entry */
+ offset = fs->header.root_dir_offset;
+ offset_to = fs->header.cluster_zero_offset;
+ dir_entry_offset = offset;
+ }
}
-
+
while(1)
{
- if(offset == offset_to)
- {
- if(cluster_num == 0)
- /* We iterated through the whole root directory and
- * could not find enough space for the directory entry.
- */
- return 0;
-
- if(offset)
- {
- /* We reached a cluster boundary and have to
- * switch to the next cluster.
- */
-
- cluster_t cluster_next = fat_get_next_cluster(fs, cluster_num);
- if(!cluster_next)
- {
- cluster_next = fat_append_clusters(fs, cluster_num, 1);
- if(!cluster_next)
- return 0;
-
- /* we appended a new cluster and know it is free */
- dir_entry_offset = fs->header.cluster_zero_offset +
- (offset_t) (cluster_next - 2) * fs->header.cluster_size;
-
- /* clear cluster to avoid garbage directory entries */
- fat_clear_cluster(fs, cluster_next);
-
- break;
- }
- cluster_num = cluster_next;
- }
-
- offset = fat_cluster_offset(fs, cluster_num);
- offset_to = offset + fs->header.cluster_size;
- dir_entry_offset = offset;
+ if(offset == offset_to)
+ {
+ if(cluster_num == 0)
+ /* We iterated through the whole root directory and
+ * could not find enough space for the directory entry.
+ */
+ return 0;
+
+ if(offset)
+ {
+ /* We reached a cluster boundary and have to
+ * switch to the next cluster.
+ */
+
+ cluster_t cluster_next = fat_get_next_cluster(fs, cluster_num);
+ if(!cluster_next)
+ {
+ cluster_next = fat_append_clusters(fs, cluster_num, 1);
+ if(!cluster_next)
+ return 0;
+
+ /* we appended a new cluster and know it is free */
+ dir_entry_offset = fs->header.cluster_zero_offset +
+ (offset_t) (cluster_next - 2) * fs->header.cluster_size;
+
+ /* clear cluster to avoid garbage directory entries */
+ fat_clear_cluster(fs, cluster_next);
+
+ break;
+ }
+ cluster_num = cluster_next;
+ }
+
+ offset = fat_cluster_offset(fs, cluster_num);
+ offset_to = offset + fs->header.cluster_size;
+ dir_entry_offset = offset;
#if FAT_LFN_SUPPORT
- free_dir_entries_found = 0;
-#endif
- }
-
- /* read next lfn or 8.3 entry */
- uint8_t first_char;
- if(!fs->partition->device_read(offset, &first_char, sizeof(first_char)))
- return 0;
-
- /* check if we found a free directory entry */
- if(first_char == FAT_DIRENTRY_DELETED || !first_char)
- {
- /* check if we have the needed number of available entries */
+ free_dir_entries_found = 0;
+#endif
+ }
+
+ /* read next lfn or 8.3 entry */
+ uint8_t first_char;
+ if(!fs->partition->device_read(offset, &first_char, sizeof(first_char)))
+ return 0;
+
+ /* check if we found a free directory entry */
+ if(first_char == FAT_DIRENTRY_DELETED || !first_char)
+ {
+ /* check if we have the needed number of available entries */
#if FAT_LFN_SUPPORT
- ++free_dir_entries_found;
- if(free_dir_entries_found >= free_dir_entries_needed)
+ ++free_dir_entries_found;
+ if(free_dir_entries_found >= free_dir_entries_needed)
#endif
- break;
+ break;
- offset += 32;
- }
- else
- {
- offset += 32;
- dir_entry_offset = offset;
+ offset += 32;
+ }
+ else
+ {
+ offset += 32;
+ dir_entry_offset = offset;
#if FAT_LFN_SUPPORT
- free_dir_entries_found = 0;
+ free_dir_entries_found = 0;
#endif
- }
+ }
}
return dir_entry_offset;
@@ -1870,20 +1870,20 @@ offset_t fat_find_offset_for_dir_entry(struct fat_fs_struct* fs, const struct fa
uint8_t fat_write_dir_entry(const struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir_entry)
{
if(!fs || !dir_entry)
- return 0;
-
+ return 0;
+
#if FAT_DATETIME_SUPPORT
{
- uint16_t year;
- uint8_t month;
- uint8_t day;
- uint8_t hour;
- uint8_t min;
- uint8_t sec;
-
- fat_get_datetime(&year, &month, &day, &hour, &min, &sec);
- fat_set_file_modification_date(dir_entry, year, month, day);
- fat_set_file_modification_time(dir_entry, hour, min, sec);
+ uint16_t year;
+ uint8_t month;
+ uint8_t day;
+ uint8_t hour;
+ uint8_t min;
+ uint8_t sec;
+
+ fat_get_datetime(&year, &month, &day, &hour, &min, &sec);
+ fat_set_file_modification_date(dir_entry, year, month, day);
+ fat_set_file_modification_time(dir_entry, hour, min, sec);
}
#endif
@@ -1903,59 +1903,59 @@ uint8_t fat_write_dir_entry(const struct fat_fs_struct* fs, struct fat_dir_entry
char* name_ext = strrchr(name, '.');
if(name_ext && *++name_ext)
{
- uint8_t name_ext_len = strlen(name_ext);
- name_len -= name_ext_len + 1;
+ uint8_t name_ext_len = strlen(name_ext);
+ name_len -= name_ext_len + 1;
- if(name_ext_len > 3)
+ if(name_ext_len > 3)
#if FAT_LFN_SUPPORT
- name_ext_len = 3;
+ name_ext_len = 3;
#else
- return 0;
+ return 0;
#endif
-
- memcpy(&buffer[8], name_ext, name_ext_len);
+
+ memcpy(&buffer[8], name_ext, name_ext_len);
}
-
+
if(name_len <= 8)
{
- memcpy(buffer, name, name_len);
+ memcpy(buffer, name, name_len);
#if FAT_LFN_SUPPORT
- /* For now, we create lfn entries for all files,
- * except the "." and ".." directory references.
- * This is to avoid difficulties with capitalization,
- * as 8.3 filenames allow uppercase letters only.
- *
- * Theoretically it would be possible to leave
- * the 8.3 entry alone if the basename and the
- * extension have no mixed capitalization.
- */
- if(name[0] == '.' &&
- ((name[1] == '.' && name[2] == '\0') ||
- name[1] == '\0')
- )
- lfn_entry_count = 0;
+ /* For now, we create lfn entries for all files,
+ * except the "." and ".." directory references.
+ * This is to avoid difficulties with capitalization,
+ * as 8.3 filenames allow uppercase letters only.
+ *
+ * Theoretically it would be possible to leave
+ * the 8.3 entry alone if the basename and the
+ * extension have no mixed capitalization.
+ */
+ if(name[0] == '.' &&
+ ((name[1] == '.' && name[2] == '\0') ||
+ name[1] == '\0')
+ )
+ lfn_entry_count = 0;
#endif
}
else
{
#if FAT_LFN_SUPPORT
- memcpy(buffer, name, 8);
+ memcpy(buffer, name, 8);
- /* Minimize 8.3 name clashes by appending
- * the lower byte of the cluster number.
- */
- uint8_t num = dir_entry->cluster & 0xff;
+ /* Minimize 8.3 name clashes by appending
+ * the lower byte of the cluster number.
+ */
+ uint8_t num = dir_entry->cluster & 0xff;
- buffer[6] = (num < 0xa0) ? ('0' + (num >> 4)) : ('a' + (num >> 4));
- num &= 0x0f;
- buffer[7] = (num < 0x0a) ? ('0' + num) : ('a' + num);
+ buffer[6] = (num < 0xa0) ? ('0' + (num >> 4)) : ('a' + (num >> 4));
+ num &= 0x0f;
+ buffer[7] = (num < 0x0a) ? ('0' + num) : ('a' + num);
#else
- return 0;
+ return 0;
#endif
}
if(buffer[0] == FAT_DIRENTRY_DELETED)
- buffer[0] = 0x05;
+ buffer[0] = 0x05;
/* fill directory entry buffer */
memset(&buffer[11], 0, sizeof(buffer) - 11);
@@ -1976,62 +1976,62 @@ uint8_t fat_write_dir_entry(const struct fat_fs_struct* fs, struct fat_dir_entry
#else
if(!device_write(offset, buffer, sizeof(buffer)))
#endif
- return 0;
-
+ return 0;
+
#if FAT_LFN_SUPPORT
/* calculate checksum of 8.3 name */
uint8_t checksum = fat_calc_83_checksum(buffer);
-
+
/* write lfn entries */
for(uint8_t lfn_entry = lfn_entry_count; lfn_entry > 0; --lfn_entry)
{
- memset(buffer, 0xff, sizeof(buffer));
-
- /* set file name */
- const char* long_name_curr = name + (lfn_entry - 1) * 13;
- uint8_t i = 1;
- while(i < 0x1f)
- {
- buffer[i++] = *long_name_curr;
- buffer[i++] = 0;
-
- switch(i)
- {
- case 0x0b:
- i = 0x0e;
- break;
- case 0x1a:
- i = 0x1c;
- break;
- }
-
- if(!*long_name_curr++)
- break;
- }
-
- /* set index of lfn entry */
- buffer[0x00] = lfn_entry;
- if(lfn_entry == lfn_entry_count)
- buffer[0x00] |= FAT_DIRENTRY_LFNLAST;
-
- /* mark as lfn entry */
- buffer[0x0b] = 0x0f;
-
- /* set 8.3 checksum */
- buffer[0x0d] = checksum;
-
- /* clear reserved bytes */
- buffer[0x0c] = 0;
- buffer[0x1a] = 0;
- buffer[0x1b] = 0;
-
- /* write entry */
- device_write(offset, buffer, sizeof(buffer));
-
- offset += sizeof(buffer);
+ memset(buffer, 0xff, sizeof(buffer));
+
+ /* set file name */
+ const char* long_name_curr = name + (lfn_entry - 1) * 13;
+ uint8_t i = 1;
+ while(i < 0x1f)
+ {
+ buffer[i++] = *long_name_curr;
+ buffer[i++] = 0;
+
+ switch(i)
+ {
+ case 0x0b:
+ i = 0x0e;
+ break;
+ case 0x1a:
+ i = 0x1c;
+ break;
+ }
+
+ if(!*long_name_curr++)
+ break;
+ }
+
+ /* set index of lfn entry */
+ buffer[0x00] = lfn_entry;
+ if(lfn_entry == lfn_entry_count)
+ buffer[0x00] |= FAT_DIRENTRY_LFNLAST;
+
+ /* mark as lfn entry */
+ buffer[0x0b] = 0x0f;
+
+ /* set 8.3 checksum */
+ buffer[0x0d] = checksum;
+
+ /* clear reserved bytes */
+ buffer[0x0c] = 0;
+ buffer[0x1a] = 0;
+ buffer[0x1b] = 0;
+
+ /* write entry */
+ device_write(offset, buffer, sizeof(buffer));
+
+ offset += sizeof(buffer);
}
#endif
-
+
return 1;
}
#endif
@@ -2066,19 +2066,19 @@ uint8_t fat_write_dir_entry(const struct fat_fs_struct* fs, struct fat_dir_entry
uint8_t fat_create_file(struct fat_dir_struct* parent, const char* file, struct fat_dir_entry_struct* dir_entry)
{
if(!parent || !file || !file[0] || !dir_entry)
- return 0;
+ return 0;
/* check if the file already exists */
while(1)
{
- if(!fat_read_dir(parent, dir_entry))
- break;
-
- if(strcmp(file, dir_entry->long_name) == 0)
- {
- fat_reset_dir(parent);
- return 2;
- }
+ if(!fat_read_dir(parent, dir_entry))
+ break;
+
+ if(strcmp(file, dir_entry->long_name) == 0)
+ {
+ fat_reset_dir(parent);
+ return 2;
+ }
}
struct fat_fs_struct* fs = parent->fs;
@@ -2089,12 +2089,12 @@ uint8_t fat_create_file(struct fat_dir_struct* parent, const char* file, struct
/* find place where to store directory entry */
if(!(dir_entry->entry_offset = fat_find_offset_for_dir_entry(fs, parent, dir_entry)))
- return 0;
-
+ return 0;
+
/* write directory entry to disk */
if(!fat_write_dir_entry(fs, dir_entry))
- return 0;
-
+ return 0;
+
return 1;
}
#endif
@@ -2108,7 +2108,7 @@ uint8_t fat_create_file(struct fat_dir_struct* parent, const char* file, struct
* subdirectories and files, disk space occupied by these
* files will get wasted as there is no chance to release
* it and mark it as free.
- *
+ *
* \param[in] fs The filesystem on which to operate.
* \param[in] dir_entry The directory entry of the file to delete.
* \returns 0 on failure, 1 on success.
@@ -2117,39 +2117,39 @@ uint8_t fat_create_file(struct fat_dir_struct* parent, const char* file, struct
uint8_t fat_delete_file(struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir_entry)
{
if(!fs || !dir_entry)
- return 0;
+ return 0;
/* get offset of the file's directory entry */
offset_t dir_entry_offset = dir_entry->entry_offset;
if(!dir_entry_offset)
- return 0;
+ return 0;
#if FAT_LFN_SUPPORT
uint8_t buffer[12];
while(1)
{
- /* read directory entry */
- if(!fs->partition->device_read(dir_entry_offset, buffer, sizeof(buffer)))
- return 0;
-
- /* mark the directory entry as deleted */
- buffer[0] = FAT_DIRENTRY_DELETED;
-
- /* write back entry */
- if(!fs->partition->device_write(dir_entry_offset, buffer, sizeof(buffer)))
- return 0;
-
- /* check if we deleted the whole entry */
- if(buffer[11] != 0x0f)
- break;
-
- dir_entry_offset += 32;
+ /* read directory entry */
+ if(!fs->partition->device_read(dir_entry_offset, buffer, sizeof(buffer)))
+ return 0;
+
+ /* mark the directory entry as deleted */
+ buffer[0] = FAT_DIRENTRY_DELETED;
+
+ /* write back entry */
+ if(!fs->partition->device_write(dir_entry_offset, buffer, sizeof(buffer)))
+ return 0;
+
+ /* check if we deleted the whole entry */
+ if(buffer[11] != 0x0f)
+ break;
+
+ dir_entry_offset += 32;
}
#else
/* mark the directory entry as deleted */
uint8_t first_char = FAT_DIRENTRY_DELETED;
if(!fs->partition->device_write(dir_entry_offset, &first_char, 1))
- return 0;
+ return 0;
#endif
/* We deleted the directory entry. The next thing to do is
@@ -2169,7 +2169,7 @@ uint8_t fat_delete_file(struct fat_fs_struct* fs, struct fat_dir_entry_struct* d
* target file name must not exist. Moving a file to a
* different filesystem (i.e. \a parent_new doesn't lie on
* \a fs) is not supported.
- *
+ *
* After successfully renaming (and moving) the file, the
* given directory entry is updated such that it points to
* the file's new location.
@@ -2187,18 +2187,18 @@ uint8_t fat_delete_file(struct fat_fs_struct* fs, struct fat_dir_entry_struct* d
uint8_t fat_move_file(struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir_entry, struct fat_dir_struct* parent_new, const char* file_new)
{
if(!fs || !dir_entry || !parent_new || (file_new && !file_new[0]))
- return 0;
+ return 0;
if(fs != parent_new->fs)
- return 0;
+ return 0;
/* use existing file name if none has been specified */
if(!file_new)
- file_new = dir_entry->long_name;
+ file_new = dir_entry->long_name;
/* create file with new file name */
struct fat_dir_entry_struct dir_entry_new;
if(!fat_create_file(parent_new, file_new, &dir_entry_new))
- return 0;
+ return 0;
/* copy members of directory entry which do not change with rename */
dir_entry_new.attributes = dir_entry->attributes;
@@ -2212,14 +2212,14 @@ uint8_t fat_move_file(struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir
/* make the new file name point to the old file's content */
if(!fat_write_dir_entry(fs, &dir_entry_new))
{
- fat_delete_file(fs, &dir_entry_new);
- return 0;
+ fat_delete_file(fs, &dir_entry_new);
+ return 0;
}
-
+
/* delete the old file, but not its clusters, which have already been remapped above */
dir_entry->cluster = 0;
if(!fat_delete_file(fs, dir_entry))
- return 0;
+ return 0;
*dir_entry = dir_entry_new;
return 1;
@@ -2248,16 +2248,16 @@ uint8_t fat_move_file(struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir
uint8_t fat_create_dir(struct fat_dir_struct* parent, const char* dir, struct fat_dir_entry_struct* dir_entry)
{
if(!parent || !dir || !dir[0] || !dir_entry)
- return 0;
+ return 0;
/* check if the file or directory already exists */
while(fat_read_dir(parent, dir_entry))
{
- if(strcmp(dir, dir_entry->long_name) == 0)
- {
- fat_reset_dir(parent);
- return 0;
- }
+ if(strcmp(dir, dir_entry->long_name) == 0)
+ {
+ fat_reset_dir(parent);
+ return 0;
+ }
}
struct fat_fs_struct* fs = parent->fs;
@@ -2265,23 +2265,23 @@ uint8_t fat_create_dir(struct fat_dir_struct* parent, const char* dir, struct fa
/* allocate cluster which will hold directory entries */
cluster_t dir_cluster = fat_append_clusters(fs, 0, 1);
if(!dir_cluster)
- return 0;
+ return 0;
/* clear cluster to prevent bogus directory entries */
fat_clear_cluster(fs, dir_cluster);
-
+
memset(dir_entry, 0, sizeof(*dir_entry));
dir_entry->attributes = FAT_ATTRIB_DIR;
/* create "." directory self reference */
dir_entry->entry_offset = fs->header.cluster_zero_offset +
- (offset_t) (dir_cluster - 2) * fs->header.cluster_size;
+ (offset_t) (dir_cluster - 2) * fs->header.cluster_size;
dir_entry->long_name[0] = '.';
dir_entry->cluster = dir_cluster;
if(!fat_write_dir_entry(fs, dir_entry))
{
- fat_free_clusters(fs, dir_cluster);
- return 0;
+ fat_free_clusters(fs, dir_cluster);
+ return 0;
}
/* create ".." parent directory reference */
@@ -2290,8 +2290,8 @@ uint8_t fat_create_dir(struct fat_dir_struct* parent, const char* dir, struct fa
dir_entry->cluster = parent->dir_entry.cluster;
if(!fat_write_dir_entry(fs, dir_entry))
{
- fat_free_clusters(fs, dir_cluster);
- return 0;
+ fat_free_clusters(fs, dir_cluster);
+ return 0;
}
/* fill directory entry */
@@ -2301,15 +2301,15 @@ uint8_t fat_create_dir(struct fat_dir_struct* parent, const char* dir, struct fa
/* find place where to store directory entry */
if(!(dir_entry->entry_offset = fat_find_offset_for_dir_entry(fs, parent, dir_entry)))
{
- fat_free_clusters(fs, dir_cluster);
- return 0;
+ fat_free_clusters(fs, dir_cluster);
+ return 0;
}
/* write directory to disk */
if(!fat_write_dir_entry(fs, dir_entry))
{
- fat_free_clusters(fs, dir_cluster);
- return 0;
+ fat_free_clusters(fs, dir_cluster);
+ return 0;
}
return 1;
@@ -2325,7 +2325,7 @@ uint8_t fat_create_dir(struct fat_dir_struct* parent, const char* dir, struct fa
* subdirectories and files, disk space occupied by these
* files will get wasted as there is no chance to release
* it and mark it as free.
- *
+ *
* \param[in] fs The filesystem on which to operate.
* \param[in] dir_entry The directory entry of the directory to delete.
* \returns 0 on failure, 1 on success.
@@ -2340,7 +2340,7 @@ uint8_t fat_delete_dir(struct fat_fs_struct* fs, struct fat_dir_entry_struct* di
* Moves or renames a directory.
*
* This is just a synonym for fat_move_file().
- *
+ *
* \param[in] fs The filesystem on which to operate.
* \param[in,out] dir_entry The directory entry of the directory to move.
* \param[in] parent_new The handle of the new parent directory.
@@ -2365,7 +2365,7 @@ uint8_t fat_move_dir(struct fat_fs_struct* fs, struct fat_dir_entry_struct* dir_
void fat_get_file_modification_date(const struct fat_dir_entry_struct* dir_entry, uint16_t* year, uint8_t* month, uint8_t* day)
{
if(!dir_entry)
- return;
+ return;
*year = 1980 + ((dir_entry->modification_date >> 9) & 0x7f);
*month = (dir_entry->modification_date >> 5) & 0x0f;
@@ -2386,7 +2386,7 @@ void fat_get_file_modification_date(const struct fat_dir_entry_struct* dir_entry
void fat_get_file_modification_time(const struct fat_dir_entry_struct* dir_entry, uint8_t* hour, uint8_t* min, uint8_t* sec)
{
if(!dir_entry)
- return;
+ return;
*hour = (dir_entry->modification_time >> 11) & 0x1f;
*min = (dir_entry->modification_time >> 5) & 0x3f;
@@ -2407,12 +2407,12 @@ void fat_get_file_modification_time(const struct fat_dir_entry_struct* dir_entry
void fat_set_file_modification_date(struct fat_dir_entry_struct* dir_entry, uint16_t year, uint8_t month, uint8_t day)
{
if(!dir_entry)
- return;
+ return;
dir_entry->modification_date =
- ((year - 1980) << 9) |
- ((uint16_t) month << 5) |
- ((uint16_t) day << 0);
+ ((year - 1980) << 9) |
+ ((uint16_t) month << 5) |
+ ((uint16_t) day << 0);
}
#endif
@@ -2429,12 +2429,12 @@ void fat_set_file_modification_date(struct fat_dir_entry_struct* dir_entry, uint
void fat_set_file_modification_time(struct fat_dir_entry_struct* dir_entry, uint8_t hour, uint8_t min, uint8_t sec)
{
if(!dir_entry)
- return;
+ return;
dir_entry->modification_time =
- ((uint16_t) hour << 11) |
- ((uint16_t) min << 5) |
- ((uint16_t) sec >> 1) ;
+ ((uint16_t) hour << 11) |
+ ((uint16_t) min << 5) |
+ ((uint16_t) sec >> 1) ;
}
#endif
@@ -2448,14 +2448,14 @@ void fat_set_file_modification_time(struct fat_dir_entry_struct* dir_entry, uint
offset_t fat_get_fs_size(const struct fat_fs_struct* fs)
{
if(!fs)
- return 0;
+ return 0;
#if FAT_FAT32_SUPPORT
if(fs->partition->type == PARTITION_TYPE_FAT32)
- return (offset_t) (fs->header.fat_size / 4 - 2) * fs->header.cluster_size;
+ return (offset_t) (fs->header.fat_size / 4 - 2) * fs->header.cluster_size;
else
#endif
- return (offset_t) (fs->header.fat_size / 2 - 2) * fs->header.cluster_size;
+ return (offset_t) (fs->header.fat_size / 2 - 2) * fs->header.cluster_size;
}
/**
@@ -2471,7 +2471,7 @@ offset_t fat_get_fs_size(const struct fat_fs_struct* fs)
offset_t fat_get_fs_free(const struct fat_fs_struct* fs)
{
if(!fs)
- return 0;
+ return 0;
uint8_t fat[32];
struct fat_usage_count_callback_arg count_arg;
@@ -2482,28 +2482,28 @@ offset_t fat_get_fs_free(const struct fat_fs_struct* fs)
uint32_t fat_size = fs->header.fat_size;
while(fat_size > 0)
{
- uintptr_t length = UINTPTR_MAX - 1;
- if(fat_size < length)
- length = fat_size;
-
- if(!fs->partition->device_read_interval(fat_offset,
- fat,
- sizeof(fat),
- length,
+ uintptr_t length = UINTPTR_MAX - 1;
+ if(fat_size < length)
+ length = fat_size;
+
+ if(!fs->partition->device_read_interval(fat_offset,
+ fat,
+ sizeof(fat),
+ length,
#if FAT_FAT32_SUPPORT
- (fs->partition->type == PARTITION_TYPE_FAT16) ?
- fat_get_fs_free_16_callback :
- fat_get_fs_free_32_callback,
+ (fs->partition->type == PARTITION_TYPE_FAT16) ?
+ fat_get_fs_free_16_callback :
+ fat_get_fs_free_32_callback,
#else
- fat_get_fs_free_16_callback,
+ fat_get_fs_free_16_callback,
#endif
- &count_arg
- )
- )
- return 0;
+ &count_arg
+ )
+ )
+ return 0;
- fat_offset += length;
- fat_size -= length;
+ fat_offset += length;
+ fat_size -= length;
}
return (offset_t) count_arg.cluster_count * fs->header.cluster_size;
@@ -2520,9 +2520,9 @@ uint8_t fat_get_fs_free_16_callback(uint8_t* buffer, offset_t offset, void* p)
for(uintptr_t i = 0; i < buffer_size; i += 2, buffer += 2)
{
- uint16_t cluster = read16(buffer);
- if(cluster == HTOL16(FAT16_CLUSTER_FREE))
- ++(count_arg->cluster_count);
+ uint16_t cluster = read16(buffer);
+ if(cluster == HTOL16(FAT16_CLUSTER_FREE))
+ ++(count_arg->cluster_count);
}
return 1;
@@ -2540,9 +2540,9 @@ uint8_t fat_get_fs_free_32_callback(uint8_t* buffer, offset_t offset, void* p)
for(uintptr_t i = 0; i < buffer_size; i += 4, buffer += 4)
{
- uint32_t cluster = read32(buffer);
- if(cluster == HTOL32(FAT32_CLUSTER_FREE))
- ++(count_arg->cluster_count);
+ uint32_t cluster = read32(buffer);
+ if(cluster == HTOL32(FAT32_CLUSTER_FREE))
+ ++(count_arg->cluster_count);
}
return 1;
diff --git a/fat_config.h b/fat_config.h
index 0ca8d4c..6e22e00 100644
--- a/fat_config.h
+++ b/fat_config.h
@@ -48,7 +48,7 @@ extern "C"
/**
* \ingroup fat_config
* Controls FAT date and time support.
- *
+ *
* Set to 1 to enable FAT date and time stamping support.
*/
#define FAT_DATETIME_SUPPORT 0
diff --git a/i2c_config.h b/i2c_config.h
index 48d84e5..2634073 100644
--- a/i2c_config.h
+++ b/i2c_config.h
@@ -1,6 +1,6 @@
-/*
+/*
* Copyright Droids Corporation, Microb Technology, Eirbot (2005)
- *
+ *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
diff --git a/i2c_helper.c b/i2c_helper.c
index de929ae..b6e304c 100644
--- a/i2c_helper.c
+++ b/i2c_helper.c
@@ -1,7 +1,6 @@
#include
#include
-#include
#include
#include
@@ -16,17 +15,17 @@ uint8_t read_reg(uint8_t address_dev, uint8_t address_reg, uint8_t * value)
uint8_t err = 0;
err = i2c_send(address_dev, &address_reg, 1, I2C_CTRL_SYNC);
if (err) {
- printf("i2c error\r\n");
+ printf("read reg: i2c error send\r\n");
return err;
}
err = i2c_recv(address_dev, 1, I2C_CTRL_SYNC);
if (err) {
- printf("i2c error\r\n");
+ printf("read reg: i2c error recv\r\n");
return err;
}
err = i2c_get_recv_buffer(value, 1);
if (err != 1) {
- printf("i2c error\r\n");
+ printf("read reg: i2c error get recv\r\n");
return 0xff;
}
return 0;
@@ -39,18 +38,18 @@ uint8_t read_reg_len(uint8_t address_dev, uint8_t address_reg, uint8_t * values,
uint8_t err = 0;
err = i2c_send(address_dev, &address_reg, 1, I2C_CTRL_SYNC);
if (err) {
- printf("i2c error\r\n");
+ printf("read reg len: i2c error send\r\n");
return err;
}
err = i2c_recv(address_dev, len, I2C_CTRL_SYNC);
if (err) {
- printf("i2c error\r\n");
+ printf("read reg len: i2c error recv\r\n");
return err;
}
err = i2c_get_recv_buffer(values, len);
if (err != len) {
- printf("i2c error\r\n");
- return 0xDD;
+ printf("read reg len: i2c error get recv\r\n");
+ return 0xFF;
}
return 0;
diff --git a/partition.c b/partition.c
index 6c5d982..d4eb015 100644
--- a/partition.c
+++ b/partition.c
@@ -68,36 +68,36 @@ struct partition_struct* partition_open(device_read_t device_read, device_read_i
uint8_t buffer[0x10];
if(!device_read || !device_read_interval || index >= 4)
- return 0;
+ return 0;
if(index >= 0)
{
- /* read specified partition table index */
- if(!device_read(0x01be + index * 0x10, buffer, sizeof(buffer)))
- return 0;
+ /* read specified partition table index */
+ if(!device_read(0x01be + index * 0x10, buffer, sizeof(buffer)))
+ return 0;
- /* abort on empty partition entry */
- if(buffer[4] == 0x00)
- return 0;
+ /* abort on empty partition entry */
+ if(buffer[4] == 0x00)
+ return 0;
}
/* allocate partition descriptor */
#if USE_DYNAMIC_MEMORY
new_partition = malloc(sizeof(*new_partition));
if(!new_partition)
- return 0;
+ return 0;
#else
new_partition = partition_handles;
uint8_t i;
for(i = 0; i < PARTITION_COUNT; ++i)
{
- if(new_partition->type == PARTITION_TYPE_FREE)
- break;
+ if(new_partition->type == PARTITION_TYPE_FREE)
+ break;
- ++new_partition;
+ ++new_partition;
}
if(i >= PARTITION_COUNT)
- return 0;
+ return 0;
#endif
memset(new_partition, 0, sizeof(*new_partition));
@@ -110,13 +110,13 @@ struct partition_struct* partition_open(device_read_t device_read, device_read_i
if(index >= 0)
{
- new_partition->type = buffer[4];
- new_partition->offset = read32(&buffer[8]);
- new_partition->length = read32(&buffer[12]);
+ new_partition->type = buffer[4];
+ new_partition->offset = read32(&buffer[8]);
+ new_partition->length = read32(&buffer[12]);
}
else
{
- new_partition->type = 0xff;
+ new_partition->type = 0xff;
}
return new_partition;
@@ -137,7 +137,7 @@ struct partition_struct* partition_open(device_read_t device_read, device_read_i
uint8_t partition_close(struct partition_struct* partition)
{
if(!partition)
- return 0;
+ return 0;
/* destroy partition descriptor */
#if USE_DYNAMIC_MEMORY
diff --git a/sd_main.c b/sd_main.c
index 47298d4..82bb8ad 100644
--- a/sd_main.c
+++ b/sd_main.c
@@ -42,7 +42,7 @@
* The first one is the one I started with, but when I implemented FAT16 write
* support, I ran out of flash space and switched to the ATmega168. For FAT32, an
* ATmega328 is required.
- *
+ *
* The circuit board is a self-made and self-soldered board consisting of a single
* copper layer and standard DIL components, except of the MMC/SD card connector.
*
@@ -50,7 +50,7 @@
* eject button which, when a card is inserted, needs some space beyond the connector
* itself. As an additional feature the connector has two electrical switches
* to detect wether a card is inserted and wether this card is write-protected.
- *
+ *
* \section pictures Pictures
* \image html pic01.jpg "The circuit board used to implement and test this application."
* \image html pic02.jpg "The MMC/SD card connector on the soldering side of the circuit board."
@@ -78,7 +78,7 @@
* - mkdir \\n
* Creates a directory called \.
* - mv \ \\n
- * Renames \ to \.
+ * Renames \ to \.
* - rm \\n
* Deletes \.
* - sync\n
@@ -122,13 +122,13 @@
* The static RAM is mostly used for buffering memory card access, which
* improves performance and reduces implementation complexity.
*
- *
+ *
*
* Please note that the numbers above do not include the C library functions
* used, e.g. some string functions. These will raise the numbers somewhat
* if they are not already used in other program parts.
*
- *
+ *
*
* When opening a partition, filesystem, file or directory, a little amount
* of RAM is used, as listed in the following table. Depending on the library
@@ -157,7 +157,7 @@
*
49 |
*
*
- *
+ *
* \endhtmlonly
*
* \section adaptation Adapting the software to your needs
@@ -174,7 +174,7 @@
*
* For further information, visit the project's
* FAQ page.
- *
+ *
* \section bugs Bugs or comments?
* If you have comments or found a bug in the software - there might be some
* of them - you may contact me per mail at feedback@roland-riegel.de.
@@ -183,7 +183,7 @@
* Thanks go to Ulrich Radig, who explained on his homepage how to interface
* MMC cards to the Atmel microcontroller (http://www.ulrichradig.de/).
* I adapted his work for my circuit.
- *
+ *
* \section copyright Copyright 2006-2012 by Roland Riegel
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by
@@ -208,7 +208,7 @@
static uint8_t read_line(char* buffer, uint8_t buffer_length);
static uint32_t strtolong(const char* str);
static 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);
-static struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name);
+static struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name);
static uint8_t print_disk_info(const struct fat_fs_struct* fs);
int main()
@@ -221,333 +221,333 @@ int main()
while(1)
{
- /* setup sd card slot */
- if(!sd_raw_init())
- {
+ /* setup sd card slot */
+ if(!sd_raw_init())
+ {
#if DEBUG
- uart_puts_p(PSTR("MMC/SD initialization failed\n"));
+ uart_puts_p(PSTR("MMC/SD initialization failed\n"));
#endif
- continue;
- }
+ continue;
+ }
- /* open first partition */
- struct partition_struct* partition = partition_open(sd_raw_read,
- sd_raw_read_interval,
+ /* open first partition */
+ struct partition_struct* partition = partition_open(sd_raw_read,
+ sd_raw_read_interval,
#if SD_RAW_WRITE_SUPPORT
- sd_raw_write,
- sd_raw_write_interval,
+ sd_raw_write,
+ sd_raw_write_interval,
#else
- 0,
- 0,
+ 0,
+ 0,
#endif
- 0
- );
-
- if(!partition)
- {
- /* If the partition did not open, assume the storage device
- * is a "superfloppy", i.e. has no MBR.
- */
- partition = partition_open(sd_raw_read,
- sd_raw_read_interval,
+ 0
+ );
+
+ if(!partition)
+ {
+ /* If the partition did not open, assume the storage device
+ * is a "superfloppy", i.e. has no MBR.
+ */
+ partition = partition_open(sd_raw_read,
+ sd_raw_read_interval,
#if SD_RAW_WRITE_SUPPORT
- sd_raw_write,
- sd_raw_write_interval,
+ sd_raw_write,
+ sd_raw_write_interval,
#else
- 0,
- 0,
+ 0,
+ 0,
#endif
- -1
- );
- if(!partition)
- {
+ -1
+ );
+ if(!partition)
+ {
#if DEBUG
- uart_puts_p(PSTR("opening partition failed\n"));
+ uart_puts_p(PSTR("opening partition failed\n"));
#endif
- continue;
- }
- }
-
- /* open file system */
- struct fat_fs_struct* fs = fat_open(partition);
- if(!fs)
- {
+ continue;
+ }
+ }
+
+ /* open file system */
+ struct fat_fs_struct* fs = fat_open(partition);
+ if(!fs)
+ {
#if DEBUG
- uart_puts_p(PSTR("opening filesystem failed\n"));
+ uart_puts_p(PSTR("opening filesystem failed\n"));
#endif
- continue;
- }
+ continue;
+ }
- /* open root directory */
- struct fat_dir_entry_struct directory;
- fat_get_dir_entry_of_path(fs, "/", &directory);
+ /* open root directory */
+ struct fat_dir_entry_struct directory;
+ fat_get_dir_entry_of_path(fs, "/", &directory);
- struct fat_dir_struct* dd = fat_open_dir(fs, &directory);
- if(!dd)
- {
+ struct fat_dir_struct* dd = fat_open_dir(fs, &directory);
+ if(!dd)
+ {
#if DEBUG
- uart_puts_p(PSTR("opening root directory failed\n"));
+ uart_puts_p(PSTR("opening root directory failed\n"));
#endif
- continue;
- }
-
- /* print some card information as a boot message */
- print_disk_info(fs);
-
- /* provide a simple shell */
- char buffer[24];
- while(1)
- {
- /* print prompt */
- uart_putc('>');
- uart_putc(' ');
-
- /* read command */
- char* command = buffer;
- if(read_line(command, sizeof(buffer)) < 1)
- continue;
-
- /* execute command */
- if(strcmp_P(command, PSTR("init")) == 0)
- {
- break;
- }
- else if(strncmp_P(command, PSTR("cd "), 3) == 0)
- {
- command += 3;
- if(command[0] == '\0')
- continue;
-
- /* change directory */
- struct fat_dir_entry_struct subdir_entry;
- if(find_file_in_dir(fs, dd, command, &subdir_entry))
- {
- struct fat_dir_struct* dd_new = fat_open_dir(fs, &subdir_entry);
- if(dd_new)
- {
- fat_close_dir(dd);
- dd = dd_new;
- continue;
- }
- }
-
- uart_puts_p(PSTR("directory not found: "));
- uart_puts(command);
- uart_putc('\n');
- }
- else if(strcmp_P(command, PSTR("ls")) == 0)
- {
- /* print directory listing */
- struct fat_dir_entry_struct dir_entry;
- while(fat_read_dir(dd, &dir_entry))
- {
- uint8_t spaces = sizeof(dir_entry.long_name) - strlen(dir_entry.long_name) + 4;
-
- uart_puts(dir_entry.long_name);
- uart_putc(dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' ');
- while(spaces--)
- uart_putc(' ');
- uart_putdw_dec(dir_entry.file_size);
- uart_putc('\n');
- }
- }
- else if(strncmp_P(command, PSTR("cat "), 4) == 0)
- {
- command += 4;
- if(command[0] == '\0')
- continue;
-
- /* search file in current directory and open it */
- struct fat_file_struct* fd = open_file_in_dir(fs, dd, command);
- if(!fd)
- {
- uart_puts_p(PSTR("error opening "));
- uart_puts(command);
- uart_putc('\n');
- continue;
- }
-
- /* print file contents */
- uint8_t buffer[8];
- uint32_t offset = 0;
- intptr_t count;
- while((count = fat_read_file(fd, buffer, sizeof(buffer))) > 0)
- {
- uart_putdw_hex(offset);
- uart_putc(':');
- for(intptr_t i = 0; i < count; ++i)
- {
- uart_putc(' ');
- uart_putc_hex(buffer[i]);
- }
- uart_putc('\n');
- offset += 8;
- }
-
- fat_close_file(fd);
- }
- else if(strcmp_P(command, PSTR("disk")) == 0)
- {
- if(!print_disk_info(fs))
- uart_puts_p(PSTR("error reading disk info\n"));
- }
+ continue;
+ }
+
+ /* print some card information as a boot message */
+ print_disk_info(fs);
+
+ /* provide a simple shell */
+ char buffer[24];
+ while(1)
+ {
+ /* print prompt */
+ uart_putc('>');
+ uart_putc(' ');
+
+ /* read command */
+ char* command = buffer;
+ if(read_line(command, sizeof(buffer)) < 1)
+ continue;
+
+ /* execute command */
+ if(strcmp_P(command, PSTR("init")) == 0)
+ {
+ break;
+ }
+ else if(strncmp_P(command, PSTR("cd "), 3) == 0)
+ {
+ command += 3;
+ if(command[0] == '\0')
+ continue;
+
+ /* change directory */
+ struct fat_dir_entry_struct subdir_entry;
+ if(find_file_in_dir(fs, dd, command, &subdir_entry))
+ {
+ struct fat_dir_struct* dd_new = fat_open_dir(fs, &subdir_entry);
+ if(dd_new)
+ {
+ fat_close_dir(dd);
+ dd = dd_new;
+ continue;
+ }
+ }
+
+ uart_puts_p(PSTR("directory not found: "));
+ uart_puts(command);
+ uart_putc('\n');
+ }
+ else if(strcmp_P(command, PSTR("ls")) == 0)
+ {
+ /* print directory listing */
+ struct fat_dir_entry_struct dir_entry;
+ while(fat_read_dir(dd, &dir_entry))
+ {
+ uint8_t spaces = sizeof(dir_entry.long_name) - strlen(dir_entry.long_name) + 4;
+
+ uart_puts(dir_entry.long_name);
+ uart_putc(dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' ');
+ while(spaces--)
+ uart_putc(' ');
+ uart_putdw_dec(dir_entry.file_size);
+ uart_putc('\n');
+ }
+ }
+ else if(strncmp_P(command, PSTR("cat "), 4) == 0)
+ {
+ command += 4;
+ if(command[0] == '\0')
+ continue;
+
+ /* search file in current directory and open it */
+ struct fat_file_struct* fd = open_file_in_dir(fs, dd, command);
+ if(!fd)
+ {
+ uart_puts_p(PSTR("error opening "));
+ uart_puts(command);
+ uart_putc('\n');
+ continue;
+ }
+
+ /* print file contents */
+ uint8_t buffer[8];
+ uint32_t offset = 0;
+ intptr_t count;
+ while((count = fat_read_file(fd, buffer, sizeof(buffer))) > 0)
+ {
+ uart_putdw_hex(offset);
+ uart_putc(':');
+ for(intptr_t i = 0; i < count; ++i)
+ {
+ uart_putc(' ');
+ uart_putc_hex(buffer[i]);
+ }
+ uart_putc('\n');
+ offset += 8;
+ }
+
+ fat_close_file(fd);
+ }
+ else if(strcmp_P(command, PSTR("disk")) == 0)
+ {
+ if(!print_disk_info(fs))
+ uart_puts_p(PSTR("error reading disk info\n"));
+ }
#if FAT_WRITE_SUPPORT
- else if(strncmp_P(command, PSTR("rm "), 3) == 0)
- {
- command += 3;
- if(command[0] == '\0')
- continue;
-
- struct fat_dir_entry_struct file_entry;
- if(find_file_in_dir(fs, dd, command, &file_entry))
- {
- if(fat_delete_file(fs, &file_entry))
- continue;
- }
-
- uart_puts_p(PSTR("error deleting file: "));
- uart_puts(command);
- uart_putc('\n');
- }
- else if(strncmp_P(command, PSTR("touch "), 6) == 0)
- {
- command += 6;
- if(command[0] == '\0')
- continue;
-
- struct fat_dir_entry_struct file_entry;
- if(!fat_create_file(dd, command, &file_entry))
- {
- uart_puts_p(PSTR("error creating file: "));
- uart_puts(command);
- uart_putc('\n');
- }
- }
- else if(strncmp_P(command, PSTR("mv "), 3) == 0)
- {
- command += 3;
- if(command[0] == '\0')
- continue;
-
- char* target = command;
- while(*target != ' ' && *target != '\0')
- ++target;
-
- if(*target == ' ')
- *target++ = '\0';
- else
- continue;
-
- struct fat_dir_entry_struct file_entry;
- if(find_file_in_dir(fs, dd, command, &file_entry))
- {
- if(fat_move_file(fs, &file_entry, dd, target))
- continue;
- }
-
- uart_puts_p(PSTR("error moving file: "));
- uart_puts(command);
- uart_putc('\n');
- }
- else if(strncmp_P(command, PSTR("write "), 6) == 0)
- {
- command += 6;
- if(command[0] == '\0')
- continue;
-
- char* offset_value = command;
- while(*offset_value != ' ' && *offset_value != '\0')
- ++offset_value;
-
- if(*offset_value == ' ')
- *offset_value++ = '\0';
- else
- continue;
-
- /* search file in current directory and open it */
- struct fat_file_struct* fd = open_file_in_dir(fs, dd, command);
- if(!fd)
- {
- uart_puts_p(PSTR("error opening "));
- uart_puts(command);
- uart_putc('\n');
- continue;
- }
-
- int32_t offset = strtolong(offset_value);
- if(!fat_seek_file(fd, &offset, FAT_SEEK_SET))
- {
- uart_puts_p(PSTR("error seeking on "));
- uart_puts(command);
- uart_putc('\n');
-
- fat_close_file(fd);
- continue;
- }
-
- /* read text from the shell and write it to the file */
- uint8_t data_len;
- while(1)
- {
- /* give a different prompt */
- uart_putc('<');
- uart_putc(' ');
-
- /* read one line of text */
- data_len = read_line(buffer, sizeof(buffer));
- if(!data_len)
- break;
-
- /* write text to file */
- if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len)
- {
- uart_puts_p(PSTR("error writing to file\n"));
- break;
- }
- }
-
- fat_close_file(fd);
- }
- else if(strncmp_P(command, PSTR("mkdir "), 6) == 0)
- {
- command += 6;
- if(command[0] == '\0')
- continue;
-
- struct fat_dir_entry_struct dir_entry;
- if(!fat_create_dir(dd, command, &dir_entry))
- {
- uart_puts_p(PSTR("error creating directory: "));
- uart_puts(command);
- uart_putc('\n');
- }
- }
+ else if(strncmp_P(command, PSTR("rm "), 3) == 0)
+ {
+ command += 3;
+ if(command[0] == '\0')
+ continue;
+
+ struct fat_dir_entry_struct file_entry;
+ if(find_file_in_dir(fs, dd, command, &file_entry))
+ {
+ if(fat_delete_file(fs, &file_entry))
+ continue;
+ }
+
+ uart_puts_p(PSTR("error deleting file: "));
+ uart_puts(command);
+ uart_putc('\n');
+ }
+ else if(strncmp_P(command, PSTR("touch "), 6) == 0)
+ {
+ command += 6;
+ if(command[0] == '\0')
+ continue;
+
+ struct fat_dir_entry_struct file_entry;
+ if(!fat_create_file(dd, command, &file_entry))
+ {
+ uart_puts_p(PSTR("error creating file: "));
+ uart_puts(command);
+ uart_putc('\n');
+ }
+ }
+ else if(strncmp_P(command, PSTR("mv "), 3) == 0)
+ {
+ command += 3;
+ if(command[0] == '\0')
+ continue;
+
+ char* target = command;
+ while(*target != ' ' && *target != '\0')
+ ++target;
+
+ if(*target == ' ')
+ *target++ = '\0';
+ else
+ continue;
+
+ struct fat_dir_entry_struct file_entry;
+ if(find_file_in_dir(fs, dd, command, &file_entry))
+ {
+ if(fat_move_file(fs, &file_entry, dd, target))
+ continue;
+ }
+
+ uart_puts_p(PSTR("error moving file: "));
+ uart_puts(command);
+ uart_putc('\n');
+ }
+ else if(strncmp_P(command, PSTR("write "), 6) == 0)
+ {
+ command += 6;
+ if(command[0] == '\0')
+ continue;
+
+ char* offset_value = command;
+ while(*offset_value != ' ' && *offset_value != '\0')
+ ++offset_value;
+
+ if(*offset_value == ' ')
+ *offset_value++ = '\0';
+ else
+ continue;
+
+ /* search file in current directory and open it */
+ struct fat_file_struct* fd = open_file_in_dir(fs, dd, command);
+ if(!fd)
+ {
+ uart_puts_p(PSTR("error opening "));
+ uart_puts(command);
+ uart_putc('\n');
+ continue;
+ }
+
+ int32_t offset = strtolong(offset_value);
+ if(!fat_seek_file(fd, &offset, FAT_SEEK_SET))
+ {
+ uart_puts_p(PSTR("error seeking on "));
+ uart_puts(command);
+ uart_putc('\n');
+
+ fat_close_file(fd);
+ continue;
+ }
+
+ /* read text from the shell and write it to the file */
+ uint8_t data_len;
+ while(1)
+ {
+ /* give a different prompt */
+ uart_putc('<');
+ uart_putc(' ');
+
+ /* read one line of text */
+ data_len = read_line(buffer, sizeof(buffer));
+ if(!data_len)
+ break;
+
+ /* write text to file */
+ if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len)
+ {
+ uart_puts_p(PSTR("error writing to file\n"));
+ break;
+ }
+ }
+
+ fat_close_file(fd);
+ }
+ else if(strncmp_P(command, PSTR("mkdir "), 6) == 0)
+ {
+ command += 6;
+ if(command[0] == '\0')
+ continue;
+
+ struct fat_dir_entry_struct dir_entry;
+ if(!fat_create_dir(dd, command, &dir_entry))
+ {
+ uart_puts_p(PSTR("error creating directory: "));
+ uart_puts(command);
+ uart_putc('\n');
+ }
+ }
#endif
#if SD_RAW_WRITE_BUFFERING
- else if(strcmp_P(command, PSTR("sync")) == 0)
- {
- if(!sd_raw_sync())
- uart_puts_p(PSTR("error syncing disk\n"));
- }
+ else if(strcmp_P(command, PSTR("sync")) == 0)
+ {
+ if(!sd_raw_sync())
+ uart_puts_p(PSTR("error syncing disk\n"));
+ }
#endif
- else
- {
- uart_puts_p(PSTR("unknown command: "));
- uart_puts(command);
- uart_putc('\n');
- }
- }
-
- /* close directory */
- fat_close_dir(dd);
-
- /* close file system */
- fat_close(fs);
-
- /* close partition */
- partition_close(partition);
+ else
+ {
+ uart_puts_p(PSTR("unknown command: "));
+ uart_puts(command);
+ uart_putc('\n');
+ }
+ }
+
+ /* close directory */
+ fat_close_dir(dd);
+
+ /* close file system */
+ fat_close(fs);
+
+ /* close partition */
+ partition_close(partition);
}
-
+
return 0;
}
@@ -558,35 +558,35 @@ uint8_t read_line(char* buffer, uint8_t buffer_length)
uint8_t read_length = 0;
while(read_length < buffer_length - 1)
{
- uint8_t c = uart_getc();
-
- if(c == 0x08 || c == 0x7f)
- {
- if(read_length < 1)
- continue;
-
- --read_length;
- buffer[read_length] = '\0';
-
- uart_putc(0x08);
- uart_putc(' ');
- uart_putc(0x08);
-
- continue;
- }
-
- uart_putc(c);
-
- if(c == '\n')
- {
- buffer[read_length] = '\0';
- break;
- }
- else
- {
- buffer[read_length] = c;
- ++read_length;
- }
+ uint8_t c = uart_getc();
+
+ if(c == 0x08 || c == 0x7f)
+ {
+ if(read_length < 1)
+ continue;
+
+ --read_length;
+ buffer[read_length] = '\0';
+
+ uart_putc(0x08);
+ uart_putc(' ');
+ uart_putc(0x08);
+
+ continue;
+ }
+
+ uart_putc(c);
+
+ if(c == '\n')
+ {
+ buffer[read_length] = '\0';
+ break;
+ }
+ else
+ {
+ buffer[read_length] = c;
+ ++read_length;
+ }
}
return read_length;
@@ -596,7 +596,7 @@ uint32_t strtolong(const char* str)
{
uint32_t l = 0;
while(*str >= '0' && *str <= '9')
- l = l * 10 + (*str++ - '0');
+ l = l * 10 + (*str++ - '0');
return l;
}
@@ -605,11 +605,11 @@ uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, co
{
while(fat_read_dir(dd, dir_entry))
{
- if(strcmp(dir_entry->long_name, name) == 0)
- {
- fat_reset_dir(dd);
- return 1;
- }
+ if(strcmp(dir_entry->long_name, name) == 0)
+ {
+ fat_reset_dir(dd);
+ return 1;
+ }
}
return 0;
@@ -619,7 +619,7 @@ struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_di
{
struct fat_dir_entry_struct file_entry;
if(!find_file_in_dir(fs, dd, name, &file_entry))
- return 0;
+ return 0;
return fat_open_file(fs, &file_entry);
}
@@ -627,11 +627,11 @@ struct fat_file_struct* open_file_in_dir(struct fat_fs_struct* fs, struct fat_di
uint8_t print_disk_info(const struct fat_fs_struct* fs)
{
if(!fs)
- return 0;
+ return 0;
struct sd_raw_info disk_info;
if(!sd_raw_get_info(&disk_info))
- return 0;
+ return 0;
uart_puts_p(PSTR("manuf: 0x")); uart_putc_hex(disk_info.manufacturer); uart_putc('\n');
uart_puts_p(PSTR("oem: ")); uart_puts((char*) disk_info.oem); uart_putc('\n');
@@ -639,14 +639,14 @@ uint8_t print_disk_info(const struct fat_fs_struct* fs)
uart_puts_p(PSTR("rev: ")); uart_putc_hex(disk_info.revision); uart_putc('\n');
uart_puts_p(PSTR("serial: 0x")); uart_putdw_hex(disk_info.serial); uart_putc('\n');
uart_puts_p(PSTR("date: ")); uart_putw_dec(disk_info.manufacturing_month); uart_putc('/');
- uart_putw_dec(disk_info.manufacturing_year); uart_putc('\n');
+ uart_putw_dec(disk_info.manufacturing_year); uart_putc('\n');
uart_puts_p(PSTR("size: ")); uart_putdw_dec(disk_info.capacity / 1024 / 1024); uart_puts_p(PSTR("MB\n"));
uart_puts_p(PSTR("copy: ")); uart_putw_dec(disk_info.flag_copy); uart_putc('\n');
uart_puts_p(PSTR("wr.pr.: ")); uart_putw_dec(disk_info.flag_write_protect_temp); uart_putc('/');
- uart_putw_dec(disk_info.flag_write_protect); uart_putc('\n');
+ uart_putw_dec(disk_info.flag_write_protect); uart_putc('\n');
uart_puts_p(PSTR("format: ")); uart_putw_dec(disk_info.format); uart_putc('\n');
uart_puts_p(PSTR("free: ")); uart_putdw_dec(fat_get_fs_free(fs)); uart_putc('/');
- uart_putdw_dec(fat_get_fs_size(fs)); uart_putc('\n');
+ uart_putdw_dec(fat_get_fs_size(fs)); uart_putc('\n');
return 1;
}
diff --git a/sd_raw.c b/sd_raw.c
index 5e80ed3..8c830a3 100644
--- a/sd_raw.c
+++ b/sd_raw.c
@@ -185,26 +185,26 @@ uint8_t sd_raw_init()
/* initialize SPI with lowest frequency; max. 400kHz during identification mode of card */
SPCR = (0 << SPIE) | /* SPI Interrupt Enable */
- (1 << SPE) | /* SPI Enable */
- (0 << DORD) | /* Data Order: MSB first */
- (1 << MSTR) | /* Master mode */
- (0 << CPOL) | /* Clock Polarity: SCK low when idle */
- (0 << CPHA) | /* Clock Phase: sample on rising SCK edge */
- (1 << SPR1) | /* Clock Frequency: f_OSC / 128 */
- (1 << SPR0);
+ (1 << SPE) | /* SPI Enable */
+ (0 << DORD) | /* Data Order: MSB first */
+ (1 << MSTR) | /* Master mode */
+ (0 << CPOL) | /* Clock Polarity: SCK low when idle */
+ (0 << CPHA) | /* Clock Phase: sample on rising SCK edge */
+ (1 << SPR1) | /* Clock Frequency: f_OSC / 128 */
+ (1 << SPR0);
SPSR &= ~(1 << SPI2X); /* No doubled clock frequency */
/* initialization procedure */
sd_raw_card_type = 0;
-
+
if(!sd_raw_available())
- return 0;
+ return 0;
/* card needs 74 cycles minimum to start up */
for(uint8_t i = 0; i < 10; ++i)
{
- /* wait 8 clock cycles */
- sd_raw_rec_byte();
+ /* wait 8 clock cycles */
+ sd_raw_rec_byte();
}
/* address card */
@@ -214,15 +214,15 @@ uint8_t sd_raw_init()
uint8_t response;
for(uint16_t i = 0; ; ++i)
{
- response = sd_raw_send_command(CMD_GO_IDLE_STATE, 0);
- if(response == (1 << R1_IDLE_STATE))
- break;
-
- if(i == 0x1ff)
- {
- unselect_card();
- return 0;
- }
+ response = sd_raw_send_command(CMD_GO_IDLE_STATE, 0);
+ if(response == (1 << R1_IDLE_STATE))
+ break;
+
+ if(i == 0x1ff)
+ {
+ unselect_card();
+ return 0;
+ }
}
#if SD_RAW_SDHC
@@ -230,84 +230,84 @@ uint8_t sd_raw_init()
response = sd_raw_send_command(CMD_SEND_IF_COND, 0x100 /* 2.7V - 3.6V */ | 0xaa /* test pattern */);
if((response & (1 << R1_ILL_COMMAND)) == 0)
{
- sd_raw_rec_byte();
- sd_raw_rec_byte();
- if((sd_raw_rec_byte() & 0x01) == 0)
- return 0; /* card operation voltage range doesn't match */
- if(sd_raw_rec_byte() != 0xaa)
- return 0; /* wrong test pattern */
-
- /* card conforms to SD 2 card specification */
- sd_raw_card_type |= (1 << SD_RAW_SPEC_2);
+ sd_raw_rec_byte();
+ sd_raw_rec_byte();
+ if((sd_raw_rec_byte() & 0x01) == 0)
+ return 0; /* card operation voltage range doesn't match */
+ if(sd_raw_rec_byte() != 0xaa)
+ return 0; /* wrong test pattern */
+
+ /* card conforms to SD 2 card specification */
+ sd_raw_card_type |= (1 << SD_RAW_SPEC_2);
}
else
#endif
{
- /* determine SD/MMC card type */
- sd_raw_send_command(CMD_APP, 0);
- response = sd_raw_send_command(CMD_SD_SEND_OP_COND, 0);
- if((response & (1 << R1_ILL_COMMAND)) == 0)
- {
- /* card conforms to SD 1 card specification */
- sd_raw_card_type |= (1 << SD_RAW_SPEC_1);
- }
- else
- {
- /* MMC card */
- }
+ /* determine SD/MMC card type */
+ sd_raw_send_command(CMD_APP, 0);
+ response = sd_raw_send_command(CMD_SD_SEND_OP_COND, 0);
+ if((response & (1 << R1_ILL_COMMAND)) == 0)
+ {
+ /* card conforms to SD 1 card specification */
+ sd_raw_card_type |= (1 << SD_RAW_SPEC_1);
+ }
+ else
+ {
+ /* MMC card */
+ }
}
/* wait for card to get ready */
for(uint16_t i = 0; ; ++i)
{
- if(sd_raw_card_type & ((1 << SD_RAW_SPEC_1) | (1 << SD_RAW_SPEC_2)))
- {
- uint32_t arg = 0;
+ if(sd_raw_card_type & ((1 << SD_RAW_SPEC_1) | (1 << SD_RAW_SPEC_2)))
+ {
+ uint32_t arg = 0;
#if SD_RAW_SDHC
- if(sd_raw_card_type & (1 << SD_RAW_SPEC_2))
- arg = 0x40000000;
+ if(sd_raw_card_type & (1 << SD_RAW_SPEC_2))
+ arg = 0x40000000;
#endif
- sd_raw_send_command(CMD_APP, 0);
- response = sd_raw_send_command(CMD_SD_SEND_OP_COND, arg);
- }
- else
- {
- response = sd_raw_send_command(CMD_SEND_OP_COND, 0);
- }
-
- if((response & (1 << R1_IDLE_STATE)) == 0)
- break;
-
- if(i == 0x7fff)
- {
- unselect_card();
- return 0;
- }
+ sd_raw_send_command(CMD_APP, 0);
+ response = sd_raw_send_command(CMD_SD_SEND_OP_COND, arg);
+ }
+ else
+ {
+ response = sd_raw_send_command(CMD_SEND_OP_COND, 0);
+ }
+
+ if((response & (1 << R1_IDLE_STATE)) == 0)
+ break;
+
+ if(i == 0x7fff)
+ {
+ unselect_card();
+ return 0;
+ }
}
#if SD_RAW_SDHC
if(sd_raw_card_type & (1 << SD_RAW_SPEC_2))
{
- if(sd_raw_send_command(CMD_READ_OCR, 0))
- {
- unselect_card();
- return 0;
- }
-
- if(sd_raw_rec_byte() & 0x40)
- sd_raw_card_type |= (1 << SD_RAW_SPEC_SDHC);
-
- sd_raw_rec_byte();
- sd_raw_rec_byte();
- sd_raw_rec_byte();
+ if(sd_raw_send_command(CMD_READ_OCR, 0))
+ {
+ unselect_card();
+ return 0;
+ }
+
+ if(sd_raw_rec_byte() & 0x40)
+ sd_raw_card_type |= (1 << SD_RAW_SPEC_SDHC);
+
+ sd_raw_rec_byte();
+ sd_raw_rec_byte();
+ sd_raw_rec_byte();
}
#endif
/* set block size to 512 bytes */
if(sd_raw_send_command(CMD_SET_BLOCKLEN, 512))
{
- unselect_card();
- return 0;
+ unselect_card();
+ return 0;
}
/* deaddress card */
@@ -324,7 +324,7 @@ uint8_t sd_raw_init()
raw_block_written = 1;
#endif
if(!sd_raw_read(0, raw_block, sizeof(raw_block)))
- return 0;
+ return 0;
#endif
return 1;
@@ -407,23 +407,23 @@ uint8_t sd_raw_send_command(uint8_t command, uint32_t arg)
sd_raw_send_byte((arg >> 0) & 0xff);
switch(command)
{
- case CMD_GO_IDLE_STATE:
- sd_raw_send_byte(0x95);
- break;
- case CMD_SEND_IF_COND:
- sd_raw_send_byte(0x87);
- break;
- default:
- sd_raw_send_byte(0xff);
- break;
+ case CMD_GO_IDLE_STATE:
+ sd_raw_send_byte(0x95);
+ break;
+ case CMD_SEND_IF_COND:
+ sd_raw_send_byte(0x87);
+ break;
+ default:
+ sd_raw_send_byte(0xff);
+ break;
}
-
+
/* receive response */
for(uint8_t i = 0; i < 10; ++i)
{
- response = sd_raw_rec_byte();
- if(response != 0xff)
- break;
+ response = sd_raw_rec_byte();
+ if(response != 0xff)
+ break;
}
return response;
@@ -446,81 +446,81 @@ uint8_t sd_raw_read(offset_t offset, uint8_t* buffer, uintptr_t length)
uint16_t read_length;
while(length > 0)
{
- /* determine byte count to read at once */
- block_offset = offset & 0x01ff;
- block_address = offset - block_offset;
- read_length = 512 - block_offset; /* read up to block border */
- if(read_length > length)
- read_length = length;
-
+ /* determine byte count to read at once */
+ block_offset = offset & 0x01ff;
+ block_address = offset - block_offset;
+ read_length = 512 - block_offset; /* read up to block border */
+ if(read_length > length)
+ read_length = length;
+
#if !SD_RAW_SAVE_RAM
- /* check if the requested data is cached */
- if(block_address != raw_block_address)
+ /* check if the requested data is cached */
+ if(block_address != raw_block_address)
#endif
- {
+ {
#if SD_RAW_WRITE_BUFFERING
- if(!sd_raw_sync())
- return 0;
+ if(!sd_raw_sync())
+ return 0;
#endif
- /* address card */
- select_card();
+ /* address card */
+ select_card();
- /* send single block request */
+ /* send single block request */
#if SD_RAW_SDHC
- if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? block_address / 512 : block_address)))
+ if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? block_address / 512 : block_address)))
#else
- if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, block_address))
+ if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, block_address))
#endif
- {
- unselect_card();
- return 0;
- }
+ {
+ unselect_card();
+ return 0;
+ }
- /* wait for data block (start byte 0xfe) */
- while(sd_raw_rec_byte() != 0xfe);
+ /* wait for data block (start byte 0xfe) */
+ while(sd_raw_rec_byte() != 0xfe);
#if SD_RAW_SAVE_RAM
- /* read byte block */
- uint16_t read_to = block_offset + read_length;
- for(uint16_t i = 0; i < 512; ++i)
- {
- uint8_t b = sd_raw_rec_byte();
- if(i >= block_offset && i < read_to)
- *buffer++ = b;
- }
+ /* read byte block */
+ uint16_t read_to = block_offset + read_length;
+ for(uint16_t i = 0; i < 512; ++i)
+ {
+ uint8_t b = sd_raw_rec_byte();
+ if(i >= block_offset && i < read_to)
+ *buffer++ = b;
+ }
#else
- /* read byte block */
- uint8_t* cache = raw_block;
- for(uint16_t i = 0; i < 512; ++i)
- *cache++ = sd_raw_rec_byte();
- raw_block_address = block_address;
-
- memcpy(buffer, raw_block + block_offset, read_length);
- buffer += read_length;
+ /* read byte block */
+ uint8_t* cache = raw_block;
+ for(uint16_t i = 0; i < 512; ++i)
+ *cache++ = sd_raw_rec_byte();
+ raw_block_address = block_address;
+
+ memcpy(buffer, raw_block + block_offset, read_length);
+ buffer += read_length;
#endif
-
- /* read crc16 */
- sd_raw_rec_byte();
- sd_raw_rec_byte();
-
- /* deaddress card */
- unselect_card();
-
- /* let card some time to finish */
- sd_raw_rec_byte();
- }
+
+ /* read crc16 */
+ sd_raw_rec_byte();
+ sd_raw_rec_byte();
+
+ /* deaddress card */
+ unselect_card();
+
+ /* let card some time to finish */
+ sd_raw_rec_byte();
+ }
#if !SD_RAW_SAVE_RAM
- else
- {
- /* use cached data */
- memcpy(buffer, raw_block + block_offset, read_length);
- buffer += read_length;
- }
+ else
+ {
+ /* use cached data */
+ memcpy(buffer, raw_block + block_offset, read_length);
+ buffer += read_length;
+ }
#endif
- length -= read_length;
- offset += read_length;
+ length -= read_length;
+ offset += read_length;
}
return 1;
@@ -553,20 +553,20 @@ uint8_t sd_raw_read(offset_t offset, uint8_t* buffer, uintptr_t length)
uint8_t sd_raw_read_interval(offset_t offset, uint8_t* buffer, uintptr_t interval, uintptr_t length, sd_raw_read_interval_handler_t callback, void* p)
{
if(!buffer || interval == 0 || length < interval || !callback)
- return 0;
+ return 0;
#if !SD_RAW_SAVE_RAM
while(length >= interval)
{
- /* as reading is now buffered, we directly
- * hand over the request to sd_raw_read()
- */
- if(!sd_raw_read(offset, buffer, interval))
- return 0;
- if(!callback(buffer, offset, p))
- break;
- offset += interval;
- length -= interval;
+ /* as reading is now buffered, we directly
+ * hand over the request to sd_raw_read()
+ */
+ if(!sd_raw_read(offset, buffer, interval))
+ return 0;
+ if(!callback(buffer, offset, p))
+ break;
+ offset += interval;
+ length -= interval;
}
return 1;
@@ -580,64 +580,64 @@ uint8_t sd_raw_read_interval(offset_t offset, uint8_t* buffer, uintptr_t interva
uint8_t finished = 0;
do
{
- /* determine byte count to read at once */
- block_offset = offset & 0x01ff;
- read_length = 512 - block_offset;
-
- /* send single block request */
+ /* determine byte count to read at once */
+ block_offset = offset & 0x01ff;
+ read_length = 512 - block_offset;
+
+ /* send single block request */
#if SD_RAW_SDHC
- if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? offset / 512 : offset - block_offset)))
+ if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? offset / 512 : offset - block_offset)))
#else
- if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, offset - block_offset))
+ if(sd_raw_send_command(CMD_READ_SINGLE_BLOCK, offset - block_offset))
#endif
- {
- unselect_card();
- return 0;
- }
-
- /* wait for data block (start byte 0xfe) */
- while(sd_raw_rec_byte() != 0xfe);
-
- /* read up to the data of interest */
- for(uint16_t i = 0; i < block_offset; ++i)
- sd_raw_rec_byte();
-
- /* read interval bytes of data and execute the callback */
- do
- {
- if(read_length < interval || length < interval)
- break;
-
- buffer_cur = buffer;
- for(uint16_t i = 0; i < interval; ++i)
- *buffer_cur++ = sd_raw_rec_byte();
-
- if(!callback(buffer, offset + (512 - read_length), p))
- {
- finished = 1;
- break;
- }
-
- read_length -= interval;
- length -= interval;
-
- } while(read_length > 0 && length > 0);
-
- /* read rest of data block */
- while(read_length-- > 0)
- sd_raw_rec_byte();
-
- /* read crc16 */
- sd_raw_rec_byte();
- sd_raw_rec_byte();
-
- if(length < interval)
- break;
-
- offset = offset - block_offset + 512;
+ {
+ unselect_card();
+ return 0;
+ }
+
+ /* wait for data block (start byte 0xfe) */
+ while(sd_raw_rec_byte() != 0xfe);
+
+ /* read up to the data of interest */
+ for(uint16_t i = 0; i < block_offset; ++i)
+ sd_raw_rec_byte();
+
+ /* read interval bytes of data and execute the callback */
+ do
+ {
+ if(read_length < interval || length < interval)
+ break;
+
+ buffer_cur = buffer;
+ for(uint16_t i = 0; i < interval; ++i)
+ *buffer_cur++ = sd_raw_rec_byte();
+
+ if(!callback(buffer, offset + (512 - read_length), p))
+ {
+ finished = 1;
+ break;
+ }
+
+ read_length -= interval;
+ length -= interval;
+
+ } while(read_length > 0 && length > 0);
+
+ /* read rest of data block */
+ while(read_length-- > 0)
+ sd_raw_rec_byte();
+
+ /* read crc16 */
+ sd_raw_rec_byte();
+ sd_raw_rec_byte();
+
+ if(length < interval)
+ break;
+
+ offset = offset - block_offset + 512;
} while(!finished);
-
+
/* deaddress card */
unselect_card();
@@ -666,89 +666,89 @@ uint8_t sd_raw_read_interval(offset_t offset, uint8_t* buffer, uintptr_t interva
uint8_t sd_raw_write(offset_t offset, const uint8_t* buffer, uintptr_t length)
{
if(sd_raw_locked())
- return 0;
+ return 0;
offset_t block_address;
uint16_t block_offset;
uint16_t write_length;
while(length > 0)
{
- /* determine byte count to write at once */
- block_offset = offset & 0x01ff;
- block_address = offset - block_offset;
- write_length = 512 - block_offset; /* write up to block border */
- if(write_length > length)
- write_length = length;
-
- /* Merge the data to write with the content of the block.
- * Use the cached block if available.
- */
- if(block_address != raw_block_address)
- {
+ /* determine byte count to write at once */
+ block_offset = offset & 0x01ff;
+ block_address = offset - block_offset;
+ write_length = 512 - block_offset; /* write up to block border */
+ if(write_length > length)
+ write_length = length;
+
+ /* Merge the data to write with the content of the block.
+ * Use the cached block if available.
+ */
+ if(block_address != raw_block_address)
+ {
#if SD_RAW_WRITE_BUFFERING
- if(!sd_raw_sync())
- return 0;
+ if(!sd_raw_sync())
+ return 0;
#endif
- if(block_offset || write_length < 512)
- {
- if(!sd_raw_read(block_address, raw_block, sizeof(raw_block)))
- return 0;
- }
- raw_block_address = block_address;
- }
+ if(block_offset || write_length < 512)
+ {
+ if(!sd_raw_read(block_address, raw_block, sizeof(raw_block)))
+ return 0;
+ }
+ raw_block_address = block_address;
+ }
- if(buffer != raw_block)
- {
- memcpy(raw_block + block_offset, buffer, write_length);
+ if(buffer != raw_block)
+ {
+ memcpy(raw_block + block_offset, buffer, write_length);
#if SD_RAW_WRITE_BUFFERING
- raw_block_written = 0;
+ raw_block_written = 0;
- if(length == write_length)
- return 1;
+ if(length == write_length)
+ return 1;
#endif
- }
+ }
- /* address card */
- select_card();
+ /* address card */
+ select_card();
- /* send single block request */
+ /* send single block request */
#if SD_RAW_SDHC
- if(sd_raw_send_command(CMD_WRITE_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? block_address / 512 : block_address)))
+ if(sd_raw_send_command(CMD_WRITE_SINGLE_BLOCK, (sd_raw_card_type & (1 << SD_RAW_SPEC_SDHC) ? block_address / 512 : block_address)))
#else
- if(sd_raw_send_command(CMD_WRITE_SINGLE_BLOCK, block_address))
+ if(sd_raw_send_command(CMD_WRITE_SINGLE_BLOCK, block_address))
#endif
- {
- unselect_card();
- return 0;
- }
+ {
+ unselect_card();
+ return 0;
+ }
- /* send start byte */
- sd_raw_send_byte(0xfe);
+ /* send start byte */
+ sd_raw_send_byte(0xfe);
- /* write byte block */
- uint8_t* cache = raw_block;
- for(uint16_t i = 0; i < 512; ++i)
- sd_raw_send_byte(*cache++);
+ /* write byte block */
+ uint8_t* cache = raw_block;
+ for(uint16_t i = 0; i < 512; ++i)
+ sd_raw_send_byte(*cache++);
- /* write dummy crc16 */
- sd_raw_send_byte(0xff);
- sd_raw_send_byte(0xff);
+ /* write dummy crc16 */
+ sd_raw_send_byte(0xff);
+ sd_raw_send_byte(0xff);
- /* wait while card is busy */
- while(sd_raw_rec_byte() != 0xff);
- sd_raw_rec_byte();
+ /* wait while card is busy */
+ while(sd_raw_rec_byte() != 0xff);
+ sd_raw_rec_byte();
- /* deaddress card */
- unselect_card();
+ /* deaddress card */
+ unselect_card();
- buffer += write_length;
- offset += write_length;
- length -= write_length;
+ buffer += write_length;
+ offset += write_length;
+ length -= write_length;
#if SD_RAW_WRITE_BUFFERING
- raw_block_written = 1;
+ raw_block_written = 1;
#endif
}
@@ -782,25 +782,25 @@ uint8_t sd_raw_write_interval(offset_t offset, uint8_t* buffer, uintptr_t length
#endif
if(!buffer || !callback)
- return 0;
+ return 0;
uint8_t endless = (length == 0);
while(endless || length > 0)
{
- uint16_t bytes_to_write = callback(buffer, offset, p);
- if(!bytes_to_write)
- break;
- if(!endless && bytes_to_write > length)
- return 0;
-
- /* as writing is always buffered, we directly
- * hand over the request to sd_raw_write()
- */
- if(!sd_raw_write(offset, buffer, bytes_to_write))
- return 0;
-
- offset += bytes_to_write;
- length -= bytes_to_write;
+ uint16_t bytes_to_write = callback(buffer, offset, p);
+ if(!bytes_to_write)
+ break;
+ if(!endless && bytes_to_write > length)
+ return 0;
+
+ /* as writing is always buffered, we directly
+ * hand over the request to sd_raw_write()
+ */
+ if(!sd_raw_write(offset, buffer, bytes_to_write))
+ return 0;
+
+ offset += bytes_to_write;
+ length -= bytes_to_write;
}
return 1;
@@ -824,9 +824,9 @@ uint8_t sd_raw_sync()
{
#if SD_RAW_WRITE_BUFFERING
if(raw_block_written)
- return 1;
+ return 1;
if(!sd_raw_write(raw_block_address, raw_block, sizeof(raw_block)))
- return 0;
+ return 0;
raw_block_written = 1;
#endif
return 1;
@@ -851,7 +851,7 @@ uint8_t sd_raw_sync()
uint8_t sd_raw_get_info(struct sd_raw_info* info)
{
if(!info || !sd_raw_available())
- return 0;
+ return 0;
memset(info, 0, sizeof(*info));
@@ -860,47 +860,47 @@ uint8_t sd_raw_get_info(struct sd_raw_info* info)
/* read cid register */
if(sd_raw_send_command(CMD_SEND_CID, 0))
{
- unselect_card();
- return 0;
+ unselect_card();
+ return 0;
}
while(sd_raw_rec_byte() != 0xfe);
for(uint8_t i = 0; i < 18; ++i)
{
- uint8_t b = sd_raw_rec_byte();
-
- switch(i)
- {
- case 0:
- info->manufacturer = b;
- break;
- case 1:
- case 2:
- info->oem[i - 1] = b;
- break;
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- info->product[i - 3] = b;
- break;
- case 8:
- info->revision = b;
- break;
- case 9:
- case 10:
- case 11:
- case 12:
- info->serial |= (uint32_t) b << ((12 - i) * 8);
- break;
- case 13:
- info->manufacturing_year = b << 4;
- break;
- case 14:
- info->manufacturing_year |= b >> 4;
- info->manufacturing_month = b & 0x0f;
- break;
- }
+ uint8_t b = sd_raw_rec_byte();
+
+ switch(i)
+ {
+ case 0:
+ info->manufacturer = b;
+ break;
+ case 1:
+ case 2:
+ info->oem[i - 1] = b;
+ break;
+ case 3:
+ case 4:
+ case 5:
+ case 6:
+ case 7:
+ info->product[i - 3] = b;
+ break;
+ case 8:
+ info->revision = b;
+ break;
+ case 9:
+ case 10:
+ case 11:
+ case 12:
+ info->serial |= (uint32_t) b << ((12 - i) * 8);
+ break;
+ case 13:
+ info->manufacturing_year = b << 4;
+ break;
+ case 14:
+ info->manufacturing_year |= b >> 4;
+ info->manufacturing_month = b & 0x0f;
+ break;
+ }
}
/* read csd register */
@@ -914,81 +914,81 @@ uint8_t sd_raw_get_info(struct sd_raw_info* info)
uint8_t csd_structure = 0;
if(sd_raw_send_command(CMD_SEND_CSD, 0))
{
- unselect_card();
- return 0;
+ unselect_card();
+ return 0;
}
while(sd_raw_rec_byte() != 0xfe);
for(uint8_t i = 0; i < 18; ++i)
{
- uint8_t b = sd_raw_rec_byte();
-
- if(i == 0)
- {
- csd_structure = b >> 6;
- }
- else if(i == 14)
- {
- if(b & 0x40)
- info->flag_copy = 1;
- if(b & 0x20)
- info->flag_write_protect = 1;
- if(b & 0x10)
- info->flag_write_protect_temp = 1;
- info->format = (b & 0x0c) >> 2;
- }
- else
- {
+ uint8_t b = sd_raw_rec_byte();
+
+ if(i == 0)
+ {
+ csd_structure = b >> 6;
+ }
+ else if(i == 14)
+ {
+ if(b & 0x40)
+ info->flag_copy = 1;
+ if(b & 0x20)
+ info->flag_write_protect = 1;
+ if(b & 0x10)
+ info->flag_write_protect_temp = 1;
+ info->format = (b & 0x0c) >> 2;
+ }
+ else
+ {
#if SD_RAW_SDHC
- if(csd_structure == 0x01)
- {
- switch(i)
- {
- case 7:
- b &= 0x3f;
- case 8:
- case 9:
- csd_c_size <<= 8;
- csd_c_size |= b;
- break;
- }
- if(i == 9)
- {
- ++csd_c_size;
- info->capacity = (offset_t) csd_c_size * 512 * 1024;
- }
- }
- else if(csd_structure == 0x00)
+ if(csd_structure == 0x01)
+ {
+ switch(i)
+ {
+ case 7:
+ b &= 0x3f;
+ case 8:
+ case 9:
+ csd_c_size <<= 8;
+ csd_c_size |= b;
+ break;
+ }
+ if(i == 9)
+ {
+ ++csd_c_size;
+ info->capacity = (offset_t) csd_c_size * 512 * 1024;
+ }
+ }
+ else if(csd_structure == 0x00)
#endif
- {
- switch(i)
- {
- case 5:
- csd_read_bl_len = b & 0x0f;
- break;
- case 6:
- csd_c_size = b & 0x03;
- csd_c_size <<= 8;
- break;
- case 7:
- csd_c_size |= b;
- csd_c_size <<= 2;
- break;
- case 8:
- csd_c_size |= b >> 6;
- ++csd_c_size;
- break;
- case 9:
- csd_c_size_mult = b & 0x03;
- csd_c_size_mult <<= 1;
- break;
- case 10:
- csd_c_size_mult |= b >> 7;
-
- info->capacity = (uint32_t) csd_c_size << (csd_c_size_mult + csd_read_bl_len + 2);
- break;
- }
- }
- }
+ {
+ switch(i)
+ {
+ case 5:
+ csd_read_bl_len = b & 0x0f;
+ break;
+ case 6:
+ csd_c_size = b & 0x03;
+ csd_c_size <<= 8;
+ break;
+ case 7:
+ csd_c_size |= b;
+ csd_c_size <<= 2;
+ break;
+ case 8:
+ csd_c_size |= b >> 6;
+ ++csd_c_size;
+ break;
+ case 9:
+ csd_c_size_mult = b & 0x03;
+ csd_c_size_mult <<= 1;
+ break;
+ case 10:
+ csd_c_size_mult |= b >> 7;
+
+ info->capacity = (uint32_t) csd_c_size << (csd_c_size_mult + csd_read_bl_len + 2);
+ break;
+ }
+ }
+ }
}
unselect_card();
diff --git a/sd_raw_config.h b/sd_raw_config.h
index ca88eaa..23c4959 100644
--- a/sd_raw_config.h
+++ b/sd_raw_config.h
@@ -49,7 +49,7 @@ extern "C"
/**
* \ingroup sd_raw_config
* Controls MMC/SD access buffering.
- *
+ *
* Set to 1 to save static RAM, but be aware that you will
* lose performance.
*
diff --git a/uart.c b/uart.c
index df5e1fd..3d218a5 100644
--- a/uart.c
+++ b/uart.c
@@ -32,7 +32,7 @@
#define RXCIE RXCIE0
#define UCSRC UCSR0C
-#define URSEL
+#define URSEL
#define UCSZ0 UCSZ00
#define UCSZ1 UCSZ01
#define UCSRC_SELECT 0
@@ -80,7 +80,7 @@ void uart_init()
void uart_putc(uint8_t c)
{
if(c == '\n')
- uart_putc('\r');
+ uart_putc('\r');
/* wait until transmit buffer is empty */
while(!(UCSRA & (1 << UDRE)));
@@ -93,15 +93,15 @@ void uart_putc_hex(uint8_t b)
{
/* upper nibble */
if((b >> 4) < 0x0a)
- uart_putc((b >> 4) + '0');
+ uart_putc((b >> 4) + '0');
else
- uart_putc((b >> 4) - 0x0a + 'a');
+ uart_putc((b >> 4) - 0x0a + 'a');
/* lower nibble */
if((b & 0x0f) < 0x0a)
- uart_putc((b & 0x0f) + '0');
+ uart_putc((b & 0x0f) + '0');
else
- uart_putc((b & 0x0f) - 0x0a + 'a');
+ uart_putc((b & 0x0f) - 0x0a + 'a');
}
void uart_putw_hex(uint16_t w)
@@ -123,15 +123,15 @@ void uart_putw_dec(uint16_t w)
while(num > 0)
{
- uint8_t b = w / num;
- if(b > 0 || started || num == 1)
- {
- uart_putc('0' + b);
- started = 1;
- }
- w -= b * num;
-
- num /= 10;
+ uint8_t b = w / num;
+ if(b > 0 || started || num == 1)
+ {
+ uart_putc('0' + b);
+ started = 1;
+ }
+ w -= b * num;
+
+ num /= 10;
}
}
@@ -142,33 +142,33 @@ void uart_putdw_dec(uint32_t dw)
while(num > 0)
{
- uint8_t b = dw / num;
- if(b > 0 || started || num == 1)
- {
- uart_putc('0' + b);
- started = 1;
- }
- dw -= b * num;
-
- num /= 10;
+ uint8_t b = dw / num;
+ if(b > 0 || started || num == 1)
+ {
+ uart_putc('0' + b);
+ started = 1;
+ }
+ dw -= b * num;
+
+ num /= 10;
}
}
void uart_puts(const char* str)
{
while(*str)
- uart_putc(*str++);
+ uart_putc(*str++);
}
void uart_puts_p(PGM_P str)
{
while(1)
{
- uint8_t b = pgm_read_byte_near(str++);
- if(!b)
- break;
+ uint8_t b = pgm_read_byte_near(str++);
+ if(!b)
+ break;
- uart_putc(b);
+ uart_putc(b);
}
}
@@ -180,7 +180,7 @@ uint8_t uart_getc()
sei();
while(!(UCSRA & (1 << RXC)))
- sleep_mode();
+ sleep_mode();
SREG = sreg;
#else
@@ -189,7 +189,7 @@ uint8_t uart_getc()
uint8_t b = UDR;
if(b == '\r')
- b = '\n';
+ b = '\n';
return b;
}
diff --git a/uart_config.h b/uart_config.h
index 6a9ae2c..4822653 100644
--- a/uart_config.h
+++ b/uart_config.h
@@ -1,6 +1,6 @@
-/*
+/*
* Copyright Droids Corporation, Microb Technology, Eirbot (2005)
- *
+ *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
@@ -27,11 +27,11 @@
#define UART_CONFIG_H
/*
- * UART0 definitions
+ * UART0 definitions
*/
/* compile uart0 fonctions, undefine it to pass compilation */
-#define UART0_COMPILE
+#define UART0_COMPILE
/* enable uart0 if == 1, disable if == 0 */
#define UART0_ENABLED 1
@@ -42,9 +42,9 @@
#define UART0_BAUDRATE 57600
//#define UART0_BAUDRATE 115200
-/*
- * if you enable this, the maximum baudrate you can reach is
- * higher, but the precision is lower.
+/*
+ * if you enable this, the maximum baudrate you can reach is
+ * higher, but the precision is lower.
*/
//#define UART0_USE_DOUBLE_SPEED 0
#define UART0_USE_DOUBLE_SPEED 1