📄 usb_to_serial.asm
字号:
jnz Fill_Config_Buffer ;
mov A, STATUS_IN_ONLY ;
mov [ep0_next_mode], A ;
ret ;
;****************************************************************
; Ep0_In handles an in transaction. This occurs in the data stage
; of a control read or in the status phase of a control write.
;****************************************************************
Ep0_In:
mov A, [ep0_mode_shadow] ;
and A, MODE ;
cmp A, NAK_IN_STATUS_OUT ;
jnz Stall_In_Out ;
call Control_Read ;
ret ;
;****************************************************************
; Stall_In_Out just calls send stall and then returns.
;****************************************************************
Stall_In_Out:
call Send_Stall ;
ret ;
;****************************************************************
; Stage_One
;****************************************************************
; Stage_One figures out the request type and the request
; using the request table to find the right value and then
; jumping to corresponding position in the jump table which
; in turn then jumps to the proper subroutine.
;****************************************************************
Stage_One:
mov X, SIZEOF_REQUEST_TABLE ;
.loop:
dec X ; If negative, this is
jc Send_Stall ; an unsupported request.
dec X ; Get the table entry for this
mov A,X ; index position.
index request_table ;
cmp A, [request_type] ; If there is no match then loop
jnz .loop ;
mov A, X ; Else find the matching request
index (request_table + 1) ;
cmp A, [request] ; If there's no match then loop
jnz .loop ;
; jump to jump table entry that
; corresponds the request_type/request
; pair that we found
.found:
mov A, X ;
jacc Jump_Table ;
;****************************************************************
; Send_Stall stalls unsupported functions by setting the
; EP0 mode register to STALL IN/OUT mode.
;****************************************************************
Send_Stall:
mov A, STALL_IN_OUT ;
mov [ep0_next_mode], A ;
ret ;
;============================================================
; Stage two: this is where the requests are handled. The
; jumps in the jump table above move into this stage.
;============================================================
;****************************************************************
; DEVICE REQUEST: Clear_Remote_Wakeup
;****************************************************************
; Clear_Remote_Wakeup is a CLEAR_FEATURE operation with a
; DEVICE recipient. The only feature available for a DEVICE
; is the DEVICE_REMOTE_WAKEUP feature.
;****************************************************************
Clear_Remote_Wakeup:
; If value is not DEVICE_REMOTE_WAKEUP,
; then stall.
mov A, [value_hi] ;
cmp A, 00h ;
jnz Send_Stall ;
mov A, [value] ;
cmp A, DEVICE_REMOTE_WAKEUP ;
jnz Send_Stall ;
mov A, DISABLE_REMOTE_WAKEUP ;
mov [device_status], A ;
call No_Data_Control ;
ret ;
;****************************************************************
; DEVICE REQUEST: Set_Remote_Wakeup
;****************************************************************
Set_Remote_Wakeup:
; If value is not DEVICE_REMOTE_WAKEUP,
; then stall.
mov A, [value_hi] ;
cmp A, 00h ;
jnz Send_Stall ;
mov A, [value] ;
cmp A, DEVICE_REMOTE_WAKEUP ;
jnz Send_Stall ;
mov A, ENABLE_REMOTE_WAKEUP ;
mov [device_status], A ;
call No_Data_Control ;
ret ;
;****************************************************************
; DEVICE REQUEST: Set_Address
;****************************************************************
Set_Address:
call No_Data_Control_Wait ;
iord EP0_MODE ;
and A, SETUP_RXD ;
jnz Set_Address_Done ;
mov A, [value] ;
or A, USB_ADDR_EN ;
iowr USB_ADDR ;
Set_Address_Done:
ret ;
;****************************************************************
; DEVICE REQUEST: Set_Configuration
;****************************************************************
Set_Configuration:
; The only allowed values for value are
; CONFIGURED (1) and UNCONFIGURED (0).
mov A, [value] ;
and A, ~CONFIGURED ;
or A, [value_hi] ;
jnz Send_Stall ;
;----------------------------------------------------------------
Set_Config_Value:
mov A, [value] ;
mov [configuration], A ;
cmp A, UNCONFIGURED ;
jnz Device_Configured ;
; Device is unconfigured. Disable endpoints
; and interrupts for endpoints 1 and 2.
mov A, DISABLED ; EP1_MODE = EP2_MODE = DISABLED
iowr EP1_MODE ;
iowr EP2_MODE ;
mov A, EP0_IE ; Disable EP1 & EP2 interrupts
iowr EP_IE ;
jmp Set_Configuration_Done ;
;----------------------------------------------------------------
Device_Configured:
; Device is configured. Enable endpoint
; modes and interrupts.
mov A, NAK_IN ; EP1_MODE = NAK_IN
iowr EP1_MODE ;
mov A, NAK_OUT ; EP2_MODE = NAK_OUT
iowr EP2_MODE ;
mov A, (EP0_IE | EP1_IE | EP2_IE) ; Enable all endpoint interrupts
iowr EP_IE ;
;----------------------------------------------------------------
Set_Configuration_Done:
; The USB spec specifies returning the
; endpoints to their default values whenever
; configuration is set or cleared.
mov A, 00h ; Clear endpoint stalls
mov [ep1_status], A ;
mov [ep2_status], A ;
iord EP1_COUNT ; Clear data toggles
and A, ~DATA_TOGGLE ;
iowr EP1_COUNT ;
iord EP2_COUNT ;
and A, ~DATA_TOGGLE ;
iowr EP2_COUNT ;
;----------------------------------------------------------------
call No_Data_Control ; handshake with host
ret ; return
;****************************************************************
; ENDPOINT REQUEST: Clear_Endpoint_Stall
;****************************************************************
Clear_Endpoint_Stall:
mov A, [configuration] ;
cmp A, UNCONFIGURED ;
jz Send_Stall ;
mov A, [value] ; Check if this is the right feature
cmp A, ENDPOINT_STALL ; if not then send stall
jnz Send_Stall ;
mov A, [index_hi] ;
cmp A, 00h ;
jnz Send_Stall ;
;----------------------------------------------------------------
mov A, [index] ; Ignore bit 7
and A, 7Fh ;
cmp A, 01h ;
jnz .clear2 ;
;----------------------------------------------------------------
.clear1:
; Clear endpoint 1 stall condition.
mov A, 00h ; not stalled
mov [ep1_status], A ;
iord EP1_COUNT ; Clear data toggle
and A, ~DATA_TOGGLE ;
iowr EP1_COUNT ;
mov A, NAK_IN ; NAK INs until there is
iowr EP1_MODE ; data in EP2 FIFO
jmp .done ;
;----------------------------------------------------------------
.clear2:
cmp A, 02h ;
jnz Send_Stall ;
; Clear endpoint 2 stall condition.
mov A, 00h ; ep2_status = not stalled
mov [ep2_status], A ;
iord EP2_COUNT ; Clear toggle bit
and A, ~DATA_TOGGLE ;
iowr EP2_COUNT ;
mov A, NAK_OUT ; NAK INs until there is
iowr EP2_MODE ; data in EP2 FIFO
;----------------------------------------------------------------
.done:
call No_Data_Control ; handshake with host
ret ; return
;****************************************************************
; REQUEST: Set_Interface
;****************************************************************
Set_Interface:
jmp Send_Stall ;
;****************************************************************
; ENDPOINT REQUEST: Set_Endpoint_Stall
;****************************************************************
Set_Endpoint_Stall:
; The only valid "feature" that can be set
; for an endpoint is the "stall" feature. If
; value is not ENDPOINT_STALL, then stall
; the request.
mov A, [value] ;
cmp A, ENDPOINT_STALL ;
jnz Send_Stall ;
; If the index_hi value is not 0, stall.
mov A, [index_hi] ;
or A, 00h ;
jnz Send_Stall ;
; If the device is not configured, stall the
; request.
mov A, [configuration] ;
cmp A, UNCONFIGURED ;
jz Send_Stall ;
;----------------------------------------------------------------
mov A, [index] ; Ignore bit 7
and A, 7Fh ;
cmp A, 01h ;
jnz .se2 ;
mov A, 01h ;
mov [ep1_status], A ;
mov A, STALL_IN_OUT ;
iowr EP1_MODE ;
jmp .done ;
;----------------------------------------------------------------
.se2:
cmp A, 02h ; if it's not a stall for ep2 then we
jnz Send_Stall ; are out of endpoints and we stall
mov A, 01h ;
mov [ep2_status], A ;
mov A, STALL_IN_OUT ;
iowr EP2_MODE ;
;----------------------------------------------------------------
.done:
call No_Data_Control ; handshake with host
ret ;
;****************************************************************
; DEVICE REQUEST: Get_Device_Status
;****************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -