📄 3100_2interface.asm
字号:
bcf Tx0Rdy
bsf Tx0Flag
goto EndInt
;==============================================================================
;Function:Tx1iTask
; 1.Host receive data then send Tx1i back to MCU
;==============================================================================
Tx1iTask:
bcf Tx1i
goto EndInt
;==============================================================================
;Function:Tx2iTask
; 1.Host receive data then send Tx2i back to MCU
;==============================================================================
Tx2iTask:
bcf Tx2i
goto EndInt
;==============================================================================
;Function:RstiTask
; 1.Host request bus reset
;==============================================================================
RstiTask:
bcf Rsti
call UsbReset
bsf Rc0Rdy
goto EndInt
;==============================================================================
;Function:SuspiTask
; 1.Host want keyboard into power down mode
;==============================================================================
SuspiTask:
bcf Suspi
bsf SuspendFlag
goto EndInt
;==============================================================================
;Function:RsmiTask
; 1.Host wake keyboard up
;==============================================================================
RsmiTask:
bcf Rsmi
bcf Suspend
bcf SuspendFlag
movlw ech
movwr IntEn0
movlw 00h
movwr IntEn1
bsf StartScanFlag
clrf Timer0
bsf Rc0Rdy
goto EndInt
;==============================================================================
;Function:KbdiTask
; 1.Keyboard wake up by itself. (any keys)
;==============================================================================
KbdiTask:
bcf Kbdi
goto EndInt
;==============================================================================
;Function:Pb0iTask
; 1.Keyboard wake up only by Pb0 io port.
;==============================================================================
Pb0iTask:
bcf Pb0i
;==============================================================================
;Function:EndInt
; Recover regisiter Status.
;==============================================================================
EndInt:
movfw StateTemp1
movwf Psw
movfw StateTemp0
reti
;==================================================================================================
org 0100h ;
;==================================================================================================
Start:
call SystemReset
UsbStart:
call Ep0IntCheck
btfsc SuspendFlag
call SuspendTask
btfss StartScanFlag
goto UsbStart
call DataInTask
call ReportTask
goto UsbStart
;==============================================================================
;Function:SystemReset
; 1.Disable all Interrupts Flag
; 2.Clear all ram buffers in MCU
; 3.Turn off leds
;==============================================================================
SystemReset:
clrf Gpr0
clrf Gpr1
clrf UsbFun
clrf Int0
clrf Int1
clrf Int2
clrf UsbCtr
clrf Tx0Reg
clrf Tx1Reg
clrf Tx2Reg
movlw 00h
movwr Tm0Reload
movwr Tm0Pscl
movwr IntEn0
movwr IntEn1
bsf RamBank
ClearRam0:
movlw 20h
movwf Fsr
ClearLoop0:
movlw 00h
movwf Indf
incf Fsr,F
movlw 80h
subwf Fsr,W
btfss Zero
goto ClearLoop0
bcf RamBank
ClearRam1:
movlw 20h
movwf Fsr
ClearLoop1:
movlw 00h
movwf Indf
incf Fsr,F
movlw 80h
subwf Fsr,W
btfss Zero
goto ClearLoop1
Usb:
bsf UsbEn ;Usbe=1
movlw cch
movwr IntEn0
ret
;==============================================================================
;Function:UsbReset
; 1.Enable USBEn flag
; 2.Enable Rc0int,Tx0int,Rstint,Suspint,Timer0int flags
; 3.Enable Tx1,Tx2 toggle
; 4.Enable Timer0
;==============================================================================
UsbReset:
clrf Gpr0
clrf Gpr1
clrf UsbFun
clrf Int0
clrf Int1
clrf Int2
clrf UsbCtr
clrf Tx0Reg
clrf Tx1Reg
clrf Tx2Reg
bsf UsbEn ;Usbe=1
movlw cch
movwr IntEn0
movlw 00h
movwr IntEn1
movlw 07h
movwr Tm0Pscl
ret
;==============================================================================
;Function:Ep0IntCheck
; 1.Check two kinds of interrupts:Tx0i and Rc0i
;==============================================================================
Ep0IntCheck:
btfsc Rc0Flag
goto Rc0Task
btfsc Tx0Flag
goto Tx0Task
ret
;==============================================================================
;Function:SuspendTask
; 1.Check two kinds of interrupts:Tx0i and Rc0i
;==============================================================================
SuspendTask:
movlw 00h
movwr IntEn0 ;Disable all interrupts
movlw 08h
movwr IntEn1 ;Only Enable Rsmie
bsf Suspend
nop
nop
nop
; movlw 00h
; movwr PwrDown ;Save the power consumption of MCU
sleep
nop
ret
;==============================================================================
;Function:CheckLength
; 1.Check how many data want to send
;==============================================================================
CheckLength:
movfw DataLength
movwf WLengthTemp
testz WLengthHi
btfss Zero
ret
CheckLenTask:
bcf Carry
movfw DataLength
subwf WLength,W
btfsc Carry
ret
movfw WLength
movwf WLengthTemp
ret
;==============================================================================
;Function:Tx0Transfer
; 1.Transfer data to host
;==============================================================================
Tx0Transfer:
clrf LengthCnt
testz WLengthTemp
btfsc Zero
goto SetTgl
movfw TableCnt
call DeviceInfoTable
btfss TableSelBit
goto Sendbyte0
movfw TableCnt
call DeviceReportTable
Sendbyte0:
movwr Tx0Fifo0
incf TableCnt,F
incf LengthCnt,F
decf WLengthTemp,F
testz WLengthTemp
btfsc Zero
goto SetTgl
movfw TableCnt
call DeviceInfoTable
btfss TableSelBit
goto Sendbyte1
movfw TableCnt
call DeviceReportTable
Sendbyte1:
movwr Tx0Fifo1
incf TableCnt,F
incf LengthCnt,F
decf WLengthTemp,F
testz WLengthTemp
btfsc Zero
goto SetTgl
movfw TableCnt
call DeviceInfoTable
btfss TableSelBit
goto Sendbyte2
movfw TableCnt
call DeviceReportTable
Sendbyte2:
movwr Tx0Fifo2
incf TableCnt,F
incf LengthCnt,F
decf WLengthTemp,F
testz WLengthTemp
btfsc Zero
goto SetTgl
movfw TableCnt
call DeviceInfoTable
btfss TableSelBit
goto Sendbyte3
movfw TableCnt
call DeviceReportTable
Sendbyte3:
movwr Tx0Fifo3
incf TableCnt,F
incf LengthCnt,F
decf WLengthTemp,F
testz WLengthTemp
btfsc Zero
goto SetTgl
movfw TableCnt
call DeviceInfoTable
btfss TableSelBit
goto Sendbyte4
movfw TableCnt
call DeviceReportTable
Sendbyte4:
movwr Tx0Fifo4
incf TableCnt,F
incf LengthCnt,F
decf WLengthTemp,F
testz WLengthTemp
btfsc Zero
goto SetTgl
movfw TableCnt
call DeviceInfoTable
btfss TableSelBit
goto Sendbyte5
movfw TableCnt
call DeviceReportTable
Sendbyte5:
movwr Tx0Fifo5
incf TableCnt,F
incf LengthCnt,F
decf WLengthTemp,F
testz WLengthTemp
btfsc Zero
goto SetTgl
movfw TableCnt
call DeviceInfoTable
btfss TableSelBit
goto Sendbyte6
movfw TableCnt
call DeviceReportTable
Sendbyte6:
movwr Tx0Fifo6
incf TableCnt,F
incf LengthCnt,F
decf WLengthTemp,F
testz WLengthTemp
btfsc Zero
goto SetTgl
movfw TableCnt
call DeviceInfoTable
btfss TableSelBit
goto Sendbyte7
movfw TableCnt
call DeviceReportTable
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
movlw 40h
xorwf Tx0Reg,F
movlw f0h
andwf Tx0Reg,F
bsf Tx0Rdy
bsf Rc0Rdy
ret
NormalTrans:
call Tx0Transfer
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:
movfw Rc0Fifo0
movwf InOutBuffer0
movfw Rc0Fifo1
movwf InOutBuffer1
movfw Rc0Fifo2
movwf InOutBuffer2
movfw Rc0Fifo3
movwf InOutBuffer3
movfw Rc0Fifo4
movwf InOutBuffer4
movfw Rc0Fifo5
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -