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

📄 usbcode.asm

📁 芯片固件程序USB端Win32应用程序RS232端Win32应用程序
💻 ASM
📖 第 1 页 / 共 2 页
字号:
    mov a,(get_protocol_status_table - control_read_table)
    add a,[protocol_status]    
    jmp SendRomData                      
    
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; SetReport()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SetReport:
    mov a,[bmRequestType]
    cmp a,CLASS + HOST_TO_INTERFACE
    jnz SendStallEP0

    mov a,[wValueLi]             
    mov [current_reportid],a     
       
    mov a,[wLengthLi]            
    mov [data_count],a           

    mov a,00h                   
    iowr EP0_Rx_Status          

    mov a,[current_reportid]
    cmp a,RID_TRANSMIT          
    jz hid_report_incoming      

    cmp a,RID_COMMAND           
    jnz await_outs              
                                

  hid_report_incoming:
    mov a,TRANSMITTING_MASK       
    mov [interrupt_mask],a

    mov a,0                       
    iowr P0_IntEnable           

    FLOW_OFF                      

    mov a,FIRSTBYTE_FLG_MASK      
    or [fwcr], a                  

  await_outs:
    iord USB_Control              
    and a,~USBC_StatusOuts
    or a,USBC_EnableOuts
    iowr USB_Control

    ret    
    
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; SetIdle()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SetIdle:
    jmp SendStallEP0 
    
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; SetProtocol()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SetProtocol:
    mov a,[bmRequestType]
    cmp a,CLASS + HOST_TO_INTERFACE
    jnz SendStallEP0

    mov a,[wValueLi]
    mov [protocol_status],a      

    jmp NoDataControl            
       
    
;***********************************************************************************************
;                   USB传输函数的定义及实现
;***********************************************************************************************

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; SendROMData()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SendRomData:                                    
      mov [data_start],a           
      
      call CheckDescriptorLength   
      call ControlRead             

      ret                                 

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; SendStallEP0()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SendStallEP0:              
      mov a,EP0_Tx_Respond + EP0_Tx_Stall                   
      iowr EP0_Tx_Config           
      ret
      
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; CheckDescriptorLength()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CheckDescriptorLength:
    mov a,[wLengthHi]                
    cmp a,0                         
    jnz UseActualLength             
                                    
    mov a,[wLengthLi]             
    cmp a,[data_count]            
    jz UseActualLength            
    jnc UseActualLength           

    mov [data_count],a            

  UseActualLength:
      ret                   

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; NoDataControl()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
NoDataControl:
    iord EP0_Rx_Status               
    and a, ~EP0_Rx_Setup
    iowr EP0_Rx_Status
                                 
    mov a,EP0_Tx_Respond + EP0_Tx_Toggle   
    iowr EP0_Tx_Config                      

    ret             

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ControlRead()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ControlRead: 
    mov A, 00h         
    mov [endp0_data_toggle], A              

  ControlReadDataStage:
    push x              
    mov x,00h
    mov a,00h

    mov [loop_counter],a        
    iowr EP0_Rx_Status          
                                
    iord EP0_Rx_Status          
    and a,01h
    jnz ControlReadStatusStage          
                                            
    iord USB_Control            
    or a,USBC_StatusOuts        
    iowr USB_Control

    mov a,[data_count]          
    cmp a,00h
    jz DmaLoadDone

  DmaLoadLoop:                  
    mov a,[data_start]
    index control_read_table
    mov [x + EP0_FIFO],a        

    inc [data_start]
    inc x
    inc [loop_counter]

    dec [data_count]            
    jz DmaLoadDone              

    mov a,[loop_counter]        
    cmp a,08h                   
    jnz DmaLoadLoop

DmaLoadDone:
    iord EP0_Rx_Status          
    and a,01h                   
    jnz ControlReadStatusStage

    mov a,[endp0_data_toggle]
    xor a,EP0_Tx_Toggle
    mov [endp0_data_toggle],a

    or a,80h                    
    or a,[loop_counter]         
    iowr EP0_Tx_Config

