Skip to content

Commit

Permalink
Add sd card example project
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraembedded committed Apr 23, 2019
1 parent 068c63b commit 0ef5c2b
Show file tree
Hide file tree
Showing 8 changed files with 595 additions and 0 deletions.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,100 @@ Each release of the project is tested using self verifying test benches to ensur

If you would like to use this code in a commercial project with a closed source compatible license, please contact me.

#### Configuration
See the following defines in src/fat_opts.h:

```
FATFS_IS_LITTLE_ENDIAN [1/0]
Which endian is your system? Set to 1 for little endian, 0 for big endian.
FATFS_MAX_LONG_FILENAME [260]
By default, 260 characters (max LFN length). Increase this to support greater path depths.
FATFS_MAX_OPEN_FILES
The more files you wish to have concurrently open, the greater this number should be.
This increases the number of FL_FILE file structures in the library, each of these is around 1K in size (assuming 512 byte sectors).
FAT_BUFFER_SECTORS
Minimum is 1, more increases performance.
This defines how many FAT sectors can be buffered per FAT_BUFFER entry.
FAT_BUFFERS
Minimum is 1, more increases performance.
This defines how many FAT buffer entries are available.
Memory usage is FAT_BUFFERS * FAT_BUFFER_SECTORS * FAT_SECTOR_SIZE
FATFS_INC_WRITE_SUPPORT
Support file write functionality.
FAT_SECTOR_SIZE
Sector size used by buffers. Most likely to be 512 bytes (standard for ATA/IDE).
FAT_PRINTF
A define that allows the File IO library to print to console/stdout.
Provide your own printf function if printf not available.
FAT_CLUSTER_CACHE_ENTRIES
Size of cluster chain cache (can be undefined if not required).
Mem used = FAT_CLUSTER_CACHE_ENTRIES * 4 * 2
Improves access speed considerably.
FATFS_INC_LFN_SUPPORT [1/0]
Enable/Disable support for long filenames.
FATFS_DIR_LIST_SUPPORT [1/0]
Include support for directory listing.
FATFS_INC_TIME_DATE_SUPPORT [1/0]
Use time/date functions provided by time.h to update creation & modification timestamps.
FATFS_INC_FORMAT_SUPPORT
Include support for formatting disks (FAT16 only).
FAT_PRINTF_NOINC_STDIO
Disable use of printf & inclusion of stdio.h
```


#### Interfacing to storage media
```
-----------------------------------------------------------------
int media_read(uint32 sector, uint8 *buffer, uint32 sector_count)
-----------------------------------------------------------------
Params:
Sector: 32-bit sector number
Buffer: Target buffer to read n sectors of data into.
Sector_count: Number of sectors to read
Return:
int, 1 = success, 0 = failure.
Description:
Application/target specific disk/media read function.
Sector number (sectors are usually 512 byte pages) to read.
-----------------------------------------------------------------
int media_write(uint32 sector, uint8 *buffer, uint32 sector_count)
-----------------------------------------------------------------
Params:
Sector: 32-bit sector number
Buffer: Target buffer to write n sectors of data from.
Sector_count: Number of sectors to write.
Return:
int, 1 = success, 0 = failure.
Description:
Application/target specific disk/media write function.
Sector number (sectors are usually 512 byte pages) to write to.
-----------------------------------------------------------------
Use the following API to attach the media IO functions to the File IO library;
fl_attach_media(media_read, media_write);
```

#### History

Expand Down
38 changes: 38 additions & 0 deletions examples/sd_card_generic/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "spi.h"
#include "sd.h"
#include "fat_filelib.h"

//-----------------------------------------------------------------
// main
//-----------------------------------------------------------------
int main(int argc, char *argv[])
{
// Initialise SPI interface
spi_init();

// Initialise SD interface
if (sd_init() < 0)
{
printf("ERROR: Cannot init SD card\n");
return -1;
}

// Initialise File IO Library
fl_init();

// Attach media access functions to library
if (fl_attach_media(sd_readsector, sd_writesector) != FAT_INIT_OK)
{
printf("ERROR: Failed to init file system\n");
return -1;
}

// List the root directory
fl_listdirectory("/");

return 0;
}

0 comments on commit 0ef5c2b

Please sign in to comment.