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

📄 enchid.asm

📁 USB主控器固件程序源码
💻 ASM
📖 第 1 页 / 共 3 页
字号:
		mov		A, [wValuelo]
		mov		[pending_data], A
		jmp		initialize_no_data_control

	set_device_configuration:				; SET CONFIGURATION
		mov		A, [wValuelo]
		cmp		A, 01h
		jz		configure_device
;0 means unconfigure the device. 
		unconfigure_device:					; set device as unconfigured
			mov		[configuration], A
			mov		A, DISABLE				; disable endpoint 1
			iowr	ep1_mode
			mov 	A, EP0_INT				; turn off endpoint 1 interrupts
			iowr 	endpoint_int
			jmp		set_device_configuration_done
;1 means select configuration 1 (the only configuration option).
		configure_device:					; set device as configured
			mov		[configuration], A

			mov		A, [ep1_stall]			; if endpoint 1 is stalled
			and		A, FFh
			jz		ep1_nak_in_out
				mov		A, STALL_IN_OUT		; set endpoint 1 mode to stall
				iowr	ep1_mode
				jmp		ep1_set_int
			ep1_nak_in_out:
				mov		A, NAK_IN_OUT		; otherwise set it to NAK in/out
				iowr	ep1_mode
			ep1_set_int:
			mov 	A, EP0_INT | EP1_INT 	; enable endpoint 1 interrupt		
			iowr 	endpoint_int
			mov		A, 00h
			mov		[ep1_data_toggle], A	; reset the data toggle
			mov		[ep1_dmabuff0], A		; reset endpoint 1 fifo values
			mov		[ep1_dmabuff1], A
			mov		[ep1_dmabuff2], A
			set_device_configuration_done:
			mov		A, NO_CHANGE_PENDING
			mov		[ep0_in_flag], A
			jmp		 initialize_no_data_control


	get_device_status:						; GET STATUS
		mov		A, DEVICE_STATUS_LENGTH
		mov		[maximum_data_count], A
		mov		A, (device_status_wakeup_disabled - control_read_table)
		mov		[data_start], A
		jmp		initialize_control_read
		

	get_device_descriptor:					; GET DESCRIPTOR
		mov		A, [wValuehi]
		asl		A
		jacc	get_device_descriptor_jumptable

		send_device_descriptor:
			mov		A, 00h					; get device descriptor length
			index	device_desc_table
			mov		[maximum_data_count], A
			mov		A, (device_desc_table - control_read_table)
			mov		[data_start], A
			jmp		initialize_control_read

		send_configuration_descriptor:
			mov		A, 02h
   			index	config_desc_table:
			mov		[maximum_data_count], A
			mov		A, (config_desc_table - control_read_table)
			mov		[data_start], A
			jmp		initialize_control_read

		send_string_descriptor:
			mov		A, [wValuelo]
			asl		A
			jacc	string_jumptable:

			language_string:
				mov		A, 00h
				index	ilanguage_string
				mov		[maximum_data_count], A
				mov		A, (ilanguage_string - control_read_table)
				mov		[data_start], A
				jmp		initialize_control_read

			manufacturer_string:
				mov		A, 00h
				index	imanufacturer_string
				mov		[maximum_data_count], A
				mov		A, (imanufacturer_string - control_read_table)
				mov		[data_start], A
				jmp		initialize_control_read

			product_string:
				mov		A, 00h
				index	iproduct_string
				mov		[maximum_data_count], A
				mov		A, (iproduct_string - control_read_table)
				mov		[data_start], A
				jmp		initialize_control_read

		send_interface_descriptor:
			mov		A, 00h					; get interface descriptor length
			index	interface_desc_table 
			mov		[maximum_data_count], A
			mov		A, (interface_desc_table - control_read_table)
			mov		[data_start], A
			jmp		initialize_control_read

		send_endpoint_descriptor:
			mov		A, 00h					; get endpoint descriptor length
			index	endpoint_desc_table
			mov		[maximum_data_count], A
			mov		A, (endpoint_desc_table - control_read_table)
			mov		[data_start], A
			jmp		initialize_control_read


		get_device_configuration:			; GET CONFIGURATION
			mov		A, DEVICE_CONFIG_LENGTH
			mov		[maximum_data_count], A
			mov		A, [configuration]		; test configuration status
			and		A, FFh
			jz		device_unconfigured
			device_configured:				; send configured status
				mov		A, (device_configured_table - control_read_table)
				mov		[data_start], A
				jmp		initialize_control_read
			device_unconfigured:				; send unconfigured status
				mov		A, (device_unconfigured_table - control_read_table)
				mov		[data_start], A
				jmp		initialize_control_read


	;;************ INTERFACE REQUESTS ***********

	set_interface_interface:				; SET INTERFACE
		mov		A, [wValuelo]
		cmp		A, 00h						; there are no alternate interfaces
		jz		alternate_supported			; for this device
		alternate_not_supported:			; if the host requests any other
			jmp		request_not_supported	; alternate than 0, stall.	
		alternate_supported:
			mov		A, NO_CHANGE_PENDING
			mov		[ep0_in_flag], A
			jmp		initialize_no_data_control


	get_interface_status:					; GET STATUS
		mov		A, INTERFACE_STATUS_LENGTH
		mov		[maximum_data_count], A
		mov		A, (interface_status_table - control_read_table)
		mov		[data_start], A
		jmp		initialize_control_read
		

	get_interface_interface:				; GET INTERFACE
		mov		A, INTERFACE_ALTERNATE_LENGTH
		mov		[maximum_data_count], A
		mov		A, (interface_alternate_table - control_read_table)
		mov		[data_start], A
		jmp		initialize_control_read


	set_interface_idle:						; SET IDLE
		mov		A, [wValuehi]				; test if new idle time 
		cmp		A, 00h						; disables idle timer
		jz		idle_timer_disable

		mov		A, [idle_timer]				; test if less than 4ms left
		cmp		A, 01h
		jz		set_idle_last_not_expired

		mov		A, [wValuehi]				; test if time left less than
		sub		A, [idle_timer]				; new idle value
		jnc		set_idle_new_timer_less

		jmp		set_idle_normal

		idle_timer_disable:
			mov		[idle], A				; disable idle timer
			jmp		set_idle_done

		set_idle_last_not_expired:
			mov		A, EVENT_PENDING		; send report immediately
			mov		[event_machine], A
			mov		A, 00h					; reset idle prescaler
			mov		[idle_prescaler], A
			mov		A, [wValuehi]			; set new idle value
			mov		[idle_timer], A
			mov		[idle], A
			jmp		set_idle_done

		set_idle_new_timer_less:			
			mov		A, 00h
			mov		[idle_prescaler], A		; reset idle prescaler
			mov		A, [wValuehi]
			mov		[idle_timer], A			; update idle time value
			mov		[idle], A
			jmp		set_idle_done

		set_idle_normal:
			mov		A, 00h					; reset idle prescaler
			mov		[idle_prescaler], A
			mov		A, [wValuehi]			; update idle time value
			mov		[idle_timer], A
			mov		[idle], A

		set_idle_done:
			mov		A, NO_CHANGE_PENDING	; respond with no-data control
			mov		[ep0_in_flag], A		; transaction
			jmp		initialize_no_data_control


	set_interface_protocol:					; SET PROTOCOL
		mov		A, [wValuelo]
		mov		[protocol], A				; set protocol value
		mov		A, NO_CHANGE_PENDING
		mov		[ep0_in_flag], A			; respond with no-data control
		jmp		initialize_no_data_control	; transaction


	get_interface_report:					; GET REPORT
; I haven't tested this request.
; (No way to force a get_report request for an In report.)
; But it could be adapted to handle device-to-host Feature reports.
		mov		A, DATA_TOGGLE				; set data toggle to DATA ONE
		mov		[ep0_data_toggle], A
;Send NAK in response to any new INs or OUTs while writing to the buffers.
		mov		A, NAK_IN_OUT				; clear setup bit to write to
		iowr	ep0_mode					; endpoint fifo

;If the host requests the In report with a control transfer, 
;copy the In report data from Endpoint 1's buffer to Endpoint 0.
		mov		A, [ep1_dmabuff0]
		mov		[ep0_dmabuff0], A

		mov		A, [ep1_dmabuff1]
		mov		[ep0_dmabuff1], A

		mov		A, CONTROL_READ_DATA		; set state machine state
		mov		[ep0_in_machine], A			
		mov		X, 02h						; set number of bytes to transfer to 2
		jmp		dmabuffer_load_done			; jump to finish transfer
		
	
	get_interface_idle:						; GET IDLE
		mov		A, DATA_TOGGLE				; set data toggle to DATA ONE
		mov		[ep0_data_toggle], A
		mov		A, NAK_IN_OUT				; clear setup bit to write to
		iowr	ep0_mode					; endpoint fifo

		mov		A, [idle]					; copy over idle time
		mov		[ep0_dmabuff0], A

		mov		A, CONTROL_READ_DATA		; set state machine state
		mov		[ep0_in_machine], A			
		mov		X, 01h						; set number of byte to transfer to 3
		jmp		dmabuffer_load_done			; jump to finish transfer

	
	get_interface_protocol:					; GET PROTOCOL
		mov		A, INTERFACE_PROTOCOL_LENGTH
		mov		[maximum_data_count], A		; get offset of device descriptor table
		mov		A, [protocol]
		and		A, 01h
		jz		boot_protocol
		report_protocol:
			mov		A, (interface_report_protocol - control_read_table)
			mov		[data_start], A
			jmp		initialize_control_read	; get ready to send data
		boot_protocol:
			mov		A, (interface_boot_protocol - control_read_table)
			mov		[data_start], A
			jmp		initialize_control_read	; get ready to send data


	get_interface_hid:
		mov		A, [wValuehi]
		cmp		A, 21h
		jz		get_interface_hid_descriptor
		cmp		A, 22h
		jz		get_interface_hid_report
		jmp		request_not_supported

	get_interface_hid_descriptor:			; GET HID CLASS DESCRIPTOR
		mov		A, 00h						; get hid decriptor length
		index	hid_desc_table
		mov		[maximum_data_count], A		; get offset of device descriptor table
		mov		A, (hid_desc_table - control_read_table)
		mov		[data_start], A
		jmp		initialize_control_read		; get ready to send data


	get_interface_hid_report:				; GET HID REPORT DESCRIPTOR
		mov		A, 07h						; get hid report descriptor length
		index	hid_desc_table
		mov		[maximum_data_count], A		; get offset of device descriptor table
		mov		A, (hid_report_desc_table - control_read_table)
		mov		[data_start], A
		jmp		initialize_control_read		; get ready to send data


	;;************ ENDPOINT REQUESTS ************

	clear_endpoint_feature:					; CLEAR FEATURE
		mov		A, [wValuelo]
		cmp		A, ENDPOINT_STALL
		jnz		request_not_supported		
		mov		A, 00h						; clear endpoint 1 stall
		mov		[ep1_stall], A
		mov		A, NO_CHANGE_PENDING		; respond with no-data control
		mov		[ep0_in_flag], A
		jmp		initialize_no_data_control

	set_endpoint_feature:					; SET FEATURE
		mov		A, [wValuelo]
		cmp		A, ENDPOINT_STALL
		jnz		request_not_supported		
		mov		A, FFh						; stall endpoint 1
		mov		[ep1_stall], A
		mov		A, NO_CHANGE_PENDING		; respond with no-data control
		mov		[ep0_in_flag], A
		jmp		initialize_no_data_control

	get_endpoint_status:					; GET STATUS
		mov		A, ENDPOINT_STALL_LENGTH
		mov		[maximum_data_count], A
		mov		A, [ep1_stall]				; test if endpoint 1 stalled
		and		A, FFh
		jnz		endpoint_stalled
		endpoint_not_stalled:				; send no-stall status
			mov		A, (endpoint_nostall_table - control_read_table)
			mov		[data_start], A
			jmp		initialize_control_read
		endpoint_stalled:					; send stall status
			mov		A, (endpoint_stall_table - control_read_table)
			mov		[data_start], A
			jmp		initialize_control_read
		

	;;************ CLASS INTERFACE REQUESTS ************

	set_report:							; SET REPORT
;Find out how many bytes to read in the Out transaction(s) that will follow.
;This value is in WLengthlo and WLengthhi.
;Save the length in data_count.
		mov A, [wLengthlo]
		mov [data_count], A
;The Output reports are 2 bytes, so wLengthhi is unused.
		mov A, 0
		mov [wLengthhi], A

;Unlock the counter register so it can be updated with the 
;number of bytes in the data stage
		iord	ep0_count

;For testing,
;copy the bytes received in the last Output report
;to Endpoint 1' buffer for sending back to the host.
			mov		A, [data_byte_0]
			mov		[ep1_dmabuff0], A
			mov		A, [data_byte_1]
			mov		[ep1_dmabuff1], A

;Enable receiving data in an Out transaction.
		jmp initialize_control_write
;my code **************************************************

;;***************** CONTROL READ TRANSACTION **************

	initialize_control_read:
		mov		A, TRANS_CONTROL_READ		; set transaction type to control read
		mov		[ep0_transtype], A

		mov		A, DATA_TOGGLE				; set data toggle to DATA ONE
		mov		[ep0_data_toggle], A

		; if wLengthhi == 0
		mov		A, [wLengthhi]				; find lesser of requested and maximum
		cmp		A, 00h
		jnz		initialize_control_read_done
		; and wLengthlo < maximum_data_count
		mov		A, [wLengthlo]				; find lesser of requested and maximum
		cmp		A, [maximum_data_count]		; response lengths
		jnc		initialize_control_read_done
		; then maximum_data_count >= wLengthlo
		mov		A, [wLengthlo]
		mov		[maximum_data_count], A
		initialize_control_read_done:
			jmp		control_read_data_stage	; send first packet


;;***************** CONTROL WRITE TRANSACTION *************

	initialize_control_write:
		mov		A, TRANS_CONTROL_WRITE		; set transaction type to control write
;The firmware uses the value in ep0_transtype to decide how to respond
;to a token packet.
		mov		[ep0_transtype], A

		mov		A, DATA_TOGGLE				; set accepted data toggle
		mov		[ep0_data_toggle], A

;Send ACK in response to OUT packets, 
;which will contain the Control Write data.
;Send NAK in response to IN packets (not expected).
		mov		A, ACK_OUT_NAK_IN			; set mode
		iowr	ep0_mode

;Return from the Endpoint 0 ISR.
		pop		A
		pop		X
		reti

;;***************** NO DATA CONTROL TRANSACTION ***********

	initialize_no_data_control:
		mov		A, TRANS_NO_DATA_CONTROL	; set transaction type to no data control
		mov		[ep0_transtype], A

		mov		A, STATUS_IN_ONLY			; set SIE for STATUS IN mode
		iowr	ep0_mode
		pop		A
		pop		X
		reti


;;***************** UNSUPPORTED TRANSACTION ***************

	request_not_supported:
		iord	ep0_mode
		mov	A, STALL_IN_OUT					; send a stall to indicate that the request
		iowr	ep0_mode					; is not supported
		pop	A
		pop	X
		reti


;**********************************************************

	;**********************************
	; IN - CONTROL READ DATA STAGE
	;	 - CONTROL WRITE STATUS STAGE
	;	 - NO DATA CONTROL STATUS STAGE

	ep0_in_received:
	mov		A, [ep0_transtype]
	jacc	ep0_in_jumptable


	;**********************************

	control_read_data_stage:

		mov		X, 00h

		mov		A, [maximum_data_count]
		cmp		A, 00h						; has all been sent
		jz		dmabuffer_load_done			

		dmabuffer_load:
			mov		A, X					; check if 8 byte ep0 dma
			cmp		A, 08h					; buffer is full
			jz		dmabuffer_load_done
			mov		A, [data_start]			; read data from desc. table
			index	control_read_table
			mov		[X + ep0_dmabuff0], A

			inc		X						; increment buffer offset
			inc		[data_start]			; increment descriptor table pointer
			dec		[maximum_data_count]	; decrement number of bytes requested
			jz		dmabuffer_load_done
			jmp		dmabuffer_load			; loop to load more data
			dmabuffer_load_done:
	
		iord	ep0_count					; unlock counter register
		mov		A, X						; find number of bytes loaded
		or		A, [ep0_data_toggle]		; or data toggle
		iowr	ep0_count					; write ep0 count register

		mov		A, ACK_IN_STATUS_OUT		; set endpoint mode to ack next IN
		iowr	ep0_mode					; or STATUS OUT
			
		mov		A, DATA_TOGGLE				; toggle data toggle
		xor		[ep0_data_toggle], A

		pop		A
		pop		X
		reti

⌨️ 快捷键说明

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