📄 js50.asm
字号:
storeHat:
mov [hat_bits], A ; save the hat status
nochange:
jmp main ; loop continuously
;========================================================================
; The endpoint zero interrupt service routine supports the control
; endpoint. This firmware enumerates and configures the hardware.
USB_EP0_ISR:
push A ; save accumulator on stack
iord USB_EP0_RX_Status ; load status register into accumulator
and A, 01h ; check if SETUP packet received
jz ep0_continue ; ignore unless SETUP packet
mov A,[interrupt_mask] ; disable endpoint zero interrupts
and A, 0F7h
mov [interrupt_mask], A
iowr Global_Interrupt
call StageOne ; parse SETUP packet
mov A, [interrupt_mask] ; enable endpoint zero interrupts
or A, 08h
mov [interrupt_mask], A
ep0_continue:
mov A, [interrupt_mask] ; enable the interrupts
ipret Global_Interrupt
;========================================================================
; stage one ... test bmRequestType
;========================================================================
StageOne:
;------------------------------------------------------------------------
; Parse standard device requests as per Table 9.2 in USB Spec.
;------------------------------------------------------------------------
mov A, 00h ; clear the setup flag to write DMA
iowr USB_EP0_RX_Status
mov A, 8 ; set BadOut bit
iowr USB_Status_Control
mov A, [bmRequestType] ; load bmRequestType
; host to device
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
jz RequestType80 ; bmRequestType = 10000000 device
cmp A, 81h
jz RequestType81 ; bmRequestType = 10000001 interface
cmp A, 82h
jz RequestType82 ; bmRequestType = 10000010 endpoint
;-----------------------------------------------------------------------
; Parse HID class device requests as per HID version 1.0 Draft #4
;-----------------------------------------------------------------------
; host to device
cmp A, 21h
jz RequestType21 ; bmRequestType = 00100001 interface
cmp A, 22h ; *** not in HID spec ***
jz RequestType22 ; bmRequestType = 00100010 endpoint
; device to host
cmp A, A1h
jz RequestTypeA1 ; bmRequestType = 10100001 interface
;-----------------------------------------------------------------------
; Stall unsupported functions
;-----------------------------------------------------------------------
SendStall: ; stall unsupported functions
mov A, A0h ; send a stall to indicate the requested
iowr USB_EP0_TX_Config ; function is not supported
ret ; return
;========================================================================
; stage two ... test bRequest
;========================================================================
; host to device with device as recipient
RequestType00:
mov A, [bRequest] ; load bRequest
;------------------------------------------------------------------------
; The only standard feature defined for a "device" recipient is
; device_remote_wakeup. Remote wakeup is the ability to "wakeup" a
; system from power down mode by pressing a key or moving a button.
; The default condition at reset is remote wakeup disabled.
;------------------------------------------------------------------------
; 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
;------------------------------------------------------------------------
; This request is optional. If a device supports this request, existing
; device descriptors may be updated or new descriptors may be added.
; Set Descriptor bRequest = 7 *** not supported ***
;------------------------------------------------------------------------
; If wValue is zero, the device is unconfigured. The only other legal
; configuration for this version of firmware is one.
; Set Configuration bRequest = 9
cmp A, set_configuration
jz SetConfiguration
jmp SendStall ; stall unsupported function calls
;========================================================================
; 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 ***
;------------------------------------------------------------------------
; This request allows the host to select an alternate setting for the
; specified interface. As the joystick only has one interface setting,
; this request is not supported.
; Set Interface bRequest = 11 *** not supported ***
; jmp SendStall ; stall unsupported functions
;========================================================================
; 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
jmp SendStall ; stall unsupported functions
;=======================================================================
; 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
jmp SendStall ; stall unsuported functions
;=======================================================================
; device to host with interface as recipient
RequestType81:
mov A, [bRequest] ; load bRequest
; Get Status bRequest = 0
cmp A, get_status
jz GetInterfaceStatus
;------------------------------------------------------------------------
; This request returns the selected alternate setting for the specified
; interface. There are no alternate settings for the joystick.
; Get Interface bRequest = 10 *** not supported ***
;------------------------------------------------------------------------
; HID class defines one more request for bmRequestType=10000001
; Get Descriptor bRequest = 6
cmp A, get_descriptor
jz GetDescriptor
jmp SendStall ; stall unsupported functions
;=======================================================================
; device to host with endpoint as recipient
RequestType82:
mov A, [bRequest] ; load bRequest
; Get Status bRequest = 0
cmp A, get_status
jz GetEndpointStatus
;------------------------------------------------------------------------
; Not defined in the spec, but it must be decoded for the enumeration to
; complete under Memphis.
; Get Descriptor bRequest = 6
cmp A, get_descriptor
jz GetDescriptor
; Sync Frame bRequest = 12 *** not supported ***
jmp SendStall ; stall unsupported functions
;========================================================================
; Now parse HID class Descriptor Types
;========================================================================
; 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
jmp SendStall ; stall unsupported functions
;=======================================================================
; This one is not in the spec, but has been captured with CATC while
; Memphis beta testing.
RequestType22:
mov A, [bRequest] ; load bRequest
; Set Report bRequest = 9
cmp A, set_report
jz SetReport
jmp SendStall ; stall unsupported functions
;=======================================================================
; 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
jmp SendStall ; stall unsupported functions
;========================================================================
; stage three ... process the request
;========================================================================
; Remote wakeup is the ability to wakeup a system from power down mode
; when the user presses a key or moves a joystick. These routines
; allow the host to enable/disable the ability to request remote wakeup.
;
; Disable the remote wakeup capability.
ClearRemoteWakeup:
mov A, [wValue] ; load wValue
cmp A, device_remote_wakeup ; test for valid feature
jnz SendStall ; stall unsupported features
call no_data_control ; handshake with host
mov A, DISABLE_REMOTE_WAKEUP ; disable remote wakeup
mov [remote_wakeup_status], A
ret ; return
; Enable the remote wakeup capability.
SetRemoteWakeup:
mov A, [wValue] ; load wValue
cmp A, device_remote_wakeup ; test for valid feature
jnz SendStall ; stall unsupported features
call no_data_control ; handshake with host
mov A, ENABLE_REMOTE_WAKEUP ; enable remote wakeup
mov [remote_wakeup_status], A
ret ; return
; Set the device address to the wValue in the SETUP packet at
; the completion of the current transaction.
SetAddress:
call no_data_control ; handshake with host
mov A, [wValue] ; load wValue
iowr USB_Device_Address ; write new USB device address
ret ; return
; Set the configuration of the device to either unconfigured (0) or
; configured (1) based on wValue in the SETUP packet. According to
; the USB spec (page 178), a Set Configuration also clears the endpoint
; stall condition and re-initializes endpoints using data 0/1 toggle to
; Data0.
SetConfiguration:
call no_data_control
mov A, [wValue] ; load wValue lsb
mov [configuration_status], A ; store configuration byte
mov A, 0
mov [endpoint_stall], A ; not stalled
iord USB_EP1_TX_Config ; clear data 0/1 bit
and A, ~DataToggle
iowr USB_EP1_TX_Config
mov A, [configuration_status]
cmp A, 0
jnz device_configured
; device is unconfigured
iord USB_EP1_TX_Config
and A, EFh ; disable endpoint 1
iowr USB_EP1_TX_Config
mov A, [interrupt_mask] ; disable endpoint one interrupts
and A, EFh
mov [interrupt_mask], A
jmp done_configuration
; device is configured
device_configured:
iord USB_EP1_TX_Config ; NAK IN packets until data is
and A,7Fh ; ready on endpoint one
or A, 10h ; enable endpoint one
iowr USB_EP1_TX_Config
mov A, [interrupt_mask] ; enable endpoint one and GPIO interrupts
or A, 50h
mov [interrupt_mask], A
iord USB_Status_Control ; NAK IN packets until data is
and A,0EFh ; ready on endpoint one
iowr USB_Status_Control
done_configuration:
ret ; return
; Clear the endpoint stall feature for the selected endpoint. This
; should also set the data 0/1 bit to Data0 if endpoint one is selected.
ClearEndpointStall:
mov A, [wValue] ; load wValue (which feature)
cmp A, endpoint_stalled ; test for valid feature
jnz SendStall ; stall unsupported features
;
; clear endpoint one stall feature
;
call no_data_control ; handshake with host
mov A,0
mov [endpoint_stall], A ; not stalled
iord USB_EP1_TX_Config ; clear data 0/1 bit
and A, ~DataToggle
iowr USB_EP1_TX_Config
iord USB_Status_Control ; NAK IN packets until data is
and A,0EFh ; ready on endpoint one
iowr USB_Status_Control
ret ; return
; Set the endpoint stall feature for the selected endpoint.
SetEndpointStall:
mov A, [wValue] ; load wValue
cmp A, endpoint_stalled ; test for valid feature
jnz SendStall ; stall unsupported features
call no_data_control ; handshake with host
mov A,1
mov [endpoint_stall], A ; stalled
mov A, 30h ; stall endpoint one
iowr USB_EP1_TX_Config
ret ; return
; The device status is a 16-bit value (two bytes) with only D[1:0]
; defined. D0=0 specifies bus-powered, which never changes. D1
; reflects the status of the device_remote_wakeup feature. This
; feature can either be disabled (D1=0) or enabled (D1=1).
GetDeviceStatus:
mov A, 2 ; send two bytes
mov [data_count], A
mov A, (get_dev_status_table - control_read_table)
add A, [remote_wakeup_status] ; get correct remote wakeup
jmp execute ; send device status to host
; There are five descriptor types. The descriptor type will be in
; the high byte of wValue. The descriptor index will be in the low
; byte of wValue. The standard request to a device supports three
; of these types: device, configuration, and string. The standard
; request does not support interface or endpoint descriptor types.
GetDescriptor:
mov A, [wValueHi] ; load descriptor type
;------------------------------------------------------------------------
; Test for standard descriptor types first.
; 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
; No strings in the joystick code, yet.
;------------------------------------------------------------------------
; Then 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 ***
jmp SendStall ; stall unsupported functions
; Return the current device configuration to the host. The possible
; values are zero (unconfigured) and one (configured).
GetConfiguration:
mov A, 1 ; send one byte
mov [data_count], A
mov A, (get_configuration_status_table - control_read_table)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -