Implementing a RAM Driver


Implementing a RAM driver for the file system is simple. There is no physical driver associated with the RAM driver.

  1. Include the ramdrv_s.c and ramdrv_s.h files in your file system build. This ensures that it can be mounted.
  2. Call f_init() as shown in the example below.
  3. Call f_mountdrive() with a pointer to the memory area and the size of the area to be used for the driver. The example below shows this.
  4. The RAM drive may now be used as a standard drive.

The following example shows the implementation.

static long g_tramdrive[RAM_DRIVE_SIZE / sizeof( long )];   /* Must be 32 bit aligned */
static int initted = 0;   /* Flag to signal if volume has been initialized */

void create_ram_drive()
{
    int rc;

    rc = f_init();
 
    if (rc == SUCCESS)
    {
        rc = f_start();
    }

    if (rc == SUCCESS)
    {
        rc = f_enterFS();
    }
 
    if (rc == SUCCESS)
    {
        if ( !initted )
        {
            /* Only do this once at first power on. Fill drive with random value */
            (void)memset( g_tramdrive, 0x55, sizeof( g_tramdrive ) );
            initted = 1;
        }
     }

/* Now mount the RAM drive, calling the mount function of the target driver */ 

    if (rc == SUCCESS)
    {
        rc = f_mountdrive( 0, g_tramdrive, sizeof( g_tramdrive ), fs_mount_ramdrive, 0 );
    }
 
    if (rc == SUCCESS)
    {
        rc = f_chdrive( 0 );  /* Change to the newly mounted drive */
    }
 
    return rc;
}