ControlReadStatusStage:         
    pop x                       
    ret   

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ControlWrite()
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ControlWrite: 
ControlWriteRamDataStage:
    push x              

    mov a,[current_reportid]
    cmp a,RID_COMMAND       
    jnz ridtransmit
    
    mov a,[EP0_FIFO.1]      
    call handle_rid_command                 
    jmp cwhandshake

  ridtransmit:
    iord EP0_Rx_Status         
    rrc                                 
    rrc
    rrc
    rrc
    and a,0fh                  
    sub a,02h                  
    mov [loop_counter],a       
    
    mov x,00h                  

    mov a,[fwcr]                
    and a,FIRSTBYTE_FLG_MASK    
    jz DmaWriteRamLoop          

    mov a,[fwcr]
    and a,~FIRSTBYTE_FLG_MASK          
    or a,BUSY_TRANSMITTING             
    mov [fwcr],a

    mov a,[x+EP0_FIFO]                 
    cmp a,RID_TRANSMIT                 
    jnz cwhandshake                    

    dec [data_count]     
    dec [loop_counter]   
    inc x                

    mov a,[x+EP0_FIFO]
    mov [transmit_count],a       

    dec [data_count]             
    dec [loop_counter]           
    inc x                        

    mov a,INTDISABLE_MASK	 
    iowr Global_Interrupt        

  DmaWriteRamLoop:
    mov a,[transmit_count]       
    cmp a,0
    jz DmaWriteRam_next

    iowr WDT                     
    mov a,[x + EP0_FIFO]         
    mov [serial_tx_reg],a        
    call SerialTransmitByte      
    dec [transmit_count]         

  DmaWriteRam_next:
    inc x
    dec [data_count]             
    dec [loop_counter]           
    jnz DmaWriteRamLoop          

  DmaWriteRamDone:  
    mov a,[data_count]           
    cmp a,00h                    
    jnz cwenableouts             

  cwhandshake:
    call NoDataControl           
                                 
    jmp cwexit                   
    
  cwenableouts:
    iord USB_Control             
    or a,USBC_EnableOuts
    iowr USB_Control

  cwexit:
    pop X                                   
    ret

;*********************************************************************************************
;               ROM数据段 Data Segment (ROM)
;*********************************************************************************************
XPAGEOFF
control_read_table:

;============================================================================================
device_desc_table:          
    db (end_device_desc_table - device_desc_table)    
    db 01h                                            
    db 10h,01h                                        
    db 00h                                            
    db 00h                                            
    db 00h                                            
    db 08h                                            
    DWL xxxxh                          ;xxxx-十六进制数,需更改
    DWL xxxxh                          ;xxxx-十六进制数,需更改
    DWL 0001h                                         
    db 01h                                            
    db 02h                                            
    db 00h                                            
    db 01h                                            
end_device_desc_table:

;============================================================================================
config_desc_table:                                  
    db (Interface_Descriptor - config_desc_table)     
    db 02h                                          
    db (end_config_desc_table - config_desc_table) ,00h  
    db 01h                                               
    db 01h                                               
    db 00h                                               
    db 80h                                               
    db 32h                                               

;============================================================================================
Interface_Descriptor:                                    
    db (Class_Descriptor - Interface_Descriptor)         
    db 04h                                               
    db 00h                                               
    db 00h                                               
    db 01h                                               
    db 03h                                               
    db 00h                                               
    db 00h                                               
    db 00h                                               

;============================================================================================
Class_Descriptor:                                        
    db (Endpoint_Descriptor - Class_Descriptor)          
    db 21h                                               
    db 00h,01h                                           
    db 00h                                               
    db 01h                                               
    db 22h                                               
    dwl (end_hid_report_desc_table - hid_report_desc_table)

;============================================================================================
Endpoint_Descriptor:                                       
    db (end_config_desc_table - Endpoint_Descriptor)       
    db 05h                                                
    db 81h                                                
    db 03h                                                
    db EP1_REPORT_LENGTH+1,00h                            
    db POLLING_INTERVAL                                   
end_config_desc_table:

;============================================================================================
hid_report_desc_table:   
    db 06h,  00h, ffh    

    db 09h,  01h         
    db a1h,  01h         

    db 85h,  RID_RECEIVE    
    db  9h,  1h             
    db 15h,  0h             
    db 26h,  ffh, 0h        
    db 75h,  8h             
    db 95h,  EP1_REPORT_LENGTH 
    db 81h,  6h                
    db  9h,  1h                
    db b1h,  6h                

    db 85h,  RID_COMMAND       
    db 09h,  01h               
    db 15h,  00h               
    db 26h,  8h, 00h           
    db 75h,  08h               
    db 95h,  01h               
    db 09h,  01h        
    db b1h,  06h        
        
    db 85h,  RID_TRANSMIT    
    db 09h,  03h             
    db 96h,  07h, 00h        
    db b1h,  06h             

    db c0h                   
end_hid_report_desc_table:

;========================================================================
USBStringLanguageDescription:
    db (USBStringDescription1 - USBStringLanguageDescription) 
    db 03h      
    db 09h      
    db 04h      

USBStringDescription1:             
    db (USBStringDescription2 - USBStringDescription1)  
    db 03h                                              
    dsu "Ysu"                                           
                                                        

; string 2
USBStringDescription2:                                  
    db (USBStringDescription3 - USBStringDescription2)  
    db 03h                                              
    dsu "RS232-USB"  

; string 3                 
USBStringDescription3:                                  
    db (USBStringEnd - USBStringDescription3)           
    db 03h                                              
    dsu "Lyw"
USBStringEnd:
    
;========================================================================
get_dev_status_table:
        db 00h, 00h  
wakeup_enabled:      
        db 02h, 00h  

get_interface_status_table:
        db 00h, 00h                  

get_endpoint_status_table:
        db 00h, 00h                  
stalled:
        db 01h, 00h                  

get_configuration_status_table:
        db 00h                       
        db 01h                       

get_protocol_status_table:
        db 00h                       
        db 01h                       

get_interface_table:
        db 00h                       

XPAGEON             

⌨️ 快捷键说明

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