|
34 | 34 | #include "shared-bindings/dotclockframebuffer/DotClockFramebuffer.h" |
35 | 35 |
|
36 | 36 | //| """Native helpers for driving parallel displays""" |
| 37 | +//| |
| 38 | +//| Length = typing.Literal[1, 2] |
| 39 | +//| |
| 40 | +//| def ioexpander_send_init_sequence( |
| 41 | +//| bus: busio.I2C, |
| 42 | +//| i2c_address: int, |
| 43 | +//| reg_addr: int, |
| 44 | +//| gpio_data_len: Length, |
| 45 | +//| gpio_address: int, |
| 46 | +//| gpio_data: int, |
| 47 | +//| cs_bit: int, |
| 48 | +//| mosi_bit: int, |
| 49 | +//| clk_bit: int, |
| 50 | +//| init_sequence: ReadableBuffer, |
| 51 | +//| ): |
| 52 | +//| """Send a displayio-style initialization sequence over an I2C I/O expander |
| 53 | +//| |
| 54 | +//| This function is highly generic in order to support various I/O expanders. |
| 55 | +//| What's assumed is that all the GPIO can be updated by writing to a single I2C register. |
| 56 | +//| Normal output polarity is assumed (CS and CLK are active low, data is not inverted). |
| 57 | +//| Only 8-bit I2C addresses are supported. |
| 58 | +//| 8- and 16-bit I2C addresses and data registers are supported. |
| 59 | +//| The Data/Command bit is sent as part of the serial data. |
| 60 | +//| |
| 61 | +//| Normally this function is used via a convenience library that is specific to the display & I/O expander in use. |
| 62 | +//| |
| 63 | +//| :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 |
| 65 | +//| :param int gpio_address: The address portion of the I2C transaction (1 byte) |
| 66 | +//| :param int gpio_data_len: The size of the data portion of the I2C transaction, 1 or 2 bytes |
| 67 | +//| :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) |
| 68 | +//| :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 |
| 69 | +//| :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 |
| 70 | +//| :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 |
| 71 | +//| """ |
| 72 | +//| |
37 | 73 |
|
| 74 | +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 }; |
| 76 | + |
| 77 | + static const mp_arg_t allowed_args[] = { |
| 78 | + { 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 }, |
| 86 | + { MP_QSTR_init_sequence, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 87 | + }; |
| 88 | + |
| 89 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 90 | + MP_STATIC_ASSERT(MP_ARRAY_SIZE(allowed_args) == NUM_ARGS); |
| 91 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 92 | + |
| 93 | + mp_obj_t bus_obj = mp_arg_validate_type(args[ARG_bus].u_obj, &busio_i2c_type, MP_QSTR_bus); |
| 94 | + mp_int_t i2c_address = args[ARG_i2c_address].u_int; |
| 95 | + mp_int_t gpio_address = args[ARG_gpio_address].u_int; |
| 96 | + mp_int_t gpio_data_len = args[ARG_gpio_data_len].u_int; |
| 97 | + mp_int_t gpio_data = args[ARG_gpio_data].u_int; |
| 98 | + mp_int_t cs_bit = args[ARG_cs_bit].u_int; |
| 99 | + mp_int_t mosi_bit = args[ARG_mosi_bit].u_int; |
| 100 | + mp_int_t clk_bit = args[ARG_clk_bit].u_int; |
| 101 | + |
| 102 | + mp_buffer_info_t bufinfo; |
| 103 | + mp_get_buffer_raise(args[ARG_init_sequence].u_obj, &bufinfo, MP_BUFFER_READ); |
| 104 | + |
| 105 | + mp_arg_validate_int_range(i2c_address, 0, 127, MP_QSTR_i2c_address); |
| 106 | + mp_arg_validate_int_range(gpio_data_len, 1, 2, MP_QSTR_gpio_dat_len); |
| 107 | + |
| 108 | + int max_bit = gpio_data_len * 8 - 1; |
| 109 | + mp_arg_validate_int_range(cs_bit, 0, max_bit, MP_QSTR_cs_bit); |
| 110 | + mp_arg_validate_int_range(mosi_bit, 0, max_bit, MP_QSTR_mosi_bit); |
| 111 | + mp_arg_validate_int_range(clk_bit, 0, max_bit, MP_QSTR_clk_bit); |
| 112 | + mp_arg_validate_int_range(gpio_data, 0, (1 << (max_bit * 8)) - 1, MP_QSTR_gpio_data); |
| 113 | + |
| 114 | + dotclockframebuffer_ioexpander_spi_bus b = { |
| 115 | + .bus = bus_obj, |
| 116 | + .i2c_device_address = i2c_address, |
| 117 | + .i2c_write_size = 1 + gpio_data_len, |
| 118 | + // ASSERT(LITTLE_ENDIAN, "don't have to differentiate 8- vs 16-bits here") |
| 119 | + .addr_reg_shadow = { .u32 = gpio_address | (gpio_data << 8) }, |
| 120 | + .cs_mask = 0x100 << cs_bit, |
| 121 | + .mosi_mask = 0x100 << mosi_bit, |
| 122 | + .clk_mask = 0x100 << clk_bit, |
| 123 | + }; |
| 124 | + |
| 125 | + dotclockframebuffer_ioexpander_send_init_sequence(&b, bufinfo.buf, bufinfo.len); |
| 126 | + return mp_const_none; |
| 127 | +} |
| 128 | + |
| 129 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ioexpander_send_init_sequence_obj, 0, ioexpander_send_init_sequence); |
38 | 130 |
|
39 | 131 | STATIC const mp_rom_map_elem_t dotclockframebuffer_module_globals_table[] = { |
40 | 132 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_dotclockframebuffer) }, |
41 | 133 | { MP_ROM_QSTR(MP_QSTR_DotClockFramebuffer), MP_ROM_PTR(&dotclockframebuffer_framebuffer_type) }, |
| 134 | + { MP_ROM_QSTR(MP_QSTR_ioexpander_send_init_sequence), MP_ROM_PTR(&ioexpander_send_init_sequence_obj) }, |
42 | 135 | }; |
43 | 136 |
|
44 | 137 | STATIC MP_DEFINE_CONST_DICT(dotclockframebuffer_module_globals, dotclockframebuffer_module_globals_table); |
|
0 commit comments