From 659c38562b9549f2b6e90230fce20514e87ba7c1 Mon Sep 17 00:00:00 2001 From: Fabien Chouteau Date: Thu, 28 Mar 2024 09:13:16 +0100 Subject: [PATCH] shared-module/fourwire/FourWire.c: make the chip_select pin optional If there's only one device on the bus, the chip_select pin of the peripheral can be fixed in hardware, therefore lowering the number of pins required on the microcontroller side. This patch allows this by making the chip_select pin optional. --- shared-bindings/fourwire/FourWire.c | 6 ++--- shared-module/fourwire/FourWire.c | 37 +++++++++++++++++++---------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/shared-bindings/fourwire/FourWire.c b/shared-bindings/fourwire/FourWire.c index 86ce13d36e67..9986e1ac2206 100644 --- a/shared-bindings/fourwire/FourWire.c +++ b/shared-bindings/fourwire/FourWire.c @@ -47,7 +47,7 @@ //| spi_bus: busio.SPI, //| *, //| command: Optional[microcontroller.Pin], -//| chip_select: microcontroller.Pin, +//| chip_select: Optional[microcontroller.Pin], //| reset: Optional[microcontroller.Pin] = None, //| baudrate: int = 24000000, //| polarity: int = 0, @@ -79,7 +79,7 @@ STATIC mp_obj_t fourwire_fourwire_make_new(const mp_obj_type_t *type, size_t n_a static const mp_arg_t allowed_args[] = { { MP_QSTR_spi_bus, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_command, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} }, - { MP_QSTR_chip_select, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED }, + { MP_QSTR_chip_select, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} }, { MP_QSTR_reset, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} }, { MP_QSTR_baudrate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 24000000} }, { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, @@ -89,7 +89,7 @@ STATIC mp_obj_t fourwire_fourwire_make_new(const mp_obj_type_t *type, size_t n_a mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); const mcu_pin_obj_t *command = validate_obj_is_free_pin_or_none(args[ARG_command].u_obj, MP_QSTR_command); - const mcu_pin_obj_t *chip_select = validate_obj_is_free_pin(args[ARG_chip_select].u_obj, MP_QSTR_chip_select); + const mcu_pin_obj_t *chip_select = validate_obj_is_free_pin_or_none(args[ARG_chip_select].u_obj, MP_QSTR_chip_select); const mcu_pin_obj_t *reset = validate_obj_is_free_pin_or_none(args[ARG_reset].u_obj, MP_QSTR_reset); mp_obj_t spi = mp_arg_validate_type(args[ARG_spi_bus].u_obj, &busio_spi_type, MP_QSTR_spi_bus); diff --git a/shared-module/fourwire/FourWire.c b/shared-module/fourwire/FourWire.c index ea7a9bfe9d40..720238f0e6c7 100644 --- a/shared-module/fourwire/FourWire.c +++ b/shared-module/fourwire/FourWire.c @@ -47,8 +47,6 @@ void common_hal_fourwire_fourwire_construct(fourwire_fourwire_obj_t *self, self->polarity = polarity; self->phase = phase; - common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select); - common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL); self->command.base.type = &mp_type_NoneType; if (command != NULL) { @@ -66,7 +64,14 @@ void common_hal_fourwire_fourwire_construct(fourwire_fourwire_obj_t *self, common_hal_fourwire_fourwire_reset(self); } - common_hal_never_reset_pin(chip_select); + self->chip_select.base.type = &mp_type_NoneType; + if (chip_select != NULL) { + self->chip_select.base.type = &digitalio_digitalinout_type; + common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select); + common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL); + common_hal_never_reset_pin(chip_select); + } + } void common_hal_fourwire_fourwire_deinit(fourwire_fourwire_obj_t *self) { @@ -107,7 +112,9 @@ bool common_hal_fourwire_fourwire_begin_transaction(mp_obj_t obj) { } common_hal_busio_spi_configure(self->bus, self->frequency, self->polarity, self->phase, 8); - common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); + if (self->chip_select.base.type != &mp_type_NoneType) { + common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); + } return true; } @@ -146,10 +153,12 @@ void common_hal_fourwire_fourwire_send(mp_obj_t obj, display_byte_type_t data_ty if (bits > 0) { buffer = buffer << (8 - bits); common_hal_busio_spi_write(self->bus, &buffer, 1); - // toggle CS to discard superfluous bits - common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); - common_hal_mcu_delay_us(1); - common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); + if (self->chip_select.base.type != &mp_type_NoneType) { + // toggle CS to discard superfluous bits + common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); + common_hal_mcu_delay_us(1); + common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); + } } } else { common_hal_digitalio_digitalinout_set_value(&self->command, data_type == DISPLAY_DATA); @@ -158,9 +167,11 @@ void common_hal_fourwire_fourwire_send(mp_obj_t obj, display_byte_type_t data_ty // IC latches commands based on it. for (size_t i = 0; i < data_length; i++) { common_hal_busio_spi_write(self->bus, &data[i], 1); - common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); - common_hal_mcu_delay_us(1); - common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); + if (self->chip_select.base.type != &mp_type_NoneType) { + common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); + common_hal_mcu_delay_us(1); + common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); + } } } else { common_hal_busio_spi_write(self->bus, data, data_length); @@ -170,7 +181,9 @@ void common_hal_fourwire_fourwire_send(mp_obj_t obj, display_byte_type_t data_ty void common_hal_fourwire_fourwire_end_transaction(mp_obj_t obj) { fourwire_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj); - common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); + if (self->chip_select.base.type != &mp_type_NoneType) { + common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); + } common_hal_busio_spi_unlock(self->bus); }