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

📄 usb_drv.asm

📁 Cypress cy7c63318 鼠标开发板的源代码
💻 ASM
📖 第 1 页 / 共 3 页
字号:
;;*****************************************************************************
;;*****************************************************************************
;;  FILENAME: USB_drv.asm
;;   Version: 1.5.0.1, Updated on 2005/08/17 at 15:01:28
;;  Generated by PSoC Designer ver 4.2  b1013 : 02 September, 2004
;;
;;  DESCRIPTION: USB Device User Module control endpoint driver
;;               for the enCoRe II family of devices
;;
;;  NOTE: User Module APIs conform to the fastcall convention for marshalling
;;        arguments and observe the associated "Registers are volatile" policy.
;;        This means it is the caller's responsibility to preserve any values
;;        in the X and A registers that are still needed after the API
;;        function returns. Even though these registers may be preserved now,
;;        there is no guarantee they will be preserved in future releases.
;;-----------------------------------------------------------------------------
;;  Copyright (c) Cypress Semiconductor 2004. All Rights Reserved.
;;*****************************************************************************
;;*****************************************************************************

include "m8c.inc"
include "USB_macros.inc"
include "USB.inc"

;-----------------------------------------------
;  Global Symbols
;-----------------------------------------------
export  _USB_EP0_ISR
export  USB_InitControlRead
export  USB_InitControlWrite
export  USB_InitNoDataStageControlTransfer
export  USB_NoDataStageControlTransfer

;-----------------------------------------------
;  Macro Definitions
;-----------------------------------------------

;-----------------------------------------------
;  Constant Definitions
;-----------------------------------------------

;-----------------------------------------------
; Variable Allocation
;-----------------------------------------------
AREA bss (RAM,REL)
;----------------------------------------------------------------------------
; Current Device
;----------------------------------------------------------------------------
EXPORT USB_bCurrentDevice
USB_bCurrentDevice:                     BLK   1    ;  Current Device
;----------------------------------------------------------------------------
; Current Configuration
;----------------------------------------------------------------------------
EXPORT USB_Configuration, _USB_Configuration
_USB_Configuration:
 USB_Configuration:                     BLK   1    ;  Current Configuration
;----------------------------------------------------------------------------
; Current Device Status
;----------------------------------------------------------------------------
EXPORT USB_DeviceStatus
 USB_DeviceStatus:                      BLK   1    ;  Current Device Status
;----------------------------------------------------------------------------
; Interface Setting
;----------------------------------------------------------------------------
EXPORT USB_InterfaceSetting
 USB_InterfaceSetting:                  BLK   1    ; Interface Setting
;----------------------------------------------------------------------------
; Endpoint Status--USB Status
;----------------------------------------------------------------------------
EXPORT USB_EndpointStatus
 USB_EndpointStatus:                    BLK   USB_NUM_ENDPOINTS    ; Endpoint Status
;----------------------------------------------------------------------------
; Last Packet Size
;----------------------------------------------------------------------------
EXPORT USB_LastSize
 USB_LastSize:                          BLK   1    ;  Last Packet Size
;----------------------------------------------------------------------------
; Control Transfer State Machine
; State values for Control Write
; State values for Control Read
;----------------------------------------------------------------------------
EXPORT USB_TransferType
 USB_TransferType:                      BLK   1    ; Control Transfer State Machine
;----------------------------------------------------------------------------
; Control Transfer Intermediate Buffer--Shared among the requests
;----------------------------------------------------------------------------
EXPORT USB_TransferBuffer
 USB_TransferBuffer:                    BLK   8
;----------------------------------------------------------------------------
; Transfer Descriptor Data for Control Transfer
;  --The following data have the same format as the first 5 bytes of the TD_ENTRY
;----------------------------------------------------------------------------
; Control Transfer Data Source
;   USB_DS_ROM
;   USB_DS_RAM
;   USB_DS_RAM_AS_NEEDED
;----------------------------------------------------------------------------
EXPORT USB_CurrentTD, _USB_CurrentTD
 USB_CurrentTD:
_USB_CurrentTD:
EXPORT USB_DataSource, _USB_DataSource
 USB_DataSource:
_USB_DataSource:                       BLK   1
;----------------------------------------------------------------------------
; Control Transfer Data Size
;----------------------------------------------------------------------------
EXPORT USB_TransferSize, _USB_TransferSize
 USB_TransferSize:
 _USB_TransferSize:                    BLK   2
;----------------------------------------------------------------------------
; Control Transfer Data Pointer
;   Source for Control Read
;   Destination for Control Write
;----------------------------------------------------------------------------
EXPORT USB_DataPtr, _USB_DataPtr
 USB_DataPtr:
 _USB_DataPtr:                         BLK   2
;----------------------------------------------------------------------------
; Transfer Completion Notification
;----------------------------------------------------------------------------
EXPORT USB_StatusBlockPtr, _USB_StatusBlockPtr
 USB_StatusBlockPtr:
 _USB_StatusBlockPtr:                  BLK   2
;----------------------------------------------------------------------------
; Control Transfer _TransferByteCount (Actually transfered)
;----------------------------------------------------------------------------
 USB_TransferByteCount:                 BLK   2
;----------------------------------------------------------------------------
; Control Endpoint Data toggle
EXPORT USB_EPDataToggle
 USB_EPDataToggle:
 USB_EP0DataToggle:                     BLK   1
;----------------------------------------------------------------------------
; Control Endpoint Data Pending Flag
EXPORT USB_fDataPending
 USB_fDataPending:                      BLK   1
;----------------------------------------------------------------------------
; Control Endpoint Data Pending Flag
;EXPORT USB_PendingData
; USB_PendingData:                      BLK   1
;----------------------------------------------------------------------------
; Temporary Data registers
EXPORT USB_t2, USB_t1, USB_t0
 USB_t2:                                BLK   1    ; Temporary shared by the UM
 USB_t1:                                BLK   1    ; Temporary shared by the UM
 USB_t0:                                BLK   1    ; Temporary shared by the UM
;----------------------------------------------------------------------------
; Endpoint Transfer--API Status
;----------------------------------------------------------------------------
EXPORT USB_EndpointAPIStatus, _USB_EndpointAPIStatus
 USB_EndpointAPIStatus:
_USB_EndpointAPIStatus:                 BLK   USB_NUM_ENDPOINTS    ; Endpoint Status

AREA UserModules (ROM, REL)
;-----------------------------------------------------------------------------
;  FUNCTION NAME: USB_EP0_ISR

;
;  DESCRIPTION:   The EPO ISR serves the control endpoint interrupts and
;                 dispaches all SETUP, IN, and OUT transfers to the proper
;                 dispatch routines for all supported USB requests.
;
;-----------------------------------------------------------------------------
;
;  ARGUMENTS:   n/a
;
;  RETURNS:     n/a
;
;  SIDE EFFECTS: REGISTERS ARE VOLATILE: THE A AND X REGISTERS MAY BE MODIFIED!
;
;  THEORY of OPERATION or PROCEDURE:
;
;-----------------------------------------------------------------------------
 USB_EP0_ISR:
_USB_EP0_ISR:
    PUSH    A                          ; Save the context
    PUSH    X                          ; 
    ; Dispatch to setup/in/out handlers
EXPORT  USB_mode_read
USB_mode_read:
   MOV    A, REG[USB_EP0MODE]           ; Get the mode reg   

; MSB is the SETUP bit, followed by IN, then OUT
    asl    a                           ; Shift to the carry and jump if SETUP bit set    
    jc      USB_EP0_Setup

    asl    a                           ; Shift to the carry and jump if IN bit set
    jc      USB_EP0_IN

    asl    a                           ; Shift to the carry and jump if OUT bit set
    jc     USB_EP0_OUT
    JMP    USB_Not_Supported_Local_Drv
; ISR Exit Point to update the mode register
;   mode and count have been pushed onto the stack
EXPORT USB_EP0_UPD_MODE_EXIT
USB_EP0_UPD_MODE_EXIT:

    MOV    REG[USB_EP0CNT], A          ; Update the count   
    MOV    A, X                        ; Get the new mode
    MOV    REG[USB_EP0MODE], A         ; Update the node   

USB_EP0_ISR_EXIT:
    
    POP X                              ; 
    POP A                              ; Restore Context
    RETI
;-----------------------------------------------------------------------------
;  FUNCTION NAME: USB_EP0_Setup
;
;  DESCRIPTION:   Dispatch a USB SETUP 
;
;-----------------------------------------------------------------------------
 USB_EP0_Setup:
