Library 31- USB HID Host for STM32F4
USB HID (or Human Input Device) is a library, that allows you to read mouse or keyboard data with your STM32F4xx device. So you can use keyboard to write characters to STM32F4xx or use mouse for LCD or any other stuff.
Library
Features
- Interface with USB mouse
- 3 buttons and movement supported
- Interface with USB keyboard
- QWERTY or AZERTY supported
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx GPIO
- STM32F4xx EXTI
- MISC
- TM
- defines.h
- defines.h
- USB
- USB HID Host stack provided by STMicroelectronics (included in library)
USB | FS MODE | HS IN FS MODE | DESCRIPTION |
---|---|---|---|
Data + | PA12 | PB15 | USB Data+ line |
Data – | PA11 | PB14 | USB Data- line |
ID | PA10 | PB12 | USB ID pin |
VBUS | PA9 | PB13 | USB activate |
Technically, Data+ and Data- are enough for USB communication in any way. But, STM32F4- and STM32F429- Discovery boards uses ID and VBUS pins for activate USB communication (STMPS2151 chip for USB). If you are using STM32F4 or STM32F429 Discovery boards, you need at least VBUS pin. If you are working on custom application, you can disable ID and VBUS pins. To disable pins, open project’s defines.h file and add lines below:
1 2 3 4 5 6 7 8 |
/* defines.h configuraion file */ /* Uncomment if you want to disable ID pin for USB HID HOST library */ #define USB_HID_HOST_DISABLE_ID /* Uncomment if you want to disable VBUS pin for USB HID HOST library */ /* If you do this, on F4 and F429 Discovery boards USB will not work */ #define USB_HID_HOST_DISABLE_VBUS |
By default, USB FS mode is used, also used on STM32F4-Discovery board. If you want to enable USB HS in FS mode for STM32F429 Discovery board, open project’s defines.h file and add lines below:
1 2 3 4 5 6 |
/* defines.h configuration file */ /* Uncomment if you want to enable USB HS in FS mode */ /* Used on STM32F429 Discovery board */ /* By default, USB FS mode is used */ #define USE_USB_OTG_HS |
By default, AZERTY keyboard format is in use. If you will work with QWERTY format, add lines below in defines.h file:
1 2 3 4 5 |
/* Defines.h file */ /* Keyboard supports for QWERTY mode */ /* If you want to enable it, uncomment defines below */ #define QWERTY_KEYBOARD |
That’s all. You are now ready to work with USB HID devices.
Clock was set down to 168MHz for STM32F429 because you can not get 48MHz for USB from 180MHz core clock.
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
/** * HID Host result enumeration * * Parameters: * - TM_USB_HIDHOST_Result_Error * An error occured * - TM_USB_HIDHOST_Result_KeyboardConnected * Keyboard is connected and ready to use * - TM_USB_HIDHOST_Result_MouseConnected * Mouse is connected and ready to use * - TM_USB_HIDHOST_Result_Disconnected * Device is not connected * - TM_USB_HIDHOST_Result_DeviceNotSupported * Device is not supported * - TM_USB_HIDHOST_Result_LibraryNotInitialized * Library is not initialized */ typedef enum { TM_USB_HIDHOST_Result_Error, TM_USB_HIDHOST_Result_KeyboardConnected, TM_USB_HIDHOST_Result_MouseConnected, TM_USB_HIDHOST_Result_Disconnected, TM_USB_HIDHOST_Result_DeviceNotSupported, TM_USB_HIDHOST_Result_LibraryNotInitialized } TM_USB_HIDHOST_Result_t; /** * HID Host Button enumeration * * Parameters: * - TM_USB_HIDHOST_Button_Pressed * Button was pressed * - TM_USB_HIDHOST_Button_Released * Button was released */ typedef enum { TM_USB_HIDHOST_Button_Pressed = 0, TM_USB_HIDHOST_Button_Released } TM_USB_HIDHOST_Button_t; /** * HID Host keyboard struct * * Parameters: * - TM_USB_HIDHOST_Button_t ButtonStatus * Indicates if button is pressed or released * - uint8_t Button * Button number pressed */ typedef struct { TM_USB_HIDHOST_Button_t ButtonStatus; uint8_t Button; } TM_USB_HIDHOST_Keyboard_t; /** * HID Host mouse struct * * Parameters: * - int16_t AbsoluteX * Absolute cursor X position * - int16_t AbsoluteY * Absolute cursor Y position * - int16_t DiffX * Difference cursor X position from last check * - int16_t DiffY * Difference cursor Y position from last check * - TM_USB_HIDHOST_Button_t LeftButton * Indicates if left button is pressed or released * - TM_USB_HIDHOST_Button_t RightButton * Indicates if right button is pressed or released * - TM_USB_HIDHOST_Button_t MiddleButton * Indicates if middle button is pressed or released */ typedef struct { /* Cursor movement */ int16_t AbsoluteX; int16_t AbsoluteY; int16_t DiffX; int16_t DiffY; /* Buttons */ TM_USB_HIDHOST_Button_t LeftButton; TM_USB_HIDHOST_Button_t RightButton; TM_USB_HIDHOST_Button_t MiddleButton; } TM_USB_HIDHOST_Mouse_t; /** * Initialize USB HID Host * * */ extern void TM_USB_HIDHOST_Init(void); /** * Process USB HID library * * This function has to be called periodically, as fast as possible * * Returns member of TM_USB_HIDHOST_Result_t typedef */ extern TM_USB_HIDHOST_Result_t TM_USB_HIDHOST_Process(void); /** * Checks device status * * Returns member of TM_USB_HIDHOST_Result_t typedef */ extern TM_USB_HIDHOST_Result_t TM_USB_HIDHOST_Device(void); /** * Read keyboard data * * Parameters: * - TM_USB_HIDHOST_Keyboard_t* Keyboard * Pointer to TM_USB_HIDHOST_Keyboard_t keyboard struct * * Returns TM_USB_HIDHOST_Result_KeyboardConnected if keyboard is connected */ extern TM_USB_HIDHOST_Result_t TM_USB_HIDHOST_ReadKeyboard(TM_USB_HIDHOST_Keyboard_t* Keyboard); /** * Read mouse data * * Parameters: * - TM_USB_HIDHOST_Mouse_t* Mouse * Pointer to TM_USB_HIDHOST_Mouse_t mouse struct * * Returns TM_USB_HIDHOST_Result_MouseConnected if mouse is connected */ extern TM_USB_HIDHOST_Result_t TM_USB_HIDHOST_ReadMouse(TM_USB_HIDHOST_Mouse_t* Mouse); |
Example
Example below works with USB mouse and USB keyboard:
- If you connect USB mouse, RED led will be ON to indicate this
- RED led will be always on.
- If you move mouse, then GREEN led should BLINK
- If you press left button, GREEN led will be ON
- If you press right button, GREEN led will be OFF
- If you connect USB keyboard, GREEN led will be ON to indicate this
- If keyboard is inserted, then GREEN led will be ON
- If you press lower b, RED led will be ON
- If you press lower v, RED led will be OFF
- If both leds blinks, then USB device is not recignized
- If not leds on, then device is not inserted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
/** * Keil project for USB HID Host * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_disco.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_usb_hid_host.h" #include <stdio.h> int main(void) { TM_USB_HIDHOST_Result_t USB_HID_Status; /* USB HID Host status */ TM_USB_HIDHOST_Keyboard_t Keyboard_Data; /* Keyboard handler */ TM_USB_HIDHOST_Mouse_t Mouse_Data; /* Mouse handler */ /* Initialize system */ SystemInit(); /* Leds init */ TM_DISCO_LedInit(); /* Delay init */ TM_DELAY_Init(); /* Init USB HID */ TM_USB_HIDHOST_Init(); while (1) { /* Process USB HID */ /* This must be called periodically */ TM_USB_HIDHOST_Process(); /* Get connected device */ USB_HID_Status = TM_USB_HIDHOST_Device(); /* Switch status */ switch (USB_HID_Status) { /* Keyboard connected */ case TM_USB_HIDHOST_Result_KeyboardConnected: /* GREEN led ON */ TM_DISCO_LedOn(LED_GREEN); /* Get keyboard data */ TM_USB_HIDHOST_ReadKeyboard(&Keyboard_Data); /* Check if keyboard button is pressed */ if (Keyboard_Data.ButtonStatus == TM_USB_HIDHOST_Button_Pressed) { if (Keyboard_Data.Button == 'b') { /* Turn on RED led on 'b' button */ TM_DISCO_LedOn(LED_RED); } else if (Keyboard_Data.Button == 'v') { /* Turn off RED led on 'v' button */ TM_DISCO_LedOff(LED_RED); } } break; /* Mouse connected */ case TM_USB_HIDHOST_Result_MouseConnected: /* RED led ON */ TM_DISCO_LedOn(LED_RED); /* Get mouse data */ TM_USB_HIDHOST_ReadMouse(&Mouse_Data); /* Check buttons */ if (Mouse_Data.LeftButton == TM_USB_HIDHOST_Button_Pressed) { /* Left button */ TM_DISCO_LedOn(LED_GREEN); /* Toggle GREEN led */ } if (Mouse_Data.RightButton == TM_USB_HIDHOST_Button_Pressed) { /* Right button */ TM_DISCO_LedOff(LED_GREEN); /* Toggle GREEN led */ } if (Mouse_Data.MiddleButton == TM_USB_HIDHOST_Button_Pressed) { /* Middle button */ TM_DISCO_LedToggle(LED_GREEN); /* Toggle GREEN led */ } if (Mouse_Data.DiffX != 0 || Mouse_Data.DiffY != 0) { /* If there is a difference for mouse movement at any coordinate */ TM_DISCO_LedToggle(LED_GREEN); /* Toggle RED led */ /* If you need to move cursor now at fix position */ /* Positions are stored like this */ //Mouse_Data.AbsoluteX //Absolute X position for LCD //Mouse_Data.AbsoluteY //Absolute Y position for LCD } break; /* No device connected */ case TM_USB_HIDHOST_Result_Disconnected: /* Turn off leds */ TM_DISCO_LedOff(LED_RED | LED_GREEN); break; /* Device is not supported */ case TM_USB_HIDHOST_Result_DeviceNotSupported: /* Device is not supported */ /* Toggle leds forever */ TM_DISCO_LedToggle(LED_RED | LED_GREEN); /* Delay */ Delayms(50); break; /* Error occurred somewhere */ case TM_USB_HIDHOST_Result_Error: /* Error occurred */ /* Toggle RED LED forever */ TM_DISCO_LedToggle(LED_RED); /* Delay */ Delayms(50); break; /* Library is not initialized */ case TM_USB_HIDHOST_Result_LibraryNotInitialized: /* Library is not initialized */ /* Toggle GREEN LED */ TM_DISCO_LedToggle(LED_GREEN); /* Delay */ Delayms(50); break; } } } |
Project is available on my Github account, download library below.
Recent comments