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

📄 chap_9.lst

📁 利用中断实现的单片机程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
 291   2                      init_unconfig();
 292   2              } else if (ControlData.DeviceRequest.wValue == 1) {
 293   2                      /* Configure device */
 294   2                      single_transmit(0, 0);
 295   2      
 296   2                      init_unconfig();
 297   2                      init_config();
 298   2                      
 299   2                      DISABLE;
 300   2                      bEPPflags.bits.configuration = 1;
 301   2                      ENABLE;
 302   2              } else
 303   1                      stall_ep0();
C51 COMPILER V7.06   CHAP_9                                                                06/05/2005 13:58:57 PAGE 6   

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

 366   2                      bEPPflags.bits.control_state = USB_IDLE;
 367   2                      ENABLE;
 368   2              }
 369   1      }
 370          
 371          void control_handler()
 372          {
 373   1              unsigned char type, req;
 374   1      
 375   1              type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK;
 376   1              req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK;
 377   1      
 378   1              if (type == USB_STANDARD_REQUEST)
 379   1                      (*StandardDeviceRequest[req])();
 380   1              else if (type == USB_VENDOR_REQUEST)
 381   1                      (*VendorDeviceRequest[req])();
 382   1              else
 383   1                      stall_ep0();
 384   1      }
 385          
 386          
 387          void fn_usb_isr()
 388          {
 389   1              unsigned int i_st;
 390   1              bEPPflags.bits.in_isr = 1;
 391   1              i_st = D12_ReadInterruptRegister();
 392   1      
 393   1              if(i_st != 0) {
 394   2                      if(i_st & D12_INT_BUSRESET) {
 395   3                              bus_reset();
 396   3                              bEPPflags.bits.bus_reset = 1;
 397   3                      }
 398   2      
 399   2                      if(i_st & D12_INT_EOT)
 400   2                              dma_eot();
 401   2      
 402   2                      if(i_st & D12_INT_SUSPENDCHANGE)
 403   2                              bEPPflags.bits.suspend = 1;
 404   2      
 405   2                      if(i_st & D12_INT_ENDP0IN)
 406   2                              ep0_txdone();
 407   2                      if(i_st & D12_INT_ENDP0OUT)
 408   2                              ep0_rxdone();
 409   2                      if(i_st & D12_INT_ENDP1IN)
 410   2                              ep1_txdone();
 411   2                      if(i_st & D12_INT_ENDP1OUT)
 412   2                              ep1_rxdone();
 413   2                      if(i_st & D12_INT_ENDP2IN)
 414   2                              ep2_txdone();
 415   2                      if(i_st & D12_INT_ENDP2OUT)
 416   2                              ep2_rxdone();
 417   2              }
 418   1                      
 419   1              bEPPflags.bits.in_isr = 0;
 420   1      }
 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.06   CHAP_9                                                                06/05/2005 13:58:57 PAGE 8   

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

 490   3                      }
 491   2              }
 492   1      
 493   1              else {
 494   2                      bEPPflags.bits.control_state = USB_IDLE;
 495   2              }
 496   1      }
 497          
 498          void ep0_txdone(void)
 499          {
 500   1              short i = ControlData.wLength - ControlData.wCount;
 501   1      
 502   1              D12_ReadLastTransactionStatus(1); // Clear interrupt flag
 503   1      
 504   1              if (bEPPflags.bits.control_state != USB_TRANSMIT) 
 505   1              {
 506   2                      single_transmit(0, 0);                  
 507   2                      return;
 508   2              }
 509   1              
 510   1              if( i >= EP0_PACKET_SIZE) {
 511   2                      D12_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData + ControlData.wCount);
 512   2                      ControlData.wCount += EP0_PACKET_SIZE;
 513   2      
 514   2                      bEPPflags.bits.control_state = USB_TRANSMIT;
 515   2              }
 516   1              else if( i != 0) {
 517   2                      D12_WriteEndpoint(1, i, ControlData.pData + ControlData.wCount);
 518   2                      ControlData.wCount += i;
 519   2      
 520   2                      bEPPflags.bits.control_state = USB_IDLE;
 521   2              }
 522   1              else if (i == 0){
 523   2                      D12_WriteEndpoint(1, 0, 0); // Send zero packet at the end ???
 524   2      
 525   2                      bEPPflags.bits.control_state = USB_IDLE;
 526   2              }
 527   1      }
 528          
 529          
 530          void usbserve(void)
 531          {
 532   1              if (bEPPflags.bits.bus_reset) {
 533   2                      DISABLE;
 534   2                      bEPPflags.bits.bus_reset = 0;
 535   2                      ENABLE;      
 536   2                      // Release D12's SUSPEND pin after bus reset
 537   2                      // Enable 74HCT123 pulse generation before disconnect
 538   2                      //D12SUSPD = 1;
 539   2              } // if bus reset
 540   1      
 541   1              if (bEPPflags.bits.suspend) {
 542   2                      DISABLE;
 543   2                      bEPPflags.bits.suspend= 0;
 544   2                      ENABLE;
 545   2                      /*
 546   2                      if(D12SUSPD == 1) {
 547   2                              D12SUSPD = 0;
 548   2                              P0 = 0xFF;
 549   2                              P1 = 0xFF;
 550   2                              P2 = 0xFF;
 551   2                              P3 = 0xFF;
C51 COMPILER V7.06   CHAP_9                                                                06/05/2005 13:58:57 PAGE 10  

 552   2                              D12_SetDMA(0x0);
 553   2                              D12SUSPD = 1;
 554   2                              PCON |= 0x02;
 555   2                              while (1);
 556   2                      }*/
 557   2                      
 558   2              } // if suspend change
 559   1      
 560   1              if (bEPPflags.bits.setup_packet){
 561   2                      DISABLE;
 562   2                      bEPPflags.bits.setup_packet = 0;
 563   2                      control_handler();
 564   2                      ENABLE;
 565   2                      //D12SUSPD = 1;
 566   2              } // if setup_packet
 567   1      }
 568          
 569          
 570          
 571          


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