2016-08-29 17:42:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
2019-07-25 03:42:17 -04:00
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2016-08-29 17:42:38 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "usbd_hid_interface.h"
|
|
|
|
|
2019-07-25 03:42:17 -04:00
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "py/mphal.h"
|
2016-08-29 17:42:38 -04:00
|
|
|
#include "usb.h"
|
|
|
|
|
2019-07-16 00:39:21 -04:00
|
|
|
#if MICROPY_HW_USB_HID
|
|
|
|
|
2018-05-14 02:34:31 -04:00
|
|
|
uint8_t *usbd_hid_init(usbd_hid_state_t *hid_in) {
|
|
|
|
usbd_hid_itf_t *hid = (usbd_hid_itf_t*)hid_in;
|
2019-07-25 03:42:17 -04:00
|
|
|
hid->report_in_len = USBD_HID_REPORT_INVALID;
|
|
|
|
return &hid->report_in_buf[0]; // location to place first incoming report
|
2017-01-16 10:43:09 -05:00
|
|
|
}
|
|
|
|
|
2018-05-14 02:34:31 -04:00
|
|
|
int8_t usbd_hid_receive(usbd_hid_state_t *hid_in, size_t len) {
|
2019-07-25 03:42:17 -04:00
|
|
|
// Incoming report: save the length but don't schedule next report until user reads this one
|
2018-05-14 02:34:31 -04:00
|
|
|
usbd_hid_itf_t *hid = (usbd_hid_itf_t*)hid_in;
|
2019-07-25 03:42:17 -04:00
|
|
|
hid->report_in_len = len;
|
2016-08-29 17:42:38 -04:00
|
|
|
return USBD_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-25 03:42:17 -04:00
|
|
|
int usbd_hid_rx(usbd_hid_itf_t *hid, size_t len, uint8_t *buf, uint32_t timeout_ms) {
|
|
|
|
// Wait for an incoming report
|
|
|
|
uint32_t t0 = mp_hal_ticks_ms();
|
|
|
|
while (hid->report_in_len == USBD_HID_REPORT_INVALID) {
|
|
|
|
if (mp_hal_ticks_ms() - t0 >= timeout_ms || query_irq() == IRQ_STATE_DISABLED) {
|
|
|
|
return -MP_ETIMEDOUT;
|
2016-08-29 17:42:38 -04:00
|
|
|
}
|
2019-07-25 03:42:17 -04:00
|
|
|
MICROPY_EVENT_POLL_HOOK
|
2016-08-29 17:42:38 -04:00
|
|
|
}
|
|
|
|
|
2019-07-25 03:42:17 -04:00
|
|
|
// Copy bytes from report to user buffer
|
|
|
|
int n = MIN(len, hid->report_in_len);
|
|
|
|
memcpy(buf, &hid->report_in_buf[0], n);
|
2016-08-29 17:42:38 -04:00
|
|
|
|
2019-07-25 03:42:17 -04:00
|
|
|
// Schedule to receive next incoming report
|
|
|
|
hid->report_in_len = USBD_HID_REPORT_INVALID;
|
|
|
|
USBD_HID_ReceivePacket(&hid->base, &hid->report_in_buf[0]);
|
2017-01-17 14:17:15 -05:00
|
|
|
|
2019-07-25 03:42:17 -04:00
|
|
|
// Return number of bytes read into user buffer
|
|
|
|
return n;
|
2016-08-29 17:42:38 -04:00
|
|
|
}
|
2019-07-16 00:39:21 -04:00
|
|
|
|
|
|
|
#endif // MICROPY_HW_USB_HID
|