⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbfs_1_drv.asm

📁 cypresscy74294ic键盘和鼠标原码
💻 ASM
📖 第 1 页 / 共 3 页
字号:
    JMP     USBFS_1_NoDataControlError       ; USB_TRANS_STATE_NO_DATA_CONTROL


;-----------------------------------------------------------------------------
;  FUNCTION NAME: USBFS_1_InitControlRead
;
;  DESCRIPTION:   This routine initializes a control read.  It must be JUMPed to,
;                 not called.  It assumes a tranfer descriptor has been loaded
;                 into the driver USBFS_1_CurrentTD data structure.
;
;-----------------------------------------------------------------------------
 USBFS_1_InitControlRead:
_USBFS_1_InitControlRead:
    MOV     [USBFS_1_LastSize], A      ; Save the packet size?
    CALL    USBFS_1_InitializeStatusBlock
    MOV     [USBFS_1_TransferType], USB_TRANS_STATE_CONTROL_READ

    ; Check the transfer size against the request size
    MOV    A, REG[USBFS_1_EP0DATA+wLengthHi] ; MSB of wLength
    CMP    A, [USBFS_1_TransferSize]
    JNZ    .L1

    MOV    A, REG[USBFS_1_EP0DATA+wLengthLo] ; LSB of wLength
    CMP    A, [USBFS_1_TransferSize+1]
    JZ     .L9
.L1:
    JNC    .L9
;
    MOV    [USBFS_1_TransferSize+1], A ;
    MOV    A, REG[USBFS_1_EP0DATA+wLengthHi] ;
    MOV    [USBFS_1_TransferSize], A   ;
.L9:
    MOV    [USBFS_1_TransferByteCount], 0 ;
    MOV    [USBFS_1_TransferByteCount+1], 0 ;

    OR     [USBFS_1_EP0DataToggle], 1 ; setup EP0 data toggle
    JMP    USBFS_1_LoadEndpoint        ;
;-----------------------------------------------------------------------------
;  FUNCTION NAME: USBFS_1_ControlReadDataStage
;
;  DESCRIPTION:   This routine processes the data stage of a control read.  It
;                 must be JUMPed to, not called.  It assumes a tranfer descriptor
;                 has been loaded into the driver USBFS_1_CurrentTD
;                 data structure.
;
;-----------------------------------------------------------------------------
 USBFS_1_ControlReadDataStage:
    JMP     USBFS_1_LoadEndpoint

;-----------------------------------------------------------------------------
;  FUNCTION NAME: USBFS_1_ControlReadStatusStage
;
;  DESCRIPTION:   This routine processes the status stage of a control read.  It
;                 must be JUMPed to, not called.  It handles short or 0 packet
;                 It assumes a tranfer descriptor has been loaded into the
;                 driver USBFS_1_CurrentTD data structure.
;
;-----------------------------------------------------------------------------
 USBFS_1_ControlReadStatusStage:
    MOV    A, [USBFS_1_LastSize]       ; Get the number of bytes from the last transfer
    ADD    [USBFS_1_TransferByteCount + 1], A ; Update the transfer byte count
    ADC    [USBFS_1_TransferByteCount], 0 ;
    MOV    A, USB_XFER_STATUS_ACK      ; Return a Status ACK Completion
    CALL   USBFS_1_UpdateStatusBlock
    MOV    [USBFS_1_TransferType], USB_TRANS_STATE_IDLE
    MOV    A, 0                        ; Count Register
    MOV    X, USB_MODE_STALL_IN_OUT
    JMP    USBFS_1_EP0_UPD_MODE_EXIT
;-----------------------------------------------------------------------------
;  FUNCTION NAME: USBFS_1_NoDataStageControlTransfer
;                 USBFS_1_InitNoDataStageControlTransfer
;
;  DESCRIPTION:   This routine processes the status stage of a no data control
;                 write.  It must be JUMPed to, not called.
;
;-----------------------------------------------------------------------------
 USBFS_1_NoDataStageControlTransfer:
_USBFS_1_InitNoDataStageControlTransfer:
 USBFS_1_InitNoDataStageControlTransfer:
    CALL    USBFS_1_InitializeStatusBlock

    MOV    [USBFS_1_TransferType], USB_TRANS_STATE_NO_DATA_CONTROL

    MOV    A, 0                        ; Count Register
    MOV    X, USB_MODE_STATUS_IN_ONLY
    JMP    USBFS_1_EP0_UPD_MODE_EXIT


;-----------------------------------------------------------------------------
;  FUNCTION NAME: USBFS_1_InitControlWrite
;
;  DESCRIPTION:   This routine initializes control write.  It must be JUMPed
;                 to, not called.  It assumes a tranfer descriptor has been loaded
;                 into the driver USBFS_1_CurrentTD data structure.
;
;-----------------------------------------------------------------------------
 USBFS_1_InitControlWrite:
_USBFS_1_InitControlWrite:
    MOV     A, [USBFS_1_DataSource]    ; Need to make sure the destination is not ROM
    CMP     A, USB_DS_ROM
    JZ      USBFS_1_Not_Supported_Local_Drv

    CALL    USBFS_1_InitializeStatusBlock

    MOV    [USBFS_1_TransferType], USB_TRANS_STATE_CONTROL_WRITE

    OR     [USBFS_1_EP0DataToggle], 1 ; setup EP0 data toggle

    MOV    A, 0                        ; Count Register
    MOV    X, USB_MODE_ACK_OUT_STATUS_IN
    JMP    USBFS_1_EP0_UPD_MODE_EXIT


;-----------------------------------------------------------------------------
;  FUNCTION NAME: USBFS_1_ControlWriteDataStage
;
;  DESCRIPTION:   This routine processes the data stage of a control
;                 write.  It must be JUMPed to, not called.  It assumes a
;                 tranfer descriptor has been loaded into the driver
;                 USBFS_1_CurrentTD data structure.
;
;-----------------------------------------------------------------------------
 USBFS_1_ControlWriteDataStage:
    XOR    [USBFS_1_EP0DataToggle], 1  ; Update data toggle

    MOV    A,REG[USBFS_1_EP0CNT]       ; Get the count

    AND     A, 0x0F
    SUB     A, 2                       ; Count include the two byte checksum

    MOV     [USBFS_1_t2], A            ; Assume we have room to receive the whole packet

    MOV     A, 0
    CMP     A, [USBFS_1_TransferSize]  ; If the MSB has anything just use the count
    JNZ     .L1

    MOV     A, [USBFS_1_t2]
    CMP     A, [USBFS_1_TransferSize+1]  ;
    JZ      .L6
.L1:
    JC      .L6
    MOV     [USBFS_1_t2], [USBFS_1_TransferSize+1]

.L6:
    MOV     A, [USBFS_1_t2]
    SUB     [USBFS_1_TransferSize+1],A   ; Update the bytes remaining
    SBB     [USBFS_1_TransferSize], 0  ;

    ADD     [USBFS_1_TransferByteCount + 1], A ; Update the transfer byte count
    ADC     [USBFS_1_TransferByteCount], 0 ;

    MOV     X,0                        ; Start the index at 0

.RAM_COPY:
    MOV     A, REG[X+USBFS_1_EP0DATA]  ; Get the data
    MVI     [USBFS_1_DataPtr+1], A     ; Store the data, bump the destination

    INC     X                          ; Bump the destination offset
    MOV     A,X                        ; Are we done?
    CMP     A, [USBFS_1_t2]
    JC      .RAM_COPY                  ; Not done

    MOV    A, 0                        ; Count Register
    MOV    X, USB_MODE_ACK_OUT_STATUS_IN
    JMP    USBFS_1_EP0_UPD_MODE_EXIT

; Jump here on data toggle error
.error:
    MOV    [USBFS_1_TransferType], USB_TRANS_STATE_IDLE  ; This simply aborts the transfer
    MOV    A, 0                        ; Count Register
    MOV    X, USB_MODE_STALL_IN_OUT
    JMP    USBFS_1_EP0_UPD_MODE_EXIT


;-----------------------------------------------------------------------------
;  FUNCTION NAME: USBFS_1_ControlWriteStatusStage
;
;  DESCRIPTION:   This routine processes the status stage of a control
;                 write.  It must be JUMPed to, not called.  It assumes a
;                 tranfer descriptor has been loaded into the driver
;                 USBFS_1_CurrentTD data structure.
;
;-----------------------------------------------------------------------------
 USBFS_1_ControlWriteStatusStage:
    MOV    A, USB_XFER_STATUS_ACK      ; Return a Status ACK Completion
    CALL   USBFS_1_UpdateStatusBlock
    MOV    [USBFS_1_TransferType], USB_TRANS_STATE_IDLE  ; The packet is done
    MOV    A, 0                        ; Count Register
    MOV    X, USB_MODE_STALL_IN_OUT
    JMP    USBFS_1_EP0_UPD_MODE_EXIT


;-----------------------------------------------------------------------------
;  FUNCTION NAME: USBFS_1_NoDataControlStatusStage
;
;  DESCRIPTION:   This routine processes the status stage of a control
;                 write.  It must be JUMPed to, not called.  It assumes a
;                 tranfer descriptor has been loaded into the driver
;                 USBFS_1_CurrentTD data structure.
;
;                 USB Device Addressing happens here because we can't change
;                 the SIE Address before the Status IN is received.
;
;-----------------------------------------------------------------------------
 USBFS_1_NoDataControlStatusStage:
    MOV     A, USB_XFER_STATUS_ACK     ; Return a Status ACK Completion
    CALL    USBFS_1_UpdateStatusBlock
    ; Dispatch to the proper handler
    CMP     [USBFS_1_fDataPending], USB_ADDRESS_CHANGE_PENDING
    JNZ     .L1

    ; USB ADDRESS CHANGE
    MOV     A, [USBFS_1_TransferBuffer]  ; Get the pending data

    OR      A, USB_ADDR_ENABLE         ; Set the enable bit
    MOV     REG[USBFS_1_ADDR], A       ; Update the SIE address
    JMP     .EXIT
.L1:

