upsd3400_usb_app.c

来自「upsd34xx系列单片机keil环境中的开发例程」· C语言 代码 · 共 1,128 行 · 第 1/3 页

C
1,128
字号



void OnRequestSense(void)
/*--------------------------------------------------------------------
 Function   : void OnRequestSense()
 Parameters : none
 Description: Handler for RequestSense command
        The REQUEST SENSE command requests that the device server
              transfer sense data to the application client.
---------------------------------------------------------------------*/
 {
  USEL  = INDIR | UMSC_IN_ENDPOINT;          //Select EP1 IN

  USB_FIFO[ 0] = 0x70;
  USB_FIFO[ 1] = 0x00;
  USB_FIFO[ 2] = 0x03;                       //sense key
  USB_FIFO[ 3] = 0x00;
  USB_FIFO[ 4] = 0x00;
  USB_FIFO[ 5] = 0x00;
  USB_FIFO[ 6] = 0x00;
  USB_FIFO[ 7] = 0x0A;                       //additional Length
  USB_FIFO[ 8] = 0x00;
  USB_FIFO[ 9] = 0x00;
  USB_FIFO[10] = 0x00;
  USB_FIFO[11] = 0x00;
  USB_FIFO[12] = 0x30;                       //ASC
  USB_FIFO[13] = 0x01;                       //ASCQ
  USB_FIFO[14] = 0x00;
  USB_FIFO[15] = 0x00;
  USB_FIFO[16] = 0x00;
  USB_FIFO[17] = 0x00;

  USIZE = 18;

  gbBulkPipeStage = BS_CSW_PHASE;
 }





void OnInquiry(void)
/*--------------------------------------------------------------------
 Function   : void OnInquiry()
 Parameters : none
 Description: Handler for Inquiry command
---------------------------------------------------------------------*/
 {

  USEL  = INDIR | UMSC_IN_ENDPOINT;          //Select EP1 IN

  USB_FIFO[ 0] = 0x00;
  USB_FIFO[ 1] = 0x80;                   //b7 = RMB = 1 if the medium is removable
  USB_FIFO[ 2] = 0x00;
  USB_FIFO[ 3] = 0x01;

  USB_FIFO[ 4] = 36-4;                       //additional Length (n-4)
  USB_FIFO[ 5] = 0x80;
  USB_FIFO[ 6] = 0x00;
  USB_FIFO[ 7] = 0x01;

  USB_FIFO[ 8] = 'S';                        //Vendor Information
  USB_FIFO[ 9] = 'T';
  USB_FIFO[10] = 'M';
  USB_FIFO[11] = 'i';
  USB_FIFO[12] = 'c';
  USB_FIFO[13] = 'r';
  USB_FIFO[14] = 'o';
  USB_FIFO[15] = ' ';

  USB_FIFO[16] = 'D';                        //Product Identification
  USB_FIFO[17] = 'K';
  USB_FIFO[18] = '3';
  USB_FIFO[19] = '4';
  USB_FIFO[20] = '0';
  USB_FIFO[21] = '0';
  USB_FIFO[22] = '-';
  USB_FIFO[23] = 'U';
  USB_FIFO[24] = 'S';
  USB_FIFO[25] = 'B';
  USB_FIFO[26] = ' ';
  USB_FIFO[27] = 'R';
  USB_FIFO[28] = 'E';
  USB_FIFO[29] = 'F';
  USB_FIFO[30] = '.';
  USB_FIFO[31] = ' ';

  USB_FIFO[32] = '1';                        //Product Revision Level
  USB_FIFO[33] = '0';
  USB_FIFO[34] = ' ';
  USB_FIFO[35] = ' ';

  USIZE = 36;

  gbBulkPipeStage = BS_CSW_PHASE;
 }







void OnModeSelect6(void)
/******************************************************************************
 Function   : void OnModeSelect6()
 Parameters : (void)
 Description: The MODE SELECT(6) command provides a means for
              an initiator to specify device parameters to a RBC device.
              RBC devices shall also implement the MODE SENSE(6) command.
 ******************************************************************************/
 {
  gbCSWStatus = FAIL;
  ReturnCSW();                               // return status
 }



