Creating a Media Driver: The Driver’s Initialisation Function
[Creating a FreeRTOS+FAT Media Driver]
Actions performed within a media driver’s initialisation
function (* optional steps)
The FF_Disk_t structure contains a pointer to an FF_IOManager_t structure. The FF_IOManager_t structure is created by calling FF_CreateIOManager().
For convenience, the media driver can optionally also mount a partition on the media, and add the mounted partition to FreeRTOS+FAT’s virtual file system. Performing these two optional steps within the initialisation function removes the need for the application writer to perform them explicitly. The image on the right shows the media driver’s initialisation function performing these optional steps.
Worked ExampleAs an example, below is the outline of the media driver initialisation function used by the FreeRTOS+FAT RAM disk driver. The full version, which includes additional error checking, can be found in /FreeRTOS-Plus/Source/FreeRTOS-Plus-FAT/portable/common/ff_ramdisk.c.
/* The size of each sector on the disk. */
#define ramSECTOR_SIZE 512UL
/* Only a single partition is used. Partition numbers start at 0. */
#define ramPARTITION_NUMBER 0
In this example:
– pcName is the name to give the disk within FreeRTOS+FAT’s virtual file system.
– pucDataBuffer is the start of the RAM buffer used as the disk.
– ulSectorCount is effectively the size of the disk, each sector is 512 bytes.
– xIOManagerCacheSize is the size of the IO manager’s cache, which must be a
multiple of the sector size, and at least twice as big as the sector size.
FF_Disk_t *FF_RAMDiskInit( char *pcName,
size_t xIOManagerCacheSize )
FF_Disk_t *pxDisk = NULL;
/* Check the validity of the xIOManagerCacheSize parameter. */
configASSERT( ( xIOManagerCacheSize % ramSECTOR_SIZE ) == 0 );
configASSERT( ( xIOManagerCacheSize >= ( 2 * ramSECTOR_SIZE ) ) );
/* Attempt to allocated the FF_Disk_t structure. */
pxDisk = ( FF_Disk_t * ) pvPortMalloc( sizeof( FF_Disk_t ) );
if( pxDisk != NULL )
/* It is advisable to clear the entire structure to zero after it has been
allocated – that way the media driver will be compatible with future
FreeRTOS+FAT versions, in which the FF_Disk_t structure may include
additional members. */
memset( pxDisk, ‘