aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/shared-bindings/usb_host
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2022-06-19 19:47:51 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2022-06-19 19:47:51 +0530
commit4fd287655a72b9aea14cdac715ad5b90ed082ed2 (patch)
tree65d393bc0e699dd12d05b29ba568e04cea666207 /circuitpython/shared-bindings/usb_host
parent0150f70ce9c39e9e6dd878766c0620c85e47bed0 (diff)
add circuitpython code
Diffstat (limited to 'circuitpython/shared-bindings/usb_host')
-rw-r--r--circuitpython/shared-bindings/usb_host/Port.c103
-rw-r--r--circuitpython/shared-bindings/usb_host/Port.h42
-rw-r--r--circuitpython/shared-bindings/usb_host/__init__.c53
-rw-r--r--circuitpython/shared-bindings/usb_host/__init__.h27
4 files changed, 225 insertions, 0 deletions
diff --git a/circuitpython/shared-bindings/usb_host/Port.c b/circuitpython/shared-bindings/usb_host/Port.c
new file mode 100644
index 0000000..8f54246
--- /dev/null
+++ b/circuitpython/shared-bindings/usb_host/Port.c
@@ -0,0 +1,103 @@
+/*
+ * This file is part of the Micro Python 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/runtime/context_manager_helpers.h"
+#include "shared-bindings/usb_host/Port.h"
+#include "shared-bindings/util.h"
+#include "py/objproperty.h"
+#include "py/runtime.h"
+
+//| class Port:
+//| """USB host port. Also known as a root hub port."""
+//|
+//| def __init__(self, dp: microcontroller.Pin, dm: microcontroller.Pin) -> None:
+//| """Create a USB host port on the given pins. Access attached devices
+//| through the `usb` module. Keep this object referenced while
+//| interacting with devices, otherwise they will be disconnected.
+//|
+//| :param ~microcontroller.Pin dp: The data plus pin
+//| :param ~microcontroller.Pin dm: The data minus pin
+//| """
+//| ...
+//|
+STATIC mp_obj_t usb_host_port_make_new(const mp_obj_type_t *type,
+ size_t n_args, size_t n_kw, const mp_obj_t *args) {
+ // check number of arguments
+ mp_arg_check_num(n_args, n_kw, 2, 2, false);
+
+ const mcu_pin_obj_t *dp = validate_obj_is_free_pin(args[0]);
+ const mcu_pin_obj_t *dm = validate_obj_is_free_pin(args[1]);
+
+ usb_host_port_obj_t *self = m_new_obj(usb_host_port_obj_t);
+ self->base.type = &usb_host_port_type;
+ common_hal_usb_host_port_construct(self, dp, dm);
+
+ return (mp_obj_t)self;
+}
+
+//| def deinit(self) -> None:
+//| """Turn off the USB host port and release the pins for other use."""
+//| ...
+//|
+STATIC mp_obj_t usb_host_port_obj_deinit(mp_obj_t self_in) {
+ usb_host_port_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_usb_host_port_deinit(self);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_1(usb_host_port_deinit_obj, usb_host_port_obj_deinit);
+
+//| def __enter__(self) -> Port:
+//| """No-op used by Context Managers."""
+//| ...
+//|
+// Provided by context manager helper.
+
+//| def __exit__(self) -> None:
+//| """Automatically deinitializes the hardware when exiting a context. See
+//| :ref:`lifetime-and-contextmanagers` for more info."""
+//| ...
+//|
+STATIC mp_obj_t usb_host_port_obj___exit__(size_t n_args, const mp_obj_t *args) {
+ (void)n_args;
+ common_hal_usb_host_port_deinit(MP_OBJ_TO_PTR(args[0]));
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(usb_host_port_obj___exit___obj, 4, 4, usb_host_port_obj___exit__);
+
+STATIC const mp_rom_map_elem_t usb_host_port_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&usb_host_port_deinit_obj) },
+ { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
+ { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&usb_host_port_obj___exit___obj) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(usb_host_port_locals_dict, usb_host_port_locals_dict_table);
+
+const mp_obj_type_t usb_host_port_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_Port,
+ .make_new = usb_host_port_make_new,
+ .locals_dict = (mp_obj_t)&usb_host_port_locals_dict,
+};
diff --git a/circuitpython/shared-bindings/usb_host/Port.h b/circuitpython/shared-bindings/usb_host/Port.h
new file mode 100644
index 0000000..68645d1
--- /dev/null
+++ b/circuitpython/shared-bindings/usb_host/Port.h
@@ -0,0 +1,42 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 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_BINDINGS_USB_HOST_PORT_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_USB_HOST_PORT_H
+
+#include "py/objarray.h"
+
+#include "shared-bindings/microcontroller/Pin.h"
+
+#include "common-hal/usb_host/Port.h"
+
+extern const mp_obj_type_t usb_host_port_type;
+
+void common_hal_usb_host_port_construct(usb_host_port_obj_t *self, const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm);
+void common_hal_usb_host_port_deinit(usb_host_port_obj_t *self);
+bool common_hal_usb_host_port_deinited(usb_host_port_obj_t *self);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_HOST_PORT_H
diff --git a/circuitpython/shared-bindings/usb_host/__init__.c b/circuitpython/shared-bindings/usb_host/__init__.c
new file mode 100644
index 0000000..c689a25
--- /dev/null
+++ b/circuitpython/shared-bindings/usb_host/__init__.c
@@ -0,0 +1,53 @@
+/*
+ * 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 "py/obj.h"
+#include "py/mphal.h"
+#include "py/runtime.h"
+
+#include "shared-bindings/usb_host/__init__.h"
+#include "shared-bindings/usb_host/Port.h"
+
+//| """USB Host
+//|
+//| The `usb_host` module allows you to manage USB host ports. To communicate
+//| with devices use the `usb` module that is a subset of PyUSB's API.
+//| """
+//|
+
+STATIC mp_map_elem_t usb_host_module_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usb_host) },
+ { MP_ROM_QSTR(MP_QSTR_Port), MP_OBJ_FROM_PTR(&usb_host_port_type) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(usb_host_module_globals, usb_host_module_globals_table);
+
+const mp_obj_module_t usb_host_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t *)&usb_host_module_globals,
+};
+
+MP_REGISTER_MODULE(MP_QSTR_usb_host, usb_host_module, CIRCUITPY_USB_HOST);
diff --git a/circuitpython/shared-bindings/usb_host/__init__.h b/circuitpython/shared-bindings/usb_host/__init__.h
new file mode 100644
index 0000000..d672285
--- /dev/null
+++ b/circuitpython/shared-bindings/usb_host/__init__.h
@@ -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.
+ */
+
+#pragma once