Merge pull request #4114 from tannewt/spidevice_spi

Add .spi accessor to SPIDevice
This commit is contained in:
Scott Shawcroft 2021-02-02 09:53:10 -08:00 committed by GitHub
commit a861498404
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 4 deletions

View File

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

View File

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

View File

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