Skip to content

Commit 04ad525

Browse files
committed
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
1 parent 409d1a8 commit 04ad525

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

shared-bindings/dotclockframebuffer/__init__.c

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@
3939
//|
4040
//| def ioexpander_send_init_sequence(
4141
//| bus: busio.I2C,
42+
//| init_sequence: ReadableBuffer,
43+
//| *,
44+
//| i2c_init_sequence: ReadableBuffer,
4245
//| i2c_address: int,
43-
//| reg_addr: int,
44-
//| gpio_data_len: Length,
4546
//| gpio_address: int,
47+
//| gpio_data_len: Length,
4648
//| gpio_data: int,
4749
//| cs_bit: int,
4850
//| mosi_bit: int,
4951
//| clk_bit: int,
50-
//| init_sequence: ReadableBuffer,
52+
//| reset_bit: Optional[int],
5153
//| ):
5254
//| """Send a displayio-style initialization sequence over an I2C I/O expander
5355
//|
@@ -60,30 +62,41 @@
6062
//|
6163
//| Normally this function is used via a convenience library that is specific to the display & I/O expander in use.
6264
//|
65+
//| If the board has an integrated I/O expander, ``**board.TFT_IO_EXPANDER`` expands to the proper arguments starting with ``gpio_address``.
66+
//| Note that this may include the ``i2c_init_sequence`` argument which can change the direction & value of I/O expander pins.
67+
//| If this is undesirable, take a copy of ``TFT_IO_EXPANDER`` and change or remove the ``i2c_init_sequence`` key.
68+
//|
69+
//| If the board has an integrated display that requires an initialization sequence, ``board.TFT_INIT_SEQUENCE`` is the initialization string for the display.
70+
//|
6371
//| :param busio.I2C bus: The I2C bus where the I/O expander resides
64-
//| :param busio.i2c_address: int: The I2C bus address of the I/O expander
72+
//| :param int busio.i2c_address: The I2C bus address of the I/O expander
73+
//| :param ReadableBuffer init_sequence: The initialization sequence to send to the display
6574
//| :param int gpio_address: The address portion of the I2C transaction (1 byte)
6675
//| :param int gpio_data_len: The size of the data portion of the I2C transaction, 1 or 2 bytes
6776
//| :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)
6877
//| :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
6978
//| :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
7079
//| :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
80+
//| :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
81+
//| :param Optional[ReadableBuffer] i2c_init_sequence: An initialization sequence to send to the I2C expander
7182
//| """
7283
//|
7384

7485
STATIC mp_obj_t ioexpander_send_init_sequence(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
75-
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 };
86+
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 };
7687

