# Petit FatFs Module Application Note

### Basic Considerations

The FatFs module assumes following conditions on portability.

- ANSI C
  The Petit FatFs module is a middleware written in ANSI C (C89). There is no platform dependence, so long as the compiler is in compliance with C89 or later.
- Size of integer types
  - When the C standard is in C89, size of `char`/`short`/`long` must be 8/16/32-bit respectively.
  - When it is in C99 or later, `stdint.h` is used to obtain the correct sizes.
  - Size of `char` must be 8-bit.
  - Size of `int`, as well as integer promotion, must be 16-bit or 32-bit.
  Integer types used in Petit FatFs are defined in `pff.h`. This will not be a problem on most platform. When a conflict with existing definitions occured, you must resolve it with care.

### Memory Usage (R0.03a)

| AVR | PIC24 | CM0
--- | --- | --- | ---
Compiler | gcc | C30 | gcc
text (default) | 1948 | 1962 | 1114
text (!PF_USE_READ) | -356 | -288 | -196
text (PF_USE_DIR) | +552 | +528 | +370
text (PF_USE_LSEEK) | +474 | +432 | +172
text (PF_USE_WRITE) | +440 | +453 | +276
bss | 2 | 2 | 4
Work | 42 | 42 | 44

Other options are set as: `PF_FS_FAT16 = 1`, `PF_FS_FAT12 = 0`, `PF_FS_FAT32 = 0` and `PF_USE_LCC = 0`. This is the size of the Petit FatFs module itself. In addition to this, a low level disk I/O module will be required for a complete function. For instance, size of MMC/SDC module on AVR becomes approximate 620 bytes without write function and 840 bytes with write function.

### Module Size Reduction

Follwing table shows which function is removed by configuration options for the module size reduction.

Function | PF_USE_READ | PF_USE_DIR | PF_USE_LSEEK | PF_USE_WRITE
0 | 0 | 0 | 0
pf_mount |  |  |  |
pf_open |  |  |  |
pf_read | x |  |  |
pf_lseek |  |  | x |
pf_opendir |  | x |  |
pf_readdir |  | x |  |
pf_write |  |  |  | x

### Performance effective file access

For good performance on reading a file on the small embedded system, application programmer should consider what process is done in the file system module.

The Petit FatFs reads the disk sectors without a sector buffer. This means the file system reads a part of the sector contains the required data every reference point even if they are in the same sector. However the generic storage device are not byte addressable so that the disk I/O layer will read the entire sector and pick up the data bytes from the read data steram.

When read 512 byte data from a file at a time, the data sector will be read only a time. When read that data in byte-by-byte, the data sector will be read 512 times. Therefore the byte-by-byte read request will drastically decrease the read performance. To avoid this stupid read controls, the file data should be read in long block as possible. Sector alignment access is not impotant on the Petit FatFs.

The tiny microcontrollers targeted by Petit FatFs has a limited size of RAM. It may not able to allocate a certain size of read buffer and most type of text processing will require byte-by-byte read operation. The Petit FatFs supports data forwarding feature for such purpose.

### About FatFs License

Petit FatFs has being developped as a personal project of author, ChaN. It is free from the code anyone else wrote. Following code block shows a copy of the license document that included in the source files.

    /*----------------------------------------------------------------------------/
    /  Petit FatFs - FAT file system module  R0.03a
    /-----------------------------------------------------------------------------/
    /
    / Copyright (C) 2019, ChaN, all right reserved.
    /
    / Petit FatFs module is an open source software. Redistribution and use of
    / Petit FatFs in source and binary forms, with or without modification, are
    / permitted provided that the following condition is met:
    /
    / 1. Redistributions of source code must retain the above copyright notice,
    /    this condition and the following disclaimer.
    /
    / This software is provided by the copyright holder and contributors "AS IS"
    / and any warranties related to this software are DISCLAIMED.
    / The copyright owner or contributors be NOT LIABLE for any damages caused
    / by use of this software.
    /-----------------------------------------------------------------------------/
    ...

Therefore FatFs license is one of the BSD-style licenses but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, does not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses including GNU GPL. When you redistribute the FatFs source code with any changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that not conflict with FatFs license.

Return