Skip to content

Commit f1d44f5

Browse files
UncleRussheinz
authored andcommitted
FatFs integration (SDIO backend) (#242)
1 parent 2994a56 commit f1d44f5

File tree

23 files changed

+38479
-0
lines changed

23 files changed

+38479
-0
lines changed

examples/fatfs/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
PROGRAM = fatfs
2+
EXTRA_COMPONENTS = extras/sdio extras/fatfs
3+
#ESPBAUD = 460800
4+
5+
# FatFS parameters, see extras/fatfs/defaults.mk
6+
FATFS_CODE_PAGE = 866
7+
8+
include ../../common.mk

examples/fatfs/main.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Example of using FatFs
3+
*
4+
* Part of esp-open-rtos
5+
* Copyright (C) 2016 Ruslan V. Uss <[email protected]>
6+
* BSD Licensed as described in the file LICENSE
7+
*/
8+
#include <esp/uart.h>
9+
#include <espressif/esp_common.h>
10+
#include <stdio.h>
11+
#include <fatfs/ff.h>
12+
13+
#define CS_GPIO_PIN 2
14+
#define TEST_FILENAME "/test_loooong_filename.txt"
15+
#define TEST_CONTENTS "Hello! It's FatFs on esp8266 with ESP Open RTOS!"
16+
#define READBUF_SIZE 256
17+
#define DELAY_MS 3000
18+
19+
static const char contents[] = TEST_CONTENTS;
20+
21+
static const char *results[] = {
22+
[FR_OK] = "Succeeded",
23+
[FR_DISK_ERR] = "A hard error occurred in the low level disk I/O layer",
24+
[FR_INT_ERR] = "Assertion failed",
25+
[FR_NOT_READY] = "The physical drive cannot work",
26+
[FR_NO_FILE] = "Could not find the file",
27+
[FR_NO_PATH] = "Could not find the path",
28+
[FR_INVALID_NAME] = "The path name format is invalid",
29+
[FR_DENIED] = "Access denied due to prohibited access or directory full",
30+
[FR_EXIST] = "Access denied due to prohibited access",
31+
[FR_INVALID_OBJECT] = "The file/directory object is invalid",
32+
[FR_WRITE_PROTECTED] = "The physical drive is write protected",
33+
[FR_INVALID_DRIVE] = "The logical drive number is invalid",
34+
[FR_NOT_ENABLED] = "The volume has no work area",
35+
[FR_NO_FILESYSTEM] = "There is no valid FAT volume",
36+
[FR_MKFS_ABORTED] = "The f_mkfs() aborted due to any problem",
37+
[FR_TIMEOUT] = "Could not get a grant to access the volume within defined period",
38+
[FR_LOCKED] = "The operation is rejected according to the file sharing policy",
39+
[FR_NOT_ENOUGH_CORE] = "LFN working buffer could not be allocated",
40+
[FR_TOO_MANY_OPEN_FILES] = "Number of open files > _FS_LOCK",
41+
[FR_INVALID_PARAMETER] = "Given parameter is invalid"
42+
};
43+
44+
static char readbuf[READBUF_SIZE];
45+
46+
static bool failed(FRESULT res)
47+
{
48+
bool fail = res != FR_OK;
49+
if (fail)
50+
printf("\n Error: ");
51+
printf("\n %s\n", results[res]);
52+
return fail;
53+
}
54+
55+
void check_fatfs()
56+
{
57+
const char *vol = f_gpio_to_volume(CS_GPIO_PIN);
58+
59+
printf("\nCreating test file\n----------------------------\n");
60+
61+
FATFS fs;
62+
// Mount filesystem
63+
printf("f_mount(&fs, \"%s\")", vol);
64+
if (failed(f_mount(&fs, vol, 1)))
65+
return;
66+
67+
// Set default drive
68+
printf("f_chdrive(\"%s\")", vol);
69+
if (failed(f_chdrive(vol)))
70+
return;
71+
72+
FIL f;
73+
// Create test file
74+
printf("f_open(&f, \"%s\", FA_WRITE | FA_CREATE_ALWAYS)", TEST_FILENAME);
75+
if (failed(f_open(&f, TEST_FILENAME, FA_WRITE | FA_CREATE_ALWAYS)))
76+
return;
77+
78+
size_t written;
79+
// Write test string
80+
printf("f_write(&f, \"%s\")", contents);
81+
if (failed(f_write(&f, contents, sizeof(contents) - 1, &written)))
82+
return;
83+
printf(" Bytes written: %d\n", written);
84+
85+
// Close file
86+
printf("f_close(&f)");
87+
if (failed(f_close(&f)))
88+
return;
89+
90+
printf("\nReading test file\n----------------------------\n");
91+
92+
// Open test file
93+
printf("f_open(&f, \"%s\", FA_READ)", TEST_FILENAME);
94+
if (failed(f_open(&f, TEST_FILENAME, FA_READ)))
95+
return;
96+
97+
printf(" File size: %u\n", (uint32_t)f_size(&f));
98+
99+
size_t readed;
100+
// Read file
101+
printf("f_read(&f, ...)");
102+
if (failed(f_read(&f, readbuf, sizeof(readbuf) - 1, &readed)))
103+
return;
104+
readbuf[readed] = 0;
105+
106+
printf(" Readed %u bytes, test file contents: %s\n", readed, readbuf);
107+
108+
// Close file
109+
printf("f_close(&f)");
110+
if (failed(f_close(&f)))
111+
return;
112+
113+
// Unmount
114+
printf("f_mount(NULL, \"%s\")", vol);
115+
if (failed(f_mount(NULL, vol, 0)))
116+
return;
117+
}
118+
119+
void user_init(void)
120+
{
121+
uart_set_baud(0, 115200);
122+
printf("SDK version:%s\n\n", sdk_system_get_sdk_version());
123+
124+
while (true)
125+
{
126+
printf("***********************************\nTesting FAT filesystem\n***********************************\n");
127+
check_fatfs();
128+
printf("\n\n");
129+
for (size_t i = 0; i < DELAY_MS; i ++)
130+
sdk_os_delay_us(1000);
131+
}
132+
}

examples/fatfs_rtc/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
PROGRAM = fatfs
2+
EXTRA_COMPONENTS = extras/sdio extras/fatfs extras/i2c extras/ds1307
3+
ESPBAUD = 460800
4+
5+
# We provide uint32_t get_fattime() based on the RTC
6+
FATFS_FS_NORTC = 0
7+
8+
include ../../common.mk

examples/fatfs_rtc/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
This example shows how to use real-time clock (e.g. ds1307)
2+
with FatFs for real timestamps on the filesystem objects.
3+
4+
1. Set `FATFS_FS_NORTC` to 0 (it's 1 by default) in application makefile.
5+
2. Define function `uint32_t get_fattime()` which will return current time in
6+
timestamp format:
7+
8+
Bits | Date part
9+
-------|----------
10+
0..4 | Second / 2 (0..29)
11+
5..10 | Minute (0..59)
12+
11..15 | Hour (0..23)
13+
16..20 | Day (1..31)
14+
21..24 | Month (1..12)
15+
25..31 | Year origin from 1980 (0..127)

examples/fatfs_rtc/main.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Example of using FatFs with RTC clock
3+
*
4+
* Part of esp-open-rtos
5+
* Copyright (C) 2016 Ruslan V. Uss <[email protected]>
6+
* BSD Licensed as described in the file LICENSE
7+
*/
8+
#include <esp/uart.h>
9+
#include <espressif/esp_common.h>
10+
#include <stdio.h>
11+
#include <fatfs/ff.h>
12+
#include <FreeRTOS.h>
13+
#include <task.h>
14+
#include <i2c/i2c.h>
15+
#include <ds1307/ds1307.h>
16+
17+
// SD card
18+
#define CS_GPIO_PIN 2
19+
20+
// ds1307
21+
#define SCL_PIN 5
22+
#define SDA_PIN 4
23+
24+
#define TEST_FILENAME "/test_rtc_file.txt"
25+
#define TEST_CONTENTS "Hello! It's a test file and it can be deleted!"
26+
#define DELAY_MS 3000
27+
28+
// This function called by FatFs
29+
uint32_t get_fattime()
30+
{
31+
struct tm time;
32+
ds1307_get_time(&time);
33+
34+
return ((uint32_t)(time.tm_year - 1980) << 25)
35+
| ((uint32_t)time.tm_mon << 21)
36+
| ((uint32_t)time.tm_mday << 16)
37+
| ((uint32_t)time.tm_hour << 11)
38+
| ((uint32_t)time.tm_min << 5)
39+
| ((uint32_t)time.tm_sec >> 1);
40+
}
41+
42+
static const char contents[] = TEST_CONTENTS;
43+
static FATFS fs;
44+
45+
static void dump_fileinfo()
46+
{
47+
FILINFO info;
48+
printf("File: %s\n", TEST_FILENAME);
49+
if (f_stat(TEST_FILENAME, &info) != FR_OK)
50+
{
51+
printf("Cannot get file status\n");
52+
return;
53+
}
54+
printf("File size: %u bytes\n", (uint32_t)info.fsize);
55+
printf(
56+
"Modified: %04d-%02d-%02d %02d:%02d:%02d\n",
57+
(info.fdate >> 9) + 1980, // year
58+
(info.fdate >> 5) & 0x0f, // month
59+
info.fdate & 0x1f, // day
60+
info.ftime >> 11, // hours
61+
(info.ftime >> 5) & 0x3F, // minutes
62+
(info.ftime & 0x1f) << 1 // seconds
63+
);
64+
65+
}
66+
67+
void rewrite_file_task(void *p)
68+
{
69+
const char *volume = f_gpio_to_volume(CS_GPIO_PIN);
70+
71+
while (true)
72+
{
73+
do
74+
{
75+
if (f_mount(&fs, volume, 1) != FR_OK)
76+
{
77+
printf("Cannot mount volume %s\n", volume);
78+
break;
79+
}
80+
81+
if (f_chdrive(volume) != FR_OK)
82+
{
83+
printf("Cannot set default drive %s\n", volume);
84+
break;
85+
}
86+
87+
printf("\nTest file\n----------------------------\n");
88+
89+
dump_fileinfo();
90+
91+
printf("\nRe-creating test file\n----------------------------\n");
92+
93+
FIL f; //< It's big and it's on the stack! We need larger stack size
94+
95+
if (f_open(&f, TEST_FILENAME, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK)
96+
{
97+
printf("Cannot create file %s\n", TEST_FILENAME);
98+
break;
99+
}
100+
101+
size_t bw;
102+
if (f_write(&f, contents, sizeof(contents) - 1, &bw))
103+
{
104+
printf("Cannot write to file\n");
105+
break;
106+
}
107+
printf("Bytes written: %d\n", bw);
108+
109+
if (f_close(&f) != FR_OK)
110+
{
111+
printf("Cannot close file\n");
112+
break;
113+
}
114+
115+
dump_fileinfo();
116+
117+
f_mount(NULL, volume, 0);
118+
}
119+
while (false);
120+
121+
vTaskDelay(DELAY_MS / portTICK_RATE_MS);
122+
}
123+
}
124+
125+
void user_init(void)
126+
{
127+
uart_set_baud(0, 115200);
128+
printf("SDK version:%s\n\n", sdk_system_get_sdk_version());
129+
130+
i2c_init (SCL_PIN, SDA_PIN);
131+
132+
xTaskCreate(rewrite_file_task, (signed char *)"task1", 512, NULL, 2, NULL);
133+
}

extras/fatfs/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# FatFs - Generic FAT File System Module
2+
3+
Current version: R0.12b
4+
5+
## How to use
6+
7+
Connect your SD card to ESP module
8+
9+
SD pin | ESP8266
10+
--------|------------
11+
1. DAT2 | -
12+
2. /CS | Any accessible GPIO (15, 5, 4, 0, 2, 16)
13+
3. DI | HMOSI (GPIO13)
14+
4. VDD | +3V3
15+
5. CLK | HCLK (GPIO14)
16+
6. VSS | GND
17+
7. DO | HMISO (GPIO12)
18+
8. RSV | -
19+
20+
Add `extras/sdio` and `extras/fatfs` to `EXTRA_COMPONENTS` parameter of your
21+
makefile, e.g.
22+
23+
```Makefile
24+
EXTRA_COMPONENTS = extras/sdio extras/fatfs
25+
```
26+
27+
Use `const char *f_gpio_to_volume(uint8_t gpio)` to get the FatFs volume ID
28+
based on GPIO which is used for CS pin.
29+
30+
## FatFs configuration
31+
32+
Almost all of the FatFs parameters are configurable in the Makefile of your
33+
project. See default values and their meaning in `defaults.mk`.
34+
35+
## Original documentation
36+
37+
http://elm-chan.org/fsw/ff/00index_e.html
38+
39+
## License
40+
41+
Copyright (C) 20xx, ChaN, all right reserved.
42+
43+
FatFs module is an open source software. Redistribution and use of FatFs in
44+
source and binary forms, with or without modification, are permitted provided
45+
that the following condition is met:
46+
47+
1. Redistributions of source code must retain the above copyright notice,
48+
this condition and the following disclaimer.
49+
50+
This software is provided by the copyright holder and contributors "AS IS"
51+
and any warranties related to this software are DISCLAIMED.
52+
The copyright owner or contributors be NOT LIABLE for any damages caused
53+
by use of this software.
54+
55+

0 commit comments

Comments
 (0)