Sample USB keyboard stack configured as a HID Boot Keyboard so it works at BIOS/UEFI and CMOS password screens, not only in the OS.
| Item | Typical CubeMX HID | This sample |
|---|---|---|
bInterfaceSubClass |
0x00 (none) |
0x01 (Boot) |
bInterfaceProtocol |
0x00 / mouse |
0x01 (Keyboard) |
| Report | often mouse / custom | 8-byte boot keyboard |
SET_PROTOCOL / GET_PROTOCOL |
missing / ignored | implemented |
| Device shape | sometimes composite | single keyboard IF |
- MCU family with USB Device FS (tested pattern: STM32F103 / F0 / F4 / G0 / L4)
- STM32Cube USB Device Library (
USBD) - USB Full-Speed device mode only (no composite)
- Create a new CubeMX project for your MCU.
- Connectivity → USB → Device (FS/FS Device only).
- Middleware → USB_DEVICE → Class:
Human Interface Device Class (HID). - Clock: ensure USB 48 MHz is valid.
- Generate code (STM32CubeIDE or Makefile).
- Copy/overwrite files from this repo into the generated project (see layout below).
- Call
App_Keyboard_Init()afterMX_USB_DEVICE_Init(), thenApp_Keyboard_Task()in the main loop (or use the demo button path).
USB_DEVICE/App/usbd_desc.c <- VID/PID + strings (optional)
USB_DEVICE/App/usbd_hid_if.c <- optional weak stubs
Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c
Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc/usbd_hid.h
Core/Inc/app_keyboard.h
Core/Src/app_keyboard.c
Core/Inc/usb_hid_keys.h
If CubeMX regenerates middleware, re-apply usbd_hid.c / usbd_hid.h.
After USB configures, the sample types test then Enter once (for bring-up). Disable APP_KEYBOARD_DEMO_TYPE_ONCE in app_keyboard.h for production.
- Boot subclass + keyboard protocol in config descriptor
- Exactly one HID keyboard interface
- 8-byte interrupt IN reports
-
SET_PROTOCOLaccepted (Boot = 0) - No CDC/MSC/mouse composite while validating CMOS password
- On PC BIOS: USB legacy / USB keyboard support enabled if that option exists
Sample code is provided as-is for integration into ST Cube projects.