.EXIT:
    MOV     [USBFS_1_fDataPending], 0  ; Clear data pending

    MOV    A, 0                        ; Count Register
    MOV    X, USB_MODE_STATUS_IN_ONLY  ; Wait for the next SETUP
    JMP    USBFS_1_EP0_UPD_MODE_EXIT


;-----------------------------------------------------------------------------
;  FUNCTION NAME: USBFS_1_NoDataControlError
;
;  DESCRIPTION:   This routine handles the condition when we expected a
;                 status IN, but receive an OUT
;
;-----------------------------------------------------------------------------
 USBFS_1_NoDataControlError:
    MOV    A, USB_XFER_ERROR           ; Return Transaction Error
    CALL   USBFS_1_UpdateStatusBlock
    MOV    A, 0                        ; Count Register
    MOV    X, USB_MODE_STALL_IN_OUT    ; Set the mode register
    JMP    USBFS_1_EP0_UPD_MODE_EXIT


;-----------------------------------------------------------------------------
;  FUNCTION NAME: USBFS_1_LoadEndpoint
;
;  DESCRIPTION: Moves data from either RAM OR ROM - depending on the request
;               type AND then places the appropriate number of bytes -
;               depending on the request size into the endpoint FIFO.
;               It finally sets up the endpoint to send data.
;
USBFS_1_LoadEndpoint:
    MOV     A, [USBFS_1_LastSize]      ; Get the number of bytes from the last transfer
    ADD     [USBFS_1_TransferByteCount + 1], A ; Update the transfer byte count
    ADC     [USBFS_1_TransferByteCount], 0 ;
    MOV     A,  [USBFS_1_TransferSize] ; Check to see if we have any
    OR      A,  [USBFS_1_TransferSize+1] ;  more data to send
    JNZ     .cont                      ; Jump if we have to send more data

; Flow here if there is no more data to send
;    CMP     [USBFS_1_LastSize], 8     ; Was it a full packet?
    JZ      .START_TRANSFER            ; Jump if it was full (need to send a zero length)

; Flow here if we are entering the status stage
    MOV    A, 0                        ; Count Register
    MOV    [USBFS_1_LastSize], A       ; Clear the byte count
    MOV    X, USB_MODE_STATUS_OUT_ONLY ; Only ACK the Status Out
    JMP    USBFS_1_EP0_UPD_MODE_EXIT

; Jump here to determine how many bytes should we transfer
.cont:
    CMP     [USBFS_1_TransferSize], 0  ; Check the MSB
    JNZ     .L1

    CMP     [USBFS_1_TransferSize+1], 8  ; Check the LSB
    JNC     .L1

    MOV     A,[USBFS_1_TransferSize+1]   ; Transfer all the remaing data
    JMP     .L3

.L1:
    MOV     A, 8                       ; Just transfer the next 8 bytes

.L3:

    SUB     [USBFS_1_TransferSize+1],A   ; Update the bytes remaining
    SBB     [USBFS_1_TransferSize],0

    MOV     [USBFS_1_t2],A             ; Save the count
    MOV     X,0

    CMP     [USBFS_1_DataSource],USB_DS_ROM  ; RAM or ROM copy?
    JNZ     .RAM_COPY

; Copy data from a ROM source
.ROM_COPY:
    PUSH    X                          ; Save the destination offset
    MOV     A,[USBFS_1_DataPtr]        ; Get the transfer source MSB
    MOV     X,[USBFS_1_DataPtr+1]      ; Set the transfer source LSB
    INC     [USBFS_1_DataPtr+1]        ; Increment the data pointer
    ADC     [USBFS_1_DataPtr], 0       ;   MSB if necessary

    ROMX                               ; Get the data byte

    POP     X                          ; Get the destination offset
    MOV     REG[X + USBFS_1_EP0DATA], A; Load the data
    INC     X                          ; Bump the destination offset
    MOV     A,X                        ; Are we done?
    CMP     A, [USBFS_1_t2]
    JC      .ROM_COPY                  ; Not done
    JMP     .START_TRANSFER            ; Otherwise go start the transfer

; Copy data from a RAM source
.RAM_COPY:
    MVI     A, [USBFS_1_DataPtr+1]     ; Get the data, bump the source

    MOV     REG[X +USBFS_1_EP0DATA], A ; Load the data
    INC     X                          ; Bump the destination offset
    MOV     A,X                        ; Are we done?
    CMP     A, [USBFS_1_t2]
    JC      .RAM_COPY                  ; Not done

;; Set up the IN transfer count/mode/etc
;    A contains the byte count
.START_TRANSFER:
    MOV     [USBFS_1_LastSize], A      ; Save the packet size

    MOV     A, 1
    AND     A, [USBFS_1_EP0DataToggle]
    JZ      .BYPASS_T1
    MOV     A, USB_CNT_TOGGLE          ; Or T1 in the data toggle

.BYPASS_T1:
    OR      A, [USBFS_1_LastSize]
    XOR     [USBFS_1_EP0DataToggle], 1    ; Update the data toggle for next time

    MOV    X, USB_MODE_ACK_IN_STATUS_OUT  ; Set the mode register
    JMP    USBFS_1_EP0_UPD_MODE_EXIT

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -