From 5bdf40901fe6592ce094e3d73927b03f54f96e30 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 26 Jul 2018 17:15:45 +0700 Subject: [PATCH 01/19] update tusb lib --- lib/tinyusb | 2 +- ports/nrf/usb/usb.c | 9 +++- ports/nrf/usb/usb_msc_flash.c | 83 +++++++++-------------------------- 3 files changed, 30 insertions(+), 64 deletions(-) diff --git a/lib/tinyusb b/lib/tinyusb index 421ae8fc82..e362f5fd64 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit 421ae8fc82b884e087e99c8ab3fa25883a3dd623 +Subproject commit e362f5fd64d257e3ad70962be59fc8593a0f5d95 diff --git a/ports/nrf/usb/usb.c b/ports/nrf/usb/usb.c index 648ee708dd..76ae2ad39a 100644 --- a/ports/nrf/usb/usb.c +++ b/ports/nrf/usb/usb.c @@ -71,7 +71,14 @@ tud_desc_set_t tud_desc_set = .device = NULL, .config = NULL, .string_arr = (uint8_t const **) string_desc_arr, - .hid_report = NULL + .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), + + .hid_report = + { + .composite = NULL, + .boot_keyboard = NULL, + .boot_mouse = NULL + } }; diff --git a/ports/nrf/usb/usb_msc_flash.c b/ports/nrf/usb/usb_msc_flash.c index f77a9e5c5c..3d832036ec 100644 --- a/ports/nrf/usb/usb_msc_flash.c +++ b/ports/nrf/usb/usb_msc_flash.c @@ -53,90 +53,51 @@ #define FL_PAGE_SZ 4096 -static scsi_sense_fixed_data_t mscd_sense_data = -{ - .response_code = 0x70, - .sense_key = 0, // no errors - .additional_sense_len = sizeof(scsi_sense_fixed_data_t) - 8 -}; - -static scsi_mode_parameters_t const msc_dev_mode_para = -{ - .mode_data_length = 3, - .medium_type = 0, - .device_specific_para = 0, - .block_descriptor_length = 0 -}; - //--------------------------------------------------------------------+ // tinyusb callbacks //--------------------------------------------------------------------+ -int32_t tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) { +int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) { // read10 & write10 has their own callback and MUST not be handled here - void const* ptr = NULL; + void const* resp = NULL; uint16_t len = 0; - // most scsi handled is input - bool in_xfer = true; - - switch ( scsi_cmd[0] ) - { - case SCSI_CMD_REQUEST_SENSE: - ptr = &mscd_sense_data; - len = sizeof(scsi_sense_fixed_data_t); - break; - - case SCSI_CMD_MODE_SENSE_6: - ptr = &msc_dev_mode_para; - len = sizeof(msc_dev_mode_para); - break; - + switch ( scsi_cmd[0] ) { case SCSI_CMD_TEST_UNIT_READY: - ptr = NULL; + // Command that host uses to check our readiness before sending other commands len = 0; break; case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL: - ptr = NULL; + // Host is about to read/write etc ... better not to disconnect disk len = 0; break; - default: - // negative is error -> Data stage is STALL, status = failed - return -1; + case SCSI_CMD_START_STOP_UNIT: + // Host try to eject/safe remove/powerof us. We could safely disconnect with disk storage, or go into lower power + // scsi_start_stop_unit_t const * cmd_start_stop = (scsi_start_stop_unit_t const *) scsi_cmd + len = 0; + break; + + // negative means error -> tusb could stall and/or response with failed status + default: return -1; } // return len must not larger than bufsize - TU_ASSERT(bufsize >= len); + if ( len > bufsize ) len = bufsize; - if ( ptr && len ) - { - if ( in_xfer ) - { - memcpy(buffer, ptr, len); - } else - { - // SCSI output - } - } - - //------------- clear sense data if it is not request sense command -------------// - if ( SCSI_CMD_REQUEST_SENSE != scsi_cmd[0] ) - { - mscd_sense_data.sense_key = SCSI_SENSEKEY_NONE; - mscd_sense_data.additional_sense_code = 0; - mscd_sense_data.additional_sense_qualifier = 0; + // copy response to stack's buffer if any + if ( resp && len ) { + memcpy(buffer, resp, len); } return len; } /*------------------------------------------------------------------*/ -/* Tinyusb Flash READ10 & WRITE10 +/* tinyusb Flash READ10 & WRITE10 *------------------------------------------------------------------*/ -int32_t tud_msc_read10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { - (void) rhport; +int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { (void) lun; (void) offset; @@ -147,8 +108,7 @@ int32_t tud_msc_read10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint32_t o return block_count * MSC_FLASH_BLOCK_SIZE; } -int32_t tud_msc_write10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { - (void) rhport; +int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { (void) lun; (void) offset; @@ -167,8 +127,7 @@ int32_t tud_msc_write10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint32_t return block_count * MSC_FLASH_BLOCK_SIZE; } -void tud_msc_write10_complete_cb (uint8_t rhport, uint8_t lun) { - (void) rhport; +void tud_msc_write10_complete_cb (uint8_t lun) { (void) lun; // flush pending cache when write10 is complete From e86f7d0b8856c7ca83bafab1471d2c53076d3dcd Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 26 Jul 2018 22:34:57 +0700 Subject: [PATCH 02/19] add usb msc callback description --- lib/tinyusb | 2 +- ports/nrf/usb/usb_msc_flash.c | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/tinyusb b/lib/tinyusb index e362f5fd64..72b600c393 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit e362f5fd64d257e3ad70962be59fc8593a0f5d95 +Subproject commit 72b600c393e543361b073abee3da823d41383660 diff --git a/ports/nrf/usb/usb_msc_flash.c b/ports/nrf/usb/usb_msc_flash.c index 3d832036ec..cd37edbcb7 100644 --- a/ports/nrf/usb/usb_msc_flash.c +++ b/ports/nrf/usb/usb_msc_flash.c @@ -53,12 +53,10 @@ #define FL_PAGE_SZ 4096 -//--------------------------------------------------------------------+ -// tinyusb callbacks -//--------------------------------------------------------------------+ +// Callback invoked when received an SCSI command not in built-in list below +// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE +// - READ10 and WRITE10 has their own callbacks int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) { - // read10 & write10 has their own callback and MUST not be handled here - void const* resp = NULL; uint16_t len = 0; @@ -74,7 +72,7 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, break; case SCSI_CMD_START_STOP_UNIT: - // Host try to eject/safe remove/powerof us. We could safely disconnect with disk storage, or go into lower power + // Host try to eject/safe remove/poweroff us. We could safely disconnect with disk storage, or go into lower power // scsi_start_stop_unit_t const * cmd_start_stop = (scsi_start_stop_unit_t const *) scsi_cmd len = 0; break; @@ -94,9 +92,8 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, return len; } -/*------------------------------------------------------------------*/ -/* tinyusb Flash READ10 & WRITE10 - *------------------------------------------------------------------*/ +// Callback invoked when received READ10 command. +// Copy disk's data to buffer (up to bufsize) and return number of copied bytes. int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { (void) lun; (void) offset; @@ -108,6 +105,8 @@ int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buf return block_count * MSC_FLASH_BLOCK_SIZE; } +// Callback invoked when received WRITE10 command. +// Process data in buffer to disk's storage and return number of written bytes int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { (void) lun; (void) offset; @@ -127,6 +126,8 @@ int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* bu return block_count * MSC_FLASH_BLOCK_SIZE; } +// Callback invoked when WRITE10 command is completed (status received and accepted by host). +// used to flush any pending cache. void tud_msc_write10_complete_cb (uint8_t lun) { (void) lun; From 21339c415524453b8ca991532c637de5cbfe1d31 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 27 Jul 2018 17:22:21 +0700 Subject: [PATCH 03/19] house keeping --- lib/tinyusb | 2 +- ports/nrf/usb/usb_msc_flash.c | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/tinyusb b/lib/tinyusb index 72b600c393..cf6c534c19 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit 72b600c393e543361b073abee3da823d41383660 +Subproject commit cf6c534c1974f4efeb5c136697d1f1028e6aa34a diff --git a/ports/nrf/usb/usb_msc_flash.c b/ports/nrf/usb/usb_msc_flash.c index cd37edbcb7..7972a408b8 100644 --- a/ports/nrf/usb/usb_msc_flash.c +++ b/ports/nrf/usb/usb_msc_flash.c @@ -57,24 +57,29 @@ // - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE // - READ10 and WRITE10 has their own callbacks int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) { - void const* resp = NULL; - uint16_t len = 0; + void const* response = NULL; + uint16_t resplen = 0; switch ( scsi_cmd[0] ) { case SCSI_CMD_TEST_UNIT_READY: // Command that host uses to check our readiness before sending other commands - len = 0; + resplen = 0; break; case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL: // Host is about to read/write etc ... better not to disconnect disk - len = 0; + resplen = 0; break; case SCSI_CMD_START_STOP_UNIT: // Host try to eject/safe remove/poweroff us. We could safely disconnect with disk storage, or go into lower power - // scsi_start_stop_unit_t const * cmd_start_stop = (scsi_start_stop_unit_t const *) scsi_cmd - len = 0; + /* scsi_start_stop_unit_t const * start_stop = (scsi_start_stop_unit_t const *) scsi_cmd; + // Start bit = 0 : low power mode, if load_eject = 1 : unmount disk storage as well + // Start bit = 1 : Ready mode, if load_eject = 1 : mount disk storage + start_stop->start; + start_stop->load_eject; + */ + resplen = 0; break; // negative means error -> tusb could stall and/or response with failed status @@ -82,14 +87,14 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, } // return len must not larger than bufsize - if ( len > bufsize ) len = bufsize; + if ( resplen > bufsize ) resplen = bufsize; // copy response to stack's buffer if any - if ( resp && len ) { - memcpy(buffer, resp, len); + if ( response && resplen ) { + memcpy(buffer, response, resplen); } - return len; + return resplen; } // Callback invoked when received READ10 command. From 368d59796f96a21b6b3627f33b29a66ac0861dec Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 27 Jul 2018 17:36:41 +0700 Subject: [PATCH 04/19] adding usb_hid compiled with undefined ref --- ports/nrf/Makefile | 5 ++-- ports/nrf/common-hal/usb_hid/Device.h | 40 +++++++++++++++++++++++++++ ports/nrf/mpconfigport.h | 18 ++++++++---- 3 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 ports/nrf/common-hal/usb_hid/Device.h diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 0057bdc249..5b1164fbda 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -218,8 +218,9 @@ SRC_SHARED_BINDINGS = \ bitbangio/I2C.c \ bitbangio/SPI.c \ bitbangio/OneWire.c \ - random/__init__.c - + random/__init__.c \ + usb_hid/__init__.c \ + usb_hid/Device.c \ 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.h b/ports/nrf/common-hal/usb_hid/Device.h new file mode 100644 index 0000000000..c7f20f11be --- /dev/null +++ b/ports/nrf/common-hal/usb_hid/Device.h @@ -0,0 +1,40 @@ +/* + * 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 + +#ifdef __cplusplus + extern "C" { +#endif + +typedef void* usb_hid_device_obj_t; + +#ifdef __cplusplus + } +#endif + +#endif /* COMMON_HAL_USB_HID_DEVICE_H */ diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 7731534b9d..a522a8e673 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -181,6 +181,7 @@ extern const struct _mp_obj_module_t struct_module; extern const struct _mp_obj_module_t time_module; extern const struct _mp_obj_module_t supervisor_module; extern const struct _mp_obj_module_t gamepad_module; +extern const struct _mp_obj_module_t usb_hid_module; extern const struct _mp_obj_module_t bleio_module; extern const struct _mp_obj_module_t mp_module_ubluepy; @@ -197,6 +198,12 @@ extern const struct _mp_obj_module_t mp_module_ubluepy; #define BLEIO_MODULE #endif +#ifdef NRF52840_XXAA +#define USBHID_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid), (mp_obj_t)&usb_hid_module }, +#else +#define USBHID_MOUDLE +#endif + #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR (MP_QSTR_board ), (mp_obj_t)&board_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_busio ), (mp_obj_t)&busio_module }, \ @@ -207,19 +214,20 @@ extern const struct _mp_obj_module_t mp_module_ubluepy; { MP_OBJ_NEW_QSTR (MP_QSTR_bitbangio ), (mp_obj_t)&bitbangio_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_os ), (mp_obj_t)&os_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_random ), (mp_obj_t)&random_module }, \ - { MP_OBJ_NEW_QSTR (MP_QSTR_storage ), (mp_obj_t)&storage_module },\ + { MP_OBJ_NEW_QSTR (MP_QSTR_storage ), (mp_obj_t)&storage_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_struct ), (mp_obj_t)&struct_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_supervisor ), (mp_obj_t)&supervisor_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_gamepad ), (mp_obj_t)&gamepad_module }, \ { MP_OBJ_NEW_QSTR (MP_QSTR_time ), (mp_obj_t)&time_module }, \ - BLEIO_MODULE \ + USBHID_MODULE \ + BLEIO_MODULE \ UBLUEPY_MODULE \ // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ - { MP_ROM_QSTR (MP_QSTR_help), MP_ROM_PTR(&mp_builtin_help_obj) }, \ - { MP_OBJ_NEW_QSTR (MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \ - { MP_ROM_QSTR (MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) }, \ + { MP_ROM_QSTR (MP_QSTR_help ), MP_ROM_PTR(&mp_builtin_help_obj) }, \ + { MP_OBJ_NEW_QSTR (MP_QSTR_input ), (mp_obj_t)&mp_builtin_input_obj }, \ + { MP_ROM_QSTR (MP_QSTR_open ), MP_ROM_PTR(&mp_builtin_open_obj) }, \ #define MP_STATE_PORT MP_STATE_VM From ae783b0b1afb61928fd0038275d2450956e9cc9c Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 31 Jul 2018 13:54:47 +0700 Subject: [PATCH 05/19] update tinyusb, set scsi sense key for unsupported commands --- lib/tinyusb | 2 +- ports/nrf/usb/usb_msc_flash.c | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/tinyusb b/lib/tinyusb index cf6c534c19..191b73b58c 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit cf6c534c1974f4efeb5c136697d1f1028e6aa34a +Subproject commit 191b73b58c5ad6a098c652f31459ef39078a484b diff --git a/ports/nrf/usb/usb_msc_flash.c b/ports/nrf/usb/usb_msc_flash.c index 7972a408b8..384ef55780 100644 --- a/ports/nrf/usb/usb_msc_flash.c +++ b/ports/nrf/usb/usb_msc_flash.c @@ -82,8 +82,13 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, resplen = 0; break; - // negative means error -> tusb could stall and/or response with failed status - default: return -1; + default: + // Set Sense = Invalid Command Operation + tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); + + // negative means error -> tinyusb could stall and/or response with failed status + resplen = -1; + break; } // return len must not larger than bufsize From 221d54a595ca0554d3903b9bc1bf0080696912e4 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 31 Jul 2018 14:28:34 +0700 Subject: [PATCH 06/19] workaround to use lib/utils/interrupt_char.c --- lib/tinyusb | 2 +- ports/nrf/Makefile | 5 +++-- ports/nrf/usb/tusb_config.h | 2 +- ports/nrf/usb/usb.c | 11 ++++++++++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/tinyusb b/lib/tinyusb index 191b73b58c..cc143cccf4 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit 191b73b58c5ad6a098c652f31459ef39078a484b +Subproject commit cc143cccf414f67bf2fc04751b12ee18c447997b diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 5b1164fbda..bfe47112e6 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -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 \ @@ -115,6 +114,7 @@ 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 \ @@ -219,7 +219,8 @@ SRC_SHARED_BINDINGS = \ bitbangio/SPI.c \ bitbangio/OneWire.c \ random/__init__.c \ - usb_hid/__init__.c \ + +# usb_hid/__init__.c \ usb_hid/Device.c \ SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_BINDINGS)) \ diff --git a/ports/nrf/usb/tusb_config.h b/ports/nrf/usb/tusb_config.h index 755e4cb6a6..28f1be0877 100644 --- a/ports/nrf/usb/tusb_config.h +++ b/ports/nrf/usb/tusb_config.h @@ -72,7 +72,7 @@ #define CFG_TUD_MSC 1 #define CFG_TUD_HID_KEYBOARD 0 #define CFG_TUD_HID_MOUSE 0 -#define CFG_TUD_HID_GENERIC 0 +#define CFG_TUD_HID 0 /*------------------------------------------------------------------*/ /* CLASS DRIVER diff --git a/ports/nrf/usb/usb.c b/ports/nrf/usb/usb.c index 76ae2ad39a..704ed59092 100644 --- a/ports/nrf/usb/usb.c +++ b/ports/nrf/usb/usb.c @@ -29,6 +29,7 @@ #include "tick.h" #include "usb.h" #include "lib/utils/interrupt_char.h" +#include "lib/mp-readline/readline.h" #ifdef SOFTDEVICE_PRESENT #include "nrf_sdm.h" @@ -75,7 +76,7 @@ tud_desc_set_t tud_desc_set = .hid_report = { - .composite = NULL, + .generic = NULL, .boot_keyboard = NULL, .boot_mouse = NULL } @@ -129,6 +130,12 @@ void usb_init(void) { } tusb_init(); + +#if MICROPY_KBD_EXCEPTION + // Set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() callback will be invoked when Ctrl+C is received + // This callback always got invoked regardless of mp_interrupt_char value since we only set it once here + tud_cdc_set_wanted_char(CHAR_CTRL_C); +#endif } //--------------------------------------------------------------------+ @@ -158,6 +165,8 @@ void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) { (void) itf; // not used + // Workaround for using lib/utils/interrupt_char.c + // Compare mp_interrupt_char with wanted_char and ignore if not matched if (mp_interrupt_char == wanted_char) { tud_cdc_read_flush(); // flush read fifo mp_keyboard_interrupt(); From d15caf0dbd2c86ae0fe65bb8e5b7eedf9f5617ab Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 31 Jul 2018 16:42:04 +0700 Subject: [PATCH 07/19] hid keyboard and mouse work well --- lib/tinyusb | 2 +- ports/nrf/Makefile | 38 +++--- ports/nrf/common-hal/usb_hid/Device.c | 90 ++++++++++++++ ports/nrf/common-hal/usb_hid/Device.h | 19 ++- ports/nrf/common-hal/usb_hid/__init__.c | 155 ++++++++++++++++++++++++ ports/nrf/mpconfigport.h | 2 +- ports/nrf/usb/tusb_config.h | 44 ++++--- 7 files changed, 316 insertions(+), 34 deletions(-) create mode 100644 ports/nrf/common-hal/usb_hid/Device.c create mode 100644 ports/nrf/common-hal/usb_hid/__init__.c diff --git a/lib/tinyusb b/lib/tinyusb index cc143cccf4..a2b7b9ddb9 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit cc143cccf414f67bf2fc04751b12ee18c447997b +Subproject commit a2b7b9ddb9f3c01bd0572eba73c33548ee2ce1df diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index bfe47112e6..a299fd15b1 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 0000000000..27a50858f9 --- /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 + +#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 c7f20f11be..f4a2e7ecaf 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 +#include + +#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 0000000000..d88bf8e04c --- /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 a522a8e673..d2672edd4e 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 28f1be0877..b4eadf03d5 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 From 27b2a9fe5959893a87f83944f7f6755298333e52 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 31 Jul 2018 17:50:32 +0700 Subject: [PATCH 08/19] add usb_desc, disable tinyusb stack CFG_TUD_DESC_AUTO --- ports/nrf/Makefile | 2 +- ports/nrf/common-hal/usb_hid/Device.h | 4 + ports/nrf/common-hal/usb_hid/__init__.c | 5 +- ports/nrf/usb/tusb_config.h | 22 +- ports/nrf/usb/usb.c | 47 --- ports/nrf/usb/usb_desc.c | 378 ++++++++++++++++++++++++ ports/nrf/usb/usb_desc.h | 87 ++++++ 7 files changed, 476 insertions(+), 69 deletions(-) create mode 100644 ports/nrf/usb/usb_desc.c create mode 100644 ports/nrf/usb/usb_desc.h diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index a299fd15b1..367ef6e5f0 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -210,11 +210,11 @@ 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/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 \ diff --git a/ports/nrf/common-hal/usb_hid/Device.h b/ports/nrf/common-hal/usb_hid/Device.h index f4a2e7ecaf..291bc963c3 100644 --- a/ports/nrf/common-hal/usb_hid/Device.h +++ b/ports/nrf/common-hal/usb_hid/Device.h @@ -38,6 +38,10 @@ #define USB_HID_NUM_DEVICES 2 +#define USB_HID_REPORT_ID_KEYBOARD 1 +#define USB_HID_REPORT_ID_MOUSE 2 +//#define USB_HID_REPORT_ID_CONSUMER 3 + typedef struct { mp_obj_base_t base; uint8_t* report_buffer; diff --git a/ports/nrf/common-hal/usb_hid/__init__.c b/ports/nrf/common-hal/usb_hid/__init__.c index d88bf8e04c..9ebf041ab5 100644 --- a/ports/nrf/common-hal/usb_hid/__init__.c +++ b/ports/nrf/common-hal/usb_hid/__init__.c @@ -31,14 +31,11 @@ #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 +#ifdef USB_HID_REPORT_ID_KEYBOARD static uint8_t keyboard_report_buffer[USB_HID_REPORT_LENGTH_KEYBOARD + 1]; #endif #ifdef USB_HID_REPORT_ID_MOUSE diff --git a/ports/nrf/usb/tusb_config.h b/ports/nrf/usb/tusb_config.h index b4eadf03d5..626b99eeb2 100644 --- a/ports/nrf/usb/tusb_config.h +++ b/ports/nrf/usb/tusb_config.h @@ -60,7 +60,7 @@ // DEVICE CONFIGURATION //--------------------------------------------------------------------+ -#define CFG_TUD_ENDOINT0_SIZE 64 +#define CFG_TUD_ENDOINT0_SIZE 64 /*------------- Descriptors -------------*/ /* Enable auto generated descriptor, tinyusb will try its best to create @@ -68,32 +68,18 @@ * * 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_DESC_AUTO 0 //------------- CLASS -------------// #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 *------------------------------------------------------------------*/ +/*------------- CDC -------------*/ // FIFO size of CDC TX and RX #define CFG_TUD_CDC_RX_BUFSIZE 1024 #define CFG_TUD_CDC_TX_BUFSIZE 1024 @@ -105,6 +91,8 @@ */ #define CFG_TUD_CDC_FLUSH_ON_SOF 0 + +/*------------- MSC -------------*/ // Number of supported Logical Unit Number (At least 1) #define CFG_TUD_MSC_MAXLUN 1 diff --git a/ports/nrf/usb/usb.c b/ports/nrf/usb/usb.c index 704ed59092..953b61e396 100644 --- a/ports/nrf/usb/usb.c +++ b/ports/nrf/usb/usb.c @@ -36,53 +36,6 @@ #include "nrf_soc.h" #endif -//--------------------------------------------------------------------+ -// STRING DESCRIPTORS -//--------------------------------------------------------------------+ - -// array of pointer to string descriptors -uint16_t const * const string_desc_arr [] = -{ - // 0 index is supported language = English - TUD_DESC_STRCONV(0x0409), - - // Manufacturer - TUD_DESC_STRCONV('A','d','a','f','r','u','i','t',' ','I','n','d','u','s','t','r','i','e','s'), - - // Product - TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','n','R','F','5','2'), - - // Serials TODO use chip ID - TUD_DESC_STRCONV('1', '2', '3', '4', '5'), - - // CDC Interface - TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','S','e','r','i','a','l'), - - // MSC Interface - TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','S','t','o','r','a','g','e'), - - // Custom Interface - TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','C','u','s','t','o','m') -}; - -// tud_desc_set is required by tinyusb stack -// since CFG_TUD_DESC_AUTO is enabled, we only need to set string_arr -tud_desc_set_t tud_desc_set = -{ - .device = NULL, - .config = NULL, - .string_arr = (uint8_t const **) string_desc_arr, - .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), - - .hid_report = - { - .generic = NULL, - .boot_keyboard = NULL, - .boot_mouse = NULL - } -}; - - /* tinyusb function that handles power event (detected, ready, removed) * We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled. */ diff --git a/ports/nrf/usb/usb_desc.c b/ports/nrf/usb/usb_desc.c new file mode 100644 index 0000000000..1e3e8b2c28 --- /dev/null +++ b/ports/nrf/usb/usb_desc.c @@ -0,0 +1,378 @@ +/* + * 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 "usb_desc.h" +#include "common-hal/usb_hid/Device.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ +#define USB_VID 0x239A + +/* 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 USB_PID 0x802A + +/*------------- Interface Numbering -------------*/ +#define ITF_NUM_CDC 0 +#define ITF_NUM_MSC 2 +#define ITF_NUM_HID_GEN 3 +#define ITF_TOTAL 4 + + +#define ITF_STR_CDC 4 +#define ITF_STR_MSC 5 +#define ITF_STR_HID 6 + +/*------------- Endpoint Numbering & Size -------------*/ +#define _EP_IN(x) (0x80 | (x)) +#define _EP_OUT(x) (x) + +// CDC +#define EP_CDC_NOTIF _EP_IN ( ITF_NUM_CDC+1 ) +#define EP_CDC_NOTIF_SIZE 8 + +#define EP_CDC_OUT _EP_OUT( ITF_NUM_CDC+2 ) +#define EP_CDC_IN _EP_IN ( ITF_NUM_CDC+2 ) + +// Mass Storage +#define EP_MSC_OUT _EP_OUT( ITF_NUM_MSC+1 ) +#define EP_MSC_IN _EP_IN ( ITF_NUM_MSC+1 ) + +// HID composite = keyboard + mouse + gamepad + etc ... +#define EP_HID_GEN _EP_IN ( ITF_NUM_HID_GEN+1 ) +#define EP_HID_GEN_SIZE 16 + +//--------------------------------------------------------------------+ +// STRING DESCRIPTORS +//--------------------------------------------------------------------+ + +// array of pointer to string descriptors +uint16_t const * const string_desc_arr [] = +{ + // 0 index is supported language = English + TUD_DESC_STRCONV(0x0409), + + // 1 Manufacturer + TUD_DESC_STRCONV('A','d','a','f','r','u','i','t',' ','I','n','d','u','s','t','r','i','e','s'), + + // 2 Product + TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','n','R','F','5','2'), + + // 3 Serials TODO use chip ID + TUD_DESC_STRCONV('1', '2', '3', '4', '5'), + + // 4 CDC Interface + TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','S','e','r','i','a','l'), + + // 5 MSC Interface + TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','S','t','o','r','a','g','e'), + + // 6 HID Interface + TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','H','I','D'), + + // Custom Interface +// TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','C','u','s','t','o','m') +}; + + +//--------------------------------------------------------------------+ +// Device Descriptor +//--------------------------------------------------------------------+ +tusb_desc_device_t const usb_desc_dev = +{ + .bLength = sizeof(tusb_desc_device_t), + .bDescriptorType = TUSB_DESC_DEVICE, + .bcdUSB = 0x0200, + + #if CFG_TUD_CDC + // Use Interface Association Descriptor (IAD) for CDC + // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1) + .bDeviceClass = TUSB_CLASS_MISC, + .bDeviceSubClass = MISC_SUBCLASS_COMMON, + .bDeviceProtocol = MISC_PROTOCOL_IAD, + #else + .bDeviceClass = 0x00, + .bDeviceSubClass = 0x00, + .bDeviceProtocol = 0x00, + #endif + + .bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE, + + .idVendor = USB_VID, + .idProduct = USB_PID, + .bcdDevice = 0x0100, + + .iManufacturer = 0x01, + .iProduct = 0x02, + .iSerialNumber = 0x03, + + .bNumConfigurations = 0x01 // TODO multiple configurations +}; + +//--------------------------------------------------------------------+ +// HID Report Descriptor +//--------------------------------------------------------------------+ +// TODO remove const for dynamic descriptors + + +uint8_t const usb_desc_hid_generic_report[] = +{ +#if USB_HID_REPORT_ID_KEYBOARD + HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(USB_HID_REPORT_ID_KEYBOARD), ), +#endif + +#if USB_HID_REPORT_ID_MOUSE + HID_REPORT_DESC_MOUSE( HID_REPORT_ID(USB_HID_REPORT_ID_MOUSE), ) +#endif + +}; + + +//--------------------------------------------------------------------+ +// Configuration Descriptor +//--------------------------------------------------------------------+ +// TODO remove const for dynamic descriptors +usb_desc_cfg_t const usb_desc_cfg = +{ + .config = + { + .bLength = sizeof(tusb_desc_configuration_t), + .bDescriptorType = TUSB_DESC_CONFIGURATION, + + .wTotalLength = sizeof(usb_desc_cfg_t), + .bNumInterfaces = ITF_TOTAL, + + .bConfigurationValue = 1, + .iConfiguration = 0x00, + .bmAttributes = TUSB_DESC_CONFIG_ATT_BUS_POWER, + .bMaxPower = TUSB_DESC_CONFIG_POWER_MA(100) + }, + + // IAD points to CDC Interfaces + .cdc = + { + .iad = + { + .bLength = sizeof(tusb_desc_interface_assoc_t), + .bDescriptorType = TUSB_DESC_INTERFACE_ASSOCIATION, + + .bFirstInterface = ITF_NUM_CDC, + .bInterfaceCount = 2, + + .bFunctionClass = TUSB_CLASS_CDC, + .bFunctionSubClass = CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, + .bFunctionProtocol = CDC_COMM_PROTOCOL_ATCOMMAND, + .iFunction = 0 + }, + + //------------- CDC Communication Interface -------------// + .comm_itf = + { + .bLength = sizeof(tusb_desc_interface_t), + .bDescriptorType = TUSB_DESC_INTERFACE, + .bInterfaceNumber = ITF_NUM_CDC, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = TUSB_CLASS_CDC, + .bInterfaceSubClass = CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, + .bInterfaceProtocol = CDC_COMM_PROTOCOL_ATCOMMAND, + .iInterface = ITF_STR_CDC + }, + + .header = + { + .bLength = sizeof(cdc_desc_func_header_t), + .bDescriptorType = TUSB_DESC_CLASS_SPECIFIC, + .bDescriptorSubType = CDC_FUNC_DESC_HEADER, + .bcdCDC = 0x0120 + }, + + .call = + { + .bLength = sizeof(cdc_desc_func_call_management_t), + .bDescriptorType = TUSB_DESC_CLASS_SPECIFIC, + .bDescriptorSubType = CDC_FUNC_DESC_CALL_MANAGEMENT, + .bmCapabilities = { 0 }, + .bDataInterface = ITF_NUM_CDC+1, + }, + + .acm = + { + .bLength = sizeof(cdc_desc_func_acm_t), + .bDescriptorType = TUSB_DESC_CLASS_SPECIFIC, + .bDescriptorSubType = CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, + .bmCapabilities = { // 0x02 + .support_line_request = 1, + } + }, + + .union_func = + { + .bLength = sizeof(cdc_desc_func_union_t), // plus number of + .bDescriptorType = TUSB_DESC_CLASS_SPECIFIC, + .bDescriptorSubType = CDC_FUNC_DESC_UNION, + .bControlInterface = ITF_NUM_CDC, + .bSubordinateInterface = ITF_NUM_CDC+1, + }, + + .ep_notif = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_CDC_NOTIF, + .bmAttributes = { .xfer = TUSB_XFER_INTERRUPT }, + .wMaxPacketSize = { .size = EP_CDC_NOTIF_SIZE }, + .bInterval = 0x10 + }, + + //------------- CDC Data Interface -------------// + .data_itf = + { + .bLength = sizeof(tusb_desc_interface_t), + .bDescriptorType = TUSB_DESC_INTERFACE, + .bInterfaceNumber = ITF_NUM_CDC+1, + .bAlternateSetting = 0x00, + .bNumEndpoints = 2, + .bInterfaceClass = TUSB_CLASS_CDC_DATA, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = 0x00 + }, + + .ep_out = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_CDC_OUT, + .bmAttributes = { .xfer = TUSB_XFER_BULK }, + .wMaxPacketSize = { .size = CFG_TUD_CDC_EPSIZE }, + .bInterval = 0 + }, + + .ep_in = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_CDC_IN, + .bmAttributes = { .xfer = TUSB_XFER_BULK }, + .wMaxPacketSize = { .size = CFG_TUD_CDC_EPSIZE }, + .bInterval = 0 + }, + }, + + //------------- Mass Storage-------------// + .msc = + { + .itf = + { + .bLength = sizeof(tusb_desc_interface_t), + .bDescriptorType = TUSB_DESC_INTERFACE, + .bInterfaceNumber = ITF_NUM_MSC, + .bAlternateSetting = 0x00, + .bNumEndpoints = 2, + .bInterfaceClass = TUSB_CLASS_MSC, + .bInterfaceSubClass = MSC_SUBCLASS_SCSI, + .bInterfaceProtocol = MSC_PROTOCOL_BOT, + .iInterface = ITF_STR_MSC + }, + + .ep_out = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_MSC_OUT, + .bmAttributes = { .xfer = TUSB_XFER_BULK }, + .wMaxPacketSize = { .size = CFG_TUD_MSC_EPSIZE}, + .bInterval = 1 + }, + + .ep_in = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_MSC_IN, + .bmAttributes = { .xfer = TUSB_XFER_BULK }, + .wMaxPacketSize = { .size = CFG_TUD_MSC_EPSIZE}, + .bInterval = 1 + } + }, + + //------------- HID Generic Multiple report -------------// + .hid_generic = + { + .itf = + { + .bLength = sizeof(tusb_desc_interface_t), + .bDescriptorType = TUSB_DESC_INTERFACE, + .bInterfaceNumber = ITF_NUM_HID_GEN, + .bAlternateSetting = 0x00, + .bNumEndpoints = 1, + .bInterfaceClass = TUSB_CLASS_HID, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = ITF_STR_HID + }, + + .hid_desc = + { + .bLength = sizeof(tusb_hid_descriptor_hid_t), + .bDescriptorType = HID_DESC_TYPE_HID, + .bcdHID = 0x0111, + .bCountryCode = HID_Local_NotSupported, + .bNumDescriptors = 1, + .bReportType = HID_DESC_TYPE_REPORT, + .wReportLength = sizeof(usb_desc_hid_generic_report) + }, + + .ep_in = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_HID_GEN, + .bmAttributes = { .xfer = TUSB_XFER_INTERRUPT }, + .wMaxPacketSize = { .size = EP_HID_GEN_SIZE }, + .bInterval = 0x0A + } + } +}; + + +// tud_desc_set is required by tinyusb stack +tud_desc_set_t tud_desc_set = +{ + .device = (uint8_t const*) &usb_desc_dev, + .config = (uint8_t const*) &usb_desc_cfg, + .string_arr = (uint8_t const **) string_desc_arr, + .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), + + .hid_report = + { + .generic = usb_desc_hid_generic_report, + .boot_keyboard = NULL, + .boot_mouse = NULL + } +}; diff --git a/ports/nrf/usb/usb_desc.h b/ports/nrf/usb/usb_desc.h new file mode 100644 index 0000000000..19f3f6d5bf --- /dev/null +++ b/ports/nrf/usb/usb_desc.h @@ -0,0 +1,87 @@ +/* + * 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 USB_DESC_H_ +#define USB_DESC_H_ + +#include "tusb.h" + +#ifdef __cplusplus + extern "C" { +#endif + +/*------------- Configuration Descriptor -------------*/ +typedef struct ATTR_PACKED +{ + tusb_desc_configuration_t config; + + //------------- CDC -------------// + struct ATTR_PACKED + { + tusb_desc_interface_assoc_t iad; + + //CDC Control Interface + tusb_desc_interface_t comm_itf; + cdc_desc_func_header_t header; + cdc_desc_func_call_management_t call; + cdc_desc_func_acm_t acm; + cdc_desc_func_union_t union_func; + tusb_desc_endpoint_t ep_notif; + + //CDC Data Interface + tusb_desc_interface_t data_itf; + tusb_desc_endpoint_t ep_out; + tusb_desc_endpoint_t ep_in; + }cdc; + + //------------- Mass Storage -------------// + struct ATTR_PACKED + { + tusb_desc_interface_t itf; + tusb_desc_endpoint_t ep_out; + tusb_desc_endpoint_t ep_in; + } msc; + + //------------- HID -------------// + struct ATTR_PACKED + { + tusb_desc_interface_t itf; + tusb_hid_descriptor_hid_t hid_desc; + tusb_desc_endpoint_t ep_in; + } hid_generic; + +} usb_desc_cfg_t; + + +// Descriptors set used by tinyusb stack +extern tud_desc_set_t tud_desc_set; + + +#ifdef __cplusplus + } +#endif + +#endif /* USB_DESC_H_ */ From fd661c1d57ebf4090e8fd37db9a6a01358ce6a46 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 31 Jul 2018 21:06:24 +0700 Subject: [PATCH 09/19] improve usb hid --- ports/nrf/common-hal/usb_hid/Device.h | 30 ++++++++++++++++--- ports/nrf/common-hal/usb_hid/__init__.c | 40 ++++++++++++++----------- ports/nrf/usb/usb_desc.c | 9 ++++-- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/ports/nrf/common-hal/usb_hid/Device.h b/ports/nrf/common-hal/usb_hid/Device.h index 291bc963c3..a0d26fd313 100644 --- a/ports/nrf/common-hal/usb_hid/Device.h +++ b/ports/nrf/common-hal/usb_hid/Device.h @@ -36,11 +36,33 @@ extern "C" { #endif -#define USB_HID_NUM_DEVICES 2 +// 1 to enable device, 0 to disable +#define USB_HID_DEVICE_KEYBOARD 1 +#define USB_HID_DEVICE_MOUSE 1 +#define USB_HID_DEVICE_CONSUMER 0 +#define USB_HID_DEVICE_GAMEPAD 0 -#define USB_HID_REPORT_ID_KEYBOARD 1 -#define USB_HID_REPORT_ID_MOUSE 2 -//#define USB_HID_REPORT_ID_CONSUMER 3 +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_GAMEPAD + USB_HID_REPORT_ID_GAMEPAD, +#endif +}; + +#define USB_HID_NUM_DEVICES (USB_HID_DEVICE_KEYBOARD + USB_HID_DEVICE_MOUSE + USB_HID_DEVICE_CONSUMER + USB_HID_DEVICE_GAMEPAD ) typedef struct { mp_obj_base_t base; diff --git a/ports/nrf/common-hal/usb_hid/__init__.c b/ports/nrf/common-hal/usb_hid/__init__.c index 9ebf041ab5..0c637e63cd 100644 --- a/ports/nrf/common-hal/usb_hid/__init__.c +++ b/ports/nrf/common-hal/usb_hid/__init__.c @@ -31,53 +31,58 @@ #include "common-hal/usb_hid/Device.h" #include "shared-bindings/usb_hid/Device.h" -#define USB_HID_REPORT_LENGTH_KEYBOARD 8 -#define USB_HID_REPORT_LENGTH_MOUSE 4 +#define USB_HID_REPORT_LENGTH_KEYBOARD 8 +#define USB_HID_REPORT_LENGTH_MOUSE 4 +#define USB_HID_REPORT_LENGTH_CONSUMER 2 -// Buffers are report size + 1 to include the Report ID prefix byte if needed. -#ifdef USB_HID_REPORT_ID_KEYBOARD -static uint8_t keyboard_report_buffer[USB_HID_REPORT_LENGTH_KEYBOARD + 1]; +#if USB_HID_DEVICE_KEYBOARD +static uint8_t keyboard_report_buffer[USB_HID_REPORT_LENGTH_KEYBOARD]; #endif -#ifdef USB_HID_REPORT_ID_MOUSE -static uint8_t mouse_report_buffer[USB_HID_REPORT_LENGTH_MOUSE + 1]; + +#if USB_HID_DEVICE_MOUSE +static uint8_t mouse_report_buffer[USB_HID_REPORT_LENGTH_MOUSE]; #endif -#ifdef USB_HID_REPORT_ID_CONSUMER -static uint8_t consumer_report_buffer[USB_HID_REPORT_LENGTH_CONSUMER + 1]; + +#if USB_HID_DEVICE_CONSUMER +static uint8_t consumer_report_buffer[USB_HID_REPORT_LENGTH_CONSUMER]; #endif + #ifdef USB_HID_REPORT_ID_SYS_CONTROL -static uint8_t sys_control_report_buffer[USB_HID_REPORT_LENGTH_SYS_CONTROL + 1]; +static uint8_t sys_control_report_buffer[USB_HID_REPORT_LENGTH_SYS_CONTROL]; #endif + #ifdef USB_HID_REPORT_ID_GAMEPAD -static uint8_t gamepad_report_buffer[USB_HID_REPORT_LENGTH_GAMEPAD + 1]; +static uint8_t gamepad_report_buffer[USB_HID_REPORT_LENGTH_GAMEPAD]; #endif + #ifdef USB_HID_REPORT_ID_DIGITIZER -static uint8_t digitizer_report_buffer[USB_HID_REPORT_LENGTH_DIGITIZER + 1]; +static uint8_t digitizer_report_buffer[USB_HID_REPORT_LENGTH_DIGITIZER]; #endif usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = { -#ifdef USB_HID_REPORT_ID_KEYBOARD +#if USB_HID_DEVICE_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 + +#if USB_HID_DEVICE_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 + +#if USB_HID_DEVICE_CONSUMER { .base = { .type = &usb_hid_device_type }, .report_buffer = consumer_report_buffer, @@ -88,6 +93,7 @@ usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = { .usage = 0x01, }, #endif + #ifdef USB_HID_REPORT_ID_SYS_CONTROL { .base = { .type = &usb_hid_device_type }, diff --git a/ports/nrf/usb/usb_desc.c b/ports/nrf/usb/usb_desc.c index 1e3e8b2c28..07d8c837db 100644 --- a/ports/nrf/usb/usb_desc.c +++ b/ports/nrf/usb/usb_desc.c @@ -141,14 +141,17 @@ tusb_desc_device_t const usb_desc_dev = uint8_t const usb_desc_hid_generic_report[] = { -#if USB_HID_REPORT_ID_KEYBOARD +#if USB_HID_DEVICE_KEYBOARD HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(USB_HID_REPORT_ID_KEYBOARD), ), #endif -#if USB_HID_REPORT_ID_MOUSE - HID_REPORT_DESC_MOUSE( HID_REPORT_ID(USB_HID_REPORT_ID_MOUSE), ) +#if USB_HID_DEVICE_MOUSE + HID_REPORT_DESC_MOUSE( HID_REPORT_ID(USB_HID_REPORT_ID_MOUSE), ), #endif +#if USB_HID_DEVICE_CONSUMER + HID_REPORT_DESC_MOUSE( HID_REPORT_ID(USB_HID_REPORT_ID_CONSUMER), ) +#endif }; From ef58adacd3a37c0df37bf0f1f2b9568e70c163f8 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 31 Jul 2018 21:19:02 +0700 Subject: [PATCH 10/19] usb hid consumer control works --- ports/nrf/common-hal/usb_hid/Device.h | 2 +- ports/nrf/common-hal/usb_hid/__init__.c | 16 +++++++--------- ports/nrf/usb/usb_desc.c | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/ports/nrf/common-hal/usb_hid/Device.h b/ports/nrf/common-hal/usb_hid/Device.h index a0d26fd313..b6a832f5c7 100644 --- a/ports/nrf/common-hal/usb_hid/Device.h +++ b/ports/nrf/common-hal/usb_hid/Device.h @@ -39,7 +39,7 @@ // 1 to enable device, 0 to disable #define USB_HID_DEVICE_KEYBOARD 1 #define USB_HID_DEVICE_MOUSE 1 -#define USB_HID_DEVICE_CONSUMER 0 +#define USB_HID_DEVICE_CONSUMER 1 #define USB_HID_DEVICE_GAMEPAD 0 enum { diff --git a/ports/nrf/common-hal/usb_hid/__init__.c b/ports/nrf/common-hal/usb_hid/__init__.c index 0c637e63cd..536ad07fa2 100644 --- a/ports/nrf/common-hal/usb_hid/__init__.c +++ b/ports/nrf/common-hal/usb_hid/__init__.c @@ -30,6 +30,7 @@ #include "common-hal/usb_hid/Device.h" #include "shared-bindings/usb_hid/Device.h" +#include "tusb.h" #define USB_HID_REPORT_LENGTH_KEYBOARD 8 #define USB_HID_REPORT_LENGTH_MOUSE 4 @@ -66,8 +67,8 @@ usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = { .report_buffer = keyboard_report_buffer, .report_id = USB_HID_REPORT_ID_KEYBOARD, .report_length = USB_HID_REPORT_LENGTH_KEYBOARD, - .usage_page = 0x01, - .usage = 0x06, + .usage_page = HID_USAGE_PAGE_DESKTOP, + .usage = HID_USAGE_DESKTOP_KEYBOARD, }, #endif @@ -77,8 +78,8 @@ usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = { .report_buffer = mouse_report_buffer, .report_id = USB_HID_REPORT_ID_MOUSE, .report_length = USB_HID_REPORT_LENGTH_MOUSE, - .usage_page = 0x01, - .usage = 0x02, + .usage_page = HID_USAGE_PAGE_DESKTOP, + .usage = HID_USAGE_DESKTOP_MOUSE, }, #endif @@ -86,11 +87,10 @@ usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = { { .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, + .usage_page = HID_USAGE_PAGE_CONSUMER, + .usage = HID_USAGE_CONSUMER_CONTROL, }, #endif @@ -98,7 +98,6 @@ usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = { { .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, @@ -109,7 +108,6 @@ usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = { { .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, diff --git a/ports/nrf/usb/usb_desc.c b/ports/nrf/usb/usb_desc.c index 07d8c837db..54d3133675 100644 --- a/ports/nrf/usb/usb_desc.c +++ b/ports/nrf/usb/usb_desc.c @@ -150,7 +150,7 @@ uint8_t const usb_desc_hid_generic_report[] = #endif #if USB_HID_DEVICE_CONSUMER - HID_REPORT_DESC_MOUSE( HID_REPORT_ID(USB_HID_REPORT_ID_CONSUMER), ) + HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(USB_HID_REPORT_ID_CONSUMER), ) #endif }; From 2c85f423307fcb0c685b949567a74e6d64c4b131 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 31 Jul 2018 21:57:00 +0700 Subject: [PATCH 11/19] add usb hid syscontrol --- ports/nrf/common-hal/usb_hid/Device.h | 21 ++++++++++++---- ports/nrf/common-hal/usb_hid/__init__.c | 32 +++++++++++++------------ ports/nrf/usb/usb_desc.c | 12 +++++----- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/ports/nrf/common-hal/usb_hid/Device.h b/ports/nrf/common-hal/usb_hid/Device.h index b6a832f5c7..98328fab96 100644 --- a/ports/nrf/common-hal/usb_hid/Device.h +++ b/ports/nrf/common-hal/usb_hid/Device.h @@ -37,10 +37,12 @@ #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_GAMEPAD 0 +#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 0 +#define USB_HID_DEVICE_DIGITIZER 0 enum { USB_HID_REPORT_ID_UNUSED = 0, @@ -57,12 +59,21 @@ enum { 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_GAMEPAD ) +#define USB_HID_NUM_DEVICES (USB_HID_DEVICE_KEYBOARD + USB_HID_DEVICE_MOUSE + USB_HID_DEVICE_CONSUMER + \ + USB_HID_REPORT_ID_SYS_CONTROL + USB_HID_DEVICE_GAMEPAD + USB_HID_DEVICE_DIGITIZER ) typedef struct { mp_obj_base_t base; diff --git a/ports/nrf/common-hal/usb_hid/__init__.c b/ports/nrf/common-hal/usb_hid/__init__.c index 536ad07fa2..304c6a0545 100644 --- a/ports/nrf/common-hal/usb_hid/__init__.c +++ b/ports/nrf/common-hal/usb_hid/__init__.c @@ -32,9 +32,10 @@ #include "shared-bindings/usb_hid/Device.h" #include "tusb.h" -#define USB_HID_REPORT_LENGTH_KEYBOARD 8 -#define USB_HID_REPORT_LENGTH_MOUSE 4 -#define USB_HID_REPORT_LENGTH_CONSUMER 2 +#define USB_HID_REPORT_LENGTH_KEYBOARD 8 +#define USB_HID_REPORT_LENGTH_MOUSE 4 +#define USB_HID_REPORT_LENGTH_CONSUMER 2 +#define USB_HID_REPORT_LENGTH_SYS_CONTROL 1 #if USB_HID_DEVICE_KEYBOARD static uint8_t keyboard_report_buffer[USB_HID_REPORT_LENGTH_KEYBOARD]; @@ -48,19 +49,19 @@ static uint8_t mouse_report_buffer[USB_HID_REPORT_LENGTH_MOUSE]; static uint8_t consumer_report_buffer[USB_HID_REPORT_LENGTH_CONSUMER]; #endif -#ifdef USB_HID_REPORT_ID_SYS_CONTROL +#if USB_HID_DEVICE_SYS_CONTROL static uint8_t sys_control_report_buffer[USB_HID_REPORT_LENGTH_SYS_CONTROL]; #endif -#ifdef USB_HID_REPORT_ID_GAMEPAD +#if USB_HID_DEVICE_GAMEPAD static uint8_t gamepad_report_buffer[USB_HID_REPORT_LENGTH_GAMEPAD]; #endif -#ifdef USB_HID_REPORT_ID_DIGITIZER +#if USB_HID_DEVICE_DIGITIZER static uint8_t digitizer_report_buffer[USB_HID_REPORT_LENGTH_DIGITIZER]; #endif -usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = { +usb_hid_device_obj_t usb_hid_devices[] = { #if USB_HID_DEVICE_KEYBOARD { .base = { .type = &usb_hid_device_type }, @@ -94,31 +95,32 @@ usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = { }, #endif -#ifdef USB_HID_REPORT_ID_SYS_CONTROL +#if USB_HID_DEVICE_SYS_CONTROL { .base = { .type = &usb_hid_device_type }, .report_buffer = sys_control_report_buffer, .report_id = USB_HID_REPORT_ID_SYS_CONTROL, .report_length = USB_HID_REPORT_LENGTH_SYS_CONTROL, - .usage_page = 0x01, - .usage = 0x80, + .usage_page = HID_USAGE_PAGE_DESKTOP, + .usage = HID_USAGE_DESKTOP_SYSTEM_CONTROL, }, #endif -#ifdef USB_HID_REPORT_ID_GAMEPAD + +#if USB_HID_DEVICE_GAMEPAD { .base = { .type = &usb_hid_device_type }, .report_buffer = gamepad_report_buffer, .report_id = USB_HID_REPORT_ID_GAMEPAD, .report_length = USB_HID_REPORT_LENGTH_GAMEPAD, - .usage_page = 0x01, - .usage = 0x05, + .usage_page = HID_USAGE_PAGE_DESKTOP, + .usage = HID_USAGE_DESKTOP_GAMEPAD, }, #endif -#ifdef USB_HID_REPORT_ID_DIGITIZER + +#if USB_HID_DEVICE_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, diff --git a/ports/nrf/usb/usb_desc.c b/ports/nrf/usb/usb_desc.c index 54d3133675..b332ebc1b0 100644 --- a/ports/nrf/usb/usb_desc.c +++ b/ports/nrf/usb/usb_desc.c @@ -136,9 +136,6 @@ tusb_desc_device_t const usb_desc_dev = //--------------------------------------------------------------------+ // HID Report Descriptor //--------------------------------------------------------------------+ -// TODO remove const for dynamic descriptors - - uint8_t const usb_desc_hid_generic_report[] = { #if USB_HID_DEVICE_KEYBOARD @@ -150,15 +147,18 @@ uint8_t const usb_desc_hid_generic_report[] = #endif #if USB_HID_DEVICE_CONSUMER - HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(USB_HID_REPORT_ID_CONSUMER), ) + HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(USB_HID_REPORT_ID_CONSUMER), ), #endif -}; +#if USB_HID_DEVICE_SYS_CONTROL + HID_REPORT_DESC_SYSTEM_CONTROL( HID_REPORT_ID(USB_HID_REPORT_ID_SYS_CONTROL ), ), +#endif + +}; //--------------------------------------------------------------------+ // Configuration Descriptor //--------------------------------------------------------------------+ -// TODO remove const for dynamic descriptors usb_desc_cfg_t const usb_desc_cfg = { .config = From 05139e2aee21b6e49ff91eb0652cf6068a35bf84 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 31 Jul 2018 22:54:26 +0700 Subject: [PATCH 12/19] all hid devices seems to be ok --- ports/nrf/common-hal/usb_hid/Device.h | 8 ++++---- ports/nrf/common-hal/usb_hid/__init__.c | 2 ++ ports/nrf/usb/usb_desc.c | 4 ++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ports/nrf/common-hal/usb_hid/Device.h b/ports/nrf/common-hal/usb_hid/Device.h index 98328fab96..75dd37f924 100644 --- a/ports/nrf/common-hal/usb_hid/Device.h +++ b/ports/nrf/common-hal/usb_hid/Device.h @@ -41,8 +41,8 @@ #define USB_HID_DEVICE_MOUSE 1 #define USB_HID_DEVICE_CONSUMER 1 #define USB_HID_DEVICE_SYS_CONTROL 1 -#define USB_HID_DEVICE_GAMEPAD 0 -#define USB_HID_DEVICE_DIGITIZER 0 +#define USB_HID_DEVICE_GAMEPAD 1 +#define USB_HID_DEVICE_DIGITIZER 0 // not supported yet enum { USB_HID_REPORT_ID_UNUSED = 0, @@ -73,7 +73,7 @@ enum { }; #define USB_HID_NUM_DEVICES (USB_HID_DEVICE_KEYBOARD + USB_HID_DEVICE_MOUSE + USB_HID_DEVICE_CONSUMER + \ - USB_HID_REPORT_ID_SYS_CONTROL + USB_HID_DEVICE_GAMEPAD + USB_HID_DEVICE_DIGITIZER ) + USB_HID_DEVICE_SYS_CONTROL + USB_HID_DEVICE_GAMEPAD + USB_HID_DEVICE_DIGITIZER ) typedef struct { mp_obj_base_t base; @@ -85,7 +85,7 @@ typedef struct { } usb_hid_device_obj_t; -usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES]; +extern usb_hid_device_obj_t usb_hid_devices[]; #ifdef __cplusplus } diff --git a/ports/nrf/common-hal/usb_hid/__init__.c b/ports/nrf/common-hal/usb_hid/__init__.c index 304c6a0545..0085d688ed 100644 --- a/ports/nrf/common-hal/usb_hid/__init__.c +++ b/ports/nrf/common-hal/usb_hid/__init__.c @@ -36,6 +36,8 @@ #define USB_HID_REPORT_LENGTH_MOUSE 4 #define USB_HID_REPORT_LENGTH_CONSUMER 2 #define USB_HID_REPORT_LENGTH_SYS_CONTROL 1 +#define USB_HID_REPORT_LENGTH_GAMEPAD 6 +#define USB_HID_REPORT_LENGTH_DIGITIZER 5 #if USB_HID_DEVICE_KEYBOARD static uint8_t keyboard_report_buffer[USB_HID_REPORT_LENGTH_KEYBOARD]; diff --git a/ports/nrf/usb/usb_desc.c b/ports/nrf/usb/usb_desc.c index b332ebc1b0..20557cc020 100644 --- a/ports/nrf/usb/usb_desc.c +++ b/ports/nrf/usb/usb_desc.c @@ -154,6 +154,10 @@ uint8_t const usb_desc_hid_generic_report[] = HID_REPORT_DESC_SYSTEM_CONTROL( HID_REPORT_ID(USB_HID_REPORT_ID_SYS_CONTROL ), ), #endif +#if USB_HID_DEVICE_GAMEPAD + HID_REPORT_DESC_GAMEPAD( HID_REPORT_ID(USB_HID_REPORT_ID_GAMEPAD ), ) +#endif + }; //--------------------------------------------------------------------+ From 4bece22c9224799c8aa495f73f1019e3266de031 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 31 Jul 2018 23:02:15 +0700 Subject: [PATCH 13/19] add hid keyboard LED indicator stub --- ports/nrf/common-hal/usb_hid/Device.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/ports/nrf/common-hal/usb_hid/Device.c b/ports/nrf/common-hal/usb_hid/Device.c index 27a50858f9..19ccb3fe22 100644 --- a/ports/nrf/common-hal/usb_hid/Device.c +++ b/ports/nrf/common-hal/usb_hid/Device.c @@ -70,21 +70,29 @@ void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t* } // 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) -{ +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[report_id].report_buffer, reqlen); + 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) -{ +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 ) { - // TODO Support Keyboard LED e.g Capslock + // 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 + } } } From 85bd46a4e14b0e73d7bcbc61dd847b061072641d Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 31 Jul 2018 23:09:39 +0700 Subject: [PATCH 14/19] enable MICROPY_PY_ARRAY_SLICE_ASSIGN, MICROPY_PY_BUILTINS_SLICE_ATTRS --- ports/nrf/mpconfigport.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index d2672edd4e..2016c5c156 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -99,9 +99,9 @@ #define MICROPY_MODULE_BUILTIN_INIT (1) #define MICROPY_PY_ALL_SPECIAL_METHODS (0) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) -#define MICROPY_PY_ARRAY_SLICE_ASSIGN (0) +#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) #define MICROPY_NONSTANDARD_TYPECODES (0) -#define MICROPY_PY_BUILTINS_SLICE_ATTRS (0) +#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1) #define MICROPY_PY_SYS_EXIT (1) #define MICROPY_PY_SYS_MAXSIZE (1) #define MICROPY_PY_SYS_STDFILES (0) From 20c25f61d92cfde9f259ee06ee47915e7a3ea3c5 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 Aug 2018 00:53:56 +0700 Subject: [PATCH 15/19] update tinyusb --- lib/tinyusb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tinyusb b/lib/tinyusb index a2b7b9ddb9..6d96b12e27 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit a2b7b9ddb9f3c01bd0572eba73c33548ee2ce1df +Subproject commit 6d96b12e27ae60ca500365ee2137105145ada9dd From 6ddd8583da6993c61f56ec6cbb908fac16924acd Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 Aug 2018 08:20:20 +0700 Subject: [PATCH 16/19] clean up --- ports/nrf/common-hal/usb_hid/__init__.c | 72 ++++++++++++------------- ports/nrf/usb/usb_desc.c | 34 +++++++----- 2 files changed, 56 insertions(+), 50 deletions(-) diff --git a/ports/nrf/common-hal/usb_hid/__init__.c b/ports/nrf/common-hal/usb_hid/__init__.c index 0085d688ed..00fc8bd002 100644 --- a/ports/nrf/common-hal/usb_hid/__init__.c +++ b/ports/nrf/common-hal/usb_hid/__init__.c @@ -66,67 +66,67 @@ static uint8_t digitizer_report_buffer[USB_HID_REPORT_LENGTH_DIGITIZER]; usb_hid_device_obj_t usb_hid_devices[] = { #if USB_HID_DEVICE_KEYBOARD { - .base = { .type = &usb_hid_device_type }, - .report_buffer = keyboard_report_buffer, - .report_id = USB_HID_REPORT_ID_KEYBOARD, - .report_length = USB_HID_REPORT_LENGTH_KEYBOARD, - .usage_page = HID_USAGE_PAGE_DESKTOP, - .usage = HID_USAGE_DESKTOP_KEYBOARD, + .base = { .type = &usb_hid_device_type } , + .report_buffer = keyboard_report_buffer , + .report_id = USB_HID_REPORT_ID_KEYBOARD , + .report_length = USB_HID_REPORT_LENGTH_KEYBOARD , + .usage_page = HID_USAGE_PAGE_DESKTOP , + .usage = HID_USAGE_DESKTOP_KEYBOARD , }, #endif #if USB_HID_DEVICE_MOUSE { - .base = { .type = &usb_hid_device_type }, - .report_buffer = mouse_report_buffer, - .report_id = USB_HID_REPORT_ID_MOUSE, - .report_length = USB_HID_REPORT_LENGTH_MOUSE, - .usage_page = HID_USAGE_PAGE_DESKTOP, - .usage = HID_USAGE_DESKTOP_MOUSE, + .base = { .type = &usb_hid_device_type } , + .report_buffer = mouse_report_buffer , + .report_id = USB_HID_REPORT_ID_MOUSE , + .report_length = USB_HID_REPORT_LENGTH_MOUSE , + .usage_page = HID_USAGE_PAGE_DESKTOP , + .usage = HID_USAGE_DESKTOP_MOUSE , }, #endif #if USB_HID_DEVICE_CONSUMER { - .base = { .type = &usb_hid_device_type }, - .report_buffer = consumer_report_buffer, - .report_id = USB_HID_REPORT_ID_CONSUMER, - .report_length = USB_HID_REPORT_LENGTH_CONSUMER, - .usage_page = HID_USAGE_PAGE_CONSUMER, - .usage = HID_USAGE_CONSUMER_CONTROL, + .base = { .type = &usb_hid_device_type } , + .report_buffer = consumer_report_buffer , + .report_id = USB_HID_REPORT_ID_CONSUMER , + .report_length = USB_HID_REPORT_LENGTH_CONSUMER , + .usage_page = HID_USAGE_PAGE_CONSUMER , + .usage = HID_USAGE_CONSUMER_CONTROL , }, #endif #if USB_HID_DEVICE_SYS_CONTROL { - .base = { .type = &usb_hid_device_type }, - .report_buffer = sys_control_report_buffer, - .report_id = USB_HID_REPORT_ID_SYS_CONTROL, - .report_length = USB_HID_REPORT_LENGTH_SYS_CONTROL, - .usage_page = HID_USAGE_PAGE_DESKTOP, - .usage = HID_USAGE_DESKTOP_SYSTEM_CONTROL, + .base = { .type = &usb_hid_device_type } , + .report_buffer = sys_control_report_buffer , + .report_id = USB_HID_REPORT_ID_SYS_CONTROL , + .report_length = USB_HID_REPORT_LENGTH_SYS_CONTROL , + .usage_page = HID_USAGE_PAGE_DESKTOP , + .usage = HID_USAGE_DESKTOP_SYSTEM_CONTROL , }, #endif #if USB_HID_DEVICE_GAMEPAD { - .base = { .type = &usb_hid_device_type }, - .report_buffer = gamepad_report_buffer, - .report_id = USB_HID_REPORT_ID_GAMEPAD, - .report_length = USB_HID_REPORT_LENGTH_GAMEPAD, - .usage_page = HID_USAGE_PAGE_DESKTOP, - .usage = HID_USAGE_DESKTOP_GAMEPAD, + .base = { .type = &usb_hid_device_type } , + .report_buffer = gamepad_report_buffer , + .report_id = USB_HID_REPORT_ID_GAMEPAD , + .report_length = USB_HID_REPORT_LENGTH_GAMEPAD , + .usage_page = HID_USAGE_PAGE_DESKTOP , + .usage = HID_USAGE_DESKTOP_GAMEPAD , }, #endif #if USB_HID_DEVICE_DIGITIZER { - .base = { .type = &usb_hid_device_type }, - .report_buffer = digitizer_report_buffer, - .report_id = USB_HID_REPORT_ID_DIGITIZER, - .report_length = USB_HID_REPORT_LENGTH_DIGITIZER, - .usage_page = 0x0D, - .usage = 0x02, + .base = { .type = &usb_hid_device_type } , + .report_buffer = digitizer_report_buffer , + .report_id = USB_HID_REPORT_ID_DIGITIZER , + .report_length = USB_HID_REPORT_LENGTH_DIGITIZER , + .usage_page = 0x0D , + .usage = 0x02 , }, #endif }; diff --git a/ports/nrf/usb/usb_desc.c b/ports/nrf/usb/usb_desc.c index 20557cc020..3f64956d28 100644 --- a/ports/nrf/usb/usb_desc.c +++ b/ports/nrf/usb/usb_desc.c @@ -37,15 +37,23 @@ #define USB_PID 0x802A /*------------- Interface Numbering -------------*/ -#define ITF_NUM_CDC 0 -#define ITF_NUM_MSC 2 -#define ITF_NUM_HID_GEN 3 -#define ITF_TOTAL 4 +enum { + ITF_NUM_CDC = 0 , + ITF_NUM_CDC_DATA , + ITF_NUM_MSC , + ITF_NUM_HID_GEN , + ITF_NUM_TOTAL +}; - -#define ITF_STR_CDC 4 -#define ITF_STR_MSC 5 -#define ITF_STR_HID 6 +enum { + ITF_STR_LANGUAGE = 0 , + ITF_STR_MANUFACTURER , + ITF_STR_PRODUCT , + ITF_STR_SERIAL , + ITF_STR_CDC , + ITF_STR_MSC , + ITF_STR_HID +}; /*------------- Endpoint Numbering & Size -------------*/ #define _EP_IN(x) (0x80 | (x)) @@ -169,10 +177,8 @@ usb_desc_cfg_t const usb_desc_cfg = { .bLength = sizeof(tusb_desc_configuration_t), .bDescriptorType = TUSB_DESC_CONFIGURATION, - .wTotalLength = sizeof(usb_desc_cfg_t), - .bNumInterfaces = ITF_TOTAL, - + .bNumInterfaces = ITF_NUM_TOTAL, .bConfigurationValue = 1, .iConfiguration = 0x00, .bmAttributes = TUSB_DESC_CONFIG_ATT_BUS_POWER, @@ -371,9 +377,9 @@ usb_desc_cfg_t const usb_desc_cfg = // tud_desc_set is required by tinyusb stack tud_desc_set_t tud_desc_set = { - .device = (uint8_t const*) &usb_desc_dev, - .config = (uint8_t const*) &usb_desc_cfg, - .string_arr = (uint8_t const **) string_desc_arr, + .device = &usb_desc_dev, + .config = &usb_desc_cfg, + .string_arr = (uint8_t const **) string_desc_arr, .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), .hid_report = From bac233e99b6924fadbc40e95e440432a956308c2 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 Aug 2018 08:47:41 +0700 Subject: [PATCH 17/19] clean up --- ports/nrf/common-hal/usb_hid/Device.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/ports/nrf/common-hal/usb_hid/Device.c b/ports/nrf/common-hal/usb_hid/Device.c index 19ccb3fe22..c2fff70016 100644 --- a/ports/nrf/common-hal/usb_hid/Device.c +++ b/ports/nrf/common-hal/usb_hid/Device.c @@ -25,22 +25,12 @@ */ #include - #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; } From 200669216ef0a4666a380e674f1d65318815bec7 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 Aug 2018 08:52:07 +0700 Subject: [PATCH 18/19] more clean up --- ports/nrf/common-hal/usb_hid/Device.h | 4 +- ports/nrf/interrupt_char.c | 58 --------------------------- 2 files changed, 2 insertions(+), 60 deletions(-) delete mode 100644 ports/nrf/interrupt_char.c diff --git a/ports/nrf/common-hal/usb_hid/Device.h b/ports/nrf/common-hal/usb_hid/Device.h index 75dd37f924..612423715c 100644 --- a/ports/nrf/common-hal/usb_hid/Device.h +++ b/ports/nrf/common-hal/usb_hid/Device.h @@ -78,8 +78,8 @@ enum { 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 report_id; + uint8_t report_length; uint8_t usage_page; uint8_t usage; } usb_hid_device_obj_t; diff --git a/ports/nrf/interrupt_char.c b/ports/nrf/interrupt_char.c deleted file mode 100644 index 8793b0d889..0000000000 --- a/ports/nrf/interrupt_char.c +++ /dev/null @@ -1,58 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013-2016 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. - */ - -#include "py/obj.h" -#include "py/mpstate.h" - -#ifdef NRF52840_XXAA -#include "usb.h" -#endif - -#if MICROPY_KBD_EXCEPTION - -int mp_interrupt_char; - -void mp_hal_set_interrupt_char(int c) { - if (c != -1) { - mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))); - } - mp_interrupt_char = c; - -#ifdef NRF52840_XXAA - tud_cdc_set_wanted_char(c); -#endif -} - -void mp_keyboard_interrupt(void) { - MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)); - #if MICROPY_ENABLE_SCHEDULER - if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) { - MP_STATE_VM(sched_state) = MP_SCHED_PENDING; - } - #endif -} - -#endif From 592c190069d1b3b3cd3722bbe278f1d4998764f4 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 1 Aug 2018 11:31:32 +0700 Subject: [PATCH 19/19] PR review clean up --- ports/nrf/usb/usb_desc.c | 10 +++++----- ports/nrf/usb/usb_msc_flash.c | 4 +++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/ports/nrf/usb/usb_desc.c b/ports/nrf/usb/usb_desc.c index 3f64956d28..31f80b80d2 100644 --- a/ports/nrf/usb/usb_desc.c +++ b/ports/nrf/usb/usb_desc.c @@ -88,22 +88,22 @@ uint16_t const * const string_desc_arr [] = TUD_DESC_STRCONV('A','d','a','f','r','u','i','t',' ','I','n','d','u','s','t','r','i','e','s'), // 2 Product - TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','n','R','F','5','2'), + TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','y',' ','n','R','F','5','2'), // 3 Serials TODO use chip ID TUD_DESC_STRCONV('1', '2', '3', '4', '5'), // 4 CDC Interface - TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','S','e','r','i','a','l'), + TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','y',' ','S','e','r','i','a','l'), // 5 MSC Interface - TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','S','t','o','r','a','g','e'), + TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','y',' ','S','t','o','r','a','g','e'), // 6 HID Interface - TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','H','I','D'), + TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','y',' ','H','I','D'), // Custom Interface -// TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','C','u','s','t','o','m') +// TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','y',' ','C','u','s','t','o','m') }; diff --git a/ports/nrf/usb/usb_msc_flash.c b/ports/nrf/usb/usb_msc_flash.c index 384ef55780..2d0e9bec7f 100644 --- a/ports/nrf/usb/usb_msc_flash.c +++ b/ports/nrf/usb/usb_msc_flash.c @@ -92,7 +92,9 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, } // return len must not larger than bufsize - if ( resplen > bufsize ) resplen = bufsize; + if ( resplen > bufsize ) { + resplen = bufsize; + } // copy response to stack's buffer if any if ( response && resplen ) {