How to send USB HID Reports without guessing the HAL_Delay()?
I want to send USB HID Reports as fast as the MCU or the USB protocol allows.
In order to send an USB HID Report on an STM32F103 I'm doing something like:
USBD_HID_SendReport(&hUsbDeviceFS, (u8*)&report,sizeof(report));
HAL_Delay(some_delay);
or even:
USBD_HID_SendReport(&hUsbDeviceFS, (u8*)&report,sizeof(report));
for(volatile int k=0; k<some_other_delay; ++k) asm volatile("nop");
but it's obviously dicey and suboptimal to "guess" at the correct sleep value (which in my case changes from situation to situation, and also depending on the size of the USB HID Report).
What I really want is to do is something like:
for(int i=0; i<N; ++i) USBD_HID_SendReport(&hUsbDeviceFS, (u8*)&report,sizeof(report));
and have it just work (which it currently doesn't).
I tried modifying USBD_HID_SendReport() from the original:
uint8_t USBD_HID_SendReport(USBD_HandleTypeDef* pdev, uint8_t* report, uint16_t len){
USBD_HID_HandleTypeDef* hhid = (USBD_HID_HandleTypeDef*)pdev->pClassData;
if(pdev->dev_state==USBD_STATE_CONFIGURED){
if(hhid->state==HID_IDLE){ // if it's not idle, then it discards!
hhid->state = HID_BUSY;
USBD_LL_Transmit(pdev, HID_EPIN_ADDR, report, len);
}
}
return USBD_OK;
}
to the following:
uint8_t USBD_HID_SendReport(USBD_HandleTypeDef* pdev, uint8_t* report, uint16_t len){
USBD_HID_HandleTypeDef* hhid = (USBD_HID_HandleTypeDef*)pdev->pClassData;
if(pdev->dev_state==USBD_STATE_CONFIGURED){
while(hhid->state!=HID_IDLE) asm volatile("nop");
hhid->state = HID_BUSY;
USBD_LL_Transmit(pdev, HID_EPIN_ADDR, report, len);
}
return USBD_OK;
}
but it's not working as intended.
So how I can achieve the desired result of sending USB HID Reports as fast as possible, without any manual wait, ie. something like:
for(int i=0; i<N; ++i) USBD_HID_SendReport(&hUsbDeviceFS, (u8*)&report,sizeof(report));
