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

📄 ide.c

📁 CY7C68033芯片 USB DEVICE 代码;标准c代码
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// IDE.c
// This file contains the IDE specific portions of the code.
//
// Format:  indent 3.  NO TABS!
//
//--------------------------------------------------------------------------
// $Archive: /USB/atapifx2/NX2LP/SRC/CY3686FW/ide.c $
// $Modtime: 8/31/05 7:56a $
// $Revision: 2 $
//--------------------------------------------------------------------------
//
// Copyright 2005, Cypress Semiconductor Corporation.
//
// This software is owned by Cypress Semiconductor Corporation (Cypress)
// and is protected by and subject to worldwide patent protection (United
// States and foreign), United States copyright laws and international 
// treaty provisions. Cypress hereby grants to licensee a personal, 
// non-exclusive, non-transferable license to copy, use, modify, create 
// derivative works of, and compile the Cypress Source Code and derivative 
// works for the sole purpose of creating custom software in support of 
// licensee product to be used only in conjunction with a Cypress integrated 
// circuit as specified in the applicable agreement. Any reproduction, 
// modification, translation, compilation, or representation of this 
// software except as specified above is prohibited without the express 
// written permission of Cypress.
//
// Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND,EXPRESS OR IMPLIED, 
// WITH REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Cypress reserves the right to make changes without further notice to the
// materials described herein. Cypress does not assume any liability arising
// out of the application or use of any product or circuit described herein.
// Cypress does not authorize its products for use as critical components in
// life-support systems where a malfunction or failure may reasonably be
// expected to result in significant injury to the user. The inclusion of
// Cypress' product in a life-support systems application implies that the
// manufacturer assumes all risk of such use and in doing so indemnifies
// Cypress against all charges.
//
// Use may be limited by and subject to the applicable Cypress software
// license agreement.
//
//--------------------------------------------------------------------------

#include "globals.h"

void loadEP4BC(WORD dataLen);

void cMedia() { if (bEject) sensePtr = senseNoMedia; else sensePtr=senseOk; }

// This command is only used for removable DRIVES, not removable media
// An example of removable drive is Compact Flash 
#define checkForMedia(x) cMedia()


// From SCSI spec SPC (SCSI primary commands)
// Byte 0 -- 70 = Current error
// Byte 1 -- Segment number
// Byte 2 -- Sense key (SPC table 107)
//             5 = ILLEGAL REQUEST. Indicates that there was an illegal parameter in 
//                 the CDB or in the additional parameters supplied as data for some commands
//             B = ABORTED COMMAND. Indicates that the device server aborted the command.
//                 The application client may be able to recover by trying the command again.
//             E = MISCOMPARE. Indicates that the source data did not match the data read 
//                 from the medium.
// Byte 3-6  -- Information (not used)
// Byte 7    -- add'l sense length 
// byte 8-11 -- Command specific information
// byte 12   -- ASC (Add'l sense code)
// byte 13   -- ASQ (Add'l sense qualifier)
//                                                Key                                                        ASC   ASQ
//                                    0     1     2     3     4     5     6     7     8     9    10    11    12    13    14    15
//const char code senseTemplate[]= {0x70,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};   
//                                                SK    ASC   ASQ

const char code senseArray[senseWriteProtected+1][3] = 
{{0x0b, 0x08, 0x03},   // senseCRCError               0 // Set on CRC error.  Causes host to retry  
{0x05, 0x24, 0x00},    // senseInvalidFieldInCDB      1 
{0x00, 0x00, 0x00},    // senseOk                     2
{0x02, 0x3a, 0x00},    // senseNoMedia                3
{0x03, 0x03, 0x00},    // senseWriteFault             4
{0x03, 0x11, 0x00},    // senseReadError              5
{0x03, 0x12, 0x00},    // senseAddrNotFound           6
{0x05, 0x20, 0x00},    // senseInvalidOpcode          7
{0x05, 0x21, 0x00},    // senseInvalidLBA             8
{0x05, 0x26, 0x00},    // senseInvalidParameter       9
{0x05, 0x53, 0x02},    // senseCantEject              0xa
{0x06, 0x28, 0x00},    // senseMediaChanged           0xb
{0x06, 0x29, 0x00},    // senseDeviceReset            0xc 
{0x07, 0x27, 0x00}};   // senseWriteProtected         0xd

char sensePtr;

/////////////////////////////////////////////////////////////////////////////////
//
// Called when we have received a valid CBW from the host
//
// Input:
//    EP2FIFOBUF -- Contains CBW
//   
// Output:
//    EP4FIFOBUF -- Used to send the data phase of the command back to the host.
//    Returns a byte to be sent as the CSW status byte.
//////////////////////////////////////////////////////////////////////////////////
bit IDECommand()
{
   BYTE cmd;

   bShortPacketSent = !directionIn;
   
   cmd = EP2FIFOBUF[0xf]; 

#ifndef NO_WP
   ////////////////////////////////////////////////////////////////////////////////////////////////////
   // Check for WP status changed.  If the WP status has changed, fail ANY command that comes in and
   // set the status to "media changed"
   if (bWriteProtectEnable)
   {
      if ((bWPSwitchState != NAND_WP_SWITCH) )
         {
         // Free the OUT buffer containing the command, load the error pointer, store new switch value and fail the command   
         OUTPKTEND = 0x82;
         bWPSwitchState = NAND_WP_SWITCH;
         sensePtr = senseMediaChanged; 
         if (directionIn) failedIn();
         else stallEP2OUT();
         //bEject = 0;  // toggle the switch will enable device again
         return(USBS_FAILED);
         }
   }
#endif

   switch (cmd)
   {
/// IN Command
      //////////////////////////////////////////////////////////////////////////////////
      // Good example of the processing required for a case in this switch statement.
      // -- Gives OUT bulk buffer back to the host
      // -- Loads IN buffer with a response
      // -- Adjusts dataTransferLen (inside loadEP4BC)
      // -- Returns PASSED or FAILED
      case INQUIRY:
      {
         OUTPKTEND = 0x82;
         // Send out our stored inquiry data
         waitForInBuffer();
         AUTOPTRL2 = LSB(EP4FIFOBUF);
#ifdef USE_2LUN
         if (!bLUN1) P_XAUTODAT2 = 0; else P_XAUTODAT2 = cDriveType;
#else
         P_XAUTODAT2 = 0;
#endif

#ifndef NO_WP
         if (bWriteProtectEnable) P_XAUTODAT2 = SCSI_INQUIRY_REMOVABLE_BIT;
         else P_XAUTODAT2 = 0;
#else
         P_XAUTODAT2 = SCSI_INQUIRY_REMOVABLE_BIT;
#endif
         P_XAUTODAT2 = 0;
         P_XAUTODAT2 = 0;
         P_XAUTODAT2 = 0x1F;
         P_XAUTODAT2 = 0;
         P_XAUTODAT2 = 0;
         P_XAUTODAT2 = 0;
         // copy SCSI Inquiry string
         mymemmovexx(EP4FIFOBUF+8, halfKBuffer+cHALF_BUFF_OFFSET, cINQUIRY_LENGTH); 
         loadEP4BC(0x1F+5);
         sensePtr = senseOk;
         return(USBS_PASSED);
      }

      case NAND_SCSI_CMD_VENDOR_CBW: return handleVendorCBW();

      // This command is "spoofed".  We tell the host that the command has succeeded but
      // don't actually do anything.
      case SYNCHRONIZE_CACHE:
      case SEEK_10:
      {
            // relinquish control of the bulk buffer occupied by the CBW
            OUTPKTEND = 0x82;
            return(USBS_PASSED);
      }                

      case READ_FORMAT_CAPACITIES:
      case READ_CAPACITY:
      {
         BYTE num_bytes = 8;

         // Nand Mfg support
         GetNandCfg();
         // add this for 30ns cycle support
         if (b30nsCycle && EZUSB_HIGHSPEED())
         {
              GPIF_40();
              b30nsCycle = 0;  // load only one time
         }
         // relinquish control of the bulk buffer occupied by the CBW
         OUTPKTEND = 0x82;       
         checkForMedia(1);
                 
         waitForInBuffer();
         if (sensePtr == senseOk)
         {
            AUTOPTRL2 = LSB(EP4FIFOBUF);
            if(cmd == READ_FORMAT_CAPACITIES) // add 4 byte capacity list header
            {
               P_XAUTODAT2 = 0x0;
               P_XAUTODAT2 = 0x0;
               P_XAUTODAT2 = 0x0;
               P_XAUTODAT2 = 0x08;
               num_bytes = 12;
            }
#ifdef USE_2LUN
            if (bLUN1)
            {
               P_XAUTODAT2 = (cLUN1_Capacity >> 24);
               P_XAUTODAT2 = (cLUN1_Capacity >> 16);
               P_XAUTODAT2 = (cLUN1_Capacity >>  8);
               P_XAUTODAT2 = (cLUN1_Capacity >>  0);
            }
            else
            {
               P_XAUTODAT2 = ((BYTE *) &gDriveCapacity)[0];
               P_XAUTODAT2 = ((BYTE *) &gDriveCapacity)[1];
               P_XAUTODAT2 = ((BYTE *) &gDriveCapacity)[2];
               P_XAUTODAT2 = ((BYTE *) &gDriveCapacity)[3];
            }
#else
            P_XAUTODAT2 = ((BYTE *) &gDriveCapacity)[0];
            P_XAUTODAT2 = ((BYTE *) &gDriveCapacity)[1];
            P_XAUTODAT2 = ((BYTE *) &gDriveCapacity)[2];
            P_XAUTODAT2 = ((BYTE *) &gDriveCapacity)[3];
#endif
            if(cmd == READ_FORMAT_CAPACITIES) 
               P_XAUTODAT2 = ((NAND_ATA_SECTOR_SIZE >> 24) & 0xff) | 2;     // Report media type -- Formatted
            else
               P_XAUTODAT2 = (NAND_ATA_SECTOR_SIZE >> 24) & 0xff;
            P_XAUTODAT2 = (NAND_ATA_SECTOR_SIZE >> 16) & 0xff;
            P_XAUTODAT2 = (NAND_ATA_SECTOR_SIZE >>  8) & 0xff;
            P_XAUTODAT2 = (NAND_ATA_SECTOR_SIZE >>  0) & 0xff;

            loadEP4BC(num_bytes);
            return(USBS_PASSED);
         }
         else
         {
            failedIn();
            return(USBS_FAILED);

⌨️ 快捷键说明

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