Skip to content

Commit

Permalink
zephyr: Refactor device lookup into a common helper function.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
MaureenHelm committed Oct 2, 2024
1 parent 545d4ef commit f33df71
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 25 deletions.
1 change: 1 addition & 0 deletions ports/zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ set(MICROPY_SOURCE_PORT
modzsensor.c
mphalport.c
uart_core.c
zephyr_device.c
zephyr_storage.c
mpthreadport.c
)
Expand Down
8 changes: 2 additions & 6 deletions ports/zephyr/machine_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"));
Expand Down
7 changes: 2 additions & 5 deletions ports/zephyr/machine_pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "extmod/modmachine.h"
#include "shared/runtime/mpirq.h"
#include "modmachine.h"
#include "zephyr_device.h"

#if MICROPY_PY_MACHINE

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions ports/zephyr/machine_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"));
Expand Down
6 changes: 2 additions & 4 deletions ports/zephyr/machine_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions ports/zephyr/modzsensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <zephyr/kernel.h>
#include <zephyr/drivers/sensor.h>
#include "zephyr_device.h"

#if MICROPY_PY_ZSENSOR

Expand All @@ -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);
}

Expand Down
43 changes: 43 additions & 0 deletions ports/zephyr/zephyr_device.c
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 30 additions & 0 deletions ports/zephyr/zephyr_device.h
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit f33df71

Please sign in to comment.