; This module initializes the microcontroller then executes MAIN forever

;



Reset:

	MOV	SP, #235		; Initialize the Stack at top of internal memory

	MOV	PageReg, #7FH		; Needed to use MOVX @Ri



	MOV	R0, #LOW(USBControl)	; Simulate a disconnect

	MOVX	A, @R0

	ANL	A, #11110011b		; Clear DISCON, DISCOE

	MOVX	@R0, A

	CALL	Wait100msec   		; Give the host time to react

	MOVX	A, @R0			; Reconnect with this new identity

	ORL	A, #00000110b           ; Set DISCOE to enable pullup resistor

	MOVX	@R0, A	 		; Set RENUM so that 8051 handles USB requests

	CLR	A

	MOV	FLAGS, A		; Start in Default state

InitializeIOSystem:			; This example uses only Port A output

	MOV	R0, #LOW(PortA_Config)	; PageReg = 7F = HIGH(PortA_Config)

	CLR	A

	MOVX	@R0, A			; No alternate functions

	MOV	R1, #LOW(PortA_OE)

	CPL	A			; = 0FFH

	MOVX	@R1, A			; Enable PortA for Output

InitializeInterruptSystem:		; First initialize the USB level

	MOV	R0, #LOW(IN07IEN)

	MOVX	@R0, A			; Disable interrupts from IN Endpoints 0-7

	INC	R0

	MOVX	@R0, A			; Disable interrupts from OUT Endpoints 0-7

	INC	R0

	MOV	A, #00000011b

	MOVX	@R0, A			; Enable (Resume, Suspend,) SOF and SUDAV INTs

	INC	R0

	MOV	A, #00000001b

	MOVX	@R0, A	     		; Enable Auto Vectoring for USB interrupts

	MOV	R0, #LOW(OUT07IRQ)

	MOV	A, #0FFH

	MOVX	@R0, A			; Clear out any pending interrupts

	                                ; Now enable the main level

	MOV	EIE, #00000001b		; Enable INT2 = USB Interrupt (only) 		

        MOV	EI, #11000000b		; Enable interrupt subsystem (and Ser1 for Dscope)

 

; Initialization Complete.

; 

MAIN:

	NOP				; Not much of a main loop for this example

	JMP	MAIN			; All actions are initiated by interrupts

; We are a slave, we wait to be told what to do



Wait100msec:

	MOV	Temp, #100

Wait1msec:				; A delay loop

	MOV	DPTR, #-1200		

More:	INC	DPTR			; 3 cycles

	MOV	A, DPL                  ; + 2

	ORL	A, DPH                  ; + 2

	JNZ	More                    ; + 3 = 10 cycles x 1200 = 1msec

	DJNZ	Temp, Wait1msec

	RET



ProcessOutputReport:		  	; A Report has just been received

; The report is three bytes long

; Save the values for the INTERRUPT service routine

	MOV	R0, #MotorControl	; Initialize the pointers to be used

	MOV	DPTR, #EP0OutBuffer	; Point to the Report

	MOV	R7, #3

CopyOR:	MOVX	A, @DPTR		; Retrieve Report Byte 1

	MOV	@R0, A

	INC	DPTR

	INC	R0

	DJNZ	R7, CopyOR

	RET



CreateInputReport:

; Not used in this example