RAM Disk device driver
A simple device driver for using system RAM as storage.
Overview
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
RAM disks are a portion of memory allocated to be used as a partition. The RAM disk driver takes some of that memory and uses it as a storage device. This storage device can then be formatted, mounted, have files saved to it, etc. Every bit of RAM is important for the well being of a system. The bigger a RAM disk is, the less memory will be available for the system.
Supported devices
The RAM driver can be used with every target that has enough 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.
Formatting
A RAM disk is reformatted 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 than one RAM disk is configured, the device name must be specified. 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
}