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

📄 usbasm.txt

📁 cy7c64113的汇编usb源码,质量很高,
💻 TXT
📖 第 1 页 / 共 4 页
字号:
parse:

	mov A, nak				; first, set to NAK IN/OUT mode, nak=01h
	iowr EP0_mode
	iord EP0_mode
	cmp A, nak
	jnz parse

;-------------------------------
	mov A, [EP0_buffer0]		; parse packet
	cmp A, 00h
	jnz parse_bmRequest1

;bmRequest=[00]                     (Set Device, Host->Device) 
	mov A, [EP0_buffer1]		; [00],[01] host2device and clear device feature 
	cmp A, clear_feature
	jz clear_feat

	mov A, [EP0_buffer1]		; Parse 
	cmp A, set_feature		; [00],[03] host2device and set device feature
	jz set_feat

	mov A, [EP0_buffer1]		; Parse
	cmp A, set_address		; [00],[05] host2device and set device address
	jz set_addr

	mov A, [EP0_buffer1]		; Parse
	cmp A, set_configuration	; [00],[09] host2device and set device configuration
	jz set_config

h2dd_stall:


	mov A, stall			; send a stall to indicate that the requested
	iowr EP0_mode			; function is not supported
	iord EP0_mode			; write retry
	cmp A, stall	
	jnz h2dd_stall
	ret

;--------------------------------

parse_bmRequest1:
	mov A, [EP0_buffer0]
	cmp A, 01h
	jnz parse_bmRequest2

;bmRequest=[01]				(Set Interface, Host->Device) 
	mov A, [EP0_buffer1]		; Parse
	cmp A, set_interface		; [01],[0B] host2device and set interface
	jz set_interf

h2di_stall:

	mov A, stall			; send a stall to indicate that the requested
	iowr EP0_mode			; function is not supported
	iord EP0_mode			; write retry
	cmp A, stall	
	jnz h2di_stall
	ret

;-------------------------------

parse_bmRequest2:
	mov A, [EP0_buffer0]
	cmp A, 02h		
	jnz parse_bmRequest3

;bmRequest=[02]				(Set Endpoint, Host->Device)
	mov A, [EP0_buffer1]		; parse
	cmp A, set_feature		; [02],[03] host2device and set endpoint feature
	jz set_feat

	mov A, [EP0_buffer1]
	cmp A, clear_feature		; [02],[01] host2device and clear endpoint feature
	jz clear_feat

h2de_stall:
	mov A, stall			; send a stall to indicate that the requested
	iowr EP0_mode			; function is not supported
;	iord EP0_mode			; write retry
;	cmp A, stall	
;	jnz h2de_stall
	ret
;-------------------------------

parse_bmRequest3:
	mov A, [EP0_buffer0]		; read the first byte of the buffer
	cmp A, 80h				; compare to 80h
	jnz parse_bmRequest4

;bmRequest=[80]				(Get Device, Device->Host) 
	mov A, [EP0_buffer1]		; parse
	cmp A, get_configuration	; [80],[08] device2host and get device configuration
	jz get_config
	
	mov A, [EP0_buffer1]
	cmp A, get_status			; [80],[00] device2host and get device status
	jz get_stat

	mov A, [EP0_buffer1]
	cmp A, get_descriptor		; [80],[06] device2host and get device descriptor
	jz get_desc

d2hd_stall:
	mov A, stall			; send a stall to indicate that the requested
	iowr EP0_mode			; function is not supported
;	iord EP0_mode			; write retry
;	cmp A, stall	
;	jnz d2hd_stall
	ret
;-------------------------------

parse_bmRequest4:
	mov A, [EP0_buffer0]
	cmp A, 81h
	jnz parse_bmRequest5

;bmRequest=[81]				(Get interface, Device->Host)
	mov A, [EP0_buffer1]		; parse
	cmp A, get_interface    	; [81],[0A] device2host and get interface
	jz get_interf	

d2hi_stall:
	mov A, stall			; send a stall to indicate that the requested
	iowr EP0_mode			; function is not supported
;	iord EP0_mode			; write retry
;	cmp A, stall	
;	jnz d2hi_stall
	ret
;-------------------------------

parse_bmRequest5:
	mov A, [EP0_buffer0]
	cmp A, 82h
	jnz  end_parse_bmRequest
	
;bmRequest=[82]				(Get Endpoint, Device->Host)
;match:
	mov A, [EP0_buffer1]		; parse
	cmp A, get_descriptor		; [82],[06] device2host and get endpoint descriptor
	jz get_desc

	mov A, [EP0_buffer1]		; parse
	cmp A, endpoint_status		; [82],[00] device2host and get endpoint status
	jz endp_status

d2he_stall:
	mov A, stall			; send a stall to indicate that the requested
	iowr EP0_mode			; function is not supported
;	iord EP0_mode			; write retry
;	cmp A, stall	
;	jnz d2he_stall
	ret
;-----------------------------
;-----------------------------
;-----------------------------      ; add for printer class specified request
;parse_bmRequest6:
;	mov A, [EP0_buffer0]
;	cmp A, 0a1h
;	jnz parse_bmRequest7

