* add the download link for 1.4.1 fw (#417)

* Fix the example of file system. (#419)
This commit is contained in:
Arthur Ma 2018-08-28 19:47:36 +08:00 коммит произвёл GitHub
Родитель c78a50544c
Коммит 81368d6aef
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 147 добавлений и 92 удалений

Просмотреть файл

@ -50,8 +50,8 @@ FATFileSystem (const char *name = NULL, BlockDevice *bd = NULL)
>
> | Type | Name | Description |
> | :--- | :--- | :---------- |
> | const char * | name | Name to add filesystem to tree. |
> | BlockDevice | bd | BlockDevice to mount, could be passed in to avoid mount() call |
> | const char * | name | Name to add filesystem to tree. |
> | BlockDevice | bd | BlockDevice to mount, could be passed in to avoid mount() call |
>
@ -68,9 +68,9 @@ static int format(BlockDevice *bd, int allocation_unit = 0)
> | Type | Name | Description |
> | :--- | :--- | :---------- |
> | BlockDevice | bd | Block device that will be formated. |
> | int | allocation_unit | This is the number of bytes per cluster size.The valid value is N times the sector size. If zero is given, the default allocation unit size is selected by the underlying filesystem, which depends on the volume size. |
>
> | BlockDevice | bd | Block device that will be formated. |
> | int | allocation_unit | This is the number of bytes per cluster size.The valid value is N times the sector size. If zero is given, the default allocation unit size is selected by the underlying filesystem, which depends on the volume size. |
>
>
> #### Return value
>
@ -88,7 +88,7 @@ virtual int mount(BlockDevice *bd)
> | Type | Name | Description |
> | :--- | :--- | :---------- |
> | BlockDevice | bd | Block device that will be formated. |
> | BlockDevice | bd | Block device that will be formated. |
>
> #### Return value
>
@ -103,11 +103,11 @@ virtual int mount(BlockDevice *bd, bool force)
> Mounts a filesystem to a block device.
> #### Parameters
> | Type | Name | Description |
> | :--- | :--- | :---------- |
> | BlockDevice | bd | Block device that will be formated. |
> | bool | force | Flag to force the underlying filesystem to force mounting the filesystem. |
> | BlockDevice | bd | Block device that will be formated. |
> | bool | force | Flag to force the underlying filesystem to force mounting the filesystem. |
>
> #### Return value
>
@ -133,10 +133,10 @@ virtual int remove(const char *path)
> Remove a file from the filesystem.
> #### Parameters
> | Type | Name | Description |
> | :--- | :--- | :---------- |
> | const char* | path | The name of the file to remove. |
> | const char* | path | The name of the file to remove. |
>
> #### Return value
>
@ -150,11 +150,11 @@ virtual int rename(const char *path, const char *newpath);
> Rename a file in the filesystem.
> #### Parameters
> | Type | Name | Description |
> | :--- | :--- | :---------- |
> | const char* | path | The name of the file to rename. |
> | const char* | newpath | The name of the file to rename to. |
> | const char* | path | The name of the file to rename. |
> | const char* | newpath | The name of the file to rename to. |
>
> #### Return value
>
@ -169,11 +169,11 @@ virtual int stat(const char *path, struct stat *st);
> #### Parameters
> | Type | Name | Description |
> | :--- | :--- | :---------- |
> | const char* | path | The name of the file to find information about. |
> | struct stat * | st | The stat buffer to write to. |
> | const char* | path | The name of the file to find information about. |
> | struct stat * | st | The stat buffer to write to. |
>
> #### Return value
>
@ -188,11 +188,11 @@ virtual int mkdir(const char *path, mode_t mode);
> #### Parameters
> | Type | Name | Description |
> | :--- | :--- | :---------- |
> | const char* | path | The name of the directory to create. |
> | mode_t | mode |The permissions with which to create the directory. |
> | const char* | path | The name of the directory to create. |
> | mode_t | mode |The permissions with which to create the directory. |
>
> #### Return value
>
@ -221,7 +221,6 @@ filesystem_info fatfs_get_info()
## Sample code
```cpp
#include "mbed.h"
#include "FATFileSystem.h"
#include "SFlashBlockDevice.h"
#include "fatfs_exfuns.h"
@ -231,91 +230,147 @@ filesystem_info fatfs_get_info()
SFlashBlockDevice bd;
FATFileSystem fs("fs");
void processError(int ret_val) {
if (ret_val) {
Serial.print("Failure.");
Serial.println(ret_val);
} else {
Serial.println("done.");
}
}
void processError(void* ret_val) {
if (ret_val == NULL) {
Serial.println(" Failure.");
} else {
Serial.println(" done.");
}
}
void setup() {
int error = 0;
Serial.println("Welcome to the filesystem example.");
// Format file system
Serial.print("Formatting a FAT, RAM-backed filesystem.");
error = FATFileSystem::format(&bd);
processError(error);
static int initFS()
{
// Mount the file system
Serial.print("Mounting the filesystem on \"/fs\".");
error = fs.mount(&bd);
processError(error);
// Open file to write
Serial.print("Opening a new file, numbers.txt.");
FILE* fd = fopen("/fs/numbers.txt", "w");
processError(fd);
Serial.print("Writing decimal numbers 1~20 to a file.");
for (int i = 0; i < 20; i++){
fprintf(fd, "%d\r\n", i + 1);
int error = fs.mount(&bd);
if (error != 0)
{
Serial.printf("Mount failed %d.\r\n", error);
return -1;
}
Serial.print("done.\r\n");
filesystem_info info = fatfs_get_info();
if (info.total_space == 0)
{
fs.unmount();
// Format the disk
Serial.print("Formatting the file system...");
error = FATFileSystem::format(&bd);
if (error != 0)
{
Serial.printf("failed (%d).\r\n", error);
return -2;
}
Serial.println("done.");
// Close file
Serial.print("Closing file.");
fclose(fd);
Serial.print(" done.\r\n");
// Re-open file to read
Serial.print("Re-opening file read-only.");
fd = fopen("/fs/numbers.txt", "r");
processError(fd);
Serial.print("Dumping file to screen.\r\n");
delay(100);
char buff[16] = {0};
while (!feof(fd)){
int size = fread(&buff[0], 1, 15, fd);
fwrite(&buff[0], 1, size, stdout);
// Mount again
int error = fs.mount(&bd);
if (error != 0)
{
Serial.printf("Mount failed %d.\r\n", error);
return -1;
}
filesystem_info info = fatfs_get_info();
if (info.total_space == 0)
{
Serial.println("Internal error, load filesystem fault.");
return -2;
}
}
Serial.println("EOF.");
Serial.print("Closing file.");
fclose(fd);
Serial.println(" done.");
Serial.println("Mount the filesystem on \"/fs\".");
Serial.printf("Total drive space: %d %cB; free space :%d %cB\r\n", info.total_space, info.unit, info.free_space, info.unit);
return 0;
}
Serial.print("Opening root directory.");
static int listFiles()
{
DIR* dir = opendir("/fs/");
processError(fd);
if (dir == NULL)
{
Serial.println("Open root directory failed.");
return -1;
}
struct dirent* de;
Serial.print("Printing all filenames:\r\n");
Serial.println("ls /fs:");
while((de = readdir(dir)) != NULL){
Serial.print(" ");
Serial.println(&(de->d_name)[0]);
}
Serial.print("Closeing root directory. ");
error = closedir(dir);
processError(error);
int error = closedir(dir);
if (error != 0)
{
Serial.printf("Close directory failed %d.\r\n", error);
return - 1;
}
return 0;
}
Serial.println("FileSystem Demo complete.");
filesystem_info info = fatfs_get_info();
char buf[128];
sprintf(buf, "Total drive space: %d %cB; free space :%d %cB\r\n", info.total_space, info.unit, info.free_space, info.unit);
Serial.println(buf);
static int writeFile()
{
// Open file to write
FILE* fd = fopen("/fs/numbers.txt", "w");
if (fd == NULL)
{
Serial.printf("Open /fs/numbers.txt failed %d.\r\n", error);
return -1;
}
Serial.print("Writing decimal numbers 1~20 to the file...");
for (int i = 0; i < 20; i++){
fprintf(fd, "%d\r\n", i + 1);
}
Serial.println("done.");
fclose(fd);
return 0;
}
static int readFile()
{
// Open file to read
Serial.print("Re-opening file with read-only mode,");
FILE* fd = fopen("/fs/numbers.txt", "r");
if (fd == NULL)
{
Serial.println("failed.");
return -1;
}
Serial.println("done.");
Serial.println("Dumping file:");
delay(100);
char buff[16] = {0};
while (!feof(fd)){
int size = fread(buff, 1, 15, fd);
if (size > 0)
{
buff[size] = 0;
Serial.print(buff);
}
}
Serial.println("EOF.");
fclose(fd);
return 0;
}
void setup() {
Serial.println("Welcome to the FileSystem example.");
if (initFS() != 0)
{
return;
}
if (listFiles() != 0)
{
return;
}
if (writeFile() != 0)
{
return;
}
if (readFile() != 0)
{
return;
}
Serial.println("All done.");
}
void loop() {