Skip to content

Commit 8027efe

Browse files
authored
Merge pull request #9106 from wee-noise-makers/fourwire_optional_cs
shared-module/fourwire/FourWire.c: make the chip_select pin optional
2 parents 5836f11 + 659c385 commit 8027efe

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

shared-bindings/fourwire/FourWire.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
//| spi_bus: busio.SPI,
4848
//| *,
4949
//| command: Optional[microcontroller.Pin],
50-
//| chip_select: microcontroller.Pin,
50+
//| chip_select: Optional[microcontroller.Pin],
5151
//| reset: Optional[microcontroller.Pin] = None,
5252
//| baudrate: int = 24000000,
5353
//| 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
7979
static const mp_arg_t allowed_args[] = {
8080
{ MP_QSTR_spi_bus, MP_ARG_REQUIRED | MP_ARG_OBJ },
8181
{ MP_QSTR_command, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
82-
{ MP_QSTR_chip_select, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
82+
{ MP_QSTR_chip_select, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
8383
{ MP_QSTR_reset, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
8484
{ MP_QSTR_baudrate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 24000000} },
8585
{ 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
8989
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
9090

9191
const mcu_pin_obj_t *command = validate_obj_is_free_pin_or_none(args[ARG_command].u_obj, MP_QSTR_command);
92-
const mcu_pin_obj_t *chip_select = validate_obj_is_free_pin(args[ARG_chip_select].u_obj, MP_QSTR_chip_select);
92+
const mcu_pin_obj_t *chip_select = validate_obj_is_free_pin_or_none(args[ARG_chip_select].u_obj, MP_QSTR_chip_select);
9393
const mcu_pin_obj_t *reset = validate_obj_is_free_pin_or_none(args[ARG_reset].u_obj, MP_QSTR_reset);
9494

9595
mp_obj_t spi = mp_arg_validate_type(args[ARG_spi_bus].u_obj, &busio_spi_type, MP_QSTR_spi_bus);

shared-module/fourwire/FourWire.c

+25-12
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ void common_hal_fourwire_fourwire_construct(fourwire_fourwire_obj_t *self,
4747
self->polarity = polarity;
4848
self->phase = phase;
4949

50-
common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select);
51-
common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL);
5250

5351
self->command.base.type = &mp_type_NoneType;
5452
if (command != NULL) {
@@ -66,7 +64,14 @@ void common_hal_fourwire_fourwire_construct(fourwire_fourwire_obj_t *self,
6664
common_hal_fourwire_fourwire_reset(self);
6765
}
6866

69-
common_hal_never_reset_pin(chip_select);
67+
self->chip_select.base.type = &mp_type_NoneType;
68+
if (chip_select != NULL) {
69+
self->chip_select.base.type = &digitalio_digitalinout_type;
70+
common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select);
71+
common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL);
72+
common_hal_never_reset_pin(chip_select);
73+
}
74+
7075
}
7176

7277
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) {
107112
}
108113
common_hal_busio_spi_configure(self->bus, self->frequency, self->polarity,
109114
self->phase, 8);
110-
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
115+
if (self->chip_select.base.type != &mp_type_NoneType) {
116+
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
117+
}
111118
return true;
112119
}
113120

@@ -146,10 +153,12 @@ void common_hal_fourwire_fourwire_send(mp_obj_t obj, display_byte_type_t data_ty
146153
if (bits > 0) {
147154
buffer = buffer << (8 - bits);
148155
common_hal_busio_spi_write(self->bus, &buffer, 1);
149-
// toggle CS to discard superfluous bits
150-
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
151-
common_hal_mcu_delay_us(1);
152-
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
156+
if (self->chip_select.base.type != &mp_type_NoneType) {
157+
// toggle CS to discard superfluous bits
158+
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
159+
common_hal_mcu_delay_us(1);
160+
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
161+
}
153162
}
154163
} else {
155164
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
158167
// IC latches commands based on it.
159168
for (size_t i = 0; i < data_length; i++) {
160169
common_hal_busio_spi_write(self->bus, &data[i], 1);
161-
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
162-
common_hal_mcu_delay_us(1);
163-
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
170+
if (self->chip_select.base.type != &mp_type_NoneType) {
171+
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
172+
common_hal_mcu_delay_us(1);
173+
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
174+
}
164175
}
165176
} else {
166177
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
170181

171182
void common_hal_fourwire_fourwire_end_transaction(mp_obj_t obj) {
172183
fourwire_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj);
173-
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
184+
if (self->chip_select.base.type != &mp_type_NoneType) {
185+
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
186+
}
174187
common_hal_busio_spi_unlock(self->bus);
175188
}
176189

0 commit comments

Comments
 (0)