Re-work ioexpander_send_init_sequence

* can now send the I2C bus initialization code
 * can now reset the display on an I/O expander pin
 * parameters re-ordered to enable easy use with **board.TFT_IO_EXPANDER
This commit is contained in:
Jeff Epler 2023-09-25 11:31:16 -05:00
parent 409d1a83b6
commit 04ad525c09
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
3 changed files with 67 additions and 20 deletions

View File

@ -39,15 +39,17 @@
//| //|
//| def ioexpander_send_init_sequence( //| def ioexpander_send_init_sequence(
//| bus: busio.I2C, //| bus: busio.I2C,
//| init_sequence: ReadableBuffer,
//| *,
//| i2c_init_sequence: ReadableBuffer,
//| i2c_address: int, //| i2c_address: int,
//| reg_addr: int,
//| gpio_data_len: Length,
//| gpio_address: int, //| gpio_address: int,
//| gpio_data_len: Length,
//| gpio_data: int, //| gpio_data: int,
//| cs_bit: int, //| cs_bit: int,
//| mosi_bit: int, //| mosi_bit: int,
//| clk_bit: int, //| clk_bit: int,
//| init_sequence: ReadableBuffer, //| reset_bit: Optional[int],
//| ): //| ):
//| """Send a displayio-style initialization sequence over an I2C I/O expander //| """Send a displayio-style initialization sequence over an I2C I/O expander
//| //|
@ -60,30 +62,41 @@
//| //|
//| Normally this function is used via a convenience library that is specific to the display & I/O expander in use. //| Normally this function is used via a convenience library that is specific to the display & I/O expander in use.
//| //|
//| If the board has an integrated I/O expander, ``**board.TFT_IO_EXPANDER`` expands to the proper arguments starting with ``gpio_address``.
//| Note that this may include the ``i2c_init_sequence`` argument which can change the direction & value of I/O expander pins.
//| If this is undesirable, take a copy of ``TFT_IO_EXPANDER`` and change or remove the ``i2c_init_sequence`` key.
//|
//| If the board has an integrated display that requires an initialization sequence, ``board.TFT_INIT_SEQUENCE`` is the initialization string for the display.
//|
//| :param busio.I2C bus: The I2C bus where the I/O expander resides //| :param busio.I2C bus: The I2C bus where the I/O expander resides
//| :param busio.i2c_address: int: The I2C bus address of the I/O expander //| :param int busio.i2c_address: The I2C bus address of the I/O expander
//| :param ReadableBuffer init_sequence: The initialization sequence to send to the display
//| :param int gpio_address: The address portion of the I2C transaction (1 byte) //| :param int gpio_address: The address portion of the I2C transaction (1 byte)
//| :param int gpio_data_len: The size of the data portion of the I2C transaction, 1 or 2 bytes //| :param int gpio_data_len: The size of the data portion of the I2C transaction, 1 or 2 bytes
//| :param int gpio_data: The output value for all GPIO bits other than cs, mosi, and clk (needed because GPIO expanders may be unable to read back the current output value) //| :param int gpio_data: The output value for all GPIO bits other than cs, mosi, and clk (needed because GPIO expanders may be unable to read back the current output value)
//| :param int cs_bit: The bit number (from 0 to 7, or from 0 to 15) of the chip select bit in the GPIO register //| :param int cs_bit: The bit number (from 0 to 7, or from 0 to 15) of the chip select bit in the GPIO register
//| :param int mosi_value: The bit number (from 0 to 7, or from 0 to 15) of the data out bit in the GPIO register //| :param int mosi_value: The bit number (from 0 to 7, or from 0 to 15) of the data out bit in the GPIO register
//| :param int clk_value: The bit number (from 0 to 7, or from 0 to 15) of the clock out bit in the GPIO register //| :param int clk_value: The bit number (from 0 to 7, or from 0 to 15) of the clock out bit in the GPIO register
//| :param Optional[int] reset_value: The bit number (from 0 to 7, or from 0 to 15) of the display reset bit in the GPIO register
//| :param Optional[ReadableBuffer] i2c_init_sequence: An initialization sequence to send to the I2C expander
//| """ //| """
//| //|
STATIC mp_obj_t ioexpander_send_init_sequence(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC mp_obj_t ioexpander_send_init_sequence(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_bus, ARG_i2c_address, ARG_gpio_address, ARG_gpio_data_len, ARG_gpio_data, ARG_cs_bit, ARG_mosi_bit, ARG_clk_bit, ARG_init_sequence, NUM_ARGS }; enum { ARG_bus, ARG_init_sequence, ARG_i2c_address, ARG_gpio_address, ARG_gpio_data_len, ARG_gpio_data, ARG_cs_bit, ARG_mosi_bit, ARG_clk_bit, ARG_reset_bit, ARG_i2c_init_sequence, NUM_ARGS };
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
{ MP_QSTR_bus, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_bus, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_i2c_address, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_gpio_address, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_gpio_data_len, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_gpio_data, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_cs_bit, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_mosi_bit, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_clk_bit, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_init_sequence, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_init_sequence, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_i2c_address, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_gpio_address, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_gpio_data_len, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_gpio_data, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_cs_bit, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_mosi_bit, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_clk_bit, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_reset_bit, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = MP_ROM_NONE } },
{ MP_QSTR_i2c_init_sequence, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = MP_ROM_NONE } },
}; };
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@ -99,8 +112,8 @@ STATIC mp_obj_t ioexpander_send_init_sequence(size_t n_args, const mp_obj_t *pos
mp_int_t mosi_bit = args[ARG_mosi_bit].u_int; mp_int_t mosi_bit = args[ARG_mosi_bit].u_int;
mp_int_t clk_bit = args[ARG_clk_bit].u_int; mp_int_t clk_bit = args[ARG_clk_bit].u_int;
mp_buffer_info_t bufinfo; mp_buffer_info_t bufinfo_display_init_sequence;
mp_get_buffer_raise(args[ARG_init_sequence].u_obj, &bufinfo, MP_BUFFER_READ); mp_get_buffer_raise(args[ARG_init_sequence].u_obj, &bufinfo_display_init_sequence, MP_BUFFER_READ);
mp_arg_validate_int_range(i2c_address, 0, 127, MP_QSTR_i2c_address); mp_arg_validate_int_range(i2c_address, 0, 127, MP_QSTR_i2c_address);
mp_arg_validate_int_range(gpio_data_len, 1, 2, MP_QSTR_gpio_dat_len); mp_arg_validate_int_range(gpio_data_len, 1, 2, MP_QSTR_gpio_dat_len);
@ -110,6 +123,16 @@ STATIC mp_obj_t ioexpander_send_init_sequence(size_t n_args, const mp_obj_t *pos
mp_arg_validate_int_range(mosi_bit, 0, max_bit, MP_QSTR_mosi_bit); mp_arg_validate_int_range(mosi_bit, 0, max_bit, MP_QSTR_mosi_bit);
mp_arg_validate_int_range(clk_bit, 0, max_bit, MP_QSTR_clk_bit); mp_arg_validate_int_range(clk_bit, 0, max_bit, MP_QSTR_clk_bit);
mp_arg_validate_int_range(gpio_data, 0, (1 << (max_bit * 8)) - 1, MP_QSTR_gpio_data); mp_arg_validate_int_range(gpio_data, 0, (1 << (max_bit * 8)) - 1, MP_QSTR_gpio_data);
mp_int_t reset_mask = 0;
if (args[ARG_reset_bit].u_obj != MP_ROM_NONE) {
mp_int_t reset_bit = mp_arg_validate_int_range(mp_arg_validate_type_int(args[ARG_reset_bit].u_obj, MP_QSTR_reset_bit), 0, max_bit, MP_QSTR_reset_bit);
reset_mask = (1 << reset_bit);
}
mp_buffer_info_t bufinfo_i2c_init_sequence = {};
if (args[ARG_i2c_init_sequence].u_obj != mp_const_none) {
mp_get_buffer_raise(args[ARG_i2c_init_sequence].u_obj, &bufinfo_i2c_init_sequence, MP_BUFFER_READ);
}
dotclockframebuffer_ioexpander_spi_bus b = { dotclockframebuffer_ioexpander_spi_bus b = {
.bus = bus_obj, .bus = bus_obj,
@ -120,9 +143,10 @@ STATIC mp_obj_t ioexpander_send_init_sequence(size_t n_args, const mp_obj_t *pos
.cs_mask = 0x100 << cs_bit, .cs_mask = 0x100 << cs_bit,
.mosi_mask = 0x100 << mosi_bit, .mosi_mask = 0x100 << mosi_bit,
.clk_mask = 0x100 << clk_bit, .clk_mask = 0x100 << clk_bit,
.reset_mask = reset_mask,
}; };
dotclockframebuffer_ioexpander_send_init_sequence(&b, bufinfo.buf, bufinfo.len); dotclockframebuffer_ioexpander_send_init_sequence(&b, &bufinfo_i2c_init_sequence, &bufinfo_display_init_sequence);
return mp_const_none; return mp_const_none;
} }

View File

@ -51,6 +51,7 @@ typedef struct {
uint32_t cs_mask; uint32_t cs_mask;
uint32_t mosi_mask; uint32_t mosi_mask;
uint32_t clk_mask; uint32_t clk_mask;
uint32_t reset_mask;
} dotclockframebuffer_ioexpander_spi_bus; } dotclockframebuffer_ioexpander_spi_bus;
void dotclockframebuffer_ioexpander_send_init_sequence(dotclockframebuffer_ioexpander_spi_bus *bus, const uint8_t *init_sequence, uint16_t init_sequence_len); void dotclockframebuffer_ioexpander_send_init_sequence(dotclockframebuffer_ioexpander_spi_bus *bus, const mp_buffer_info_t *i2c_bus_init, const mp_buffer_info_t *display_init);

