;                            Software License Agreement
;
; The software supplied herewith by Microchip Technology Incorporated (the "Company")
; for its PICmicro® Microcontroller is intended and supplied to you, the Company’s
; customer, for use solely and exclusively on Microchip PICmicro Microcontroller
; products.
;
; The software is owned by the Company and/or its supplier, and is protected under
; applicable copyright laws. All rights are reserved. Any use in violation of the
; foregoing restrictions may subject the user to criminal sanctions under applicable
; laws, as well as to civil liability for the breach of the terms and conditions of
; this license.
;
; THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, WHETHER EXPRESS,
; IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE
; COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
; CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
;
; ###############################################################################
;	filename:		USBMOUSE.ASM
;				USB Descriptors and other user modified code
;
; Implements the descriptor lookup tables and other user modified code.  
; Currently this consists of SetConfiguration, which is called when the
; host sends a Set Configuration command.  SetConfiguration gives meaning
; to the configuration.
;
; ###############################################################################
;
;    	Author:			Dan Butler
;    	Company:		Microchip Technology Inc
;
;	Revision:		1.10
;	Date:			07-December-2000
;	Assembled using		MPASM 2.30.07
;	Change log:
;	24 August 2000  DZB  Changed descriptor tables to 16 bit pointers
;
;################################################################################
;
;	include files:
;		P16C765.inc	Rev 1.00
;		usb_defs.inc	Rev 1.00
;
;################################################################################
#include <p16C745.inc>
#include "usb_defs.inc"


descriptors	code
	global	Config_desc_index
	global	Descriptions
	global	string_index
	global	StringDescriptors
	global	ReportDescriptor
	global	ReportDescriptorStart
	global	ReportDescriptorEnd
	global	HIDDescriptorStart
	global	HIDDescriptorEnd

	extern	EP0_start


; ******************************************************************
; Given a configuration descriptor index, returns the beginning address
; of the descriptor within the descriptions table
; ******************************************************************
	extern 	temp	; temp var used in get config index
Config_desc_index
	movwf	temp
	movlw	HIGH CDI_start
	movwf	PCLATH
	movlw	low  CDI_start
	addwf	temp,w	
	btfsc	STATUS,C
	incf	PCLATH,f
	movwf	PCL
CDI_start	; this table calculates the offsets for each configuration
	retlw	low  (Config1-StartDevDescr)	; descriptor from the beginning
	retlw	high (Config1-StartDevDescr)	; of the table, effectively
	retlw	low  (Config2-StartDevDescr)	; adjusting the pointers anytime
	retlw	high (Config2-StartDevDescr)	; the descriptors change.
	retlw	low  (Config3-StartDevDescr)
	retlw	high (Config3-StartDevDescr)
	retlw	low  (Config4-StartDevDescr)
	retlw	high (Config4-StartDevDescr)
; extend table here as needed.  Currently only Config 1 is implemented


; ******************************************************************
; This table is polled by the host immediately after USB Reset has been released.
; This table defines the maximum packet size EP0 can take.
; See section 9.6.1 of the Rev 1.0 USB specification.
; These fields are application DEPENDENT. Modify these to meet
; your specifications.
; the offset is passed in P0 and P1 (P0 is low order byte).
; ******************************************************************
Descriptions
	movlw	high   StartDevDescr
	addwf	EP0_start+1,w			; set up PCLATCH
	movwf	PCLATH
	movlw	low    StartDevDescr	; add offset to start of table
	addwf	EP0_start,w			
	btfsc	STATUS,C		; did it overflow?
	incf	PCLATH,f		; yes, bump PCLATH
	movwf	PCL			; adjust PCL
StartDevDescr
	retlw	.18	; bLength	Length of this descriptor
	retlw	1	; bDescType 	This is a DEVICE descriptor
	retlw	0;.10	; bcdUSB	USB revision 1.10 (low byte)
	retlw	1	; high byte
	retlw	0	; bDeviceClass	zero means each interface operates independently
	retlw	0	; bDeviceSubClass
	retlw	0	; bDeviceProtocol
	retlw	0x08	; bMaxPacketSize0 - inited in UsbInit()
	retlw	0x61	; idVendor - 0x0461
	retlw	0x04	; idVendor high order byte
	retlw	0x01	; idProduct - 0x0001
	retlw	0x00	; idProduct high order byte
	retlw	0x01	; bcdDevice - 0x0001
	retlw	0x00	; bcdDevice high order byte
	retlw	0;0x01	; iManufacturer
	retlw	0;0x02	; iProduct
	retlw	0x0	; iSerialNumber - 3
	retlw	NUM_CONFIGURATIONS	; bNumConfigurations

