NetBurner 3.5.8
PDF Version
Multiple Tasks

Example Path: examples/EFFS/Fat/MultiTask

This program illustrates accessing the file system from multiple tasks at the same time. The EFFS FAT library lets up to 10 tasks share one flash drive concurrently, with the library serializing the underlying card access. UserMain plus two additional tasks (Task1 and Task2) each:

  • Call f_enterFS() once (required in every task that touches the file system)
  • Select the external flash drive
  • Create, write, read, and re-read their own file (Main.txt, Task1.txt, and Task2.txt)
  • List the directory and then delay for a task-specific interval before repeating

Because each task works on its own file and loops on its own timer, their output interleaves on the debug serial port, demonstrating that several tasks can safely operate on the card at once.

File System Access

The EFFS FAT file system is a single shared resource that any number of RTOS tasks may use at the same time. To make that safe, the library keeps a small amount of per-task state, and each task must register itself before it makes any file system call. That registration is what f_enterFS() does. Specifically:

  • It allocates this task's file system context. The library tracks a current drive and current working directory separately for every task, so one task changing directories with f_chdir() doesn't move another task out from under it. f_enterFS() creates the slot that holds this task's context; until it is called, the library has no place to store that state for the task.
  • It enrolls the task in the file system's locking. Concurrent access is coordinated with internal mutual-exclusion. A task has to be registered for that locking to apply to it, which is why calling any other f_* function before f_enterFS() is undefined and will fail.
  • It must be called exactly once per task, before any other file system call. Calling it more than once in the same task, or calling file functions before it, is an error.
  • Slots are a limited resource. By default up to 10 tasks may be registered at once (configurable). A task that no longer needs file access should call f_releaseFS() to free its slot for another task.

This example puts that to work directly: UserMain(), Task1(), and Task2() each call f_enterFS() once before doing any file work, which is exactly what lets all three operate on the card concurrently without corrupting one another's state. The Basic example shows the simpler single-task case.

Note
All EFFS FAT examples require that you add the Embedded Flash File System File Allocation Table (EFFS FAT) library to your project: Add a Library to a Project