forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Nrf52840 usb hid #1074
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5bdf409
update tusb lib
hathach e86f7d0
add usb msc callback description
hathach 21339c4
house keeping
hathach 368d597
adding usb_hid
hathach ae783b0
update tinyusb, set scsi sense key for unsupported commands
hathach 221d54a
workaround to use lib/utils/interrupt_char.c
hathach d15caf0
hid keyboard and mouse work well
hathach 27b2a9f
add usb_desc, disable tinyusb stack CFG_TUD_DESC_AUTO
hathach fd661c1
improve usb hid
hathach ef58ada
usb hid consumer control works
hathach 2c85f42
add usb hid syscontrol
hathach 05139e2
all hid devices seems to be ok
hathach 4bece22
add hid keyboard LED indicator stub
hathach 85bd46a
enable MICROPY_PY_ARRAY_SLICE_ASSIGN, MICROPY_PY_BUILTINS_SLICE_ATTRS
hathach 20c25f6
update tinyusb
hathach 1e524f1
Merge branch 'master' into nrf52840_usb_hid
hathach 6ddd858
clean up
hathach bac233e
clean up
hathach 2006692
more clean up
hathach 592c190
PR review clean up
hathach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule tinyusb
updated
61 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
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 | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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