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

📄 usb232.asm

📁 用CY7C630芯片做USB转串口的汇编原程序,里面有编译生成的hex文件.写入芯片就可使用.最高波特率为24
💻 ASM
📖 第 1 页 / 共 3 页
字号:
      jmp Get_Received_Data

      DoneWithReceivedData:
      pop X

;Handshake by sending a 0-byte data packet.
call Send0ByteDataPacket

done_with_packet:
;Re-enable Endpoint 0 interrupts.
     mov A,[interrupt_mask]
     or A, 08h
     mov [interrupt_mask], A
     ipret Global_Interrupt

StageOne:
;Clear the setup flag
     mov A, 00h
     iowr USB_EP0_RX_Status
;Set the StatusOuts bit to cause auto-handshake after receiving a data packet.
     mov A, 8
     iowr USB_Status_Control
;bmRequestType contains the request.
      mov A, [bmRequestType]

;Standard device requests. From the USB spec.
; host to device requests
        cmp A, 00h
        jz RequestType00                 ; bmRequestType = 00000000 device
;       cmp A, 01h                       *** not required ***  
;       jz RequestType01                 ; bmRequestType = 00000001 interface
        cmp A, 02h              
        jz RequestType02                 ; bmRequestType = 00000010 endpoint
        cmp A, 80h             
; device to host requests
        jz RequestType80                 ; bmRequestType = 10000000 device
        cmp A, 81h
        jz RequestType81                 ; bmRequestType = 10000001 interface
        cmp A, 82h
        jz RequestType82                 ; bmRequestType = 10000010 endpoint

;HID-class device requests. From the HID spec
; host to device requests
        cmp A, 21h
        jz RequestType21                 ; bmRequestType = 00100001 interface
        cmp A, 22h                       ; *** not in HID spec ***
        jz RequestType22                 ; bmRequestType = 00100010 endpoint
; device to host requests
        cmp A, A1h
        jz RequestTypeA1                 ; bmRequestType = 10100001 interface

; Stall unsupported requests
SendStall:
      mov A, A0h
     iowr USB_EP0_TX_Config
      ret

;Host to device with device as recipient
RequestType00:

;The Remote Wakeup feature is disabled on reset.
     mov A, [bRequest]     ; load bRequest
; Clear Feature                      bRequest = 1
     cmp A, clear_feature
     jz ClearRemoteWakeup 
; Set Feature                     bRequest = 3
     cmp A, set_feature
     jz SetRemoteWakeup

; Set the device address to a non-zero value.
; Set Address                     bRequest = 5
     cmp A, set_address
     jz SetAddress

; Set Descriptor is optional.
; Set Descriptor                bRequest = 7    *** not supported ***

;If wValue is zero, the device is unconfigured.
;The only other legal value for this firmware is 1.
;Set Configuration           bRequest = 9
    cmp A, set_configuration
    jz SetConfiguration

;Stall unsupported requests.
    jmp SendStall


;Host to device with interface as recipient    *** not required ***
; RequestType01:
;        mov A, [bRequest]       ; load bRequest

; There are no interface features defined in the spec.
; Clear Feature                 bRequest = 1    *** not supported ***
; Set Feature                   bRequest = 3    *** not supported ***

; Set Interface is optional.
; Set Interface                 bRequest = 11   *** not supported ***

;Stall unsupported requests.
;        jmp SendStall

;Host to device with endpoint as recipient
RequestType02:
     mov A, [bRequest]     ; load bRequest

; The only standard feature defined for an endpoint is endpoint_stalled.
; Clear Feature               bRequest = 1
     cmp A, clear_feature
     jz ClearEndpointStall
; Set Feature               bRequest = 3
     cmp A, set_feature
     jz SetEndpointStall
 
;Stall unsupported functions.
    jmp SendStall

;Device to host with device as recipient
RequestType80:
     mov A, [bRequest]          ; load bRequest

; Get Status               bRequest = 0
     cmp A, get_status
     jz GetDeviceStatus

; Get Descriptor               bRequest = 6
     cmp A, get_descriptor
     jz GetDescriptor

; Get Configuration          bRequest = 8
     cmp A, get_configuration
     jz GetConfiguration

;Stall unsupported requests.
     jmp SendStall

;Device to host with interface as recipient
RequestType81:
     mov A, [bRequest]     ; load bRequest

; Get Status               bRequest = 0
      cmp A, get_status
      jz GetInterfaceStatus