7788
static const mp_arg_t allowed_args[] = {
7889
{ MP_QSTR_bus, MP_ARG_REQUIRED | MP_ARG_OBJ },
79-
{ MP_QSTR_i2c_address, MP_ARG_REQUIRED | MP_ARG_INT },
80-
{ MP_QSTR_gpio_address, MP_ARG_REQUIRED | MP_ARG_INT },
81-
{ MP_QSTR_gpio_data_len, MP_ARG_REQUIRED | MP_ARG_INT },
82-
{ MP_QSTR_gpio_data, MP_ARG_REQUIRED | MP_ARG_INT },
83-
{ MP_QSTR_cs_bit, MP_ARG_REQUIRED | MP_ARG_INT },
84-
{ MP_QSTR_mosi_bit, MP_ARG_REQUIRED | MP_ARG_INT },
85-
{ MP_QSTR_clk_bit, MP_ARG_REQUIRED | MP_ARG_INT },
8690
{ MP_QSTR_init_sequence, MP_ARG_REQUIRED | MP_ARG_OBJ },
91+
{ MP_QSTR_i2c_address, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
92+
{ MP_QSTR_gpio_address, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
93+
{ MP_QSTR_gpio_data_len, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
94+
{ MP_QSTR_gpio_data, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
95+
{ MP_QSTR_cs_bit, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
96+
{ MP_QSTR_mosi_bit, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
97+
{ MP_QSTR_clk_bit, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
98+
{ MP_QSTR_reset_bit, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = MP_ROM_NONE } },
99+
{ MP_QSTR_i2c_init_sequence, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = MP_ROM_NONE } },
87100
};
88101

89102
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
99112
mp_int_t mosi_bit = args[ARG_mosi_bit].u_int;
100113
mp_int_t clk_bit = args[ARG_clk_bit].u_int;
101114

102-
mp_buffer_info_t bufinfo;
103-
mp_get_buffer_raise(args[ARG_init_sequence].u_obj, &bufinfo, MP_BUFFER_READ);
115+
mp_buffer_info_t bufinfo_display_init_sequence;
116+
mp_get_buffer_raise(args[ARG_init_sequence].u_obj, &bufinfo_display_init_sequence, MP_BUFFER_READ);
104117

105118
mp_arg_validate_int_range(i2c_address, 0, 127, MP_QSTR_i2c_address);
106119
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
110123
mp_arg_validate_int_range(mosi_bit, 0, max_bit, MP_QSTR_mosi_bit);
111124
mp_arg_validate_int_range(clk_bit, 0, max_bit, MP_QSTR_clk_bit);
112125
mp_arg_validate_int_range(gpio_data, 0, (1 << (max_bit * 8)) - 1, MP_QSTR_gpio_data);
126+
mp_int_t reset_mask = 0;
127+
if (args[ARG_reset_bit].u_obj != MP_ROM_NONE) {
128+
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);
129+
reset_mask = (1 << reset_bit);
130+
}
131+
132+
mp_buffer_info_t bufinfo_i2c_init_sequence = {};
133+
if (args[ARG_i2c_init_sequence].u_obj != mp_const_none) {
134+
mp_get_buffer_raise(args[ARG_i2c_init_sequence].u_obj, &bufinfo_i2c_init_sequence, MP_BUFFER_READ);
135+
}
113136

114137
dotclockframebuffer_ioexpander_spi_bus b = {
115138
.bus = bus_obj,
@@ -120,9 +143,10 @@ STATIC mp_obj_t ioexpander_send_init_sequence(size_t n_args, const mp_obj_t *pos
120143
.cs_mask = 0x100 << cs_bit,
121144
.mosi_mask = 0x100 << mosi_bit,
122145
.clk_mask = 0x100 << clk_bit,
146+
.reset_mask = reset_mask,
123147
};
124148

125-
dotclockframebuffer_ioexpander_send_init_sequence(&b, bufinfo.buf, bufinfo.len);
149+
dotclockframebuffer_ioexpander_send_init_sequence(&b, &bufinfo_i2c_init_sequence, &bufinfo_display_init_sequence);
126150
return mp_const_none;
127151
}
128152

shared-bindings/dotclockframebuffer/__init__.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ typedef struct {
5151
uint32_t cs_mask;
5252
uint32_t mosi_mask;
5353
uint32_t clk_mask;
54+
uint32_t reset_mask;
5455
} dotclockframebuffer_ioexpander_spi_bus;
5556

56-
void dotclockframebuffer_ioexpander_send_init_sequence(dotclockframebuffer_ioexpander_spi_bus *bus, const uint8_t *init_sequence, uint16_t init_sequence_len);
57+
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);

shared-module/dotclockframebuffer/__init__.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,37 @@ static void ioexpander_bus_send(dotclockframebuffer_ioexpander_spi_bus *bus,
4343
// * CPOL=CPHA=0
4444
// * CS deasserted after each init sequence step, but not otherwise just like
4545
// displayio fourwire bus without data_as_commands
46-
void dotclockframebuffer_ioexpander_send_init_sequence(dotclockframebuffer_ioexpander_spi_bus *bus, const uint8_t *init_sequence, uint16_t init_sequence_len) {
46+
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) {
4747
while (!common_hal_busio_i2c_try_lock(bus->bus)) {
4848
RUN_BACKGROUND_TASKS;
4949
}
5050

51-
// ensure deasserted CS and idle CLK
52-
pin_change(bus, /* set */ bus->cs_mask, /* clear */ bus->clk_mask);
51+
// send i2c init sequence
52+
{
53+
size_t init_sequence_len = i2c_bus_init->len;
54+
const uint8_t *init_sequence = i2c_bus_init->buf;
5355

54-
for (uint32_t i = 0; i < init_sequence_len; /* NO INCREMENT */) {
56+
for (size_t i = 0; i < init_sequence_len; /* NO INCREMENT */) {
57+
uint8_t data_size = init_sequence[i];
58+
const uint8_t *data_ptr = &init_sequence[i + 1];
59+
(void)common_hal_busio_i2c_write(bus->bus, bus->i2c_device_address, data_ptr, data_size);
60+
i = i + data_size + 1;
61+
}
62+
}
63+
64+
// ensure deasserted CS and idle CLK (and set other pins according to addr_reg_shadow); enter reset mode if applicable
65+
pin_change(bus, /* set */ bus->cs_mask, /* clear */ bus->clk_mask | bus->reset_mask);
66+
67+
if (bus->reset_mask) {
68+
mp_hal_delay_ms(10); // reset pulse length
69+
pin_change(bus, /* set */ bus->reset_mask, /* clear */ 0);
70+
mp_hal_delay_ms(100); // display start-up time
71+
}
72+
73+
size_t init_sequence_len = display_init->len;
74+
const uint8_t *init_sequence = display_init->buf;
75+
76+
for (size_t i = 0; i < init_sequence_len; /* NO INCREMENT */) {
5577
const uint8_t *cmd = init_sequence + i;
5678
uint8_t data_size = *(cmd + 1);
5779
bool delay = (data_size & DELAY) != 0;

0 commit comments

Comments
 (0)