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

📄 chap_9.lst

📁 D12的驱动程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
 301   2                      init_unconfig();
 302   2                      init_config();
 303   2                      
C51 COMPILER V7.08   CHAP_9                                                                03/11/2005 10:40:00 PAGE 6   

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

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

 428          void ep0_rxdone(void)
 429          {
 430   1              unsigned char ep_last, i;
 431   1      
 432   1      /*      int a;
 433   1        for(a=0;a<=20000;a++)  ;
 434   1              MCU_LED0 = 0;//灭LED显示(实际应用中可去掉)
 435   1              MCU_LED1 = 0;
 436   1               for(a=0;a<=20000;a++)  ;
 437   1      MCU_LED0 = 1;//灭LED显示(实际应用中可去掉)
 438   1              MCU_LED1 = 1;           //dtftrtsu857d88rrydgfgggggggggggggggggggggggg
 439   1      */
 440   1              ep_last = D12_ReadLastTransactionStatus(0); // Clear interrupt flag
 441   1      
 442   1              if (ep_last & D12_SETUPPACKET) 
 443   1              {
 444   2                      ControlData.wLength = 0;
 445   2                      ControlData.wCount = 0;
 446   2      
 447   2                      if( D12_ReadEndpoint(0, sizeof(ControlData.DeviceRequest),
 448   2                              (unsigned char *)(&(ControlData.DeviceRequest))) != sizeof(DEVICE_REQUEST) ) {
 449   3      
 450   3                              D12_SetEndpointStatus(0, 1);
 451   3                              D12_SetEndpointStatus(1, 1);
 452   3                              bEPPflags.bits.control_state = USB_IDLE;
 453   3                              
 454   3                              return;
 455   3                      }
 456   2         /*for(a=0;a<=20000;a++)  ;
 457   2              MCU_LED0 = 0;//灭LED显示(实际应用中可去掉)
 458   2              MCU_LED1 = 0;
 459   2               for(a=0;a<=20000;a++)  ;
 460   2      MCU_LED0 = 1;//灭LED显示(实际应用中可去掉)
 461   2              MCU_LED1 = 1;           //dtftrtsu857d88rrydgfgggggggggggggggggggggggg
 462   2      
 463   2      */
 464   2                      ControlData.DeviceRequest.wValue = SWAP(ControlData.DeviceRequest.wValue);
 465   2                      ControlData.DeviceRequest.wIndex = SWAP(ControlData.DeviceRequest.wIndex);
 466   2                      ControlData.DeviceRequest.wLength = SWAP(ControlData.DeviceRequest.wLength);
 467   2      
 468   2                      // Acknowledge setup here to unlock in/out endp
 469   2                      D12_AcknowledgeEndpoint(0);
 470   2                      D12_AcknowledgeEndpoint(1);
 471   2      
 472   2                      ControlData.wLength = ControlData.DeviceRequest.wLength;
 473   2                      ControlData.wCount = 0;
 474   2      
 475   2                      if (ControlData.DeviceRequest.bmRequestType & (unsigned char)USB_ENDPOINT_DIRECTION_MASK) {
 476   3                              bEPPflags.bits.setup_packet = 1;
 477   3              
 478   3        /* for(a=0;a<=20000;a++)  ;
 479   3              MCU_LED0 = 0;//灭LED显示(实际应用中可去掉)
 480   3              MCU_LED1 = 0;
 481   3               for(a=0;a<=20000;a++)  ;
 482   3      MCU_LED0 = 1;//灭LED显示(实际应用中可去掉)
 483   3              MCU_LED1 = 1;           //dtftrtsu857d88rrydgfgggggggggggggggggggggggg
 484   3                      */
 485   3                      bEPPflags.bits.control_state = USB_TRANSMIT;            /* get command */
 486   3                      }
 487   2                      else {
 488   3                              if (ControlData.DeviceRequest.wLength == 0) {
 489   4                                      bEPPflags.bits.setup_packet = 1;
C51 COMPILER V7.08   CHAP_9                                                                03/11/2005 10:40:00 PAGE 9   

 490   4                                      bEPPflags.bits.control_state = USB_IDLE;                /* set command */
 491   4                              }
 492   3                              else {
 493   4                                      if(ControlData.DeviceRequest.wLength > MAX_CONTROLDATA_SIZE) {
 494   5                                              bEPPflags.bits.control_state = USB_IDLE;
 495   5                                              D12_SetEndpointStatus(0, 1);
 496   5                                              D12_SetEndpointStatus(1, 1);
 497   5                                      }
 498   4                                      else {
 499   5                                              bEPPflags.bits.control_state = USB_RECEIVE;     /* set command with OUT token */
 500   5                                      }
 501   4                              } // set command with data
 502   3                      } // else set command
 503   2              } // if setup packet
 504   1      
 505   1              else if (bEPPflags.bits.control_state == USB_RECEIVE) {
 506   2                      i =     D12_ReadEndpoint(0, EP0_PACKET_SIZE,
 507   2                              ControlData.dataBuffer + ControlData.wCount);
 508   2      
 509   2                      ControlData.wCount += i;
 510   2                      if( i != EP0_PACKET_SIZE || ControlData.wCount >= ControlData.wLength) {
 511   3                              bEPPflags.bits.setup_packet = 1;
 512   3                              bEPPflags.bits.control_state = USB_IDLE;
 513   3                      }
 514   2              }
 515   1      
 516   1              else {
 517   2                      bEPPflags.bits.control_state = USB_IDLE;
 518   2              }
 519   1      }
 520          
 521          void ep0_txdone(void)
 522          {
 523   1              short i = ControlData.wLength - ControlData.wCount;
 524   1      
 525   1              D12_ReadLastTransactionStatus(1); // Clear interrupt flag
 526   1      
 527   1              if (bEPPflags.bits.control_state != USB_TRANSMIT) 
 528   1              {
 529   2                      single_transmit(0, 0);                  
 530   2                      return;
 531   2              }
 532   1              
 533   1              if( i >= EP0_PACKET_SIZE) {
 534   2                      D12_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData + ControlData.wCount);
 535   2                      ControlData.wCount += EP0_PACKET_SIZE;
 536   2      
 537   2                      bEPPflags.bits.control_state = USB_TRANSMIT;
 538   2              }
 539   1              else if( i != 0) {
 540   2                      D12_WriteEndpoint(1, i, ControlData.pData + ControlData.wCount);
 541   2                      ControlData.wCount += i;
 542   2      
 543   2                      bEPPflags.bits.control_state = USB_IDLE;
 544   2              }
 545   1              else if (i == 0){
 546   2                      D12_WriteEndpoint(1, 0, 0); // Send zero packet at the end ???
 547   2      
 548   2                      bEPPflags.bits.control_state = USB_IDLE;
 549   2              }
 550   1      }
 551          
C51 COMPILER V7.08   CHAP_9                                                                03/11/2005 10:40:00 PAGE 10  

 552          
 553          void usbserve(void)
 554          {
 555   1              if (bEPPflags.bits.bus_reset) {
 556   2                      DISABLE;
 557   2                      bEPPflags.bits.bus_reset = 0;
 558   2                      ENABLE;      
 559   2                      // Release D12's SUSPEND pin after bus reset
 560   2                      // Enable 74HCT123 pulse generation before disconnect
 561   2                      //D12SUSPD = 1;
 562   2              } // if bus reset
 563   1      
 564   1              if (bEPPflags.bits.suspend) {
 565   2                      DISABLE;
 566   2                      bEPPflags.bits.suspend= 0;
 567   2                      ENABLE;
 568   2                      /*
 569   2                      if(D12SUSPD == 1) {
 570   2                              D12SUSPD = 0;
 571   2                              P0 = 0xFF;
 572   2                              P1 = 0xFF;
 573   2                              P2 = 0xFF;
 574   2                              P3 = 0xFF;
 575   2                              D12_SetDMA(0x0);
 576   2                              D12SUSPD = 1;
 577   2                              PCON |= 0x02;
 578   2                              while (1);
 579   2                      }*/
 580   2                      
 581   2              } // if suspend change
 582   1      
 583   1              if (bEPPflags.bits.setup_packet){
 584   2                      DISABLE;
 585   2                      bEPPflags.bits.setup_packet = 0;
 586   2                      control_handler();
 587   2                      ENABLE;
 588   2                      //D12SUSPD = 1;
 589   2              } // if setup_packet
 590   1      }
 591          
 592          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   1463    ----
   CONSTANT SIZE    =    112    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----      26
   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 + -