|
| 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 | +} |
0 commit comments