├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── other-issue.md └── PULL_REQUEST_TEMPLATE.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── SECURITY.md ├── documents ├── 00index_e.html ├── css_e.css ├── doc │ ├── appnote.html │ ├── chdir.html │ ├── chdrive.html │ ├── chmod.html │ ├── close.html │ ├── closedir.html │ ├── config.html │ ├── dinit.html │ ├── dioctl.html │ ├── dread.html │ ├── dstat.html │ ├── dwrite.html │ ├── eof.html │ ├── error.html │ ├── expand.html │ ├── fattime.html │ ├── fdisk.html │ ├── filename.html │ ├── findfirst.html │ ├── findnext.html │ ├── forward.html │ ├── getcwd.html │ ├── getfree.html │ ├── getlabel.html │ ├── gets.html │ ├── lseek.html │ ├── mkdir.html │ ├── mkfs.html │ ├── mount.html │ ├── open.html │ ├── opendir.html │ ├── printf.html │ ├── putc.html │ ├── puts.html │ ├── rc.html │ ├── read.html │ ├── readdir.html │ ├── rename.html │ ├── sdir.html │ ├── setcp.html │ ├── setlabel.html │ ├── sfatfs.html │ ├── sfile.html │ ├── sfileinfo.html │ ├── size.html │ ├── stat.html │ ├── sync.html │ ├── tell.html │ ├── truncate.html │ ├── unlink.html │ ├── utime.html │ └── write.html ├── res │ ├── app1.c │ ├── app2.c │ ├── app3.c │ ├── app4.c │ ├── app5.c │ ├── app6.c │ ├── f1.png │ ├── f2.png │ ├── f3.png │ ├── f4.png │ ├── f5.png │ ├── f6.png │ ├── f7.png │ ├── funcs.png │ ├── layers.png │ ├── layers1.png │ ├── layers2.png │ ├── layers3.png │ ├── mkfatimg.zip │ ├── mkfs.xlsx │ ├── modules.png │ ├── rwtest1.png │ ├── rwtest2.png │ ├── rwtest3.png │ └── uniconv.zip └── updates.html ├── source ├── 00history.txt ├── 00readme.txt ├── diskio.c ├── diskio.h ├── drivers │ ├── sd │ │ ├── sd_diskio.c │ │ ├── sd_diskio.h │ │ ├── sd_diskio_dma_rtos.c │ │ ├── sd_diskio_dma_rtos.h │ │ ├── sd_diskio_dma_standalone.c │ │ └── sd_diskio_dma_standalone.h │ ├── sram │ │ ├── sram_diskio.c │ │ └── sram_diskio.h │ ├── template │ │ ├── sd_diskio_config.h │ │ ├── sram_diskio_config.h │ │ ├── usbh_diskio_config.h │ │ ├── user_diskio.c │ │ └── user_diskio.h │ └── usb_host │ │ ├── usbh_diskio.c │ │ └── usbh_diskio.h ├── ff.c ├── ff.h ├── ff_gen_drv.c ├── ff_gen_drv.h ├── ffconf_template.h ├── ffsystem_baremetal.c ├── ffsystem_cmsis_os.c ├── ffsystem_template.c ├── ffunicode.c └── st_readme.txt └── st_license.txt /.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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at https://www.st.com/content/st_com/en/contact-us.html. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing guide 2 | 3 | This guide serves as a checklist before contributing to this repository. It mainly focuses on the steps to follow to submit an issue or a pull-request. 4 | 5 | ## 1. Issues 6 | 7 | ### 1.1 Before opening an issue 8 | 9 | Please check the following points before posting an issue: 10 | * Make sure you are using the latest commit (major releases are tagged, but corrections are available as new commits). 11 | * Make sure your issue is a question/feedback/suggestions **related to** the software provided in this repository. Otherwise, please refer to section [3](CONTRIBUTING.md#3-support-requests-and-questions) below. 12 | * Make sure your issue is not already reported/fixed on GitHub or discussed on a previous issue. Do not forget to browse into the **closed** issues. 13 | 14 | ### 1.2 Posting the issue 15 | 16 | When you have checked the previous points, create a new report from the **Issues** tab of this repository. A template is available [here](../../issues/new/choose) to help you report the issue you are facing or the enhancement you would like to propose. 17 | 18 | ## 2. Pull Requests 19 | 20 | ### 2.1 Before opening a pull-request 21 | 22 | STMicrolectronics is happy to receive contributions from the community, based on an initial Contributor License Agreement (CLA) procedure. 23 | 24 | * If you are an individual writing original source code and you are sure **you own the intellectual property**, then you need to sign an Individual [CLA](https://cla.st.com). 25 | * If you work for a company that wants also to allow you to contribute with your work, your company needs to provide a Corporate [CLA](https://cla.st.com) mentioning your GitHub account name. 26 | * If you are not sure that a CLA (Individual or Corporate) has been signed for your GitHub account you can check the [CLA](https://cla.st.com) dedicated page. 27 | 28 | Please note that: 29 | * The Corporate CLA will always take precedence over the Individual CLA. 30 | * One CLA submission is sufficient, for any project proposed by STMicroelectronics. 31 | 32 | ### 2.2 How to proceed 33 | 34 | * We recommend to engage first a communication thru an issue, in order to present your proposal, just to confirm that it corresponds to STMicroelectronics' domain or scope. 35 | * Then fork the project to your GitHub account to further develop your contribution. Please use the latest commit version. 36 | * Please, submit one pull-request per new feature or proposal. This will ease the analysis and the final merge if accepted. 37 | 38 | ## 3. Support requests and questions 39 | 40 | For support requests or any other question related to the product, the tools, the environment, you can submit a post to the **ST Community** on the appropriate topic [page](https://community.st.com/s/topiccatalog). 41 | -------------------------------------------------------------------------------- /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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Middleware FatFS MCU Component 2 | 3 | ![latest tag](https://img.shields.io/github/v/tag/STMicroelectronics/stm32_mw_fatfs.svg?color=brightgreen) 4 | 5 | ## Overview 6 | 7 | **STM32Cube** is an STMicroelectronics original initiative to ease developers' life by reducing efforts, time and cost. 8 | 9 | **STM32Cube** covers the overall STM32 products portfolio. It includes a comprehensive embedded software platform delivered for each STM32 series. 10 | * The CMSIS modules (core and device) corresponding to the ARM(tm) core implemented in this STM32 product. 11 | * The STM32 HAL-LL drivers, an abstraction layer offering a set of APIs ensuring maximized portability across the STM32 portfolio. 12 | * The BSP drivers of each evaluation, discovery, or nucleo board provided for this STM32 series. 13 | * A consistent set of middleware libraries such as RTOS, USB, FatFS, graphics, touch sensing library... 14 | * A full set of software projects (basic examples, applications, and demonstrations) for each board provided for this STM32 series. 15 | 16 | Two models of publication are proposed for the STM32Cube embedded software: 17 | * The monolithic **MCU Package**: all STM32Cube software modules of one STM32 series are present (Drivers, Middleware, Projects, Utilities) in the repository (usual name **STM32Cubexx**, xx corresponding to the STM32 series). 18 | * The **MCU component**: each STM32Cube software module being part of the STM32Cube MCU Package, is delivered as an individual repository, allowing the user to select and get only the required software functions. 19 | 20 | ## Description 21 | 22 | This **stm32_mw_fatfs** MCU component repository is one element **common to all** STM32Cube MCU embedded software packages, providing the **FatFS MCU Middleware** part. 23 | 24 | ## Release note 25 | 26 | * Details about the content of this release are available in the release note [here](https://github.com/STMicroelectronics/stm32_mw_fatfs/blob/master/src/00history.txt). 27 | * Details about the updates made by STMicroelectronics are available in the release note [here](https://github.com/STMicroelectronics/stm32_mw_fatfs/blob/master/src/st_readme.txt). 28 | 29 | ## Compatibility information 30 | 31 | Please refer to the release note in the repository of the STM32Cube **firmware** you are using to know which version of this middleware library to use with other components' versions (**e.g.**, other middleware libraries, drivers). It is **crucial** that you use a consistent set of versions. 32 | 33 | ## Troubleshooting 34 | 35 | Please refer to the [CONTRIBUTING.md](CONTRIBUTING.md) guide. 36 | -------------------------------------------------------------------------------- /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/css_e.css: -------------------------------------------------------------------------------- 1 | * {margin: 0; padding: 0; border-width: 0;} 2 | body {margin: 8px; background-color: #e0ffff; font-color: black; font-family: serif; line-height: 133%; max-width: 1024px;} 3 | a:link {color: blue;} 4 | a:visited {color: darkmagenta;} 5 | a:hover {background-color: #a0ffff;} 6 | a:active {color: darkmagenta; overflow: hidden; outline:none; position: relative; top: 1px; left: 1px;} 7 | abbr {border-width: 1px;} 8 | 9 | p {margin: 0 0 0.3em 1em;} 10 | i {margin: 0 0.3em 0 0;} 11 | b {margin: 0 0.1em;} 12 | em {font-style: normal; font-weight: bold; margin: 0 0.1em;} 13 | strong {} 14 | pre {border: 1px dashed gray; margin: 0.5em 1em; padding: 0.5em; line-height: 1.2em; font-size: 85%; font-family: "Consolas", "Courier New", monospace; background-color: white;} 15 | pre span.c {color: green;} 16 | pre span.k {color: blue;} 17 | pre span.e {color: red;} 18 | pre span.b {font-weight: bold;} 19 | pre span.arg {font-style: italic;} 20 | tt {margin: 0 0.2em; font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; } 21 | tt.arg {font-style: italic;} 22 | ol {margin: 0.5em 2.5em;} 23 | ul {margin: 0.5em 2em;} 24 | ul ul {margin: 0 2em 0.5em 1em;} 25 | dl {margin: 0.5em 1em;} 26 | dd {margin: 0 2em;} 27 | dt {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace;} 28 | dl.par dt {margin: 0.5em 0 0 0 ; font-style: italic; } 29 | dl.ret dt {margin: 0.5em 0 0 0 ; font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; font-weight: bold; } 30 | hr {border-width: 1px; margin: 1em;} 31 | div.abst {font-family: sans-serif;} 32 | div.para {clear: both; font-family: serif;} 33 | div.ret a {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; } 34 | .equ {text-indent: 0; margin: 1em 2em 1em;} 35 | .indent {margin-left: 2em;} 36 | .rset {float: right; margin: 0.3em 0 0.5em 0.5em;} 37 | .lset {float: left; margin: 0.3em 0.5em 0.5em 0.5em;} 38 | ul.flat li {list-style-type: none; margin: 0;} 39 | a.imglnk img {border: 1px solid;} 40 | .iequ {white-space: nowrap; font-weight: bold;} 41 | .clr {clear: both;} 42 | .it {font-style: italic;} 43 | .mfd {font-size: 0.7em; padding: 0 1px; border: 1px solid; white-space : nowrap} 44 | .ral {text-align: right; } 45 | .lal {text-align: left; } 46 | .cal {text-align: center; } 47 | 48 | h1 {line-height: 1em; font-size: 2em; font-family: sans-serif; padding: 0.3em 0 0.3em;} 49 | h2 {font-size: 2em; font-family: sans-serif; background-color: #d8d8FF; padding: 0.5em 0.5em; margin: 0 0 0.5em;} 50 | h3 {font-size: 1.5em; font-family: sans-serif; margin: 1.5em 0 0.5em;} 51 | div.doc h3 {border-color: #b0d8d8; border-style: solid; border-width: 0px 0px 4px 12px; padding: 4px; margin-top: 3em;} 52 | h4 {font-size: 1.2em; font-family: sans-serif; margin: 2em 0 0.2em;} 53 | h5 {font-size: 1em; font-family: sans-serif; margin: 1em 0 0em;} 54 | p.hdd {float: right; text-align: right; margin-top: 0.5em;} 55 | hr.hds {clear: both; margin-bottom: 1em;} 56 | kbd {letter-spacing: 0;} 57 | small {font-size: 80%;} 58 | .indent {margin-left: 2em;} 59 | 60 | /* Tables */ 61 | table {margin: 0.5em 1em; border-collapse: collapse; border: 2px solid gray; } 62 | table caption {font-family: sans-serif; font-weight: bold;} 63 | table th {background-color: white; border-style: solid; border-width: 1px 1px 2px; border-color: gray; padding: 0 3px; vertical-align: top; white-space: nowrap;} 64 | table td {background-color: white; border: 1px solid gray; padding: 0 3px; vertical-align: top; line-height: 1.3em;} 65 | table.lst td:first-child {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; white-space: nowrap;} 66 | table.lst2 td {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; white-space: nowrap;} 67 | table.lst3 td {font-family: "Consolas", "Courier New", monospace; white-space: nowrap;} 68 | tr.lst3 td {border-width: 2px 1px 1px; } 69 | table.lst4 td {padding: 0.3em;} 70 | table.lst4 td:nth-child(2) {width: 45%;} 71 | table.lst4 td:nth-child(3) {width: 45%;} 72 | 73 | p.foot {clear: both; text-indent: 0; margin: 1em 0.5em 1em;} 74 | -------------------------------------------------------------------------------- /documents/doc/chdir.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_chdir 10 | 11 | 12 | 13 | 14 |
15 |

f_chdir

16 |

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 |
23 | 24 |
25 |

Parameters

26 |
27 |
path
28 |
Pointer to the null-terminated string that specifies the directory to be set as current directory.
29 |
30 |
31 | 32 | 33 |
34 |

Return Values

35 |

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 |
49 | 50 | 51 |
52 |

Description

53 |

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 |
56 | 57 | 58 |
59 |

QuickInfo

60 |

Available when FF_FS_RPATH >= 1.

61 |
62 | 63 | 64 |
65 |

Example

66 |
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 |
80 | 81 |
82 |

See Also

83 |

f_chdrive, f_getcwd

84 |
85 | 86 |

Return

87 | 88 | 89 | -------------------------------------------------------------------------------- /documents/doc/chdrive.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_chdrive 10 | 11 | 12 | 13 | 14 |
15 |

f_chdrive

16 |

The f_chdrive function changes the current drive.

17 |
18 | FRESULT f_chdrive (
19 |   const TCHAR* path  /* [IN] Logical drive number */
20 | );
21 | 
22 |
23 | 24 |
25 |

Parameters

26 |
27 |
path
28 |
Specifies the logical drive number to be set as the current drive.
29 |
30 |
31 | 32 | 33 |
34 |

Return Values

35 |

36 | FR_OK, 37 | FR_INVALID_DRIVE 38 |

39 |
40 | 41 | 42 |
43 |

Description

44 |

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 |
46 | 47 |
48 |

QuickInfo

49 |

Available when FF_FS_RPATH >= 1.

50 |
51 | 52 | 53 |
54 |

Example

55 |
56 |     f_chdrive("2:");  /* Set drive 2 as current drive */
57 | 
58 |     f_chdrive("");    /* No effect (set current drive as current drive) */
59 | 
60 |
61 | 62 | 63 |
64 |

See Also

65 |

f_chdir, f_getcwd

66 |
67 | 68 |

Return

69 | 70 | 71 | -------------------------------------------------------------------------------- /documents/doc/chmod.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_chmod 10 | 11 | 12 | 13 | 14 |
15 |

f_chmod

16 |

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 |
25 | 26 |
27 |

Parameters

28 |
29 |
path
30 |
Pointer to the null-terminated string that specifies an object to be changed
31 |
attr
32 |
Attribute flags to be set in one or more combination of the following flags. The specified flags are set and others are cleard.
33 | 34 | 35 | 36 | 37 | 38 | 39 |
AttributeDescription
AM_RDORead only
AM_ARCArchive
AM_SYSSystem
AM_HIDHidden
40 |
41 |
mask
42 |
Attribute mask that specifies which attribute is changed. The specified attributes are set or cleard and others are left unchanged.
43 |
44 |
45 | 46 | 47 |
48 |

Return Values

49 |

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 |
65 | 66 | 67 |
68 |

Description

69 |

The f_chmod function changes the attribute of a file or sub-directory.

70 |
71 | 72 | 73 |
74 |

QuickInfo

75 |

Available when FF_FS_READONLY == 0 and FF_USE_CHMOD == 1.

76 |
77 | 78 | 79 |
80 |

Example

81 |
82 |     /* Set Read-only, clear Archive and others are left unchanged. */
83 |     f_chmod("file.txt", AM_RDO, AM_RDO | AM_ARC);
84 | 
85 |
86 | 87 |

Return

88 | 89 | 90 | -------------------------------------------------------------------------------- /documents/doc/close.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_close 10 | 11 | 12 | 13 | 14 |
15 |

f_close

16 |

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 |
23 | 24 |
25 |

Parameter

26 |
27 |
fp
28 |
Pointer to the open file object structure to be closed.
29 |
30 |
31 | 32 | 33 |
34 |

Return Values

35 |

36 | FR_OK, 37 | FR_DISK_ERR, 38 | FR_INT_ERR, 39 | FR_INVALID_OBJECT, 40 | FR_TIMEOUT 41 |

42 |
43 | 44 | 45 |
46 |

Description

47 |

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 |
50 | 51 | 52 |
53 |

QuickInfo

54 |

Always available.

55 |
56 | 57 | 58 |
59 |

See Also

60 |

f_open, f_read, f_write, f_sync, FIL, FATFS

61 |
62 | 63 |

Return

64 | 65 | 66 | -------------------------------------------------------------------------------- /documents/doc/closedir.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_closedir 10 | 11 | 12 | 13 | 14 |
15 |

f_closedir

16 |

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 |
23 | 24 |
25 |

Parameter

26 |
27 |
dp
28 |
Pointer to the open directory object structure to be closed.
29 |
30 |
31 | 32 | 33 |
34 |

Return Values

35 |

36 | FR_OK, 37 | FR_INT_ERR, 38 | FR_INVALID_OBJECT, 39 | FR_TIMEOUT 40 |

41 |
42 | 43 | 44 |
45 |

Description

46 |

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 |
49 | 50 | 51 |
52 |

QuickInfo

53 |

Available when FF_FS_MINIMIZE <= 1.

54 |
55 | 56 | 57 |
58 |

See Also

59 |

f_opendir, f_readdir, DIR

60 |
61 | 62 |

Return

63 | 64 | 65 | -------------------------------------------------------------------------------- /documents/doc/dinit.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - disk_initialize 10 | 11 | 12 | 13 | 14 |
15 |

disk_initialize

16 |

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 |
23 | 24 |
25 |

Parameter

26 |
27 |
pdrv
28 |
Physical drive number to identify the target device. Always zero at single drive system.
29 |
30 |
31 | 32 | 33 |
34 |

Return Values

35 |

This function returns the current drive status flags as the result. For details of the drive status, refer to the disk_status function.

36 |
37 | 38 |
39 |

Description

40 |

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 |
43 | 44 |

Return

45 | 46 | 47 | -------------------------------------------------------------------------------- /documents/doc/dread.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - disk_read 10 | 11 | 12 | 13 | 14 |
15 |

disk_read

16 |

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 |
26 | 27 |
28 |

Parameters

29 |
30 |
pdrv
31 |
Physical drive number to identify the target device.
32 |
buff
33 |
Pointer to the first item of the byte array to store read data. Size of read data will be the sector size * count bytes.
34 |
sector
35 |
Start sector number in LBA. The data type LBA_t is an alias of DWORD or QWORD depends on the configuration option.
36 |
count
37 |
Number of sectors to read.
38 |
39 |
40 | 41 | 42 |
43 |

Return Value

44 |
45 |
RES_OK (0)
46 |
The function succeeded.
47 |
RES_ERROR
48 |
An unrecoverable hard error occured during the read operation.
49 |
RES_PARERR
50 |
Invalid parameter.
51 |
RES_NOTRDY
52 |
The device has not been initialized.
53 |
54 |
55 | 56 | 57 |
58 |

Description

59 |

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 | 66 |

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 |
69 | 70 | 71 |

Return

72 | 73 | 74 | -------------------------------------------------------------------------------- /documents/doc/dstat.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - disk_status 10 | 11 | 12 | 13 | 14 |
15 |

disk_status

16 |

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 |
23 | 24 |
25 |

Parameter

26 |
27 |
pdrv
28 |
Physical drive number to identify the target device. Always zero in single drive system.
29 |
30 |
31 | 32 | 33 |
34 |

Return Values

35 |

The current drive status is returned in combination of status flags described below. FatFs refers only STA_NOINIT and STA_PROTECT.

36 |
37 |
STA_NOINIT
38 |
Indicates that the device has not been initialized and not ready to work. This flag is set on system reset, media removal or failure of disk_initialize function. It is cleared on disk_initialize function succeeded. Any media change that occurs asynchronously must be captured and reflect it to the status flags, or auto-mount function will not work correctly. If the system does not support media change detection, application program needs to explicitly re-mount the volume with f_mount function after each media change.
39 |
STA_NODISK
40 |
Indicates that no medium in the drive. This is always cleared when the drive is non-removable class. Note that FatFs does not refer this flag.
41 |
STA_PROTECT
42 |
Indicates that the medium is write protected. This is always cleared when the drive has no write protect function. Not valid if STA_NODISK is set.
43 |
44 |
45 | 46 |

Return

47 | 48 | 49 | -------------------------------------------------------------------------------- /documents/doc/dwrite.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - disk_write 10 | 11 | 12 | 13 | 14 |
15 |

disk_write

16 |

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 |
26 | 27 |
28 |

Parameters

29 |
30 |
pdrv
31 |
Physical drive number to identify the target device.
32 |
buff
33 |
Pointer to the first item of the byte array to be written. The size of data to be written is sector size * count bytes.
34 |
sector
35 |
Start sector number in LBA. The data type LBA_t is an alias of DWORD or QWORD depends on the configuration option.
36 |
count
37 |
Number of sectors to write.
38 |
39 |
40 | 41 | 42 |
43 |

Return Values

44 |
45 |
RES_OK (0)
46 |
The function succeeded.
47 |
RES_ERROR
48 |
An unrecoverable hard error occured during the write operation.
49 |
RES_WRPRT
50 |
The medium is write protected.
51 |
RES_PARERR
52 |
Invalid parameter.
53 |
RES_NOTRDY
54 |
The device has not been initialized.
55 |
56 |
57 | 58 | 59 |
60 |

Description

61 |

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 |
66 | 67 | 68 |
69 |

QuickInfo

70 |

This function is not needed when FF_FS_READONLY == 1.

71 |
72 | 73 | 74 |

Return

75 | 76 | 77 | -------------------------------------------------------------------------------- /documents/doc/eof.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_eof 10 | 11 | 12 | 13 | 14 |
15 |

f_eof

16 |

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 |
23 | 24 | 25 |
26 |

Parameters

27 |
28 |
fp
29 |
Pointer to the open file object structure.
30 |
31 |
32 | 33 | 34 |
35 |

Return Values

36 |

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 |
38 | 39 | 40 |
41 |

Description

42 |

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 |
47 | 48 | 49 |
50 |

QuickInfo

51 |

Always available.

52 |
53 | 54 | 55 |
56 |

See Also

57 |

f_open, f_lseek, FIL

58 |
59 | 60 |

Return

61 | 62 | 63 | -------------------------------------------------------------------------------- /documents/doc/error.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_error 10 | 11 | 12 | 13 | 14 |
15 |

f_error

16 |

The f_error tests for an error on a file.

17 |
18 | int f_error (
19 |   FIL* fp   /* [IN] File object */
20 | );
21 | 
22 |
23 | 24 | 25 |
26 |

Parameters

27 |
28 |
fp
29 |
Pointer to the open file object structure.
30 |
31 |
32 | 33 | 34 |
35 |

Return Values

36 |

Returns a non-zero value if a hard error has occured; otherwise it returns a zero.

37 |
38 | 39 | 40 |
41 |

Description

42 |

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 |
47 | 48 | 49 |
50 |

QuickInfo

51 |

Always available.

52 |
53 | 54 | 55 |
56 |

See Also

57 |

f_open, FIL

58 |
59 | 60 |

Return

61 | 62 | 63 | -------------------------------------------------------------------------------- /documents/doc/expand.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_expand 10 | 11 | 12 | 13 | 14 |
15 |

f_expand

16 |

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 |
26 | 27 |
28 |

Parameters

29 |
30 |
fp
31 |
Pointer to the open file object.
32 |
fsz
33 |
Number of bytes in size to prepare or allocate for the file. The data type FSIZE_t is an alias of either DWORD(32-bit) or QWORD(64-bit) depends on the configuration option FF_FS_EXFAT.
34 |
opt
35 |
Allocation mode. Prepare to allocate (0) or Allocate now (1).
36 |
37 |
38 | 39 | 40 |
41 |

Return Values

42 |

43 | FR_OK, 44 | FR_DISK_ERR, 45 | FR_INT_ERR, 46 | FR_INVALID_OBJECT, 47 | FR_DENIED, 48 | FR_TIMEOUT 49 |

50 |
51 | 52 | 53 |
54 |

Description

55 |

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 | 62 |

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 |
66 | 67 |
68 |

QuickInfo

69 |

Available when FF_USE_EXPAND == 1 and FF_FS_READONLY == 0.

70 |
71 | 72 | 73 |
74 |

Example

75 |
 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 |
108 | 109 | 110 |
111 |

See Also

112 |

f_open, f_lseek, FIL

113 |
114 | 115 |

Return

116 | 117 | 118 | -------------------------------------------------------------------------------- /documents/doc/fattime.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - get_fattime 10 | 11 | 12 | 13 | 14 |
15 |

get_fattime

16 |

The get_fattime function is called to get the current time.

17 |
18 | DWORD get_fattime (void);
19 | 
20 |
21 | 22 | 23 |
24 |

Return Value

25 |

Currnet local time shall be returned as bit-fields packed into a DWORD value. The bit fields are as follows:

26 |
27 |
bit31:25
28 |
Year origin from the 1980 (0..127, e.g. 37 for 2017)
29 |
bit24:21
30 |
Month (1..12)
31 |
bit20:16
32 |
Day of the month (1..31)
33 |
bit15:11
34 |
Hour (0..23)
35 |
bit10:5
36 |
Minute (0..59)
37 |
bit4:0
38 |
Second / 2 (0..29, e.g. 25 for 50)
39 |
40 |
41 | 42 | 43 |
44 |

Description

45 |

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 |
47 | 48 | 49 |
50 |

QuickInfo

51 |

This function is not needed when FF_FS_READONLY == 1 or FF_FS_NORTC == 1.

52 |
53 | 54 | 55 |
56 |

Example

57 |
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 |
76 | 77 | 78 |

Return

79 | 80 | 81 | -------------------------------------------------------------------------------- /documents/doc/fdisk.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_fdisk 10 | 11 | 12 | 13 | 14 |
15 |

f_fdisk

16 |

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 |
25 | 26 |
27 |

Parameters

28 |
29 |
pdrv
30 |
Specifies the physical drive to be divided. This is not the logical drive number but the drive identifier passed to the low level disk functions.
31 |
ptbl
32 |
List of partition size to create on the drive. The data type LBA_t is an alias of DWORD or QWORD depends on the configuration option FF_LBA64.
33 |
work
34 |
Pointer to the function work area. The size must be at least FF_MAX_SS bytes. When a null pointer is given with FF_USE_LFN = 3, a memory block is obtained in this function for the working buffer.
35 |
36 |
37 | 38 |
39 |

Return Values

40 |

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 |
49 | 50 |
51 |

Description

52 |

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 |
55 | 56 |
57 |

QuickInfo

58 |

Available when FF_FS_READOLNY == 0, FF_USE_MKFS == 1 and FF_MULTI_PARTITION == 1.

59 |
60 | 61 |
62 |

Example

63 |
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 |
87 | 88 |
89 |

See Also

90 |

Volume management, f_mkfs

91 |
92 | 93 |

Return

94 | 95 | 96 | -------------------------------------------------------------------------------- /documents/doc/findfirst.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_findfirst 10 | 11 | 12 | 13 | 14 |
15 |

f_findfirst

16 |

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 |
26 | 27 |
28 |

Parameters

29 |
30 |
dp
31 |
Pointer to the blank directory object.
32 |
fno
33 |
Pointer to the file information structure to store the information about the found item.
34 |
path
35 |
Pointer to the null-terminated string that specifies the directory name to be opened.
36 |
pattern
37 |
Pointer to the nul-terminated string that specifies the name matching pattern to be searched for. It is referred by also subsequent f_findnext function, so that the string must be valid while the successive function calls.
38 |
39 |
40 | 41 | 42 |
43 |

Return Values

44 |

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 |
60 | 61 | 62 |
63 |

Description

64 |

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 | 72 |

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 | 78 |
79 | 80 | 81 |
82 |

QuickInfo

83 |

This is a wrapper function of f_opendir and f_readdir function. Available when FF_USE_FIND >= 1 and FF_FS_MINIMIZE <= 1.

84 |
85 | 86 | 87 |
88 |

Examples

89 |
 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 |
109 | 110 | 111 |
112 |

See Also

113 |

f_findnext, f_closedir, DIR, FILINFO

114 |
115 | 116 |

Return

117 | 118 | 119 | -------------------------------------------------------------------------------- /documents/doc/findnext.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_findnext 10 | 11 | 12 | 13 | 14 |
15 |

f_findnext

16 |

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 |
24 | 25 |
26 |

Parameters

27 |
28 |
dp
29 |
Pointer to the valid directory object created by f_findfirst function.
30 |
fno
31 |
Pointer to the file information structure to store the information about the found directory item.
32 |
33 |
34 | 35 | 36 |
37 |

Return Values

38 |

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 |
48 | 49 | 50 |
51 |

Description

52 |

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 |
54 | 55 | 56 |
57 |

QuickInfo

58 |

This is a wrapper function of f_readdir function. Available when FF_USE_FIND == 1 and FF_FS_MINIMIZE <= 1.

59 |
60 | 61 | 62 |
63 |

See Also

64 |

f_findfirst, f_closedir, DIR, FILINFO

65 |
66 | 67 |

Return

68 | 69 | 70 | -------------------------------------------------------------------------------- /documents/doc/forward.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_forward 10 | 11 | 12 | 13 | 14 |
15 |

f_forward

16 |

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 |
26 | 27 |
28 |

Parameters

29 |
30 |
fp
31 |
Pointer to the open file object.
32 |
func
33 |
Pointer to the user-defined data streaming function. For details, refer to the sample code.
34 |
btf
35 |
Number of bytes to forward in range of UINT.
36 |
bf
37 |
Pointer to the UINT variable to return number of bytes forwarded.
38 |
39 |
40 | 41 | 42 |
43 |

Return Values

44 |

45 | FR_OK, 46 | FR_DISK_ERR, 47 | FR_INT_ERR, 48 | FR_INVALID_OBJECT, 49 | FR_DENIED, 50 | FR_TIMEOUT 51 |

52 |
53 | 54 | 55 |
56 |

Description

57 |

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 |
59 | 60 | 61 |
62 |

QuickInfo

63 |

Available when FF_USE_FORWARD == 1.

64 |
65 | 66 | 67 |
68 |

Example

69 |
 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 |
130 | 131 | 132 |
133 |

See Also

134 |

f_open, fgets, f_write, f_close, FIL

135 |
136 | 137 |

Return

138 | 139 | 140 | -------------------------------------------------------------------------------- /documents/doc/getcwd.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_getcwd 10 | 11 | 12 | 13 | 14 |
15 |

f_getcwd

16 |

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 |
24 | 25 |
26 |

Parameters

27 |
28 |
buff
29 |
Pointer to the buffer to receive the current directory string.
30 |
len
31 |
Size of the buffer in unit of TCHAR.
32 |
33 |
34 | 35 | 36 |
37 |

Return Values

38 |

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 |
49 | 50 | 51 |
52 |

Description

53 |

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 |
56 | 57 | 58 |
59 |

QuickInfo

60 |

Available when FF_FS_RPATH == 2.

61 |
62 | 63 | 64 |
65 |

Example

66 |
67 |     FRESULT fr;
68 |     TCHAR str[SZ_STR];
69 | 
70 |     fr = f_getcwd(str, SZ_STR);  /* Get current directory path */
71 | 
72 | 
73 |
74 | 75 | 76 |
77 |

See Also

78 |

f_chdrive, f_chdir

79 |
80 | 81 |

Return

82 | 83 | 84 | -------------------------------------------------------------------------------- /documents/doc/getfree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_getfree 10 | 11 | 12 | 13 | 14 |
15 |

f_getfree

16 |

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 |
25 | 26 |
27 |

Parameters

28 |
29 |
path
30 |
Pointer to the null-terminated string that specifies the logical drive. A null-string means the default drive.
31 |
nclst
32 |
Pointer to the DWORD variable to store number of free clusters.
33 |
fatfs
34 |
Pointer to pointer that to store a pointer to the corresponding filesystem object.
35 |
36 |
37 | 38 | 39 |
40 |

Return Values

41 |

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 |
52 | 53 | 54 |
55 |

Descriptions

56 |

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 |
58 | 59 | 60 |
61 |

QuickInfo

62 |

Available when FF_FS_READONLY == 0 and FF_FS_MINIMIZE == 0.

63 |
64 | 65 | 66 |
67 |

Example

68 |
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 |
85 | 86 | 87 |
88 |

See Also

89 |

FATFS

90 |
91 | 92 |

Return

93 | 94 | 95 | -------------------------------------------------------------------------------- /documents/doc/getlabel.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_getlabel 10 | 11 | 12 | 13 | 14 |
15 |

f_getlabel

16 |

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 |
25 | 26 |
27 |

Parameters

28 |
29 |
path
30 |
Pointer to the null-terminated string that specifies the logical drive. Null-string specifies the default drive.
31 |
label
32 |
Pointer to the buffer to store the volume label. If the volume has no label, a null-string will be returned. Set null pointer if this information is not needed. The buffer size should be shown below at least to avoid buffer overflow.
33 | 34 | 35 | 36 | 37 | 38 | 39 |
ConfigurationFF_FS_EXFAT == 0FF_FS_EXFAT == 1
FF_USE_LFN == 012 items-
FF_LFN_UNICODE == 012 items23 items
FF_LFN_UNICODE == 1,312 items12 items
FF_LFN_UNICODE == 234 items34 items
40 |
41 |
vsn
42 |
Pointer to the DWORD variable to store the volume serial number. Set null pointer if this information is not needed.
43 |
44 |
45 | 46 | 47 |
48 |

Return Values

49 |

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 |
60 | 61 | 62 |
63 |

QuickInfo

64 |

Available when FF_USE_LABEL == 1.

65 |
66 | 67 | 68 |
69 |

Example

70 |
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 |
80 | 81 | 82 |
83 |

See Also

84 | f_setlabel 85 |
86 | 87 | 88 |

Return

89 | 90 | 91 | -------------------------------------------------------------------------------- /documents/doc/gets.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_gets 10 | 11 | 12 | 13 | 14 |
15 |

f_gets

16 |

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 |
25 | 26 |
27 |

Parameters

28 |
29 |
buff
30 |
Pointer to read buffer to store the read string.
31 |
len
32 |
Size of the read buffer in unit of item.
33 |
fp
34 |
Pointer to the open file object structure.
35 |
36 |
37 | 38 | 39 |
40 |

Return Values

41 |

When the function succeeded, buff will be returuned.

42 |
43 | 44 | 45 |
46 |

Description

47 |

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 |
50 | 51 | 52 |
53 |

QuickInfo

54 |

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 |
56 | 57 | 58 |
59 |

See Also

60 |

f_open, f_read, f_putc, f_puts, f_printf, f_close, FIL

61 |
62 | 63 |

Return

64 | 65 | 66 | -------------------------------------------------------------------------------- /documents/doc/mkdir.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_mkdir 10 | 11 | 12 | 13 | 14 |
15 |

f_mkdir

16 |

The f_mkdir function creates a new directory.

17 |
18 | FRESULT f_mkdir (
19 |   const TCHAR* path /* [IN] Directory name */
20 | );
21 | 
22 |
23 | 24 |
25 |

Parameter

26 |
27 |
path
28 |
Pointer to the null-terminated string that specifies the directory name to create.
29 |
30 |
31 | 32 | 33 |
34 |

Return Value

35 |

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 |
52 | 53 | 54 |
55 |

Description

56 |

This function creates a new directory. To remove a directory, use f_unlink function.

57 |
58 | 59 | 60 |
61 |

QuickInfo

62 |

Available when FF_FS_READONLY == 0 and FF_FS_MINIMIZE == 0.

63 |
64 | 65 | 66 |
67 |

Example

68 |
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 |
77 | 78 |

Return

79 | 80 | 81 | -------------------------------------------------------------------------------- /documents/doc/opendir.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_opendir 10 | 11 | 12 | 13 | 14 |
15 |

f_opendir

16 |

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 |
24 | 25 |
26 |

Parameters

27 |
28 |
dp
29 |
Pointer to the blank directory object to create a new one.
30 |
path
31 |
Pointer to the null-terminated string that specifies the directory name to be opened.
32 |
33 |
34 | 35 | 36 |
37 |

Return Values

38 |

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 |
54 | 55 | 56 |
57 |

Description

58 |

The f_opendir function opens an exsisting directory and creates a directory object for subsequent f_readdir function.

59 |
60 | 61 | 62 |
63 |

QuickInfo

64 |

Available when FF_FS_MINIMIZE <= 1.

65 |
66 | 67 | 68 |
69 |

See Also

70 |

f_readdir, f_closedir, DIR

71 |
72 | 73 |

Return

74 | 75 | 76 | -------------------------------------------------------------------------------- /documents/doc/putc.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_putc 10 | 11 | 12 | 13 | 14 |
15 |

f_putc

16 |

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 |
24 | 25 |
26 |

Parameters

27 |
28 |
chr
29 |
A character to write.
30 |
fp
31 |
Pointer to the open file object structuer.
32 |
33 |
34 | 35 | 36 |
37 |

Return Values

38 |

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 |
40 | 41 | 42 |
43 |

Description

44 |

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 |
46 | 47 |
48 |

QuickInfo

49 |

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 |
51 | 52 | 53 |
54 |

See Also

55 |

f_open, f_puts, f_printf, f_gets, f_close, FIL

56 |
57 | 58 |

Return

59 | 60 | 61 | -------------------------------------------------------------------------------- /documents/doc/puts.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_puts 10 | 11 | 12 | 13 | 14 |
15 |

f_puts

16 |

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 |
24 | 25 |
26 |

Parameters

27 |
28 |
str
29 |
Pointer to the null terminated string to be written. The terminator character will not be written.
30 |
fp
31 |
Pointer to the open file object structure.
32 |
33 |
34 | 35 | 36 |
37 |

Return Value

38 |

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 |
40 | 41 | 42 |
43 |

Description

44 |

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 |
46 | 47 | 48 |
49 |

QuickInfo

50 |

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 |
52 | 53 | 54 |
55 |

See Also

56 |

f_open, f_putc, f_printf, f_gets, f_close, FIL

57 |
58 | 59 |

Return

60 | 61 | 62 | -------------------------------------------------------------------------------- /documents/doc/read.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_read 10 | 11 | 12 | 13 | 14 |
15 |

f_read

16 |

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 |
26 | 27 |
28 |

Parameters

29 |
30 |
fp
31 |
Pointer to the open file object.
32 |
buff
33 |
Pointer to the buffer to store the read data.
34 |
btr
35 |
Number of bytes to read in range of UINT type. If the file needs to be read fast, it should be read in large chunk as possible.
36 |
br
37 |
Pointer to the UINT variable that receives number of bytes read. This value is always valid after the function call regardless of the function return code. If the return value is equal to btr, the function return code should be FR_OK.
38 |
39 |
40 | 41 | 42 |
43 |

Return Values

44 |

45 | FR_OK, 46 | FR_DISK_ERR, 47 | FR_INT_ERR, 48 | FR_DENIED, 49 | FR_INVALID_OBJECT, 50 | FR_TIMEOUT 51 |

52 |
53 | 54 | 55 |
56 |

Description

57 |

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 |
59 | 60 | 61 |
62 |

QuickInfo

63 |

Always available.

64 |
65 | 66 | 67 |
68 |

Example

69 |

Refer to the example in f_open.

70 |
71 | 72 | 73 |
74 |

See Also

75 |

f_open, fgets, f_write, f_close, FIL

76 |
77 | 78 |

Return

79 | 80 | 81 | -------------------------------------------------------------------------------- /documents/doc/readdir.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_readdir 10 | 11 | 12 | 13 | 14 |
15 |

f_readdir

16 |

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 |
29 | 30 |
31 |

Parameters

32 |
33 |
dp
34 |
Pointer to the open directory object.
35 |
fno
36 |
Pointer to the file information structure to store the information about read item. A null pointer rewinds the read index of the directory.
37 |
38 |
39 | 40 | 41 |
42 |

Return Values

43 |

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 |
52 | 53 | 54 |
55 |

Description

56 |

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 | 67 |

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 |
70 | 71 | 72 |
73 |

QuickInfo

74 |

Available when FF_FS_MINIMIZE <= 1.

75 |
76 | 77 | 78 |
79 |

Sample Code

80 |
 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 |
130 | 131 | 132 |
133 |

See Also

134 |

f_opendir, f_closedir, f_stat, FILINFO, DIR

135 |
136 | 137 |

Return

138 | 139 | 140 | -------------------------------------------------------------------------------- /documents/doc/rename.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_rename 10 | 11 | 12 | 13 | 14 |
15 |

f_rename

16 |

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 |
24 | 25 |
26 |

Parameters

27 |
28 |
old_name
29 |
Pointer to a null-terminated string that specifies the existing file or sub-directory to be renamed.
30 |
new_name
31 |
Pointer to a null-terminated string that specifies the new object name. A drive number may be specified in this string but it is ignored and assumed as the same drive of the old_name. Any object with this path name except old_name must not be exist, or the function fails with FR_EXIST.
32 |
33 |
34 | 35 | 36 |
37 |

Return Values

38 |

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 |
56 | 57 | 58 |
59 |

Description

60 |

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 |
62 | 63 | 64 |
65 |

QuickInfo

66 |

Available when FF_FS_READONLY == 0 and FF_FS_MINIMIZE == 0.

67 |
68 | 69 | 70 |
71 |

Example

72 |
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 |
83 | 84 | 85 |

Return

86 | 87 | 88 | -------------------------------------------------------------------------------- /documents/doc/sdir.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - DIR 10 | 11 | 12 | 13 | 14 |
15 |

DIR

16 |

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 |
35 | 36 |

Return

37 | 38 | 39 | -------------------------------------------------------------------------------- /documents/doc/setcp.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_setcp 10 | 11 | 12 | 13 | 14 |
15 |

f_setcp

16 |

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 |
23 | 24 |
25 |

Parameters

26 |
27 |
cp
28 |
OEM code page to be used for the path name. Valid values are as follows.
29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 |
ValueMeaning
0Initial value (any extended character cannot be used)
437U.S.
720Arabic
737Greek
771KBL
775Baltic
850Latin 1
852Latin 2
855Cyrillic
857Turkish
860Portuguese
861Icelandic
862Hebrew
863Canadian French
864Arabic
865Nordic
866Russian
869Greek 2
932Japanese (DBCS)
936Simplified Chinese (DBCS)
949Korean (DBCS)
950Traditional Chinese (DBCS)
54 |
55 |
56 |
57 | 58 | 59 |
60 |

Return Values

61 |

62 | FR_OK, 63 | FR_INVALID_PARAMETER 64 |

65 |
66 | 67 | 68 |
69 |

Description

70 |

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 |
72 | 73 | 74 |
75 |

QuickInfo

76 |

Available when FF_CODE_PAGE == 0.

77 |
78 | 79 | 80 |

Return

81 | 82 | 83 | -------------------------------------------------------------------------------- /documents/doc/setlabel.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_setlabel 10 | 11 | 12 | 13 | 14 |
15 |

f_setlabel

16 |

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 |
23 | 24 |
25 |

Parameters

26 |
27 |
label
28 |
Pointer to the null-terminated string that specifies the volume label to be set.
29 |
30 |
31 | 32 | 33 |
34 |

Return Values

35 |

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 |
48 | 49 | 50 |
51 |

Description

52 |

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 | 60 |

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 |
62 | 63 |
64 |

QuickInfo

65 |

Available when FF_FS_READONLY == 0 and FF_USE_LABEL == 1.

66 |
67 | 68 | 69 |
70 |

Example

71 |
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 |
82 | 83 | 84 |
85 |

See Also

86 | f_getlabel 87 |
88 | 89 | 90 |

Return

91 | 92 | 93 | -------------------------------------------------------------------------------- /documents/doc/sfatfs.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - FATFS 10 | 11 | 12 | 13 | 14 |
15 |

FATFS

16 |

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 |
59 | 60 |

Return

61 | 62 | 63 | -------------------------------------------------------------------------------- /documents/doc/sfile.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - FIL 10 | 11 | 12 | 13 | 14 |
15 |

FIL

16 |

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 |
40 | 41 |

Return

42 | 43 | 44 | -------------------------------------------------------------------------------- /documents/doc/sfileinfo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - FILINFO 10 | 11 | 12 | 13 | 14 |
15 |

FILINFO

16 |

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 |
32 | 33 |

Members

34 |
35 |
fsize
36 |
Size of the file in unit of byte. FSIZE_t is an alias of integer type either DWORD(32-bit) or QWORD(64-bit) depends on the configuration option FF_FS_EXFAT. Do not care if the item is a sub-directory.
37 |
fdate
38 |
The date when the file was modified or the directory was created.
39 |
40 |
bit15:9
41 |
Year origin from 1980 (0..127)
42 |
bit8:5
43 |
Month (1..12)
44 |
bit4:0
45 |
Day (1..31)
46 |
47 |
48 |
ftime
49 |
The time when the file was modified or the directory was created.
50 |
51 |
bit15:11
52 |
Hour (0..23)
53 |
bit10:5
54 |
Minute (0..59)
55 |
bit4:0
56 |
Second / 2 (0..29)
57 |
58 |
59 |
fattrib
60 |
The attribute flags in combination of:
61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 |
FlagMeaning
AM_RDORead-only. Write mode open and deleting is rejected.
AM_HIDHidden. Should not be shown in normal directory listing.
AM_SYSSystem. Used by system and should not be accessed.
AM_ARCArchive. Set on new creation or any modification to the file.
AM_DIRDirectory. This is not a file but a sub-directory container.
69 |
70 |
fname[]
71 |
Null-terminated object name. A null string is stored when no item to read and it indicates this structure is invalid. The size of fname[] and altname[] each can be configured in LFN configuration.
72 |
altname[]
73 |
Alternative object name is stored if available. This member is not available in non-LFN configuration.
74 |
75 | 76 |

Return

77 | 78 | 79 | -------------------------------------------------------------------------------- /documents/doc/size.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_size 10 | 11 | 12 | 13 | 14 |
15 |

f_size

16 |

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 |
23 | 24 | 25 |
26 |

Parameters

27 |
28 |
fp
29 |
Pointer to the open file object structure.
30 |
31 |
32 | 33 | 34 |
35 |

Return Values

36 |

Returns the size of the file in unit of byte.

37 |
38 | 39 | 40 |
41 |

Description

42 |

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 |
47 | 48 | 49 |
50 |

QuickInfo

51 |

Always available.

52 |
53 | 54 | 55 |
56 |

See Also

57 |

f_open, f_lseek, FIL

58 |
59 | 60 |

Return

61 | 62 | 63 | -------------------------------------------------------------------------------- /documents/doc/stat.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_stat 10 | 11 | 12 | 13 | 14 |
15 |

f_stat

16 |

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 |
24 | 25 |
26 |

Parameters

27 |
28 |
path
29 |
Pointer to the null-terminated string that specifies the object to get its information. The object must not be the root direcotry.
30 |
fno
31 |
Pointer to the blank FILINFO structure to store the information of the object. Set null pointer if this information is not needed.
32 |
33 |
34 | 35 | 36 |
37 |

Return Values

38 |

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 |
53 | 54 | 55 |
56 |

Description

57 |

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 |
60 | 61 | 62 |
63 |

QuickInfo

64 |

Available when FF_FS_MINIMIZE == 0.

65 |
66 | 67 | 68 |
69 |

Example

70 |
 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 |
103 | 104 | 105 |
106 |

References

107 |

f_opendir, f_readdir, FILINFO

108 |
109 | 110 |

Return

111 | 112 | 113 | -------------------------------------------------------------------------------- /documents/doc/sync.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_sync 10 | 11 | 12 | 13 | 14 |
15 |

f_sync

16 |

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 |
23 | 24 |
25 |

Parameter

26 |
27 |
fp
28 |
Pointer to the open file object to be flushed.
29 |
30 |
31 | 32 | 33 |
34 |

Return Values

35 |

36 | FR_OK, 37 | FR_DISK_ERR, 38 | FR_INT_ERR, 39 | FR_INVALID_OBJECT, 40 | FR_TIMEOUT 41 |

42 |
43 | 44 | 45 |
46 |

Description

47 |

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 |
73 | 74 | 75 |
76 |

QuickInfo

77 |

Available when FF_FS_READONLY == 0.

78 |
79 | 80 | 81 |
82 |

See Also

83 |

f_close, Critical section

84 |
85 | 86 |

Return

87 | 88 | 89 | -------------------------------------------------------------------------------- /documents/doc/tell.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_tell 10 | 11 | 12 | 13 | 14 |
15 |

f_tell

16 |

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 |
23 | 24 | 25 |
26 |

Parameters

27 |
28 |
fp
29 |
Pointer to the open file object structure.
30 |
31 |
32 | 33 | 34 |
35 |

Return Values

36 |

Returns current read/write pointer of the file.

37 |
38 | 39 | 40 |
41 |

Description

42 |

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 |
47 | 48 | 49 |
50 |

QuickInfo

51 |

Always available.

52 |
53 | 54 | 55 |
56 |

See Also

57 |

f_open, f_lseek, FIL

58 |
59 | 60 |

Return

61 | 62 | 63 | -------------------------------------------------------------------------------- /documents/doc/truncate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_truncate 10 | 11 | 12 | 13 | 14 |
15 |

f_truncate

16 |

The f_truncate function truncates the file size.

17 |
18 | FRESULT f_truncate (
19 |   FIL* fp     /* [IN] File object */
20 | );
21 | 
22 |
23 | 24 |
25 |

Parameter

26 |
27 |
fp
28 |
Pointer to the open file object to be truncated.
29 |
30 |
31 | 32 | 33 |
34 |

Return Values

35 |

36 | FR_OK, 37 | FR_DISK_ERR, 38 | FR_INT_ERR, 39 | FR_DENIED, 40 | FR_INVALID_OBJECT, 41 | FR_TIMEOUT 42 |

43 |
44 | 45 | 46 |
47 |

Description

48 |

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 |
50 | 51 | 52 |
53 |

QuickInfo

54 |

Available when FF_FS_READONLY == 0 and FF_FS_MINIMIZE == 0.

55 |
56 | 57 | 58 |
59 |

See Also

60 |

f_open, f_lseek, FIL

61 |
62 | 63 | 64 |

Return

65 | 66 | 67 | -------------------------------------------------------------------------------- /documents/doc/unlink.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_unlink 10 | 11 | 12 | 13 | 14 |
15 |

f_unlink

16 |

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 |
23 | 24 |
25 |

Parameter

26 |
27 |
path
28 |
Pointer to a null-terminated string that specifies the file or sub-directory to be removed.
29 |
30 |
31 | 32 | 33 |
34 |

Return Values

35 |

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 |
53 | 54 | 55 |
56 |

Description

57 |

58 | If condition of the object to be removed is applicable to the following terms, the function will be rejected.

63 |
64 | 65 | 66 |
67 |

QuickInfo

68 |

Available when FF_FS_READONLY == 0 and FF_FS_MINIMIZE == 0.

69 |
70 | 71 | 72 |

Return

73 | 74 | 75 | -------------------------------------------------------------------------------- /documents/doc/utime.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_utime 10 | 11 | 12 | 13 | 14 |
15 |

f_utime

16 |

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 |
24 | 25 |
26 |

Parameters

27 |
28 |
path
29 |
Pointer to the null-terminated string that specifies an object to be changed.
30 |
fno
31 |
Pointer to the file information structure that has a timestamp to be set in member fdate and ftime. Do not care any other members.
32 |
33 |
34 | 35 | 36 |
37 |

Return Values

38 |

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 |
54 | 55 | 56 |
57 |

Description

58 |

The f_utime function changes the timestamp of a file or sub-directory

59 |
60 | 61 | 62 |
63 |

Example

64 |
 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 |
84 | 85 | 86 |
87 |

QuickInfo

88 |

Available when FF_FS_READONLY == 0 and FF_USE_CHMOD == 1.

89 |
90 | 91 | 92 |
93 |

See Also

94 |

f_stat, FILINFO

95 |
96 | 97 |

Return

98 | 99 | 100 | -------------------------------------------------------------------------------- /documents/doc/write.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | FatFs - f_write 10 | 11 | 12 | 13 | 14 |
15 |

f_write

16 |

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 |
26 | 27 |
28 |

Parameters

29 |
30 |
fp
31 |
Pointer to the open file object structure.
32 |
buff
33 |
Pointer to the data to be written.
34 |
btw
35 |
Specifies number of bytes to write in range of UINT type. If the data needs to be written fast, it should be written in large chunk as possible.
36 |
bw
37 |
Pointer to the UINT variable that receives the number of bytes written. This value is always valid after the function call regardless of the function return code. If the return value is equal to btw, the function return code should be FR_OK.
38 |
39 |
40 | 41 | 42 |
43 |

Return Values

44 |

45 | FR_OK, 46 | FR_DISK_ERR, 47 | FR_INT_ERR, 48 | FR_DENIED, 49 | FR_INVALID_OBJECT, 50 | FR_TIMEOUT 51 |

52 |
53 | 54 | 55 |
56 |

Description

57 |

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 |
59 | 60 | 61 |
62 |

QuickInfo

63 |

Available when FF_FS_READONLY == 0.

64 |
65 | 66 | 67 |
68 |

Example

69 |

Refer to the example in f_open.

70 |
71 | 72 | 73 |
74 |

See Also

75 |

f_open, f_read, fputc, fputs, fprintf, f_close, FIL

76 |
77 | 78 |

Return

79 | 80 | 81 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /documents/res/app2.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------/ 2 | / Delete a sub-directory even if it contains any file 3 | /-------------------------------------------------------------/ 4 | / The delete_node() function is for R0.12+. 5 | / It works regardless of FF_FS_RPATH. 6 | */ 7 | 8 | 9 | FRESULT delete_node ( 10 | TCHAR* path, /* Path name buffer with the sub-directory to delete */ 11 | UINT sz_buff, /* Size of path name buffer (items) */ 12 | FILINFO* fno /* Name read buffer */ 13 | ) 14 | { 15 | UINT i, j; 16 | FRESULT fr; 17 | DIR dir; 18 | 19 | 20 | fr = f_opendir(&dir, path); /* Open the sub-directory to make it empty */ 21 | if (fr != FR_OK) return fr; 22 | 23 | for (i = 0; path[i]; i++) ; /* Get current path length */ 24 | path[i++] = _T('/'); 25 | 26 | for (;;) { 27 | fr = f_readdir(&dir, fno); /* Get a directory item */ 28 | if (fr != FR_OK || !fno->fname[0]) break; /* End of directory? */ 29 | j = 0; 30 | do { /* Make a path name */ 31 | if (i + j >= sz_buff) { /* Buffer over flow? */ 32 | fr = 100; break; /* Fails with 100 when buffer overflow */ 33 | } 34 | path[i + j] = fno->fname[j]; 35 | } while (fno->fname[j++]); 36 | if (fno->fattrib & AM_DIR) { /* Item is a sub-directory */ 37 | fr = delete_node(path, sz_buff, fno); 38 | } else { /* Item is a file */ 39 | fr = f_unlink(path); 40 | } 41 | if (fr != FR_OK) break; 42 | } 43 | 44 | path[--i] = 0; /* Restore the path name */ 45 | f_closedir(&dir); 46 | 47 | if (fr == FR_OK) fr = f_unlink(path); /* Delete the empty sub-directory */ 48 | return fr; 49 | } 50 | 51 | 52 | 53 | 54 | int main (void) /* How to use */ 55 | { 56 | FRESULT fr; 57 | FATFS fs; 58 | TCHAR buff[256]; 59 | FILINFO fno; 60 | 61 | 62 | f_mount(&fs, _T("5:"), 0); 63 | 64 | /* Directory to be deleted */ 65 | _tcscpy(buff, _T("5:dir")); 66 | 67 | /* Delete the directory */ 68 | fr = delete_node(buff, sizeof buff / sizeof buff[0], &fno); 69 | 70 | /* Check the result */ 71 | if (fr) { 72 | _tprintf(_T("Failed to delete the directory. (%u)\n"), fr); 73 | return fr; 74 | } else { 75 | _tprintf(_T("The directory and the contents have successfully been deleted.\n"), buff); 76 | return 0; 77 | } 78 | } 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /documents/res/app3.c: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------/ 2 | / Allocate a contiguous area to the file 3 | /-----------------------------------------------------------------------/ 4 | / This function checks if the file is contiguous with desired size. 5 | / If not, a block of contiguous sectors is allocated to the file. 6 | / If the file has been opened without FA_WRITE flag, it only checks if 7 | / the file is contiguous and returns the resulut. 8 | /-----------------------------------------------------------------------/ 9 | / This function can work with FatFs R0.09 - R0.11a. 10 | / It is incompatible with R0.12+. Use f_expand function instead. 11 | /----------------------------------------------------------------------*/ 12 | 13 | /* Declarations of FatFs internal functions accessible from applications. 14 | / This is intended to be used for disk checking/fixing or dirty hacks :-) */ 15 | DWORD clust2sect (FATFS* fs, DWORD clst); 16 | DWORD get_fat (FATFS* fs, DWORD clst); 17 | FRESULT put_fat (FATFS* fs, DWORD clst, DWORD val); 18 | 19 | 20 | DWORD allocate_contiguous_clusters ( /* Returns the first sector in LBA (0:error or not contiguous) */ 21 | FIL* fp, /* Pointer to the open file object */ 22 | DWORD len /* Number of bytes to allocate */ 23 | ) 24 | { 25 | DWORD csz, tcl, ncl, ccl, cl; 26 | 27 | 28 | if (f_lseek(fp, 0) || !len) /* Check if the given parameters are valid */ 29 | return 0; 30 | csz = 512UL * fp->fs->csize; /* Cluster size in unit of byte (assuming 512 bytes/sector) */ 31 | tcl = (len + csz - 1) / csz; /* Total number of clusters required */ 32 | len = tcl * csz; /* Round-up file size to the cluster boundary */ 33 | 34 | /* Check if the existing cluster chain is contiguous */ 35 | if (len == fp->fsize) { 36 | ncl = 0; ccl = fp->sclust; 37 | do { 38 | cl = get_fat(fp->fs, ccl); /* Get the cluster status */ 39 | if (cl + 1 < 3) return 0; /* Hard error? */ 40 | if (cl != ccl + 1 && cl < fp->fs->n_fatent) break; /* Not contiguous? */ 41 | ccl = cl; 42 | } while (++ncl < tcl); 43 | if (ncl == tcl) /* Is the file contiguous? */ 44 | return clust2sect(fp->fs, fp->sclust); /* File is contiguous. Return the start sector */ 45 | } 46 | 47 | /* File is not contiguous */ 48 | #if _FS_READONLY 49 | return 0; /* Exit if in read-only cfg. */ 50 | #else 51 | if (!(fp->flag & FA_WRITE)) return 0; /* Exit if the file object is for read-only */ 52 | 53 | if (f_truncate(fp)) return 0; /* Remove the non-contiguous chain */ 54 | 55 | /* Find a free contiguous area */ 56 | ccl = cl = 2; ncl = 0; 57 | do { 58 | if (cl >= fp->fs->n_fatent) return 0; /* No contiguous area is found. */ 59 | if (get_fat(fp->fs, cl)) { /* Encounterd a cluster in use */ 60 | do { /* Skip the block of used clusters */ 61 | cl++; 62 | if (cl >= fp->fs->n_fatent) return 0; /* No contiguous area is found. */ 63 | } while (get_fat(fp->fs, cl)); 64 | ccl = cl; ncl = 0; 65 | } 66 | cl++; ncl++; 67 | } while (ncl < tcl); 68 | 69 | /* Create a contiguous cluster chain */ 70 | fp->fs->last_clust = ccl - 1; 71 | if (f_lseek(fp, len)) return 0; 72 | 73 | return clust2sect(fp->fs, fp->sclust); /* Return file start sector */ 74 | #endif 75 | } 76 | 77 | 78 | int main (void) 79 | { 80 | FRESULT fr; 81 | DRESULT dr; 82 | FATFS fs; 83 | FIL fil; 84 | DWORD org; 85 | 86 | 87 | /* Open or create a file to write */ 88 | f_mount(&fs, "", 0); 89 | fr = f_open(&fil, "fastrec.log", FA_READ | FA_WRITE | FA_OPEN_ALWAYS); 90 | if (fr) return 1; 91 | 92 | /* Check if the file is 256MB in size and occupies a contiguous area. 93 | / If not, a contiguous area will be re-allocated to the file. */ 94 | org = allocate_contiguous_clusters(&fil, 0x10000000); 95 | if (!org) { 96 | printf("Function failed due to any error or insufficient contiguous area.\n"); 97 | f_close(&fil); 98 | return 1; 99 | } 100 | 101 | /* Now you can read/write the file without filesystem layer. */ 102 | ... 103 | dr = disk_write(fil.fs->drv, Buff, org, 1024); /* Write 512KiB from top of the file */ 104 | ... 105 | 106 | f_close(&fil); 107 | return 0; 108 | } 109 | 110 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /documents/res/app6.c: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------*/ 2 | /* Raw Read/Write Throughput Checker */ 3 | /*---------------------------------------------------------------------*/ 4 | 5 | #include 6 | #include 7 | #include "diskio.h" 8 | #include "ff.h" 9 | 10 | 11 | int test_raw_speed ( 12 | BYTE pdrv, /* Physical drive number */ 13 | DWORD lba, /* Start LBA for read/write test */ 14 | DWORD len, /* Number of bytes to read/write (must be multiple of sz_buff) */ 15 | void* buff, /* Read/write buffer */ 16 | UINT sz_buff /* Size of read/write buffer (must be multiple of FF_MAX_SS) */ 17 | ) 18 | { 19 | WORD ss; 20 | DWORD ofs, tmr; 21 | 22 | 23 | #if FF_MIN_SS != FF_MAX_SS 24 | if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK) { 25 | printf("\ndisk_ioctl() failed.\n"); 26 | return 0; 27 | } 28 | #else 29 | ss = FF_MAX_SS; 30 | #endif 31 | 32 | printf("Starting raw write test at sector %lu in %u bytes of data chunks...", lba, sz_buff); 33 | tmr = systimer(); 34 | for (ofs = 0; ofs < len / ss; ofs += sz_buff / ss) { 35 | if (disk_write(pdrv, buff, lba + ofs, sz_buff / ss) != RES_OK) { 36 | printf("\ndisk_write() failed.\n"); 37 | return 0; 38 | } 39 | } 40 | if (disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK) { 41 | printf("\ndisk_ioctl() failed.\n"); 42 | return 0; 43 | } 44 | tmr = systimer() - tmr; 45 | printf("\n%lu bytes written and it took %lu timer ticks.\n", len, tmr); 46 | 47 | printf("Starting raw read test at sector %lu in %u bytes of data chunks...", lba, sz_buff); 48 | tmr = systimer(); 49 | for (ofs = 0; ofs < len / ss; ofs += sz_buff / ss) { 50 | if (disk_read(pdrv, buff, lba + ofs, sz_buff / ss) != RES_OK) { 51 | printf("\ndisk_read() failed.\n"); 52 | return 0; 53 | } 54 | } 55 | tmr = systimer() - tmr; 56 | printf("\n%lu bytes read and it took %lu timer ticks.\n", len, tmr); 57 | 58 | printf("Test completed.\n"); 59 | return 1; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /documents/res/f1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/f1.png -------------------------------------------------------------------------------- /documents/res/f2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/f2.png -------------------------------------------------------------------------------- /documents/res/f3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/f3.png -------------------------------------------------------------------------------- /documents/res/f4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/f4.png -------------------------------------------------------------------------------- /documents/res/f5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/f5.png -------------------------------------------------------------------------------- /documents/res/f6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/f6.png -------------------------------------------------------------------------------- /documents/res/f7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/f7.png -------------------------------------------------------------------------------- /documents/res/funcs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/funcs.png -------------------------------------------------------------------------------- /documents/res/layers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/layers.png -------------------------------------------------------------------------------- /documents/res/layers1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/layers1.png -------------------------------------------------------------------------------- /documents/res/layers2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/layers2.png -------------------------------------------------------------------------------- /documents/res/layers3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/layers3.png -------------------------------------------------------------------------------- /documents/res/mkfatimg.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/mkfatimg.zip -------------------------------------------------------------------------------- /documents/res/mkfs.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/mkfs.xlsx -------------------------------------------------------------------------------- /documents/res/modules.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/modules.png -------------------------------------------------------------------------------- /documents/res/rwtest1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/rwtest1.png -------------------------------------------------------------------------------- /documents/res/rwtest2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/rwtest2.png -------------------------------------------------------------------------------- /documents/res/rwtest3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/rwtest3.png -------------------------------------------------------------------------------- /documents/res/uniconv.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/documents/res/uniconv.zip -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /source/diskio.c: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------*/ 2 | /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2023 */ 3 | /* */ 4 | /* Portions COPYRIGHT 2017-2023 STMicroelectronics */ 5 | /* Portions Copyright (C) 2013, ChaN, all right reserved */ 6 | /*-----------------------------------------------------------------------*/ 7 | /* If a working storage control module is available, it should be */ 8 | /* attached to the FatFs via a glue function rather than modifying it. */ 9 | /* This is an example of glue functions to attach various existing */ 10 | /* storage control modules to the FatFs module with a defined API. */ 11 | /*-----------------------------------------------------------------------*/ 12 | 13 | /* Includes ------------------------------------------------------------------*/ 14 | #include "diskio.h" 15 | #include "ff_gen_drv.h" 16 | 17 | #if defined ( __GNUC__ ) 18 | #ifndef __weak 19 | #define __weak __attribute__((weak)) 20 | #endif 21 | #endif 22 | 23 | /* Private typedef -----------------------------------------------------------*/ 24 | /* Private define ------------------------------------------------------------*/ 25 | /* Private variables ---------------------------------------------------------*/ 26 | extern Disk_drvTypeDef disk; 27 | 28 | /* Private function prototypes -----------------------------------------------*/ 29 | /* Private functions ---------------------------------------------------------*/ 30 | 31 | /** 32 | * @brief Gets Disk Status 33 | * @param pdrv: Physical drive number (0..) 34 | * @retval DSTATUS: Operation status 35 | */ 36 | DSTATUS disk_status ( 37 | BYTE pdrv /* Physical drive number to identify the drive */ 38 | ) 39 | { 40 | DSTATUS stat; 41 | 42 | stat = disk.drv[pdrv]->disk_status(disk.lun[pdrv]); 43 | return stat; 44 | } 45 | 46 | /** 47 | * @brief Initializes a Drive 48 | * @param pdrv: Physical drive number (0..) 49 | * @retval DSTATUS: Operation status 50 | */ 51 | DSTATUS disk_initialize ( 52 | BYTE pdrv /* Physical drive nmuber to identify the drive */ 53 | ) 54 | { 55 | DSTATUS stat = RES_OK; 56 | 57 | if(disk.is_initialized[pdrv] == 0) 58 | { 59 | stat = disk.drv[pdrv]->disk_initialize(disk.lun[pdrv]); 60 | if(stat == RES_OK) 61 | { 62 | disk.is_initialized[pdrv] = 1; 63 | } 64 | } 65 | return stat; 66 | } 67 | 68 | /** 69 | * @brief Reads Sector(s) 70 | * @param pdrv: Physical drive number (0..) 71 | * @param *buff: Data buffer to store read data 72 | * @param sector: Sector address (LBA) 73 | * @param count: Number of sectors to read (1..128) 74 | * @retval DRESULT: Operation result 75 | */ 76 | DRESULT disk_read ( 77 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ 78 | BYTE *buff, /* Data buffer to store read data */ 79 | LBA_t sector, /* Sector address in LBA */ 80 | UINT count /* Number of sectors to read */ 81 | ) 82 | 83 | { 84 | DRESULT res; 85 | 86 | res = disk.drv[pdrv]->disk_read(disk.lun[pdrv], buff, sector, count); 87 | return res; 88 | } 89 | 90 | /** 91 | * @brief Writes Sector(s) 92 | * @param pdrv: Physical drive number (0..) 93 | * @param *buff: Data to be written 94 | * @param sector: Sector address (LBA) 95 | * @param count: Number of sectors to write (1..128) 96 | * @retval DRESULT: Operation result 97 | */ 98 | 99 | DRESULT disk_write ( 100 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ 101 | const BYTE *buff, /* Data to be written */ 102 | LBA_t sector, /* Sector address in LBA */ 103 | UINT count /* Number of sectors to write */ 104 | ) 105 | { 106 | DRESULT res; 107 | 108 | res = disk.drv[pdrv]->disk_write(disk.lun[pdrv], buff, sector, count); 109 | return res; 110 | } 111 | 112 | 113 | /** 114 | * @brief I/O control operation 115 | * @param pdrv: Physical drive number (0..) 116 | * @param cmd: Control code 117 | * @param *buff: Buffer to send/receive control data 118 | * @retval DRESULT: Operation result 119 | */ 120 | 121 | DRESULT disk_ioctl ( 122 | BYTE pdrv, /* Physical drive nmuber (0..) */ 123 | BYTE cmd, /* Control code */ 124 | void *buff /* Buffer to send/receive control data */ 125 | ) 126 | { 127 | DRESULT res; 128 | 129 | res = disk.drv[pdrv]->disk_ioctl(disk.lun[pdrv], cmd, buff); 130 | return res; 131 | } 132 | 133 | 134 | /** 135 | * @brief Gets Time from RTC 136 | * @param None 137 | * @retval Time in DWORD 138 | */ 139 | __weak DWORD get_fattime (void) 140 | { 141 | return 0; 142 | } 143 | 144 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 145 | 146 | -------------------------------------------------------------------------------- /source/diskio.h: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------/ 2 | / Low level disk interface modlue include file (C)ChaN, 2019 / 3 | /-----------------------------------------------------------------------*/ 4 | 5 | #ifndef _DISKIO_DEFINED 6 | #define _DISKIO_DEFINED 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | #include "ff.h" 13 | 14 | /* Status of Disk Functions */ 15 | typedef BYTE DSTATUS; 16 | 17 | /* Results of Disk Functions */ 18 | typedef enum { 19 | RES_OK = 0, /* 0: Successful */ 20 | RES_ERROR, /* 1: R/W Error */ 21 | RES_WRPRT, /* 2: Write Protected */ 22 | RES_NOTRDY, /* 3: Not Ready */ 23 | RES_PARERR /* 4: Invalid Parameter */ 24 | } DRESULT; 25 | 26 | 27 | /*---------------------------------------*/ 28 | /* Prototypes for disk control functions */ 29 | 30 | 31 | DSTATUS disk_initialize (BYTE pdrv); 32 | DSTATUS disk_status (BYTE pdrv); 33 | DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count); 34 | DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count); 35 | DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); 36 | 37 | 38 | /* Disk Status Bits (DSTATUS) */ 39 | 40 | #define STA_NOINIT 0x01 /* Drive not initialized */ 41 | #define STA_NODISK 0x02 /* No medium in the drive */ 42 | #define STA_PROTECT 0x04 /* Write protected */ 43 | 44 | 45 | /* Command code for disk_ioctrl fucntion */ 46 | 47 | /* Generic command (Used by FatFs) */ 48 | #define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */ 49 | #define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */ 50 | #define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */ 51 | #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */ 52 | #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */ 53 | 54 | /* Generic command (Not used by FatFs) */ 55 | #define CTRL_POWER 5 /* Get/Set power status */ 56 | #define CTRL_LOCK 6 /* Lock/Unlock media removal */ 57 | #define CTRL_EJECT 7 /* Eject media */ 58 | #define CTRL_FORMAT 8 /* Create physical format on the media */ 59 | 60 | /* MMC/SDC specific ioctl command */ 61 | #define MMC_GET_TYPE 10 /* Get card type */ 62 | #define MMC_GET_CSD 11 /* Get CSD */ 63 | #define MMC_GET_CID 12 /* Get CID */ 64 | #define MMC_GET_OCR 13 /* Get OCR */ 65 | #define MMC_GET_SDSTAT 14 /* Get SD status */ 66 | #define ISDIO_READ 55 /* Read data form SD iSDIO register */ 67 | #define ISDIO_WRITE 56 /* Write data to SD iSDIO register */ 68 | #define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */ 69 | 70 | /* ATA/CF specific ioctl command */ 71 | #define ATA_GET_REV 20 /* Get F/W revision */ 72 | #define ATA_GET_MODEL 21 /* Get model name */ 73 | #define ATA_GET_SN 22 /* Get serial number */ 74 | 75 | #ifdef __cplusplus 76 | } 77 | #endif 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /source/drivers/sd/sd_diskio.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file sd_diskio.c 4 | * @author MCD Application Team 5 | * @brief SD Disk I/O driver 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 | /* Includes ------------------------------------------------------------------*/ 19 | #include "sd_diskio.h" 20 | 21 | /* Private typedef -----------------------------------------------------------*/ 22 | /* Private define ------------------------------------------------------------*/ 23 | /* Private variables ---------------------------------------------------------*/ 24 | #ifndef BLOCKSIZE 25 | #define BLOCKSIZE 512 26 | #endif 27 | /* Disk status */ 28 | static volatile DSTATUS Stat = STA_NOINIT; 29 | 30 | /* Private function prototypes -----------------------------------------------*/ 31 | static DSTATUS SD_check_status (BYTE); 32 | static DSTATUS SD_initialize (BYTE); 33 | static DSTATUS SD_status (BYTE); 34 | static DRESULT SD_read (BYTE, BYTE* , LBA_t, UINT); 35 | static DRESULT SD_write (BYTE, const BYTE* , LBA_t, UINT); 36 | static DRESULT SD_ioctl (BYTE, BYTE, void* ); 37 | 38 | const Diskio_drvTypeDef SD_Driver = 39 | { 40 | SD_initialize, 41 | SD_status, 42 | SD_read, 43 | SD_write, 44 | SD_ioctl 45 | }; 46 | 47 | /* Private functions ---------------------------------------------------------*/ 48 | /** 49 | * @brief Check the status of the sd card 50 | * @param lun : not used 51 | * @retval DSTATUS: return 0 if the sd card is ready and 1 otherwise 52 | */ 53 | static DSTATUS SD_check_status(BYTE lun) 54 | { 55 | Stat = STA_NOINIT; 56 | 57 | if (HAL_SD_GetCardState(&sdmmc_handle) == HAL_SD_CARD_TRANSFER) 58 | { 59 | Stat &= ~STA_NOINIT; 60 | } 61 | 62 | return Stat; 63 | } 64 | 65 | /** 66 | * @brief Initialize the SD Diskio lowlevel driver 67 | * @param lun : not used 68 | * @retval DSTATUS: return 0 on Success STA_NOINIT otherwise 69 | */ 70 | static DSTATUS SD_initialize(BYTE lun) 71 | { 72 | Stat = STA_NOINIT; 73 | 74 | #if (ENABLE_SD_INIT == 1) 75 | sdmmc_sd_init(); 76 | Stat = SD_check_status(lun); 77 | #else 78 | Stat = SD_check_status(lun); 79 | #endif 80 | return Stat; 81 | } 82 | 83 | /** 84 | * @brief Get the Disk Status 85 | * @param lun : not used 86 | * @retval DSTATUS: return 0 if the sd card is ready and 1 otherwise status 87 | */ 88 | static DSTATUS SD_status(BYTE lun) 89 | { 90 | return SD_check_status(lun); 91 | } 92 | 93 | /** 94 | * @brief Read data from sd card into a buffer 95 | * @param lun : not used 96 | * @param *buff: Data buffer to store read data 97 | * @param sector: Sector address (LBA) 98 | * @param count: Number of sectors to read (1..128) 99 | * @retval DRESULT: return RES_OK otherwise 100 | */ 101 | static DRESULT SD_read(BYTE lun, BYTE *buff, LBA_t sector, UINT count) 102 | { 103 | DRESULT res = RES_ERROR; 104 | 105 | if (HAL_SD_ReadBlocks(&sdmmc_handle, (uint8_t*)buff, sector, count, SD_TIMEOUT) == HAL_OK) 106 | { 107 | /* Wait until the card state is ready */ 108 | while (HAL_SD_GetCardState(&sdmmc_handle) != HAL_SD_CARD_TRANSFER) 109 | { 110 | } 111 | res = RES_OK; 112 | } 113 | return res; 114 | } 115 | 116 | /** 117 | * @brief Write data from sd card into a buffer 118 | * @param lun : not used 119 | * @param *buff: Data to be written 120 | * @param sector: Sector address (LBA) 121 | * @param count: Number of sectors to write (1..128) 122 | * @retval DRESULT: return RES_OK otherwise 123 | */ 124 | static DRESULT SD_write(BYTE lun, const BYTE *buff, LBA_t sector, UINT count) 125 | { 126 | DRESULT res = RES_ERROR; 127 | 128 | if (HAL_SD_WriteBlocks(&sdmmc_handle, (uint8_t*)buff, sector, count, SD_TIMEOUT) == HAL_OK) 129 | { 130 | /* Wait until the card state is ready */ 131 | while (HAL_SD_GetCardState(&sdmmc_handle) != HAL_SD_CARD_TRANSFER) 132 | { 133 | } 134 | res = RES_OK; 135 | } 136 | return res; 137 | } 138 | 139 | /** 140 | * @brief I/O control operation 141 | * @param lun : not used 142 | * @param cmd: Control code 143 | * @param *buff: Buffer to send/receive control data 144 | * @retval DRESULT: return RES_OK otherwise 145 | */ 146 | static DRESULT SD_ioctl(BYTE lun, BYTE cmd, void *buff) 147 | { 148 | DRESULT res = RES_ERROR; 149 | HAL_SD_CardInfoTypeDef CardInfo; 150 | 151 | if (Stat & STA_NOINIT) return RES_NOTRDY; 152 | 153 | switch (cmd) 154 | { 155 | /* Make sure that no pending write process */ 156 | case CTRL_SYNC: 157 | res = RES_OK; 158 | break; 159 | 160 | /* Get number of sectors on the disk (DWORD) */ 161 | case GET_SECTOR_COUNT: 162 | HAL_SD_GetCardInfo(&sdmmc_handle, &CardInfo); 163 | *(DWORD*)buff = CardInfo.LogBlockNbr; 164 | res = RES_OK; 165 | break; 166 | 167 | /* Get R/W sector size (WORD) */ 168 | case GET_SECTOR_SIZE: 169 | HAL_SD_GetCardInfo(&sdmmc_handle, &CardInfo); 170 | *(WORD*)buff = CardInfo.LogBlockSize; 171 | res = RES_OK; 172 | break; 173 | 174 | /* Get erase block size in unit of sector (DWORD) */ 175 | case GET_BLOCK_SIZE: 176 | HAL_SD_GetCardInfo(&sdmmc_handle, &CardInfo); 177 | *(DWORD*)buff = CardInfo.LogBlockSize / BLOCKSIZE; 178 | res = RES_OK; 179 | break; 180 | 181 | default: 182 | res = RES_PARERR; 183 | } 184 | 185 | return res; 186 | } -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /source/drivers/sram/sram_diskio.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file sram_diskio.c 4 | * @author MCD Application Team 5 | * @brief SRAM Disk I/O driver 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 | /* Includes ------------------------------------------------------------------*/ 19 | #include "sram_diskio.h" 20 | #include 21 | 22 | /* Private variables ---------------------------------------------------------*/ 23 | /* Private function prototypes -----------------------------------------------*/ 24 | static DSTATUS SRAMDISK_initialize (BYTE); 25 | static DSTATUS SRAMDISK_status (BYTE); 26 | static DRESULT SRAMDISK_read (BYTE, BYTE*, LBA_t, UINT); 27 | static DRESULT SRAMDISK_write (BYTE, const BYTE*, LBA_t, UINT); 28 | static DRESULT SRAMDISK_ioctl (BYTE, BYTE, void*); 29 | 30 | const Diskio_drvTypeDef SRAMDISK_Driver = 31 | { 32 | SRAMDISK_initialize, 33 | SRAMDISK_status, 34 | SRAMDISK_read, 35 | SRAMDISK_write, 36 | SRAMDISK_ioctl 37 | }; 38 | 39 | /* Private functions ---------------------------------------------------------*/ 40 | 41 | /** 42 | * @brief Initialize the SRAM Diskio lowlevel driver 43 | * @param lun : not used 44 | * @retval DSTATUS: return 0 45 | */ 46 | static DSTATUS SRAMDISK_initialize(BYTE lun) 47 | { 48 | memset((void *)SRAM_DISK_BASE_ADDR, '\0', SRAM_DISK_SIZE); 49 | return 0; 50 | } 51 | 52 | /** 53 | * @brief Gets the Disk Status 54 | * @param lun : not used 55 | * @retval DSTATUS: return 0 56 | */ 57 | static DSTATUS SRAMDISK_status(BYTE lun) 58 | { 59 | return 0; 60 | } 61 | 62 | /** 63 | * @brief Read data from sram into a buffer 64 | * @param lun : not used 65 | * @param *buff: Data buffer to store read data 66 | * @param sector: Sector address (LBA) 67 | * @param count: Number of sectors to read (1..128) 68 | * @retval DRESULT: return RES_OK otherwise 69 | */ 70 | static DRESULT SRAMDISK_read(BYTE lun, BYTE *buff, LBA_t sector, UINT count) 71 | { 72 | uint32_t BufferSize = (BLOCK_SIZE * count); 73 | uint8_t *pMem = (uint8_t *) (SRAM_DISK_BASE_ADDR + (sector * BLOCK_SIZE)); 74 | 75 | for(; BufferSize != 0; BufferSize--) 76 | { 77 | *buff++ = *(__IO uint8_t *)pMem++; 78 | } 79 | 80 | return RES_OK; 81 | } 82 | 83 | /** 84 | * @brief Write data from sram into a buffer 85 | * @param lun : not used 86 | * @param *buff: Data to be written 87 | * @param sector: Sector address (LBA) 88 | * @param count: Number of sectors to write (1..128) 89 | * @retval DRESULT: return RES_OK otherwise 90 | */ 91 | static DRESULT SRAMDISK_write(BYTE lun, const BYTE *buff, LBA_t sector, UINT count) 92 | { 93 | uint32_t BufferSize = (BLOCK_SIZE * count); 94 | uint8_t *pMem = (uint8_t *) (SRAM_DISK_BASE_ADDR + (sector * BLOCK_SIZE)); 95 | 96 | for(; BufferSize != 0; BufferSize--) 97 | { 98 | *(__IO uint8_t *)pMem++ = *buff++; 99 | } 100 | 101 | return RES_OK; 102 | } 103 | 104 | 105 | /** 106 | * @brief I/O control operation 107 | * @param lun : not used 108 | * @param cmd: Control code 109 | * @param *buff: Buffer to send/receive control data 110 | * @retval DRESULT: return RES_OK on success RES_ERROR otherwise 111 | */ 112 | static DRESULT SRAMDISK_ioctl(BYTE lun, BYTE cmd, void *buff) 113 | { 114 | DRESULT res = RES_ERROR; 115 | 116 | switch (cmd) 117 | { 118 | /* Make sure that no pending write process */ 119 | case CTRL_SYNC : 120 | res = RES_OK; 121 | break; 122 | 123 | /* Get number of sectors on the disk (DWORD) */ 124 | case GET_SECTOR_COUNT : 125 | *(DWORD*)buff = SRAM_DISK_SIZE / BLOCK_SIZE; 126 | res = RES_OK; 127 | break; 128 | 129 | /* Get R/W sector size (WORD) */ 130 | case GET_SECTOR_SIZE : 131 | *(WORD*)buff = BLOCK_SIZE; 132 | res = RES_OK; 133 | break; 134 | 135 | /* Get erase block size in unit of sector (DWORD) */ 136 | case GET_BLOCK_SIZE : 137 | *(DWORD*)buff = 1; 138 | res = RES_OK; 139 | break; 140 | 141 | default: 142 | res = RES_PARERR; 143 | } 144 | 145 | return res; 146 | } 147 | 148 | -------------------------------------------------------------------------------- /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/template/sd_diskio_config.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file sd_diskio_config.h 4 | * @author MCD Application Team 5 | * @brief Template for the sd_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 SD_DISKIO_CONFIG_H 21 | #define SD_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 | 34 | /* Maximum waiting time to do an operation in the HAL_SD_ReadBlocks() and HAL_SD_WriteBlocks() */ 35 | #define SD_TIMEOUT 30 * 1000 36 | 37 | /* 38 | * Depending on the usecase, the SD card initialization could be done at the 39 | * application level, if it is the case define the flag below to enable 40 | * the MX_SD_Init(). 41 | */ 42 | #define ENABLE_SD_INIT 0 43 | 44 | /* 45 | * when using cacheable memory region, it may be needed to maintain the cache 46 | * validity. Enable the define below to activate a cache maintenance at each 47 | * read and write operation. 48 | * Notice: This is applicable only for cortex M7 based platform. 49 | */ 50 | 51 | #define ENABLE_SD_DMA_CACHE_MAINTENANCE 1 52 | 53 | 54 | extern SD_HandleTypeDef hsd_sdmmc1; 55 | 56 | /* Default handle used in sd_diskio.c file */ 57 | #define sdmmc_handle hsd_sdmmc1 58 | 59 | /* Default Init function of sdmmc IP used in sd_diskio.c file */ 60 | #if (ENABLE_SD_INIT == 1) 61 | extern void MX_SDMMC1_SD_Init(void); 62 | #define sdmmc_sd_init MX_SDMMC1_SD_Init 63 | #endif 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | #endif /* SD_DISKIO_CONFIG_H */ -------------------------------------------------------------------------------- /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/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 */ -------------------------------------------------------------------------------- /source/drivers/template/user_diskio.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file user_diskio.c 4 | * @author MCD Application Team 5 | * @brief User Disk I/O driver 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 | /* Includes ------------------------------------------------------------------*/ 19 | 20 | /* Private typedef -----------------------------------------------------------*/ 21 | /* Private define ------------------------------------------------------------*/ 22 | /* Private variables ---------------------------------------------------------*/ 23 | /* Private function prototypes -----------------------------------------------*/ 24 | static DSTATUS USER_initialize (BYTE); 25 | static DSTATUS USER_status (BYTE); 26 | static DRESULT USER_read (BYTE, BYTE*, LBA_t, UINT); 27 | static DRESULT USER_write (BYTE, const BYTE*, LBA_t, UINT); 28 | static DRESULT USER_ioctl (BYTE, BYTE, void*); 29 | 30 | const Diskio_drvTypeDef USER_Driver = 31 | { 32 | USER_initialize, 33 | USER_status, 34 | USER_read, 35 | USER_write, 36 | USER_ioctl 37 | }; 38 | 39 | /* Private functions ---------------------------------------------------------*/ 40 | 41 | /** 42 | * @brief Initialize the user Diskio lowlevel driver 43 | * @param lun : not used 44 | * @retval DSTATUS: return 0 45 | */ 46 | static DSTATUS USER_initialize(BYTE lun) 47 | { 48 | return 0; 49 | } 50 | 51 | /** 52 | * @brief Gets the Disk Status 53 | * @param lun : not used 54 | * @retval DSTATUS: return 0 55 | */ 56 | static DSTATUS USER_status(BYTE lun) 57 | { 58 | return 0; 59 | } 60 | 61 | /** 62 | * @brief Read data from media into a buffer 63 | * @param lun : not used 64 | * @param *buff: Data buffer to store read data 65 | * @param sector: Sector address (LBA) 66 | * @param count: Number of sectors to read (1..128) 67 | * @retval DRESULT: return RES_OK otherwise 68 | */ 69 | static DRESULT USER_read(BYTE lun, BYTE *buff, LBA_t sector, UINT count) 70 | { 71 | 72 | return RES_OK; 73 | } 74 | 75 | /** 76 | * @brief Write data from media into a buffer 77 | * @param lun : not used 78 | * @param *buff: Data to be written 79 | * @param sector: Sector address (LBA) 80 | * @param count: Number of sectors to write (1..128) 81 | * @retval DRESULT: return RES_OK otherwise 82 | */ 83 | static DRESULT USER_write(BYTE lun, const BYTE *buff, LBA_t sector, UINT count) 84 | { 85 | 86 | return RES_OK; 87 | } 88 | 89 | /** 90 | * @brief I/O control operation 91 | * @param lun : not used 92 | * @param cmd: Control code 93 | * @param *buff: Buffer to send/receive control data 94 | * @retval DRESULT: return RES_OK otherwise 95 | */ 96 | static DRESULT USER_ioctl(BYTE lun, BYTE cmd, void *buff) 97 | { 98 | DRESULT res = RES_ERROR; 99 | switch (cmd) 100 | { 101 | /* Make sure that no pending write process */ 102 | case CTRL_SYNC: 103 | res = RES_OK; 104 | break; 105 | 106 | /* Get number of sectors on the disk (DWORD) */ 107 | case GET_SECTOR_COUNT: 108 | res = RES_OK; 109 | break; 110 | 111 | /* Get R/W sector size (WORD) */ 112 | case GET_SECTOR_SIZE: 113 | res = RES_OK; 114 | break; 115 | 116 | /* Get erase block size in unit of sector (DWORD) */ 117 | case GET_BLOCK_SIZE: 118 | res = RES_OK; 119 | break; 120 | 121 | default: 122 | res = RES_PARERR; 123 | } 124 | return res; 125 | } 126 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/ff_gen_drv.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file ff_gen_drv.c 4 | * @author MCD Application Team 5 | * @brief FatFs generic low level driver. 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 | 19 | /* Includes ------------------------------------------------------------------*/ 20 | #include "ff_gen_drv.h" 21 | 22 | /* Private typedef -----------------------------------------------------------*/ 23 | /* Private define ------------------------------------------------------------*/ 24 | /* Private variables ---------------------------------------------------------*/ 25 | Disk_drvTypeDef disk = {{0},{0},{0},0}; 26 | 27 | /* Private function prototypes -----------------------------------------------*/ 28 | /* Private functions ---------------------------------------------------------*/ 29 | 30 | /** 31 | * @brief Links a compatible diskio driver/lun id and increments the number of active 32 | * linked drivers. 33 | * @note The number of linked drivers (volumes) is up to 10 due to FatFs limits. 34 | * @param drv: pointer to the disk IO Driver structure 35 | * @param path: pointer to the logical drive path 36 | * @param lun : only used for USB Key Disk to add multi-lun management 37 | else the parameter must be equal to 0 38 | * @retval Returns 0 in case of success, otherwise 1. 39 | */ 40 | uint8_t FATFS_LinkDriverEx(const Diskio_drvTypeDef *drv, char *path, uint8_t lun) 41 | { 42 | uint8_t ret = 1; 43 | uint8_t DiskNum = 0; 44 | 45 | if(disk.nbr < FF_VOLUMES) 46 | { 47 | disk.is_initialized[disk.nbr] = 0; 48 | disk.drv[disk.nbr] = drv; 49 | disk.lun[disk.nbr] = lun; 50 | DiskNum = disk.nbr++; 51 | path[0] = DiskNum + '0'; 52 | path[1] = ':'; 53 | path[2] = '/'; 54 | path[3] = 0; 55 | ret = 0; 56 | } 57 | 58 | return ret; 59 | } 60 | 61 | /** 62 | * @brief Links a compatible diskio driver and increments the number of active 63 | * linked drivers. 64 | * @note The number of linked drivers (volumes) is up to 10 due to FatFs limits 65 | * @param drv: pointer to the disk IO Driver structure 66 | * @param path: pointer to the logical drive path 67 | * @retval Returns 0 in case of success, otherwise 1. 68 | */ 69 | uint8_t FATFS_LinkDriver(const Diskio_drvTypeDef *drv, char *path) 70 | { 71 | return FATFS_LinkDriverEx(drv, path, 0); 72 | } 73 | 74 | /** 75 | * @brief Unlinks a diskio driver and decrements the number of active linked 76 | * drivers. 77 | * @param path: pointer to the logical drive path 78 | * @param lun : not used 79 | * @retval Returns 0 in case of success, otherwise 1. 80 | */ 81 | uint8_t FATFS_UnLinkDriverEx(char *path, uint8_t lun) 82 | { 83 | uint8_t DiskNum = 0; 84 | uint8_t ret = 1; 85 | 86 | if(disk.nbr >= 1) 87 | { 88 | DiskNum = path[0] - '0'; 89 | if(disk.drv[DiskNum] != 0) 90 | { 91 | disk.drv[DiskNum] = 0; 92 | disk.lun[DiskNum] = 0; 93 | disk.nbr--; 94 | ret = 0; 95 | } 96 | } 97 | 98 | return ret; 99 | } 100 | 101 | /** 102 | * @brief Unlinks a diskio driver and decrements the number of active linked 103 | * drivers. 104 | * @param path: pointer to the logical drive path 105 | * @retval Returns 0 in case of success, otherwise 1. 106 | */ 107 | uint8_t FATFS_UnLinkDriver(char *path) 108 | { 109 | return FATFS_UnLinkDriverEx(path, 0); 110 | } 111 | 112 | /** 113 | * @brief Gets number of linked drivers to the FatFs module. 114 | * @param None 115 | * @retval Number of attached drivers. 116 | */ 117 | uint8_t FATFS_GetAttachedDriversNbr(void) 118 | { 119 | return disk.nbr; 120 | } 121 | -------------------------------------------------------------------------------- /source/ff_gen_drv.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file ff_gen_drv.h 4 | * @author MCD Application Team 5 | * @brief Header for ff_gen_drv.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 | 19 | /* Define to prevent recursive inclusion -------------------------------------*/ 20 | #ifndef __FF_GEN_DRV_H 21 | #define __FF_GEN_DRV_H 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | /* Includes ------------------------------------------------------------------*/ 28 | #include "diskio.h" 29 | #include "ff.h" 30 | #include "stdint.h" 31 | 32 | 33 | /* Exported types ------------------------------------------------------------*/ 34 | 35 | /** 36 | * @brief Disk IO Driver structure definition 37 | */ 38 | typedef struct 39 | { 40 | DSTATUS (*disk_initialize) (BYTE); /*!< Initialize Disk Drive*/ 41 | DSTATUS (*disk_status) (BYTE); /*!< Get Disk Status*/ 42 | DRESULT (*disk_read) (BYTE, BYTE*, DWORD, UINT); /*!< Read Sector(s)*/ 43 | DRESULT (*disk_write) (BYTE, const BYTE*, DWORD, UINT); /*!< Write Sector(s)*/ 44 | DRESULT (*disk_ioctl) (BYTE, BYTE, void*); /*!< I/O control operation*/ 45 | }Diskio_drvTypeDef; 46 | 47 | /** 48 | * @brief Global Disk IO Drivers structure definition 49 | */ 50 | typedef struct 51 | { 52 | uint8_t is_initialized[FF_VOLUMES]; 53 | const Diskio_drvTypeDef *drv[FF_VOLUMES]; 54 | uint8_t lun[FF_VOLUMES]; 55 | volatile uint8_t nbr; 56 | 57 | }Disk_drvTypeDef; 58 | 59 | /* Exported constants --------------------------------------------------------*/ 60 | /* Exported macro ------------------------------------------------------------*/ 61 | /* Exported functions ------------------------------------------------------- */ 62 | uint8_t FATFS_LinkDriver(const Diskio_drvTypeDef *drv, char *path); 63 | uint8_t FATFS_UnLinkDriver(char *path); 64 | uint8_t FATFS_LinkDriverEx(const Diskio_drvTypeDef *drv, char *path, BYTE lun); 65 | uint8_t FATFS_UnLinkDriverEx(char *path, BYTE lun); 66 | uint8_t FATFS_GetAttachedDriversNbr(void); 67 | 68 | #ifdef __cplusplus 69 | } 70 | #endif 71 | 72 | #endif /* __FF_GEN_DRV_H */ 73 | -------------------------------------------------------------------------------- /source/ffsystem_baremetal.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/source/ffsystem_baremetal.c -------------------------------------------------------------------------------- /source/ffsystem_cmsis_os.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32-mw-fatfs/710f8ced97d724f1bb9e4d5baa9f738d27b87f83/source/ffsystem_cmsis_os.c -------------------------------------------------------------------------------- /source/ffsystem_template.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------*/ 2 | /* A Sample Code of User Provided OS Dependent Functions for FatFs */ 3 | /*------------------------------------------------------------------------*/ 4 | 5 | #include "ff.h" 6 | 7 | 8 | #if FF_USE_LFN == 3 /* Use dynamic memory allocation */ 9 | 10 | /*------------------------------------------------------------------------*/ 11 | /* Allocate/Free a Memory Block */ 12 | /*------------------------------------------------------------------------*/ 13 | 14 | 15 | void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */ 16 | UINT msize /* Number of bytes to allocate */ 17 | ) 18 | { 19 | void* mem_ptr = NULL; /* Memory block to be allocated */ 20 | 21 | return mem_ptr; 22 | } 23 | 24 | 25 | void ff_memfree ( 26 | void* mblock /* Pointer to the memory block to free (no effect if null) */ 27 | ) 28 | { 29 | free(mblock); /* Free the memory block */ 30 | } 31 | 32 | #endif 33 | 34 | 35 | 36 | 37 | #if FF_FS_REENTRANT /* Mutal exclusion */ 38 | /*------------------------------------------------------------------------*/ 39 | /* Create a Mutex */ 40 | /*------------------------------------------------------------------------*/ 41 | /* This function is called in f_mount function to create a new mutex 42 | / or semaphore for the volume. When a 0 is returned, the f_mount function 43 | / fails with FR_INT_ERR. 44 | */ 45 | 46 | int ff_mutex_create ( /* Returns 1:Function succeeded or 0:Could not create the mutex */ 47 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 48 | ) 49 | { 50 | return 1; 51 | } 52 | 53 | 54 | /*------------------------------------------------------------------------*/ 55 | /* Delete a Mutex */ 56 | /*------------------------------------------------------------------------*/ 57 | /* This function is called in f_mount function to delete a mutex or 58 | / semaphore of the volume created with ff_mutex_create function. 59 | */ 60 | 61 | void ff_mutex_delete ( /* Returns 1:Function succeeded or 0:Could not delete due to an error */ 62 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 63 | ) 64 | { 65 | return 1; 66 | } 67 | 68 | 69 | /*------------------------------------------------------------------------*/ 70 | /* Request a Grant to Access the Volume */ 71 | /*------------------------------------------------------------------------*/ 72 | /* This function is called on enter file functions to lock the volume. 73 | / When a 0 is returned, the file function fails with FR_TIMEOUT. 74 | */ 75 | 76 | int ff_mutex_take ( /* Returns 1:Succeeded or 0:Timeout */ 77 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 78 | ) 79 | { 80 | return 1; 81 | } 82 | 83 | 84 | 85 | /*------------------------------------------------------------------------*/ 86 | /* Release a Grant to Access the Volume */ 87 | /*------------------------------------------------------------------------*/ 88 | /* This function is called on leave file functions to unlock the volume. 89 | */ 90 | 91 | void ff_mutex_give ( 92 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 93 | ) 94 | { 95 | return 1; 96 | } 97 | 98 | #endif /* FF_FS_REENTRANT */ 99 | 100 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------