Skip to content

Nrf52840 usb hid #1074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/tinyusb
Submodule tinyusb updated 61 files
+0 −1 examples/device/device_virtual_com/src/tusb_config.h
+2 −1 examples/device/nrf52840/segger/nrf52840.emProject
+68 −10 examples/device/nrf52840/src/main.c
+32 −48 examples/device/nrf52840/src/msc_device_app.c
+0 −2 examples/device/nrf52840/src/msc_device_app.h
+7 −5 examples/device/nrf52840/src/msc_device_ramdisk.c
+56 −29 examples/device/nrf52840/src/tusb_config.h
+25 −2 examples/device/nrf52840/src/tusb_descriptors.c
+8 −8 examples/obsolete/device/src/keyboard_device_app.c
+4 −9 examples/obsolete/device/src/mouse_device_app.c
+9 −77 examples/obsolete/device/src/msc_device_app.c
+2 −2 examples/obsolete/device/src/msc_device_ramdisk.c
+2 −2 examples/obsolete/device/src/msc_device_romdisk.c
+2 −3 examples/obsolete/device/src/tusb_config.h
+2 −0 examples/obsolete/host/src/msc_cli.c
+2 −0 examples/obsolete/host/src/msc_cli.h
+0 −1 hw/bsp/board.h
+1 −1 hw/bsp/lpcxpresso1769/board_lpcxpresso1769.c
+62 −18 hw/bsp/pca10056/board_pca10056.c
+2 −0 hw/bsp/pca10056/board_pca10056.h
+6 −7 src/class/cdc/cdc_device.c
+4 −2 src/class/custom/custom_device.c
+3 −1 src/class/custom/custom_device.h
+134 −110 src/class/hid/hid.h
+485 −159 src/class/hid/hid_device.c
+263 −87 src/class/hid/hid_device.h
+1 −1 src/class/hid/hid_host.c
+64 −40 src/class/msc/msc.h
+195 −84 src/class/msc/msc_device.c
+44 −51 src/class/msc/msc_device.h
+2 −2 src/class/msc/msc_host.c
+9 −16 src/common/tusb_common.h
+7 −7 src/common/tusb_compiler.h
+3 −0 src/common/tusb_error.h
+30 −8 src/common/tusb_timeout.h
+22 −0 src/common/tusb_types.h
+2 −0 src/common/tusb_verify.h
+1 −0 src/device/dcd.h
+47 −32 src/device/usbd.c
+12 −4 src/device/usbd.h
+247 −221 src/device/usbd_auto_desc.c
+5 −0 src/device/usbd_pvt.h
+4 −5 src/host/ehci/ehci.c
+0 −1 src/host/ohci/ohci.c
+19 −1 src/portable/nordic/nrf5x/dcd_nrf5x.c
+4 −48 src/portable/nordic/nrf5x/hal_nrf5x.c
+0 −0 src/portable/nordic/nrf5x/nrf_drv_usbd_errata.h
+1 −2 src/portable/nxp/lpc11xx_lpc13xx/dcd_lpc_11uxx_13uxx.c
+1 −1 src/portable/nxp/lpc17xx/dcd_lpc175x_6x.c
+2 −2 src/portable/nxp/lpc17xx/hal_lpc175x_6x.c
+1 −2 src/portable/nxp/lpc43xx_lpc18xx/dcd_lpc43xx.c
+6 −6 src/portable/nxp/lpc43xx_lpc18xx/hal_lpc43xx.c
+2 −2 src/tusb.c
+2 −2 src/tusb.h
+30 −16 src/tusb_option.h
+1 −1 tests/lpc175x_6x/test/test_usbd.c
+2 −2 tests/lpc18xx_43xx/test/host/hid/test_hidh_keyboard.c
+1 −1 tests/lpc18xx_43xx/test/host/hid/test_hidh_mouse.c
+1 −1 tests/lpc18xx_43xx/test/host/msc/test_msc_host.c
+2 −3 tests/support/tusb_config.h
+108 −0 tools/windows_cdc_driver/tusb_usbser.inf
42 changes: 24 additions & 18 deletions ports/nrf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ SRC_C += \
tick.c \
background.c \
internal_flash.c \
interrupt_char.c \
drivers/bluetooth/ble_drv.c \
drivers/bluetooth/ble_uart.c \
boards/$(BOARD)/board.c \
Expand All @@ -115,26 +114,11 @@ SRC_C += \
lib/utils/buffer_helper.c \
lib/utils/context_manager_helpers.c \
lib/utils/pyexec.c \
lib/utils/interrupt_char.c \
lib/utils/stdout_helpers.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 \
Expand Down Expand Up @@ -218,7 +202,29 @@ SRC_SHARED_BINDINGS = \
bitbangio/I2C.c \
bitbangio/SPI.c \
bitbangio/OneWire.c \
random/__init__.c
random/__init__.c \

