# abbrev/fatfs architecture

> Architecture diagram of the GitHub repository abbrev/fatfs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abbrev/fatfs
- Repository: https://github.com/abbrev/fatfs

## Overview

##### Project type and purpose
– Project type: A low-level, open-source file-system library for small embedded systems (FAT12/16/32).
– Purpose: Provide a generic, portable FAT file-system API for applications, abstracting away hardware differences.

##### File structure
Top-level directories:
- documents/ : HTML user manual, example apps, and reference.
- source/    : Core C modules (ff.c, ffsystem.c, ffunicode.c) and disk I/O stub (diskio.c/.h), plus configuration (ffconf.h).
- LICENSE.txt, README.asc

Key modules in source/:
- ff.c        – Public API handlers (f_open, f_read, f_write, etc.)
- ffsystem.c  – Internal FS logic (cluster chains, FAT table updates)
- ffunicode.c – Unicode path support
- diskio.c/.h – Abstraction layer to physical media (SD card, SPI flash, etc.)
- ffconf.h    – Compile-time configuration (sector size, code page, feature toggles)

##### README for insights
– Confirms “generic FAT file-system module for small embedded systems.”
– Directs users to a forum for bug reports.
– No built-in example apps in the code tree; examples reside in documents/res as .c files.

##### Main components to illustrate
A. Application Layer (User Code)
- Example apps (documents/res/*.c) or any embedded application
- Invokes FatFs API: f_mount(), f_open(), f_read(), f_write(), f_close(), etc.
B. FatFs Core Library
- API Interface module (ff.c)
- File-system engine (ffsystem.c)
- Unicode support (ffunicode.c)
C. Configuration
- ffconf.h – Controls features (long-file name support, reentrancy, code page)
D. Disk I/O Abstraction Layer
- diskio.h – Defines drive number, data types, I/O function prototypes
- diskio.c – Weak stub implementations; to be overridden by hardware-specific drivers
E. Hardware/Media
- Physical block devices (SD card, SPI Flash, USB Mass Storage)
- Platform driver code (outside this repo) that implements disk_read(), disk_write(), disk_ioctl()

##### Relationships and interactions
1. The Application makes calls to the FatFs API in ff.c
2. ff.c calls into ffsystem.c and ffunicode.c, depending on configuration
3. ffsystem.c processes file allocation, directory scanning, FAT updates
4. When ffsystem.c needs to read/write raw sectors, it calls the disk I/O interface functions (disk_read, disk_write, disk_ioctl) defined in diskio.h
5. The target platform provides a diskio.c override or hardware driver to perform actual I/O on the block device

##### Architectural patterns and design principles
– Layered architecture (API → FS engine → I/O abstraction → Hardware)
– Dependency inversion: FS engine depends on abstract I/O interface, not on concrete hardware
– Single‐responsibility modules: each C file encapsulates a distinct concern
– Configurable features via compile-time flags in ffconf.h

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Application Layer
    subgraph "Application Layer"
        app1["app1.c"]:::app
        app2["app2.c"]:::app
        app3["app3.c"]:::app
        app4["app4.c"]:::app
        app5["app5.c"]:::app
        app6["app6.c"]:::app
    end

    %% FatFs Core Modules
    subgraph "FatFs Core Library"
        API["API Interface\n(ff.c, ff.h)"]:::module
        FS["FS Engine\n(ffsystem.c)"]:::module
        UNI["Unicode Support\n(ffunicode.c)"]:::module
        CONF["Configuration\n(ffconf.h)"]:::config
        IO["Disk I/O Abstraction\n(diskio.c, diskio.h)"]:::module
    end

    %% Hardware and Documentation
    HW[(Block Device)]:::external
    DOC["Documentation\n(doc HTML)"]:::doc

    %% Connections
    app1 -->|"f_mount(), f_open(), f_read(), f_write(), f_close()"| API
    app2 -->|"f_mount(), f_open(), f_read(), f_write(), f_close()"| API
    app3 -->|"f_mount(), f_open(), f_read(), f_write(), f_close()"| API
    app4 -->|"f_mount(), f_open(), f_read(), f_write(), f_close()"| API
    app5 -->|"f_mount(), f_open(), f_read(), f_write(), f_close()"| API
    app6 -->|"f_mount(), f_open(), f_read(), f_write(), f_close()"| API

    API -->|"calls internal logic"| FS
    API -->|"unicode paths"| UNI
    API -.->|"includes at compile-time"| CONF

    FS -->|"disk_read(), disk_write(), disk_ioctl()"| IO
    IO -->|"raw sector I/O"| HW

    DOC -.->|"reference"| API

    %% Click Events
    click app1 "https://github.com/abbrev/fatfs/blob/master/documents/res/app1.c"
    click app2 "https://github.com/abbrev/fatfs/blob/master/documents/res/app2.c"
    click app3 "https://github.com/abbrev/fatfs/blob/master/documents/res/app3.c"
    click app4 "https://github.com/abbrev/fatfs/blob/master/documents/res/app4.c"
    click app5 "https://github.com/abbrev/fatfs/blob/master/documents/res/app5.c"
    click app6 "https://github.com/abbrev/fatfs/blob/master/documents/res/app6.c"
    click API "https://github.com/abbrev/fatfs/blob/master/source/ff.c"
    click FS "https://github.com/abbrev/fatfs/blob/master/source/ffsystem.c"
    click UNI "https://github.com/abbrev/fatfs/blob/master/source/ffunicode.c"
    click CONF "https://github.com/abbrev/fatfs/blob/master/source/ffconf.h"
    click IO "https://github.com/abbrev/fatfs/blob/master/source/diskio.c"
    click DOC "https://github.com/abbrev/fatfs/blob/master/documents/00index_e.html"

    %% Styles
    classDef module fill:#ADD8E6,stroke:#333,stroke-width:1px;
    classDef config fill:#98FB98,stroke:#333,stroke-width:1px;
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px;
    classDef app fill:#E6E6FA,stroke:#333,stroke-width:1px;
    classDef doc fill:#FFFACD,stroke:#333,stroke-width:1px;
```
