diff --git a/stmhal/usb.c b/stmhal/usb.c index 5ba5ddc6e7..ac85c53714 100644 --- a/stmhal/usb.c +++ b/stmhal/usb.c @@ -607,7 +607,11 @@ STATIC mp_obj_t pyb_usb_hid_send(mp_obj_t self_in, mp_obj_t report_in) { } // send the data - USBD_HID_SendReport(&hUSBDDevice, bufinfo.buf, bufinfo.len); + if (USBD_OK == USBD_HID_SendReport(&hUSBDDevice, bufinfo.buf, bufinfo.len)) { + return mp_obj_new_int(bufinfo.len); + } else { + return mp_obj_new_int(0); + } #endif return mp_const_none; diff --git a/stmhal/usbd_hid_interface.c b/stmhal/usbd_hid_interface.c index 04f1b7fd0c..837feece9d 100644 --- a/stmhal/usbd_hid_interface.c +++ b/stmhal/usbd_hid_interface.c @@ -45,21 +45,35 @@ /* Private variables ---------------------------------------------------------*/ -static __IO uint8_t dev_is_connected = 0; // indicates if we are connected - static uint8_t buffer[2][HID_DATA_FS_MAX_PACKET_SIZE]; // pair of buffers to read individual packets into static int8_t current_read_buffer = 0; // which buffer to read from -static uint32_t last_read_len; // length of last read +static uint32_t last_read_len = 0; // length of last read static int8_t current_write_buffer = 0; // which buffer to write to - /* Private function prototypes -----------------------------------------------*/ +static int8_t HID_Itf_Init (void); static int8_t HID_Itf_Receive (uint8_t* pbuf, uint32_t Len); const USBD_HID_ItfTypeDef USBD_HID_fops = { + HID_Itf_Init, HID_Itf_Receive }; +/** + * @brief HID_Itf_Init + * Initializes the HID media low layer + * @param None + * @retval Result of the opeartion: USBD_OK if all operations are OK else USBD_FAIL + */ +static int8_t HID_Itf_Init(void) +{ + current_read_buffer = 0; + last_read_len = 0; + current_write_buffer = 0; + USBD_HID_SetRxBuffer(&hUSBDDevice, buffer[current_write_buffer]); + return USBD_OK; +} + /** * @brief HID_Itf_Receive * Data received over USB OUT endpoint is processed here. diff --git a/stmhal/usbdev/class/inc/usbd_cdc_msc_hid.h b/stmhal/usbdev/class/inc/usbd_cdc_msc_hid.h index 76a7678929..7cb64fd845 100644 --- a/stmhal/usbdev/class/inc/usbd_cdc_msc_hid.h +++ b/stmhal/usbdev/class/inc/usbd_cdc_msc_hid.h @@ -48,6 +48,7 @@ typedef struct { } USBD_CDC_HandleTypeDef; typedef struct _USBD_HID_Itf { + int8_t (* Init) (void); int8_t (* Receive)(uint8_t *, uint32_t); } USBD_HID_ItfTypeDef; diff --git a/stmhal/usbdev/class/src/usbd_cdc_msc_hid.c b/stmhal/usbdev/class/src/usbd_cdc_msc_hid.c index d136570234..3ebc7d8280 100644 --- a/stmhal/usbdev/class/src/usbd_cdc_msc_hid.c +++ b/stmhal/usbdev/class/src/usbd_cdc_msc_hid.c @@ -724,6 +724,8 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { USBD_EP_TYPE_INTR, mps_out); + HID_fops->Init(); + // Prepare Out endpoint to receive next packet USBD_LL_PrepareReceive(pdev, hid_out_ep, HID_ClassData.RxBuffer, mps_out);