📄 usbled.asm
字号:
mov A,[Data_Byte0]
cpl a
iowr Port0_Data
iowr Watchdog
nochange:
jmp main
;----------------------------------------------------------------------
;The endpoint 0 ISR supports the control endpoint.
;This code enumerates and configures the hardware.
;It also responds to Set Report requests that receive data from the host.
;----------------------------------------------------------------------
USB_EP0_ISR:
push A
iord USB_EP0_RX_Status
;Has a Setup packet been received?
and A, 01h
;If no, ignore.
jz check_for_out_packet
;If yes, handle it.
;Disable endpoint 0 interrupts.
mov A,[interrupt_mask]
and A, 0F7h
mov [interrupt_mask], A
iowr Global_Interrupt
;Find out what the setup packet contains and handle the request.
call StageOne
;Re-enable Endpoint 0 interrupts.
mov A, [interrupt_mask]
or A, 08h
mov [interrupt_mask], A
jmp done_with_packet
check_for_out_packet:
iord USB_EP0_RX_Status
;Is it an OUT packet?
and A, 02h
;If no, ignore it.
jz done_with_packet
;If yes, process the received data.
;Disable Endpoint 0 interrupts.
mov A,[interrupt_mask]
and A, 0F7h
mov [interrupt_mask], A
iowr Global_Interrupt
push X
;data_count holds the number of bytes left to read.
;X holds the index of the address to read
;and the index of the address to store the received data.
;Initialize the X register.
mov X, 0
Get_Received_Data:
;Find out if there are any bytes to read.
mov A, 0
cmp A, [data_count]
;Jump if nothing to read.
jz DoneWithReceivedData
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Get a byte.
mov A, [X + Endpoint_0]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mov [X + Data_Byte0], A
; cpl a
; iowr Port0_Data
;Decrement the number of bytes to read.
dec [data_count]
;Increment the address to read.
inc X
;Do another
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
;========================================================================
;Control transfers
;========================================================================
;------------------------------------------------------------------------
;Control transfer, stage one.
;Find out whether the request is a standard device or HID request,
;the direction of data transfer,
;and whether the request is to a device, interface, or endpoint.
;(from Table 9.2 in the USB spec)
;------------------------------------------------------------------------
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
;----------------------------------------------------------------------
;Control transfer, stage two
;Find out which request it is.
;----------------------------------------------------------------------
;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
;----------------------------------------------------------------------
;Control transfer, stage three
;Process the request.
;----------------------------------------------------------------------
;The host controls whether or not a device can request a remote wakeup.
; Disable the remote wakeup capability.
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -