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

📄 counter.asm

📁 驱动程序开发基础教程
💻 ASM
📖 第 1 页 / 共 3 页
字号:
    ; New size
    mov  [gbUSBSendBytes],a

  USBSendDescriptorCheckLengthEnd:
    ret

;//$PAGE
;********************************************************
; USBSendROMBuffer()
; @func Send a number of ROM bytes on end point 0.
; @parm BYTE | gbUSBSendBytes | Number of bytes to send.
; @parm BYTE | gbUSBSendBuffer | Offset from ROM base
;       of data to send.
; @comm assumes IN packets are ignored in the interrupt routine
; @devnote Enables interrupts
;********************************************************
USBSendROMBuffer:

    ; Clear flag
    mov  a,0h
    iowr USBEndP0RxStatus

    ; Enable interrupts
    mov  a,[gbSysInterruptMask]
    and  a,~SysIntUSBEndP0
    iowr SysInterrupt

    ; Auto ACK OUT packet (This would be a Status Out)
    mov  a,USBControlAckStatusData
    iowr USBControl

    ; Initialize sequence
    mov  a,0h
    mov  [gbUSBSendSequence],a

    ; Send count
    mov  a,[gbUSBSendBytes]

USendROMBufferLoop:
    ; One 8-byte chunk or less left?
    cmp  a,08h
    jz   USendROMBufferLoopDone     ; exactly 8 bytes left, branch
    jc   USendROMBufferLoopDone     ; less than 8 bytes left, branch

    ; more than 8 bytes left, fall through and loop 
    ;    until there are 8 bytes or less.
    ; Save count
    push a

    ; Send 8 byte chunk
    mov  a,08h
    mov  [gbUSBSendBytes],a
    call _USBSendROMBuffer

    ; Check for OUT packet cancelling send
    iord USBEndP0RxStatus
    and  a,USBEndP0RxOut

    ; Restore count
    pop  a

    ; Handle exception: OUT packet cancel send
    jnz  USendROMBufferLoopExit     ; Cancelled

    ; Save bytes left
    sub  a,08h
    mov  [gbUSBSendBytes],a
    jmp  USendROMBufferLoop

  USendROMBufferLoopDone:
    ; Send last 8 or less bytes
    call _USBSendROMBuffer

USendROMBufferLoopExit:
    ret

;//$PAGE
;********************************************************
; _USBSendROMBuffer()
; @func Buffer and inialize USB send of up
;               to 8 bytes of ROM data on end point 0.
; @comm affects gbUSBSendBytes & gbUSBSendBuffer
;********************************************************
_USBSendROMBuffer:

    ; Save x
    push x

    ; Initialize
    mov  x,0h

  _USendROMBufferLoop:
    ; Any more?
    mov  a,0h
    cmp  a,[gbUSBSendBytes]
    jz  _USendROMBufferLoopDone         ; No more
    dec  [gbUSBSendBytes]

    ; Move bytes to FIFO
    mov  a,[gbUSBSendBuffer]
    index USBSendROMBufferBase
    mov  [x +USBEndP0FIFO],a
    inc  x

    ; Next byte
    inc  [gbUSBSendBuffer]
    jmp _USendROMBufferLoop

  _USendROMBufferLoopDone:
    ; Re-enable reception
    mov  a,0h
    iowr USBEndP0RxStatus

    ; Toggle sequence
    mov  a,USBEndP0TxSequence
    xor  [gbUSBSendSequence],a

    ; Send bytes
    push x
    pop  a
    or   a,[gbUSBSendSequence]
    or   a,USBEndP0TxRespond
    iowr USBEndP0TxConfig

    ; The FIFO is loaded, go and wait untill it's read
    call USBSendWaitForComplete

  _USendROMBufferEnd:
    ; Restore and exit
    pop  x
    ret

;//$PAGE
;********************************************************
; USBSendACK()
; func Respond to a "USB Status In" with a zero byte buffer with 
;   Sequence field set) on end point 0.
; Called by SetAddress and SetConfig commands
;********************************************************
USBSendACK:

    ; Status response to Status In is to send a zero byte packet
    mov  a,USBEndP0TxRespond | USBEndP0TxSequence
    iowr USBEndP0TxConfig

    ; Enable interrupts
    mov  a,[gbSysInterruptMask]
    iowr SysInterrupt

    ; Wait for send complete
    jmp  USBSendWaitForComplete

;********************************************************
; USBSendWaitForComplete()
; @func Wait for send to complete on end point 0.
;********************************************************

; At some point, either the 0 data will be ACK'd or a SETUP
;    will come in. 
; Either event will cause the "Enable Respond
;    to In Packets" to be reset, and we will fall out of the loop.
; In either case, an EP0 IRQ will be generated (5.9.2.2 in Cyp
;    device spec) if EP0 irq is enabled.

