summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaureen Helm <maureen.helm@analog.com>2024-09-08 15:09:57 -0500
committerMaureen Helm <maureen.helm@analog.com>2024-10-01 20:02:01 -0500
commitf33df7197e30db1e0ede3632a0baebf8f711d7d2 (patch)
tree18ebfcffdd4536a122372da6f9ddc5e580da4599
parent545d4efb5590b0144e1f29e437d816a7b58aa40d (diff)
zephyr: Refactor device lookup into a common helper function.
Refactors Zephyr device lookup operations into a common helper function to reduce boilerplate code that was repeated in multiple modules. Suggested-by: Damien George <damien@micropython.org> Signed-off-by: Maureen Helm <maureen.helm@analog.com>
-rw-r--r--ports/zephyr/CMakeLists.txt1
-rw-r--r--ports/zephyr/machine_i2c.c8
-rw-r--r--ports/zephyr/machine_pin.c7
-rw-r--r--ports/zephyr/machine_spi.c8
-rw-r--r--ports/zephyr/machine_uart.c6
-rw-r--r--ports/zephyr/modzsensor.c6
-rw-r--r--ports/zephyr/zephyr_device.c43
-rw-r--r--ports/zephyr/zephyr_device.h30
8 files changed, 84 insertions, 25 deletions
diff --git a/ports/zephyr/CMakeLists.txt b/ports/zephyr/CMakeLists.txt
index a2c212031..68b6c0700 100644
--- a/ports/zephyr/CMakeLists.txt
+++ b/ports/zephyr/CMakeLists.txt
@@ -46,6 +46,7 @@ set(MICROPY_SOURCE_PORT
modzsensor.c
mphalport.c
uart_core.c
+ zephyr_device.c
zephyr_storage.c
mpthreadport.c
)
diff --git a/ports/zephyr/machine_i2c.c b/ports/zephyr/machine_i2c.c
index 342ebafe7..ee8edfa34 100644
--- a/ports/zephyr/machine_i2c.c
+++ b/ports/zephyr/machine_i2c.c
@@ -37,6 +37,7 @@
#include "py/mphal.h"
#include "py/mperrno.h"
#include "extmod/modmachine.h"
+#include "zephyr_device.h"
#if MICROPY_PY_MACHINE_I2C
@@ -64,12 +65,7 @@ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
- const char *dev_name = mp_obj_str_get_str(args[ARG_id].u_obj);
- const struct device *dev = device_get_binding(dev_name);
-
- if (dev == NULL) {
- mp_raise_ValueError(MP_ERROR_TEXT("device not found"));
- }
+ const struct device *dev = zephyr_device_find(args[ARG_id].u_obj);
if ((args[ARG_scl].u_obj != MP_OBJ_NULL) || (args[ARG_sda].u_obj != MP_OBJ_NULL)) {
mp_raise_NotImplementedError(MP_ERROR_TEXT("explicit choice of scl/sda is not implemented"));
diff --git a/ports/zephyr/machine_pin.c b/ports/zephyr/machine_pin.c
index 8a62ea1d2..224019669 100644
--- a/ports/zephyr/machine_pin.c
+++ b/ports/zephyr/machine_pin.c
@@ -38,6 +38,7 @@
#include "extmod/modmachine.h"
#include "shared/runtime/mpirq.h"
#include "modmachine.h"
+#include "zephyr_device.h"
#if MICROPY_PY_MACHINE
@@ -131,12 +132,8 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
}
mp_obj_t *items;
mp_obj_get_array_fixed_n(args[0], 2, &items);
- const char *drv_name = mp_obj_str_get_str(items[0]);
+ const struct device *wanted_port = zephyr_device_find(items[0]);
int wanted_pin = mp_obj_get_int(items[1]);
- const struct device *wanted_port = device_get_binding(drv_name);
- if (!wanted_port) {
- mp_raise_ValueError(MP_ERROR_TEXT("invalid port"));
- }
machine_pin_obj_t *pin = m_new_obj(machine_pin_obj_t);
pin->base = machine_pin_obj_template;
diff --git a/ports/zephyr/machine_spi.c b/ports/zephyr/machine_spi.c
index 80bfa2e54..23e44caf3 100644
--- a/ports/zephyr/machine_spi.c
+++ b/ports/zephyr/machine_spi.c
@@ -36,6 +36,7 @@
#include "py/mphal.h"
#include "py/mperrno.h"
#include "extmod/modmachine.h"
+#include "zephyr_device.h"
#if MICROPY_PY_MACHINE_SPI
@@ -81,12 +82,7 @@ mp_obj_t machine_hard_spi_make_new(const mp_obj_type_t *type, size_t n_args, siz
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
- const char *dev_name = mp_obj_str_get_str(args[ARG_id].u_obj);
- const struct device *dev = device_get_binding(dev_name);
-
- if (dev == NULL) {
- mp_raise_ValueError(MP_ERROR_TEXT("device not found"));
- }
+ const struct device *dev = zephyr_device_find(args[ARG_id].u_obj);
if ((args[ARG_sck].u_obj != MP_OBJ_NULL) || (args[ARG_miso].u_obj != MP_OBJ_NULL) || (args[ARG_mosi].u_obj != MP_OBJ_NULL)) {
mp_raise_NotImplementedError(MP_ERROR_TEXT("explicit choice of sck/miso/mosi is not implemented"));
diff --git a/ports/zephyr/machine_uart.c b/ports/zephyr/machine_uart.c
index ae5ac4a19..1927335dd 100644
--- a/ports/zephyr/machine_uart.c
+++ b/ports/zephyr/machine_uart.c
@@ -32,6 +32,7 @@
#include <zephyr/drivers/uart.h>
#include "py/mperrno.h"
+#include "zephyr_device.h"
// The UART class doesn't have any constants for this port.
#define MICROPY_PY_MACHINE_UART_CLASS_CONSTANTS
@@ -75,10 +76,7 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
machine_uart_obj_t *self = mp_obj_malloc(machine_uart_obj_t, &machine_uart_type);
- self->dev = device_get_binding(mp_obj_str_get_str(args[0]));
- if (!self->dev) {
- mp_raise_ValueError(MP_ERROR_TEXT("Bad device name"));
- }
+ self->dev = zephyr_device_find(args[0]);
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
diff --git a/ports/zephyr/modzsensor.c b/ports/zephyr/modzsensor.c
index de7fe7f52..51b7b2dbf 100644
--- a/ports/zephyr/modzsensor.c
+++ b/ports/zephyr/modzsensor.c
@@ -30,6 +30,7 @@
#include <zephyr/kernel.h>
#include <zephyr/drivers/sensor.h>
+#include "zephyr_device.h"
#if MICROPY_PY_ZSENSOR
@@ -41,10 +42,7 @@ typedef struct _mp_obj_sensor_t {
static mp_obj_t sensor_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, 1, false);
mp_obj_sensor_t *o = mp_obj_malloc(mp_obj_sensor_t, type);
- o->dev = device_get_binding(mp_obj_str_get_str(args[0]));
- if (o->dev == NULL) {
- mp_raise_ValueError(MP_ERROR_TEXT("dev not found"));
- }
+ o->dev = zephyr_device_find(args[0]);
return MP_OBJ_FROM_PTR(o);
}
diff --git a/ports/zephyr/zephyr_device.c b/ports/zephyr/zephyr_device.c
new file mode 100644
index 000000000..2ef331742
--- /dev/null
+++ b/ports/zephyr/zephyr_device.c
@@ -0,0 +1,43 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2024 Analog Devices, Inc.
+ *
+ * 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 "zephyr_device.h"
+#include "py/runtime.h"
+
+const struct device *zephyr_device_find(mp_obj_t name) {
+ const char *dev_name = mp_obj_str_get_str(name);
+ const struct device *dev = device_get_binding(dev_name);
+
+ if (dev == NULL) {
+ #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
+ mp_raise_ValueError(MP_ERROR_TEXT("device not found"));
+ #else
+ mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("device %s not found"), dev_name);
+ #endif
+ }
+
+ return dev;
+}
diff --git a/ports/zephyr/zephyr_device.h b/ports/zephyr/zephyr_device.h
new file mode 100644
index 000000000..ef82eba93
--- /dev/null
+++ b/ports/zephyr/zephyr_device.h
@@ -0,0 +1,30 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2024 Analog Devices, Inc.
+ *
+ * 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 <zephyr/device.h>
+#include "py/obj.h"
+
+const struct device *zephyr_device_find(mp_obj_t name);