;bmRequset=[a1]				(Get Class, Device->Host)
;	mov A, [EP0_buffer1]
;	cmp A, get_deviceID		; [a1],[00] device2host and get class device ID
;     jz get_ID

;	mov A, [EP0_buffer1]
;	cmp A, get_portstatus		; [a1],[01] device2host and get port status 
;     jz get_portstat

d2hc_stall:
;	mov A, stall			; send a stall to indicate that the requested
;	iowr EP0_mode			; function is not supported
;	iord EP0_mode			; write retry
;	cmp A, stall	
;	jnz d2hc_stall
;	ret
;------------------------------

;parse_bmRequest7:
;	mov A, [EP0_buffer0]
;	cmp A, 21h
;	jnz end_parse_bmRequest

;bmRequest=[21]				(Set, Soft-Reset, Host->Device)
;	mov A, [EP0_buffer1]
;	cmp A, 02h				; [21],[02] device2host and soft-reset
;	jz soft_reset
	        
end_parse_bmRequest:

	mov A, stall			; send a stall to indicate that the requested
	iowr EP0_mode			; function is not supported
;	iord EP0_mode			; write retry
;	cmp A, stall	
;	jnz end_parse_bmRequest
	ret


;*******************************************************
; step 2: Determine request type (bRequest) 
;*******************************************************

set_addr:

	call no_data_control
	mov A, [EP0_buffer2]		; get address from buffer2
	or A, 80h				; address enable bit
	iowr USB_address			; set usb address register
	ret

set_feat:

	mov A, [EP0_buffer2]
	cmp A, endpoint_stalled
	jnz remote_wakeup
	mov A, 01h
	mov [endpoint_stall], A
	call no_data_control	
	ret
remote_wakeup:
	call no_data_control
	ret

clear_feat:
	mov A, [EP0_buffer2]
	cmp A, endpoint_stalled
	jnz not_endp_clear 
	mov A, 00h
	mov [endpoint_stall], A
not_endp_clear:
	call no_data_control
	ret

get_interf:
	mov A, 36h ;2eh			; send interface value
	mov [data_start], A
	mov A, [EP0_buffer6]
	mov [data_count], A		 
	call control_read
	ret

set_interf:
	call no_data_control
	ret

get_config:					; [80], [08]
	mov A, 34h ;2dh   		; send configuration value

;--------------------------------------------------------
; code added to complete chapter 9 Get_Configuration Test
;-------------------------------------------------------- 	
      add A, [config_status]
;--------------------------------------------------------
; added code end
;--------------------------------------------------------

      mov [data_start], A
	mov A, [EP0_buffer6]
	mov [data_count], A	 
	call control_read
	ret

set_config:					; [00], [09]
	call no_data_control

;--------------------------------------------------------
; code added to complete chapter 9 Get_Configuration Test
;-------------------------------------------------------- 	
	
	mov A, [EP0_buffer2]
	mov [config_status], A
	mov A, 00h
	mov [endpoint_stall], A
	iord EP0_counter
	and A, 7fh				; clear data_0/1_toggle bit[7]
	iowr EP0_counter
	mov A, [config_status]
	cmp A, 00h
	jnz device_configured
						; device is unconfigured
	mov A, disabled			; disable endpoint1
	iowr EP1_mode			; disable endpoint2
	iowr EP2_mode
	
	iord EP_int
	and A, 19h				; [0001,1001]
	iowr EP_int				; disable endpoint1&2 interrupts	
	jmp done_configuration
	
device_configured: 			; device is configured	

	iord EP_int
	or A,06h 				; [0000,0110]
	iowr EP_int				; enable endpoint1&2 interrupts
	call prepare_EP 

;--------------------------------------------------------
; added code end
;--------------------------------------------------------

done_configuration:
	ret

get_stat:
	mov A, [EP0_buffer7]	       
	cmp A, device_status	
	jz dev_status

get_stat_install:
	mov A, stall			; send a stall to indicate that the requested
	iowr EP0_mode			; function is not supported
;	iord EP0_mode			; write retry
;	cmp A, stall	
;	jnz get_stat_install
	ret

endp_status:
	mov A, [endpoint_stall]
	cmp A, endpoint_stalled
	jnz endp_stalled_status
	mov A, 37h ;2fh   
	mov [data_start], A
	mov A, [EP0_buffer6]
	mov [data_count], A
	call control_read
	ret

endp_stalled_status:
	mov A, 39h ;31h
	mov [data_start], A
	mov A, [EP0_buffer6]
	mov [data_count], A
	call control_read
	ret
;--------------------------------

;--------------------------------	

get_desc:
	mov A, [EP0_buffer3]
	cmp A, device			; device=01H
	jz device_desc

	mov A, [EP0_buffer3]
	cmp A, configuration		; configuration=02h
	jz config_desc

	mov A, [EP0_buffer3]
	cmp A, string			; string=03h
	jz string_desc
	

get_desc_stall:
	mov A, stall			; send a stall to indicate that the requested
	iowr EP0_mode			; function is not supported
;	iord EP0_mode			; write retry
;	cmp A, stall	
;	jnz get_dest_stall
	ret
 
;*******************************************************
; step 3: Determine descriptor type (descriptor) 
;*******************************************************

dev_status:
	mov A, 32h ;2bh 
	mov [data_start], A
	mov A, [EP0_buffer6]
	mov [data_count], A
	call control_read
	ret

device_desc:
	mov A, 00h
	mov [data_start], A
	mov A, [EP0_buffer6]
	mov [data_count], A
	call control_read
	ret

config_desc:
	mov A, 12h
	mov [data_start], A
	mov A, [EP0_buffer6]
	cmp A, 20h ;19h 			; descriptor length=20(32) bytes
	jc config_desc_data_length
	mov A, 20h ;19h   

config_desc_data_length:
	mov [data_count], A
	call control_read
	ret

string_desc:
	mov A, [EP0_buffer2]
	cmp A,00h
	jnz next_string1
	
	mov A, 3bh ;33h               ; 00h, language string
	mov [data_start], A
	mov A, [EP0_buffer6];04h	; 4 bytes
	mov [data_count], A
	call control_read
	ret

next_string1:
	mov A, [EP0_buffer2]
	cmp A,01h
	jnz next_string2
	
	mov A, 3fh ;37h			; 01h, manufacture string
	mov [data_start], A
	mov A, [EP0_buffer6] ;0ch	; 12 bytes
	mov [data_count], A
	call control_read
	ret


next_string2:
	mov A, [EP0_buffer2]
	cmp A,02h
	jnz string_desc_stall
	
	mov A, 4bh ;43h 			; 02h, product string
	mov [data_start], A
	mov A, [EP0_buffer6] ;18h	; 24 bytes
	mov [data_count], A
	call control_read
	ret

string_desc_stall:
	mov A, stall			; send a stall to indicate that the requested
	iowr EP0_mode			; function is not supported
;	iord EP0_mode			; write retry
;	cmp A, stall	
;	jnz get_dest_stall
	ret



;*******************************************************
; 		Function: prepare_EP
;		Purpose: Get ready for endpoint 1 & 2 
;*******************************************************
prepare_EP:	
      
	push X

	mov A, 00h				; clear endpoint1&2 buffers 32 bytes
	mov X, 1fh				; 32 bytes


clear_loop: 
	mov [X + EP1_buffer0], A	 
	mov [X + EP2_buffer0], A
	dec X 
	jnc clear_loop

;-------- mod at July,26,2001--------------------

	mov A, out_ack			; respond to OUT on endpoint2, enable, 32 bytes
	iowr EP2_mode

	mov A, in_nak			;in_ack	; respond to IN on endpoint1, enable, 32 bytes
	iowr EP1_mode
	
	mov A, 20h				; data length 32 bytes
	iowr EP1_counter	

;-------- mod end at July,26,2001--------------------

	mov A, [HAPI_STAT_reg]
	and A, 0afh				 ; clear bit4 UBUSY and bit6 UERROR [1010,1111] USB ready  	
	mov [HAPI_STAT_reg], A

	pop X
	ret		
	
;*******************************************************
;	Interrupt handler: endpoint_one ( mod at July,26,2001)
;	Purpose: This interrupt routine handles the specially
;		 reserved data endpoint 1. 
;		 This interrupt happens every time a host sends an
;		 IN on endpoint 1.  The data to send (NAK or 1
;		 byte packet) is already loaded, so this routine
;		 just prepares the dma buffers for the next packet
;*******************************************************

endpoint_one:

	push A				; store A register
	push X

	iord EP1_mode
	and a, 10h				; check ACK bit [4]
	jz ep1_done

	mov A, [EP1_data_toggle]	; change endpoint2
	xor A, 80h				; data toggle
	and A, 80h
	mov [EP1_data_toggle], A

;	mov A, 00h				; clear buffer
;	mov X, 1fh

;clear_EP1_buffer:	
;	mov [X + EP1_buffer0], A
;	dec X
;	jnz clear_EP1_buffer

;--------- mod at Aug,09 2001 ----------------------
	
	iord EP_int				; disable EP1 interrupt x[1111,1101]
	and A, 0fdh				
	iowr EP_int

	mov A, in_nak			; will be set to IN-ACK at HAPI_int (After 32 bytes received from HC12)
	iowr EP1_mode

	mov A, [HAPI_STAT_reg]		; set bit1 UTDRE,
	or A, HAPI_UTDRE
	mov [HAPI_STAT_reg], A


;--------- mod end at Aug,09 2001 ----------------------

ep1_done:	
	pop X
	pop A					; restore A register
	reti
	

;*******************************************************
;	Function: Send Data to Host ( mod at July,26,2001)
;	Purpose: Moves data in "USB" pattern (endpoint1)
;*******************************************************

;send_data:
	
	push A
	push X

⌨️ 快捷键说明

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