USB Component  Version 5.0
MDK-Professional Middleware for USB Device and Host
 All Data Structures Functions Variables Enumerations Enumerator Groups Pages
Human Interface Device Class Functions

Enable the USB Host to support USB Human Interface Devices. More...

Functions

usbStatus USBH_HID_GetDeviceStatus (int8_t instance)
 Get status of Human Interface Device.
 
int32_t USBH_HID_Read (int8_t instance, uint8_t *buf, int32_t len)
 Read data received from Human Interface Device.
 
int32_t USBH_HID_Write (int8_t instance, const uint8_t *buf, int32_t len)
 Write data to Human Interface Device.
 
int USBH_HID_GetKeyboardKey (int8_t instance)
 Retrieve first pending pressed keyboard key on HID Keyboard.
 
usbStatus USBH_HID_GetMouseState (int8_t instance, usbHID_MouseState *state)
 Retrieve state change since last call of this function.
 

Description

Enable the USB Host to support USB Human Interface Devices.

The HID class in the USB Component is used for attaching input devices to your system.

Refer to:

To create an USB Host with support for the HID class:

Configuration File USBH_Config_HID.h
The USB Host Component has one configuration file for the HID Device class: USBH_Config_HID.h. This configuration file defines the maximum number of concurrent HID Devices that may be attached to the system.

Code Example

/*------------------------------------------------------------------------------
USB Host Thread
*----------------------------------------------------------------------------*/
void USBH_Thread (void const *arg) {
osPriority priority; /* Thread priority */
char con = 0; /* Connection status of kbd */
char con_ex = 40; /* Previous connection status
+ initial time in 100 ms
intervals for initial disp */
char out = 1; /* Output to keyboard LEDs */
USBH_Initialize (0); /* Initialize USB Host 0 */
USBH_Initialize (1); /* Initialize USB Host 1 */
while (1) {
con = USBH_HID_GetStatus(0) == usbOK; /* Get kbd connection status */
if ((con ^ con_ex) & 1) { /* If connection status changed */
priority = osThreadGetPriority (osThreadGetId());
osThreadSetPriority (osThreadGetId(), osPriorityHigh);
if (con) {
USBH_HID_Write (0,(uint8_t *)&out,1);/* Turn on NUM LED */
printf ("\nKeyboard connected\n");
} else {
printf ("\nKeyboard disconnected ...\n");
}
osThreadSetPriority (osThreadGetId(), priority);
con_ex = con;
} else if (con_ex > 1) { /* If initial time active */
con_ex -= 2; /* Decrement initial time */
if ((con_ex <= 1) && (!con)) { /* If initial time expired */
priority = osThreadGetPriority (osThreadGetId());
osThreadSetPriority (osThreadGetId(), osPriorityHigh);
printf ("\nNo keyboard connected ... \n");
osThreadSetPriority (osThreadGetId(), priority);
con_ex = con;
} else {
osDelay(200);
}
}
osDelay(100);
}
}
osThreadDef(USBH_Thread, osPriorityNormal, 1, NULL);

Function Documentation

usbStatus USBH_HID_GetDeviceStatus ( int8_t  instance)

Get status of Human Interface Device.

Parameters
[in]instanceinstance of HID Device.
Returns
status code that indicates the execution status of the function as defined with usbStatus.

The function USBH_HID_GetDeviceStatus checks whether the human interface device is connected and initialized.

The argument instance is specifying the device instance.

Code Example

