From 8fd6bff7274335d139691d2ce0438b01e58b3ec2 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 1 Feb 2021 20:03:23 -0800 Subject: [PATCH] Add .spi accessor to SPIDevice Fixes #4108 --- .../adafruit_bus_device/SPIDevice.c | 34 +++++++++++++++++-- .../adafruit_bus_device/SPIDevice.h | 3 +- shared-module/adafruit_bus_device/SPIDevice.c | 7 +++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/shared-bindings/adafruit_bus_device/SPIDevice.c b/shared-bindings/adafruit_bus_device/SPIDevice.c index e127e39b81..e37f883682 100644 --- a/shared-bindings/adafruit_bus_device/SPIDevice.c +++ b/shared-bindings/adafruit_bus_device/SPIDevice.c @@ -34,6 +34,7 @@ #include "lib/utils/buffer_helper.h" #include "lib/utils/context_manager_helpers.h" +#include "py/objproperty.h" #include "py/runtime.h" #include "supervisor/shared/translate.h" @@ -100,22 +101,51 @@ STATIC mp_obj_t adafruit_bus_device_spidevice_make_new(const mp_obj_type_t *type return (mp_obj_t)self; } +//| def __enter__(self) -> busio.SPI: +//| """Starts a SPI transaction by configuring the SPI and asserting chip select.""" +//| ... +//| STATIC mp_obj_t adafruit_bus_device_spidevice_obj___enter__(mp_obj_t self_in) { adafruit_bus_device_spidevice_obj_t *self = MP_OBJ_TO_PTR(self_in); - common_hal_adafruit_bus_device_spidevice_enter(self); - return self->spi; + return common_hal_adafruit_bus_device_spidevice_enter(self); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(adafruit_bus_device_spidevice___enter___obj, adafruit_bus_device_spidevice_obj___enter__); + +//| def __exit__(self) -> None: +//| """Ends a SPI transaction by deasserting chip select. See +//| :ref:`lifetime-and-contextmanagers` for more info.""" +//| ... +//| STATIC mp_obj_t adafruit_bus_device_spidevice_obj___exit__(size_t n_args, const mp_obj_t *args) { common_hal_adafruit_bus_device_spidevice_exit(MP_OBJ_TO_PTR(args[0])); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(adafruit_bus_device_spidevice___exit___obj, 4, 4, adafruit_bus_device_spidevice_obj___exit__); +//| spi: busio.SPI +//| """The underlying SPI bus. Useful for weird uses like clocking an SD card without chip select. +//| +//| You shouldn't normally need this.""" +//| +STATIC mp_obj_t adafruit_bus_device_spidevice_obj_get_spi(mp_obj_t self_in) { + adafruit_bus_device_spidevice_obj_t *self = MP_OBJ_TO_PTR(self_in); + return common_hal_adafruit_bus_device_spidevice_get_spi(self); +} +MP_DEFINE_CONST_FUN_OBJ_1(adafruit_bus_device_spidevice_get_spi_obj, adafruit_bus_device_spidevice_obj_get_spi); + +const mp_obj_property_t adafruit_bus_device_spidevice_spi_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&adafruit_bus_device_spidevice_get_spi_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + STATIC const mp_rom_map_elem_t adafruit_bus_device_spidevice_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&adafruit_bus_device_spidevice___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&adafruit_bus_device_spidevice___exit___obj) }, + + { MP_ROM_QSTR(MP_QSTR_spi), MP_ROM_PTR(&adafruit_bus_device_spidevice_spi_obj) }, }; STATIC MP_DEFINE_CONST_DICT(adafruit_bus_device_spidevice_locals_dict, adafruit_bus_device_spidevice_locals_dict_table); diff --git a/shared-bindings/adafruit_bus_device/SPIDevice.h b/shared-bindings/adafruit_bus_device/SPIDevice.h index 5596b157f0..baa9ec464a 100644 --- a/shared-bindings/adafruit_bus_device/SPIDevice.h +++ b/shared-bindings/adafruit_bus_device/SPIDevice.h @@ -44,7 +44,8 @@ extern const mp_obj_type_t adafruit_bus_device_spidevice_type; // Initializes the hardware peripheral. extern void common_hal_adafruit_bus_device_spidevice_construct(adafruit_bus_device_spidevice_obj_t *self, busio_spi_obj_t *spi, digitalio_digitalinout_obj_t *cs, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t extra_clocks); -extern void common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spidevice_obj_t *self); +extern mp_obj_t common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spidevice_obj_t *self); extern void common_hal_adafruit_bus_device_spidevice_exit(adafruit_bus_device_spidevice_obj_t *self); +extern mp_obj_t common_hal_adafruit_bus_device_spidevice_get_spi(adafruit_bus_device_spidevice_obj_t *self); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE_SPIDEVICE_H diff --git a/shared-module/adafruit_bus_device/SPIDevice.c b/shared-module/adafruit_bus_device/SPIDevice.c index e489fc7c07..5c1a69b128 100644 --- a/shared-module/adafruit_bus_device/SPIDevice.c +++ b/shared-module/adafruit_bus_device/SPIDevice.c @@ -41,7 +41,7 @@ void common_hal_adafruit_bus_device_spidevice_construct(adafruit_bus_device_spid self->chip_select = cs; } -void common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spidevice_obj_t *self) { +mp_obj_t common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spidevice_obj_t *self) { bool success = false; while (!success) { success = common_hal_busio_spi_try_lock(self->spi); @@ -54,6 +54,7 @@ void common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spidevic if (self->chip_select != MP_OBJ_NULL) { common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), false); } + return self->spi; } void common_hal_adafruit_bus_device_spidevice_exit(adafruit_bus_device_spidevice_obj_t *self) { @@ -83,3 +84,7 @@ void common_hal_adafruit_bus_device_spidevice_exit(adafruit_bus_device_spidevice common_hal_busio_spi_unlock(self->spi); } + +mp_obj_t common_hal_adafruit_bus_device_spidevice_get_spi(adafruit_bus_device_spidevice_obj_t *self) { + return self->spi; +}