View File

@ -43,15 +43,37 @@ static void ioexpander_bus_send(dotclockframebuffer_ioexpander_spi_bus *bus,
// * CPOL=CPHA=0 // * CPOL=CPHA=0
// * CS deasserted after each init sequence step, but not otherwise just like // * CS deasserted after each init sequence step, but not otherwise just like
// displayio fourwire bus without data_as_commands // displayio fourwire bus without data_as_commands
void dotclockframebuffer_ioexpander_send_init_sequence(dotclockframebuffer_ioexpander_spi_bus *bus, const uint8_t *init_sequence, uint16_t init_sequence_len) { void dotclockframebuffer_ioexpander_send_init_sequence(dotclockframebuffer_ioexpander_spi_bus *bus, const mp_buffer_info_t *i2c_bus_init, const mp_buffer_info_t *display_init) {
while (!common_hal_busio_i2c_try_lock(bus->bus)) { while (!common_hal_busio_i2c_try_lock(bus->bus)) {
RUN_BACKGROUND_TASKS; RUN_BACKGROUND_TASKS;
} }
// ensure deasserted CS and idle CLK // send i2c init sequence
pin_change(bus, /* set */ bus->cs_mask, /* clear */ bus->clk_mask); {
size_t init_sequence_len = i2c_bus_init->len;
const uint8_t *init_sequence = i2c_bus_init->buf;
for (uint32_t i = 0; i < init_sequence_len; /* NO INCREMENT */) { for (size_t i = 0; i < init_sequence_len; /* NO INCREMENT */) {
uint8_t data_size = init_sequence[i];
const uint8_t *data_ptr = &init_sequence[i + 1];
(void)common_hal_busio_i2c_write(bus->bus, bus->i2c_device_address, data_ptr, data_size);
i = i + data_size + 1;
}
}
// ensure deasserted CS and idle CLK (and set other pins according to addr_reg_shadow); enter reset mode if applicable
pin_change(bus, /* set */ bus->cs_mask, /* clear */ bus->clk_mask | bus->reset_mask);
if (bus->reset_mask) {
mp_hal_delay_ms(10); // reset pulse length
pin_change(bus, /* set */ bus->reset_mask, /* clear */ 0);
mp_hal_delay_ms(100); // display start-up time
}
size_t init_sequence_len = display_init->len;
const uint8_t *init_sequence = display_init->buf;
for (size_t i = 0; i < init_sequence_len; /* NO INCREMENT */) {
const uint8_t *cmd = init_sequence + i; const uint8_t *cmd = init_sequence + i;
uint8_t data_size = *(cmd + 1); uint8_t data_size = *(cmd + 1);
bool delay = (data_size & DELAY) != 0; bool delay = (data_size & DELAY) != 0;