├── documents ├── res │ ├── f1.png │ ├── f2.png │ ├── f3.png │ ├── f4.png │ ├── f5.png │ ├── f6.png │ ├── f7.png │ ├── funcs.png │ ├── layers.png │ ├── mkfs.xlsx │ ├── layers1.png │ ├── layers2.png │ ├── layers3.png │ ├── mkfatimg.zip │ ├── modules.png │ ├── rwtest1.png │ ├── rwtest2.png │ ├── rwtest3.png │ ├── uniconv.zip │ ├── app1.c │ ├── app5.c │ ├── app6.c │ ├── app2.c │ └── app3.c ├── doc │ ├── error.html │ ├── size.html │ ├── tell.html │ ├── eof.html │ ├── dinit.html │ ├── sdir.html │ ├── truncate.html │ ├── closedir.html │ ├── close.html │ ├── dstat.html │ ├── chdrive.html │ ├── fattime.html │ ├── mkdir.html │ ├── opendir.html │ ├── findnext.html │ ├── sfile.html │ ├── putc.html │ ├── unlink.html │ ├── getcwd.html │ ├── puts.html │ ├── setcp.html │ ├── read.html │ ├── gets.html │ ├── chmod.html │ ├── utime.html │ ├── getlabel.html │ ├── chdir.html │ ├── write.html │ ├── rename.html │ ├── sync.html │ ├── sfileinfo.html │ ├── setlabel.html │ ├── getfree.html │ ├── dwrite.html │ ├── sfatfs.html │ ├── dread.html │ ├── stat.html │ ├── fdisk.html │ ├── expand.html │ ├── forward.html │ ├── findfirst.html │ └── readdir.html └── css_e.css ├── source ├── ffsystem_baremetal.c ├── ffsystem_cmsis_os.c ├── 00readme.txt ├── drivers │ ├── template │ │ ├── user_diskio.h │ │ ├── usbh_diskio_config.h │ │ ├── sram_diskio_config.h │ │ ├── sd_diskio_config.h │ │ └── user_diskio.c │ ├── sd │ │ ├── sd_diskio.h │ │ ├── sd_diskio_dma_rtos.h │ │ ├── sd_diskio_dma_standalone.h │ │ └── sd_diskio.c │ ├── sram │ │ ├── sram_diskio.h │ │ └── sram_diskio.c │ └── usb_host │ │ └── usbh_diskio.h ├── ff_gen_drv.h ├── diskio.h ├── ffsystem_template.c ├── ff_gen_drv.c └── diskio.c ├── st_license.txt ├── .github ├── PULL_REQUEST_TEMPLATE.md └── ISSUE_TEMPLATE │ ├── other-issue.md │ └── bug_report.md ├── LICENSE.md ├── SECURITY.md ├── README.md ├── CONTRIBUTING.md └── CODE_OF_CONDUCT.md /documents/res/f1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/f1.png -------------------------------------------------------------------------------- /documents/res/f2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/f2.png -------------------------------------------------------------------------------- /documents/res/f3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/f3.png -------------------------------------------------------------------------------- /documents/res/f4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/f4.png -------------------------------------------------------------------------------- /documents/res/f5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/f5.png -------------------------------------------------------------------------------- /documents/res/f6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/f6.png -------------------------------------------------------------------------------- /documents/res/f7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/f7.png -------------------------------------------------------------------------------- /documents/res/funcs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/funcs.png -------------------------------------------------------------------------------- /documents/res/layers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/layers.png -------------------------------------------------------------------------------- /documents/res/mkfs.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/mkfs.xlsx -------------------------------------------------------------------------------- /documents/res/layers1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/layers1.png -------------------------------------------------------------------------------- /documents/res/layers2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/layers2.png -------------------------------------------------------------------------------- /documents/res/layers3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/layers3.png -------------------------------------------------------------------------------- /documents/res/mkfatimg.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/mkfatimg.zip -------------------------------------------------------------------------------- /documents/res/modules.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/modules.png -------------------------------------------------------------------------------- /documents/res/rwtest1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/rwtest1.png -------------------------------------------------------------------------------- /documents/res/rwtest2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/rwtest2.png -------------------------------------------------------------------------------- /documents/res/rwtest3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/rwtest3.png -------------------------------------------------------------------------------- /documents/res/uniconv.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/documents/res/uniconv.zip -------------------------------------------------------------------------------- /source/ffsystem_baremetal.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/source/ffsystem_baremetal.c -------------------------------------------------------------------------------- /source/ffsystem_cmsis_os.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/HEAD/source/ffsystem_cmsis_os.c -------------------------------------------------------------------------------- /st_license.txt: -------------------------------------------------------------------------------- 1 | This software component is provided to you as part of a software package and 2 | applicable license terms are in the Package_license file. If you received this 3 | software component outside of a package or without applicable license terms, 4 | the terms of the BSD-3-Clause license shall apply. 5 | You may obtain a copy of the BSD-3-Clause at: 6 | https://opensource.org/licenses/BSD-3-Clause 7 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## IMPORTANT INFORMATION 2 | 3 | ### Contributor License Agreement (CLA) 4 | * The Pull Request feature will be considered by STMicroelectronics after the signature of a **Contributor License Agreement (CLA)** by the submitter. 5 | * If you did not sign such agreement, please follow the steps mentioned in the [CONTRIBUTING.md](https://github.com/STMicroelectronics/stm32_mw_fatfs/blob/master/CONTRIBUTING.md) file. 6 | -------------------------------------------------------------------------------- /source/00readme.txt: -------------------------------------------------------------------------------- 1 | FatFs Module Source Files R0.15 2 | 3 | 4 | FILES 5 | 6 | 00readme.txt This file. 7 | 00history.txt Revision history. 8 | ff.c FatFs module. 9 | ffconf.h Configuration file of FatFs module. 10 | ff.h Common include file for FatFs and application module. 11 | diskio.h Common include file for FatFs and disk I/O module. 12 | diskio.c An example of glue function to attach existing disk I/O module to FatFs. 13 | ffunicode.c Optional Unicode utility functions. 14 | ffsystem.c An example of optional O/S related functions. 15 | 16 | 17 | Low level disk I/O module is not included in this archive because the FatFs 18 | module is only a generic file system layer and it does not depend on any specific 19 | storage device. You need to provide a low level disk I/O module written to 20 | control the storage device that attached to the target system. 21 | 22 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/other-issue.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 'Other Issue ' 3 | about: Generic issue description 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Caution** 11 | The Issues are strictly limited for the reporting of problem encountered with the software provided in this project. 12 | For any other problem related to the STM32 product, the performance, the hardware characteristics and boards, the tools the environment in general, please post a topic in the [ST Community/STM32 MCUs forum](https://community.st.com/s/group/0F90X000000AXsASAW/stm32-mcus). 13 | 14 | **Describe the set-up** 15 | * The board (either ST RPN reference or your custom board). 16 | * IDE or at least the compiler and its version. 17 | 18 | **Additional context** 19 | If you have a first analysis or a patch proposal, thank you to share your proposal. 20 | 21 | **Screenshots** 22 | If applicable, add screenshots to help explain your problem. 23 | -------------------------------------------------------------------------------- /documents/res/app1.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------/ 2 | / Open or create a file in append mode 3 | / (This function was sperseded by FA_OPEN_APPEND flag at FatFs R0.12a) 4 | /------------------------------------------------------------*/ 5 | 6 | FRESULT open_append ( 7 | FIL* fp, /* [OUT] File object to create */ 8 | const char* path /* [IN] File name to be opened */ 9 | ) 10 | { 11 | FRESULT fr; 12 | 13 | /* Opens an existing file. If not exist, creates a new file. */ 14 | fr = f_open(fp, path, FA_WRITE | FA_OPEN_ALWAYS); 15 | if (fr == FR_OK) { 16 | /* Seek to end of the file to append data */ 17 | fr = f_lseek(fp, f_size(fp)); 18 | if (fr != FR_OK) 19 | f_close(fp); 20 | } 21 | return fr; 22 | } 23 | 24 | 25 | int main (void) 26 | { 27 | FRESULT fr; 28 | FATFS fs; 29 | FIL fil; 30 | 31 | /* Open or create a log file and ready to append */ 32 | f_mount(&fs, "", 0); 33 | fr = open_append(&fil, "logfile.txt"); 34 | if (fr != FR_OK) return 1; 35 | 36 | /* Append a line */ 37 | f_printf(&fil, "%02u/%02u/%u, %2u:%02u\n", Mday, Mon, Year, Hour, Min); 38 | 39 | /* Close the file */ 40 | f_close(&fil); 41 | 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve the quality of our software 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Caution** 11 | The Issues are strictly limited for the reporting of problem encountered with the software provided in this project. 12 | For any other problem related to the STM32 product, the performance, the hardware characteristics and boards, the tools the environment in general, please post a topic in the [ST Community/STM32 MCUs forum](https://community.st.com/s/group/0F90X000000AXsASAW/stm32-mcus). 13 | 14 | **Describe the set-up** 15 | * The board (either ST RPN reference or your custom board). 16 | * IDE or at least the compiler and its version. 17 | 18 | **Describe the bug** 19 | A clear and concise description of what the bug is. 20 | 21 | **How To Reproduce** 22 | 1. Indicate the global behavior of your application project. 23 | 24 | 2. The modules that you suspect to be the cause of the problem (Driver, BSP, MW ...). 25 | 26 | 3. The use case that generates the problem. 27 | 28 | 4. How we can reproduce the problem. 29 | 30 | 31 | **Additional context** 32 | If you have a first analysis or patch correction, thank you to share your proposal. 33 | 34 | **Screenshots** 35 | If applicable, add screenshots to help explain your problem. 36 | -------------------------------------------------------------------------------- /source/drivers/template/user_diskio.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file user_diskio.h 4 | * @author MCD Application Team 5 | * @brief Header for user_diskio.c module 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2023 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the st_license.txt 13 | * file in the root directory of this software component. 14 | * If no st_license.txt file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* Define to prevent recursive inclusion -------------------------------------*/ 19 | #ifndef __USER_DISKIO_H 20 | #define __USER_DISKIO_H 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | #include "ff_gen_drv.h" 28 | 29 | /* Exported types ------------------------------------------------------------*/ 30 | /* Exported constants --------------------------------------------------------*/ 31 | /* Exported functions ------------------------------------------------------- */ 32 | extern Diskio_drvTypeDef USER_Driver; 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif /* __USER_DISKIO_H */ 39 | -------------------------------------------------------------------------------- /documents/res/app5.c: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------/ 2 | / Test if the file is contiguous / 3 | /----------------------------------------------------------------------*/ 4 | 5 | FRESULT test_contiguous_file ( 6 | FIL* fp, /* [IN] Open file object to be checked */ 7 | int* cont /* [OUT] 1:Contiguous, 0:Fragmented or zero-length */ 8 | ) 9 | { 10 | DWORD clst, clsz, step; 11 | FSIZE_t fsz; 12 | FRESULT fr; 13 | 14 | 15 | *cont = 0; 16 | fr = f_rewind(fp); /* Validates and prepares the file */ 17 | if (fr != FR_OK) return fr; 18 | 19 | #if FF_MAX_SS == FF_MIN_SS 20 | clsz = (DWORD)fp->obj.fs->csize * FF_MAX_SS; /* Cluster size */ 21 | #else 22 | clsz = (DWORD)fp->obj.fs->csize * fp->obj.fs->ssize; 23 | #endif 24 | fsz = f_size(fp); 25 | if (fsz > 0) { 26 | clst = fp->obj.sclust - 1; /* A cluster leading the first cluster for first test */ 27 | while (fsz) { 28 | step = (fsz >= clsz) ? clsz : (DWORD)fsz; 29 | fr = f_lseek(fp, f_tell(fp) + step); /* Advances file pointer a cluster */ 30 | if (fr != FR_OK) return fr; 31 | if (clst + 1 != fp->clust) break; /* Is not the cluster next to previous one? */ 32 | clst = fp->clust; fsz -= step; /* Get current cluster for next test */ 33 | } 34 | if (fsz == 0) *cont = 1; /* All done without fail? */ 35 | } 36 | 37 | return FR_OK; 38 | } 39 | -------------------------------------------------------------------------------- /source/drivers/template/usbh_diskio_config.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbh_diskio_config.h 4 | * @author MCD Application Team 5 | * @brief Template for the usbh_diskio_config.h. This file should be copied and 6 | under project. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the st_license.txt 14 | * file in the root directory of this software component. 15 | * If no st_license.txt file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | #ifndef USBH_DISKIO_CONFIG_H 21 | #define USBH_DISKIO_CONFIG_H 22 | 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | /* Includes ------------------------------------------------------------------*/ 28 | 29 | #include "usbh_msc.h" 30 | 31 | /* Private typedef -----------------------------------------------------------*/ 32 | /* Private define ------------------------------------------------------------*/ 33 | /* Block size */ 34 | #define USB_BLOCK_SIZE 512 35 | 36 | extern USBH_HandleTypeDef hUsbHost; 37 | /* Default handle used in usbh_diskio.c file */ 38 | #define hUsb_Host hUsbHost 39 | 40 | #ifdef __cplusplus 41 | } 42 | #endif 43 | 44 | #endif /* USBH_DISKIO_CONFIG_H */ -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2015, ChaN, all right reserved. 2 | # Copyright (c) 2019 STMicroelectronics. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation and/or 12 | other materials provided with the distribution. 13 | 14 | 3. Neither the name of the copyright holder nor the names of its contributors 15 | may be used to endorse or promote products derived from this software without 16 | specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /source/drivers/template/sram_diskio_config.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file sram_diskio_config.h 4 | * @author MCD Application Team 5 | * @brief Template for the sram_diskio_config.h. This file should be copied and 6 | under project. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | * Copyright (c) 2023 STMicroelectronics. 11 | * All rights reserved. 12 | * 13 | * This software is licensed under terms that can be found in the st_license.txt 14 | * file in the root directory of this software component. 15 | * If no st_license.txt file comes with this software, it is provided AS-IS. 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | #ifndef SRAM_DISKIO_CONFIG_H 21 | #define SRAM_DISKIO_CONFIG_H 22 | 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | /* Includes ------------------------------------------------------------------*/ 28 | 29 | #include "stm32{family}xx_hal.h" 30 | 31 | /* Private typedef -----------------------------------------------------------*/ 32 | /* Private define ------------------------------------------------------------*/ 33 | /* Block size */ 34 | #define BLOCK_SIZE 512 35 | 36 | /* Base Address */ 37 | #define SRAM_DISK_BASE_ADDR 0x24000000 38 | 39 | /* SRAM Disk size in bytes */ 40 | #define SRAM_DISK_SIZE (32 * 1024) 41 | 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif /* SRAM_DISKIO_CONFIG_H */ -------------------------------------------------------------------------------- /source/drivers/sd/sd_diskio.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file sd_diskio.h 4 | * @author MCD Application Team 5 | * @brief Header for sd_diskio.c module 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2023 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the st_license.txt 13 | * file in the root directory of this software component. 14 | * If no st_license.txt file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* Define to prevent recursive inclusion -------------------------------------*/ 19 | #ifndef __SD_DISKIO_H 20 | #define __SD_DISKIO_H 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | /* 28 | * The sd_diskio_config.h is under application project 29 | * and contain the config parameters for SD diskio 30 | * 31 | */ 32 | #include "sd_diskio_config.h" 33 | #include "ff_gen_drv.h" 34 | 35 | /* Exported types ------------------------------------------------------------*/ 36 | /* Exported constants --------------------------------------------------------*/ 37 | /* Exported functions ------------------------------------------------------- */ 38 | extern const Diskio_drvTypeDef SD_Driver; 39 | 40 | #ifdef __cplusplus 41 | } 42 | #endif 43 | 44 | #endif /* __SD_DISKIO_H */ 45 | -------------------------------------------------------------------------------- /source/drivers/sram/sram_diskio.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file sram_diskio.h 4 | * @author MCD Application Team 5 | * @brief Header for sram_diskio.c module 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2023 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the st_license.txt 13 | * file in the root directory of this software component. 14 | * If no st_license.txt file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* Define to prevent recursive inclusion -------------------------------------*/ 19 | #ifndef __SRAM_DISKIO_H 20 | #define __SRAM_DISKIO_H 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | /* 28 | * The sram_diskio_config.h is under application project 29 | * and contain the config parameters for SRAM diskio 30 | * 31 | */ 32 | #include "sram_diskio_config.h" 33 | #include "ff_gen_drv.h" 34 | 35 | /* Exported types ------------------------------------------------------------*/ 36 | /* Exported constants --------------------------------------------------------*/ 37 | /* Exported functions ------------------------------------------------------- */ 38 | extern const Diskio_drvTypeDef SRAMDISK_Driver; 39 | 40 | #ifdef __cplusplus 41 | } 42 | #endif 43 | 44 | #endif /* __SRAM_DISKIO_H */ 45 | 46 | -------------------------------------------------------------------------------- /source/drivers/usb_host/usbh_diskio.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usbh_diskio.h 4 | * @author MCD Application Team 5 | * @brief Header for usbh_diskio.c module. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2023 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the st_license.txt 13 | * file in the root directory of this software component. 14 | * If no st_license.txt file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* Define to prevent recursive inclusion -------------------------------------*/ 19 | #ifndef __USBH_DISKIO_H 20 | #define __USBH_DISKIO_H 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | /* 28 | * The usbh_diskio_config.h is under application project 29 | * and contain the config parameters for USBH diskio 30 | * 31 | */ 32 | #include "usbh_diskio_config.h" 33 | #include "ff_gen_drv.h" 34 | #include "usbh_core.h" 35 | 36 | /* Exported types ------------------------------------------------------------*/ 37 | /* Exported constants --------------------------------------------------------*/ 38 | /* Exported functions ------------------------------------------------------- */ 39 | extern const Diskio_drvTypeDef USBH_Driver; 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif /* __USBH_DISKIO_H */ 46 | -------------------------------------------------------------------------------- /source/drivers/sd/sd_diskio_dma_rtos.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file sd_diskio_dma_rtos.h 4 | * @author MCD Application Team 5 | * @brief Header for sd_diskio_dma_rtos.c module 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2023 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the st_license.txt 13 | * file in the root directory of this software component. 14 | * If no st_license.txt file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* Define to prevent recursive inclusion -------------------------------------*/ 19 | #ifndef __SD_DISKIO_DMA_RTOS_H 20 | #define __SD_DISKIO_DMA_RTOS_H 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | /* 28 | * The sd_diskio_config.h is under application project 29 | * and contain the config parameters for SD diskio 30 | * 31 | */ 32 | #include "sd_diskio_config.h" 33 | #include "cmsis_os2.h" 34 | #include "ff_gen_drv.h" 35 | 36 | /* Exported types ------------------------------------------------------------*/ 37 | /* Exported constants --------------------------------------------------------*/ 38 | /* Exported functions ------------------------------------------------------- */ 39 | extern const Diskio_drvTypeDef SD_DMA_Driver; 40 | 41 | #ifdef __cplusplus 42 | } 43 | #endif 44 | 45 | #endif /* __SD_DISKIO_DMA_RTOS_H */ 46 | -------------------------------------------------------------------------------- /source/drivers/sd/sd_diskio_dma_standalone.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file sd_diskio_dma_standalone.h 4 | * @author MCD Application Team 5 | * @brief Header for sd_diskio_dma_standalone.c module 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2023 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the st_license.txt 13 | * file in the root directory of this software component. 14 | * If no st_license.txt file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* Define to prevent recursive inclusion -------------------------------------*/ 19 | #ifndef __SD_DISKIO_DMA_STANDALONE_H 20 | #define __SD_DISKIO_DMA_STANDALONE_H 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | /* 28 | * The sd_diskio_config.h is under application project 29 | * and contain the config parameters for SD diskio 30 | * 31 | */ 32 | #include "sd_diskio_config.h" 33 | #include "ff_gen_drv.h" 34 | 35 | /* Exported types ------------------------------------------------------------*/ 36 | /* Exported constants --------------------------------------------------------*/ 37 | /* Exported functions ------------------------------------------------------- */ 38 | extern const Diskio_drvTypeDef SD_DMA_Driver; 39 | 40 | #ifdef __cplusplus 41 | } 42 | #endif 43 | 44 | #endif /* __SD_DISKIO_DMA_STANDALONE_H */ 45 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Report potential product security vulnerabilities 2 | 3 | ST places a high priority on security, and our Product Security Incident 4 | Response Team (PSIRT) is committed to rapidly addressing potential security 5 | vulnerabilities affecting our products. PSIRT's long history and vast experience 6 | in security allows ST to perform clear analyses and provide appropriate guidance 7 | on mitigations and solutions when applicable. 8 | 9 | If you wish to report potential security vulnerabilities regarding our products, 10 | **please do not report them through public GitHub issues.** Instead, we 11 | encourage you to report them to our ST PSIRT following the process described at: 12 | **https://www.st.com/content/st_com/en/security/report-vulnerabilities.html** 13 | 14 | ### IMPORTANT - READ CAREFULLY: 15 | 16 | STMicroelectronics International N.V., on behalf of itself, its affiliates and 17 | subsidiaries, (collectively “ST”) takes all potential security vulnerability 18 | reports or other related communications (“Report(s)”) seriously. In order to 19 | review Your Report (the terms “You” and “Yours” include your employer, and all 20 | affiliates, subsidiaries and related persons or entities) and take actions as 21 | deemed appropriate, ST requires that we have the rights and Your permission to 22 | do so. 23 | 24 | As such, by submitting Your Report to ST, You agree that You have the right to 25 | do so, and You grant to ST the rights to use the Report for purposes related to 26 | security vulnerability analysis, testing, correction, patching, reporting and 27 | any other related purpose or function. 28 | 29 | By submitting Your Report, You agree that ST’s 30 | [Privacy Policy](https://www.st.com/content/st_com/en/common/privacy-portal.html) 31 | applies to all related communications. 32 | -------------------------------------------------------------------------------- /documents/doc/error.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 9 |The f_error tests for an error on a file.
17 |18 | int f_error ( 19 | FIL* fp /* [IN] File object */ 20 | ); 21 |22 |
Returns a non-zero value if a hard error has occured; otherwise it returns a zero.
37 |In this revision, this function is implemented as a macro. It does not have any validation and mutual exclusion.
43 |
44 | #define f_error(fp) ((fp)->err)
45 |
46 | Always available.
52 |The f_size function gets the size of a file.
17 |18 | FSIZE_t f_size ( 19 | FIL* fp /* [IN] File object */ 20 | ); 21 |22 |
Returns the size of the file in unit of byte.
37 |In this revision, the f_size function is implemented as a macro. It does not have any validation and mutual exclusion.
43 |
44 | #define f_size(fp) ((fp)->obj.objsize)
45 |
46 | Always available.
52 |The f_tell function gets the current read/write pointer of a file.
17 |18 | FSIZE_t f_tell ( 19 | FIL* fp /* [IN] File object */ 20 | ); 21 |22 |
Returns current read/write pointer of the file.
37 |In this revision, the f_tell function is implemented as a macro. It does not have any validation and mutual exclusion.
43 |
44 | #define f_tell(fp) ((fp)->fptr)
45 |
46 | Always available.
52 |The f_eof function tests for end-of-file on a file.
17 |18 | int f_eof ( 19 | FIL* fp /* [IN] File object */ 20 | ); 21 |22 |
The f_eof function returns a non-zero value if the read/write pointer has reached end of the file; otherwise it returns a zero.
37 |In this revision, this function is implemented as a macro. It does not have any validation and mutual exclusion.
43 |
44 | #define f_eof(fp) ((int)((fp)->fptr == (fp)->fsize))
45 |
46 | Always available.
52 |The disk_initialize function is called to initializes the storage device.
17 |18 | DSTATUS disk_initialize ( 19 | BYTE pdrv /* [IN] Physical drive number */ 20 | ); 21 |22 |
This function returns the current drive status flags as the result. For details of the drive status, refer to the disk_status function.
36 |This function initializes the storage device and put it ready to generic read/write. When the function succeeded, STA_NOINIT flag in the return value is cleared.
41 |Remarks: This function needs to be under control of FatFs module. Application program MUST NOT call this function while FatFs is in use, or FAT structure on the volume can be broken. To re-initialize the filesystem, use f_mount function instead.
42 |The DIR structure is used for the work area to read a directory by f_oepndir, f_readdir, f_findfirst and f_findnext function. Application program must not modify any member in this structure, or f_readdir function will not work properly.
17 |18 | typedef struct { 19 | FFOBJID obj; /* Object identifier */ 20 | DWORD dptr; /* Current read/write offset */ 21 | DWORD clust; /* Current cluster */ 22 | LBA_t sect; /* Current sector */ 23 | BYTE* dir; /* Pointer to the current SFN entry in the win[] */ 24 | BYTE* fn; /* Pointer to the SFN buffer (in/out) {file[8],ext[3],status[1]} */ 25 | #if FF_USE_LFN 26 | DWORD blk_ofs; /* Offset of the entry block (0xFFFFFFFF:Invalid) */ 27 | WCHAR* lfn; /* Pointer to the LFN working buffer (in/out) */ 28 | #endif 29 | #if FF_USE_FIND 30 | const TCHAR* pat; /* Ponter to the matching pattern */ 31 | #endif 32 | } DIR; 33 |34 |
The f_truncate function truncates the file size.
17 |18 | FRESULT f_truncate ( 19 | FIL* fp /* [IN] File object */ 20 | ); 21 |22 |
36 | FR_OK, 37 | FR_DISK_ERR, 38 | FR_INT_ERR, 39 | FR_DENIED, 40 | FR_INVALID_OBJECT, 41 | FR_TIMEOUT 42 |
43 |The f_truncate function truncates the file size to the current file read/write pointer. This function has no effect if the file read/write pointer is already pointing end of the file.
49 |Available when FF_FS_READONLY == 0 and FF_FS_MINIMIZE == 0.
55 |The f_closedir function closes an open directory.
17 |18 | FRESULT f_closedir ( 19 | DIR* dp /* [IN] Pointer to the directory object */ 20 | ); 21 |22 |
36 | FR_OK, 37 | FR_INT_ERR, 38 | FR_INVALID_OBJECT, 39 | FR_TIMEOUT 40 |
41 |The f_closedir function closes an open directory object. After the function succeeded, the directory object is no longer valid and it can be discarded.
47 |Note that the directory object can also be discarded without this procedure when option FF_FS_LOCK is not enabled. However this is not recommended for future compatibility.
48 |Available when FF_FS_MINIMIZE <= 1.
54 |The f_close function closes an open file.
17 |18 | FRESULT f_close ( 19 | FIL* fp /* [IN] Pointer to the file object */ 20 | ); 21 |22 |
36 | FR_OK, 37 | FR_DISK_ERR, 38 | FR_INT_ERR, 39 | FR_INVALID_OBJECT, 40 | FR_TIMEOUT 41 |
42 |The f_close function closes an open file object. If the file has been changed, the cached information of the file is written back to the volume. After the function succeeded, the file object is no longer valid and it can be discarded.
48 |Note that if the file object is in read-only mode and FF_FS_LOCK is not enabled, the file object can also be discarded without this procedure. However this is not recommended for future compatibility.
49 |Always available.
55 |The disk_status function is called to inquire the current drive status.
17 |18 | DSTATUS disk_status ( 19 | BYTE pdrv /* [IN] Physical drive number */ 20 | ); 21 |22 |
The current drive status is returned in combination of status flags described below. FatFs refers only STA_NOINIT and STA_PROTECT.
36 |The f_chdrive function changes the current drive.
17 |18 | FRESULT f_chdrive ( 19 | const TCHAR* path /* [IN] Logical drive number */ 20 | ); 21 |22 |
36 | FR_OK, 37 | FR_INVALID_DRIVE 38 |
39 |The f_chdrive function changes only the current drive. The initial value of the current drive number is 0. In Unix style drive prefix configuration, this function will not be needed because f_chdir function changes also the current drive. Note that the current drive is retained in a static variable, so that it also affects other tasks that using the file functions.
45 |Available when FF_FS_RPATH >= 1.
50 |
56 | f_chdrive("2:"); /* Set drive 2 as current drive */
57 |
58 | f_chdrive(""); /* No effect (set current drive as current drive) */
59 |
60 | The get_fattime function is called to get the current time.
17 |18 | DWORD get_fattime (void); 19 |20 |
Currnet local time shall be returned as bit-fields packed into a DWORD value. The bit fields are as follows:
26 |The get_fattime function shall return any valid time even if the system does not support a real time clock. If a zero is returned, the file will not have a valid timestamp.
46 |This function is not needed when FF_FS_READONLY == 1 or FF_FS_NORTC == 1.
52 |
58 | DWORD get_fattime (void)
59 | {
60 | time_t t;
61 | struct tm *stm;
62 |
63 |
64 | t = time(0);
65 | stm = localtime(&t);
66 |
67 | return (DWORD)(stm->tm_year - 80) << 25 |
68 | (DWORD)(stm->tm_mon + 1) << 21 |
69 | (DWORD)stm->tm_mday << 16 |
70 | (DWORD)stm->tm_hour << 11 |
71 | (DWORD)stm->tm_min << 5 |
72 | (DWORD)stm->tm_sec >> 1;
73 | }
74 |
75 | The f_mkdir function creates a new directory.
17 |18 | FRESULT f_mkdir ( 19 | const TCHAR* path /* [IN] Directory name */ 20 | ); 21 |22 |
36 | FR_OK, 37 | FR_DISK_ERR, 38 | FR_INT_ERR, 39 | FR_NOT_READY, 40 | FR_NO_PATH, 41 | FR_INVALID_NAME, 42 | FR_DENIED, 43 | FR_EXIST, 44 | FR_WRITE_PROTECTED, 45 | FR_INVALID_DRIVE, 46 | FR_NOT_ENABLED, 47 | FR_NO_FILESYSTEM, 48 | FR_TIMEOUT, 49 | FR_NOT_ENOUGH_CORE 50 |
51 |This function creates a new directory. To remove a directory, use f_unlink function.
57 |Available when FF_FS_READONLY == 0 and FF_FS_MINIMIZE == 0.
63 |
69 | res = f_mkdir("sub1");
70 | if (res) die(res);
71 | res = f_mkdir("sub1/sub2");
72 | if (res) die(res);
73 | res = f_mkdir("sub1/sub2/sub3");
74 | if (res) die(res);
75 |
76 | The f_opendir function opens a directory.
17 |18 | FRESULT f_opendir ( 19 | DIR* dp, /* [OUT] Pointer to the directory object structure */ 20 | const TCHAR* path /* [IN] Directory name */ 21 | ); 22 |23 |
39 | FR_OK, 40 | FR_DISK_ERR, 41 | FR_INT_ERR, 42 | FR_NOT_READY, 43 | FR_NO_PATH, 44 | FR_INVALID_NAME, 45 | FR_INVALID_OBJECT, 46 | FR_INVALID_DRIVE, 47 | FR_NOT_ENABLED, 48 | FR_NO_FILESYSTEM, 49 | FR_TIMEOUT, 50 | FR_NOT_ENOUGH_CORE, 51 | FR_TOO_MANY_OPEN_FILES 52 |
53 |The f_opendir function opens an exsisting directory and creates a directory object for subsequent f_readdir function.
59 |Available when FF_FS_MINIMIZE <= 1.
65 |The f_findnext function searches for a next matched object
17 |18 | FRESULT f_findnext ( 19 | DIR* dp, /* [IN] Poninter to the directory object */ 20 | FILINFO* fno /* [OUT] Pointer to the file information structure */ 21 | ); 22 |23 |
39 | FR_OK, 40 | FR_DISK_ERR, 41 | FR_INT_ERR, 42 | FR_NOT_READY, 43 | FR_INVALID_OBJECT, 44 | FR_TIMEOUT, 45 | FR_NOT_ENOUGH_CORE 46 |
47 |It continues the search from a previous call to the f_findfirst or f_findnext function. If found, the information about the object is stored into the file information structure. If no item to be read, a null string will be returned into fno->fname[].
53 |This is a wrapper function of f_readdir function. Available when FF_USE_FIND == 1 and FF_FS_MINIMIZE <= 1.
59 |The FIL structure (file object) holds the state of an open file. It is created by f_open function and discarded by f_close function. Application program must not modify any member in this structure except for cltbl, or the FAT volume will be collapsed. Note that a sector buffer is defined in this structure at non-tiny configuration (FF_FS_TINY == 0), so that the FIL structures at that configuration should not be defined as auto variable.
17 | 18 |19 | typedef struct { 20 | FFOBJID obj; /* Object identifier */ 21 | BYTE flag; /* File object status flags */ 22 | BYTE err; /* Abort flag (error code) */ 23 | FSIZE_t fptr; /* File read/write pointer (Byte offset origin from top of the file) */ 24 | DWORD clust; /* Current cluster of fptr (One cluster behind if fptr is on the cluster boundary. Invalid if fptr == 0.) */ 25 | LBA_t sect; /* Current data sector (Can be invalid if fptr is on the cluster boundary.)*/ 26 | #if !FF_FS_READONLY 27 | LBA_t dir_sect; /* Sector number containing the directory entry */ 28 | BYTE* dir_ptr; /* Ponter to the directory entry in the window */ 29 | #endif 30 | #if FF_USE_FASTSEEK 31 | DWORD* cltbl; /* Pointer to the cluster link map table (Nulled on file open. Set by application.) */ 32 | #endif 33 | #if !FF_FS_TINY 34 | BYTE buf[FF_MAX_SS]; /* File private data transfer buffer (Always valid if fptr is not on the sector boundary but can be invalid if fptr is on the sector boundary.) */ 35 | #endif 36 | } FIL; 37 |38 | 39 |
The f_putc funciton puts a character to the file.
17 |18 | int f_putc ( 19 | TCHAR chr, /* [IN] A character to write */ 20 | FIL* fp /* [IN] File object */ 21 | ); 22 |23 |
When the character was written successfuly, it returns number of character encoding units written to the file. When the function failed due to disk full or any error, a negative value will be returned.
39 |When FatFs is configured for Unicode API (FF_LFN_UNICODE >= 1), character encoding on the string fuctions, f_putc, f_puts, f_printf and f_gets function, is also switched to Unicode. The character encoding on the file to be read/written via those functions is selected by FF_STRF_ENCODE. The Unicode characters in multiple encoding unit, such as surrogate pair and multi-byte sequence, cannot be written with this function.
45 |This is a wrapper function of f_write function. Available when FF_FS_READONLY == 0 and FF_USE_STRFUNC >= 1. When FF_USE_STRFUNC == 2, a '\n' is output as '\r'+'\n'.
50 |The f_unlink function removes a file or sub-directory from the volume.
17 |18 | FRESULT f_unlink ( 19 | const TCHAR* path /* [IN] Object name */ 20 | ); 21 |22 |
36 | FR_OK, 37 | FR_DISK_ERR, 38 | FR_INT_ERR, 39 | FR_NOT_READY, 40 | FR_NO_FILE, 41 | FR_NO_PATH, 42 | FR_INVALID_NAME, 43 | FR_DENIED, 44 | FR_WRITE_PROTECTED, 45 | FR_INVALID_DRIVE, 46 | FR_NOT_ENABLED, 47 | FR_NO_FILESYSTEM, 48 | FR_TIMEOUT, 49 | FR_LOCKED, 50 | FR_NOT_ENOUGH_CORE 51 |
52 |58 | If condition of the object to be removed is applicable to the following terms, the function will be rejected.
Available when FF_FS_READONLY == 0 and FF_FS_MINIMIZE == 0.
69 |The f_getcwd function retrieves the current directory of the current drive.
17 |18 | FRESULT f_getcwd ( 19 | TCHAR* buff, /* [OUT] Buffer to return path name */ 20 | UINT len /* [IN] The length of the buffer */ 21 | ); 22 |23 |
39 | FR_OK, 40 | FR_DISK_ERR, 41 | FR_INT_ERR, 42 | FR_NOT_READY, 43 | FR_NOT_ENABLED, 44 | FR_NO_FILESYSTEM, 45 | FR_TIMEOUT, 46 | FR_NOT_ENOUGH_CORE 47 |
48 |The f_getcwd function retrieves full path name of the current directory of the current drive. When FF_VOLUMES >= 2, a heading drive prefix is added to the path name. The style of drive prefix depends on FF_STR_VOLUME_ID.
54 |Note: In this revision, this function cannot retrieve the current directory path on the exFAT volume. It always returns the root directory path.
55 |Available when FF_FS_RPATH == 2.
61 |
67 | FRESULT fr;
68 | TCHAR str[SZ_STR];
69 |
70 | fr = f_getcwd(str, SZ_STR); /* Get current directory path */
71 |
72 |
73 | The f_puts function writes a string to the file.
17 |18 | int f_puts ( 19 | const TCHAR* str, /* [IN] String */ 20 | FIL* fp /* [IN] File object */ 21 | ); 22 |23 |
When the string was written successfuly, it returns number of character encoding units written to the file. When the function failed due to disk full or any error, a negative value will be returned.
39 |When FatFs is configured for Unicode API (FF_LFN_UNICODE >= 1), character encoding on the string fuctions, f_putc, f_puts, f_printf and f_gets function, is also switched to Unicode. The input Unicode characters in multiple encoding unit, such as surrogate pair and multi-byte sequence, should not be divided into two function calls, or the character will be lost. The character encoding on the file to be written via this functions is selected by FF_STRF_ENCODE. The characters with wrong encoding or invalid for the output encoding will be lost.
45 |This is a wrapper function of f_write function. Available when FF_FS_READONLY == 0 and FF_USE_STRFUNC >= 1. When FF_USE_STRFUNC == 2, '\n's contained in the input string are output as '\r'+'\n' each.
51 |The f_setcp function sets the active code page.
17 |18 | FRESULT f_setcp ( 19 | WORD cp /* [IN] Code page to be set */ 20 | ); 21 |22 |
| Value | Meaning |
|---|---|
| 0 | Initial value (any extended character cannot be used) |
| 437 | U.S. |
| 720 | Arabic |
| 737 | Greek |
| 771 | KBL |
| 775 | Baltic |
| 850 | Latin 1 |
| 852 | Latin 2 |
| 855 | Cyrillic |
| 857 | Turkish |
| 860 | Portuguese |
| 861 | Icelandic |
| 862 | Hebrew |
| 863 | Canadian French |
| 864 | Arabic |
| 865 | Nordic |
| 866 | Russian |
| 869 | Greek 2 |
| 932 | Japanese (DBCS) |
| 936 | Simplified Chinese (DBCS) |
| 949 | Korean (DBCS) |
| 950 | Traditional Chinese (DBCS) |
62 | FR_OK, 63 | FR_INVALID_PARAMETER 64 |
65 |The f_setcp function sets the active code page for the path name. Also code conversion of string functions will be affected by the setting of code page when FF_LFN_UNICODE >= 1 and FF_STRF_ENCODE == 0. Because the initial setting of the code page is 0 and API function with extended character will not work properly, a valid code page needs to be set on the system start-up and it should not be changed on the fly.
71 |Available when FF_CODE_PAGE == 0.
77 |The f_read function reads data from a file.
17 |18 | FRESULT f_read ( 19 | FIL* fp, /* [IN] File object */ 20 | void* buff, /* [OUT] Buffer to store read data */ 21 | UINT btr, /* [IN] Number of bytes to read */ 22 | UINT* br /* [OUT] Number of bytes read */ 23 | ); 24 |25 |
45 | FR_OK, 46 | FR_DISK_ERR, 47 | FR_INT_ERR, 48 | FR_DENIED, 49 | FR_INVALID_OBJECT, 50 | FR_TIMEOUT 51 |
52 |The function starts to read data from the file at the file offset pointed by read/write pointer. The read/write pointer advances as number of bytes read. After the function succeeded, *br should be checked to detect end of the file. In case of *br < btr, it means the read/write pointer hit end of the file during read operation.
58 |Always available.
64 |Refer to the example in f_open.
70 |The f_gets reads a string from the file.
17 |18 | TCHAR* f_gets ( 19 | TCHAR* buff, /* [OUT] Read buffer */ 20 | int len, /* [IN] Size of the read buffer */ 21 | FIL* fp /* [IN] File object */ 22 | ); 23 |24 |
When the function succeeded, buff will be returuned.
42 |The read operation continues until a '\n' is stored, reached end of the file or the buffer is filled with len - 1 characters. The read string is terminated with a '\0'. When no character to read or any error occured during read operation, it returns a null pointer. The status of EOF and error can be examined with f_eof and f_error function.
48 |When FatFs is configured to Unicode API (FF_LFN_UNICODE >= 1), data types on the srting fuctions, f_putc, f_puts, f_printf and f_gets, is also switched to Unicode. The character encoding on the file to be read via this function is assumed as FF_STRF_ENCODE. If the character encoding on the file differs from that on the API, it is converted in this function. In this case, input characters with wrong encoding will be lost.
49 |This is a wrapper function of f_read function. Available when FF_USE_STRFUNC >= 1. When it is set to 2, '\r's contained in the file are stripped out.
55 |The f_chmod function changes the attribute of a file or sub-directory.
17 |18 | FRESULT f_chmod ( 19 | const TCHAR* path, /* [IN] Object name */ 20 | BYTE attr, /* [IN] Attribute flags */ 21 | BYTE mask /* [IN] Attribute masks */ 22 | ); 23 |24 |
| Attribute | Description |
|---|---|
| AM_RDO | Read only |
| AM_ARC | Archive |
| AM_SYS | System |
| AM_HID | Hidden |
50 | FR_OK, 51 | FR_DISK_ERR, 52 | FR_INT_ERR, 53 | FR_NOT_READY, 54 | FR_NO_FILE, 55 | FR_NO_PATH, 56 | FR_INVALID_NAME, 57 | FR_WRITE_PROTECTED, 58 | FR_INVALID_DRIVE, 59 | FR_NOT_ENABLED, 60 | FR_NO_FILESYSTEM, 61 | FR_TIMEOUT, 62 | FR_NOT_ENOUGH_CORE 63 |
64 |The f_chmod function changes the attribute of a file or sub-directory.
70 |Available when FF_FS_READONLY == 0 and FF_USE_CHMOD == 1.
76 |
82 | /* Set Read-only, clear Archive and others are left unchanged. */
83 | f_chmod("file.txt", AM_RDO, AM_RDO | AM_ARC);
84 |
85 | The f_utime function changes the timestamp of a file or sub-directory.
17 |18 | FRESULT f_utime ( 19 | const TCHAR* path, /* [IN] Object name */ 20 | const FILINFO* fno /* [IN] Time and data to be set */ 21 | ); 22 |23 |
39 | FR_OK, 40 | FR_DISK_ERR, 41 | FR_INT_ERR, 42 | FR_NOT_READY, 43 | FR_NO_FILE, 44 | FR_NO_PATH, 45 | FR_INVALID_NAME, 46 | FR_WRITE_PROTECTED, 47 | FR_INVALID_DRIVE, 48 | FR_NOT_ENABLED, 49 | FR_NO_FILESYSTEM, 50 | FR_TIMEOUT, 51 | FR_NOT_ENOUGH_CORE 52 |
53 |The f_utime function changes the timestamp of a file or sub-directory
59 |
65 | FRESULT set_timestamp (
66 | char *obj, /* Pointer to the file name */
67 | int year,
68 | int month,
69 | int mday,
70 | int hour,
71 | int min,
72 | int sec
73 | )
74 | {
75 | FILINFO fno;
76 |
77 | fno.fdate = (WORD)(((year - 1980) * 512U) | month * 32U | mday);
78 | fno.ftime = (WORD)(hour * 2048U | min * 32U | sec / 2U);
79 |
80 | return f_utime(obj, &fno);
81 | }
82 |
83 | Available when FF_FS_READONLY == 0 and FF_USE_CHMOD == 1.
89 |The f_getlabel function returns volume label and volume serial number of a volume.
17 |18 | FRESULT f_getlabel ( 19 | const TCHAR* path, /* [IN] Drive number */ 20 | TCHAR* label, /* [OUT] Volume label */ 21 | DWORD* vsn /* [OUT] Volume serial number */ 22 | ); 23 |24 |
| Configuration | FF_FS_EXFAT == 0 | FF_FS_EXFAT == 1 |
| FF_USE_LFN == 0 | 12 items | - |
| FF_LFN_UNICODE == 0 | 12 items | 23 items |
| FF_LFN_UNICODE == 1,3 | 12 items | 12 items |
| FF_LFN_UNICODE == 2 | 34 items | 34 items |
50 | FR_OK, 51 | FR_DISK_ERR, 52 | FR_INT_ERR, 53 | FR_NOT_READY, 54 | FR_INVALID_DRIVE, 55 | FR_NOT_ENABLED, 56 | FR_NO_FILESYSTEM, 57 | FR_TIMEOUT 58 |
59 |Available when FF_USE_LABEL == 1.
65 |71 | char str[12]; 72 | 73 | /* Get volume label of the default drive */ 74 | f_getlabel("", str, 0); 75 | 76 | /* Get volume label of the drive 2 */ 77 | f_getlabel("2:", str, 0); 78 |79 |
The f_chdir function changes the current directory of the logical drive.
17 |18 | FRESULT f_chdir ( 19 | const TCHAR* path /* [IN] Path name */ 20 | ); 21 |22 |
36 | FR_OK, 37 | FR_DISK_ERR, 38 | FR_INT_ERR, 39 | FR_NOT_READY, 40 | FR_NO_PATH, 41 | FR_INVALID_NAME, 42 | FR_INVALID_DRIVE, 43 | FR_NOT_ENABLED, 44 | FR_NO_FILESYSTEM, 45 | FR_TIMEOUT, 46 | FR_NOT_ENOUGH_CORE 47 |
48 |The f_chdir function changes the current directory of the logical drive. Also the current drive will be changed when in Unix style drive prefix, FF_STR_VOLUME_ID == 2. The current directory of each logical drive is initialized to the root directory on mount.
54 |Note that the current directory is retained in the each file system object and the current drive is retained in a static variable, so that it also affects other tasks that use the file functions.
55 |Available when FF_FS_RPATH >= 1.
61 |67 | /* Change current direcoty of the current drive ("dir1" under root directory) */ 68 | f_chdir("/dir1"); 69 | 70 | /* Change current direcoty of current drive (parent directory of drive 2) */ 71 | f_chdir("2:.."); 72 | 73 | /* Change current direcoty of the drive "sdcard" (at DOS/Windows style volume ID) */ 74 | f_chdir("sdcard:/dir1"); 75 | 76 | /* Change current direcoty of the drive "flash" and set it as current drive (at Unix style volume ID) */ 77 | f_chdir("/flash/dir1"); 78 |79 |
The f_write writes data to a file.
17 |18 | FRESULT f_write ( 19 | FIL* fp, /* [IN] Pointer to the file object structure */ 20 | const void* buff, /* [IN] Pointer to the data to be written */ 21 | UINT btw, /* [IN] Number of bytes to write */ 22 | UINT* bw /* [OUT] Pointer to the variable to return number of bytes written */ 23 | ); 24 |25 |
45 | FR_OK, 46 | FR_DISK_ERR, 47 | FR_INT_ERR, 48 | FR_DENIED, 49 | FR_INVALID_OBJECT, 50 | FR_TIMEOUT 51 |
52 |The function starts to write data to the file at the file offset pointed by read/write pointer. The read/write pointer advances as number of bytes written. After the function succeeded, *bw should be checked to detect the disk full. In case of *bw < btw, it means the volume got full during the write operation. The function can take a time when the volume is full or close to full.
58 |Available when FF_FS_READONLY == 0.
64 |Refer to the example in f_open.
70 |The f_rename function renames and/or moves a file or sub-directory.
17 |18 | FRESULT f_rename ( 19 | const TCHAR* old_name, /* [IN] Old object name */ 20 | const TCHAR* new_name /* [IN] New object name */ 21 | ); 22 |23 |
39 | FR_OK, 40 | FR_DISK_ERR, 41 | FR_INT_ERR, 42 | FR_NOT_READY, 43 | FR_NO_FILE, 44 | FR_NO_PATH, 45 | FR_INVALID_NAME, 46 | FR_EXIST, 47 | FR_WRITE_PROTECTED, 48 | FR_INVALID_DRIVE, 49 | FR_NOT_ENABLED, 50 | FR_NO_FILESYSTEM, 51 | FR_TIMEOUT, 52 | FR_LOCKED, 53 | FR_NOT_ENOUGH_CORE 54 |
55 |Renames a file or sub-directory and can also move it to other directory in the same volume. The object to be renamed must not be an open object, or the FAT volume can be collapsed. Such the wrong operation is rejected safely when file lock function is enabled.
61 |Available when FF_FS_READONLY == 0 and FF_FS_MINIMIZE == 0.
67 |73 | /* Rename an object in the default drive */ 74 | f_rename("oldname.txt", "newname.txt"); 75 | 76 | /* Rename an object in the drive 2 */ 77 | f_rename("2:oldname.txt", "newname.txt"); 78 | 79 | /* Rename an object and move it to another directory in the drive */ 80 | f_rename("log.txt", "old/log0001.txt"); 81 |82 |
The f_sync function flushes the cached information of a writing file.
17 |18 | FRESULT f_sync ( 19 | FIL* fp /* [IN] File object */ 20 | ); 21 |22 |
36 | FR_OK, 37 | FR_DISK_ERR, 38 | FR_INT_ERR, 39 | FR_INVALID_OBJECT, 40 | FR_TIMEOUT 41 |
42 |The f_sync function performs the same process as f_close function but the file is left opened and can continue read/write/seek operations to the file. This is suitable for the applications that open files for a long time in write mode, such as data logger. Performing f_sync function in certain interval can minimize the risk of data loss due to a sudden blackout, wrong media removal or unrecoverable disk error. For more information, refer to application note.
48 |49 | Case 1. Normal write sequence 50 | 51 | Time --> ↓Normal shutdown 52 | OwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwC <Power off> 53 | 54 | 55 | Case 2. Without using f_sync() 56 | 57 | Time --> ↓System crush 58 | Owwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww 59 | |<--------------- All data written will be lost ------------------>| 60 | 61 | 62 | Case 3. Using f_sync() 63 | Time --> ↓System crush 64 | OwwwwwwwwSwwwwwwwwSwwwwwwwwSwwwwwwwwSwwwwwwwwSwwwwwwwwSwwwwwwwwSwwwww 65 | Data after last f_sync will be lost |<->| 66 | O - f_open() 67 | C - f_close() 68 | w - f_write() 69 | S - f_sync() 70 |71 |
However there is no sense in f_sync function immediataly before f_close function because it performs f_sync function in it. In other words, the differnce between those functions is that the file object is invalidated or not.
72 |Available when FF_FS_READONLY == 0.
78 |The FILINFO structure holds information about the object retrieved by f_readdir, f_findfirst, f_findnext and f_stat function. Be careful in the size of structure when LFN is enabled.
17 |18 | typedef struct { 19 | FSIZE_t fsize; /* File size */ 20 | WORD fdate; /* Last modified date */ 21 | WORD ftime; /* Last modified time */ 22 | BYTE fattrib; /* Attribute */ 23 | #if FF_USE_LFN 24 | TCHAR altname[FF_SFN_BUF + 1]; /* Alternative object name */ 25 | TCHAR fname[FF_LFN_BUF + 1]; /* Primary object name */ 26 | #else 27 | TCHAR fname[12 + 1]; /* Object name */ 28 | #endif 29 | } FILINFO; 30 |31 |
| Flag | Meaning |
|---|---|
| AM_RDO | Read-only. Write mode open and deleting is rejected. |
| AM_HID | Hidden. Should not be shown in normal directory listing. |
| AM_SYS | System. Used by system and should not be accessed. |
| AM_ARC | Archive. Set on new creation or any modification to the file. |
| AM_DIR | Directory. This is not a file but a sub-directory container. |
The f_setlabel function sets/removes the label of a volume.
17 |18 | FRESULT f_setlabel ( 19 | const TCHAR* label /* [IN] Volume label to be set */ 20 | ); 21 |22 |
36 | FR_OK, 37 | FR_DISK_ERR, 38 | FR_INT_ERR, 39 | FR_NOT_READY, 40 | FR_INVALID_NAME, 41 | FR_WRITE_PROTECTED, 42 | FR_INVALID_DRIVE, 43 | FR_NOT_ENABLED, 44 | FR_NO_FILESYSTEM, 45 | FR_TIMEOUT 46 |
47 |When the string has a drive prefix, the volume label will be set to the volume specified by the drive prefix. Unix style volume ID cannot be used to specify the volume. If drive number is not specified, the volume label will be set to the default drive. If length of the given volume label is zero, the volume label on the volume will be removed. The format of the volume label is as shown below:
53 |Remark: The standard system (Windows) has a problem at the volume label with a heading \xE5 on the FAT volume. To avoid this problem, this function rejects such volume label as invalid name.
61 |Available when FF_FS_READONLY == 0 and FF_USE_LABEL == 1.
66 |72 | /* Set volume label to the default drive */ 73 | f_setlabel("DATA DISK"); 74 | 75 | /* Set volume label to the drive 2 */ 76 | f_setlabel("2:DISK 3 OF 4"); 77 | 78 | /* Remove volume label of the drive 2 */ 79 | f_setlabel("2:"); 80 |81 |
The f_getfree function gets number of the free clusters on the volume.
17 |18 | FRESULT f_getfree ( 19 | const TCHAR* path, /* [IN] Logical drive number */ 20 | DWORD* nclst, /* [OUT] Number of free clusters */ 21 | FATFS** fatfs /* [OUT] Corresponding filesystem object */ 22 | ); 23 |24 |
42 | FR_OK, 43 | FR_DISK_ERR, 44 | FR_INT_ERR, 45 | FR_NOT_READY, 46 | FR_INVALID_DRIVE, 47 | FR_NOT_ENABLED, 48 | FR_NO_FILESYSTEM, 49 | FR_TIMEOUT 50 |
51 |The f_getfree function gets number of free clusters on the volume. The member csize in the filesystem object indicates number of sectors per cluster, so that the free space in unit of sector can be calcurated with this information. In case of FSINFO structure on the FAT32 volume is not in sync, this function can return an incorrect free cluster count. To avoid this problem, FatFs can be forced full FAT scan by FF_FS_NOFSINFO option.
57 |Available when FF_FS_READONLY == 0 and FF_FS_MINIMIZE == 0.
63 |69 | FATFS *fs; 70 | DWORD fre_clust, fre_sect, tot_sect; 71 | 72 | 73 | /* Get volume information and free clusters of drive 1 */ 74 | res = f_getfree("1:", &fre_clust, &fs); 75 | if (res) die(res); 76 | 77 | /* Get total sectors and free sectors */ 78 | tot_sect = (fs->n_fatent - 2) * fs->csize; 79 | fre_sect = fre_clust * fs->csize; 80 | 81 | /* Print the free space (assuming 512 bytes/sector) */ 82 | printf("%10lu KiB total drive space.\n%10lu KiB available.\n", tot_sect / 2, fre_sect / 2); 83 |84 |
The disk_write function is called to write data to the storage device.
17 |18 | DRESULT disk_write ( 19 | BYTE pdrv, /* [IN] Physical drive number */ 20 | const BYTE* buff, /* [IN] Pointer to the data to be written */ 21 | LBA_t sector, /* [IN] Sector number to write from */ 22 | UINT count /* [IN] Number of sectors to write */ 23 | ); 24 |25 |
The specified memory address is not that always aligned to word boundary because the argument is defined as BYTE*. For more information, refer to the description of disk_read function.
62 |Generally, a multiple sector write request (count > 1) must not be split into single sector transactions to the storage device, or the file write throughput will be drastically decreased.
63 |FatFs expects delayed write function of the disk control layer. The write operation to the media does not need to be completed when return from this function by what write operation is in progress or data is only stored into the write-back cache. But write data on the buff is invalid after return from this function. The write completion request is done by CTRL_SYNC command of disk_ioctl function. Therefore, if a delayed write function is implemented, the write throughput of the filesystem will be improved.
64 |Remarks: Application program MUST NOT call this function, or FAT structure on the volume can be collapsed.
65 |This function is not needed when FF_FS_READONLY == 1.
71 |The FATFS structure (filesystem object) holds dynamic work area of individual logical drives. It is given by application program and registerd/unregisterd to the FatFs module with f_mount function. Initialization of the structure is done by volume mount process whenever necessary. Application program must not modify any member in this structure, or the FAT volume will be collapsed.
17 |18 | typedef struct { 19 | BYTE fs_type; /* FAT type (0, FS_FAT12, FS_FAT16, FS_FAT32 or FS_EXFAT) */ 20 | BYTE pdrv; /* Hosting physical drive of this volume */ 21 | BYTE n_fats; /* Number of FAT copies (1,2) */ 22 | BYTE wflag; /* win[] flag (b0:win[] is dirty) */ 23 | BYTE fsi_flag; /* FSINFO flags (b7:Disabled, b0:Dirty) */ 24 | WORD id; /* Volume mount ID */ 25 | WORD n_rootdir; /* Number of root directory entries (FAT12/16) */ 26 | WORD csize; /* Sectors per cluster */ 27 | #if FF_MAX_SS != FF_MIN_SS 28 | WORD ssize; /* Sector size (512,1024,2048 or 4096) */ 29 | #endif 30 | #if FF_FS_EXFAT 31 | BYTE* dirbuf; /* Directory entry block scratchpad buffer */ 32 | #endif 33 | #if FF_FS_REENTRANT 34 | FF_SYNC_t sobj; /* Identifier of sync object */ 35 | #endif 36 | #if !FF_FS_READONLY 37 | DWORD last_clust; /* FSINFO: Last allocated cluster (0xFFFFFFFF if invalid) */ 38 | DWORD free_clust; /* FSINFO: Number of free clusters (0xFFFFFFFF if invalid) */ 39 | #endif 40 | #if FF_FS_RPATH 41 | DWORD cdir; /* Cluster number of current directory (0:root) */ 42 | #if FF_FS_EXFAT 43 | DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */ 44 | DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */ 45 | DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */ 46 | #endif 47 | #endif 48 | DWORD n_fatent; /* Number of FAT entries (Number of clusters + 2) */ 49 | DWORD fsize; /* Sectors per FAT */ 50 | LBA_t volbase; /* Volume base LBA */ 51 | LBA_t fatbase; /* FAT base LBA */ 52 | LBA_t dirbase; /* Root directory base (LBA|Cluster) */ 53 | LBA_t database; /* Data base LBA */ 54 | LBA_t winsect; /* Sector LBA appearing in the win[] */ 55 | BYTE win[FF_MAX_SS]; /* Disk access window for directory, FAT (and file data at tiny cfg) */ 56 | } FATFS; 57 |58 |
The disk_read function is called to read data from the storage device.
17 |18 | DRESULT disk_read ( 19 | BYTE pdrv, /* [IN] Physical drive number */ 20 | BYTE* buff, /* [OUT] Pointer to the read data buffer */ 21 | LBA_t sector, /* [IN] Start sector number */ 22 | UINT count /* [IN] Number of sectros to read */ 23 | ); 24 |25 |
Read/write operation to the generic storage devices, such as memory card, hadddisk and optical disk, is done in unit of block of data bytes called sector. FatFs supports the sector size in range of 512 to 4096 bytes. When FatFs is configured for fixed sector size (FF_MIN_SS == FF_MAX_SS, this is the most case), the generic read/write function must work at this sector size only. When FatFs is configured for variable sector size (FF_MIN_SS < FF_MAX_SS), the sector size of medium is inquired with disk_ioctl function after disk_initialize function succeeds.
60 |There are some considerations about the memory addres passed via buff. It is not that always aligned with the word boundary, because the argument is defined as BYTE*. The unaligned transfer request can occure at direct transfer. If the bus architecture, especially DMA controller, does not allow unaligned memory access, it should be solved in this function. If it is the case, there are some workarounds described below to avoid this issue.
61 |Also the memory area may be out of reach in DMA. This is the case if it is located on the tightly coupled memory which is usually used for stack. Use double buffered transfer, or avoid to define file I/O buffer, FATFS and FIL structure as local variables where on the stack.
67 |Generally, a multiple sector read request must not be split into single sector transactions to the storage device, or read throughput gets worse.
68 |The f_stat function checks the existence of a file or sub-directory.
17 |18 | FRESULT f_stat ( 19 | const TCHAR* path, /* [IN] Object name */ 20 | FILINFO* fno /* [OUT] FILINFO structure */ 21 | ); 22 |23 |
39 | FR_OK, 40 | FR_DISK_ERR, 41 | FR_INT_ERR, 42 | FR_NOT_READY, 43 | FR_NO_FILE, 44 | FR_NO_PATH, 45 | FR_INVALID_NAME, 46 | FR_INVALID_DRIVE, 47 | FR_NOT_ENABLED, 48 | FR_NO_FILESYSTEM, 49 | FR_TIMEOUT, 50 | FR_NOT_ENOUGH_CORE 51 |
52 |The f_stat function checks the existence of a file or sub-directory in the directory. If it is not exist, the function returns with FR_NO_FILE. If it is exist, the function returns with FR_OK and the informations about the object, size, timestamp and attribute, is stored to the file information structure. For details of the file information, refer to the FILINFO structure and f_readdir function.
58 |Note that the file information comes from the meta data in the directory. If the file has been opend and modified, the file will need to be synched or closed in order to obtain the latest file information.
59 |Available when FF_FS_MINIMIZE == 0.
65 |
71 | FRESULT fr;
72 | FILINFO fno;
73 | const char *fname = "file.txt";
74 |
75 |
76 | printf("Test for \"%s\"...\n", fname);
77 |
78 | fr = f_stat(fname, &fno);
79 | switch (fr) {
80 |
81 | case FR_OK:
82 | printf("Size: %lu\n", fno.fsize);
83 | printf("Timestamp: %u-%02u-%02u, %02u:%02u\n",
84 | (fno.fdate >> 9) + 1980, fno.fdate >> 5 & 15, fno.fdate & 31,
85 | fno.ftime >> 11, fno.ftime >> 5 & 63);
86 | printf("Attributes: %c%c%c%c%c\n",
87 | (fno.fattrib & AM_DIR) ? 'D' : '-',
88 | (fno.fattrib & AM_RDO) ? 'R' : '-',
89 | (fno.fattrib & AM_HID) ? 'H' : '-',
90 | (fno.fattrib & AM_SYS) ? 'S' : '-',
91 | (fno.fattrib & AM_ARC) ? 'A' : '-');
92 | break;
93 |
94 | case FR_NO_FILE:
95 | printf("\"%s\" is not exist.\n", fname);
96 | break;
97 |
98 | default:
99 | printf("An error occured. (%d)\n", fr);
100 | }
101 |
102 | The f_fdisk fucntion divides a physical drive.
17 |18 | FRESULT f_fdisk ( 19 | BYTE pdrv, /* [IN] Physical drive number */ 20 | const LBA_t ptbl[], /* [IN] Partition map table */ 21 | void* work /* [IN] Work area */ 22 | ); 23 |24 |
41 | FR_OK, 42 | FR_DISK_ERR, 43 | FR_NOT_READY, 44 | FR_WRITE_PROTECTED, 45 | FR_INVALID_PARAMETER, 46 | FR_NOT_ENOUGH_CORE 47 |
48 |The f_fdisk function creates partitions on the physical drive. The partitioning format can be in generic MBR or GPT. The partition map table specifies how to divide the physical drive. The first item specifies the size of the first partition and the partitions are located on the drive in order of from the first item. When the value of item is less than or equal to 100, it specifies the partition size in percentage of the entire drive space. When it is larger than 100, it specifies number of sectors. The partition map table is terminated by a zero, no space is remaining for next allocation or 4th partition is created in MBR format. If the specified size is larger than remaining space on the drive, the partition is truncated at end of the drive.
53 |By default, partitions are created in MBR format. It can create upto four primary partitions on a drive. GPT format is used to create the partitions when 64-bit LBA is enabled (FF_LBA64 = 1) and the drive size is equal to or larger than FF_MIN_GPT sectors. It can create over ten partitions on a drive.
54 |Available when FF_FS_READOLNY == 0, FF_USE_MKFS == 1 and FF_MULTI_PARTITION == 1.
59 |64 | /* Volume mapping table defined by user (required when FF_MULTI_PARTITION == 1) */ 65 | 66 | PARTITION VolToPart[FF_VOLUMES] = { 67 | {0, 1}, /* "0:" ==> 1st partition in PD#0 */ 68 | {0, 2}, /* "1:" ==> 2nd partition in PD#0 */ 69 | {1, 0} /* "2:" ==> PD#1 as removable drive */ 70 | }; 71 |72 |
73 | /* Initialize a brand-new disk drive mapped to physical drive 0 */ 74 | 75 | BYTE work[FF_MAX_SS]; /* Working buffer */ 76 | LBA_t plist[] = {50, 50, 0}; /* Divide the drive into two partitions */ 77 | /* {0x10000000, 100}; 256M sectors for 1st partition and left all for 2nd partition */ 78 | /* {20, 20, 20, 0}; 20% for 3 partitions each and remaing space is left not allocated */ 79 | 80 | f_fdisk(0, plist, work); /* Divide physical drive 0 */ 81 | 82 | f_mkfs("0:", 0, work, sizeof work); /* Create FAT volume on the logical drive 0 */ 83 | f_mkfs("1:", 0, work, sizeof work); /* Create FAT volume on the logical drive 1 */ 84 | 85 |86 |
The f_expand function prepares or allocates a contiguous data area to the file.
17 | 18 |19 | FRESULT f_expand ( 20 | FIL* fp, /* [IN] File object */ 21 | FSIZE_t fsz, /* [IN] File size expanded to */ 22 | BYTE opt /* [IN] Allocation mode */ 23 | ); 24 |25 |
43 | FR_OK, 44 | FR_DISK_ERR, 45 | FR_INT_ERR, 46 | FR_INVALID_OBJECT, 47 | FR_DENIED, 48 | FR_TIMEOUT 49 |
50 |The f_expand function prepares or allocates a contiguous data area to the file. When opt is 1, the data area is allocated to the file in this function. Unlike expansion of file size by f_lseek function, the file must be truncated prior to use this function and read/write pointer of the file stays at offset 0 after the function call. The file content allocated with this function is undefined, because no data is written to the file in this process. The function can fail with FR_DENIED due to some reasons below.
56 |When opt is 0, the function finds a contiguous data area and set it as suggested point for next allocation. The subsequent cluster allocation begins at top of the contiguous area found by this function. Thus the file allocation is guaranteed be contiguous and without allocation delay until the file size reaches this size unless any other changes to the volume is performed.
63 |The contiguous file has an advantage for time-critical read/write operations. It eliminates some overheads in the filesystem and the storage device caused by random access for fragmented file.
64 |Also the contiguous file can be easily accessed directly via low-level disk functions. However, this is not recommended in consideration of portability and future compatibility. If the file has not been confirmed be contiguous, use this function to examine if the file is contiguous or not.
65 |Available when FF_USE_EXPAND == 1 and FF_FS_READONLY == 0.
70 |76 | /* Creating a contiguous file */ 77 | 78 | /* Create a new file */ 79 | res = f_open(fp = malloc(sizeof (FIL)), "file.dat", FA_WRITE|FA_CREATE_ALWAYS); 80 | if (res) { /* Check if the file has been opened */ 81 | free(fp); 82 | die("Failed to open the file."); 83 | } 84 | 85 | /* Alloacte a 100 MiB of contiguous area to the file */ 86 | res = f_expand(fp, 104857600, 1); 87 | if (res) { /* Check if the file has been expanded */ 88 | f_close(fp); 89 | free(fp); 90 | die("Failed to allocate contiguous area."); 91 | } 92 | 93 | /* Now you have a contiguous file accessible with fp */ 94 | 95 |96 |
97 | /* Accessing the contiguous file via low-level disk functions */ 98 | 99 | /* Get physical location of the file data */ 100 | drv = fp->obj.fs->pdrv; 101 | lba = fp->obj.fs->database + fp->obj.fs->csize * (fp->obj.sclust - 2); 102 | 103 | /* Write 2048 sectors from top of the file at a time */ 104 | res = disk_write(drv, buffer, lba, 2048); 105 | 106 |107 |
The f_forward function reads the file data and forward it to the data streaming device.
17 |18 | FRESULT f_forward ( 19 | FIL* fp, /* [IN] File object */ 20 | UINT (*func)(const BYTE*,UINT), /* [IN] Data streaming function */ 21 | UINT btf, /* [IN] Number of bytes to forward */ 22 | UINT* bf /* [OUT] Number of bytes forwarded */ 23 | ); 24 |25 |
45 | FR_OK, 46 | FR_DISK_ERR, 47 | FR_INT_ERR, 48 | FR_INVALID_OBJECT, 49 | FR_DENIED, 50 | FR_TIMEOUT 51 |
52 |The f_forward function reads the data from the file and forward it to the outgoing stream. This function is suitable for small memory system, because it does not require any data buffer in the application module. The file pointer of the file object advances in number of bytes forwarded. In case of *bf is less than btf without error, it means the requested size of data could not be transferred due to end of file or stream goes busy during data transfer.
58 |Available when FF_USE_FORWARD == 1.
64 |70 | /*------------------------------------------------------------------------*/ 71 | /* Sample code of data transfer function to be called back from f_forward */ 72 | /*------------------------------------------------------------------------*/ 73 | 74 | UINT out_stream ( /* Returns number of bytes sent or stream status */ 75 | const BYTE *p, /* Pointer to the data block to be sent */ 76 | UINT btf /* >0: Transfer call (Number of bytes to be sent). 0: Sense call */ 77 | ) 78 | { 79 | UINT cnt = 0; 80 | 81 | 82 | if (btf == 0) { /* Sense call */ 83 | /* Return stream status (0: Busy, 1: Ready) */ 84 | /* When once it returned ready to sense call, it must accept a byte at least */ 85 | /* at subsequent transfer call, or f_forward will fail with FR_INT_ERR. */ 86 | if (FIFO_READY) cnt = 1; 87 | } 88 | else { /* Transfer call */ 89 | do { /* Repeat while there is any data to be sent and the stream is ready */ 90 | FIFO_PORT = *p++; 91 | cnt++; 92 | } while (cnt < btf && FIFO_READY); 93 | } 94 | 95 | return cnt; 96 | } 97 | 98 | 99 | /*------------------------------------------------------------------------*/ 100 | /* Sample code using f_forward function */ 101 | /*------------------------------------------------------------------------*/ 102 | 103 | FRESULT play_file ( 104 | char *fn /* Pointer to the audio file name to be played */ 105 | ) 106 | { 107 | FRESULT rc; 108 | FIL fil; 109 | UINT dmy; 110 | 111 | /* Open the audio file in read only mode */ 112 | rc = f_open(&fil, fn, FA_READ); 113 | if (rc) return rc; 114 | 115 | /* Repeat until the file pointer reaches end of the file */ 116 | while (rc == FR_OK && !f_eof(&fil)) { 117 | 118 | /* some processes... */ 119 | 120 | /* Fill output stream periodicaly or on-demand */ 121 | rc = f_forward(&fil, out_stream, 1000, &dmy); 122 | } 123 | 124 | /* Close the file and return */ 125 | f_close(&fil); 126 | return rc; 127 | } 128 |129 |
The f_findfirst function searches a directroy for an item.
17 |18 | FRESULT f_findfirst ( 19 | DIR* dp, /* [OUT] Poninter to the directory object */ 20 | FILINFO* fno, /* [OUT] Pointer to the file information structure */ 21 | const TCHAR* path, /* [IN] Pointer to the directory name to be opened */ 22 | const TCHAR* pattern /* [IN] Pointer to the matching pattern string */ 23 | ); 24 |25 |
45 | FR_OK, 46 | FR_DISK_ERR, 47 | FR_INT_ERR, 48 | FR_NOT_READY, 49 | FR_NO_PATH, 50 | FR_INVALID_NAME, 51 | FR_INVALID_OBJECT, 52 | FR_INVALID_DRIVE, 53 | FR_NOT_ENABLED, 54 | FR_NO_FILESYSTEM, 55 | FR_TIMEOUT, 56 | FR_NOT_ENOUGH_CORE, 57 | FR_TOO_MANY_OPEN_FILES 58 |
59 |After the directory specified by path could be opened, it starts to search the directory for items with the matching pattern specified by pattern. If the first item is found, the information about the item is stored into the file information structure fno. If not found, fno->fname[] has a null string.
65 |The matching pattern string can contain wildcards. For example:
66 |Since the matching algorithm uses recursion, number of wildcards in the matching pattern is limited to four to limit the stack usage. Any pattern with too many wildcards does not match any name. In LFN configuration, only fname[] is tested when FF_USE_FIND == 1 and also altname[] is tested when FF_USE_FIND == 2. There are some differences listed below between FatFs and standard systems in matching condition.
73 |This is a wrapper function of f_opendir and f_readdir function. Available when FF_USE_FIND >= 1 and FF_FS_MINIMIZE <= 1.
84 |90 | /* Search a directory for objects and display it */ 91 | 92 | void find_image_file (void) 93 | { 94 | FRESULT fr; /* Return value */ 95 | DIR dj; /* Directory object */ 96 | FILINFO fno; /* File information */ 97 | 98 | fr = f_findfirst(&dj, &fno, "", "????????.JPG"); /* Start to search for photo files */ 99 | 100 | while (fr == FR_OK && fno.fname[0]) { /* Repeat while an item is found */ 101 | printf("%s\n", fno.fname); /* Print the object name */ 102 | fr = f_findnext(&dj, &fno); /* Search for next item */ 103 | } 104 | 105 | f_closedir(&dj); 106 | } 107 |108 |
The f_readdir function reads an item of the directory.
17 |18 | FRESULT f_readdir ( 19 | DIR* dp, /* [IN] Directory object */ 20 | FILINFO* fno /* [OUT] File information structure */ 21 | ); 22 |23 |
24 | FRESULT f_rewinddir ( 25 | DIR* dp /* [IN] Directory object */ 26 | ); 27 |28 |
44 | FR_OK, 45 | FR_DISK_ERR, 46 | FR_INT_ERR, 47 | FR_INVALID_OBJECT, 48 | FR_TIMEOUT, 49 | FR_NOT_ENOUGH_CORE 50 |
51 |The f_readdir function reads a directory item, informations about the object, from the open directory. Items in the directory can be read in sequence by f_readdir function calls. When all items in the directory have been read and no item to read, a null string is stored into the fno->fname[] without any error. When a null pointer is given to the fno, the read index of the directory object is rewinded. The f_rewinddir function is implemented as a macro.
57 |58 | #define f_rewinddir(dp) f_readdir((dp), 0) 59 |60 |
When LFN is enabled, a member altname[] is defined in the file information structure to store the short file name of the object. If the long file name is not accessible due to a reason listed below, short file name is stored to the fname[] and the altname[] has a null string.
61 |There is an issue on read directories in exFAT volume. The exFAT does not support short file name. This means no name can be returned on the condition above. If it is the case, "?" is returned as the file name to indicate that the object is not accessible. To avoid this problem, configure FatFs FF_LFN_UNICODE != 0 and FF_MAX_LFN == 255 to support the full feature of LFN specification.
68 |Dot entries ("." and "..") in the sub-directory of FAT volume are filtered out and they will never appear in the read items because exFAT lacks dot entries in the sub-directory.
69 |Available when FF_FS_MINIMIZE <= 1.
75 |81 | FRESULT scan_files ( 82 | char* path /* Start node to be scanned (***also used as work area***) */ 83 | ) 84 | { 85 | FRESULT res; 86 | DIR dir; 87 | UINT i; 88 | static FILINFO fno; 89 | 90 | 91 | res = f_opendir(&dir, path); /* Open the directory */ 92 | if (res == FR_OK) { 93 | for (;;) { 94 | res = f_readdir(&dir, &fno); /* Read a directory item */ 95 | if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */ 96 | if (fno.fattrib & AM_DIR) { /* It is a directory */ 97 | i = strlen(path); 98 | sprintf(&path[i], "/%s", fno.fname); 99 | res = scan_files(path); /* Enter the directory */ 100 | if (res != FR_OK) break; 101 | path[i] = 0; 102 | } else { /* It is a file. */ 103 | printf("%s/%s\n", path, fno.fname); 104 | } 105 | } 106 | f_closedir(&dir); 107 | } 108 | 109 | return res; 110 | } 111 | 112 | 113 | int main (void) 114 | { 115 | FATFS fs; 116 | FRESULT res; 117 | char buff[256]; 118 | 119 | 120 | res = f_mount(&fs, "", 1); 121 | if (res == FR_OK) { 122 | strcpy(buff, "/"); 123 | res = scan_files(buff); 124 | } 125 | 126 | return res; 127 | } 128 |129 |