Skip to content

Filesystem Usage

Richard Arthurs edited this page Jun 4, 2018 · 4 revisions

Writing to files

To write to a file, use sfu_write_fname(). The arguments are:

  • File suffix (type), we use macros for this. Think of this as the file name or the log type. sfu_fs_structure.h contains the definitions.
  • Data and optional arguments, printf style

Example:

#include "sfu_fs_structure.h"

sfu_write_fname(FSYS_SYS, "Hello world! %i", my_integer);

The filesystem will handle the timestamping and the file prefix automatically (more on this later).

File write length

The number of bytes (characters) you can write into a file is limited by SFU_MAX_DATA_WRITE in sfu_fs_structure.h. It is currently 21.

Nothing bad will happen if you exceed this (it may actually work early in the mission), but you risk your data being chopped off.

Adding a log file

Let's say you added a new sensor and you want to log the data from it. Here's how to add a log:

  1. in sfu_fs_structure.h, add a new #define for the file suffix. Make sure to increment the number that your define maps to by 1 from what's there already. Otherwise, your file won't be created.
  2. Using your spiffy new #define, call sfu_write_fname() with it

Filesystem Architecture

We use the filesystem called SPIFFS, which is designed to operate with NOR flash and small amounts of RAM on embedded devices.

Design:

  • Every sensor or log type is a separate file. So there's a file for the current monitor log, and a file for system messages.
  • To avoid file sizes getting too large, we create a new set of files every day
  • We must keep 7 days of logs
  • All writes to files are timestamped by the RTC automatically
  • We delete the oldest set of files to maintain only days' worth

File names:

File names are composed of a prefix and a suffix, one character each.

The file prefixes run from 'a' to 'g', one for each day from 1-7.

The suffixes start at 'A' and are used to distinguish logs/files for the different sensors and features.

This naming method was done to make it easy to delete files. When we need to create a new set of 'a' files on the 8th day, we must delete the old set of 'a' files first. SPIFFS is slow to search filenames, so keeping it short and unique is likely the most efficient way to do this.

The suffixes are a single character that characterizes what's in the log. It was kept to a single character to reduce overhead, and because we won't have very many different log types.

Examples:

'aA' - system log from day 1

'bA' - system log from day 2

'dC' - some other log from day 4

File Content

The data is written in files in the following format (ignore the '', spaces, and +)

timestamp + | + data_21_bytes + '/0'

The number of characters the timestamp takes depends on the mission epoch, and can be a maximum of 10 characters (but this would take a very long time). This variable-length timestamp is why we can only guarantee a user data length of SFU_MAX_DATA_WRITE bytes at any time. But early on in the mission, if the timestamp only takes 4 or 5 bytes, the user could, but should not, write more than 21 characters.