PetitFS on GitHub

I posted an Arduino Library version of Petit FatFS on GitHub as PetitFS.

This example opens the file "TEST.TXT" and prints it to Serial.

// Petit FS test.   

#include "PetitFS.h"

// The SD chip select pin is currently defined as 10
// in pffArduino.h.  Edit pffArduino.h to change the CS pin.

FATFS fs;     /* File system object */
//------------------------------------------------------------------------------
void errorHalt(char* msg) {
  Serial.print("Error: ");
  Serial.println(msg);
  while(1);
}
//------------------------------------------------------------------------------
void test() {
  uint8_t buf[32];
  
  // Initialize SD and file system.
  if (pf_mount(&fs)) errorHalt("pf_mount");
  
  // Open test file.
  if (pf_open("TEST.TXT")) errorHalt("pf_open");
  
  // Dump test file to Serial.
  while (1) {
    UINT nr;
    if (pf_read(buf, sizeof(buf), &nr)) errorHalt("pf_read");
    if (nr == 0) break;
    Serial.write(buf, nr);
  }
}
//------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  test();
  Serial.println("\nDone!");
}
void loop() {}

The code size on an Uno with the default PetitFS options is:

Sketch uses 5,806 bytes (17%) of program storage space. Maximum is 32,256 bytes.
Global variables use 297 bytes (14%) of dynamic memory, leaving 1,751 bytes for local variables. Maximum is 2,048 bytes.

If you only select the _USE_READ and _FS_FAT32 options, the code size on Uno is:

Sketch uses 4,940 bytes (15%) of program storage space. Maximum is 32,256 bytes.
Global variables use 295 bytes (14%) of dynamic memory, leaving 1,753 bytes for local variables. Maximum is 2,048 bytes.

Wow! It would fit on some of the ATtiny parts.

Can it write to the SD card?

Wow! It would fit on some of the ATtiny parts.

I started with a version for ATtiny. Much or the space is taken by Serial.

Can it write to the SD card?

It has limited write capability. It does write to the SD "on the fly" by keeping SD chip select low during a "write transaction".

Here is the description of write:

The write function has some restrictions listed below:

Cannot create file. Only existing file can be written.

Cannot expand file size.

Cannot update time stamp of the file.

Write operation can start/stop on the sector boundary.

Read-only attribute of the file cannot block write operation.

File write operation must be done in following sequence.

  1. pf_lseek(ofs); read/write pointer must be moved to sector bondary prior to initiate write operation, or it will be rounded-down to the sector boundary at first write operation.

  2. pf_write(buff, btw, &bw); Initiate write operation. Write first data to the file.

  3. pf_write(buff, btw, &bw); Write next data. Any other file function cannot be used while a write operation is in progress.

  4. pf_write(0, 0, &bw); Finalize the write operation. If read/write pointer is not on the sector boundary, left bytes in the sector will be filled with zero.

The read/write pointer in the file system object advances in number of bytes written. After the function succeeded, *bw should be checked to detect end of file. In case of *bw is less than btw, it means the read/write pointer reached end of file during the write operation. Once a write operation is initiated, it must be finalized properly, or the written data can be lost.

I added a "PetitSerial" unbuffered Serial class.

The PetitFS example opens "TEST.TXT" and copies the file to the serial port using PetitSerial.

Here is the size of the example with only _USE_READ and _FS_FAT32 enabled.

Sketch uses 4,006 bytes (12%) of program storage space. Maximum is 32,256 bytes.
Global variables use 132 bytes (6%) of dynamic memory, leaving 1,916 bytes for local variables. Maximum is 2,048 bytes.

@fat16lib: I note that in your example, you open TEST.TXT, but you do not close it.

With SD or SdFat there is a specific "close" instruction.

Is the close instruction not required in PetitFS?

Is the close instruction not required in PetitFS?

This is the best source of information.

There is no close call. You can only open a single file. Opening a file effectively closes any previous file.

If you write to a file, you must end the write before opening a new file. See the rules and restrictions for write in the Petit FatFS web page.