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

📄 storage.c

📁 增强型51单片机fs7821cf卡读写程序
💻 C
字号:
/*
**********************************************************************************************
* Project:	TK7821
* File:		Storage.c
* Contents: 
*          The storage access wrapper 
*
* $Date: 02/25/05    Jason    v0.1
*        04/21/05    Mingo    v0.2   www.fameg.com
*
* Copyright (c) 2005 Fameg, Inc. All rights reserved
***********************************************************************************************
*/

#include <reg51.h>
#include <string.h>
#include "Device.h"
#include "UsbDisk.h"
#include "Storage.h"
#include "ScsiCmd.h"

bit AccessStatus;          
bit AccessWritable = false;
BYTE AccessDevice;         
BYTE CmdTimer;

BYTE LastDevice;
idata BYTE DeviceType[MAX_DEVICE_NUM];

UINT16 CopyBuffer = DMA_BUFFER2; // Address of temporary block copy buffer
UINT16 CurrentBuffer;
bit Authenticated = false;
xdata TDeviceInfo DeviceInfo;
idata STATUS CfMediaStatus = STATUS_NO_MEDIA;

//----------------------------------------------------------------------------
// Description:
//   Initiate the storage interfaces
//----------------------------------------------------------------------------
void StorageInit()
{

  LastDevice = 0;

  CurrentBuffer = DMA_BUFFER0;


  #ifndef DEBUG_UART
    CfInit();
    DeviceType[LastDevice] = DEVICE_CF;
  #endif
}

//--------------------------------------------------------------------
// Make the storage devices into suspend mode
//--------------------------------------------------------------------
void StorageSuspend()
{
    CfSuspend();
}

//--------------------------------------------------------------------
// Make the storage devices resume
//--------------------------------------------------------------------
void StorageResume()
{
    CfPowerOn(); // Fix the problem that some CF cards will hold 
                 // data bus low when power off
    CfResume(); // Resume CF first or SM might fail
}

//----------------------------------------------------------------------------
// Description:
//   Get the next buffer address
//----------------------------------------------------------------------------
UINT16 GetNextBuffer(UINT16 Buffer)
{
  if (((TDataCast *)&Buffer)->ucByte[0] == (DMA_BUFFER0 >> 8))
    return DMA_BUFFER1;
  else
    return DMA_BUFFER0;
}

//---------------------------------------------------------------------------
// Used to change device STATUS when StartStopUint "Start" cmd issued 
//---------------------------------------------------------------------------
void StorageStart()
{
    switch(ScsiLun)
    {
        case DEVICE_CF:  CfStart();  return;
    }
}

//---------------------------------------------------------------------------
// Used to change device STATUS when StartStopUint "Stop" cmd issued
//---------------------------------------------------------------------------
void StorageEject()
{
    switch(ScsiLun)
    {
        case DEVICE_CF:  CfEject();  return;
    }
}

//----------------------------------------------------------------------------
//   Query one storage device for status
//----------------------------------------------------------------------------
STATUS QueryDevice()
{
    xdata BYTE Status;

    switch (DeviceType[ScsiLun])
    {
        case DEVICE_CF: Status = CfQueryDevice(); break;
        default:
            return STATUS_PARAM_ERROR;
    }

    return Status;
}

//----------------------------------------------------------------------------
//   Storage sector read routine
//----------------------------------------------------------------------------
STATUS ReadSector()
{
    BYTE Status;


    AccessStatus = true; 
    AccessDevice = ScsiLun;
    CurrentBuffer = GetNextBuffer(CurrentBuffer);

    switch (DeviceType[ScsiLun])
    {
        case DEVICE_CF: Status = CfReadSector(); break;
        default:
            return STATUS_PARAM_ERROR;
    }

    return Status;
}

//----------------------------------------------------------------------------
//   Storage next sector read routine
//----------------------------------------------------------------------------
STATUS ReadNextSector()
{
    BYTE Status;
  
    CurrentBuffer = GetNextBuffer(CurrentBuffer);

    switch (DeviceType[ScsiLun])
    {
        case DEVICE_CF: Status = CfReadNextSector(); break;
        default:
            return STATUS_PARAM_ERROR;
    }

    return Status;
}

//----------------------------------------------------------------------------
//   Storage sector write routine
//----------------------------------------------------------------------------
STATUS WriteSector()
{
    BYTE Status;

    AccessStatus = true;
    AccessDevice = ScsiLun;

    switch (DeviceType[ScsiLun])
    {
        case DEVICE_CF: Status = CfWriteSector(); break;
        default:
            Status = STATUS_PARAM_ERROR;
    }
    return Status;
}

//----------------------------------------------------------------------------
//   Storage next sector write routine
//----------------------------------------------------------------------------
STATUS WriteNextSector()
{
    BYTE Status;

    switch (DeviceType[ScsiLun])
    {
        case DEVICE_CF: Status = CfWriteNextSector(); break;
        default:
            Status = STATUS_PARAM_ERROR;
    }
    return Status;
}

//----------------------------------------------------------------------------
// Convert Big/Little endian to Little/Big endian, UINT32 applyed. 
//  (Due to Keil simulation result,system was Big Endian)
//----------------------------------------------------------------------------
UINT32 ConvertEndian32(UINT32 Src)
{
  UINT32 Des;
  PBYTE pSrc = (PBYTE)(&Src);
  PBYTE pDesc = (PBYTE)(&Des);
	
  pDesc[0] = pSrc[3];	
  pDesc[1] = pSrc[2];	
  pDesc[2] = pSrc[1];
  pDesc[3] = pSrc[0];	
  
  return Des; 
}

//----------------------------------------------------------------------------
// Convert Big/Little endian to Little/Big endian, UINT16 applyed. 
//  (Due to Keil simulation result,system was Big Endian)
//----------------------------------------------------------------------------
UINT16 ConvertEndian16(UINT16 Src)
{
  UINT16 Des;
  PBYTE pSrc = (PBYTE)(&Src);
  PBYTE pDesc = (PBYTE)(&Des);
	
  pDesc[0] = pSrc[1];	
  pDesc[1] = pSrc[0];	

  return Des; 
}    

//----------------------------------------------------------------------------
// Convert Big/Little endian to Little/Big endian, UINT16 applyed. 
//  (Due to Keil simulation result,system was Big Endian)
//----------------------------------------------------------------------------
void ConvertBufEndian16(UINT16 Buffer, UINT16 Length)
{
  UINT16 i;
  BYTE Temp;

  for (i = Buffer; i < (Buffer + Length); i += 2)
  {
    Temp = *(BYTE xdata *)i;
    *(BYTE xdata *)i = *(BYTE xdata *)(i + 1);
    *(BYTE xdata *)(i + 1) = Temp;
  }
}

⌨️ 快捷键说明

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