From 6ace84b08962baff5baae09443e9d4f968d11e84 Mon Sep 17 00:00:00 2001
From: Pavol Rusnak <stick@gk2.sk>
Date: Wed, 28 Dec 2016 18:56:03 +0100
Subject: [PATCH] stmhal: Implement ioctl for USB HID read.

---
 stmhal/usb.c                | 3 +++
 stmhal/usbd_hid_interface.c | 5 +++++
 stmhal/usbd_hid_interface.h | 1 +
 3 files changed, 9 insertions(+)

diff --git a/stmhal/usb.c b/stmhal/usb.c
index ac85c5371..7eb3f179a 100644
--- a/stmhal/usb.c
+++ b/stmhal/usb.c
@@ -636,6 +636,9 @@ STATIC mp_uint_t pyb_usb_hid_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_
     if (request == MP_STREAM_POLL) {
         mp_uint_t flags = arg;
         ret = 0;
+        if ((flags & MP_STREAM_POLL_RD) && USBD_HID_RxNum() > 0) {
+            ret |= MP_STREAM_POLL_RD;
+        }
         if ((flags & MP_STREAM_POLL_WR) && USBD_HID_CanSendReport(&hUSBDDevice)) {
             ret |= MP_STREAM_POLL_WR;
         }
diff --git a/stmhal/usbd_hid_interface.c b/stmhal/usbd_hid_interface.c
index 837feece9..4987314bd 100644
--- a/stmhal/usbd_hid_interface.c
+++ b/stmhal/usbd_hid_interface.c
@@ -93,6 +93,11 @@ static int8_t HID_Itf_Receive(uint8_t* Buf, uint32_t Len) {
     return USBD_OK;
 }
 
+// Returns number of ready rx buffers.
+int USBD_HID_RxNum(void) {
+    return (current_read_buffer != current_write_buffer);
+}
+
 // timout in milliseconds.
 // Returns number of bytes read from the device.
 int USBD_HID_Rx(uint8_t *buf, uint32_t len, uint32_t timeout) {
diff --git a/stmhal/usbd_hid_interface.h b/stmhal/usbd_hid_interface.h
index 9cdf32549..959c46ff8 100644
--- a/stmhal/usbd_hid_interface.h
+++ b/stmhal/usbd_hid_interface.h
@@ -6,4 +6,5 @@
 
 extern const USBD_HID_ItfTypeDef USBD_HID_fops;
 
+int USBD_HID_RxNum(void);
 int USBD_HID_Rx(uint8_t *buf, uint32_t len, uint32_t timeout);
-- 
GitLab