MCU User Forum
  USB
  USB HID KEYBOARD (Search a Programmer)

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone! next newest topic | next oldest topic
Author Topic:   USB HID KEYBOARD (Search a Programmer)
bliebb
New Member
posted August 28, 2007 12:34 PM     Click Here to See the Profile for bliebb   Click Here to Email bliebb     Edit/Delete Message
Hi,

At the moment I try to convert the USB_HID MouseExample from Silabs to a USB HID Keyboard Example

Microcontroller: C8051F34x (development kit)

I am not an expert at USB and at the moment and nothing works !

If someone can help me to convert rapidly this mouse example to a keyboard example ...

I just need a source example ... that when I click to the button on the development board the example send the character "A" to the keyboard via HID.

Tell me how much is it to do this kind of job and the delay ?

Best Regards,

Steve

IP: Logged

Tsuneo
Member
posted August 29, 2007 01:10 AM     Click Here to See the Profile for Tsuneo   Click Here to Email Tsuneo     Edit/Delete Message
"I am not an expert at USB and at the moment and nothing works !"

Everyone has his (her) starting point. So, don't get so desperate.
I think a little push will lead you to success.
Let's see the implementation together.


Compiler and its version
If you are working on KEIL, and its version is greater than v8.0,
you have to fix the 'const' problem.
HID Example Blinky: ERROR L107: ADDRESS SPACE OVERFLOW


a) References
These references are the 'must' for keyboard implementation.
Download them from USB.org.

USB HID spec 1.11
http://www.usb.org/developers/devclass_docs/HID1_11.pdf"
HID Usage Table 1.12
http://www.usb.org/developers/devclass_docs/Hut1_12.pdf


b) Keyboard class implementation
Class implementation is divided into three processes.
- Descriptors
- Class specific requests
- Endpoint handler

b-1) Descriptors

You'll find the example of keyboard descriptors in the HID spec (HID1_11.pdf p66)
Appendix E: Example USB Descriptors for HID Class Devices
Modify the mouse example according to this keyboard example.

b-1-1) Device descriptor
VID/PID (idVendor, idProduct)
Modify at least PID (idProduct) until you get an unique VID/PID
Otherwise, Windows will conflict it with the mouse implementation.

USB for Dummies (one dummy, anyway)
posted September 26, 2006 12:02 AM
a) How to get unique VID/PID

b-1-2) Config descriptor
no change

b-1-3) Interface descriptor
bInterfaceProtocol: 0x01 (keyboard)

b-1-4) HID class descriptor
F3xx_USB0_Descriptors.h
#define HID_REPORT_DESCRIPTOR_SIZE 0x0032 <--- 0x003F
#define HID_REPORT_DESCRIPTOR_SIZE_LE 0x3200 <--- 0x3F00

b-1-5) Endpoint descriptor
F3xx_USB0_InterruptServiceRoutine.h
#define EP1_PACKET_SIZE 0x000A <--- 0x0008
#define EP1_PACKET_SIZE_LE 0x0A00 <--- 0x0800

OUT endpoint is not used - delete it

b-1-6) Report descriptor
Copy this one from the HID spec
E.6 Report Descriptor (Keyboard) (HID1_11.pdf p69)


To be continued

Tsuneo

IP: Logged

Tsuneo
Member
posted August 29, 2007 05:24 AM     Click Here to See the Profile for Tsuneo   Click Here to Email Tsuneo     Edit/Delete Message
Before continuing on the implementation of class specific requests and endpoint handling, it's better to explain about the report format.

INPUT report
INPUT report is sent from the device to host PC.
As the report descriptor defines, the INPUT report is formatted as follows;
one byte of modifier byte and 6 bytes keycode array.

byte
0 Modifier byte
1 reserved
2 keycode array (0)
3 keycode array (1)
4 keycode array (2)
5 keycode array (3)
6 keycode array (4)
7 keycode array (5)

The bitmap of modifier byte is defined in the HID spec.
8.3 Report Format for Array Items (HID1_11.pdf p56)

bit
0 LEFT CTRL
1 LEFT SHIFT
2 LEFT ALT
3 LEFT GUI
4 RIGHT CTRL
5 RIGHT SHIFT
6 RIGHT ALT
7 RIGHT GUI

This assignment is done by this part of the report descriptor
Usage Page (Key Codes);
Usage Minimum (224), <--- LeftControl
Usage Maximum (231), <--- Right GUI

"HID Usage Tables" spec defines keycode
10 Keyboard/Keypad Page (0x07) (Hut1_12.pdf p54)
Available range of keycode is also defined in the report descriptor
Usage Page (Key Codes),
Usage Minimum (0), <--- no event indicated
Usage Maximum (101), <--- Application

While no key is pushed, all report bytes are '0'.
This section of the HID spec shows the transition of the report while user types keys.
8.3 Report Format for Array Items (HID1_11.pdf p56)
In this example, ALT+CTRL+DEL are keyed in and released.

Transition        Modifier Byte   keycode Array (0)
LEFT ALT down 00000100 00
RIGHT CTRL down 00010100 00
DEL down 00010100 4C <--- DEL keycode
DEL up 00010100 00
RIGHT CTRL up 00000100 00
LEFT ALT up 00000000 00

Please note, the report shows a 'snapshot' of the state of pushed keys.
The keycode array can hold up to 6 keys which are simultaneously pushed. The order of keycode in the array is arbitrary. When more than 6 keys are pushed at the same time, ErrorRollOver (0x01) is filled to all six bytes in the array.

Appendix C: Keyboard Implementation (HID1_11.pdf p62)
The keyboard must report a phantom state indexing Usage(ErrorRollOver) in all array fields whenever the number of keys pressed exceeds the Report Count.


OUTPUT report
OUTPUT report is sent from host PC to the device.
The report descriptor defines a byte for five LEDs on the output report.

bit
0 CAPS LOCK
1 NUM LOCK
2 SCROLL LOCK
3 COMPOSE
4 KANA
5-7 reserved

This assignment derives from the LED usage, defined in the "HID Usage Tables" spec
11 LED Page (0x08) (Hut1_12.pdf p62)

And this definition in the report descriptor makes the assignment.
Usage Page (Page# for LEDs),
Usage Minimum (1), <-- CAPS LOCK
Usage Maximum (5), <-- KANA


To be continued

Tsuneo

[This message has been edited by Tsuneo (edited August 29, 2007).]

IP: Logged

Tsuneo
Member
posted August 30, 2007 03:41 AM     Click Here to See the Profile for Tsuneo   Click Here to Email Tsuneo     Edit/Delete Message
b-2) Class specific request

The HID spec describes the implementation about class specific requests in this appendix.
Appendix C: Keyboard Implementation (HID1_11.pdf p62)

According to this description, these requests are required for keyboard implementation.

- Get_Report( Input )  - send keycode array
- Set_Report( Output ) - on/off LEDs
- Get_Idle - return current report duration
- Set_Idle - set the report duration

In the interface descriptor, bootable option is selected
bInterfaceSubClass: 0x01 (bootable)
Then, these requests also have to be supported.

- Get_Protocol         - return current protocol boot/report
- Set_Protocol - switch the protocol

You'll see detailed description of these request in the HID spec.
7.2 Class-Specific Requests (HID1_11.pdf p50)

b-2-1) Get_Report( Input )
When Get_Report( Input ) arrives, send 'snapshot' of the INPUT report just at the moment.

b-2-2) Set_Report( Output )
On/off the LEDs according to the Output byte.

b-2-3) Set_Idle, Get_Idle
Set_Idle determines the repetition duration to send INPUT reports over the interrupt IN endpoint.
- When the MSB of wValue is 0, it sets the duration to infinite; the IN EP sends report just when the any key is made/released.
- When the MSB of wValue is not 0, it sets the duration to the value, multiplied by 4 ms.

The HID spec recommends 125 (500 ms) as the default duration for keyboard.
Windows sets this duration to 0 (infinite) in its enumeration.
MacOSX sets it to 9 (36 ms).

Get_Idle returns current duration.

b-2-4) Set_Protocol, Get_Protocol
On PC starts up, BIOS enables attached keyboard and mouse, when these device supports 'Boot' option. In some implementation, the report format can be different on 'Boot' and usual 'Report' state. Set_Protocol switches the state between 'Boot' and 'Report'.
The default after power on is 'Report' state.

In this implementation, the report format is the same between 'Boot' and 'Report'. Then, Set_Protocol switches just the state variable.

Get_Protocol returns current protocol state.


b-3) Endpoint handler
Send INPUT report over the interrupt IN endpoint.
The timing to send the report depends on the duration set by Set_Idle.
- When the duration is 'infinite', send report just when the device detects any key change, either make or break.
- When the duration is a valid value, send report in specified duration.

SOF interrupt (1ms interval) gives a good timer to count the duration.
In either duration, the endpoint handler is executed in the main loop, not in the endpoint interrupt.

Tsuneo

[This message has been edited by Tsuneo (edited September 01, 2007).]

IP: Logged

bliebb
New Member
posted September 05, 2007 08:27 AM     Click Here to See the Profile for bliebb   Click Here to Email bliebb     Edit/Delete Message
Hi Tsuneo,

Thank you verry much for your help !

Now I understand all and all work like a charm !

Steve Bilodeau

IP: Logged

All times are CT (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | MCU User Forum

Have you seen our MCU Knowledge Base?


Ultimate Bulletin Board 5.47b

Site Guide Privacy Legal