STM32 USB Custom HID
Jump to navigation
Jump to search
Work in progress!
Videos
Initial setup:
Bi-directional:
STM32CubeMX Setup
File usbd_custom_hid_if.h:
/* USER CODE BEGIN EXPORTED_FUNCTIONS */ void CUSTOM_HID_OutEvent_FS_Handler(uint8_t *buffer); /* USER CODE END EXPORTED_FUNCTIONS */
File usbd_custom_hid_if.c:
/** Usb HID report descriptor. */
__ALIGN_BEGIN static uint8_t CUSTOM_HID_ReportDesc_FS[USBD_CUSTOM_HID_REPORT_DESC_SIZE] __ALIGN_END =
{
/* USER CODE BEGIN 0 */
0xA1, 0x01,
/* USER CODE END 0 */
0xC0 /* END_COLLECTION */
};
/**
* @brief Manage the CUSTOM HID class events
* @param event_idx: Event index
* @param state: Event state
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state)
{
/* USER CODE BEGIN 6 */
UNUSED(event_idx);
UNUSED(state);
uint8_t *buffer = (uint8_t*) ((uint32_t) event_idx);
CUSTOM_HID_OutEvent_FS_Handler(buffer);
/* Start next USB packet transfer once data processing is completed */
if (USBD_CUSTOM_HID_ReceivePacket(&hUsbDeviceFS) != (uint8_t) USBD_OK)
{
return -1;
}
return (USBD_OK);
/* USER CODE END 6 */
}
void CUSTOM_HID_OutEvent_FS_Handler(uint8_t *buffer) {
printf("Recv buffer: 0x%02x, 0x%02x 0x%02x\n", buffer[0], buffer[1], buffer[2]);
switch (buffer[0]) {
case 3:
uint16_t new = ((uint16_t) buffer[1] << 8) | buffer[2];
printf("Setting relays to: 0x%04x\n", new);
set_relays(&rel, new);
break;
case 4:
RTC_DateTypeDef date;
RTC_TimeTypeDef time;
date.Year = buffer[1];
date.Month = buffer[2];
date.Date = buffer[3];
time.Hours = buffer[4];
time.Minutes = buffer[5];
time.Seconds = buffer[6];
printf("Setting date/time %d %d %d %d %d %d\n", buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6]);
if (HAL_RTC_SetTime(&hrtc, &time, RTC_FORMAT_BIN) != HAL_OK) {
printf("RTC SetTime error\n");
}
if (HAL_RTC_SetDate(&hrtc, &date, RTC_FORMAT_BIN) != HAL_OK) {
printf("RTC SetDate error\n");
}
break;
}
}