Skip navigation

emFile—RAM Disk Device Driver

Simple device driver that uses RAM as storage.

About the Driver

emFile comes with a simple RAM disk driver that makes it possible to use a portion of your system RAM as a drive for data storage. This can be very helpful to examine your system performance and may also be used as an in-system test procedure.

Theory of Operation

A RAM disk is a portion of memory that you allocate to use as a partition. The RAM disk driver takes some of your memory and pretends that it is a storage device that you can format, mount, save files to, etc. Remember that every bit of RAM is important for the well being of your system and the bigger your RAM disk is, the less memory there is available for your system.

Supported Devices

The RAM driver can be used with every target with sufficient RAM. The size of the disk is defined as the number of sectors reserved for the drive.

Fail-Safe Operation

When power is lost, the data of the RAM drive is typically lost as well except for systems with battery backup for the RAM used as storage device. For this reason, fail safety is relevant only for systems which provide such battery backup.

Unexpected reset

The data will be preserved. However, if the power failure / unexpected reset interrupts a write operation, the data of the sector may contain partially invalid data.

Power failure

Power failure causes an unexpected reset and has the same effects.

Wear Leveling

The RAM disk driver does not require wear leveling.

Formating

A RAM disk is unformatted after each startup. Exceptions to this rule are RAM disks that use battery backed up memory. Before any data can be stored on the RAM disk, the RAM disk has to be formatted using the FS_Format() function.

If one RAM disk driver is configured the FS_Format() function can be called with an empty string as device name, for example: FS_Format("", NULL). If more then one RAM disk is configured, you have to specify the device name. For example, FS_Format("ram:0:", NULL) for the first device and FS_Format("ram:1:", NULL) for the second and so on.

Sample Configuration

The following sample shows how to configure a RAM disk instance. To add the driver, FS_AddDevice() is called with the driver label set to FS_RAMDISK_Driver. This function is called from within  FS_X_AddDevices() which is called at the file system initialization. The size of the storage is defined as the number of sectors reserved for the drive. Each sector in this sample consists of 512 bytes but other sector sizes are also supported. The minimum value for the number of sectors when using a file system is 7. A FAT file system needs a minimum sector size of 512 bytes. 

#define ALLOC_SIZE          0x1100    // Memory pool for the file system in bytes
#define NUM_SECTORS         16        // Number of sectors on the RAM storage
#define BYTES_PER_SECTOR    512       // Size of a sector in bytes

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
static U32 _aMemBlock[ALLOC_SIZE / 4];                        // Memory pool used for semi-dynamic allocation.
static U32 _aRAMDisk[(NUM_SECTORS * BYTES_PER_SECTOR) / 4];   // Memory to be used as storage.

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/

/*********************************************************************
*
*       FS_X_AddDevices
*
*  Function description
*    This function is called by the FS during FS_Init().
*    It is supposed to add all devices, using primarily FS_AddDevice().
*
*  Note
*    (1) Other API functions
*        Other API functions may NOT be called, since this function is called
*        during initialization. The devices are not yet ready at this point.
*/
void FS_X_AddDevices(void) {
  FS_AssignMemory(&_aMemBlock[0], sizeof(_aMemBlock));
  //
  // Add and configure the driver.
  //
  FS_AddDevice(&FS_RAMDISK_Driver);
  FS_RAMDISK_Configure(0, _aRAMDisk, BYTES_PER_SECTOR, NUM_SECTORS);
#if FS_USE_FILE_BUFFER
  //
  // Enable the file buffer to increase the performance when reading/writing a small number of bytes.
  //
  FS_ConfigFileBufferDefault(512, FS_FILE_BUFFER_WRITE);
#endif
}