Skip to content

Commit

Permalink
Merge pull request #9106 from wee-noise-makers/fourwire_optional_cs
Browse files Browse the repository at this point in the history
shared-module/fourwire/FourWire.c: make the chip_select pin optional
  • Loading branch information
dhalbert authored Apr 5, 2024
2 parents 5836f11 + 659c385 commit 8027efe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
6 changes: 3 additions & 3 deletions shared-bindings/fourwire/FourWire.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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} },
Expand All @@ -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);
Expand Down
37 changes: 25 additions & 12 deletions shared-module/fourwire/FourWire.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}

Expand Down

0 comments on commit 8027efe

Please sign in to comment.