PDA

View Full Version : USB Keyboard



Ioannis
- 1st December 2021, 17:18
This feels like deep water to me. I 'd like to develop a small keyboard that will send to the PC specific characters when buttons are pressed.

In short it will be a stripped keyboard with much less keys (F1-F12, number 1-8 and another 8 capital letters).

Can PBP support such a function? Found for mouse but not a working keyboard emulation.

Prefer to use 4550 or 45K50 chips.

Thanks,
Ioannis

mpgmike
- 1st December 2021, 18:48
I do believe there is a sub-category in the HID USB category specific to keyboards. Each key has its own command code. If you enumerate as a keyboard, it is just using what has already been developed.

Jerson
- 2nd December 2021, 02:10
The easy part is to first verify the mouse code emulates correctly when connected to the PC. This ensures the skeleton is a working one.

The not so easy thing to do is to get the Descriptor tables for the keyboard matched to what is expected by the PC. Once you have that, it's just a matter of reading/writing to the USB system every mS or so.

Suggest you look for the 'Descriptor Tool' DT.EXE which will help you build the descriptor table. However, you will need to adjust the tables for PBP

Good luck

HenrikOlsson
- 2nd December 2021, 05:56
I have some code (based on something found on the forum I'm sure) that does USB keyboard emulation.
Letters and numbers I know works but I don't think I implemented F1-F12 etc. I'll dig it out and post it if you're interested.

aerostar
- 2nd December 2021, 10:13
Did you find this in the forum ?

http://www.picbasic.co.uk/forum/showthread.php?t=19695&highlight=USB+keyboard+emulation

Ioannis
- 2nd December 2021, 11:37
Oh, you naughty aerostar! I guess alzheimer is knocking my door...

Too much info on this USB mess... Lost the links I kept in case I needed them back.

Thanks people for the info.

Henrik, if you find your test code, please do post it. Will save me some time I am sure.

Ioannis

pedja089
- 2nd December 2021, 15:25
I made keyboard to use as shortcuts for EAGLE. Should be exactly what you need. I used it on easypic dev board. Pic should be 18F4550.
Here is complete project 9108

I created that using many examples from forum...

Ioannis
- 2nd December 2021, 19:57
Thank you very much pedja089!

Looks great and very close to what I need to do. have to add a small graphics display and test it on Win 10. Hope Win 10 do not pose any communication problem.

Really appreciated.
Ioannis

pedja089
- 2nd December 2021, 21:01
I used it on win7, and if I remember correctly on XP. It should work on win10.
You have keys.PBP, with all defined keys, also in .doc document you have codes.

Also USBSERVICE should be called every 10mS or so. I wanted to implement it with DT INT, but newer did...
It should be something like this


INCLUDE "DT_INTS-18.bas"
INCLUDE "ReEnterPBP-18.bas"

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

USBINIT 'INITIALIZE USB
USBSERVICE 'SERVICE USB
UIE = $7F 'ENABLE USB INTERRUPTS
UEIE = $9F 'ENABLE USB ERROR INTERRUPTS
@ INT_ENABLE USB_INT


SERVICE_USB:
USBSERVICE
@ INT_RETURN

This code is also from forum, but never tried it.

Ioannis
- 3rd December 2021, 08:04
Thanks for this tip also. I had in mind to make this Interrupt driven service but in a later stage.

Ioannis

Ioannis
- 18th December 2021, 21:04
Thanks to your help I am making a progress after a lot of experimenting. Sure I do not understand the whole process but anyway...

I managed to enumerate the PIC and display on an open text file a short string of characters from an 8 byte buffer.

How can this buffer made bigger? I changed these in the main program


USBBufferSizeTX con 16
USBBufferSizeRX con 16
USBBufferCount Var Byte
USBBufferIn var byte[16]
USBBufferOut Var Byte[16]


but still sends only the half buffer.

Then I changed in the hid_desc.bas file this line



retlw 0x10 ; This should be the size of the endpoint buffer


from 0x08 to 0x10 but still no joy. I attach the descriptor file.

Any idea?

Ioannis

P.S. I also changed

#define HID_INT_OUT_EP_SIZE 8

in the descriptor file to 16 but still sends 8 byte buffer...

richard
- 19th December 2021, 00:39
never tried it as hid kb but for a hid cdc anyway the data you need to send must be chunked into usb-tx buffer sized lumps
then sent a multiple usb transmissions.

what have you tried ?

pedja089
- 19th December 2021, 03:35
It is not so easy to change buffer. You must change descriptor file, not just length(If I understand usb correctly).

You can send one letter and modifier key at time.

Use this


For i = 0 To 20
lookup i,[KEY_V, KEY_A, KEY_L, KEY_U, KEY_E, KEY_ENTER, 0], USBBufferOut[2]
call DoUsbOut
IF USBBufferOut[2]=0 THEN EXIT
Next i

You can use array

For i = 0 To 20
USBBufferOut[2]= MyArray[i]
call DoUsbOut
IF USBBufferOut[2]=0 THEN EXIT
Next i

Jerson
- 19th December 2021, 03:42
If you follow the HID descriptor fields, you will notice you need to change the report count for the report which has to carry more data. The endpoint buffer is just a holding place for the in/out data that is handled by the USB interrupt. I am not very conversant with HID at the moment, but can look it up as I have worked on HID keyboards with PSoC

Ioannis
- 19th December 2021, 10:52
what have you tried ?

I think every variable or constant I could find in the program and the descriptor file. One at a time or combination of them. The result was sending the 8 wide buffer all the time.


It is not so easy to change buffer. You must change descriptor file, not just length(If I understand usb correctly).

[/CODE]

Yes, I found the hard way...

I guess for now, your approach is the only way to do it. I thought I could escape with just a number change from 8 to 16! :(

Jerson, as I noted above, I did change all those variables related to the buffer size (all where at 8 bytes) within descriptor file, but nothing really worked.

Ioannis

pedja089
- 19th December 2021, 21:16
Along buffer size, you need to change in hid_desc.bas:


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
If I understand correctly. But I never had any luck with changing descriptor files.

pedja089
- 20th December 2021, 00:19
I think you can find some info in this series:
https://www.youtube.com/watch?v=wdgULBpRoXk

https://www.youtube.com/watch?v=2lPzTU-3ONI
Look for nkey roll over descriptor...

richard
- 20th December 2021, 02:08
if i watch that first video again i could fool myself into believing that one day i might understand usb enough to actually get somewhere with it

great info pedja089

Jerson
- 20th December 2021, 03:29
very nice instructive videos

Ioannis
- 20th December 2021, 07:46
Along buffer size, you need to change in hid_desc.bas:


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
If I understand correctly. But I never had any luck with changing descriptor files.

Me too... :( There is no documentation of all these parameters...

Will watch the videos. Thanks for the info each and everyone.
Ioannis