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 #6109
This commit is contained in:
parent
6af4c7797c
commit
102ee716a7
|
@ -43,7 +43,7 @@
|
|||
//| """Manage updating a display over SPI four wire protocol in the background while Python code runs.
|
||||
//| It doesn't handle display initialization."""
|
||||
//|
|
||||
//| 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:
|
||||
//| 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:
|
||||
//| """Create a FourWire object associated with the given pins.
|
||||
//|
|
||||
//| The SPI bus and pins are then in use by the display until `displayio.release_displays()` is
|
||||
|
@ -51,8 +51,13 @@
|
|||
//| is done.) So, the first time you initialize a display bus in code.py you should call
|
||||
//| :py:func:`displayio.release_displays` first, otherwise it will error after the first code.py run.
|
||||
//|
|
||||
//| If the ``command`` pin is not specified, a 9-bit SPI mode will be simulated by adding a
|
||||
//| data/command bit to every bit being transmitted, and splitting the resulting data back
|
||||
//| into 8-bit bytes for transmission. The extra bits that this creates at the end are ignored
|
||||
//| by the receiving device.
|
||||
//|
|
||||
//| :param busio.SPI spi_bus: The SPI bus that make up the clock and data lines
|
||||
//| :param microcontroller.Pin command: Data or command pin
|
||||
//| :param microcontroller.Pin command: Data or command pin. When None, 9-bit SPI is simulated.
|
||||
//| :param microcontroller.Pin chip_select: Chip select pin
|
||||
//| :param microcontroller.Pin reset: Reset pin. When None only software reset can be used
|
||||
//| :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_
|
|||
enum { ARG_spi_bus, ARG_command, ARG_chip_select, ARG_reset, ARG_baudrate, ARG_polarity, ARG_phase };
|
||||
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 | MP_ARG_REQUIRED },
|
||||
{ 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_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} },
|
||||
|
@ -75,7 +80,7 @@ STATIC mp_obj_t displayio_fourwire_make_new(const mp_obj_type_t *type, size_t n_
|
|||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
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(args[ARG_command].u_obj);
|
||||
const mcu_pin_obj_t *command = validate_obj_is_free_pin_or_none(args[ARG_command].u_obj);
|
||||
const mcu_pin_obj_t *chip_select = validate_obj_is_free_pin(args[ARG_chip_select].u_obj);
|
||||
const mcu_pin_obj_t *reset = validate_obj_is_free_pin_or_none(args[ARG_reset].u_obj);
|
||||
|
||||
|
|
|
@ -51,11 +51,16 @@ void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t *self,
|
|||
self->polarity = polarity;
|
||||
self->phase = phase;
|
||||
|
||||
common_hal_digitalio_digitalinout_construct(&self->command, command);
|
||||
common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL);
|
||||
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) {
|
||||
self->command.base.type = &digitalio_digitalinout_type;
|
||||
common_hal_digitalio_digitalinout_construct(&self->command, command);
|
||||
common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL);
|
||||
common_hal_never_reset_pin(command);
|
||||
}
|
||||
self->reset.base.type = &mp_type_NoneType;
|
||||
if (reset != NULL) {
|
||||
self->reset.base.type = &digitalio_digitalinout_type;
|
||||
|
@ -65,7 +70,6 @@ void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t *self,
|
|||
common_hal_displayio_fourwire_reset(self);
|
||||
}
|
||||
|
||||
common_hal_never_reset_pin(command);
|
||||
common_hal_never_reset_pin(chip_select);
|
||||
}
|
||||
|
||||
|
@ -114,18 +118,57 @@ bool common_hal_displayio_fourwire_begin_transaction(mp_obj_t obj) {
|
|||
void common_hal_displayio_fourwire_send(mp_obj_t obj, display_byte_type_t data_type,
|
||||
display_chip_select_behavior_t chip_select, const uint8_t *data, uint32_t data_length) {
|
||||
displayio_fourwire_obj_t *self = MP_OBJ_TO_PTR(obj);
|
||||
common_hal_digitalio_digitalinout_set_value(&self->command, data_type == DISPLAY_DATA);
|
||||
if (chip_select == CHIP_SELECT_TOGGLE_EVERY_BYTE) {
|
||||
// Toggle chip select after each command byte in case the display driver
|
||||
// IC latches commands based on it.
|
||||
if (self->command.base.type == &mp_type_NoneType) {
|
||||
// When the data/command pin is not specified, we simulate a 9-bit SPI mode, by
|
||||
// adding a data/command bit to every byte, and then splitting the resulting data back
|
||||
// into 8-bit chunks for transmission. If the length of the data being transmitted
|
||||
// is not a multiple of 8, there will be additional bits at the end of the
|
||||
// transmission. We toggle the CS pin to make the receiver discard them.
|
||||
uint8_t buffer = 0;
|
||||
uint8_t bits = 0;
|
||||
uint8_t dc = (data_type == DISPLAY_DATA);
|
||||
|
||||
for (size_t i = 0; i < data_length; i++) {
|
||||
common_hal_busio_spi_write(self->bus, &data[i], 1);
|
||||
bits = (bits + 1) % 8;
|
||||
|
||||
if (bits == 0) {
|
||||
// send the previous byte and the dc bit
|
||||
// we will send the current byte later
|
||||
buffer = (buffer << 1) | dc;
|
||||
common_hal_busio_spi_write(self->bus, &buffer, 1);
|
||||
// send the current byte, because previous byte already filled all bits
|
||||
common_hal_busio_spi_write(self->bus, &data[i], 1);
|
||||
} else {
|
||||
// send remaining bits from previous byte, dc and beginning of current byte
|
||||
buffer = (buffer << (9 - bits)) | (dc << (8 - bits)) | (data[i] >> bits);
|
||||
common_hal_busio_spi_write(self->bus, &buffer, 1);
|
||||
}
|
||||
// save the current byte
|
||||
buffer = data[i];
|
||||
}
|
||||
// send any remaining bits
|
||||
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);
|
||||
}
|
||||
} else {
|
||||
common_hal_busio_spi_write(self->bus, data, data_length);
|
||||
common_hal_digitalio_digitalinout_set_value(&self->command, data_type == DISPLAY_DATA);
|
||||
if (chip_select == CHIP_SELECT_TOGGLE_EVERY_BYTE) {
|
||||
// Toggle chip select after each command byte in case the display driver
|
||||
// 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);
|
||||
}
|
||||
} else {
|
||||
common_hal_busio_spi_write(self->bus, data, data_length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue