Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adafruit_bus_device SPIDevice can have None for chip select #7970

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions shared-bindings/adafruit_bus_device/spi_device/SPIDevice.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
//| def __init__(
//| self,
//| spi: busio.SPI,
//| chip_select: digitalio.DigitalInOut,
//| chip_select: Optional[digitalio.DigitalInOut] = None,
//| *,
//| baudrate: int = 100000,
//| polarity: int = 0,
Expand All @@ -55,7 +55,7 @@
//| Represents a single SPI device and manages locking the bus and the device address.
//|
//| :param ~busio.SPI spi: The SPI bus the device is on
//| :param ~digitalio.DigitalInOut chip_select: The chip select pin object that implements the DigitalInOut API.
//| :param ~digitalio.DigitalInOut chip_select: The chip select pin object that implements the DigitalInOut API. ``None`` if a chip select pin is not being used.
//| :param bool cs_active_value: Set to true if your device requires CS to be active high. Defaults to false.
//| :param int extra_clocks: The minimum number of clock cycles to cycle the bus after CS is high. (Used for SD cards.)
//|
Expand Down Expand Up @@ -84,7 +84,7 @@ STATIC mp_obj_t adafruit_bus_device_spidevice_make_new(const mp_obj_type_t *type
enum { ARG_spi, ARG_chip_select, ARG_cs_active_value, ARG_baudrate, ARG_polarity, ARG_phase, ARG_extra_clocks };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_spi, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_chip_select, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_chip_select, MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_cs_active_value, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
Expand All @@ -96,12 +96,12 @@ STATIC mp_obj_t adafruit_bus_device_spidevice_make_new(const mp_obj_type_t *type

busio_spi_obj_t *spi = args[ARG_spi].u_obj;

mp_arg_validate_type(args[ARG_chip_select].u_obj, &digitalio_digitalinout_type, MP_QSTR_chip_select);
mp_arg_validate_type_or_none(args[ARG_chip_select].u_obj, &digitalio_digitalinout_type, MP_QSTR_chip_select);

common_hal_adafruit_bus_device_spidevice_construct(MP_OBJ_TO_PTR(self), spi, args[ARG_chip_select].u_obj, args[ARG_cs_active_value].u_bool, args[ARG_baudrate].u_int, args[ARG_polarity].u_int,
args[ARG_phase].u_int, args[ARG_extra_clocks].u_int);

if (args[ARG_chip_select].u_obj != MP_OBJ_NULL) {
if (args[ARG_chip_select].u_obj != mp_const_none) {
digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(MP_OBJ_TO_PTR(args[ARG_chip_select].u_obj),
true, DRIVE_MODE_PUSH_PULL);
#if CIRCUITPY_DIGITALIO_HAVE_INPUT_ONLY
Expand Down
5 changes: 3 additions & 2 deletions shared-module/adafruit_bus_device/spi_device/SPIDevice.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void common_hal_adafruit_bus_device_spidevice_construct(adafruit_bus_device_spid
self->polarity = polarity;
self->phase = phase;
self->extra_clocks = extra_clocks;
// May be mp_const_none if CS not used.
self->chip_select = cs;
self->cs_active_value = cs_active_value;
}
Expand Down Expand Up @@ -66,14 +67,14 @@ mp_obj_t common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spid
mp_call_method_n_kw(0, 4, dest);
}

if (self->chip_select != MP_OBJ_NULL) {
if (self->chip_select != mp_const_none) {
common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), self->cs_active_value);
}
return self->spi;
}

void common_hal_adafruit_bus_device_spidevice_exit(adafruit_bus_device_spidevice_obj_t *self) {
if (self->chip_select != MP_OBJ_NULL) {
if (self->chip_select != mp_const_none) {
common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), !(self->cs_active_value));
}

Expand Down