# USB source files for nrf52840
ifeq ($(MCU_SUB_VARIANT),nrf52840)

SRC_C += \
usb/usb.c \
usb/usb_msc_flash.c \
usb/usb_desc.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/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)) \
Expand Down
88 changes: 88 additions & 0 deletions ports/nrf/common-hal/usb_hid/Device.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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/usb_hid/Device.h"
#include "tusb.h"

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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use mp_raise_OSError("USB Busy") here and similarly elsewhere. Easier to read and saves a few bytes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mp_raise_OSError takes int parameter, since usb does have its error enu, what should be the value for these in the list
https://github.com/adafruit/circuitpython/blob/master/py/mperrno.h

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, never mind, I forgot that it takes only an integer arg.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, nrf52 still got plenty of space left anyway :D

}

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;

// index is ID-1
uint8_t idx = ( report_id ? (report_id-1) : 0 );

// fill buffer with current report
memcpy(buffer, usb_hid_devices[idx].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) {
// index is ID-1
uint8_t idx = ( report_id ? (report_id-1) : 0 );

if ( report_type == HID_REPORT_TYPE_OUTPUT ) {
// Check if it is Keyboard device
if ( (usb_hid_devices[idx].usage_page == HID_USAGE_PAGE_DESKTOP) && (usb_hid_devices[idx].usage == HID_USAGE_DESKTOP_KEYBOARD) ) {
// This is LED indicator (CapsLock, NumLock)
// TODO Light up some LED here
}
}
}

94 changes: 94 additions & 0 deletions ports/nrf/common-hal/usb_hid/Device.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.
*/

#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

// 1 to enable device, 0 to disable
#define USB_HID_DEVICE_KEYBOARD 1
#define USB_HID_DEVICE_MOUSE 1
#define USB_HID_DEVICE_CONSUMER 1
#define USB_HID_DEVICE_SYS_CONTROL 1
#define USB_HID_DEVICE_GAMEPAD 1
#define USB_HID_DEVICE_DIGITIZER 0 // not supported yet
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhalbert list of enabled hid devices is here, just in case you wan to disable any of these.


enum {
USB_HID_REPORT_ID_UNUSED = 0,

#if USB_HID_DEVICE_KEYBOARD
USB_HID_REPORT_ID_KEYBOARD,
#endif

#if USB_HID_DEVICE_MOUSE
USB_HID_REPORT_ID_MOUSE,
#endif

#if USB_HID_DEVICE_CONSUMER
USB_HID_REPORT_ID_CONSUMER,
#endif

#if USB_HID_DEVICE_SYS_CONTROL
USB_HID_REPORT_ID_SYS_CONTROL,
#endif

#if USB_HID_DEVICE_GAMEPAD
USB_HID_REPORT_ID_GAMEPAD,
#endif

#if USB_HID_DEVICE_DIGITIZER
USB_HID_REPORT_ID_DIGITIZER,
#endif
};

#define USB_HID_NUM_DEVICES (USB_HID_DEVICE_KEYBOARD + USB_HID_DEVICE_MOUSE + USB_HID_DEVICE_CONSUMER + \
USB_HID_DEVICE_SYS_CONTROL + USB_HID_DEVICE_GAMEPAD + USB_HID_DEVICE_DIGITIZER )

typedef struct {
mp_obj_base_t base;
uint8_t* report_buffer;
uint8_t report_id;
uint8_t report_length;
uint8_t usage_page;
uint8_t usage;
} usb_hid_device_obj_t;


extern usb_hid_device_obj_t usb_hid_devices[];

#ifdef __cplusplus
}
#endif

#endif /* COMMON_HAL_USB_HID_DEVICE_H */
Loading