; ******************************************************************
; This table is retrieved by the host after the address has been set.
; This table defines the configurations available for the device.
; See section 9.6.2 of the Rev 1.0 USB specification (page 184).
; These fields are application DEPENDENT. 
; Modify these to meet your specifications.
; ******************************************************************
Config1
	retlw	9	; bLength	Length of this descriptor
	retlw	2	; bDescType	2=CONFIGURATION

	retlw	EndConfig1 - Config1
	retlw	0
	retlw	1	; bNumInterfaces	Number of interfaces

;USB_CONFIG_VAL
	retlw	0x01	; bConfigValue	Configuration Value
	retlw	0;0x04	; iConfig	String Index for this config = #01
	retlw	0xA0	; bmAttributes	attributes - bus powered
	retlw	0x32	; MaxPower	self-powered draws 0 mA from the bus.
Interface1
	retlw	9	; length of descriptor
	retlw	INTERFACE;
	retlw	0	; number of interface, 0 based array
	retlw	0	; alternate setting
	retlw	1	; number of endpoints used in this interface
	retlw	3	; interface class - assigned by the USB
	retlw	0	; boot device
	retlw	0;2	; interface protocol - mouse
	retlw 	0;5	; index to string descriptor that describes this interface
HID_Descriptor
        retlw	09h     ; descriptor size (9 bytes)
        retlw	21h     ; descriptor type (HID)
        retlw	00h
	retlw	01h     ; HID class release number (1.00)
        retlw	00h     ; Localized country code (none)
        retlw	01h     ; # of HID class descriptor to follow (1)
        retlw	22h     ; Report descriptor type (HID)
        retlw	(end_ReportDescriptor - ReportDescriptor)
        retlw	00h
Endpoint1
	retlw	7	; length of descriptor
	retlw	ENDPOINT
	retlw	0x81	; EP1, In
	retlw	3	; Interrupt
	retlw	4	; max packet size (4 bytes) low order byte
	retlw	0	; max packet size (4 bytes) high order byte
	retlw	1	; polling interval (1ms)
EndConfig1
Config2
Config3
Config4
ReportDescriptorStart
	retlw	ReportDescriptor-StartDevDescr
ReportDescriptorEnd
	retlw	end_ReportDescriptor-StartDevDescr
HIDDescriptorStart
	retlw	HID_Descriptor-StartDevDescr
HIDDescriptorEnd
	retlw	Endpoint1-HID_Descriptor

ReportDescriptor
        retlw	0x05
	retlw	0x01        ; usage page (generic desktop)
        retlw	0x09
	retlw	0x05        ; usage (game pad)
        retlw	0xA1
	retlw	0x01        ; collection (application)
        retlw	0x05
	retlw	0x09        ; usage page (buttons)
        retlw	0x19
	retlw	0x0A        ; usage minimum (10)
        retlw	0x29
	retlw	0x29        ; usage maximum (41)
        retlw	0x15
	retlw	0x00        ; logical minimum (0)
        retlw	0x25
	retlw	0x01        ; logical maximum (1)
        retlw	0x95
	retlw	0x20        ; report count (48 bits)
        retlw	0x75
	retlw	0x01        ; report size (1)
        retlw	0x81
	retlw	0x02        ; input (48 button bits)
	retlw	0xC0        ; end collection
end_ReportDescriptor

; ******************************************************************
; Given a configuration descriptor index, returns the beginning address
; of the descriptor within the descriptions table
; ******************************************************************
string_index
	movwf	temp
	movlw	high StringIndex
	movwf	PCLATH
	movlw	low  StringIndex
	addwf	temp,w	
	btfsc	STATUS,C
	incf	PCLATH,f
	movwf	PCL
StringIndex
	retlw	low  (String0-String0)
	retlw	high (String0-String0)
	retlw	low  (String1-String0)
	retlw	high (String1-String0)
	retlw	low  (String2-String0)
	retlw	high (String2-String0)
	retlw	low  (String3-String0)
	retlw	high (String3-String0)
	retlw	low  (String4-String0)
	retlw	high (String4-String0)
	retlw	low  (String5-String0)
	retlw	high (String5-String0)

StringDescriptors
	movlw	high   String0
	addwf	EP0_start+1,w		; set up PCLATCH
	movwf	PCLATH
	movlw	low    String0		; add offset to start of table
	addwf	EP0_start,w			
	btfsc	STATUS,C		; did it overflow?
	incf	PCLATH,f		; yes, bump PCLATH
	movwf	PCL			; adjust PCL

String0
	retlw	0x04	; length of string 
	retlw	0x03	; descriptor type 3?
	retlw	0x09	; language ID (as defined by MS 0x0409)
	retlw	0x04
String1	;"Microchip"
	retlw	String2-String1	; length of string
	retlw	0x03	; string descriptor type 3
	retlw	'M'
	retlw	0x00
	retlw	'i'
	retlw	0x00
	retlw	'c'
	retlw	0x00
	retlw	'r'
	retlw	0x00
	retlw	'o'
	retlw	0x00
	retlw	'c'
	retlw	0x00
	retlw	'h'
	retlw	0x00
	retlw	'i'
	retlw	0x00
	retlw	'p'
	retlw	0x00
String2 ;"PIC16C765 USB Mouse"
	retlw	String3-String2
	retlw	0x03
	retlw	'P'
	retlw	0x00
	retlw	'i'
	retlw	0x00
	retlw	'c'
	retlw	0x00
	retlw	'1'
	retlw	0x00
	retlw	'6'
	retlw	0x00
	retlw	'C'
	retlw	0x00
	retlw	'7'
	retlw	0x00
	retlw	'6'
	retlw	0x00
	retlw	'5'
	retlw	0x00
	retlw 	' '
	retlw	0x00
	retlw	'U'
	retlw	0x00
	retlw	'S'
	retlw	0x00
	retlw	'B'
	retlw	0x00
	retlw	' '
	retlw	0x00
	retlw	'M'
	retlw	0x00
	retlw	'o'
	retlw	0x00
	retlw	'u'
	retlw	0x00
	retlw	's'
	retlw	0x00
	retlw	'e'
	retlw	0x00
String3 ;"Beta"
	retlw	String4-String3
	retlw	0x03
	retlw	'B'
	retlw	0x00
	retlw	'e'
	retlw	0x00
	retlw	't'
	retlw	0x00
	retlw	'a'
	retlw	0x00
String4 ;"Cfg1"
	retlw	String5-String4
	retlw	0x03
	retlw	'C'
	retlw	0x00
	retlw	'f'
	retlw	0x00
	retlw 	'g'
	retlw	0x00
	retlw 	'1'
	retlw	0x00
String5 ;"EP10In"
	retlw	String6-String5
	retlw	0x03
	retlw	'E'
	retlw	0x00
	retlw	'P'
	retlw	0x00
	retlw	'1'
	retlw	0x00
	retlw	'0'
	retlw	0x00
	retlw	'I'
	retlw	0x00
	retlw	'n'
	retlw	0x00
String6
	code
; *********************************************************************
; SetConfiguration
; 
; This function is called when the host issues a Set Configuration 
; command.  The housekeeping within USB is handled within the CH9 commands
; This function should be filled in to give meaning to the command within
; the application.
;
; SetConfiguration is called from within the ISR so this function should
; be kept as short as possible.
; *********************************************************************
SetConfiguration
	global	SetConfiguration
	return

; *********************************************************************
; Vendor Specific calls
; control is transferred here when bmRequestType bits 5 & 6 = 10 indicating
; the request is a vendor specific request.  This function then would 
; interpret the bRequest field to determine what action is required.
; The end of each vendor specific command should be terminated with a 
; return.  
; *********************************************************************
CheckVendor	
	global	CheckVendor
	return	; *** remove this line and uncomment out the remainder	

;	movlw	high VendorIndex
;	movwf	PCLATH
;	movlw	low  VendorIndex
;	addwf	BufferData+bRequest,w	; was our request Set Interface
;	btfsc	STATUS,C
;	incf	PCLATH,f
;	movwf	PCL
;	goto	...	; Vendor Specific Request 0
;	goto	...	; Vendor Specific Request 1
;	goto	...	; Vendor Specific Request 2
;	goto	...	; Vendor Specific Request 3
;	goto	...	; Vendor Specific Request 4
;	goto	...	; Vendor Specific Request 5
;	goto	...	; Vendor Specific Request 6
;	goto	...	; Vendor Specific Request 7

	end
