![](/media/cache/profiles/e5522f7ac7cd76f19b396595c9c25b40.50x50_q85.jpg)
SD Card Interface class. Log raw data bytes to memory addresses of your choice, or format the card and use the FAT file system to write files.
Revision 4:9a5878d316d5, committed 2010-08-27
- Comitter:
- Blaze513
- Date:
- Fri Aug 27 00:59:28 2010 +0000
- Parent:
- 3:210eb67b260c
- Child:
- 5:d85e20b6b904
- Commit message:
Changed in this revision
--- a/FATFileSystem/Interface/FATDirHandle.cpp Mon Aug 23 07:12:13 2010 +0000 +++ b/FATFileSystem/Interface/FATDirHandle.cpp Fri Aug 27 00:59:28 2010 +0000 @@ -1,50 +1,50 @@ -/* mbed Microcontroller Library - FATDirHandle - Copyright (c) 2008, sford */ - -//Modified by Thomas Hamilton, Copyright 2010 - -#include "FATDirHandle.h" - -FATDirHandle::FATDirHandle(FAT_DIR InputDirStr) -{ - DirectoryObject = InputDirStr; -} - -int FATDirHandle::closedir() -{ - delete this; - return 0; -} - -struct dirent* FATDirHandle::readdir() -{ - FILINFO FileInfo; - FRESULT Result = f_readdir(&DirectoryObject, &FileInfo); - if (Result || !FileInfo.fname[0]) - { - return NULL; - } - else - { - for (unsigned char i = 0; i < 13; i++) - { - CurrentEntry.d_name[i] = ((char*)FileInfo.fname)[i]; - } - return &CurrentEntry; - } -} - -void FATDirHandle::rewinddir() -{ - DirectoryObject.index = 0; -} - -off_t FATDirHandle::telldir() -{ - return (off_t)DirectoryObject.index; -} - -void FATDirHandle::seekdir(off_t location) -{ - DirectoryObject.index = (WORD)location; +/* mbed Microcontroller Library - FATDirHandle + Copyright (c) 2008, sford */ + +//Modified by Thomas Hamilton, Copyright 2010 + +#include "FATDirHandle.h" + +FATDirHandle::FATDirHandle(FAT_DIR InputDirStr) +{ + DirectoryObject = InputDirStr; +} + +int FATDirHandle::closedir() +{ + delete this; + return 0; +} + +struct dirent* FATDirHandle::readdir() +{ + FILINFO FileInfo; + FRESULT Result = f_readdir(&DirectoryObject, &FileInfo); + if (Result || !FileInfo.fname[0]) + { + return NULL; + } + else + { + for (unsigned char i = 0; i < 13; i++) + { + CurrentEntry.d_name[i] = ((char*)FileInfo.fname)[i]; + } + return &CurrentEntry; + } +} + +void FATDirHandle::rewinddir() +{ + DirectoryObject.index = 0; +} + +off_t FATDirHandle::telldir() +{ + return (off_t)DirectoryObject.index; +} + +void FATDirHandle::seekdir(off_t location) +{ + DirectoryObject.index = (WORD)location; } \ No newline at end of file
--- a/FATFileSystem/Interface/FATDirHandle.h Mon Aug 23 07:12:13 2010 +0000 +++ b/FATFileSystem/Interface/FATDirHandle.h Fri Aug 27 00:59:28 2010 +0000 @@ -1,29 +1,30 @@ -/* mbed Microcontroller Library - FATDirHandle - Copyright (c) 2008, sford */ - -//Modified by Thomas Hamilton, Copyright 2010 - -#ifndef MBED_FATDIRHANDLE_H -#define MBED_FATDIRHANDLE_H - -#include "ff.h" -#include "mbed.h" -#include "DirHandle.h" -#include <stdio.h> - -class FATDirHandle : public DirHandle -{ - private: - FAT_DIR DirectoryObject; - struct dirent CurrentEntry; - - public: - FATDirHandle(FAT_DIR InputDirStr); - virtual int closedir(); - virtual struct dirent* readdir(); - virtual void rewinddir(); - virtual off_t telldir(); - virtual void seekdir(off_t location); -}; - +/* mbed Microcontroller Library - FATDirHandle + Copyright (c) 2008, sford */ + +//Modified by Thomas Hamilton, Copyright 2010 + +#ifndef MBED_FATDIRHANDLE_H +#define MBED_FATDIRHANDLE_H + +#include "stdint.h" +#include "ff.h" +#include "mbed.h" +#include "DirHandle.h" +#include <stdio.h> + +class FATDirHandle : public DirHandle +{ + private: + FAT_DIR DirectoryObject; + struct dirent CurrentEntry; + + public: + FATDirHandle(FAT_DIR InputDirStr); + virtual int closedir(); + virtual struct dirent* readdir(); + virtual void rewinddir(); + virtual off_t telldir(); + virtual void seekdir(off_t location); +}; + #endif \ No newline at end of file
--- a/FATFileSystem/Interface/FATFileHandle.cpp Mon Aug 23 07:12:13 2010 +0000 +++ b/FATFileSystem/Interface/FATFileHandle.cpp Fri Aug 27 00:59:28 2010 +0000 @@ -1,89 +1,89 @@ -/* mbed Microcontroller Library - FATFileHandle - Copyright (c) 2008, sford */ - -//Modified by Thomas Hamilton, Copyright 2010 - -#include "FATFileHandle.h" - -FATFileHandle::FATFileHandle(FAT_FIL InputFilStr) -{ - FileObject = InputFilStr; -} - -ssize_t FATFileHandle::write(const void* buffer, size_t length) -{ - UINT ByteWritten; - if (f_write(&FileObject, buffer, (UINT)length, &ByteWritten)) - { - return -1; - } - else - { - return (ssize_t)ByteWritten; - } -} - -int FATFileHandle::close() -{ - if (f_close(&FileObject)) - { - return -1; - } - else - { - delete this; - return 0; - } -} - -ssize_t FATFileHandle::read(void* buffer, size_t length) -{ - UINT ByteRead; - if (f_read(&FileObject, buffer, (UINT)length, &ByteRead)) - { - return -1; - } - else - { - return (ssize_t)ByteRead; - } -} - -int FATFileHandle::isatty() -{ - return 0; -} - -off_t FATFileHandle::lseek(off_t offset, int whence) -{ - switch (whence) - { - case SEEK_CUR: offset += FileObject.fptr; break; - case SEEK_END: offset += FileObject.fsize; break; - } - if (f_lseek(&FileObject, (DWORD)offset)) - { - return -1; - } - else - { - return (off_t)FileObject.fptr; - } -} - -int FATFileHandle::fsync() -{ - if (f_sync(&FileObject)) - { - return -1; - } - else - { - return 0; - } -} - -off_t FATFileHandle::flen() -{ - return (off_t)FileObject.fsize; +/* mbed Microcontroller Library - FATFileHandle + Copyright (c) 2008, sford */ + +//Modified by Thomas Hamilton, Copyright 2010 + +#include "FATFileHandle.h" + +FATFileHandle::FATFileHandle(FAT_FIL InputFilStr) +{ + FileObject = InputFilStr; +} + +ssize_t FATFileHandle::write(const void* buffer, size_t length) +{ + UINT ByteWritten; + if (f_write(&FileObject, buffer, (UINT)length, &ByteWritten)) + { + return -1; + } + else + { + return (ssize_t)ByteWritten; + } +} + +int FATFileHandle::close() +{ + if (f_close(&FileObject)) + { + return -1; + } + else + { + delete this; + return 0; + } +} + +ssize_t FATFileHandle::read(void* buffer, size_t length) +{ + UINT ByteRead; + if (f_read(&FileObject, buffer, (UINT)length, &ByteRead)) + { + return -1; + } + else + { + return (ssize_t)ByteRead; + } +} + +int FATFileHandle::isatty() +{ + return 0; +} + +off_t FATFileHandle::lseek(off_t offset, int whence) +{ + switch (whence) + { + case SEEK_CUR: offset += FileObject.fptr; break; + case SEEK_END: offset += FileObject.fsize; break; + } + if (f_lseek(&FileObject, (DWORD)offset)) + { + return -1; + } + else + { + return (off_t)FileObject.fptr; + } +} + +int FATFileHandle::fsync() +{ + if (f_sync(&FileObject)) + { + return -1; + } + else + { + return 0; + } +} + +off_t FATFileHandle::flen() +{ + return (off_t)FileObject.fsize; } \ No newline at end of file
--- a/FATFileSystem/Interface/FATFileHandle.h Mon Aug 23 07:12:13 2010 +0000 +++ b/FATFileSystem/Interface/FATFileHandle.h Fri Aug 27 00:59:28 2010 +0000 @@ -1,30 +1,31 @@ -/* mbed Microcontroller Library - FATFileHandle - Copyright (c) 2008, sford */ - -//Modified by Thomas Hamilton, Copyright 2010 - -#ifndef MBED_FATFILEHANDLE_H -#define MBED_FATFILEHANDLE_H - -#include "ff.h" -#include "mbed.h" -#include "FileHandle.h" -#include <stdio.h> - -class FATFileHandle : public FileHandle -{ - private: - FAT_FIL FileObject; - - public: - FATFileHandle(FAT_FIL InputFilStr); - virtual ssize_t write(const void* buffer, size_t length); - virtual int close(); - virtual ssize_t read(void* buffer, size_t length); - virtual int isatty(); - virtual off_t lseek(off_t offset, int whence); - virtual int fsync(); - virtual off_t flen(); -}; - +/* mbed Microcontroller Library - FATFileHandle + Copyright (c) 2008, sford */ + +//Modified by Thomas Hamilton, Copyright 2010 + +#ifndef MBED_FATFILEHANDLE_H +#define MBED_FATFILEHANDLE_H + +#include "stdint.h" +#include "ff.h" +#include "mbed.h" +#include "FileHandle.h" +#include <stdio.h> + +class FATFileHandle : public FileHandle +{ + private: + FAT_FIL FileObject; + + public: + FATFileHandle(FAT_FIL InputFilStr); + virtual ssize_t write(const void* buffer, size_t length); + virtual int close(); + virtual ssize_t read(void* buffer, size_t length); + virtual int isatty(); + virtual off_t lseek(off_t offset, int whence); + virtual int fsync(); + virtual off_t flen(); +}; + #endif \ No newline at end of file
--- a/FATFileSystem/Interface/FATFileSystem.cpp Mon Aug 23 07:12:13 2010 +0000 +++ b/FATFileSystem/Interface/FATFileSystem.cpp Fri Aug 27 00:59:28 2010 +0000 @@ -25,7 +25,7 @@ } } } - + FATFileSystem::~FATFileSystem() { for (unsigned char i = 0; i < _DRIVES; i++) @@ -38,13 +38,13 @@ } delete this; } - + FileHandle* FATFileSystem::open(const char* filename, int flags) { FAT_FIL FileObject; char FileName[64]; BYTE ModeFlags = 0; - + sprintf(FileName, "%d:/%s", Drive, filename); switch (flags & 3) { @@ -80,11 +80,11 @@ return new FATFileHandle(FileObject); } } - + int FATFileSystem::remove(const char* filename) { char FileName[64]; - + sprintf(FileName, "%d:/%s", Drive, filename); if (f_unlink((const TCHAR*)FileName)) { @@ -98,8 +98,8 @@ int FATFileSystem::rename(const char* oldname, const char* newname) { - char OldName[64]; - + /*char OldName[64]; + sprintf(OldName, "%d:/%s", Drive, oldname); if (f_rename((const TCHAR*)OldName, (const TCHAR*)newname)) { @@ -108,14 +108,15 @@ else { return 0; - } + }*/ + return 0; } DirHandle* FATFileSystem::opendir(const char* name) { FAT_DIR DirectoryObject; char DirectoryName[64]; - + sprintf(DirectoryName, "%d:%s", Drive, name); if (f_opendir(&DirectoryObject, (const TCHAR*)DirectoryName)) { @@ -130,7 +131,7 @@ int FATFileSystem::mkdir(const char* name, mode_t mode) { char DirectoryName[64]; - + sprintf(DirectoryName, "%d:%s", Drive, name); if (f_mkdir((const TCHAR*)DirectoryName)) {
--- a/FATFileSystem/Interface/FATFileSystem.h Mon Aug 23 07:12:13 2010 +0000 +++ b/FATFileSystem/Interface/FATFileSystem.h Fri Aug 27 00:59:28 2010 +0000 @@ -1,46 +1,47 @@ -/* mbed Microcontroller Library - FATFileSystem - Copyright (c) 2008, sford */ - -//Modified by Thomas Hamilton, Copyright 2010 - -#ifndef MBED_FATFILESYSTEM_H -#define MBED_FATFILESYSTEM_H - -#include "ff.h" -#include "mbed.h" -#include "FileSystemLike.h" -#include "FATFileHandle.h" -#include "FATDirHandle.h" -#include <stdio.h> - -class FATFileSystem : public FileSystemLike -{ - private: - FATFS FileSystemObject; - unsigned char Drive; - - public: - static FATFileSystem* DriveArray[_DRIVES]; - - FATFileSystem(const char* SystemName); - virtual ~FATFileSystem(); - - virtual FileHandle* open(const char* filename, int flags); - virtual int remove(const char* filename); - virtual int rename(const char* oldname, const char* newname); - virtual DirHandle* opendir(const char* name); - virtual int mkdir(const char* name, mode_t mode); - - virtual unsigned char disk_initialize() { return 0x00; } - virtual unsigned char disk_status() { return 0x00; } - virtual unsigned char disk_read(unsigned char* buff, - unsigned long sector, unsigned char count) = 0; - virtual unsigned char disk_write(const unsigned char* buff, - unsigned long sector, unsigned char count) = 0; - virtual unsigned char disk_sync() { return 0x00; } - virtual unsigned long disk_sector_count() = 0; - virtual unsigned short disk_sector_size() { return 512; } - virtual unsigned long disk_block_size() { return 1; } -}; - +/* mbed Microcontroller Library - FATFileSystem + Copyright (c) 2008, sford */ + +//Modified by Thomas Hamilton, Copyright 2010 + +#ifndef MBED_FATFILESYSTEM_H +#define MBED_FATFILESYSTEM_H + +#include "stdint.h" +#include "ff.h" +#include "mbed.h" +#include "FileSystemLike.h" +#include "FATFileHandle.h" +#include "FATDirHandle.h" +#include <stdio.h> + +class FATFileSystem : public FileSystemLike +{ + private: + FATFS FileSystemObject; + unsigned char Drive; + + public: + static FATFileSystem* DriveArray[_DRIVES]; + + FATFileSystem(const char* SystemName); + virtual ~FATFileSystem(); + + virtual FileHandle* open(const char* filename, int flags); + virtual int remove(const char* filename); + virtual int rename(const char* oldname, const char* newname); + virtual DirHandle* opendir(const char* name); + virtual int mkdir(const char* name, mode_t mode); + + virtual unsigned char disk_initialize() { return 0x00; } + virtual unsigned char disk_status() { return 0x00; } + virtual unsigned char disk_read(unsigned char* buff, + unsigned long sector, unsigned char count) = 0; + virtual unsigned char disk_write(const unsigned char* buff, + unsigned long sector, unsigned char count) = 0; + virtual unsigned char disk_sync() { return 0x00; } + virtual unsigned long disk_sector_count() = 0; + virtual unsigned short disk_sector_size() { return 512; } + virtual unsigned long disk_block_size() { return 1; } +}; + #endif \ No newline at end of file
--- a/SDCard.cpp Mon Aug 23 07:12:13 2010 +0000 +++ b/SDCard.cpp Fri Aug 27 00:59:28 2010 +0000 @@ -13,10 +13,10 @@ ChipSelect.write(1); //chip select is active low GenerateCRCTable(1, 137, CommandCRCTable); - //generate the command crc lookup table + //generate the command crc lookup table; //(generator polynomial x^7 + x^3 + 1 converts to decimal 137) GenerateCRCTable(2, 69665, DataCRCTable); - //generate the command crc lookup table + //generate the command crc lookup table; //(generator polynomial x^16 + x^12 + x^5 + 1 converts to decimal 69665) Initialize(); //run card setup operations @@ -159,8 +159,8 @@ Command(25, 0, Workspace); Mode = 0x01; } - //if previous call was not a write operation, sync the card, start a new write - //block, and set function to write mode + //if previous call was not a write operation, sync the card, + //start a new write block, and set function to write mode if (Index == 0) { ChipSelect.write(0); @@ -193,8 +193,8 @@ ChipSelect.write(1); Index = 0; } - //if the index is at the last address, get through CRC, Data response token, and - //busy signal and reset the index + //if the index is at the last address, get through CRC, + //Data response token, and busy signal and reset the index return 0xFF; //return stuff bits; control code 1 writes a byte @@ -205,8 +205,8 @@ Command(18, 0, Workspace); Mode = 0x02; } - //if previous call was not a read operation, sync the card, start a new read block, - //and set function to read mode + //if previous call was not a read operation, sync the card, + //start a new read block, and set function to read mode if (Index == 0) { ChipSelect.write(0); @@ -240,8 +240,8 @@ Index = 0; return Workspace[0]; } - //if the index is at the last address, get through CRC and reset the index; - //control code 2 reads a byte + //if the index is at the last address, get through + //CRC and reset the index; control code 2 reads a byte default: return 0xFF; @@ -345,6 +345,7 @@ { ChipSelect.write(0); DataLines.write(0xFD); + DataLines.write(0xFF); while (!DataLines.write(0xFF)); ChipSelect.write(1); DataLines.write(0xFF); @@ -540,8 +541,7 @@ { Command(55, 0, Workspace); Command(41, 1073741824, Workspace); - //specify host supports high capacity - //cards, [40,00,00,00] = 1073741824 + //specify host supports high capacity cards, [40,00,00,00] = 1073741824 t++; } while (Workspace[0] && (t < Timeout)); //check if card is ready @@ -732,8 +732,8 @@ do { Response[0] = DataLines.write(0xFF); - //clock the card high to let it run operations, the first byte will be - //busy (all high), the response will be sent some time later + //clock the card high to let it run operations, the first byte will + //be busy (all high), the response will be sent some time later t++; } while ((Response[0] & 0x80) && (t < Timeout)); //check for a response by testing if the first bit is low @@ -851,8 +851,10 @@ for (unsigned char l = 0; l < Index[8]; l++) //increment through the encountered generator 1s { - Table[(Size * ((0x01 << i) + j)) + k] ^= (((unsigned char*)&Generator)[7-k] << (i + 1 - Index[l])); - Table[(Size * ((0x01 << i) + j)) + k] ^= (((unsigned char*)&Generator)[6-k] >> (7 - i + Index[l])); + Table[(Size * ((0x01 << i) + j)) + k] ^= + (((unsigned char*)&Generator)[7-k] << (i + 1 - Index[l])); + Table[(Size * ((0x01 << i) + j)) + k] ^= + (((unsigned char*)&Generator)[6-k] >> (7 - i + Index[l])); //xor the new bit and the new generator 1s } }
--- a/SDCard.h Mon Aug 23 07:12:13 2010 +0000 +++ b/SDCard.h Fri Aug 27 00:59:28 2010 +0000 @@ -21,7 +21,7 @@ unsigned char DataCRCTable[512]; //CRC CCITT lookup table unsigned char OCR[4]; - //operating condition register + //operating conditions register unsigned char CSD[16]; //card-specific data register unsigned char FSR[64]; @@ -74,8 +74,8 @@ // 0 synchronizes card and resets internal counter to finalize I/O operations // 1 successively write input to a raw data byte in order starting at address 0 // 2 successively read and return a raw data byte in order starting at address 0 - //return data from sync or write operations, and input data of sync or read operations - //are "don't care" bits + //return data from sync or write operations, and input + //data of sync or read operations are "don't care" bits unsigned char Write(unsigned int Address, unsigned char* Data); unsigned char Write(unsigned int Address, unsigned char SectorCount, unsigned char* Data); //write data sectors to the card @@ -85,8 +85,8 @@ unsigned char SelectCRCMode(bool Mode); //turn CRC mode on or off, default is off void SetTimeout(unsigned int Retries); - //change the number of retries for interface functions; increase if lines are - //unreliable; default is 1024 + //change the number of retries for interface functions; + //increase if lines are unreliable; default is 1024 }; #endif \ No newline at end of file
--- a/main.cpp Mon Aug 23 07:12:13 2010 +0000 +++ b/main.cpp Fri Aug 27 00:59:28 2010 +0000 @@ -30,8 +30,12 @@ } closedir(d); remove("/SDCard/testdir/TEST.txt");*/ - //rename("/SDCard/testdir/TEST.txt", "/SDCard/testdir/TEST2.txt"); + int test = rename("/SDCard/message.txt", "/SDCard/message2.txt"); + fp = fopen("/SDCard/message.txt", "a"); + fprintf(fp, " result = %d", test); + fclose(fp); + ///////////////////// if (1) {