8000 Merge branch 'master' into nrf52_sys_module · sparkfun/circuitpython@0bc3432 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0bc3432

Browse files
committed
Merge branch 'master' into nrf52_sys_module
2 parents 8640d37 + 65bd07b commit 0bc3432

File tree

12 files changed

+931
-210
lines changed

12 files changed

+931
-210
lines changed

lib/tinyusb

Submodule tinyusb updated 61 files

ports/nrf/Makefile

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ SRC_C += \
102102
tick.c \
103103
background.c \
104104
internal_flash.c \
105-
interrupt_char.c \
106105
drivers/bluetooth/ble_drv.c \
107106
drivers/bluetooth/ble_uart.c \
108107
boards/$(BOARD)/board.c \
@@ -115,27 +114,12 @@ SRC_C += \
115114
lib/utils/buffer_helper.c \
116115
lib/utils/context_manager_helpers.c \
117116
lib/utils/pyexec.c \
117+
lib/utils/interrupt_char.c \
118118
lib/utils/stdout_helpers.c \
119119
lib/utils/sys_stdio_mphal.c \
120120
lib/libc/string0.c \
121121
lib/mp-readline/readline.c \
122122

123-
ifeq ($(MCU_SUB_VARIANT),nrf52840)
124-
125-
SRC_C += \
126-
usb/usb.c \
127-
usb/usb_msc_flash.c \
128-
lib/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c \
129-
lib/tinyusb/src/portable/nordic/nrf5x/hal_nrf5x.c \
130-
lib/tinyusb/src/common/tusb_fifo.c \
131-
lib/tinyusb/src/device/usbd.c \
132-
lib/tinyusb/src/device/usbd_desc.c \
133-
lib/tinyusb/src/class/msc/msc_device.c \
134-
lib/tinyusb/src/class/cdc/cdc_device.c \
135-
lib/tinyusb/src/tusb.c \
136-
137-
endif
138-
139123
DRIVERS_SRC_C += $(addprefix modules/,\
140124
ubluepy/modubluepy.c \
141125
ubluepy/ubluepy_peripheral.c \
@@ -219,7 +203,29 @@ SRC_SHARED_BINDINGS = \
219203
bitbangio/I2C.c \
220204
bitbangio/SPI.c \
221205
bitbangio/OneWire.c \
222-
random/__init__.c
206+
random/__init__.c \
207+
208+
# USB source files for nrf52840
209+
ifeq ($(MCU_SUB_VARIANT),nrf52840)
210+
211+
SRC_C += \
212+
usb/usb.c \
213+
usb/usb_msc_flash.c \
214+
usb/usb_desc.c \
215+
lib/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c \
216+
lib/tinyusb/src/portable/nordic/nrf5x/hal_nrf5x.c \
217+
lib/tinyusb/src/common/tusb_fifo.c \
218+
lib/tinyusb/src/device/usbd.c \
219+
lib/tinyusb/src/class/msc/msc_device.c \
220+
lib/tinyusb/src/class/cdc/cdc_device.c \
221+
lib/tinyusb/src/class/hid/hid_device.c \
222+
lib/tinyusb/src/tusb.c \
223+
224+
SRC_COMMON_HAL += \
225+
usb_hid/__init__.c \
226+
usb_hid/Device.c \
227+
228+
endif
223229

224230

225231
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_BINDINGS)) \

ports/nrf/common-hal/usb_hid/Device.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 hathach for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <string.h>
28+
#include "tick.h"
29+
#include "common-hal/usb_hid/Device.h"
30+
#include "py/runtime.h"
31+
#include "shared-bindings/usb_hid/Device.h"
32+
#include "tusb.h"
33+
34+
uint8_t common_hal_usb_hid_device_get_usage_page(usb_hid_device_obj_t *self) {
35+
return self->usage_page;
36+
}
37+
38+
uint8_t common_hal_usb_hid_device_get_usage(usb_hid_device_obj_t *self) {
39+
return self->usage;
40+
}
41+
42+
void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t* report, uint8_t len) {
43+
if (len != self->report_length) {
44+
mp_raise_ValueError_varg("Buffer incorrect size. Should be %d bytes.", self->report_length);
45+
}
46+
47+
// Wait until interface is ready, timeout = 2 seconds
48+
uint64_t end_ticks = ticks_ms + 2000;
49+
while ( (ticks_ms < end_ticks) && !tud_hid_generic_ready() ) { }
50+
51+
if ( !tud_hid_generic_ready() ) {
52+
mp_raise_msg(&mp_type_OSError, "USB Busy");
53+
}
54+
55+
memcpy(self->report_buffer, report, len);
56+
57+
if ( !tud_hid_generic_report(self->report_id, self->report_buffer, len) ) {
58+
mp_raise_msg(&mp_type_OSError, "USB Error");
59+
}
60+
}
61+
62+
// Callbacks invoked when receive Get_Report request through control endpoint
63+
uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) {
64+
// only support Input Report
65+
if ( report_type != HID_REPORT_TYPE_INPUT ) return 0;
66+
67+
// index is ID-1
68+
uint8_t idx = ( report_id ? (report_id-1) : 0 );
69+
70+
// fill buffer with current report
71+
memcpy(buffer, usb_hid_devices[idx].report_buffer, reqlen);
72+
return reqlen;
73+
}
74+
75+
// Callbacks invoked when receive Set_Report request through control endpoint
76+
void tud_hid_generic_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) {
77+
// index is ID-1
78+
uint8_t idx = ( report_id ? (report_id-1) : 0 );
79+
80+
if ( report_type == HID_REPORT_TYPE_OUTPUT ) {
81+
// Check if it is Keyboard device
82+
if ( (usb_hid_devices[idx].usage_page == HID_USAGE_PAGE_DESKTOP) && (usb_hid_devices[idx].usage == HID_USAGE_DESKTOP_KEYBOARD) ) {
83+
// This is LED indicator (CapsLock, NumLock)
84+
// TODO Light up some LED here
85+
}
86+
}
87+
}
88+

ports/nrf/common-hal/usb_hid/Device.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 hathach for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef COMMON_HAL_USB_HID_DEVICE_H
28+
#define COMMON_HAL_USB_HID_DEVICE_H
29+
30+
#include <stdint.h>
31+
#include <stdbool.h>
32+
33+
#include "py/obj.h"
34+
35+
#ifdef __cplusplus
36+
extern "C" {
37+
#endif
38+
39+
// 1 to enable device, 0 to disable
40+
#define USB_HID_DEVICE_KEYBOARD 1
41+
#define USB_HID_DEVICE_MOUSE 1
42+
#define USB_HID_DEVICE_CONSUMER 1
43+
#define USB_HID_DEVICE_SYS_CONTROL 1
44+
#define USB_HID_DEVICE_GAMEPAD 1
45+
#define USB_HID_DEVICE_DIGITIZER 0 // not supported yet
46+
47+
enum {
48+
USB_HID_REPORT_ID_UNUSED = 0,
49+
50+
#if USB_HID_DEVICE_KEYBOARD
51+
USB_HID_REPORT_ID_KEYBOARD,
52+
#endif
53+
54+
#if USB_HID_DEVICE_MOUSE
55+
USB_HID_REPORT_ID_MOUSE,
56+
#endif
57+
58+
#if USB_HID_DEVICE_CONSUMER
59+
USB_HID_REPORT_ID_CONSUMER,
60+
#endif
61+
62+
#if USB_HID_DEVICE_SYS_CONTROL
63+
USB_HID_REPORT_ID_SYS_CONTROL,
64+
#endif
65+
66+
#if USB_HID_DEVICE_GAMEPAD
67+
USB_HID_REPORT_ID_GAMEPAD,
68+
#endif
69+
70+
#if USB_HID_DEVICE_DIGITIZER
71+
USB_HID_REPORT_ID_DIGITIZER,
72+
#endif
73+
};
74+
75+
#define USB_HID_NUM_DEVICES (USB_HID_DEVICE_KEYBOARD + USB_HID_DEVICE_MOUSE + USB_HID_DEVICE_CONSUMER + \
76+
USB_HID_DEVICE_SYS_CONTROL + USB_HID_DEVICE_GAMEPAD + USB_HID_DEVICE_DIGITIZER )
77+
78+
typedef struct {
79+
mp_obj_base_t base;
80+
uint8_t* report_buffer;
81+
uint8_t report_id;
82+
uint8_t report_length;
83+
uint8_t usage_page;
84+
uint8_t usage;
85+
} usb_hid_device_obj_t;
86+
87+
88+
extern usb_hid_device_obj_t usb_hid_devices[];
89+
90+
#ifdef __cplusplus
91+
}
92+
#endif
93+
94+
#endif /* COMMON_HAL_USB_HID_DEVICE_H */

0 commit comments

Comments
 (0)
0