📄 usb_id_key.asm
字号:
incf TableCnt,F
incf LengthCnt,F
decf WLengthTemp,F
testz WLengthTemp
btfsc Zero
goto SetTgl
movfw TableCnt
call DeviceDescTable
btfss TableSelBit
goto Sendbyte5
movfw TableCnt
call Report1DescTable
Sendbyte5:
movwr Tx0Fifo5
incf TableCnt,F
incf LengthCnt,F
decf WLengthTemp,F
testz WLengthTemp
btfsc Zero
goto SetTgl
movfw TableCnt
call DeviceDescTable
btfss TableSelBit
goto Sendbyte6
movfw TableCnt
call Report1DescTable
Sendbyte6:
movwr Tx0Fifo6
incf TableCnt,F
incf LengthCnt,F
decf WLengthTemp,F
testz WLengthTemp
btfsc Zero
goto SetTgl
movfw TableCnt
call DeviceDescTable
btfss TableSelBit
goto Sendbyte7
movfw TableCnt
call Report1DescTable
Sendbyte7:
movwr Tx0Fifo7
incf TableCnt,F
incf LengthCnt,F
decf WLengthTemp,F
SetTgl:
movlw 40h
xorwf Tx0Reg,F
SetCnt:
movlw f0h
andwf Tx0Reg,F
movfw LengthCnt
iorwf Tx0Reg,F
bsf Tx0Rdy
; bsf Rc0Rdy
ret
;==============================================================================
;Function:NoDataControl
; No data control task.
;==============================================================================
NoDataControl:
movlw c0h
movwf Tx0Reg
bsf Rc0Rdy
ret
;==============================================================================
;Function:Tx0Task
; 1.Check transfer buffer, if we still have data to transfer
;==============================================================================
Tx0Task:
Ep0In:
bcf Tx0Flag
btfss NewAdrFlag
goto Tx0TransCheck
SetNewAdr:
movfw WValue
iorwf UsbFun,F
bcf NewAdrFlag
Tx0TransCheck:
testz WLengthTemp
btfss Zero
goto NormalTrans
bcf TableSelBit
bsf Rc0Rdy
ret
NormalTrans:
call Tx0Transfer
clrf DelayCount_us ;Used for a 5 ms delay at power-on
Delay5ms:
decfsz DelayCount_us,F
goto Delay5ms
btfsc Tx0Flag
ret
bsf Rc0Rdy
ret
;==============================================================================
;Function:Rc0Task
; In the case of the keyboard, check whitch command to be received, an ACK out
; occurs either in the case of the status phase of a control read, or the data
; out phase of a control write.
;==============================================================================
Rc0Task:
bcf Rc0Flag
btfsc Rc0Err
goto Rc0TaskError
btfsc Ep0Dir
goto Rc0TaskError
btfss Ep0Set
goto Ep0Out
btfsc Rc0Tgl
goto Rc0TaskError
movlw 0fh
andwf Rc0Cnt,W
movwf AnyCountTemp
movlw 08h
subwf AnyCountTemp,F
btfss Zero
goto Rc0TaskError
Ep0Setup:
bcf Tx0Tgl
movlw HidSetRequest ;(21h)
subwf BmRequestType,W
btfsc Zero
goto HidSetRequestTask
movlw HidGetRequest ;(a1h)
subwf BmRequestType,W
btfsc Zero
goto HidGetRequestTask
movlw SetAddress ;(05h)
subwf BRequest,W
btfsc Zero
goto SetAddressTask
movlw GetConfiguration ;(08h)
subwf BRequest,W
btfsc Zero
goto GetConfigurationTask
movlw SetConfiguration ;(09h)
subwf BRequest,W
btfsc Zero
goto SetConfigurationTask
movlw GetStatus ;(00h)
subwf BRequest,W
btfsc Zero
goto GetStatusTask
movlw GetInterface ;(0ah)
subwf BRequest,W
btfsc Zero
goto GetInterfaceTask
movlw SetInterface ;(0bh)
subwf BRequest,W
btfsc Zero
goto SetInterfaceTask
movlw ClearFeature ;(01h)
subwf BRequest,W
btfsc Zero
goto ClearFeatureTask
movlw SetFeature ;(03h)
subwf BRequest,W
btfsc Zero
goto SetFeatureTask
movlw GetDescriptor ;(06h)
subwf BRequest,W
btfsc Zero
goto GetDescriptorTask
goto Rc0TaskError
Ep0Out:
movfw Rc0Cnt
movwf AnyCountTemp
movlw 0fh
andwf AnyCountTemp,F
testz AnyCountTemp
btfsc Zero
goto NoDataControlWrite
ControlWrite:
bsf host_send_data
movfw Rc0Fifo0
call UsbLedTable1
movwf UsbLedDataTemp
movwf usbkeyled
comf UsbLedDataTemp,W
movwf Led
NoDataControlWrite:
call NoDataControl
ret
UsbLedTable1:
addwf Pc,F
retlw 00h
retlw 02h
retlw 01h
retlw 03h
retlw 04h
retlw 06h
retlw 05h
retlw 07h
Rc0TaskError:
bsf Ep0Stall
ret
;==============================================================================
;Function:HidSetRequestTask(21,09)(21,0a)(21,0b)
; There are three Hid reports to be set:
; 1.Set Report is used to control the LEDs in the keyboard. An OUT packet
; will be received that will have a bit set for every LED that should be
; turned on.
; 2.Set Idle silences a particular report on the interrupt pipe until a new
; event occurs or the specified amount of time (wValueHi) passes.
; The resolution of the specified duration is 4ms (i.e the weight of the
; LSB is 4ms)
; 3.Set Protocol switches between the boot protocol and the report protocol.
; For boot protocol, wValue=0. For report protocol, wValue=1.
;==============================================================================
HidSetRequestTask:
movlw SetReport
subwf BRequest,W
btfsc Zero
goto HidSetReport
movlw SetIdle
subwf BRequest,W
btfsc Zero
goto HidSetIdle
movlw SetProtocol
subwf BRequest,W
btfss Zero
goto HidSetRequestTaskError
HidSetProtocol:
movfw WValue
movwf HidProtocolStatus
goto HidSetRequestTaskSend
HidSetIdle:
movfw WValueHi
movwf IdlePeriodStatus
movwf IdlePeriodTemp
goto HidSetRequestTaskSend
HidSetReport:
testz WIndex ;WIndex = Interface1 ?
btfss Zero
goto HidSetRequestTaskError
movlw LedValue
subwf WValueHi,W
btfss Zero
goto HidSetRequestTaskError
testz ConfigurationStatus ;ConfigurationStatus = UnConfig ?
btfsc Zero
goto HidSetRequestTaskError
HidSetRequestTaskSend:
call NoDataControl
ret
HidSetRequestTaskError:
bsf Ep0Stall
ret
;==============================================================================
;Function:SetAddressTask(00,05)
; Set the device address to the wValue in the SETUP packet at
; the completion of the current transaction.
;==============================================================================
SetAddressTask:
bsf NewAdrFlag
call NoDataControl
ret
;==============================================================================
;Function:SetConfigurationTask(00,09)
; 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.
;==============================================================================
SetConfigurationTask:
testz BmRequestType ;BmRequestType = HostToDevice ?
btfss Zero
goto SetConfigurationTaskError
SetConfigurationType:
testz WValue ;WValue = Config ?
btfss Zero
goto ConfigType
UnConfigType:
movfw WValue
movwf ConfigurationStatus
movlw cch
movwr IntEn0
bcf Ep1Cfg
call NoDataControl
ret
ConfigType:
movfw WValue
movwf ConfigurationStatus
movlw DefaultIdlePeriod
movwf IdlePeriodStatus
movwf IdlePeriodTemp
movlw fch
movwr IntEn0
bsf Ep1Cfg
bsf Tx1Tgl
clrf Timer0
call NoDataControl
ret
SetConfigurationTaskError:
bsf Ep0Stall
ret
;==============================================================================
;Function:GetDescriptorTask(80,06)(81,06)(82,06)
; 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.
; But keyboard is a HID device, so we have to support HID class and
; HID report two types.
;==============================================================================
GetDescriptorTask:
movlw DeviceToHost
subwf BmRequestType,W
btfsc Zero
goto GetDescriptorType
movlw InterfaceToHost
subwf BmRequestType,W
btfsc Zero
goto GetDescriptorType
movlw EndpointToHost
subwf BmRequestType,W
btfss Zero
goto GetDescriptorTaskError
GetDescriptorType:
movlw Device
subwf WValueHi,W
btfsc Zero
goto DeviceType
movlw Configuration
subwf WValueHi,W
btfsc Zero
goto ConfigurationType
movlw String
subwf WValueHi,W
btfsc Zero
goto StringType
movlw HidClass
subwf WValueHi,W
btfsc Zero
goto HidClassType
movlw HidReport
subwf WValueHi,W
btfsc Zero
goto HidReportType
goto GetDescriptorTaskError
DeviceType:
clrf TableCnt
movlw DeviceLen
movwf DataLength
goto GetDescriptorTaskSend
ConfigurationType:
movlw 12h
movwf TableCnt
movlw ConfigLen
movwf DataLength
goto GetDescriptorTaskSend
StringType:
movlw String0
subwf WValue,W
btfsc Zero
goto SendString0
movlw String2
subwf WValue,W
btfsc Zero
goto SendString2
goto GetDescriptorTaskError
SendString0:
movlw 4dh
movwf TableCnt
movlw String0Len
movwf DataLength
goto GetDescriptorTaskSend
SendString2:
movlw 51h
movwf TableCnt
movlw String2Len
movwf DataLength
goto GetDescriptorTaskSend
HidClassType:
movlw 24h
testz WIndex
btfss Zero
movlw 3dh
movwf TableCnt
movlw HidClassLen
movwf DataLength
goto GetDescriptorTaskSend
HidReportType:
movlw Report1
subwf WIndex,W
btfsc Zero
goto SendReport1
; movlw Report2
; subwf WIndex,W
; btfsc Zero
; goto SendReport2
goto GetDescriptorTaskError
SendReport1:
clrf TableCnt
movlw HidReport1Len
movwf DataLength
bsf TableSelBit
goto GetDescriptorTaskSend
; SendReport2:
; movlw 41h
; movwf TableCnt
; movlw HidReport2Len
; movwf DataLength
; bsf TableSelBit
GetDescriptorTaskSend:
call CheckLength
call Tx0Transfer
ret
GetDescriptorTaskError:
bsf Ep0Stall
ret
;==============================================================================
;Function:GetConfigurationTask(80,08)
; Return the current device configuration to the host. The possible
; values are zero (unconfigured) and one (configured).
;==============================================================================
GetConfigurationTask:
movlw DeviceToHost
subwf BmRequestType,W
btfss Zero
goto GetConfigurationTaskError
GetConfiguration:
movfw ConfigurationStatus
movwr Tx0Fifo0
movlw c1h
movwf Tx0Reg
; bsf Rc0Rdy
ret
GetConfigurationTaskError:
bsf Ep0Stall
ret
;==============================================================================
;Function:ClearFeatureTask(00,01)(02,01)
; There are two kinds of situation to be clear:
; 1.These routines allow the host to disable the ability to request
; remote wakeup.
; 2.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.
;==============================================================================
ClearFeatureTask:
movlw HostToDevice
subwf BmRequestType,W
btfsc Zero
goto ClearRemoteWakeup
movlw HostToEndpoint
subwf BmRequestType,W
btfss Zero
goto ClearFeatureTaskError
ClearEndpointStall:
testz ConfigurationStatus ;ConfigurationStatus = Config ?
btfsc Zero
goto ClearFeatureTaskError
CheckClrEndpointStall:
movlw Endpoint1
subwf WIndex,W
btfsc Zero
goto ClrEp1Stall
; movlw Endpoint2
; subwf WIndex,W
; btfss Zero
goto ClearFeatureTaskError
; ClrEp2Stall:
; clrf Endpoint2Status
; bcf Ep2Stall
; goto ClearFeatureTaskSend
ClrEp1Stall:
clrf Endpoint1Status
bcf Ep1Stall
goto ClearFeatureTaskSend
ClearRemoteWakeup:
movlw Device
subwf WValue,W ;This byte must be 01h
btfss Zero
goto ClearFeatureTaskError
movlw DisableRemoteWakeup
movwf RemoteWakeupStatus
ClearFeatureTaskSend:
call NoDataControl
ret
ClearFeatureTaskError:
bsf Ep0Stall
ret
;==============================================================================
;Function:SetFeatureTask(00,03)(02,03)
; There are two kinds of situation to be set:
; 1.These routines allow the host to enable the ability to request
; remote wakeup.
; 2.Set the endpoint stall feature for the selected endpoint.
;==============================================================================
SetFeatureTask:
movlw HostToDevice
subwf BmRequestType,W
btfsc Zero
goto SetRemoteWakeup
movlw HostToEndpoint
subwf BmRequestType,W
btfss Zero
goto SetFeatureTaskError
SetEndpointStall:
testz ConfigurationStatus ;ConfigurationStatus = Config ?
btfsc Zero
goto SetFeatureTaskError
CheckSetEndpointStall:
movlw Endpoint1
subwf WIndex,W
btfsc Zero
goto SetEp1Stall
; movlw Endpoint2
; subwf WIndex,W
; btfss Zero
goto SetFeatureTaskError
; SetEp2Stall:
; movlw StallValue
; movwf Endpoint2Status
; bsf Ep2Stall
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -