PDA

View Full Version : USB Learning



ruijc
- 21st July 2014, 18:20
Hi guys,

I'm trying to write a program that could send a keystroke to the PC when a button is pressed.
I have the main program figured out...the problem is the descriptor files...:eek:
These last few days i've been reading alot of posts and examples but i always end up without a way out.

Some examples i tried do not compile ( errors ), the example tutorials created by Squibcakes use software that is not avaliable anymore...the only piece of code that worked ( also tested with hardware ) is the CDC_DEMO from the PBP folder.
I tried to modify the descriptor to act as a keyboard but i cannot understand ( not the same format as the tutorials ).

Is there anyone that could help me point to the right direction with tips or examples?

I thank in advance

Best Regards
Rui

andywpg
- 22nd July 2014, 00:12
If you find out, please share it - I started something along the same lines, and kind of got scared off by the USB part. I just want something to send one character when a button is pressed.

ruijc
- 22nd July 2014, 00:15
These are the files i'm working with ( main code and descriptor )
The descriptor is from a different thread ( http://www.picbasic.co.uk/forum/showthread.php?t=2872 )

Main:


buffer Var Byte[8]
loopcnt Var Byte
state Var Byte

DEFINE OSC 48
@ __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
@ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
@ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
@ __CONFIG _CONFIG4L, _LVP_OFF_4L & _ICPRT_OFF_4L &_XINST_OFF_4L

TRISB=%11111111

B1 VAR PORTB.0 '
B2 VAR PORTB.1 '
B3 VAR PORTB.2 '
B4 VAR PORTB.3 '
B5 VAR PORTB.4 '
B6 VAR PORTB.5 '
B7 VAR PORTB.6 '
B8 VAR PORTB.7 '


' Include "DESCKB.ASM" ' Include the HID descriptors
' INCLUDE "USB_ASM_Service.pbp" ; USB Init and Service interrupt routines
Pause 10

USBInit

buffer[0] = 0
buffer[1] = 0
buffer[2] = 0
buffer[3] = 0
buffer[4] = 0
buffer[5] = 0
buffer[6] = 0
buffer[7] = 0

MAIN:

IF B1=1 THEN

buffer[0] = 0 '1st byte = modifier keys (i.e. ctrl, alt, shift)
buffer[1] = 0 '2nd byte
buffer[2] = $14 '3rd byte = q character
buffer[3] = 0

ENDIF

IF B2=1 THEN

buffer[0] = $2 '1st byte = modifier keys (i.e. ctrl, alt, shift)
buffer[1] = 0 '2nd byte
buffer[2] = $14 '3rd byte = Q character
buffer[3] = 0

ENDIF

IF (B1=0) AND (B2=0) THEN

buffer[0] = 0
buffer[1] = 0
buffer[2] = 0
buffer[3] = 0

ENDIF

endgame:
USBService ' Must service USB regularly
USBOut 1, buffer, 8, endgame ' Send buffer to endpoint 1
while(1)
wend
Goto MAIN ' Do it forever

Descriptor:


; KBDDESC.ASM
;
; This file contains a set of descriptors for a standard keyboard.

#define EP0_BUFF_SIZE 8 ; 8, 16, 32, or 64
#define MAX_NUM_INT 1 ; For tracking Alternate Setting
#define MAX_EP_NUMBER 1 ; UEP1
#define NUM_CONFIGURATIONS 1
#define NUM_INTERFACES 1

#define MODE_PP _PPBM0
#define UCFG_VAL _PUEN|_TRINT|_FS|MODE_PP ; Full-speed
;#define UCFG_VAL _PUEN|_TRINT|MODE_PP ; Low-speed

;#define USE_SELF_POWER_SENSE_IO
;#define USE_USB_BUS_SENSE_IO

; DEVICE CLASS USAGE
#define USB_USE_HID

; HID
; Endpoints Allocation
#define HID_INTF_ID 0x00
#define HID_UEP UEP1
#define HID_BD_OUT ep1Bo
#define HID_INT_OUT_EP_SIZE 8
#define HID_BD_IN ep1Bi
#define HID_INT_IN_EP_SIZE 8
#define HID_NUM_OF_DSC 1

; ************************************************** ****************
; 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.
; ************************************************** ****************
DeviceDescriptor
retlw (EndDeviceDescriptor-DeviceDescriptor)/2 ; bLength Length of this descriptor
retlw DSC_DEV ; bDescType This is a DEVICE descriptor
retlw 0x10 ; bcdUSBUSB Revision 1.10 (low byte)
retlw 0x01 ; high byte
retlw 0x00 ; bDeviceClass
retlw 0x00 ; bDeviceSubClass
retlw 0x00 ; bDeviceProtocol
retlw EP0_BUFF_SIZE ; bMaxPacketSize for EP0
retlw 0xD8 ; idVendor - 0x04D8 is Microchip Vendor ID (low byte)
retlw 0x04 ; (high byte)
retlw 0x02 ; idProduct (low byte)
retlw 0x00 ; (high byte)
retlw 0x01 ; bcdDevice (low byte)
retlw 0x00 ; (high byte)
retlw 0x01 ; iManufacturer
retlw 0x02 ; iProduct
retlw 0x03 ; iSerialNumber
retlw NUM_CONFIGURATIONS ; bNumConfigurations
EndDeviceDescriptor

; ************************************************** ****************
; 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.
; ************************************************** ****************
; Configuration pointer table
USB_CD_Ptr
Configs
db low Config1, high Config1
db low Config1, high Config1

; Configuration Descriptor
Config1
retlw (Interface1-Config1)/2 ; bLength Length of this descriptor
retlw DSC_CFG ; bDescType2 = CONFIGURATION
Config1Len
retlw low ((EndConfig1-Config1)/2) ; Length of this configuration
retlw high ((EndConfig1-Config1)/2)
retlw 0x01 ; bNumInterfaces Number of interfaces
retlw 0x01 ; bConfigValue Configuration Value
retlw 0x04 ; iConfigString Index for this config = #01
retlw 0xA0 ; bmAttributes attributes - bus powered
retlw 0x50 ; Max power consumption (2X mA)
Interface1
retlw (HIDDescriptor1-Interface1)/2 ; length of descriptor
retlw DSC_INTF
retlw 0x00 ; number of interface, 0 based array
retlw 0x00 ; alternate setting
retlw 0x01 ; number of endpoints used in this interface
retlw 0x03 ; interface class - assigned by the USB
retlw 0x01 ; boot device
retlw 0x01 ; interface protocol - keyboard
retlw 0x05 ; index to string descriptor that describes this interface
HIDDescriptor1
retlw (Endpoint1-HIDDescriptor1)/2 ; descriptor size (9 bytes)
retlw 0x21 ; descriptor type (HID)
retlw 0x01 ; HID class release number (1.00)
retlw 0x01
retlw 0x00 ; Localized country code (none)
retlw 0x01 ; # of HID class descriptor to follow (1)
retlw 0x22 ; Report descriptor type (HID)
ReportDescriptor1Len
retlw low ((EndReportDescriptor1-ReportDescriptor1)/2)
retlw high ((EndReportDescriptor1-ReportDescriptor1)/2)
Endpoint1
retlw (EndConfig1-Endpoint1)/2 ; length of descriptor
retlw DSC_EP
retlw 0x81 ; EP1, In
retlw 0x03 ; Interrupt
retlw 0x08 ; This should be the size of the endpoint buffer
retlw 0x00
retlw 0x0A ; polling interval (10ms)
EndConfig1

ReportDescriptor1
retlw 0x05
retlw 0x01 ; usage page (generic desktop)
retlw 0x09
retlw 0x06 ; usage (keyboard)
retlw 0xA1
retlw 0x01 ; collection (application)
retlw 0x05
retlw 0x07 ; usage (Key codes)
retlw 0x19
retlw 0xE0 ; usage minimum (224)
retlw 0x29
retlw 0xE7 ; usage minimum (231)
retlw 0x15
retlw 0x00 ; logical minimum (0)
retlw 0x25
retlw 0x01 ; logical maximum (1)
retlw 0x75
retlw 0x01 ; report size (1)
retlw 0x95
retlw 0x08 ; report count (8)
retlw 0x81
retlw 0x02 ; Input (data,variable,absolute)
retlw 0x95
retlw 0x01 ; report count (1)
retlw 0x75
retlw 0x08 ; report size (8)
retlw 0x81
retlw 0x01 ; Input (constant)
retlw 0x95
retlw 0x05 ; report count (5)
retlw 0x75
retlw 0x01 ; report size (1)
retlw 0x05
retlw 0x08 ; usage page (page# for leds)
retlw 0x19
retlw 0x01 ; Usage minimum (1)
retlw 0x29
retlw 0x05 ; Usage maximum (5)
retlw 0x91
retlw 0x02 ; output (data,variable,absolute)
retlw 0x95
retlw 0x01 ; report count (1)
retlw 0x75
retlw 0x03 ; report size (3)
retlw 0x91
retlw 0x01 ; output (constant)
retlw 0x95
retlw 0x06 ; report count (6)
retlw 0x75
retlw 0x08 ; report size (8)
retlw 0x15
retlw 0x00 ; logical minimum (0)
retlw 0x25
retlw 0x65 ; logical maximum (101)
retlw 0x05
retlw 0x07 ; usage page (keycodes)
retlw 0x19
retlw 0x00 ; usage minimum (0)
retlw 0x29
retlw 0x65 ; usage maximum (101)
retlw 0x81
retlw 0x00 ; input (data, array)
retlw 0xC0 ; end collection
EndReportDescriptor1


langids
retlw low lang_1
retlw high lang_1
retlw low lang_2 ; String indexes of different languages
retlw high lang_2

lang_1 ; English
retlw low String0 ; LangIDs
retlw high String0
retlw low String1_l1
retlw high String1_l1
retlw low String2_l1
retlw high String2_l1
retlw low String3_l1
retlw high String3_l1
retlw low String4_l1
retlw high String4_l1
retlw low String5_l1
retlw high String5_l1

lang_2
retlw low String0 ; Also point to LangID
retlw high String0
retlw low String1_l2
retlw high String1_l2
retlw low String2_l2
retlw high String2_l2
retlw low String3_l2
retlw high String3_l2
retlw low String4_l2
retlw high String4_l2
retlw low String5_l2
retlw high String5_l2

; String pointer table
USB_SD_Ptr
Strings
db low String0, high String0
db low String1_l1, high String1_l1
db low String2_l1, high String2_l1
db low String3_l1, high String3_l1
db low String4_l1, high String4_l1
db low String5_l1, high String5_l1

String0
retlw (String1_l1-String0)/2 ; Length of string
retlw DSC_STR ; Descriptor type 3
retlw 0x09 ; Language ID (as defined by MS 0x0409)
retlw 0x04
retlw 0x04 ; Some other language ID for testing
retlw 0x08

String1_l1
retlw (String2_l1-String1_l1)/2 ; Length of string
retlw DSC_STR ; Descriptor type 3
retlw 'L'
retlw 0x00
retlw 'o'
retlw 0x00
retlw 'r'
retlw 0x00
retlw 'd'
retlw 0x00
retlw ' '
retlw 0x00
retlw 'N'
retlw 0x00
retlw 'i'
retlw 0x00
retlw 'k'
retlw 0x00
retlw 'o'
retlw 0x00
retlw 'n'
retlw 0x00
String2_l1
retlw (String3_l1-String2_l1)/2
retlw DSC_STR
retlw 'K'
retlw 0x00
retlw 'B'
retlw 0x00
retlw 'DX'
retlw 0x00
retlw ' '
retlw 0x00
retlw 'E'
retlw 0x00
retlw 'N'
retlw 0x00
retlw 'C'
retlw 0x00
retlw 'O'
retlw 0x00
retlw 'D'
retlw 0x00
retlw 'E'
retlw 0x00
retlw 'R'
retlw 0x00
String3_l1
retlw (String4_l1-String3_l1)/2
retlw DSC_STR
retlw 'V'
retlw 0x00
retlw '1'
retlw 0x00
retlw '.'
retlw 0x00
retlw '1'
retlw 0x00
retlw '0'
retlw 0x00
retlw 'F'
retlw 0x00
retlw 'S'
retlw 0x00
String4_l1
retlw (String5_l1-String4_l1)/2
retlw DSC_STR
retlw 'C'
retlw 0x00
retlw 'f'
retlw 0x00
retlw 'g'
retlw 0x00
retlw '1'
retlw 0x00
String5_l1
retlw (String6_l1-String5_l1)/2
retlw DSC_STR
retlw 'E'
retlw 0x00
retlw 'P'
retlw 0x00
retlw '1'
retlw 0x00
retlw 'I'
retlw 0x00
retlw 'n'
retlw 0x00
String6_l1

String1_l2 ; Language 2, Chinese. Strings can be totally different than english
retlw (String2_l2-String1_l2)/2 ; Length of string
retlw DSC_STR ; Descriptor type 3
retlw 'L'
retlw 0x00
retlw 'o'
retlw 0x00
retlw 'r'
retlw 0x00
retlw 'd'
retlw 0x00
retlw ' '
retlw 0x00
retlw 'N'
retlw 0x00
retlw 'i'
retlw 0x00
retlw 'k'
retlw 0x00
retlw 'o'
retlw 0x00
retlw 'n'
retlw 0x00
String2_l2
retlw (String3_l2-String2_l2)/2
retlw DSC_STR
retlw 'K'
retlw 0x00
retlw 'B'
retlw 0x00
retlw 'D'
retlw 0x00
retlw ' '
retlw 0x00
retlw 'E'
retlw 0x00
retlw 'N'
retlw 0x00
retlw 'C'
retlw 0x00
retlw 'O'
retlw 0x00
retlw 'D'
retlw 0x00
retlw 'E'
retlw 0x00
retlw 'R'
retlw 0x00
String3_l2
retlw (String4_l2-String3_l2)/2
retlw DSC_STR
retlw 'V'
retlw 0x00
retlw '1'
retlw 0x00
retlw '.'
retlw 0x00
retlw '1'
retlw 0x00
retlw 'F'
retlw 0x00
retlw 'S'
retlw 0x00
String4_l2
retlw (String5_l2-String4_l2)/2
retlw DSC_STR
retlw 'C'
retlw 0x00
retlw 'f'
retlw 0x00
retlw 'g'
retlw 0x00
retlw '1'
retlw 0x00
String5_l2
retlw (String6_l2-String5_l2)/2
retlw DSC_STR
retlw 'E'
retlw 0x00
retlw 'P'
retlw 0x00
retlw '1'
retlw 0x00
retlw 'I'
retlw 0x00
retlw 'n'
retlw 0x00
String6_l2


The mcu is a 18F2550.
When trying to compile i get alot of errors and i dont undestand why :mad:

towlerg
- 22nd July 2014, 17:17
This NOT PicBasic nor does it use a Pic but it does do what you want and since source is available, you should be able to extract the
descriptors - http://blog.flipwork.nl/?x=entry:entry081009-142605.

One common piece of advise when dealing with descriptors, is to take a working example and make incrementally changes, testing with
each iteration.

George

ruijc
- 22nd July 2014, 18:43
Hello George,

Thank you so much for your help :).

You are right when you say to get a working example and do some changes.
That is what i'm trying to get...a working example.

All the examples i get here do not compile and i dont know why.
The one i posted ( descriptor ) is from this forum and it was suposed to be working but it does not compile :mad:

If someone would explain me why this does not compile maybe i would be able to fix it and move to the next stage.

Regards
Rui

AvionicsMaster1
- 22nd July 2014, 20:07
Any chance you could post the errors? Some of the people around here can often narrow the issue when seeing those.

ruijc
- 22nd July 2014, 20:49
Greetings,

Thanks...you are right...

If i compile with the descriptors commented i get these:


' Include "DESCKB.ASM" ' Include the HID descriptors

7400

If i add the descriptor include it will open the desckb.asm file and outline in red lines in it.

Regards
Rui

ruijc
- 22nd July 2014, 20:54
Also...

I'm using these 2 files + all of the files inside the USB18 folder from PBP.

Regards
Rui

towlerg
- 23rd July 2014, 03:01
As far as I know all the samples in ..\USB18 will compile (I just tried a couple to confirm). One thing that often goes wrong when I use PicBasic is that I forget to set the correct microcontroller.



You are right when you say to get a working example and do some changes.
That is what i'm trying to get...a working example.

The point is that you start with working descriptors.

George

ruijc
- 23rd July 2014, 14:55
Hi there,


One thing that often goes wrong when I use PicBasic is that I forget to set the correct microcontroller.

Not in this case...i have the correct microcontroller selected.


The point is that you start with working descriptors.

Exactly, like i said in my first post.
My problem with the PBP examples is that i'm having problems correlating them with the other examples posted all over the forum ( that dont compile here ) and therefore make the necessary changes to keyboard.

Too bad PBP does not have keyboard demos :mad:

pedja089
- 23rd July 2014, 16:56
There is example...
7401

pedja089
- 23rd July 2014, 16:56
There is example...
7401

towlerg
- 23rd July 2014, 18:08
Hi


Exactly, like i said in my first post.
My problem with the PBP examples is that i'm having problems correlating them with the other examples posted all over the forum ( that dont compile here ) and therefore make the necessary changes to keyboard.

Sorry, forgot that PicBasic didn't have a keyboard example.

Let's just hope that some kind soul has a working HID Keyboard descriptor.

This is a long shot but in the USB readme it says that the descriptors for the samples are derived from the original Microchip Solutions package. I've attached the keyboard and mouse descriptors from that package (you can download them yourself from Microchip's site). Using the PicBasic mouse descriptor you have compared to the Microchip version and hopefully understand how to convert the Microchip keyboard descriptor.

George

7403 7404

towlerg
- 23rd July 2014, 18:32
Please disregard my last post.

From forum search there appear to be several examples of working keyboard descriptors. For example http://www.picbasic.co.uk/forum/showthread.php?t=2872 post #2

ruijc
- 23rd July 2014, 19:45
Greetings guys,

@ towlerg,


From forum search there appear to be several examples of working keyboard descriptors. For example http://www.picbasic.co.uk/forum/showthread.php?t=2872 post #2

I did checked them...in fact, the descriptor i'm using is the one from the link you posted...but like i said...they dont compile :confused:

@pedja089,

Thanks alot for the files. I opened the main file and just tried to compile...no success :confused:
I get alot of errors when compiling....

7405

I'm very confused !!!
These should compile...why cant i compile ??

Regards
Rui

pedja089
- 23rd July 2014, 22:53
Add DT_INTS-18 files.
http://darreltaylor.com/DT_INTS-18/home.html
I downloaded that example few years ago from forum, and I'm sure that it worked with PBP2.50.

ruijc
- 24th July 2014, 00:01
Hi pedja089,

you are right...it did compile fine :D thanks...

However...it's not being recognized with windows XP :mad:

Damn...i have no luck with USB stuff !!!!
If i code the pbp demo it recognizes, this one does not...:confused:

