📄 usbcode.asm
字号:
jnz SendStall
mov A, [wValue]
mov [protocol_status], A ; write new protocol value
jmp NoDataControl ; handshake with host
;=======================================================================================================
;********** USB library main routines *******************
;=======================================================================================================
;===================================================================
; Function - Send ROM Data
;===================================================================
; This function controls the sending of data from the descriptor table
; starting from the table index passed in the accumulator. The number
; of bytes to be sent is passed using the global variable <data_count>
SendRomData:
mov [data_start], A ; save start index
call GetDescriptorLength ; correct the descriptor length
call ControlRead ; send the data
ret
;===================================================================
; Function Send Stall
;===================================================================
; Send a stall to indicate the requested function is not supported
SendStall:
;mov A, 0 ;
;iowr EP0_MODE ; set the EP0 stall bit
ret
;===================================================================
; Function Get Descriptor Length
;===================================================================
; The host sometimes lies about the number of bytes it wants from a
; descriptor. Any request to get descriptor should return the lesser
; of the number of bytes requested or the actual length of the descriptor.
GetDescriptorLength:
mov A, [wLengthHi] ;
cmp A, 0 ; confirm high byte is zero
jnz UseActualLength ; no requests should be longer than 256b
mov A, [wLength] ; test low byte against zero
cmp A, 0
jz UseActualLength ; must request some data
cmp A, [data_count] ; compare to the amount of data
jnc UseActualLength
mov [data_count], A ; use requested length
UseActualLength:
ret
;===================================================================
; Function - NoDataControl
;===================================================================
; Send a 0 length data packet to ACK the data sent by the host.
NoDataControl:
iord EP0_COUNT
iord EP0_MODE
and a, ~(EP0_SETUP_B + EP0_IN_B + EP0_OUT_B + EP0_ACK)
or a, 0Fh
iowr EP0_MODE
mov A, DATA01_B ; set up the transfer register for data1
iowr EP0_COUNT ; and 0 byte transfer
ret
;===================================================================
; Function - Control Read
;===================================================================
; Purpose: Performs the control read operation
; as defined by the USB specification
; SETUP-IN-IN-IN...OUT
;
; data_start: must be set to the descriptors info
; as an offset from the beginning of the
; control read table
; data count holds the
; data_count: must be set to the size of the
; descriptor
ControlRead:
mov A, 00h ; clear data 0/1 bit
mov [endp0_data_toggle], A
ControlReadDataStage:
push X
mov X, 00h
mov A, 00h
mov [loop_counter], A
iord EP0_MODE
and a, 0fh
iowr EP0_MODE ; clear setup bit
iord EP0_MODE ; check to make sure another setup
and A, 80h ; has not arrived. Id so, exit ISR
jnz ControlReadStatusStage
mov A, 48h ; set BADOUTS BIT
iowr USB_SCR
mov A, [data_count]
cmp A, 00h
jz DmaLoadDone
DmaLoadLoop: ; loop to load data into the data buffer
mov A, [data_start]
index control_read_table
mov [X + EP0_FIFO], A ; load dma buffer
inc [data_start]
inc X
inc [loop_counter]
dec [data_count] ; exit if descriptor
jz DmaLoadDone ; is done
mov A, [loop_counter] ; or 8 bytes sent
cmp A, 08h
jnz DmaLoadLoop
DmaLoadDone:
iord EP0_COUNT
iord EP0_MODE ; check setup bit
and A, 80h ; if not cleared, another setup
jnz ControlReadStatusStage ; has arrived so exit
mov A, [endp0_data_toggle]
xor A, DATA01_B
mov [endp0_data_toggle], A
;or A, 80h
or A, [loop_counter]
iowr EP0_COUNT
ControlReadStatusStage: ; OUT at end of data transfer
pop X ; restore X from stack
ret
;=======================================================================================================
; ROM Tables
;=======================================================================================================
;
XPAGEOFF
control_read_table:
device_desc_table:
db (end_device_desc_table - device_desc_table) ; Descriptor length (18 bytes)
db 01h ; Descriptor type (Device)
db 10h,01h ; Complies with USB Spec. Release (1.1)
db 00h ; Class code (0)
db 00h ; Subclass code (0)
db 00h ; Protocol (No specific protocol)
db 08h ; Max. packet size for EP0 (8 bytes)
db 25h,09h ; Vendor ID (Cypress) - User must chage this to own Vid
db 36h,12h ; Product ID (Frameworks)- User must Change this
db 00h,00h ; Device release number
db 01h ; Mfr string descriptor index
db 02h ; Product string descriptor index
db 00h ; Serial Number string descriptor index (None)
db 01h ; Number of possible configurations (1)
end_device_desc_table:
config_desc_table:
db (Interface_Descriptor - config_desc_table) ; Descriptor length (9 bytes)
db 02h ; Descriptor type (Configuration)
db (end_config_desc_table - config_desc_table) ,00h
; Total length of config, i/f, HID, and EP descriptors
db 01h ; Interface supported (1)
db 01h ; Configuration value (1)
db 04h ; Index of string descriptor
db 80h ; Configuration (Bus powered)
db 10h ; Max power consumption (32mA) MUST be < 100mA if bus powered
Interface_Descriptor:
db (Class_Descriptor - Interface_Descriptor) ; Descriptor length (9 bytes)
db 04h ; Descriptor type (Interface)
db 00h ; Number of interface (0)
db 00h ; Alternate setting (0)
db 02h ; Number of interface endpoint (1)
db 03h ; Class code ()
db 00h ; Subclass code ()
db 00h ; Protocol code ()
db 05h ; Index of string()
Class_Descriptor:
db (Endpoint_Descriptor1 - Class_Descriptor) ; Descriptor length (9 bytes)
db 21h ; Descriptor type (HID)
db 00h,01h ; HID class release number (1.00)
db 00h ; Localized country code (None)
db 01h ; # of HID class dscrptr to follow (1)
db 22h ; Report descriptor type (HID)
dwl (end_hid_report_desc_table - hid_report_desc_table) ; Total length of report descriptor (2 bytes)
Endpoint_Descriptor1:
db (Endpoint_Descriptor2 - Endpoint_Descriptor1) ; Descriptor length (7 bytes)
db 05h ; Descriptor type (Endpoint)
db 81h ; Encoded address (Respond to IN, 1 endpoint)
db 03h ; Endpoint attribute (Interrupt transfer)
db 01h,00h ; Maximum packet size (1 bytes)
db 0Ah ; Polling interval (10 ms)
Endpoint_Descriptor2:
db (end_config_desc_table - Endpoint_Descriptor2) ; Descriptor length (7 bytes)
db 05h ; Descriptor type (Endpoint)
db 02h ; Encoded address (Respond to OUT, 2 endpoint)
db 03h ; Endpoint attribute (Interrupt transfer)
db 01h,00h ; Maximum packet size (1 bytes)
db 0Ah ; Polling interval (10 ms)
end_config_desc_table:
; This report descriptor is taken from Appendix A12 of the HID Uasage Tables
hid_report_desc_table:
db 06h, A0h, FFh ; Usage Page (vendor defined) FFA0
db 09h, 01h ; Usage (vendor defined)
db A1h, 01h ; Collection (Application)
db 09h, 02h ; Usage (vendor defined)
db A1h, 00h ; Collection (Physical)
db 06h, A1h, FFh ; Usage Page (vendor defined)
;The input report
db 09h, 03h ; usage - vendor defined
db 09h, 04h ; usage - vendor defined
db 15h, 80h ; Logical Minimum (-128)
db 25h, 7Fh ; Logical Maximum (127)
db 35h, 00h ; Physical Minimum (0)
db 45h, FFh; Physical Maximum (255)
; db 66h, 00h, 00h; Unit (None (2 bytes))
db 75h, 08h ; Report Size (8) (bits)
db 95h, 01h ; Report Count (1) (fields)
db 81h, 02h ; Input (Data, Variable, Absolute)
;The output report
db 09h, 05h ; usage - vendor defined
db 09h, 06h ; usage - vendor defined
db 15h, 80h ; Logical Minimum (-128)
db 25h, 7Fh ; Logical Maximum (127)
db 35h, 00h ; Physical Minimum (0)
db 45h, FFh ; Physical Maximum (255)
; db 66h, 00h, 00h; Unit (None (2 bytes))
db 75h, 08h ; Report Size (8) (bits)
db 95h, 01h ; Report Count (1) (fields)
db 91h, 02h ; Output (Data, Variable, Absolute)
db C0h ; End Collection
db C0h ; End Collection
end_hid_report_desc_table:
;========================================================================
; These tables are the mechanism used to return status information to the
; host. The status can be either device, interface, or endpoint.
get_dev_status_table:
db 00h, 00h ; remote wakeup disabled, bus powered
wakeup_enabled:
db 02h, 00h ; remote wakeup enabled, bus powered
get_interface_status_table:
db 00h, 00h ; always return both bytes zero
get_endpoint_status_table:
db 00h, 00h ; not stalled
stalled:
db 01h, 00h ; stalled
get_configuration_status_table:
db 00h ; not configured
db 01h ; configured
get_protocol_status_table:
db 00h ; boot protocol
db 01h ; report protocol
get_interface_table:
db 00h ; no alternate setting
;========================================================================
; String Descriptors Do not make the string descriptor too long
; because the entire rom lookup table cannot be
; longer than 256 bytes
; string 0
USBStringLanguageDescription:
db (USBStringDescription1 - USBStringLanguageDescription)
db 03h ; Type (3=string)
db 09h ; Language: English
db 04h ; Sub-language: Default
; string 1
USBStringDescription1: ; IManufacturerName
db (USBStringDescription2 - USBStringDescription1)
db 03h ; Type (3=string)
dsu "CYPRESS" ;
; string 2
USBStringDescription2: ; IProduct
db (USBStringDescription3 - USBStringDescription2)
db 03h ; Type (3=string)
dsu "CYPRESS FRAMEWORKS" ;
;string 3
USBStringDescription3: ; serial number
; If a SN is used, this must be unique
; for every device or the device may
; not enumerate properly
; string 4
USBStringDescription4: ; configuration string descriptor
db (USBStringDescription5 - USBStringDescription4)
db 03h ; Type (3=string)
dsu "FRAMEWORKS" ;
;string 5
USBStringDescription5: ; interface string descriptor
db (USBStringEnd - USBStringDescription5)
db 03h ; Type (3=string)
dsu "EP1" ;
USBStringEnd:
XPAGEON
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -