Skip to content

Commit 40f1ac1

Browse files
authored
Merge pull request #7970 from dhalbert/adafruit_bus_device-no-cs
adafruit_bus_device SPIDevice can have None for chip select
2 parents fff72ea + fe386e7 commit 40f1ac1

File tree

2 files changed

+8
-7
lines changed
  • shared-bindings/adafruit_bus_device/spi_device
  • shared-module/adafruit_bus_device/spi_device

2 files changed

+8
-7
lines changed

shared-bindings/adafruit_bus_device/spi_device/SPIDevice.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
//| def __init__(
4545
//| self,
4646
//| spi: busio.SPI,
47-
//| chip_select: digitalio.DigitalInOut,
47+
//| chip_select: Optional[digitalio.DigitalInOut] = None,
4848
//| *,
4949
//| baudrate: int = 100000,
5050
//| polarity: int = 0,
@@ -55,7 +55,7 @@
5555
//| Represents a single SPI device and manages locking the bus and the device address.
5656
//|
5757
//| :param ~busio.SPI spi: The SPI bus the device is on
58-
//| :param ~digitalio.DigitalInOut chip_select: The chip select pin object that implements the DigitalInOut API.
58+
//| :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.
5959
//| :param bool cs_active_value: Set to true if your device requires CS to be active high. Defaults to false.
6060
//| :param int extra_clocks: The minimum number of clock cycles to cycle the bus after CS is high. (Used for SD cards.)
6161
//|
@@ -84,7 +84,7 @@ STATIC mp_obj_t adafruit_bus_device_spidevice_make_new(const mp_obj_type_t *type
8484
enum { ARG_spi, ARG_chip_select, ARG_cs_active_value, ARG_baudrate, ARG_polarity, ARG_phase, ARG_extra_clocks };
8585
static const mp_arg_t allowed_args[] = {
8686
{ MP_QSTR_spi, MP_ARG_REQUIRED | MP_ARG_OBJ },
87-
{ MP_QSTR_chip_select, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
87+
{ MP_QSTR_chip_select, MP_ARG_OBJ, {.u_obj = mp_const_none} },
8888
{ MP_QSTR_cs_active_value, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
8989
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
9090
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
@@ -96,12 +96,12 @@ STATIC mp_obj_t adafruit_bus_device_spidevice_make_new(const mp_obj_type_t *type
9696

9797
busio_spi_obj_t *spi = args[ARG_spi].u_obj;
9898

99-
mp_arg_validate_type(args[ARG_chip_select].u_obj, &digitalio_digitalinout_type, MP_QSTR_chip_select);
99+
mp_arg_validate_type_or_none(args[ARG_chip_select].u_obj, &digitalio_digitalinout_type, MP_QSTR_chip_select);
100100

101101
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,
102102
args[ARG_phase].u_int, args[ARG_extra_clocks].u_int);
103103

104-
if (args[ARG_chip_select].u_obj != MP_OBJ_NULL) {
104+
if (args[ARG_chip_select].u_obj != mp_const_none) {
105105
digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(MP_OBJ_TO_PTR(args[ARG_chip_select].u_obj),
106106
true, DRIVE_MODE_PUSH_PULL);
107107
#if CIRCUITPY_DIGITALIO_HAVE_INPUT_ONLY

shared-module/adafruit_bus_device/spi_device/SPIDevice.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void common_hal_adafruit_bus_device_spidevice_construct(adafruit_bus_device_spid
3838
self->polarity = polarity;
3939
self->phase = phase;
4040
self->extra_clocks = extra_clocks;
41+
// May be mp_const_none if CS not used.
4142
self->chip_select = cs;
4243
self->cs_active_value = cs_active_value;
4344
}
@@ -66,14 +67,14 @@ mp_obj_t common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spid
6667
mp_call_method_n_kw(0, 4, dest);
6768
}
6869

69-
if (self->chip_select != MP_OBJ_NULL) {
70+
if (self->chip_select != mp_const_none) {
7071
common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), self->cs_active_value);
7172
}
7273
return self->spi;
7374
}
7475

7576
void common_hal_adafruit_bus_device_spidevice_exit(adafruit_bus_device_spidevice_obj_t *self) {
76-
if (self->chip_select != MP_OBJ_NULL) {
77+
if (self->chip_select != mp_const_none) {
7778
common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), !(self->cs_active_value));
7879
}
7980

0 commit comments

Comments
 (0)