📄 r_misc.c
字号:
/*
**********************************************************************
* Micrium, Inc.
* 949 Crestview Circle
* Weston, FL 33327-1848
*
* uC/FS
*
* (c) Copyright 2001 - 2006, Micrium, Inc.
* All rights reserved.
*
***********************************************************************
----------------------------------------------------------------------
File : r_misc.c
Purpose : Device Driver for simple array in RAM
---------------------------END-OF-HEADER------------------------------
*/
/*********************************************************************
*
* #include Section
*
**********************************************************************
*/
#include "fs_dev.h"
#include "fs_lbl.h"
#include "FS_ConfDefaults.h" /* FS Configuration */
#if FS_USE_RAMDISK_DRIVER
#include "fs_api.h"
#include "fs_clib.h"
#include "FS_Debug.h"
#ifdef WIN32
#include <windows.h>
#endif
/*********************************************************************
*
* Static data
*
**********************************************************************
*/
typedef struct {
FS_U8 aaaData[FS_RAMDISK_MAXUNIT][FS_RR_BLOCKNUM][FS_RAMDISK_SECTOR_SIZE];
} RAMDISK;
#ifndef FS_RAMDISK_ADDR
static RAMDISK _RamDrive;
static RAMDISK * _pRAMDrive = &_RamDrive;
#else
static RAMDISK * _pRAMDrive = (RAMDISK *) FS_RAMDISK_ADDR;
#endif
#ifdef WIN32
static int _ReadDelay;
static int _WriteDelay;
#endif
/*********************************************************************
*
* Static code
*
**********************************************************************
*/
/*********************************************************************
*
* _DevStatus
*
* Description:
* FS driver function. Get status of the RAM disk.
*
* Parameters:
* Unit - Device number.
*
* Return value:
* FS_MEDIA_STATEUNKNOWN if the state of the media is unknown.
* FS_MEDIA_ISNOTPRESENT if no card is present.
* FS_MEDIA_ISPRESENT if a card is present.
*/
static int _DevStatus(FS_U8 Unit) {
FS_USE_PARA(Unit);
return FS_MEDIA_IS_PRESENT;
}
/*********************************************************************
*
* _DevRead
*
* Description:
* FS driver function. Read a sector from the RAM disk.
*
* Parameters:
* Unit - Unit 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 _DevRead(FS_U8 Unit, FS_U32 Sector, void *pBuffer) {
if (Sector >= FS_RR_BLOCKNUM) {
FS_DEBUG_ERROROUT("_DevRead: Sector out of range\n");
return -1;
}
FS_MEMCPY(pBuffer, &_pRAMDrive->aaaData[Unit][Sector][0], FS_RAMDISK_SECTOR_SIZE);
#ifdef WIN32
Sleep(_ReadDelay);
#endif
return 0;
}
/*********************************************************************
*
* _ReadBurst
*
* Description:
* Read multiple sectors.
*
* Parameters:
* Unit - Unit number.
* SectorNo - First sector to be read from the device.
* NumSectors - Number of sectors 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 _ReadBurst(FS_U8 Unit, FS_U32 SectorNo, FS_U32 NumSectors, void *pBuffer) {
if (SectorNo + NumSectors > FS_RR_BLOCKNUM) {
FS_DEBUG_ERROROUT("_ReadBurst: Sector out of range\n");
return -1;
}
FS_MEMCPY(pBuffer, &_pRAMDrive->aaaData[Unit][SectorNo][0], NumSectors * FS_RAMDISK_SECTOR_SIZE);
#ifdef WIN32
Sleep(_ReadDelay);
#endif
return 0;
}
/*********************************************************************
*
* _WriteBurst
*
* Description:
* Read multiple sectors.
*
* Parameters:
* Unit - Unit number.
* SectorNo - First sector to be written to the device.
* NumSectors - Number of sectors to be written to the device.
* pBuffer - Pointer to buffer for holding the data.
*
* Return value:
* ==0 - O.K.: Sector has been written to device.
* <0 - An error has occured.
*/
static int _WriteBurst(FS_U8 Unit, FS_U32 SectorNo, FS_U32 NumSectors, const void *pBuffer) {
if (SectorNo + NumSectors > FS_RR_BLOCKNUM) {
FS_DEBUG_ERROROUT("_WriteBurst: Sector out of range\n");
return -1;
}
FS_MEMCPY(&_pRAMDrive->aaaData[Unit][SectorNo][0], pBuffer, NumSectors * FS_RAMDISK_SECTOR_SIZE);
#ifdef WIN32
Sleep(_WriteDelay);
#endif
return 0;
}
/*********************************************************************
*
* _DevWrite
*
* Description:
* FS driver function. Write sector to the RAM disk.
*
* Parameters:
* Unit - Unit 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 _DevWrite(FS_U8 Unit, FS_U32 Sector, const void *pBuffer) {
if (Sector >= FS_RR_BLOCKNUM) {
FS_DEBUG_ERROROUT("_DevWrite: Sector out of range\n");
return -1;
}
FS_MEMCPY(&_pRAMDrive->aaaData[Unit][Sector][0], pBuffer, FS_RAMDISK_SECTOR_SIZE);
#ifdef WIN32
Sleep(_WriteDelay);
#endif
return 0;
}
/*********************************************************************
*
* _DevIoCtl
*
* Description:
* FS driver function. Execute device command.
*
* Parameters:
* Unit - Unit number.
* Cmd - Command to be executed.
* Aux - Parameter depending on command.
* pBuffer - Pointer to a buffer used for the command.
*
* Return value:
* Command specific. In general a negative value means an error.
*/
static int _DevIoCtl(FS_U8 Unit, FS_I32 Cmd, FS_I32 Aux, void *pBuffer) {
FS_DEV_INFO *pInfo;
FS_USE_PARA(Unit);
FS_USE_PARA(Aux);
switch (Cmd) {
case FS_CMD_GET_DEVINFO:
if (!pBuffer) {
return -1;
}
pInfo = (FS_DEV_INFO *)pBuffer;
pInfo->NumSectors = FS_RR_BLOCKNUM;
pInfo->BytesPerSector = FS_RAMDISK_SECTOR_SIZE;
break;
#ifdef WIN32
case FS_CMD_SET_DELAY:
_ReadDelay = Aux;
_WriteDelay = (int)pBuffer;
break;
#endif
default:
break;
}
return 0;
}
/*********************************************************************
*
* Public data
*
**********************************************************************
*/
const FS_DEVICE_TYPE FS__ramdevice_driver = {
"ram",
FS_RAMDISK_MAXUNIT,
_DevStatus,
_DevRead,
_DevWrite,
_DevIoCtl,
_ReadBurst, /* Optional function */
_WriteBurst, /* Optional function */
NULL,
NULL
};
#endif /* FS_USE_RAMDISK_DRIVER */
/*************************** End of file ****************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -