diff options
| m--------- | lib/tinyusb | 0 | ||||
| -rwxr-xr-x | ports/nrf/Makefile | 38 | ||||
| -rw-r--r-- | ports/nrf/common-hal/usb_hid/Device.c | 90 | ||||
| -rw-r--r-- | ports/nrf/common-hal/usb_hid/Device.h | 19 | ||||
| -rw-r--r-- | ports/nrf/common-hal/usb_hid/__init__.c | 155 | ||||
| -rw-r--r-- | ports/nrf/mpconfigport.h | 2 | ||||
| -rw-r--r-- | ports/nrf/usb/tusb_config.h | 44 |
7 files changed, 315 insertions, 33 deletions
diff --git a/lib/tinyusb b/lib/tinyusb -Subproject cc143cccf414f67bf2fc04751b12ee18c447997 +Subproject a2b7b9ddb9f3c01bd0572eba73c33548ee2ce1d diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index bfe47112e..a299fd15b 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -119,22 +119,6 @@ SRC_C += \ lib/libc/string0.c \ lib/mp-readline/readline.c \ -ifeq ($(MCU_SUB_VARIANT),nrf52840) - -SRC_C += \ - usb/usb.c \ - usb/usb_msc_flash.c \ - lib/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c \ - lib/tinyusb/src/portable/nordic/nrf5x/hal_nrf5x.c \ - lib/tinyusb/src/common/tusb_fifo.c \ - lib/tinyusb/src/device/usbd.c \ - lib/tinyusb/src/device/usbd_desc.c \ - lib/tinyusb/src/class/msc/msc_device.c \ - lib/tinyusb/src/class/cdc/cdc_device.c \ - lib/tinyusb/src/tusb.c \ - -endif - DRIVERS_SRC_C += $(addprefix modules/,\ ubluepy/modubluepy.c \ ubluepy/ubluepy_peripheral.c \ @@ -220,9 +204,29 @@ SRC_SHARED_BINDINGS = \ bitbangio/OneWire.c \ random/__init__.c \ -# usb_hid/__init__.c \ +# USB source files for nrf52840 +ifeq ($(MCU_SUB_VARIANT),nrf52840) + +SRC_C += \ + usb/usb.c \ + usb/usb_msc_flash.c \ + lib/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c \ + lib/tinyusb/src/portable/nordic/nrf5x/hal_nrf5x.c \ + lib/tinyusb/src/common/tusb_fifo.c \ + lib/tinyusb/src/device/usbd.c \ + lib/tinyusb/src/device/usbd_auto_desc.c \ + lib/tinyusb/src/class/msc/msc_device.c \ + lib/tinyusb/src/class/cdc/cdc_device.c \ + lib/tinyusb/src/class/hid/hid_device.c \ + lib/tinyusb/src/tusb.c \ + +SRC_COMMON_HAL += \ + usb_hid/__init__.c \ usb_hid/Device.c \ +endif + + SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_BINDINGS)) \ $(addprefix shared-module/, $(SRC_SHARED_MODULE)) diff --git a/ports/nrf/common-hal/usb_hid/Device.c b/ports/nrf/common-hal/usb_hid/Device.c new file mode 100644 index 000000000..27a50858f --- /dev/null +++ b/ports/nrf/common-hal/usb_hid/Device.c @@ -0,0 +1,90 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 hathach for Adafruit Industries + * + * 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. + */ + +#include <string.h> + +#include "tick.h" +#include "common-hal/usb_hid/Device.h" + +#include "py/runtime.h" +//#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/usb_hid/Device.h" +#include "tusb.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ + + +//------------- IMPLEMENTATION -------------// + +uint8_t common_hal_usb_hid_device_get_usage_page(usb_hid_device_obj_t *self) { + return self->usage_page; +} + +uint8_t common_hal_usb_hid_device_get_usage(usb_hid_device_obj_t *self) { + return self->usage; +} + +void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t* report, uint8_t len) { + if (len != self->report_length) { + mp_raise_ValueError_varg("Buffer incorrect size. Should be %d bytes.", self->report_length); + } + + // Wait until interface is ready, timeout = 2 seconds + uint64_t end_ticks = ticks_ms + 2000; + while ( (ticks_ms < end_ticks) && !tud_hid_generic_ready() ) { } + + if ( !tud_hid_generic_ready() ) { + mp_raise_msg(&mp_type_OSError, "USB Busy"); + } + + memcpy(self->report_buffer, report, len); + + if ( !tud_hid_generic_report(self->report_id, self->report_buffer, len) ) { + mp_raise_msg(&mp_type_OSError, "USB Error"); + } +} + +// Callbacks invoked when receive Get_Report request through control endpoint +uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) +{ + // only support Input Report + if ( report_type != HID_REPORT_TYPE_INPUT ) return 0; + + // fill buffer with current report + memcpy(buffer, usb_hid_devices[report_id].report_buffer, reqlen); + return reqlen; +} + +// Callbacks invoked when receive Set_Report request through control endpoint +void tud_hid_generic_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) +{ + if ( report_type == HID_REPORT_TYPE_OUTPUT ) { + // TODO Support Keyboard LED e.g Capslock + } +} + diff --git a/ports/nrf/common-hal/usb_hid/Device.h b/ports/nrf/common-hal/usb_hid/Device.h index c7f20f11b..f4a2e7eca 100644 --- a/ports/nrf/common-hal/usb_hid/Device.h +++ b/ports/nrf/common-hal/usb_hid/Device.h @@ -27,11 +27,28 @@ #ifndef COMMON_HAL_USB_HID_DEVICE_H #define COMMON_HAL_USB_HID_DEVICE_H +#include <stdint.h> +#include <stdbool.h> + +#include "py/obj.h" + #ifdef __cplusplus extern "C" { #endif -typedef void* usb_hid_device_obj_t; +#define USB_HID_NUM_DEVICES 2 + +typedef struct { + mp_obj_base_t base; + uint8_t* report_buffer; + uint8_t report_id; // If non-zero, prefix report with given id. + uint8_t report_length; // Length not including Report ID. + uint8_t usage_page; + uint8_t usage; +} usb_hid_device_obj_t; + + +usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES]; #ifdef __cplusplus } diff --git a/ports/nrf/common-hal/usb_hid/__init__.c b/ports/nrf/common-hal/usb_hid/__init__.c new file mode 100644 index 000000000..d88bf8e04 --- /dev/null +++ b/ports/nrf/common-hal/usb_hid/__init__.c @@ -0,0 +1,155 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 hathach for Adafruit Industries + * + * 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. + */ + +#include "py/obj.h" +#include "py/mphal.h" +#include "py/runtime.h" + +#include "common-hal/usb_hid/Device.h" +#include "shared-bindings/usb_hid/Device.h" + +#define USB_HID_REPORT_ID_KEYBOARD 1 +#define USB_HID_REPORT_LENGTH_KEYBOARD 8 + +#define USB_HID_REPORT_ID_MOUSE 2 +#define USB_HID_REPORT_LENGTH_MOUSE 4 + +// Buffers are report size + 1 to include the Report ID prefix byte if needed. +#ifdef USB_HID_REPORT_LENGTH_KEYBOARD +static uint8_t keyboard_report_buffer[USB_HID_REPORT_LENGTH_KEYBOARD + 1]; +#endif +#ifdef USB_HID_REPORT_ID_MOUSE +static uint8_t mouse_report_buffer[USB_HID_REPORT_LENGTH_MOUSE + 1]; +#endif +#ifdef USB_HID_REPORT_ID_CONSUMER +static uint8_t consumer_report_buffer[USB_HID_REPORT_LENGTH_CONSUMER + 1]; +#endif +#ifdef USB_HID_REPORT_ID_SYS_CONTROL +static uint8_t sys_control_report_buffer[USB_HID_REPORT_LENGTH_SYS_CONTROL + 1]; +#endif +#ifdef USB_HID_REPORT_ID_GAMEPAD +static uint8_t gamepad_report_buffer[USB_HID_REPORT_LENGTH_GAMEPAD + 1]; +#endif +#ifdef USB_HID_REPORT_ID_DIGITIZER +static uint8_t digitizer_report_buffer[USB_HID_REPORT_LENGTH_DIGITIZER + 1]; +#endif + +usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = { +#ifdef USB_HID_REPORT_ID_KEYBOARD + { + .base = { .type = &usb_hid_device_type }, + .report_buffer = keyboard_report_buffer, +// .endpoint = USB_HID_ENDPOINT_IN, + .report_id = USB_HID_REPORT_ID_KEYBOARD, + .report_length = USB_HID_REPORT_LENGTH_KEYBOARD, + .usage_page = 0x01, + .usage = 0x06, + }, +#endif +#ifdef USB_HID_REPORT_ID_MOUSE + { + .base = { .type = &usb_hid_device_type }, + .report_buffer = mouse_report_buffer, +// .endpoint = USB_HID_ENDPOINT_IN, + .report_id = USB_HID_REPORT_ID_MOUSE, + .report_length = USB_HID_REPORT_LENGTH_MOUSE, + .usage_page = 0x01, + .usage = 0x02, + }, +#endif +#ifdef USB_HID_REPORT_ID_CONSUMER + { + .base = { .type = &usb_hid_device_type }, + .report_buffer = consumer_report_buffer, + .endpoint = USB_HID_ENDPOINT_IN, + .report_id = USB_HID_REPORT_ID_CONSUMER, + .report_length = USB_HID_REPORT_LENGTH_CONSUMER, + .usage_page = 0x0C, + .usage = 0x01, + }, +#endif +#ifdef USB_HID_REPORT_ID_SYS_CONTROL + { + .base = { .type = &usb_hid_device_type }, + .report_buffer = sys_control_report_buffer, + .endpoint = USB_HID_ENDPOINT_IN, + .report_id = USB_HID_REPORT_ID_SYS_CONTROL, + .report_length = USB_HID_REPORT_LENGTH_SYS_CONTROL, + .usage_page = 0x01, + .usage = 0x80, + }, +#endif +#ifdef USB_HID_REPORT_ID_GAMEPAD + { + .base = { .type = &usb_hid_device_type }, + .report_buffer = gamepad_report_buffer, + .endpoint = USB_HID_ENDPOINT_IN, + .report_id = USB_HID_REPORT_ID_GAMEPAD, + .report_length = USB_HID_REPORT_LENGTH_GAMEPAD, + .usage_page = 0x01, + .usage = 0x05, + }, +#endif +#ifdef USB_HID_REPORT_ID_DIGITIZER + { + .base = { .type = &usb_hid_device_type }, + .report_buffer = digitizer_report_buffer, + .endpoint = USB_HID_ENDPOINT_IN, + .report_id = USB_HID_REPORT_ID_DIGITIZER, + .report_length = USB_HID_REPORT_LENGTH_DIGITIZER, + .usage_page = 0x0D, + .usage = 0x02, + }, +#endif +}; + + +mp_obj_tuple_t common_hal_usb_hid_devices = { + .base = { + .type = &mp_type_tuple, + }, + .len = USB_HID_NUM_DEVICES, + .items = { +#if USB_HID_NUM_DEVICES >= 1 + (mp_obj_t) &usb_hid_devices[0], +#endif +#if USB_HID_NUM_DEVICES >= 2 + (mp_obj_t) &usb_hid_devices[1], +#endif +#if USB_HID_NUM_DEVICES >= 3 + (mp_obj_t) &usb_hid_devices[2], +#endif +#if USB_HID_NUM_DEVICES >= 4 + (mp_obj_t) &usb_hid_devices[3], +#endif +#if USB_HID_NUM_DEVICES >= 5 + (mp_obj_t) &usb_hid_devices[4], +#endif +#if USB_HID_NUM_DEVICES >= 6 + (mp_obj_t) &usb_hid_devices[5], +#endif + } +}; diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index a522a8e67..d2672edd4 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -201,7 +201,7 @@ extern const struct _mp_obj_module_t mp_module_ubluepy; #ifdef NRF52840_XXAA #define USBHID_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid), (mp_obj_t)&usb_hid_module }, #else -#define USBHID_MOUDLE +#define USBHID_MODULE #endif #define MICROPY_PORT_BUILTIN_MODULES \ diff --git a/ports/nrf/usb/tusb_config.h b/ports/nrf/usb/tusb_config.h index 28f1be087..b4eadf03d 100644 --- a/ports/nrf/usb/tusb_config.h +++ b/ports/nrf/usb/tusb_config.h @@ -45,13 +45,13 @@ //--------------------------------------------------------------------+ // COMMON CONFIGURATION //--------------------------------------------------------------------+ -#define CFG_TUSB_MCU OPT_MCU_NRF5X -#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE +#define CFG_TUSB_MCU OPT_MCU_NRF5X +#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE -#define CFG_TUSB_DEBUG 0 +#define CFG_TUSB_DEBUG 0 /*------------- RTOS -------------*/ -#define CFG_TUSB_OS OPT_OS_NONE +#define CFG_TUSB_OS OPT_OS_NONE //#define CFG_TUD_TASK_QUEUE_SZ 16 //#define CFG_TUD_TASK_PRIO 0 //#define CFG_TUD_TASK_STACK_SZ 150 @@ -60,19 +60,35 @@ // DEVICE CONFIGURATION //--------------------------------------------------------------------+ -/*------------- Core -------------*/ -#define CFG_TUD_DESC_AUTO 1 -#define CFG_TUD_DESC_VID 0x239A -#define CFG_TUD_DESC_PID 0x802A +#define CFG_TUD_ENDOINT0_SIZE 64 + +/*------------- Descriptors -------------*/ +/* Enable auto generated descriptor, tinyusb will try its best to create + * descriptor ( device, configuration, hid ) that matches enabled CFG_* in this file + * + * Note: All CFG_TUD_DESC_* are relevant only if CFG_TUD_DESC_AUTO is enabled + */ +#define CFG_TUD_DESC_AUTO 1 + +/* Note: different class combination e.g CDC and (CDC + MSC) should have different + * PID since Host OS will "remembered" device driver after the first plug */ +#define CFG_TUD_DESC_VID 0x239A +#define CFG_TUD_DESC_PID 0x802A -#define CFG_TUD_ENDOINT0_SIZE 64 //------------- CLASS -------------// -#define CFG_TUD_CDC 1 -#define CFG_TUD_MSC 1 -#define CFG_TUD_HID_KEYBOARD 0 -#define CFG_TUD_HID_MOUSE 0 -#define CFG_TUD_HID 0 +#define CFG_TUD_CDC 1 +#define CFG_TUD_MSC 1 +#define CFG_TUD_HID 1 + +#define CFG_TUD_HID_KEYBOARD 1 +#define CFG_TUD_HID_MOUSE 1 + +/* Use Boot Protocol for Keyboard, Mouse. Enable this will create separated HID interface + * require more IN endpoints. If disabled, they they are all packed into a single + * multiple report interface called "Generic". */ +#define CFG_TUD_HID_KEYBOARD_BOOT 0 +#define CFG_TUD_HID_MOUSE_BOOT 0 /*------------------------------------------------------------------*/ /* CLASS DRIVER |
