⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 r_misc.c

📁 ucos平台下的文件系统的源代码
💻 C
字号:
/*
 * 版本历史:
 *
 * 版本     作者        日期        修改内容
 * 1.00     王璀        2006-07-19  将InitRamDisk()更改为RamDiskInit()
 *                                  使用WIN32来控制是否使用ram.img文件
 *                                  其它一些小修改
*/

/*
**********************************************************************
*                          Micrium, Inc.
*                      949 Crestview Circle
*                     Weston,  FL 33327-1848
*
*                            uC/FS
*
*             (c) Copyright 2001 - 2003, Micrium, Inc.
*                      All rights reserved.
*
***********************************************************************

----------------------------------------------------------------------
File        : r_misc.c 
Purpose     : Device Driver for simple array in RAM
----------------------------------------------------------------------
Known problems or limitations with current version
----------------------------------------------------------------------
None.
---------------------------END-OF-HEADER------------------------------
*/

/*********************************************************************
*
*             #include Section
*
**********************************************************************
*/
#include <assert.h>
#include "stdio.h"
#include "fs_port.h"
#include "fs_dev.h" 
#include "fs_lbl.h" 
#include "fs_conf.h"

#include "fs_api.h"
#include "fs_clib.h"


/*********************************************************************
*
*             Local Variables        
*
**********************************************************************
*/

static char _array[(long)FS_RR_BLOCKNUM * FS_RR_BLOCKSIZE];
#ifdef WIN32
static FILE  *f;
#endif

/*********************************************************************
*
*             Local functions
*
**********************************************************************
*/

#ifdef WIN32
/*********************************************************************
*
*             RamFlush
*
  Description:
  flush ram to file.

  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:
  0 means dump successfully
  >0 means dump failure
*/
static int RamFlush()
{

  fseek(f, 0, SEEK_SET);
  fwrite(_array, FS_RR_BLOCKNUM * FS_RR_BLOCKSIZE*(sizeof(char)), 1, f);
  fflush(f);
  return 0;
}
#endif

/*********************************************************************
*
*             _FS_RAM_DevStatus
*
  Description:
  FS driver function. Get status of the RAM disk.

  Parameters:
  Unit        - Unit number.
 
  Return value:
  ==1 (FS_LBL_MEDIACHANGED) - The media of the device has changed.
  ==0                       - Device okay and ready for operation.
  <0                        - An error has occured.
*/

static int _FS_RAM_DevStatus(FS_u32 Unit) {
  static int online[1];

  if (Unit != 0) {
    return -1;  /* Invalid unit number */
  }
  if (!online[Unit]) {
    /* 
       Make sure, the function returns FS_LBL_MEDIACHANGED when it is
       called the first time
    */
    online[Unit] = 1;
    return FS_LBL_MEDIACHANGED;
  }
  return 0;
}


/*********************************************************************
*
*             _FS_RAM_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 _FS_RAM_DevRead(FS_u32 Unit, FS_u32 Sector, void *pBuffer) {
  if (Unit != 0) {
    return -1;  /* Invalid unit number */
  }
  if (Sector >= FS_RR_BLOCKNUM) {
    return -1;  /* Out of physical range */
  }
  FS__CLIB_memcpy(pBuffer, ((char*)&_array[0]) + Sector * FS_RR_BLOCKSIZE,
                  (FS_size_t)FS_RR_BLOCKSIZE);
  return 0;
}


/*********************************************************************
*
*             _FS_RAM_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 _FS_RAM_DevWrite(FS_u32 Unit, FS_u32 Sector, void *pBuffer) {
  if (Unit != 0) {
    return -1;  /* Invalid unit number */
  }
  if (Sector >= FS_RR_BLOCKNUM) {
    return -1;  /* Out of physical range */
  }
  FS__CLIB_memcpy(((char*)&_array[0]) + Sector * FS_RR_BLOCKSIZE, pBuffer,
                  (FS_size_t)FS_RR_BLOCKSIZE);
  #ifdef WIN32
  RamFlush();	//write to file
  #endif
  return 0;
}


/*********************************************************************
*
*             _FS_RAM_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 _FS_RAM_DevIoCtl(FS_u32 Unit, FS_i32 Cmd, FS_i32 Aux, void *pBuffer) {
  FS_u32 *info;

  Aux = Aux;  /* Get rid of compiler warning */
  if (Unit != 0) {
    return -1;  /* Invalid unit number */
  }
  switch (Cmd) {
    case FS_CMD_GET_DEVINFO:
      if (!pBuffer) {
        return -1;
      }
      info = pBuffer;
      *info = 0;  /* hidden */
      info++;
      *info = 2;  /* head */
      info++;
      *info = 4;  /* sec per track */
      info++;
      *info = FS_RR_BLOCKNUM;
      break;
    default:
      break;
  }
  return 0;
}


/*********************************************************************
*
*             Global function
*
**********************************************************************
*/
/*********************************************************************
*
*             RamDiskInit
*
  Description:
  Initialize Ramdisk from file:ram.img

  Parameters:
  no
 
  Return value:
  ==0 means init successfully
  !=0 means init failure
*/
int RamDiskInit()
{
  #ifdef WIN32
  FS_DISKFREE_T disk_data;
  #endif
  int x;

  #ifdef WIN32

  if((f = fopen("ram.img","rb+"))==NULL)	//if exist ram.img read it
  {
    _error("open ramdisk image fail""\r\n");
        
    f = fopen("ram.img", "wb+");		//or create and format it
    assert(f != NULL);

    x = FS_IoCtl("ram:", FS_CMD_FORMAT_AUTO, 0, 0);
    _log("create new ramdisk""\r\n");
    if (x!=0) 
    {
      _error("Cannot format RAM disk.""\r\n");
      return 1;
    }
  }
  else  
  {
    if(fread(_array, sizeof(char), FS_RR_BLOCKNUM * FS_RR_BLOCKSIZE, f) != FS_RR_BLOCKNUM * FS_RR_BLOCKSIZE)
    {
      _error("ramdisk image size error, auto format it""\r\n");
        
      x = FS_IoCtl("ram:", FS_CMD_FORMAT_AUTO, 0, 0);	//read error format it
      if (x!=0) 
      {
        _error("Cannot format RAM disk.""\r\n");
        return 1;
      }
    }
    else
    {
      x = FS_IoCtl("ram:", FS_CMD_GET_DISKFREE, 0, (void*)(&disk_data));
      if (x != 0)
      {
        _error("disk ram: is not formatted or corrupt, auto format it""\r\n");
        
        x = FS_IoCtl("ram:", FS_CMD_FORMAT_AUTO, 0, 0);
        if (x != 0) 
        {
          _error("Cannot format RAM disk.""\r\n");
          return 1;
        }
      }
      else
      {
        _log("read disk from file success""\r\n");
      }
    }
  }

  #else

  x = FS_IoCtl("ram:", FS_CMD_FORMAT_AUTO, 0, 0);
  if (x != 0) 
  {
    return 1;
  }

  #endif

  _FS_RAM_DevStatus(0);	//set status
  return 0;
}
/*********************************************************************
*
*             Global variables
*
**********************************************************************
*/

const FS__device_type FS__ramdevice_driver = {
  "RAMDISK device",
  _FS_RAM_DevStatus,
  _FS_RAM_DevRead,
  _FS_RAM_DevWrite,
  _FS_RAM_DevIoCtl
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -