Skip to content

Commit 102ee71

Browse files
committed
Add support for 9-bit mode to displayio.FourWire
If the ``command`` pin is None, that information will instead be sent as a ninth bit in the SPI transactions. Fix micropython#6109
1 parent 6af4c77 commit 102ee71

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

shared-bindings/displayio/FourWire.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,21 @@
4343
//| """Manage updating a display over SPI four wire protocol in the background while Python code runs.
4444
//| It doesn't handle display initialization."""
4545
//|
46-
//| def __init__(self, spi_bus: busio.SPI, *, command: microcontroller.Pin, chip_select: microcontroller.Pin, reset: Optional[microcontroller.Pin] = None, baudrate: int = 24000000, polarity: int = 0, phase: int = 0) -> None:
46+
//| def __init__(self, spi_bus: busio.SPI, *, command: Optional[microcontroller.Pin], chip_select: microcontroller.Pin, reset: Optional[microcontroller.Pin] = None, baudrate: int = 24000000, polarity: int = 0, phase: int = 0) -> None:
4747
//| """Create a FourWire object associated with the given pins.
4848
//|
4949
//| The SPI bus and pins are then in use by the display until `displayio.release_displays()` is
5050
//| called even after a reload. (It does this so CircuitPython can use the display after your code
5151
//| is done.) So, the first time you initialize a display bus in code.py you should call
5252
//| :py:func:`displayio.release_displays` first, otherwise it will error after the first code.py run.
5353
//|
54+
//| If the ``command`` pin is not specified, a 9-bit SPI mode will be simulated by adding a
55+
//| data/command bit to every bit being transmitted, and splitting the resulting data back
56+
//| into 8-bit bytes for transmission. The extra bits that this creates at the end are ignored
57+
//| by the receiving device.
58+
//|
5459
//| :param busio.SPI spi_bus: The SPI bus that make up the clock and data lines
55-
//| :param microcontroller.Pin command: Data or command pin
60+
//| :param microcontroller.Pin command: Data or command pin. When None, 9-bit SPI is simulated.
5661
//| :param microcontroller.Pin chip_select: Chip select pin
5762
//| :param microcontroller.Pin reset: Reset pin. When None only software reset can be used
5863
//| :param int baudrate: Maximum baudrate in Hz for the display on the bus
@@ -65,7 +70,7 @@ STATIC mp_obj_t displayio_fourwire_make_new(const mp_obj_type_t *type, size_t n_
6570
enum { ARG_spi_bus, ARG_command, ARG_chip_select, ARG_reset, ARG_baudrate, ARG_polarity, ARG_phase };
6671
static const mp_arg_t allowed_args[] = {
6772
{ MP_QSTR_spi_bus, MP_ARG_REQUIRED | MP_ARG_OBJ },
68-
{ MP_QSTR_command, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
73+
{ MP_QSTR_command, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
6974
{ MP_QSTR_chip_select, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
7075
{ MP_QSTR_reset, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
7176
{ MP_QSTR_baudrate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 24000000} },
@@ -75,7 +80,7 @@ STATIC mp_obj_t displayio_fourwire_make_new(const mp_obj_type_t *type, size_t n_
7580
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
7681
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
7782

78-
const mcu_pin_obj_t *command = validate_obj_is_free_pin(args[ARG_command].u_obj);
83+
const mcu_pin_obj_t *command = validate_obj_is_free_pin_or_none(args[ARG_command].u_obj);
7984
const mcu_pin_obj_t *chip_select = validate_obj_is_free_pin(args[ARG_chip_select].u_obj);
8085
const mcu_pin_obj_t *reset = validate_obj_is_free_pin_or_none(args[ARG_reset].u_obj);
8186

shared-module/displayio/FourWire.c

+52-9
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@ void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t *self,
5151
self->polarity = polarity;
5252
self->phase = phase;
5353

54-
common_hal_digitalio_digitalinout_construct(&self->command, command);
55-
common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL);
5654
common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select);
5755
common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL);
5856

57+
self->command.base.type = &mp_type_NoneType;
58+
if (command != NULL) {
59+
self->command.base.type = &digitalio_digitalinout_type;
60+
common_hal_digitalio_digitalinout_construct(&self->command, command);
61+
common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL);
62+
common_hal_never_reset_pin(command);
63+
}
5964
self->reset.base.type = &mp_type_NoneType;
6065
if (reset != NULL) {
6166
self->reset.base.type = &digitalio_digitalinout_type;
@@ -65,7 +70,6 @@ void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t *self,
6570
common_hal_displayio_fourwire_reset(self);
6671
}
6772

68-
common_hal_never_reset_pin(command);
6973
common_hal_never_reset_pin(chip_select);
7074
}
7175

@@ -114,18 +118,57 @@ bool common_hal_displayio_fourwire_begin_transaction(mp_obj_t obj) {
114118
void common_hal_displayio_fourwire_send(mp_obj_t obj, display_byte_type_t data_type,
115119
display_chip_select_behavior_t chip_select, const uint8_t *data, uint32_t data_length) {
116120
displayio_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj);
117-
common_hal_digitalio_digitalinout_set_value(&self->command, data_type == DISPLAY_DATA);
118-
if (chip_select == CHIP_SELECT_TOGGLE_EVERY_BYTE) {
119-
// Toggle chip select after each command byte in case the display driver
120-
// IC latches commands based on it.
121+
if (self->command.base.type == &mp_type_NoneType) {
122+
// When the data/command pin is not specified, we simulate a 9-bit SPI mode, by
123+
// adding a data/command bit to every byte, and then splitting the resulting data back
124+
// into 8-bit chunks for transmission. If the length of the data being transmitted
125+
// is not a multiple of 8, there will be additional bits at the end of the
126+
// transmission. We toggle the CS pin to make the receiver discard them.
127+
uint8_t buffer = 0;
128+
uint8_t bits = 0;
129+
uint8_t dc = (data_type == DISPLAY_DATA);
130+
121131
for (size_t i = 0; i < data_length; i++) {
122-
common_hal_busio_spi_write(self->bus, &data[i], 1);
132+
bits = (bits + 1) % 8;
133+
134+
if (bits == 0) {
135+
// send the previous byte and the dc bit
136+
// we will send the current byte later
137+
buffer = (buffer << 1) | dc;
138+
common_hal_busio_spi_write(self->bus, &buffer, 1);
139+
// send the current byte, because previous byte already filled all bits
140+
common_hal_busio_spi_write(self->bus, &data[i], 1);
141+
} else {
142+
// send remaining bits from previous byte, dc and beginning of current byte
143+
buffer = (buffer << (9 - bits)) | (dc << (8 - bits)) | (data[i] >> bits);
144+
common_hal_busio_spi_write(self->bus, &buffer, 1);
145+
}
146+
// save the current byte
147+
buffer = data[i];
148+
}
149+
// send any remaining bits
150+
if (bits > 0) {
151+
buffer = buffer << (8 - bits);
152+
common_hal_busio_spi_write(self->bus, &buffer, 1);
153+
// toggle CS to discard superfluous bits
123154
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
124155
common_hal_mcu_delay_us(1);
125156
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
126157
}
127158
} else {
128-
common_hal_busio_spi_write(self->bus, data, data_length);
159+
common_hal_digitalio_digitalinout_set_value(&self->command, data_type == DISPLAY_DATA);
160+
if (chip_select == CHIP_SELECT_TOGGLE_EVERY_BYTE) {
161+
// Toggle chip select after each command byte in case the display driver
162+
// IC latches commands based on it.
163+
for (size_t i = 0; i < data_length; i++) {
164+
common_hal_busio_spi_write(self->bus, &data[i], 1);
165+
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
166+
common_hal_mcu_delay_us(1);
167+
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
168+
}
169+
} else {
170+
common_hal_busio_spi_write(self->bus, data, data_length);
171+
}
129172
}
130173
}
131174

0 commit comments

Comments
 (0)