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

📄 chap_9.lst

📁 KeilC51下的USB的slave设备源码,MCS51开发.
💻 LST
📖 第 1 页 / 共 2 页
字号:
 286          {
 287   1              if (ControlData.DeviceRequest.wValue == 0) {
 288   2                      /* put device in unconfigured state */
 289   2                      single_transmit(0, 0);
 290   2                      DISABLE;
 291   2                      bEPPflags.bits.configuration = 0;
 292   2                      ENABLE;
 293   2                      init_unconfig();
 294   2              } else if (ControlData.DeviceRequest.wValue == 1) {
 295   2                      /* Configure device */
 296   2                      single_transmit(0, 0);
 297   2      
 298   2                      init_unconfig();
 299   2                      init_config();
 300   2                      
 301   2                      DISABLE;
 302   2                      bEPPflags.bits.configuration = 1;
 303   2                      ENABLE;
C51 COMPILER V7.06   CHAP_9                                                                06/23/2003 11:17:01 PAGE 6   

 304   2              } else
 305   1                      stall_ep0();
 306   1      }
 307          
 308          void get_interface(void)
 309          {
 310   1              unsigned char txdat = 0;        /* Only/Current interface = 0 */
 311   1              single_transmit(&txdat, 1);
 312   1      }
 313          
 314          void set_interface(void)
 315          {
 316   1              if (ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0)
 317   1                      single_transmit(0, 0);
 318   1              else
 319   1                      stall_ep0();
 320   1      }
 321          
 322          
 323          
 324          
 325          
 326          
 327          //Move by Liu Ying Bin
 328          
 329          
 330          void init_unconfig(void)
 331          {
 332   1              //unsigned char i;
 333   1      
 334   1              D12_SetEndpointEnable(0);       /* Disable all endpoints but EPP0. */
 335   1      }
 336          
 337          void init_config(void)
 338          {
 339   1              D12_SetEndpointEnable(1);       /* Enable  generic/iso endpoints. */
 340   1      }
 341          
 342          void single_transmit(unsigned char * buf, unsigned char len)
 343          {
 344   1              if( len <= EP0_PACKET_SIZE) {
 345   2                      D12_WriteEndpoint(1, len, buf);
 346   2              }
 347   1      }
 348          
 349          void code_transmit(unsigned char code * pRomData, unsigned short len)
 350          {
 351   1              ControlData.wCount = 0;
 352   1              if(ControlData.wLength > len)
 353   1                      ControlData.wLength = len;
 354   1      
 355   1              ControlData.pData = pRomData;
 356   1              if( ControlData.wLength >= EP0_PACKET_SIZE) {
 357   2                      D12_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData);
 358   2                      ControlData.wCount += EP0_PACKET_SIZE;
 359   2      
 360   2                      DISABLE;
 361   2                      bEPPflags.bits.control_state = USB_TRANSMIT;
 362   2                      ENABLE;
 363   2              }
 364   1              else {
 365   2                      D12_WriteEndpoint(1, ControlData.wLength, pRomData);
C51 COMPILER V7.06   CHAP_9                                                                06/23/2003 11:17:01 PAGE 7   

 366   2                      ControlData.wCount += ControlData.wLength;
 367   2                      DISABLE;
 368   2                      bEPPflags.bits.control_state = USB_IDLE;
 369   2                      ENABLE;
 370   2              }
 371   1      }
 372          
 373          void control_handler()
 374          {
 375   1              unsigned char type, req;
 376   1      
 377   1              type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK;
 378   1              req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK;
 379   1      
 380   1              if (type == USB_STANDARD_REQUEST)
 381   1                      (*StandardDeviceRequest[req])();
 382   1              else if (type == USB_VENDOR_REQUEST)
 383   1                      (*VendorDeviceRequest[req])();
 384   1              else
 385   1                      stall_ep0();
 386   1      }
 387          
 388          /*
 389          void fn_usb_isr()
 390          {
 391                  unsigned int i_st;
 392          
 393                  bEPPflags.bits.in_isr = 1;
 394                  i_st = D12_ReadInterruptRegister();
 395          
 396                  if(i_st != 0) {
 397                          if(i_st & D12_INT_BUSRESET) {
 398                          //      bus_reset();
 399                                  bEPPflags.bits.bus_reset = 1;
 400                          }
 401          
 402                          if(i_st & D12_INT_EOT)
 403                                  dma_eot();
 404          
 405                          if(i_st & D12_INT_SUSPENDCHANGE)
 406                                  bEPPflags.bits.suspend = 1;
 407          
 408                          if(i_st & D12_INT_ENDP0IN)
 409                                  ep0_txdone();
 410                          if(i_st & D12_INT_ENDP0OUT)
 411                                  ep0_rxdone();
 412                          if(i_st & D12_INT_ENDP1IN)
 413                                  ep1_txdone();
 414                          if(i_st & D12_INT_ENDP1OUT)
 415                                  ep1_rxdone();
 416                          if(i_st & D12_INT_ENDP2IN)
 417                                  ep2_txdone();
 418                          if(i_st & D12_INT_ENDP2OUT)
 419                                  ep2_rxdone();
 420                  }
 421                          
 422                  bEPPflags.bits.in_isr = 0;
 423          }
 424          */
 425          
 426          void stall_ep0(void)
 427          {
C51 COMPILER V7.06   CHAP_9                                                                06/23/2003 11:17:01 PAGE 8   

 428   1              D12_SetEndpointStatus(0, 1);
 429   1              D12_SetEndpointStatus(1, 1);
 430   1      }
 431          
 432          void ep0_rxdone(void)
 433          {
 434   1              unsigned char ep_last, i;
 435   1      
 436   1              ep_last = D12_ReadLastTransactionStatus(0); // Clear interrupt flag
 437   1      
 438   1              if (ep_last & D12_SETUPPACKET) {
 439   2      
 440   2                      ControlData.wLength = 0;
 441   2                      ControlData.wCount = 0;
 442   2      
 443   2                      if( D12_ReadEndpoint(0, sizeof(ControlData.DeviceRequest),
 444   2                              (unsigned char *)(&(ControlData.DeviceRequest))) != sizeof(DEVICE_REQUEST) ) {
 445   3      
 446   3                              D12_SetEndpointStatus(0, 1);
 447   3                              D12_SetEndpointStatus(1, 1);
 448   3                              bEPPflags.bits.control_state = USB_IDLE;
 449   3                              
 450   3                              return;
 451   3                      }
 452   2      
 453   2                      ControlData.DeviceRequest.wValue = SWAP(ControlData.DeviceRequest.wValue);
 454   2                      ControlData.DeviceRequest.wIndex = SWAP(ControlData.DeviceRequest.wIndex);
 455   2                      ControlData.DeviceRequest.wLength = SWAP(ControlData.DeviceRequest.wLength);
 456   2      
 457   2                      // Acknowledge setup here to unlock in/out endp
 458   2                      D12_AcknowledgeEndpoint(0);
 459   2                      D12_AcknowledgeEndpoint(1);
 460   2      
 461   2                      ControlData.wLength = ControlData.DeviceRequest.wLength;
 462   2                      ControlData.wCount = 0;
 463   2      
 464   2                      if (ControlData.DeviceRequest.bmRequestType & (unsigned char)USB_ENDPOINT_DIRECTION_MASK) {
 465   3                              bEPPflags.bits.setup_packet = 1;
 466   3                              bEPPflags.bits.control_state = USB_TRANSMIT;            /* get command */
 467   3                      }
 468   2                      else {
 469   3                              if (ControlData.DeviceRequest.wLength == 0) {
 470   4                                      bEPPflags.bits.setup_packet = 1;
 471   4                                      bEPPflags.bits.control_state = USB_IDLE;                /* set command */
 472   4                              }
 473   3                              else {
 474   4                                      if(ControlData.DeviceRequest.wLength > MAX_CONTROLDATA_SIZE) {
 475   5                                              bEPPflags.bits.control_state = USB_IDLE;
 476   5                                              D12_SetEndpointStatus(0, 1);
 477   5                                              D12_SetEndpointStatus(1, 1);
 478   5                                      }
 479   4                                      else {
 480   5                                              bEPPflags.bits.control_state = USB_RECEIVE;     /* set command with OUT token */
 481   5                                      }
 482   4                              } // set command with data
 483   3                      } // else set command
 484   2              } // if setup packet
 485   1      
 486   1              else if (bEPPflags.bits.control_state == USB_RECEIVE) {
 487   2                      i =     D12_ReadEndpoint(0, EP0_PACKET_SIZE,
 488   2                              ControlData.dataBuffer + ControlData.wCount);
 489   2      
C51 COMPILER V7.06   CHAP_9                                                                06/23/2003 11:17:01 PAGE 9   

 490   2                      ControlData.wCount += i;
 491   2                      if( i != EP0_PACKET_SIZE || ControlData.wCount >= ControlData.wLength) {
 492   3                              bEPPflags.bits.setup_packet = 1;
 493   3                              bEPPflags.bits.control_state = USB_IDLE;
 494   3                      }
 495   2              }
 496   1      
 497   1              else {
 498   2                      bEPPflags.bits.control_state = USB_IDLE;
 499   2              }
 500   1      }
 501          
 502          void ep0_txdone(void)
 503          {
 504   1              short i = ControlData.wLength - ControlData.wCount;
 505   1      
 506   1              D12_ReadLastTransactionStatus(1); // Clear interrupt flag
 507   1      
 508   1              if (bEPPflags.bits.control_state != USB_TRANSMIT) 
 509   1              {
 510   2                      single_transmit(0, 0);                  
 511   2                      return;
 512   2              }
 513   1              
 514   1              if( i >= EP0_PACKET_SIZE) {
 515   2                      D12_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData + ControlData.wCount);
 516   2                      ControlData.wCount += EP0_PACKET_SIZE;
 517   2      
 518   2                      bEPPflags.bits.control_state = USB_TRANSMIT;
 519   2              }
 520   1              else if( i != 0) {
 521   2                      D12_WriteEndpoint(1, i, ControlData.pData + ControlData.wCount);
 522   2                      ControlData.wCount += i;
 523   2      
 524   2                      bEPPflags.bits.control_state = USB_IDLE;
 525   2              }
 526   1              else if (i == 0){
 527   2                      D12_WriteEndpoint(1, 0, 0); // Send zero packet at the end ???
 528   2      
 529   2                      bEPPflags.bits.control_state = USB_IDLE;
 530   2              }
 531   1      }
 532          
 533          
 534          void usbserve(void)
 535          {
 536   1              if (bEPPflags.bits.bus_reset) {
 537   2                      DISABLE;
 538   2                      bEPPflags.bits.bus_reset = 0;
 539   2                      ENABLE;
 540   2                      d12_busreset();
 541   2              } // if bus reset
 542   1      
 543   1              if (bEPPflags.bits.suspend) {
 544   2                      DISABLE;
 545   2                      bEPPflags.bits.suspend= 0;
 546   2                      ENABLE;
 547   2                      d12_suspend();
 548   2              } // if suspend change
 549   1      
 550   1              if (bEPPflags.bits.setup_packet){
 551   2                      DISABLE;
C51 COMPILER V7.06   CHAP_9                                                                06/23/2003 11:17:01 PAGE 10  

 552   2                      bEPPflags.bits.setup_packet = 0;
 553   2                      control_handler();
 554   2                      ENABLE;
 555   2              } // if setup_packet
 556   1      }
 557          
 558          void GetVer(unsigned char * VerPoint)
 559          {
 560   1              unsigned char code Ver[]={"ZLGMCU USB51S V0.4"};
 561   1              VerPoint = Ver;
 562   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   1488    ----
   CONSTANT SIZE    =    131    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----      29
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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