📄 generic_drv.c
字号:
/*
**********************************************************************
* Micrium, Inc.
* 949 Crestview Circle
* Weston, FL 33327-1848
*
* uC/FS
*
* (c) Copyright 2001 - 2003, Micrium, Inc.
* All rights reserved.
*
***********************************************************************
----------------------------------------------------------------------
File : generic_drv.c
Purpose : Skeleton device driver
----------------------------------------------------------------------
This is an example driver that should help implementing any kind of
data storage devices into the filesystem. The device you may use does
not need to provide any special functions other than reading data from
a given sector address.
---------------------------END-OF-HEADER------------------------------
*/
/*********************************************************************
*
* #include Section
*
**********************************************************************
*/
#include "fs_dev.h"
#include "fs_lbl.h"
#include "FS_ConfDefaults.h" /* FS Configuration */
/*
Wether this driver is being compiled into the FS or not can be
switched on/off in the global configuration file "fs_conf.h".
Refer the corresponding #define in the section "Device Driver Support"
*/
#if FS_USE_GENDEV_DRIVER /* only compiled if needed */
#include "fs_api.h" /* Standard includes that always must be included. */
#include "fs_clib.h"
/*********************************************************************
*
* #define constants
*
**********************************************************************
*/
/*
set the maximum number of devices that this driver can address.
*/
#define FS_GENDEV_MAX_DEVICE 1
/*
If the media size always remains the same, you can define a
constant size here.
*/
#define FS_GENDEV_NUMSEC 1024
/*********************************************************************
*
* Local Variables
*
**********************************************************************
*/
/*
To make sure you initialize the device only once, use a static
variable for that purpose.
*/
static char GenDev_Status[FS_GENDEV_MAX_DEVICE];
/*********************************************************************
*
* Local functions
*
**********************************************************************
*/
/*********************************************************************
*
* _FS_GENDEV_DevStatus
*
* Description:
* FS driver function. Gets status of the device. This function is also
* used to initialize the device and to detect a media change.
*
* Parameters:
* Unit - Device number.
*
* Return value:
* ==1 (FS_LBL_MEDIA_CHANGED) - The media of the device has changed.
* ==0 - Device okay and ready for operation.
* <0 - An error has occured.
*/
static int _FS_GENDEV_DevStatus(FS_U8 Unit) {
/* Make sure the initialization only happens once. */
if (GenDev_Status[Unit] != FS_MEDIA_ISPRESENT) {
GenDev_Status[Unit] = FS_MEDIA_ISPRESENT;
/*
Place your low level device initialization or reset code here
*/
/*
Make sure, the function returns FS_LBL_MEDIA_CHANGED when it is
called the first time.
*/
return FS_LBL_MEDIA_CHANGED;
}
return 0;
}
/*********************************************************************
*
* _FS_GENDEV_DevRead
*
* Description:
* FS driver function. Read a sector from the medium.
*
* Parameters:
* Unit - Device number.
* Sector - Sector to be read from the device.
* pBuffer - Pointer to buffer for storing the data.
*
* Return value:
* ==0 - Sector has been read and copied to pBuffer.
* <0 - An error has occured.
*/
static int _FS_GENDEV_DevRead(FS_U8 Unit, FS_U32 Sector, void *pBuffer) {
/*
The next test checks if the given sector address is within a valid range.
Depending on your device, you can set a constant value or calculate
this value from media descriptor of your hardware.
*/
if (Sector >= FS_GENDEV_NUMSEC) {
return -1; /* Out of sector range */
}
/*
Place the code that reads from your media here. The number of bytes
that has to be read equal to the sector size. This value is defined
as FS_SEC_SIZE. Below you can see the parameter that are used to
transfer a sector from the file system to the storage device.
Sector - Sector Number.
*pBuffer - Pointer to the buffer were sector data is stored.
FS_SEC_SIZE - Number of bytes that must be transferred.
*/
return 0;
}
/*********************************************************************
*
* _FS_GENDEV_DevWrite
*
* Description:
* FS driver function. Write sector to the RAM disk.
*
* Parameters:
* Unit - Device number.
* Sector - Sector to be written to the device.
* pBuffer - Pointer to data to be stored.
*
* Return value:
* ==0 - Sector has been written to the device.
* <0 - An error has occured.
*/
static int _FS_GENDEV_DevWrite(FS_U8 Unit, FS_U32 Sector, void *pBuffer) {
/*
The next test checks if the given sector address is within a valid range.
Depending on your device, you can set a constant value or calculate
this value from media descriptor of your hardware.
*/
if (Sector >= FS_GENDEV_NUMSEC) {
return -1; /* Out of sector range */
}
/*
Place the code that writes data onto your media here. The number of bytes
that has to be read equal to the sector size. This value is defined
as FS_SEC_SIZE. Below you can see the parameter that are used to
transfer a sector from the storage device to the file system.
Sector - Sector Number.
*pBuffer - Pointer to the buffer were sector data is stored.
FS_SEC_SIZE - Number of bytes that must be transferred.
*/
return 0;
/* If you would like to use a read only device, just return here with zero. */
}
/*********************************************************************
*
* _FS_GENDEV_DevIoCtl
*
* Description:
* FS driver function. Execute device command.
*
* Parameters:
* Unit - Device number.
* Cmd - Command to be executed.
* Aux - Parameter depending on command.
* pBuffer - Pointer to a buffer used for the command.
*
* Return value:
* This function is used to execute device specific commands.
* In the file fs_api.h you can find I/O commands that are currently
* implemented. If the higher layers don't know the command, they
* send it to the next lower. Your driver does not have to
* implement one of these commands. Only if automatic formatting
* is used or user routines need to get the size of the medium,
* FS_CMD_GET_DEVINFO must be implemented.
*/
static int _FS_GENDEV_DevIoCtl(FS_U8 Unit, FS_I32 Cmd, FS_I32 Aux, void *pBuffer) {
FS_DEV_INFO *pInfo;
/*
FS_DEV_INFO is a simple structure defined in fs_dev.h that is filled with
the geometrical values of the device.
*/
FS_USE_PARA(Aux);
/* This check makes sure, the device number is within valid range */
switch (Cmd) { /* Filter out the command that you want to response to */
case FS_CMD_GET_DEVINFO:
if (pBuffer == 0) { /* avoid writing into non existing buffers */
return -1;
}
pInfo = (FS_DEV_INFO *)pBuffer; /* The parameter pBuffer contains the pointer to the structure */
/*
The number of hidden sectors is used the reserve a certain amount of sectors for
operating system specific data and the partition table. If you do not need or
have a partition table or special system boot code on the drive, the number
of hidden sectors is zero.
*/
pInfo->NumHeads = 0; /* Relevant only for mechanical drives */
pInfo->SectorsPerTrack = 2; /* Relevant only for mechanical drives */
pInfo->NumSectors = FS_GENDEV_NUMSEC; /* Number of total sectors */
/* Now all necessary information is complete. */
break;
}
return 0; /* Return with zero if no problems have occurred. */
}
/*********************************************************************
*
* Global variables
*
**********************************************************************
*/
/* This structure contains pointers to the functions that all drivers
of the file system must provide. */
const FS_DEVICE_TYPE FS__genericdevice_driver = {
"gen", /* Name of the device */
1,
_FS_GENDEV_DevStatus, /* Device state */
_FS_GENDEV_DevRead, /* Device read sector */
_FS_GENDEV_DevWrite, /* Device write sector */
_FS_GENDEV_DevIoCtl /* IO control interface */
};
#endif /* FS_USE_GENDEV_DRIVER */
/*************************** End of file ****************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -