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>
This commit is contained in:
Maureen Helm
2024-09-08 15:09:57 -05:00
parent 545d4efb55
commit f33df7197e
8 changed files with 84 additions and 25 deletions

View File

@@ -46,6 +46,7 @@ set(MICROPY_SOURCE_PORT
modzsensor.c
mphalport.c
uart_core.c
zephyr_device.c
zephyr_storage.c
mpthreadport.c
)

View File

@@ -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"));

View File

@@ -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;

View File

@@ -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"));

View File

@@ -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);

View File

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

View File

@@ -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;
}

View File

@@ -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);