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

📄 usbcode.asm

📁 USB2.0原理与工程开发光盘(第二版)
💻 ASM
📖 第 1 页 / 共 2 页
字号:
;===============================================================================================
; USBCODE.asm ---处理端点0的控制传输
;===============================================================================================

;***********************************************************************************************
; DecodeRequest()
;***********************************************************************************************
DecodeRequest:
    mov a,[bmRequestType]        
    and a,60h                    
    jnz ClassRequest             

  ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ; 处理标准设备请求
  ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  StandardRequest:
    mov a,[bRequest]               
    asl a                        
    cmp a, (end_standard_request_table - standard_request_table + 1)   ;cmp a,24
    jnc SendStallEP0             
                                 
                                 
    jacc standard_request_table   
  
  
    db 00,00,00,00,00,00,00,00,00,00,00,00
    db 00,00,00,00,00,00,00,00,00,00,00,00
    
    XPAGEOFF
  standard_request_table:
    jmp GetStatus
    jmp ClearFeature
    jmp SendStallEP0         
    jmp SetFeature              
    jmp SendStallEP0         
    jmp SetAddress
    jmp GetDescriptor
    jmp SetDescriptor
    jmp GetConfiguration
    jmp SetConfiguration
    jmp GetInterface
    jmp SetInterface
  end_standard_request_table:     
    XPAGEON

  ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ; 处理HID设备类专用请求
  ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ClassRequest:
    cmp a,20h                     
    jnz VendorRequest             

    mov a,[bRequest]                   
    asl a                         
    cmp a, (end_HID_request_table - HID_request_table + 1)   
    jnc SendStallEP0

    jacc HID_request_table         
    
    XPAGEOFF
  HID_request_table:              
    jmp SendStallEP0               
    jmp GetReport
    jmp GetIdle
    jmp GetProtocol
    jmp SendStallEP0               
    jmp SendStallEP0               
    jmp SendStallEP0               
    jmp SendStallEP0               
    jmp SendStallEP0               
    jmp SetReport
    jmp SetIdle
    jmp SetProtocol
  end_HID_request_table:
    XPAGEON
    
  ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ; 处理供应商自定义的请求
  ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  VendorRequest:
    jmp SendStallEP0               

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; GetStatus()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
GetStatus:    
    mov a,2              
    mov [data_count],a

    mov a,[bmRequestType]     
    cmp a, DEVICE_TO_HOST               
    jz GetDeviceStatus

    cmp a, ENDPOINT_TO_HOST
    jz GetEndpointStatus

    cmp a, INTERFACE_TO_HOST
    jnz SendStallEP0
    
  GetInterfaceStatus:
      mov a,(get_interface_status_table - control_read_table)
      jmp SendRomData        
       
  GetDeviceStatus:
      mov a,(get_dev_status_table - control_read_table)
      jmp SendRomData        
    
  GetEndpointStatus:
      iord EP1_Tx_Config
      and a,EP1_Tx_Stall      
      jz .Send                                
      mov a,(stalled - get_endpoint_status_table) 

   .Send:
      add a,(get_endpoint_status_table - control_read_table)
      jmp SendRomData              
    
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ClearFeature()
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++    
ClearFeature:
     mov a,[bmRequestType]      
     cmp a,HOST_TO_DEVICE           
     jz ClearRemoteWakeup            

     cmp a,HOST_TO_ENDPOINT
     jnz SendStallEP0                   

  ClearEndpointStall:
     mov a,[wValueLi]
     cmp a,EP1_STALLED       
     jnz SendStallEP0        

     call NoDataControl      

     iord EP1_Tx_Config                    
     and A, ~(EP1_Tx_Stall + EP1_Tx_Toggle) 
     iowr EP1_Tx_Config

     ret

   ClearRemoteWakeup:
     jmp SendStallEP0          
  
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; SetFeature()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SetFeature:
    mov a,[bmRequestType]     
    cmp a,HOST_TO_DEVICE
    jz SetRemoteWakeup          

    cmp a,HOST_TO_ENDPOINT
    jnz SendStallEP0                   

  SetEndpointStall:
      mov a,[wValueLi]                       
      cmp a,EP1_STALLED        
      jnz SendStallEP0         

      call NoDataControl       

      mov a,(EP1_Tx_Stall + EP1_Tx_Enable) 
      iowr EP1_Tx_Config                 
      ret                                   

    SetRemoteWakeup:
      jmp SendStallEP0       
    
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; SetAddress()
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SetAddress:
    mov a,[bmRequestType]
    cmp a,HOST_TO_DEVICE
    jnz SendStallEP0

    mov a,[wValueLi]         
    and a,80h                      
    jnz SendStallEP0                

    call NoDataControl       

  .wait:
    iowr WDT                 
    iord EP0_Tx_Config       
    and a,80h                
    jnz .wait                

    mov a,[wValueLi]         
    iowr USB_DeviceAddress
    ret                     
    
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; GetDescriptor()
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
GetDescriptor:
    mov a,[bmRequestType]
    cmp a,DEVICE_TO_HOST
    jnz GetClassDescriptor            

    mov a,[wValueHi]                  
    asl a                             
    cmp a,(end_desc_type_table - desc_type_table + 1)
    jnc SendStallEP0                  

    jacc desc_type_table

 GetClassDescriptor:
    mov a,[bmRequestType]
    cmp a,INTERFACE_TO_HOST 
    jnz SendStallEP0 

    mov a,[wValueHi]        
    and a,dfh               
                            
    asl a                   
    cmp a,(end_class_desc_type_table - class_desc_type_table + 1)
    jnc SendStallEP0        

    jacc class_desc_type_table

    XPAGEOFF
  desc_type_table: 
    jmp SendStallEP0                
    jmp GetDeviceDescriptor         
    jmp GetConfigurationDescriptor  
    jmp GetStringDescriptor         
  end_desc_type_table:

  class_desc_type_table:  
    jmp SendStallEP0                
    jmp GetHIDDescriptor            
    jmp GetReportDescriptor         
    jmp GetPhysicalDescriptor       
  end_class_desc_type_table:
    XPAGEON

  GetDeviceDescriptor:
    mov a,(end_device_desc_table - device_desc_table)
    mov [data_count],a         

    mov a,(device_desc_table - control_read_table)
    jmp SendRomData            

  GetConfigurationDescriptor:
    mov a,(end_config_desc_table - config_desc_table)
    mov [data_count],a             

    mov a,(config_desc_table - control_read_table)
    jmp SendRomData                

  GetStringDescriptor:               
    mov a,[wValueLi]                
    cmp a,(end_string_length_table - string_length_table + 1)
    jnc SendStallEP0                
                                    

    index string_length_table       
    mov [data_count],a                   

    mov a,[wValueLi]                      
    index string_offset_table       
    jmp SendRomData                   
    
    XPAGEOFF
  string_length_table:
    db (USBStringDescription1 - USBStringLanguageDescription)
    db (USBStringDescription2 - USBStringDescription1)
    db (USBStringDescription3 - USBStringDescription2)
    db (USBStringEnd - USBStringDescription3)
  end_string_length_table:

  string_offset_table:
    db (USBStringLanguageDescription - control_read_table)
    db (USBStringDescription1 - control_read_table)
    db (USBStringDescription2 - control_read_table)
    db (USBStringDescription3 - control_read_table)
  end_string_offset_table:
    XPAGEON
    
  GetReportDescriptor:
    mov a,(end_hid_report_desc_table - hid_report_desc_table)
    mov [data_count],a               

    mov a,(hid_report_desc_table - control_read_table)
    jmp SendRomData                  

  GetHIDDescriptor:
    mov a,(Endpoint_Descriptor - Class_Descriptor)
    mov [data_count],a          

    mov a,(Class_Descriptor - control_read_table)
    jmp SendRomData             

  GetPhysicalDescriptor:
    jmp SendStallEP0            
        
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; SetDescriptor()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SetDescriptor:              
    jmp SendStallEP0    
    
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; GetConfiguration()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
GetConfiguration:
    mov a,[bmRequestType]
    cmp a,DEVICE_TO_HOST
    jnz SendStallEP0

    mov a,1                          
    mov [data_count],a

    mov a,(get_configuration_status_table - control_read_table)
    add a,[configuration_status]     

    jmp SendRomData                  
    
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; SetConfiguration()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SetConfiguration:
    mov a,[bmRequestType]
    and a,ffh                 
    jnz SendStallEP0

    call NoDataControl        

    mov a,[wValueLi]
    mov [configuration_status],a    

    iord EP1_Tx_Config              
    and a,~(EP1_Tx_Toggle + EP1_Tx_Stall)
    iowr EP1_Tx_Config    

    mov a,[configuration_status]
    and a,ffh                       
    jnz ConfigureDevice

  UnconfigureDevice:
    iord EP1_Tx_Config
    and a,~EP1_Tx_Enable            
    iowr EP1_Tx_Config            

    mov a,[interrupt_mask]          
    and a,~USB_EP1_IE
    mov [interrupt_mask],a

    ret

  ConfigureDevice:
    iord EP1_Tx_Config
    and a,7Fh                       
    or a,EP1_Tx_Enable              
    iowr EP1_Tx_Config            

    mov a,[interrupt_mask]          
    or a,ENUMERATED_MASK
    mov [interrupt_mask],a

    ret                             

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; GetInterface()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
GetInterface:
    mov a,[bmRequestType]
    cmp a,INTERFACE_TO_HOST
    jnz SendStallEP0

    mov a,[wIndexLi]          
    cmp a,0                   
    jnz SendStallEP0

    mov a,1                   
    mov [data_count],a

    mov a,(get_interface_table - control_read_table)
    jmp SendRomData           
    
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; SetInterface()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SetInterface:
    mov a,[bmRequestType]
    cmp a,DEVICE_TO_HOST
    jnz SendStallEP0

    mov a,[wIndexLi]            
    cmp a,0                     
    jnz SendStallEP0

    mov a,[wValueLi]            
    cmp a,0                     
    jnz SendStallEP0

    jmp NoDataControl           
    
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; GetReport()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
GetReport:
    mov a,[bmRequestType]
    cmp a,CLASS + INTERFACE_TO_HOST
    jnz SendStallEP0           

    jmp SendStallEP0
    
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; GetIdle()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
GetIdle:
      jmp SendStallEP0

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; GetProtocol()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
GetProtocol:
    mov a,[bmRequestType]
    cmp a,CLASS + INTERFACE_TO_HOST
    jnz SendStallEP0

    mov a,1         
    mov [data_count],a

⌨️ 快捷键说明

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