summaryrefslogtreecommitdiff
path: root/shared/tinyusb
diff options
context:
space:
mode:
Diffstat (limited to 'shared/tinyusb')
-rw-r--r--shared/tinyusb/tusb_config.h46
-rw-r--r--shared/tinyusb/usbd.c32
-rw-r--r--shared/tinyusb/usbd.h41
-rw-r--r--shared/tinyusb/usbd_descriptor.c157
4 files changed, 276 insertions, 0 deletions
diff --git a/shared/tinyusb/tusb_config.h b/shared/tinyusb/tusb_config.h
new file mode 100644
index 000000000..b50c70260
--- /dev/null
+++ b/shared/tinyusb/tusb_config.h
@@ -0,0 +1,46 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020-2021 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.
+ *
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_TINYUSB_TUSB_CONFIG_H
+#define MICROPY_INCLUDED_SHARED_TINYUSB_TUSB_CONFIG_H
+
+#include <py/mpconfig.h>
+#include "mpconfigport.h"
+
+#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE)
+
+#define CFG_TUD_CDC (1)
+#define CFG_TUD_CDC_EP_BUFSIZE (256)
+#define CFG_TUD_CDC_RX_BUFSIZE (256)
+#define CFG_TUD_CDC_TX_BUFSIZE (256)
+
+#if MICROPY_HW_USB_MSC
+// Board and hardware specific configuration
+#define CFG_TUD_MSC (1)
+// Set MSC EP buffer size to FatFS block size to avoid partial read/writes (offset arg).
+#define CFG_TUD_MSC_BUFSIZE (MICROPY_FATFS_MAX_SS)
+#endif
+
+#endif // MICROPY_INCLUDED_SHARED_TINYUSB_TUSB_CONFIG_H
diff --git a/shared/tinyusb/usbd.c b/shared/tinyusb/usbd.c
new file mode 100644
index 000000000..01e04b946
--- /dev/null
+++ b/shared/tinyusb/usbd.c
@@ -0,0 +1,32 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Blake W. Felt
+ *
+ * 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/runtime.h"
+#include "usbd.h"
+
+void usbd_reset_all(void) {
+ usbd_reset_descriptor();
+}
diff --git a/shared/tinyusb/usbd.h b/shared/tinyusb/usbd.h
new file mode 100644
index 000000000..9c450fb55
--- /dev/null
+++ b/shared/tinyusb/usbd.h
@@ -0,0 +1,41 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Blake W. Felt
+ *
+ * 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 MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H
+#define MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H
+
+#include "py/obj.h"
+
+// defined externally (needed per port)
+
+int usbd_serialnumber(uint8_t *buf);
+
+// external use
+
+void usbd_reset_all(void);
+void usbd_reset_descriptor(void);
+
+#endif // MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H
diff --git a/shared/tinyusb/usbd_descriptor.c b/shared/tinyusb/usbd_descriptor.c
new file mode 100644
index 000000000..d3c65aee8
--- /dev/null
+++ b/shared/tinyusb/usbd_descriptor.c
@@ -0,0 +1,157 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Damien P. George
+ * Copyright (c) 2022 Blake W. Felt
+ *
+ * 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 "mpconfigport.h"
+#include "tusb.h"
+#include "usbd.h"
+
+#if CFG_TUD_MSC
+#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN)
+#else
+#define USBD_DESC_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
+#endif
+#define USBD_MAX_POWER_MA (250)
+
+#define USBD_ITF_CDC (0) // needs 2 interfaces
+#define USBD_ITF_MSC (2)
+#if CFG_TUD_MSC
+#define USBD_ITF_MAX (3)
+#else
+#define USBD_ITF_MAX (2)
+#endif
+
+#define USBD_CDC_EP_CMD (0x81)
+#define USBD_CDC_EP_OUT (0x02)
+#define USBD_CDC_EP_IN (0x82)
+#define USBD_CDC_CMD_MAX_SIZE (8)
+#define USBD_CDC_IN_OUT_MAX_SIZE (64)
+
+#define EPNUM_MSC_OUT (0x03)
+#define EPNUM_MSC_IN (0x83)
+
+#define USBD_STR_0 (0x00)
+#define USBD_STR_MANUF (0x01)
+#define USBD_STR_PRODUCT (0x02)
+#define USBD_STR_SERIAL (0x03)
+#define USBD_STR_CDC (0x04)
+#define USBD_STR_MSC (0x05)
+
+#define USBD_DESC_STR_MAX (20)
+#define USBD_DESC_SERIAL_MAX (32)
+
+// Note: descriptors returned from callbacks must exist long enough for transfer to complete
+
+static const tusb_desc_device_t usbd_desc_device = {
+ .bLength = sizeof(tusb_desc_device_t),
+ .bDescriptorType = TUSB_DESC_DEVICE,
+ .bcdUSB = 0x0200,
+ .bDeviceClass = TUSB_CLASS_MISC,
+ .bDeviceSubClass = MISC_SUBCLASS_COMMON,
+ .bDeviceProtocol = MISC_PROTOCOL_IAD,
+ .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
+ .idVendor = MICROPY_HW_USB_VID,
+ .idProduct = MICROPY_HW_USB_PID,
+ .bcdDevice = 0x0100,
+ .iManufacturer = USBD_STR_MANUF,
+ .iProduct = USBD_STR_PRODUCT,
+ .iSerialNumber = USBD_STR_SERIAL,
+ .bNumConfigurations = 1,
+};
+
+static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
+ TUD_CONFIG_DESCRIPTOR(1, USBD_ITF_MAX, USBD_STR_0, USBD_DESC_LEN,
+ 0, USBD_MAX_POWER_MA),
+
+ TUD_CDC_DESCRIPTOR(USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD,
+ USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE),
+ #if CFG_TUD_MSC
+ TUD_MSC_DESCRIPTOR(USBD_ITF_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64),
+ #endif
+};
+
+static const char *const usbd_desc_str[] = {
+ [USBD_STR_MANUF] = "MicroPython",
+ [USBD_STR_PRODUCT] = "Board in FS mode",
+ [USBD_STR_SERIAL] = NULL, // generated dynamically
+ [USBD_STR_CDC] = "Board CDC",
+ #if CFG_TUD_MSC
+ [USBD_STR_MSC] = "Board MSC",
+ #endif
+};
+
+const uint8_t *tud_descriptor_device_cb(void) {
+ return (const uint8_t *)&usbd_desc_device;
+}
+
+const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
+ (void)index;
+ return usbd_desc_cfg;
+}
+
+const uint16_t *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
+ static uint16_t desc_str[USBD_DESC_STR_MAX];
+
+ uint8_t len;
+ if (index == 0) {
+ desc_str[1] = 0x0409; // supported language is English
+ len = 1;
+ } else {
+ if (index >= sizeof(usbd_desc_str) / sizeof(usbd_desc_str[0])) {
+ return NULL;
+ }
+ // check, if serial is requested
+ if (index == USBD_STR_SERIAL) {
+ uint8_t buffer[USBD_DESC_SERIAL_MAX] = {0};
+ int buflen;
+ const char *hexdig = "0123456789abcdef";
+
+ buflen = usbd_serialnumber(buffer);
+ // byte by byte conversion
+ len = 0;
+ for (int i=0; i<buflen; i++) {
+ uint8_t val = buffer[i];
+ desc_str[1 + len] = hexdig[val >> 4];
+ desc_str[2 + len] = hexdig[val & 0x0F];
+ len += 2;
+ }
+ } else {
+ const char *str = usbd_desc_str[index];
+ for (len = 0; len < USBD_DESC_STR_MAX - 1 && str[len]; ++len) {
+ desc_str[1 + len] = str[len];
+ }
+ }
+ }
+
+ // first byte is length (including header), second byte is string type
+ desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * len + 2);
+
+ return desc_str;
+}
+
+void usbd_reset_descriptor(void) {
+ // not used yet
+}