diff options
Diffstat (limited to 'circuitpython/shared-module/usb')
| -rw-r--r-- | circuitpython/shared-module/usb/__init__.c | 27 | ||||
| -rw-r--r-- | circuitpython/shared-module/usb/core/Device.c | 185 | ||||
| -rw-r--r-- | circuitpython/shared-module/usb/core/Device.h | 37 | ||||
| -rw-r--r-- | circuitpython/shared-module/usb/core/__init__.c | 27 | ||||
| -rw-r--r-- | circuitpython/shared-module/usb/utf16le.c | 85 | ||||
| -rw-r--r-- | circuitpython/shared-module/usb/utf16le.h | 34 |
6 files changed, 395 insertions, 0 deletions
diff --git a/circuitpython/shared-module/usb/__init__.c b/circuitpython/shared-module/usb/__init__.c new file mode 100644 index 0000000..2b3a4c5 --- /dev/null +++ b/circuitpython/shared-module/usb/__init__.c @@ -0,0 +1,27 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Scott Shawcroft 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. + */ + +// Nothing here diff --git a/circuitpython/shared-module/usb/core/Device.c b/circuitpython/shared-module/usb/core/Device.c new file mode 100644 index 0000000..81431d5 --- /dev/null +++ b/circuitpython/shared-module/usb/core/Device.c @@ -0,0 +1,185 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Scott Shawcroft 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 "shared-bindings/usb/core/Device.h" + +#include "tusb_config.h" + +#include "lib/tinyusb/src/host/usbh.h" +#include "py/runtime.h" +#include "shared/runtime/interrupt_char.h" +#include "shared-bindings/usb/core/__init__.h" +#include "shared-module/usb/utf16le.h" +#include "supervisor/shared/tick.h" + +bool common_hal_usb_core_device_construct(usb_core_device_obj_t *self, uint8_t device_number) { + if (device_number == 0 || device_number > CFG_TUH_DEVICE_MAX + CFG_TUH_HUB) { + return false; + } + if (!tuh_ready(device_number)) { + return false; + } + self->device_number = device_number; + return true; +} + +uint16_t common_hal_usb_core_device_get_idVendor(usb_core_device_obj_t *self) { + uint16_t vid; + uint16_t pid; + tuh_vid_pid_get(self->device_number, &vid, &pid); + return vid; +} + +uint16_t common_hal_usb_core_device_get_idProduct(usb_core_device_obj_t *self) { + uint16_t vid; + uint16_t pid; + tuh_vid_pid_get(self->device_number, &vid, &pid); + return pid; +} + +STATIC xfer_result_t _get_string_result; +STATIC bool _transfer_done_cb(uint8_t daddr, tusb_control_request_t const *request, xfer_result_t result) { + // Store the result so we stop waiting for the transfer. We don't need the other data for now. + (void)daddr; + (void)request; + _get_string_result = result; + return true; +} + +STATIC void _wait_for_callback(void) { + while (!mp_hal_is_interrupted() && + _get_string_result == 0xff) { + // The background tasks include TinyUSB which will call the function + // we provided above. In other words, the callback isn't in an interrupt. + RUN_BACKGROUND_TASKS; + } +} + +STATIC mp_obj_t _get_string(const uint16_t *temp_buf) { + size_t utf16_len = ((temp_buf[0] & 0xff) - 2) / sizeof(uint16_t); + if (utf16_len == 0) { + return mp_const_none; + } + return utf16le_to_string(temp_buf + 1, utf16_len); +} + +mp_obj_t common_hal_usb_core_device_get_serial_number(usb_core_device_obj_t *self) { + _get_string_result = 0xff; + uint16_t temp_buf[127]; + if (!tuh_descriptor_string_serial_get(self->device_number, 0, temp_buf, MP_ARRAY_SIZE(temp_buf), _transfer_done_cb)) { + return mp_const_none; + } + _wait_for_callback(); + return _get_string(temp_buf); +} + +mp_obj_t common_hal_usb_core_device_get_product(usb_core_device_obj_t *self) { + _get_string_result = 0xff; + uint16_t temp_buf[127]; + if (!tuh_descriptor_string_product_get(self->device_number, 0, temp_buf, MP_ARRAY_SIZE(temp_buf), _transfer_done_cb)) { + return mp_const_none; + } + _wait_for_callback(); + return _get_string(temp_buf); +} + +mp_obj_t common_hal_usb_core_device_get_manufacturer(usb_core_device_obj_t *self) { + _get_string_result = 0xff; + uint16_t temp_buf[127]; + if (!tuh_descriptor_string_manufacturer_get(self->device_number, 0, temp_buf, MP_ARRAY_SIZE(temp_buf), _transfer_done_cb)) { + return mp_const_none; + } + _wait_for_callback(); + return _get_string(temp_buf); +} + +mp_obj_t common_hal_usb_core_device_write(usb_core_device_obj_t *self, mp_int_t endpoint, const uint8_t *buffer, mp_int_t len, mp_int_t timeout) { + return mp_const_none; +} + +mp_obj_t common_hal_usb_core_device_read(usb_core_device_obj_t *self, mp_int_t endpoint, uint8_t *buffer, mp_int_t len, mp_int_t timeout) { + return mp_const_none; +} + +xfer_result_t control_result; +STATIC bool _control_complete_cb(uint8_t dev_addr, tusb_control_request_t const *request, xfer_result_t result) { + (void)dev_addr; + (void)request; + control_result = result; + return true; +} + +mp_int_t common_hal_usb_core_device_ctrl_transfer(usb_core_device_obj_t *self, + mp_int_t bmRequestType, mp_int_t bRequest, + mp_int_t wValue, mp_int_t wIndex, + uint8_t *buffer, mp_int_t len, mp_int_t timeout) { + // Timeout is in ms. + + tusb_control_request_t request = { + .bmRequestType = bmRequestType, + .bRequest = bRequest, + .wValue = wValue, + .wIndex = wIndex, + .wLength = len + }; + control_result = XFER_RESULT_STALLED; + bool result = tuh_control_xfer(self->device_number, + &request, + buffer, + _control_complete_cb); + if (!result) { + mp_raise_usb_core_USBError(NULL); + } + uint32_t start_time = supervisor_ticks_ms32(); + while (supervisor_ticks_ms32() - start_time < (uint32_t)timeout && + !mp_hal_is_interrupted() && + control_result == XFER_RESULT_STALLED) { + // The background tasks include TinyUSB which will call the function + // we provided above. In other words, the callback isn't in an interrupt. + RUN_BACKGROUND_TASKS; + } + if (control_result == XFER_RESULT_STALLED) { + mp_raise_usb_core_USBTimeoutError(); + } + if (control_result == XFER_RESULT_SUCCESS) { + return len; + } + + return 0; +} + +bool common_hal_usb_core_device_is_kernel_driver_active(usb_core_device_obj_t *self, mp_int_t interface) { + // TODO: Implement this when CP natively uses a keyboard. + return false; +} + +void common_hal_usb_core_device_detach_kernel_driver(usb_core_device_obj_t *self, mp_int_t interface) { + // TODO: Implement this when CP natively uses a keyboard. +} + +void common_hal_usb_core_device_attach_kernel_driver(usb_core_device_obj_t *self, mp_int_t interface) { + // TODO: Implement this when CP natively uses a keyboard. +} diff --git a/circuitpython/shared-module/usb/core/Device.h b/circuitpython/shared-module/usb/core/Device.h new file mode 100644 index 0000000..5959639 --- /dev/null +++ b/circuitpython/shared-module/usb/core/Device.h @@ -0,0 +1,37 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Scott Shawcroft 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 MICROPY_INCLUDED_SHARED_MODULE_USB_CORE_DEVICE_H +#define MICROPY_INCLUDED_SHARED_MODULE_USB_CORE_DEVICE_H + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t device_number; +} usb_core_device_obj_t; + +#endif // MICROPY_INCLUDED_SHARED_MODULE_USB_CORE_DEVICE_H diff --git a/circuitpython/shared-module/usb/core/__init__.c b/circuitpython/shared-module/usb/core/__init__.c new file mode 100644 index 0000000..a108f5f --- /dev/null +++ b/circuitpython/shared-module/usb/core/__init__.c @@ -0,0 +1,27 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Scott Shawcroft 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. + */ + +// Nothing implementation specific. diff --git a/circuitpython/shared-module/usb/utf16le.c b/circuitpython/shared-module/usb/utf16le.c new file mode 100644 index 0000000..24ccd09 --- /dev/null +++ b/circuitpython/shared-module/usb/utf16le.c @@ -0,0 +1,85 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Scott Shawcroft 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 "shared-module/usb/utf16le.h" + +STATIC void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) { + // TODO: Check for runover. + (void)utf8_len; + + for (size_t i = 0; i < utf16_len; i++) { + uint16_t chr = utf16[i]; + if (chr < 0x80) { + *utf8++ = chr & 0xff; + } else if (chr < 0x800) { + *utf8++ = (uint8_t)(0xC0 | (chr >> 6 & 0x1F)); + *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F)); + } else if (chr < 0x10000) { + // TODO: Verify surrogate. + *utf8++ = (uint8_t)(0xE0 | (chr >> 12 & 0x0F)); + *utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F)); + *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F)); + } else { + // TODO: Handle UTF-16 code points that take two entries. + uint32_t hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6; /* Get high 10 bits */ + chr = (chr & 0xFFFF) - 0xDC00; /* Get low 10 bits */ + chr = (hc | chr) + 0x10000; + *utf8++ = (uint8_t)(0xF0 | (chr >> 18 & 0x07)); + *utf8++ = (uint8_t)(0x80 | (chr >> 12 & 0x3F)); + *utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F)); + *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F)); + } + } +} + +// Count how many bytes a utf-16-le encoded string will take in utf-8. +STATIC mp_int_t _count_utf8_bytes(const uint16_t *buf, size_t len) { + size_t total_bytes = 0; + for (size_t i = 0; i < len; i++) { + uint16_t chr = buf[i]; + if (chr < 0x80) { + total_bytes += 1; + } else if (chr < 0x800) { + total_bytes += 2; + } else if (chr < 0x10000) { + total_bytes += 3; + } else { + total_bytes += 4; + } + } + return total_bytes; +} + +mp_obj_t utf16le_to_string(const uint16_t *buf, size_t utf16_len) { + size_t size = _count_utf8_bytes(buf, utf16_len); + vstr_t vstr; + vstr_init_len(&vstr, size + 1); + byte *p = (byte *)vstr.buf; + // Null terminate. + p[size] = '\0'; + _convert_utf16le_to_utf8(buf, utf16_len, p, size); + return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); +} diff --git a/circuitpython/shared-module/usb/utf16le.h b/circuitpython/shared-module/usb/utf16le.h new file mode 100644 index 0000000..7305a1a --- /dev/null +++ b/circuitpython/shared-module/usb/utf16le.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2022 Scott Shawcroft 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 MICROPY_INCLUDED_SHARED_MODULE_USB_UTF16LE_H +#define MICROPY_INCLUDED_SHARED_MODULE_USB_UTF16LE_H + +#include "py/obj.h" + +mp_obj_t utf16le_to_string(const uint16_t *buf, size_t utf16_len); + +#endif // MICROPY_INCLUDED_SHARED_MODULE_USB_UTF16LE_H |