void OnModeSense6(void)
/******************************************************************************
 Function   : void OnModeSense6()
 Parameters : none
 Description: Handler for ModeSense6 command
              The MODE SENSE(6) command provides a means for a device server
              to report parameters to an application client.
              It is a complementary command to the MODE SELECT(6) command.
              Device servers that implement the MODE SENSE(6) command
              shall also implement the MODE SELECT(6) command.
 ******************************************************************************/
 {
  USEL  = INDIR | UMSC_IN_ENDPOINT;          //Select EP1 IN

  USB_FIFO[ 0] = 0x03;
  USB_FIFO[ 1] = 0x00;
  USB_FIFO[ 2] = 0x00;
  USB_FIFO[ 3] = 0x00;

  USIZE = 4;

  gbBulkPipeStage = BS_CSW_PHASE;
 }







void OnModeSelect10(void)
/******************************************************************************
 Function   : void OnModeSelect10()
 Parameters : (void)
 Description: The MODE SELECT(10) command provides a means for
              the application client to specify medium,
              logical unit, or peripheral device parameters to the device server.
              Application clients should issue MODE SENSE(10) prior
              to each MODE SELECT(10) to determine supported mode pages,
              mode page lengths, and other parameters.
              Device servers that implement the MODE SELECT(10) command shall
              also implement the MODE SENSE(10) command.
 ******************************************************************************/
 {
  gbCSWStatus = FAIL;
  ReturnCSW();                               // return status
 }



void OnModeSense10(void)
/******************************************************************************
 Function   : void OnModeSense10()
 Parameters : none
 Description: Handler for ModeSense10 command
 ******************************************************************************/
 {
  USEL  = INDIR | UMSC_IN_ENDPOINT;          //Select EP1 IN

  USB_FIFO[ 0] = 0x00;
  USB_FIFO[ 1] = 0x06;
  USB_FIFO[ 2] = 0x00;
  USB_FIFO[ 3] = 0x00;
  USB_FIFO[ 4] = 0x00;
  USB_FIFO[ 5] = 0x00;
  USB_FIFO[ 6] = 0x00;
  USB_FIFO[ 7] = 0x00;

  USIZE = 8;

  gbBulkPipeStage = BS_CSW_PHASE;
 }














void DoReadCapacity(void)
/*--------------------------------------------------------------------
 Function   : void DoReadCapacity()
 Parameters : none
 Description: Handler for ReadCapacity command
              This routine returns real capacity of the device-1
---------------------------------------------------------------------*/
 {
  USEL  = INDIR | UMSC_IN_ENDPOINT;          //Select EP1 IN

  USB_FIFO[ 0] = 0x00;                       //Last logical block
  USB_FIFO[ 1] = 0x00;
  USB_FIFO[ 2] = 0x02;
  USB_FIFO[ 3] = 0x3F;

  USB_FIFO[ 4] = 0x00;                       //block length
  USB_FIFO[ 5] = 0x00;
  USB_FIFO[ 6] = 0x02;                       //512B
  USB_FIFO[ 7] = 0x00;

  USIZE = 8;

  gbBulkPipeStage = BS_CSW_PHASE;
 }











void DoReadFormatCapacity(void)
/*--------------------------------------------------------------------
 Function   : void DoReadFormatCapacity()
 Parameters : none
 Description: Handler for ReadFormatCapacity command
---------------------------------------------------------------------*/
 {
  USEL  = INDIR | UMSC_IN_ENDPOINT;          //Select EP1 IN

  USB_FIFO[ 0] = 0x00;
  USB_FIFO[ 1] = 0x00;
  USB_FIFO[ 2] = 0x00;
  USB_FIFO[ 3] = 0x08;                       //capacity list length

/* The following values are used during disk format */
  USB_FIFO[ 4] = 0x00;                       //block count
  USB_FIFO[ 5] = 0x00;
  USB_FIFO[ 6] = 0x02;
  USB_FIFO[ 7] = 0x10;
  USB_FIFO[ 8] = 0x00;                       //block length
  USB_FIFO[ 9] = 0x00;
  USB_FIFO[10] = 0x02;                       //512B
  USB_FIFO[11] = 0x00;

  USIZE = 12;

  gbBulkPipeStage = BS_CSW_PHASE;
 }


































void CheckReceiveCBW(void)
/******************************************************************************
  Function   : void CheckReceiveCBW()
  Parameters : none
  Description: Handle CBW package.
               Command Block Wrapper (CBW) shall start on a packet boundary
               and shall end as a short packet with exactly 31 (1Fh)
               bytes transferred. Fields appear aligned to byte offsets equal to
               a multiple of their byte size. All subsequent data and the CSW
               shall start at a new packet boundary.
               All CBW transfers shall be ordered with the LSB (byte 0) first
               (little endian). Refer to the USB Specification
               Terms and Abbreviations for clarification.
  ******************************************************************************/
 {
  data unsigned char i;                      // FOR variable

  USEL     = OUTDIR | UMSC_OUT_ENDPOINT;     // Select EP OUT

/* dCBWSignature:
  Signature that helps identify this data packet as a CBW. The signature field
  shall contain the value 43425355h (little endian), indicating a CBW.*/
  if((USB_FIFO[0]==0x55) && (USB_FIFO[1]==0x53) &&
  (USB_FIFO[2]==0x42) && (USB_FIFO[3]==0x43))
   {
/* dCBWTag:
    A Command Block Tag sent by the host. The device shall echo the contents of
    this field back to the host in the dCSWTag field of the associated CSW. The
    dCSWTag positively associates a CSW with the corresponding CBW.*/
    *((uchar *)&gdwCBWTag+3) = USB_FIFO[4];  // LSB first
    *((uchar *)&gdwCBWTag+2) = USB_FIFO[5];
    *((uchar *)&gdwCBWTag+1) = USB_FIFO[6];
    *((uchar *)&gdwCBWTag+0) = USB_FIFO[7];
/* dCBWDataTransferLength:
    The number of bytes of data that the host expects to transfer on the Bulk-In
    or Bulk-Out endpoint (as indicated by the Direction bit) during the execution
    of this command. If this field is zero, the device and the host shall transfer
    no data between the CBW and the associated CSW, and the device shall ignore
    the value of the Direction bit in bmCBWFlags.*/
    *((uchar *)&gdwCBWDataTransferLength+3) = USB_FIFO[ 8];           // LSB first
    *((uchar *)&gdwCBWDataTransferLength+2) = USB_FIFO[ 9];
    *((uchar *)&gdwCBWDataTransferLength+1) = USB_FIFO[10];
    *((uchar *)&gdwCBWDataTransferLength+0) = USB_FIFO[11];
/* bmCBWFlags:
    The bits of this field are defined as follows:
    Bit 7 Direction - the device shall ignore this bit if the dCBWDataTransferLength
    field is zero, otherwise:
    0 = Data-Out from host to the device, 1 = Data-In from the device to the host.
    Bit 6 Obsolete. The host shall set this bit to zero.
    Bits 5..0 Reserved - the host shall set these bits to zero.*/
    gbCBWFlags = USB_FIFO[12];
/* bCBWLUN:
    The device Logical Unit Number (LUN) to which the command block is being sent.
    For devices that support multiple LUNs, the host shall place into this field
    the LUN to which this command block is addressed. Otherwise, the host shall
    set this field to zero. */
    gbCBWLUN  = USB_FIFO[13];
/* bCBWCBLength:
    The valid length of the CBWCB in bytes. This defines the valid length of the
    command block. The only legal values are 1 through 16 (01h through 10h). All
    other values are reserved. */
    gbCBWCBLength = USB_FIFO[14];

    if (gbCBWCBLength>16)
     {
      gbCBWCBLength = 16;
     }
/* CBWCB:
    The command block to be executed by the device. The device shall interpret
    the first bCBWCBLength bytes in this field as a command block as defined by
    the command set identified by bInterfaceSubClass. If the command set supported
    by the device uses command blocks of fewer than 16 (10h) bytes in
    length, the significant bytes shall be transferred first, beginning with the
    byte at offset 15 (Fh). The device shall ignore the content of the CBWCB field

⌨️ 快捷键说明

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