_USB_EP0_Setup:
; Check the byte count and validity.  All SETUP are 8 bytes and 0 toggle
    PUSH    A                          ; Save the mode register
    MOV     A, USB_XFER_PREMATURE      ; Return a Premature Completion?
    CALL    USB_UpdateStatusBlock
    POP     A                          ; Restore the mode register
    MOV    A, REG[USB_EP0CNT]                ; Get the count reg
    CMP    A, (USB_CNT_VALID | 0x0A)            
    JZ      .dispatch

    JMP    USB_Not_Supported_Local_Drv
;-----------------------------------------------------------------------------
; Jump here to dispatch the request
; The SETUP request is encoded in [bmRequestType]. Among the 8 bits in [bmRequestType], only bits
; 7,6,5,1,0 determine what the request is. Bits [2:4] are default to zero. The below code
; re-organizes [bmRequestType] to the following format: 
; ( Zero, Zero, Bit7, Bit6, Bit5, Bit1, Bit0, Zero ), and depending on the value of this
; "re-organization", the firmware will jump to an appropriate table to handle the request.      
;-----------------------------------------------------------------------------
.dispatch:
    MOV     A, REG[USB_EP0DATA+bmRequestType]   ; Get bmRequestType
    AND     A, E3h                           ; clear bits 4-3-2, these unused for our purposes
    PUSH    A                                ; store value on the stack
    ASR     A                                ; move bits 7-6-5 into 4-3-2's place
    ASR     A                                ; "asr" instruction shift all bits one place to the right.
    ASR     A                                ; Bit7 remains the same.
    MOV     [USB_t2], A                      ; store shifted value
    POP     A                                ; get original value
    OR      A, [USB_t2]                      ; or the two to get the 5-bit field
    AND     A, 1Fh                           ; clear bits 7-6-5 (asr wraps bit7)
                                             ; Bit0 is loaded with a Zero. This results in multiplying 
                                             ; the accumulator by 2, and the reason to mutiply it by 2 
                                             ; is that each "jmp" instruction in the tables is two bytes long.

    LJMP USB_bmRequestType_Dispatch
;-----------------------------------------------------------------------------
;  FUNCTION NAME: USB_EP0_IN
;
;  DESCRIPTION:   Dispatch a USB IN
;
;-----------------------------------------------------------------------------
 USB_EP0_IN:
_USB_EP0_IN:
   
    MOV     A, [USB_TransferType]
USB_ControlInDispatch:               
    JACC    USB_ControlInDispatchTable
USB_ControlInDispatchTable:
    JMP     USB_Not_Supported_Local_Drv  ; USB_TRANS_STATE_IDLE
    JMP     USB_ControlReadDataStage     ; USB_TRANS_STATE_CONTROL_READ
    JMP     USB_ControlWriteStatusStage  ; USB_TRANS_STATE_CONTROL_WRITE
    JMP     USB_NoDataControlStatusStage ; USB_TRANS_STATE_NO_DATA_CONTROL
;-----------------------------------------------------------------------------
;  FUNCTION NAME: USB_EP0_OUT
;
;  DESCRIPTION:   Dispatch a USB OUT
;
;-----------------------------------------------------------------------------
 USB_EP0_OUT:
_USB_EP0_OUT:
    MOV     A, [USB_TransferType]
    JACC    USB_ControlOutDispatchTable
USB_ControlOutDispatchTable:
    JMP     USB_Not_Supported_Local_Drv  ; USB_TRANS_STATE_IDLE
    JMP     USB_ControlReadStatusStage   ; USB_TRANS_STATE_CONTROL_READ
    JMP     USB_ControlWriteDataStage    ; USB_TRANS_STATE_CONTROL_WRITE
    JMP     USB_NoDataControlError       ; USB_TRANS_STATE_NO_DATA_CONTROL
;-----------------------------------------------------------------------------
;  FUNCTION NAME: USB_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 USB_CurrentTD data structure.
;
;-----------------------------------------------------------------------------
 USB_InitControlRead:
    MOV     [USB_LastSize], A          ; Save the packet size?
    CALL    USB_InitializeStatusBlock
    MOV     [USB_TransferType], USB_TRANS_STATE_CONTROL_READ
    
    ; Check the transfer size against the request size
    MOV    A, REG[USB_EP0DATA+wLengthHi] ; MSB of wLength
    CMP    A, [USB_TransferSize]
    JNZ    .L1

    MOV    A, REG[USB_EP0DATA+wLengthLo] ; LSB of wLength
    CMP    A, [USB_TransferSize+1]
    JZ     .L9
.L1:
    JNC    .L9
;    
    MOV    [USB_TransferSize+1], A     ; 

⌨️ 快捷键说明

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