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

📄 usbisr.lst

📁 单片机和D12通讯的C程序.实现了单片机通过USB口和电脑通讯.
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V7.01  USBISR                                                                 02/17/2006 15:09:24 PAGE 1   


C51 COMPILER V7.01, COMPILATION OF MODULE USBISR
OBJECT MODULE PLACED IN USBISR.OBJ
COMPILER INVOKED BY: d:\Keil\C51\BIN\C51.EXE USBISR.C BROWSE MODP2 DEBUG OBJECTEXTEND

stmt level    source

   1          #include "InsUsb.h"
   2          
   3          extern SYS_INFORMATION data sSysInformation;
   4                  
   5          extern unsigned char xdata mEp0OutBuffer[EP0_OUT_BUFFER_SIZE];
   6          extern unsigned char data  mEp1OutBuffer[EP1_OUT_BUFFER_SIZE];  //store led status
   7          extern unsigned char xdata mRamBuffer[RAM_BUFFER_SIZE];
   8          
   9          
  10          extern void D12AcknowledgeSetup(void);
  11          extern unsigned char D12ReadInterruptRegister(void);
  12          extern unsigned char D12ReadLastTransactionStatus(unsigned char bEndpointIndex);
  13          extern unsigned char D12ReadBuffer(unsigned char bEndpointIndex, unsigned int bExpectedLength,unsigned cha
             -r * pBuffer);
  14          extern void D12WriteBuffer(unsigned char bEndpointIndex,unsigned char bLength, unsigned char * pBuffer);
  15          extern void D12SetEndpointStatus(unsigned char bEndpointIndex, unsigned char bStalled);
  16          
  17          
  18          //
  19          //*************************************************************************//
  20          //      Paremeter:
  21          //              In : None
  22          //              Out: None
  23          //      Function:
  24          //              USB standard request and vendor/class request is impleemnted by control
  25          //              transfer.Every control transfer contains three stage: setup stage,data
  26          //              stage(optional) and status stage.At setup stage,we will receive a setup packet,
  27          //              it must be 8 byte and the meaning of setup packet please reference USB 
  28          //              specification.
  29          //              For control-in transfer,there must has a data stage.At this stage,we must 
  30          //              write data to PDIUSBD12 endpoint0-in buffer,and when received IN token,
  31          //              those data will be transmit to host by PDIUSBD12 automatically.At last when
  32          //              we received an OUT token from host,control-in transfer complete.
  33          //              For control-out transfer,there may be has a data stage or no.If it has no data
  34          //              stage,we can simply set device or interface or endpoint status according request,
  35          //              but if it has a data stage,we must receive those data,and deal with them.
  36          //              At last,if we receive a IN token,return zero length to host and control-out 
  37          //              transfer complete.
  38          void Ep0Out(void)               //Control OUT transaction process
  39          {
  40   1              unsigned char data bLastTransactionStatus;
  41   1              unsigned char data bDataLength;
  42   1              unsigned char xdata * data pBuffer ;
  43   1      
  44   1      
  45   1              //after ReadLastTransactionStatus,
  46   1              //D12 will clear corresponding interrupt register flag bit automatically
  47   1              bLastTransactionStatus = D12ReadLastTransactionStatus(D12_READ_LAST_TRANSACTION_STATUS_CONTROL_OUT);
  48   1      
  49   1              #ifndef _INT_MODE_      
                              #ifdef _Debug_  
                                      printf("Ep0Out last status is 0x%x\n",(unsigned int)(bLastTransactionStatus));
                              #endif
                      #endif
  54   1              if (bLastTransactionStatus & D12_SETUP_PACKET_MASK)     //if received a setup packet
C51 COMPILER V7.01  USBISR                                                                 02/17/2006 15:09:24 PAGE 2   

  55   1              {
  56   2                      //read buffer,save SETUP data to sSysInformation.sUsbDeviceRequest
  57   2                      bDataLength = D12ReadBuffer(D12_SELECT_ENDPOINT_CONTROL_OUT, USB_SETUP_PACKET_LENGTH,&sSysInformation.sU
             -sbDeviceRequest.bmRequestType);
  58   2                      
  59   2                      sSysInformation.sUsbDeviceRequest.wValue = WORD_SWAP(sSysInformation.sUsbDeviceRequest.wValue);
  60   2                      sSysInformation.sUsbDeviceRequest.wIndex = WORD_SWAP(sSysInformation.sUsbDeviceRequest.wIndex);
  61   2                      sSysInformation.sUsbDeviceRequest.wLength = WORD_SWAP(sSysInformation.sUsbDeviceRequest.wLength);
  62   2              
  63   2                      sSysInformation.sUsbSetUpDealwith.wRemaindLength = sSysInformation.sUsbDeviceRequest.wLength;
  64   2      
  65   2                      D12AcknowledgeSetup();                          // Acknowledge setup 
  66   2      
  67   2                      sSysInformation.bSetup = TRUE;          //after received a setup packet,set flag
  68   2              
  69   2                      //if it is a control-in transfer
  70   2                      if (sSysInformation.sUsbDeviceRequest.bmRequestType & USB_ENDPOINT_DIRECTION_MASK)
  71   2                              sSysInformation.bUsbStatus = TRANSMIT;
  72   2                      //else it is control-out
  73   2                      else
  74   2                      {
  75   3                              //Interface Studio USB1.1 develop board support control-out transfer with data 
  76   3                              //stage.This firmware will save data to mEp0OutBuffer,which size is 128 byte,
  77   3                              //and user should assure data length of control-out transfer no more than it,else
  78   3                              //firmware will stall endpoint0.The meaning of received data should be interpreted by
  79   3                              //user,as a demo,firmware demenstrate how to deal with SetDeviceSerialNumber request,
  80   3                              //which is a vendor defined request;other control-out transfer will be resvered for
  81   3                              //user to determine how to deal with them.
  82   3                              if (sSysInformation.sUsbDeviceRequest.wLength > EP0_OUT_BUFFER_SIZE)
  83   3                              {
  84   4                                      CONTROL_ENDPOINT_STALL  //set control endpoint stall
  85   4                              }
  86   3                              //For contro-out transfer,it may be has data stage or no data stage.
  87   3                              //Whichever we should return status stage only after we had completed the required
  88   3                              //operation.So we set  bControOutCommandIsPending and bControlOutDataComplete flags
  89   3                              // to help we judge whether the data is received.
  90   3                              else
  91   3                              {
  92   4                                      sSysInformation.bUsbStatus = RECEIVE;
  93   4                                      sSysInformation.sUsbSetUpDealwith.bControlOutDataComplete = FALSE;
  94   4                                      sSysInformation.sUsbSetUpDealwith.bControOutCommandIsPending = TRUE;
  95   4                              }
  96   3                      }
  97   2              }
  98   1              //if endpoint0-out received a packet which is not a setup packet
  99   1              else
 100   1              {
 101   2                      if (sSysInformation.bUsbStatus == RECEIVE)
 102   2                      {
 103   3                              if (sSysInformation.sUsbSetUpDealwith.wRemaindLength>0)
 104   3                              {
 105   4                                      pBuffer = &mEp0OutBuffer + \
 106   4                                                      (sSysInformation.sUsbDeviceRequest.wLength-sSysInformation.sUsbSetUpDealwith.wRemaindLength);
 107   4                                      bDataLength = D12ReadBuffer(D12_SELECT_ENDPOINT_CONTROL_OUT,sSysInformation.sUsbSetUpDealwith.wRemaind
             -Length, pBuffer);
 108   4                                      if (bDataLength == D12_BUFFER_LENGTH_ERROR)
 109   4                                      {
 110   5                                              //D12_BUFFER_LENGTH_ERROR shows received data length is longer than expected length,
 111   5                                              //this is a serious error,so we stall control endpoint
 112   5                                              CONTROL_ENDPOINT_STALL  //set control endpoint stall
 113   5                                      }
 114   4                                      else
C51 COMPILER V7.01  USBISR                                                                 02/17/2006 15:09:24 PAGE 3   

 115   4                                      {
 116   5                                              //get remaind data length
 117   5                                              sSysInformation.sUsbSetUpDealwith.wRemaindLength -= bDataLength;
 118   5                                              //if remaind data length is 0, current data stage of control-out transfer 
 119   5                                              //will be finished.
 120   5                                              if (sSysInformation.sUsbSetUpDealwith.wRemaindLength<=0)
 121   5                                              {
 122   6                                                      sSysInformation.sUsbSetUpDealwith.bControlOutDataComplete = TRUE;
 123   6                                              }
 124   5                                      }
 125   4                              }
 126   3                              else    //unexpected out packet,we should stall control endpoint
 127   3                              {
 128   4                                      CONTROL_ENDPOINT_STALL  //set control endpoint stall
 129   4                              }
 130   3      
 131   3                      }
 132   2      
 133   2                      else if (sSysInformation.bUsbStatus == TRANSMIT)
 134   2                      {
 135   3                              //If remaind data length is zero and current status is TRANSMIT,
 136   3                              //it shows data stage of control-in transfer will be finished
 137   3                              //For control-in transfer,Host will send a zero length out packet to device
 138   3                              //as status stage which flags control-in transfer is finished.see 8.5.2.1
 139   3                              if (sSysInformation.sUsbSetUpDealwith.wRemaindLength<=0)        
 140   3                              {
 141   4                                      sSysInformation.bUsbStatus = IDLE;
 142   4                              }
 143   3                              else
 144   3                              {
 145   4                                      //this is an unexpected OUT packet,so we should stall endpoint0                         
 146   4                                      CONTROL_ENDPOINT_STALL  //set control endpoint stall
 147   4                              }
 148   3                      }
 149   2                      else    //if IDLE state
 150   2                      {
 151   3                              //this is an unexpected OUT packet,so we should stall endpoint0
 152   3                              CONTROL_ENDPOINT_STALL  //set control endpoint stall
 153   3                      }
 154   2              }
 155   1      }
 156          
 157          
 158          
 159          //
 160          //*************************************************************************
 161          //      Paremeter:
 162          //              In : None
 163          //              Out: None
 164          //      Function:
 165          //              All control-in transfer has a data stage.This function will check whether
 166          //              there are remaind data should transfer to host.
 167          //              Anaother case is,a control-out transfer expect a IN token as status stage
 168          //              to complete this transfer.
 169          void Ep0In(void)                
 170          {
 171   1              unsigned char data bLastTransactionStatus;
 172   1              unsigned char data bDataLength;
 173   1              unsigned char data i;
 174   1      
 175   1              bLastTransactionStatus = D12ReadLastTransactionStatus(D12_READ_LAST_TRANSACTION_STATUS_CONTROL_IN); // Cl
             -ear interrupt flag
C51 COMPILER V7.01  USBISR                                                                 02/17/2006 15:09:24 PAGE 4   

⌨️ 快捷键说明

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