Hi @FuadPurnomo
Indeed the examples provided by @TDK are interesting, you need to configure the USB device with two interfaces:
- CDC ACM interface (for serial communication)
- HID Keyboard interface (for keyboard reports)
You can refer to this article on how to Implement a USB HID keyboard device USB descriptors must reflect this composite device (2 interfaces). Regarding USBX, examples for CDC ACM exist (e.g., Ux_Device_CDC_ACM example for H533).
- Your CDC read thread usbx_cdc_acm_read_thread_entry already reads incoming data into a buffer.
for (int i = 0; i < actual_length; i++)
{
if (UserRxBufferFS[i] == 'F')
{
// Generate a high-going pulse on a GPIO pin
HAL_GPIO_WritePin(GPIOx, GPIO_PIN_y, GPIO_PIN_SET);
HAL_Delay(10); // 10 ms pulse, adjust as needed
HAL_GPIO_WritePin(GPIOx, GPIO_PIN_y, GPIO_PIN_RESET);
}
}
Or triggering a hardware timer or PWM pulse if you want precise timing.
2. Adjust function calls based on USBX HID keyboard API.
// Prepare keyboard HID report for F8 key press
keyboardHID keyboardhid = {0};
keyboardhid.KEYCODE1 = 0x41; // Check HID usage ID for F8 from table section 10 Keyboard/Keypad Page (0x07) in https://usb.org/sites/default/files/hut1_21.pdf
// Send key press
ux_device_class_hid_event_set(hid_keyboard, (UX_SLAVE_CLASS_HID_EVENT *)&keyboardhid);
// Small delay to simulate key press duration to be adjusted to avoid debounce effect
HAL_Delay(10);
// Send key release (all zeros)
memset(&keyboardhid, 0, sizeof(keyboardhid));
ux_device_class_hid_event_set(hid_keyboard, (UX_SLAVE_CLASS_HID_EVENT *)&keyboardhid);
}
}
- Use HAL_GetTick() or a hardware timer for accurate timing instead of blocking delays.
To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL