|
| ~SD () |
| Stops the SPI driver cog. More...
|
|
void | Mount (const int basepin) |
| Mounts a volume. More...
|
|
void | Mount (const int pin_do, const int pin_clk, const int pin_di, const int pin_cs) |
| Mount a volume with explicit pin numbers. More...
|
|
void | Unmount (void) |
| Closes any open files, and unmounts the SD card. More...
|
|
void | Open (const char *filename, const char file_mode) |
| Close any currently open file, and open a new one with the given file name and mode. More...
|
|
void | Close (void) |
| Flush and close the currently open file if any. More...
|
|
int | Get (void) |
| Read and return a single character from the currently open file. More...
|
|
int | Get (char *read_buffer, int bytes_to_read_count) |
| Read bytes into a buffer from currently open file. More...
|
|
int | Put (const char C) |
| Write a single character to the file. More...
|
|
int | Put (const char *B) |
| Write a null-terminated string to the file. More...
|
|
int | Put (const char *buffer, int byte_count) |
| Write bytes from buffer into the currently open file. More...
|
|
void | OpenRootDirectory (void) |
| Set up for a directory file listing. More...
|
|
bool | NextFile (char *filename) |
| Find the next file in the root directory and extract its (8.3) name into filename. More...
|
|
int | Seek (const int position) |
| Change the read pointer to a different position in the file. More...
|
|
int | SetDate (const int year, const int month, const int day, const int hour, const int minute, const int second) |
| Set the current date and time for file creation and last modified. More...
|
|
bool | HasError (void) const |
| If there was an error in the SD routines then this function will return an error code. More...
|
|
void | ClearError (void) |
| Resets the error flag to kNoError. More...
|
|
int | GetError (void) const |
| Get the error code. More...
|
|
int | GetClusterSize (void) const |
| Get the FAT cluster size. More...
|
|
int | GetClusterCount (void) const |
| Get the current FAT cluster count. More...
|
|
int | GetFilesize (void) const |
|
FAT16/32 SD card interface.
This class is based on the Spin version of FSRW 2.6 by Rokicki and Lonesock. Thanks!
This object provides FAT16/32 file read/write access on a block device. Only one file can be open at a time. Open modes are 'r' (read), 'a' (append), 'w' (write), and 'd' (delete). Only the root directory is supported. No long filenames are supported. This object also supports traversing the root directory.
This object requires pullup resistors on the four SD card I/O lines. The pullup resistors should be approximately 10kOhms each.
The SPI DO, DI, and CLK lines can be shared with other SPI devices, as long as the Release() command is called after doing SD card activities and before doing other SPI activities. Note: this has not been tested at all.
Cluster size: If it's not 32768, it should be (32K clusters). To format a drive with FAT32 with 32K clusters, use the following command under linux: sudo fdisk -l sudo mkdosfs /dev/sdX -s 64 -F 32 -I
To check the filesystem under linux: sudo dosfsck -v /dev/sdX
Note: the mkdosfs command will format the entire disk, and erase all information on it.
If an exceptional error occurs then the HasError() function will return true. To see what went wrong, query the GetError() function. After you fix the error, clear it with ClearError(); Every function may set the error, although you're generally OK with just checking after Mount() and Open()
- Warning
- Untested with multiple instances!!!
-
The various Get() and Put() methods don't check to make sure that a file is open. It's up to your code to make sure that a file is successfully opened before you need to use it.
- Author
- SRLM (srlm@.nosp@m.srlm.nosp@m.produ.nosp@m.ctio.nosp@m.ns.co.nosp@m.m)
Possible improvements: Write a function that gets a string: int Get(char * Ubuf, char EndOfStringChar);
void SD::Open |
( |
const char * |
filename, |
|
|
const char |
file_mode |
|
) |
| |
|
inline |
Close any currently open file, and open a new one with the given file name and mode.
The filename should be in 8.3 format (up to eight characters, a period, and up to three characters. The filename will be converted to uppercase. Valid characters include A through Z, digits 0 through 9, space, and '$', '', '-', '_', '@', '~', '`', '!', '(', ')', '{', '}', '^', '#', '&' and a single '.'. Filenames can be shorter than 8.3. The behavior for invalid filenames is undefined.
Modes:
- 'd' Delete a file. Deleting a file will not result in kErrorFileNotFound, even if nothing was deleted.
- 'a' Append to a file. If the file exists then calls to Put() will add the bytes to the end of the file. Otherwise, the file is created.
- 'w' Write to a file. If the file exists, it will be replaced.
- 'r' Read from a file. If the file does not exist, then an error is set.
- Parameters
-
filename | Filename in 8.3 format. |
file_mode | one of the modes described above |