USBSendWaitForComplete:

    ; Poll the send complete bit
    ; This will be reset when the data has been sent to the host
    ;   and the host has ACK's, or the host has sent another SETUP
    ;   which should terminate this activity in any case.
    iord USBEndP0TxConfig
    and  a,USBEndP0TxRespond
    jz   USBSendWaitComplete

    ; Check for OUT packet cancelling send. A STATUS OUT should
    ;   terminate any pending IN's. A Setup could also set the Out bit.
    iord USBEndP0RxStatus
    and  a,USBEndP0RxOut
    jnz  USBSendWaitComplete    ; Cancelled

    ; Keep waiting
    jmp  USBSendWaitForComplete

  USBSendWaitComplete:
    ret

;********************************************************
; USBEventEP0VendorRqst()
; @func Vendor request on end point zero.
; @devnote Runs in interrupt disabled context.
;********************************************************
USBEventEP0VendorRqst:

    ; Save it
    push x

    ; Check Protocol
    mov  a,[USBEndP0FIFO_1]
    cmp  a,0h
    jnz  USBEventEP0Stall                   ; No

    ;*********************************************
    ; Reset Counter Event
    ;*********************************************
    ; Initialize Counter
    mov  a,0
    mov  [gbButtonClicks],a     ; Initial state of 0, no button pushed
    mov  [USBEndP1FIFO_0],a

    iord USBEndP1TxConfig
    and  a,40h
    or   a,91h
    iowr USBEndP1TxConfig

    ;USBEventEP0VendorRqstFinish:

    ; Protocol ACK
    mov  a,42h
    mov  [USBEndP0FIFO_0],a

    ; Auto ACK OUT packet
    mov  a,USBControlAckStatusData
    iowr USBControl

    ; Send bytes as Data1
    mov  a,8
    or   a,USBEndP0TxSequence
    or   a,USBEndP0TxRespond
    iowr USBEndP0TxConfig

    ;call USBSendWaitForComplete

    ; Restore it
    pop  x

    ; Return
    jmp  USBEventEP0End

;********************************************************
; Data Segment (ROM)
;********************************************************
USBSendROMBufferBase:
USBDeviceDescription:
    db 12h          ; Length
    db 01h          ; Type (1=device)
    db 10h,01h      ; Complies to USB Spec. v1.10
    db 00h          ; Class code (0=??)
    db 00h          ; SubClass code (0=??)
    db 00h          ; Protocol (0=none)(9.6.1)
    db 08h          ; Max. packet size for port0
    db 34h,12h      ; Vendor ID: (0x1234=WahBook)
    db 78h,56h      ; Product ID (0x01=USB Counter)
    db 00h,01h      ; Device release v1.00
    db 01h          ; Manufacturer string descriptor index (0=none)
    db 02h          ; Product string descriptor index (0=none)
    db 03h          ; Serial number string descriptor index (0=none)
    db 01h          ; Number of possible configurations
USBDeviceDescriptionEnd:

;*************************************************
;
USBConfigurationDescription:
    db 09h          ; Length
    db 02h          ; Type (2=config)
    db 19h,00h      ; Total data length (1 config,1 interface,1 endpoints)
    db 01h          ; Interface supported (1=???)
    db 01h          ; Configuration value (1=???)
    db 04h          ; Confituration string descriptor index (0=none)
    db 80h          ; Configuration (bus powered)
    db 32h          ; Maximum power consumption in 2mA units
USBConfigurationDescriptionEnd:

;*************************************************
;
USBInterfaceDescription:
    db 09h          ; Length
    db 04h          ; Type (4=interface)
    db 00h          ; Number of interfaces (0 based)
    db 00h          ; Alternate settings
    db 01h          ; Number of endpoints (1 based) (9.6.3)
    db 00h          ; Class code (0=non-specified,1=kb,2=mouse,3=joystick ???)
    db 00h          ; Subclass code (0=???)
    db 00h          ; Protocol code (0=non-specified)
    db 05h          ; Interface string index (0=non-specified, 1,2,3,...)
USBInterfaceDescriptionEnd:

;*************************************************
; Never used for EP0
USBEndPointDescriptionInt:
    db 07h          ; Length
    db 05h          ; Type (5=endpoint)
    db 81h          ; Address (EP#=1 | [0x80=IN, 0=OUT])
    db 03h          ; Attribute (0=control,1=isochronous,2=bulk,3=interrupt)
    db 08h,00h      ; Max packet size
    db B8h          ; Interval (200 ms)
USBEndPointDescriptionIntEnd:

;*************************************************
;
USBStringLanguageDescription:
    db 04h          ; Length
    db 03h          ; Type (3=string)
    db 09h          ; Language:  English
    db 01h          ; Sub-language: US

USBStringDescription1:
    db 10h          ; Length
    db 03h          ; Type (3=string)
    dsu "WAHBOOK"

USBStringDescription2:
    db 10h          ; Length
    db 03h          ; Type (3=string)
    dsu "Counter"

USBStringDescription3:
    db 0Ah          ; Length
    db 03h          ; Type (3=string)
    dsu "0001"

USBStringDescription4:
    db 28h          ; Length
    db 03h          ; Type (3=string)
    dsu "Count Button Clicks"

USBStringDescription5:
    db 3Eh          ; Length
    db 03h          ; Type (3=string)
    dsu "EndPoint1 200ms Interrupt Pipe"

⌨️ 快捷键说明

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