; Get Interface returns the selected alternate setting.
;  This firmware supports no alternate settings.
; Get Interface                 bRequest = 10   *** not supported ***

;The HID class defines one more request for bmRequestType=10000001
; Get Descriptor                bRequest = 6
      cmp A, get_descriptor
      jz GetDescriptor

;Stall unsupported functions
      jmp SendStall

;Device to host with endpoint as recipient
RequestType82:
     mov A, [bRequest]          ; load bRequest
; Get Status               bRequest = 0
     cmp A, get_status
      jz GetEndpointStatus

; Get Descriptor               bRequest = 6
     cmp A, get_descriptor
     jz GetDescriptor
; Sync Frame                  bRequest = 12   *** not supported ***

;Stall unsupported functions.
     jmp SendStall


;Check for HID class requests

;Host to device with endpoint as recipient
RequestType21:
      mov A, [bRequest]      ; load bRequest

; Set Report               bRequest = 9
     cmp A, set_report
     jz SetReport

; Set Idle                    bRequest = 10
      cmp A, set_idle
      jz SetIdle

; Set Protocol               bRequest = 11
      cmp A, set_protocol
      jz SetProtocol

;Stall unsupported requests
     jmp SendStall

RequestType22:
        mov A, [bRequest]      ; load bRequest

; Set Report               bRequest = 9
     cmp A, set_report     
     jz SetReport

;Stall unsupported requests
     jmp SendStall

;Device to host with endpoint as recipient
RequestTypeA1:
        mov A, [bRequest]      ; load bRequest

; Get Report               bRequest = 1
        cmp A, get_report
        jz GetReport

; Get Idle                 bRequest = 2
        cmp A, get_idle
        jz GetIdle

; Get Protocol             bRequest = 3
        cmp A, get_protocol
        jz GetProtocol

;Stall unsupported requests
        jmp SendStall


ClearRemoteWakeup:
        mov A, [wValue]
        cmp A, device_remote_wakeup
        jnz SendStall
;Handshake by sending a data packet
        call Send0ByteDataPacket
        mov A, DISABLE_REMOTE_WAKEUP
        mov [remote_wakeup_status], A
        ret

; Enable the remote wakeup capability.
SetRemoteWakeup:
        mov A, [wValue]
        cmp A, device_remote_wakeup
;Not a match, so stall.
        jnz SendStall
;Handshake by sending a 0-byte data packet
        call Send0ByteDataPacket
;Perform the request.
        mov A, ENABLE_REMOTE_WAKEUP
        mov [remote_wakeup_status], A
        ret

SetAddress:
; Set the device address to the wValue in the SETUP packet.
;Unlike other requests, complete the requested action after completing
;the transaction.
;Handshake by sending a 0-byte data packet.
        call Send0ByteDataPacket
;Perform the request
        mov A, [wValue]
        iowr USB_Device_Address
        ret

SetConfiguration:
;Unconfigured: wValue=0, configured: wValue=1.
;Also clear any stall condition and set Data 0/1 to Data0.
;Handshake by sending a 0-byte data packet.
      call Send0ByteDataPacket
;Save the configuration status.
     mov A, [wValue]
      mov [configuration_status], A
;Clear any stall condtion
      mov A, 0
      mov [endpoint_stall], A
;Set data 0/1 to Data0
      iord USB_EP1_TX_Config
      and A, ~DataToggle

;Set the configuration status.
     iowr USB_EP1_TX_Config     
     mov A, [configuration_status]
     cmp A, 0
;If configured, jump.
     jnz device_configured

;If unconfigured:
;Disable Endpoint 1
     iord USB_EP1_TX_Config
     and A, EFh
     iowr USB_EP1_TX_Config
;Disable Endpoint 1 interrupts.
     mov A, [interrupt_mask]
     and A, EFh
          mov [interrupt_mask], A
     jmp done_configuration

;If configured:
device_configured:
;Send NAK in response to IN packets
     iord USB_EP1_TX_Config
     and A,7Fh
;Enable Endpoint 1
     or A, 10h
     iowr USB_EP1_TX_Config
;Enable interrupts: Endpoint 1 and GPIO
     mov A, [interrupt_mask]
     or A, 50h
     mov [interrupt_mask], A
;Send NAK in response to endpoint 0 OUT packets.
     iord USB_Status_Control
     and A,0EFh
     iowr USB_Status_Control
done_configuration:
        ret

ClearEndpointStall:
;Clear the stall condition for an endpoint.
;For endpoint 1, also set Data 0/1 to Data 0.
        mov A, [wValue]
        cmp A, endpoint_stalled
        jnz SendStall
;
;Clear Endpoint 1 stall
;Handshake by sending a 0-byte data packet
      call Send0ByteDataPacket
;Clear the stall condition
      mov A,0
      mov [endpoint_stall], A
;Set Data 0/1 to Data0
      iord USB_EP1_TX_Config
      and A, ~DataToggle
      iowr USB_EP1_TX_Config
;Send NAK in response to Endpoint 0 OUT packets.
      iord USB_Status_Control
      and A,0EFh
      iowr USB_Status_Control
      ret

;Stall Endpoint 1.
SetEndpointStall:
        mov A, [wValue]
        cmp A, endpoint_stalled
;Not a match, so stall
        jnz SendStall
;Handshake by sending a 0-byte data packet.
        call Send0ByteDataPacket
;Stall the endpoint.
        mov A,1         
        mov [endpoint_stall], A
        mov A, 30h
        iowr USB_EP1_TX_Config                 
        ret

GetDeviceStatus:
;Device Status is two bytes.
;Bit 0 must be 0 (bus-powered).
;Bit 1 is remote wakeup: 0=disabled, 1=enabled.
;All other bits are unused.
;Send two bytes.
        mov A, 2
        mov [data_count], A
;The control_read_table holds the two possible values for device status.
;Get the address of the first value.
        mov A, (get_dev_status_table - control_read_table)
;Add an index value to select the correct bytes.
        add A, [remote_wakeup_status]
;Send the value.
        jmp SendDescriptor

GetDescriptor:
;The high byte of wValue contains the descriptor type.
;The low byte of wValue contains the descriptor index.

        mov A, [wValueHi]               ; load descriptor type

;Test for standard descriptor types first.
;Supported descriptor types are device, configuration, string.
;Unsupported descriptor types are interface, endpoint.

; Get Descriptor (device)               wValueHi = 1
     cmp A, device
     jz GetDeviceDescriptor
; Get Descriptor (configuration)        wValueHi = 2
     cmp A, configuration
     jz GetConfigurationDescriptor
; Get Descriptor (string)               wValueHi = 3
     cmp A, string
     jz GetStringDescriptor

; Test for HID-class descriptor types.
; Get Descriptor (HID)                  wValueHi = 21h
        cmp A, HID
        jz GetHIDDescriptor
; Get Descriptor (report)               wValueHi = 22h  
     cmp A, report
     jz GetReportDescriptor
; Get Descriptor (physical)             wValueHi = 23h  *** not supported ***
;Stall unsupported requests.
     jmp SendStall

GetConfiguration:
;Send the current device configuration.
;0 = unconfigured, 1 = configured.

;Send 1 byte
        mov A, 1
        mov [data_count], A
;Get the address of the data to send.
        mov A, (get_configuration_status_table - control_read_table)
;Add an index to point to the correct configuration.
        add A, [configuration_status]
;Send the data.
        jmp SendDescriptor

GetInterfaceStatus:
;Interface status is 2 bytes, which are always 0.
;Send 2 bytes.
        mov A, 2
        mov [data_count], A
;Get the address of the data to send.
        mov A, (get_interface_status_table - control_read_table)
;Send the data.
        jmp SendDescriptor

GetEndpointStatus:
;Endpoint status is 2 bytes.
;Bit 0 = 0 when the endpoint is not stalled.
;Bit 0 = 1 when the endpoint is stalled.
;All other bits are unused.
;Send 2 bytes.
        mov A, 2
        mov [data_count], A
;Get the stall status.
        mov A, [endpoint_stall]
;Shift left to get an index (0 or 2) for the endpoint status table
        asl A
;Get the address of the data to send.
        add A, (get_endpoint_status_table - control_read_table)
;Send the data.
        jmp SendDescriptor

SetReport:
;The CY7C63000 doesn't support interrupt-mode OUT transfers.
;So the host uses Control transfers with Set_Report requests
;to get data from the device.

;Get the report data.

;debug: set Port 0, bit 0 =1 to show that we're here.
;      iord Port0_Data
;      or a, 1
;      iowr Port0_Data

;Find out how many bytes to read. This value is in WLength.
;Save the length in data_count.
     mov A, [wLength]
     mov [data_count], A

;Enable receiving data at endpoint 0 by setting the EnableOuts bit

⌨️ 快捷键说明

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