pedja089
- 24th July 2014, 00:07
You probably need to set fuses according to your board...
EDIT:
Copy fuses from demo to keyboard...

ruijc
- 24th July 2014, 00:22
Did that :( didnt worked

ruijc
- 26th July 2014, 22:09
Well,

after burning my eyes out until now trying to get this thing working i'm just about to give up !!!! :mad:

I made some small changes to my main code and tryed many descriptors and configurations.
All descriptors in this forum, or dont compile, or are not accepted by the PC, etc.

I was able to modify some so that they would compile and accept by the PC as keyboard but i got strange results...

With the same main code, some descriptors jam my PC keyboard inputs like one of the keys got stucked ( mostly control key or Alt Gr ). Disconnecting the circuit dos not solve...only a restart.
I also noticed that if i changed the value from "usage minimum" retlw 0x19/retlw 0xE0 to retlw 0x19/retlw 0x04 i stopped getting the key stucked simptom.
Weird is that most keyboard descriptor examples use E0 value :confused:

Anyway...

With the 04 value, when turning the circuit on i get a group of characters non stop "cdefh".
A second descriptor with also the 04 value sends out "CDEH37W*" non stop.

They both send characters even without any button being pressed...and with button pressed the output has the same characters.

Again, here are both main code and descriptor


'************************************************* ***************
'* Name : HID_USB_Keypad.BAS *
'* Author : Peter MacLeod *
'* Date : 01/30/2011 *
'* Version : 1.01 *
'* Notes : *
'************************************************* ***************

'@ CONFIG PLLDIV = 12 ;1=4MHz, 2=8MHz, 3=12MHz, 4=16MHz, 5=20MHz, 6=24MHz, 10=40MHz, 12=48MHz
'@ CONFIG FOSC = ECIO_EC ;XT_XT, XTPLL_XT, HS, HSPLL_HS, ECIO_EC

@ __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
@ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
'@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
'@ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H

'OSCTUNE.6 = 1 ' Enable PLL for 18F87J50 family

DEFINE OSC 48

INCLUDE "KYPD_DESC.pbp"
'include "KBDDESC.pbp"
'include "DESCUSBProject.bas"
'include "KBDDESCMOJA.bas"

'DEFINE LOADER_USED 1
'DEFINE RESET_ORG 1100h
'DEFINE INTERRUPT_ORG 1108h

'ASM
'INT_LIST macro ; IntSource, Label, Type, ResetFlag?
' INT_Handler USB_INT, _SERVICE_USB, ASM, yes
' ;INT_Handler RBC_INT, _SERVICE_KYPD, PBP, yes
' endm
' INT_CREATE ; Creates the interrupt processor
'ENDASM

'-------------------------------------------------------------------------------
' System Settings
'-------------------------------------------------------------------------------
i VAR BYTE
BufferOut Var Byte[8]

ADCON1 = 15 ' Set all I/Os to Digital
INTCON2.7 = 1 ' Disable PortB Pullups
CMCON = 7 ' Disable Comparators
'-------------------------------------------------------------------------------
' Keypad Settings
'-------------------------------------------------------------------------------
TRISB = %01111111 'Set PORTB as inputs for keypad

Keypad_Debounce CON 60
KeyPress VAR BYTE
KeyPress_Old VAR BYTE

Keypad_Prev VAR PORTB.0
Keypad_Next VAR PORTB.1
Keypad_Play VAR PORTB.2
Keypad_Stop VAR PORTB.3
Keypad_Eject VAR PORTB.4

Keypad_Mute VAR PORTB.5
Keypad_VolUp VAR PORTB.6
led VAR PORTB.7

Keypress = 0
'KeyPress_Old = 0
'-------------------------------------------------------------------------------
'START USB AND INTERRUPTS
'-------------------------------------------------------------------------------
PAUSE 500 'TIME TO SETTLE
USBINIT 'INITIALIZE USB
USBSERVICE 'SERVICE USB
'UIE = $7F 'ENABLE USB INTERRUPTS
'UEIE = $9F 'ENABLE USB ERROR INTERRUPTS
'@ INT_ENABLE USB_INT
;@ INT_ENABLE RBC_INT
'-------------------------------------------------------------------------------

BufferOut[0] = 0
BufferOut[1] = 0
BufferOut[2] = 0
BufferOut[3] = 0
BufferOut[4] = 0
BufferOut[5] = 0
BufferOut[6] = 0
BufferOut[7] = 0

low led
pause 100
high led
pause 2500
low led
pause 100

'-------------------------------------------------------------------------------
' Main Subroutine
'-------------------------------------------------------------------------------
Main:

if Keypad_Prev=1 then

BufferOut[0] = 0
BufferOut[1] = 0
BufferOut[2] = $15
BufferOut[3] = 0

else

BufferOut[0] = 0
BufferOut[1] = 0
BufferOut[2] = 0
BufferOut[3] = 0

endif

gosub SERVICE_USB
GOSUB DoUSBOut

GOTO Main
'-------------------------------------------------------------------------------
' Transmit data on the USB bus
'-------------------------------------------------------------------------------
DoUSBOut:
USBService
USBOut 1, BufferOut, 8, DoUSBOut
RETURN
'-------------------------------------------------------------------------------
SERVICE_USB:
USBSERVICE
'@ INT_RETURN
return


'************************************************* ***************
'* Name : KYPD_DESC.BAS *
'* Author : Peter MacLeod *
'* Date : 25/07/2014 *
'* Version : 1.01 *
'* Notes : USB HID CNC Keypad Descriptor *
'************************************************* ***************

'USBMEMORYADDRESS Con $400 ' USB RAM starts here (set in device header file)
USBMEMORYSIZE Con 256 ' USB RAM size in bytes
USBReservedMemory Var Byte[USBMEMORYSIZE] USBMEMORYADDRESS ' Reserve memory used by USB assembler code

Goto kypd_desc_end ' Skip over all of the USB assembler code
Asm

#define USB_EP0_BUFF_SIZE 8 ; 8, 16, 32, or 64
#define USB_MAX_NUM_INT 1
#define USB_MAX_EP_NUMBER 1
#define NUM_CONFIGURATIONS 1
#define NUM_INTERFACES 1

#define UCFG_VAL USB_PULLUP_ENABLE|USB_INTERNAL_TRANSCEIVER|USB_FUL L_SPEED|USB_PING_PONG__NO_PING_PONG

#define USB_POLLING
#define HID_INTF_ID 0x00
#define HID_EP 1
#define HID_INT_OUT_EP_SIZE 1
#define HID_INT_IN_EP_SIZE 1
#define HID_NUM_OF_DSC 1

include "usb_hid.asm" ; Include rest of USB files, starting with HID class code

DeviceDescriptor
retlw (EndDeviceDescriptor-DeviceDescriptor)/2 ; Length of this descriptor
retlw USB_DESCRIPTOR_DEVICE ; DEVICE descriptor type
retlw 0x10 ; USB Spec Release Number (low)
retlw 0x01 ; (high)
retlw 0x00 ; Class Code
retlw 0x00 ; Subclass code
retlw 0x00 ; Protocol code
retlw USB_EP0_BUFF_SIZE ; Max packet size for EP0
retlw 0x81 ; Vendor ID (low byte)
retlw 0x17 ; (high byte)
retlw 0xD1 ; Product ID (low byte)
retlw 0x07 ; (high byte)
;retlw 0x6F ; Product ID (low byte)
;retlw 0x09 ; (high byte)
retlw 0x01 ; Device release number (low)
retlw 0x00 ; (high)
retlw 0x01 ; Manufacturer string index
retlw 0x02 ; Product string index
retlw 0x00 ; Device serial number string index
retlw NUM_CONFIGURATIONS ; Number of possible configurations
EndDeviceDescriptor

USB_CD_Ptr
Configs
db low Config1, high Config1
db upper Config1, 0

Config1
retlw (Interface1-Config1)/2 ; Size of this descriptor in bytes
retlw USB_DESCRIPTOR_CONFIGURATION ; CONFIGURATION descriptor type
Config1Len
retlw low ((EndConfig1 - Config1)/2) ; Length of this configuration (low)
retlw high ((EndConfig1 - Config1)/2) ; (high)
retlw NUM_INTERFACES ; Number of interfaces in this cfg
retlw 0x01 ; Index value of this configuration
retlw 0x00 ; Configuration string index
retlw _DEFAULT|_SELF ; Attributes - bus powered
retlw 0x32 ; Max power consumption (2X mA)
Interface1
retlw (HIDDescriptor1-Interface1)/2 ; Size of this descriptor in bytes
retlw USB_DESCRIPTOR_INTERFACE ; INTERFACE descriptor type
retlw 0x00 ; Interface Number
retlw 0x00 ; Alternate Setting Number
retlw 0x01 ; Number of endpoints in this interface
retlw HID_INTF ; Class code
retlw BOOT_INTF_SUBCLASS ; Subclass code
retlw 0x00 ; Protocol code
retlw 0x00 ; Interface string index
HIDDescriptor1
retlw (Endpoint1In-HIDDescriptor1)/2 ; Size of this descriptor in bytes
retlw DSC_HID ; HID descriptor type
retlw 0x11 ; HID Spec Release Number in BCD format - 1.11 (low byte)
retlw 0x01 ; (high byte)
retlw 0x00 ; Country Code (0x00 for Not supported)
retlw HID_NUM_OF_DSC ; Number of class descriptors
retlw DSC_RPT ; Report descriptor type (HID)
ReportDescriptor1Len
retlw low ((EndReportDescriptor1-ReportDescriptor1)/2)
retlw high ((EndReportDescriptor1-ReportDescriptor1)/2)
Endpoint1In
retlw (EndConfig1-Endpoint1In)/2 ; length of descriptor
retlw USB_DESCRIPTOR_ENDPOINT ; ENDPOINT descriptor type
retlw HID_EP|_EP_IN ; EP1, In
retlw _INT ; Interrupt
retlw low (HID_INT_IN_EP_SIZE) ; This should be the size of the endpoint buffer
retlw high (HID_INT_IN_EP_SIZE)
retlw 0x0A ; polling interval (10ms)
EndConfig1


ENDASM
ASM


ReportDescriptor1

retlw 0x05
retlw 0x01 ; usage page (generic desktop)
retlw 0x09
retlw 0x06 ; usage (keyboard)
retlw 0xA1
retlw 0x01 ; collection (application)
retlw 0x05
retlw 0x07 ; usage (Key codes)
retlw 0x19
retlw 0x04 ; usage minimum (224) e0 04
retlw 0x29
retlw 0xE7 ; usage minimum (231)
retlw 0x15
retlw 0x00 ; logical minimum (0)
retlw 0x25
retlw 0x01 ; logical maximum (1)
retlw 0x75
retlw 0x01 ; report size (1)
retlw 0x95
retlw 0x08 ; report count (8)
retlw 0x81
retlw 0x02 ; Input (data,variable,absolute)
retlw 0x95
retlw 0x01 ; report count (1)
retlw 0x75
retlw 0x08 ; report size (8)
retlw 0x81
retlw 0x01 ; Input (constant)
retlw 0x95
retlw 0x05 ; report count (5)
retlw 0x75
retlw 0x01 ; report size (1)
retlw 0x05
retlw 0x08 ; usage page (page# for leds)
retlw 0x19
retlw 0x01 ; Usage minimum (1)
retlw 0x29
retlw 0x05 ; Usage maximum (5)
retlw 0x91
retlw 0x02 ; output (data,variable,absolute)
retlw 0x95
retlw 0x01 ; report count (1)
retlw 0x75
retlw 0x03 ; report size (3)
retlw 0x91
retlw 0x01 ; output (constant)
retlw 0x95
retlw 0x06 ; report count (6)
retlw 0x75
retlw 0x08 ; report size (8)
retlw 0x15
retlw 0x00 ; logical minimum (0)
retlw 0x25
retlw 0x65 ; logical maximum (101)
retlw 0x05
retlw 0x07 ; usage page (keycodes)
retlw 0x19
retlw 0x00 ; usage minimum (0)
retlw 0x29
retlw 0x65 ; usage maximum (101)
retlw 0x81
retlw 0x00 ; input (data, array)
retlw 0xC0 ; end collection


EndReportDescriptor1

USB_SD_Ptr
Strings
db low String0, high String0
db upper String0, 0
db low String1, high String1
db upper String1, 0
db low String2, high String2
db upper String2, 0

String0
retlw (String1-String0)/2 ; Size of this descriptor in bytes
retlw USB_DESCRIPTOR_STRING ; STRING descriptor type
retlw 0x09 ; Language ID as defined by MS - 0x0409 (low byte)
retlw 0x04 ; (high byte)

String1
retlw (String2-String1)/2
retlw USB_DESCRIPTOR_STRING
retlw 'R'
retlw 0x00
retlw 'J'
retlw 0x00
retlw 'C'
retlw 0x00
retlw ' '
retlw 0x00
retlw 'E'
retlw 0x00
retlw 'L'
retlw 0x00
retlw 'E'
retlw 0x00
retlw 'C'
retlw 0x00
retlw 'T'
retlw 0x00
retlw 'R'
retlw 0x00
retlw 'O'
retlw 0x00
retlw 'N'
retlw 0x00
retlw 'I'
retlw 0x00
retlw 'C'
retlw 0x00

String2
retlw (EndStringDescriptors-String2)/2
retlw USB_DESCRIPTOR_STRING
retlw 'H'
retlw 0x00
retlw 'I'
retlw 0x00
retlw 'D'
retlw 0x00
retlw ' '
retlw 0x00
retlw 'K'
retlw 0x00
retlw 'E'
retlw 0x00
retlw 'Y'
retlw 0x00
retlw ' '
retlw 0x00
retlw ' '
retlw 0x00
retlw 'T'
retlw 0x00
retlw 'E'
retlw 0x00
retlw 'S'
retlw 0x00
retlw 'T'
retlw 0x00
retlw 'E'
retlw 0x00
retlw 'R'
retlw 0x00
retlw ' '
retlw 0x00
retlw ' '
retlw 0x00
retlw ' '
retlw 0x00
retlw ' '
retlw 0x00
retlw '0'
retlw 0x00
retlw '1'
retlw 0x00
EndStringDescriptors

Endasm

kypd_desc_end ' End of skipped over USB assembler code

Although these are original from Peter MacLeod, they have suffered many changes in an effort of getting things to start working.