Skip to content

Commit f33df71

Browse files
committed
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 <[email protected]> Signed-off-by: Maureen Helm <[email protected]>
1 parent 545d4ef commit f33df71

8 files changed

+84
-25
lines changed

ports/zephyr/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ set(MICROPY_SOURCE_PORT
4646
modzsensor.c
4747
mphalport.c
4848
uart_core.c
49+
zephyr_device.c
4950
zephyr_storage.c
5051
mpthreadport.c
5152
)

ports/zephyr/machine_i2c.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "py/mphal.h"
3838
#include "py/mperrno.h"
3939
#include "extmod/modmachine.h"
40+
#include "zephyr_device.h"
4041

4142
#if MICROPY_PY_MACHINE_I2C
4243

@@ -64,12 +65,7 @@ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz
6465
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
6566
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
6667

67-
const char *dev_name = mp_obj_str_get_str(args[ARG_id].u_obj);
68-
const struct device *dev = device_get_binding(dev_name);
69-
70-
if (dev == NULL) {
71-
mp_raise_ValueError(MP_ERROR_TEXT("device not found"));
72-
}
68+
const struct device *dev = zephyr_device_find(args[ARG_id].u_obj);
7369

7470
if ((args[ARG_scl].u_obj != MP_OBJ_NULL) || (args[ARG_sda].u_obj != MP_OBJ_NULL)) {
7571
mp_raise_NotImplementedError(MP_ERROR_TEXT("explicit choice of scl/sda is not implemented"));

ports/zephyr/machine_pin.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "extmod/modmachine.h"
3939
#include "shared/runtime/mpirq.h"
4040
#include "modmachine.h"
41+
#include "zephyr_device.h"
4142

4243
#if MICROPY_PY_MACHINE
4344

@@ -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,
131132
}
132133
mp_obj_t *items;
133134
mp_obj_get_array_fixed_n(args[0], 2, &items);
134-
const char *drv_name = mp_obj_str_get_str(items[0]);
135+
const struct device *wanted_port = zephyr_device_find(items[0]);
135136
int wanted_pin = mp_obj_get_int(items[1]);
136-
const struct device *wanted_port = device_get_binding(drv_name);
137-
if (!wanted_port) {
138-
mp_raise_ValueError(MP_ERROR_TEXT("invalid port"));
139-
}
140137

141138
machine_pin_obj_t *pin = m_new_obj(machine_pin_obj_t);
142139
pin->base = machine_pin_obj_template;

ports/zephyr/machine_spi.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "py/mphal.h"
3737
#include "py/mperrno.h"
3838
#include "extmod/modmachine.h"
39+
#include "zephyr_device.h"
3940

4041
#if MICROPY_PY_MACHINE_SPI
4142

@@ -81,12 +82,7 @@ mp_obj_t machine_hard_spi_make_new(const mp_obj_type_t *type, size_t n_args, siz
8182
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
8283
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
8384

84-
const char *dev_name = mp_obj_str_get_str(args[ARG_id].u_obj);
85-
const struct device *dev = device_get_binding(dev_name);
86-
87-
if (dev == NULL) {
88-
mp_raise_ValueError(MP_ERROR_TEXT("device not found"));
89-
}
85+
const struct device *dev = zephyr_device_find(args[ARG_id].u_obj);
9086

9187
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)) {
9288
mp_raise_NotImplementedError(MP_ERROR_TEXT("explicit choice of sck/miso/mosi is not implemented"));

ports/zephyr/machine_uart.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <zephyr/drivers/uart.h>
3333

3434
#include "py/mperrno.h"
35+
#include "zephyr_device.h"
3536

3637
// The UART class doesn't have any constants for this port.
3738
#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
7576
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
7677

7778
machine_uart_obj_t *self = mp_obj_malloc(machine_uart_obj_t, &machine_uart_type);
78-
self->dev = device_get_binding(mp_obj_str_get_str(args[0]));
79-
if (!self->dev) {
80-
mp_raise_ValueError(MP_ERROR_TEXT("Bad device name"));
81-
}
79+
self->dev = zephyr_device_find(args[0]);
8280

8381
mp_map_t kw_args;
8482
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);

ports/zephyr/modzsensor.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include <zephyr/kernel.h>
3232
#include <zephyr/drivers/sensor.h>
33+
#include "zephyr_device.h"
3334

3435
#if MICROPY_PY_ZSENSOR
3536

@@ -41,10 +42,7 @@ typedef struct _mp_obj_sensor_t {
4142
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) {
4243
mp_arg_check_num(n_args, n_kw, 1, 1, false);
4344
mp_obj_sensor_t *o = mp_obj_malloc(mp_obj_sensor_t, type);
44-
o->dev = device_get_binding(mp_obj_str_get_str(args[0]));
45-
if (o->dev == NULL) {
46-
mp_raise_ValueError(MP_ERROR_TEXT("dev not found"));
47-
}
45+
o->dev = zephyr_device_find(args[0]);
4846
return MP_OBJ_FROM_PTR(o);
4947
}
5048

ports/zephyr/zephyr_device.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2024 Analog Devices, Inc.
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 "zephyr_device.h"
28+
#include "py/runtime.h"
29+
30+
const struct device *zephyr_device_find(mp_obj_t name) {
31+
const char *dev_name = mp_obj_str_get_str(name);
32+
const struct device *dev = device_get_binding(dev_name);
33+
34+
if (dev == NULL) {
35+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
36+
mp_raise_ValueError(MP_ERROR_TEXT("device not found"));
37+
#else
38+
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("device %s not found"), dev_name);
39+
#endif
40+
}
41+
42+
return dev;
43+
}

ports/zephyr/zephyr_device.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2024 Analog Devices, Inc.
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 <zephyr/device.h>
28+
#include "py/obj.h"
29+
30+
const struct device *zephyr_device_find(mp_obj_t name);

0 commit comments

Comments
 (0)