#include "cmsis_os.h" /* CMSIS RTOS definitions */
#include "rl_usb.h" /* RL-USB function prototypes */
/*------------------------------------------------------------------------------
USB Host Thread
*----------------------------------------------------------------------------*/
void USBH_Thread (void const *arg) {
osPriority priority; /* Thread priority */
char con = 0; /* Connection status of kbd */
char con_ex = 40; /* Previous connection status
+ initial time in 100 ms
intervals for initial disp */
char out = 1; /* Output to keyboard LEDs */
USBH_Initialize (0); /* Initialize USB Host 0 */
USBH_Initialize (1); /* Initialize USB Host 1 */
while (1) {
con = USBH_HID_GetDeviceStatus(0); /* Get kbd connection status */
if ((con ^ con_ex) & 1) { /* If connection status changed */
priority = osThreadGetPriority (osThreadGetId());
osThreadSetPriority (osThreadGetId(), osPriorityHigh);
if (con) {
USBH_HID_Write (0,(uint8_t *)&out,1);/* Turn on NUM LED */
printf ("\nKeyboard connected\n");
} else {
printf ("\nKeyboard disconnected ...\n");
}
osThreadSetPriority (osThreadGetId(), priority);
con_ex = con;
} else if (con_ex > 1) { /* If initial time active */
con_ex -= 2; /* Decrement initial time */
if ((con_ex <= 1) && (!con)) { /* If initial time expired */
priority = osThreadGetPriority (osThreadGetId());
osThreadSetPriority (osThreadGetId(), osPriorityHigh);
printf ("\nNo keyboard connected ... \n");
osThreadSetPriority (osThreadGetId(), priority);
con_ex = con;
} else {
osDelay(200);
}
}
osDelay(100);
}
}
int32_t USBH_HID_GetKeyboardKey ( int8_t  instance)

Retrieve first pending pressed keyboard key on HID Keyboard.

Parameters
[in]instanceinstance of HID Device.
Returns
value of read character or no character received.
  • value >= 0: value of first received unread character
  • value -1: indicates no character was received

The function USBH_HID_GetKeyboardKey enables programmers to handle signals from an USB keyboard. Implementing this function in user code overrides the library function that handles boot protocol device keyboard input.

The argument instance is specifying the device instance.

Code Example

#include <stdio.h> /* standard I/O .h-file */
#include <ctype.h> /* character functions */
#include "cmsis_os.h" /* CMSIS RTOS definitions */
#include "rl_usb.h" /* RL-USB function prototypes */
/*------------------------------------------------------------------------------
Application
*----------------------------------------------------------------------------*/
int main (void) {
char cmdbuf [15]; /* command input buffer */
int i; /* index for command buffer */
int idx; /* index for circular buffer */
osThreadCreate (osThread(USBH_Thread), NULL);
timer_id = osTimerCreate(osTimer(timer_1ms), osTimerPeriodic, NULL);
osTimerStart(timer_id, 1);
clear_records (); /* initialize circular buffer */
printf ( menu ); /* display command menu */
while (1) { /* loop forever */
printf ("\nCommand: ");
/* get command line input */
if (getline (&cmdbuf[0], sizeof (cmdbuf)) == false)
continue;
for (i = 0; cmdbuf[i] != 0; i++) { /* convert to upper characters */
cmdbuf[i] = toupper(cmdbuf[i]);
}
for (i = 0; cmdbuf[i] == ' '; i++); /* skip blanks */
switch (cmdbuf[i]) { /* proceed to command function */
case 'R': /* Read circular Buffer */
if ((idx = read_index (&cmdbuf[i+1])) == WRONGINDEX) break;
while (idx != sindex) { /* check end of table */
if (USBH_HID_GetKeyboardKey(0) == 0x1B) break; /* ESC terminates */
if (save_record[idx].time.hour != 0xff) {
measure_display (save_record[idx]); /* display record */
printf ("\n");
}
if (++idx == SCNT) idx = 0; /* next circular buffer entry */
}
break;
case 'T': /* Enter Current Time */
set_time (&cmdbuf[i+1]);
break;
default: /* Error Handling */
printf (ERROR_STR, "UNKNOWN COMMAND");
printf (menu); /* display command menu */
break;
}
}
}
bool USBH_HID_GetMouseState ( int8_t  instance,
usbHID_MouseState state 
)

Retrieve state change since last call of this function.

Parameters
[in]instanceinstance of HID Device.
[out]statepointer to mouse state usbHID_MouseState structure.
Returns
status code that indicates the execution status of the function as defined with usbStatus.

The function USBH_HID_GetMouseState enables programmers to handle signals from an USB mouse. Implementing this function in user code overrides the library function that handles boot protocol device mouse input.

The argument instance is specifying the device instance.

The argument state is a pointer to the mouse state structure (usbHID_MouseState).

Code Example

#include "rl_usb.h"
void getmouse (void) {
..
if (USBH_HID_GetMouseState (0, MouseState)) { // If mouse move
..
}
)
int32_t USBH_HID_Read ( int8_t  instance,
uint8_t *  buf,
int32_t  len 
)

Read data received from Human Interface Device.

Parameters
[in]instanceinstance of HID Device.
[out]bufbuffer that receives data.
[in]lenmaximum number of bytes to read.
Returns
number of bytes read or execution status.
  • value >= 0: number of bytes read
  • value < 0: error occurred, -value is execution status as defined with usbStatus

The function USBH_HID_Read retrieves the data sent by an USB HID device and stores it in a buffer.

The argument instance is specifying the device instance.

The argument buf is pointing to the location where the data will be returned.

The argument len specifies the number of bytes to be read.

Code Example

#include "rl_usb.h"
void parse_HID (void) {
U8 data[16];
..
if (USBH_HID_Read (0, &data, 2)) { // If data received from HID device
..
}
)
int32_t USBH_HID_Write ( int8_t  instance,
const uint8_t *  buf,
int32_t  len 
)

Write data to Human Interface Device.

Parameters
[in]instanceinstance of HID Device.
[in]bufdata buffer containing data to write.
[in]lennumber of data bytes to write.
Returns
number of bytes accepted for writing or execution status.
  • value >= 0: number of bytes accepted for writing
  • value < 0: error occurred, -value is execution status as defined with usbStatus

The function USBH_HID_Write writes data from a buffer to an USB HID device.

The argument instance is specifying the device instance.

The argument buf is pointing to the location where the data will be written.

The argument len specifies the number of data bytes to be written.

Code Example

#include "cmsis_os.h" /* CMSIS RTOS definitions */
#include "rl_usb.h" /* RL-USB function prototypes */
/*------------------------------------------------------------------------------
USB Host Thread
*----------------------------------------------------------------------------*/
void USBH_Thread (void const *arg) {
osPriority priority; /* Thread priority */
char con = 0; /* Connection status of kbd */
char con_ex = 40; /* Previous connection status
+ initial time in 100 ms
intervals for initial disp */
char out = 1; /* Output to keyboard LEDs */
USBH_Initialize (0); /* Initialize USB Host 0 */
USBH_Initialize (1); /* Initialize USB Host 1 */
while (1) {
con = USBH_HID_GetStatus(0); /* Get kbd connection status */
if ((con ^ con_ex) & 1) { /* If connection status changed */
priority = osThreadGetPriority (osThreadGetId());
osThreadSetPriority (osThreadGetId(), osPriorityHigh);
if (con) {
USBH_HID_Write (0,(uint8_t *)&out,1);/* Turn on NUM LED */
printf ("\nKeyboard connected\n");
} else {
printf ("\nKeyboard disconnected ...\n");
}
osThreadSetPriority (osThreadGetId(), priority);
con_ex = con;
} else if (con_ex > 1) { /* If initial time active */
con_ex -= 2; /* Decrement initial time */
if ((con_ex <= 1) && (!con)) { /* If initial time expired */
priority = osThreadGetPriority (osThreadGetId());
osThreadSetPriority (osThreadGetId(), osPriorityHigh);
printf ("\nNo keyboard connected ... \n");
osThreadSetPriority (osThreadGetId(), priority);
con_ex = con;
} else {
osDelay(200);
}
}
osDelay(100);
}
}