Skip to content

Commit 4b41fdb

Browse files
committed
Fast(ish) special purpose bitbang spi over i2c
with the i2c bus operating at 400kHz this achieves a 4.8kHz SPI clock rate which could be worse. It accepts the same style of init sequence as displayio. tested by scoping the pins on the espressif lcd dev kit with a dummy init sequence: ```python dotclockframebuffer.ioexpander_send_init_sequence( bus=bus, i2c_address=expander_addr, gpio_address=1, gpio_data_len=1, gpio_data=0xff, cs_bit=1, mosi_bit=3, clk_bit=2, init_sequence=init_sequence) ```
1 parent 843fca1 commit 4b41fdb

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

py/circuitpy_defns.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ SRC_SHARED_MODULE_ALL = \
603603
displayio/TileGrid.c \
604604
displayio/area.c \
605605
displayio/__init__.c \
606+
dotclockframebuffer/__init__.c \
606607
floppyio/__init__.c \
607608
fontio/BuiltinFont.c \
608609
fontio/__init__.c \

shared-bindings/dotclockframebuffer/__init__.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,104 @@
3434
#include "shared-bindings/dotclockframebuffer/DotClockFramebuffer.h"
3535

3636
//| """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+
//|
3773

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);
38130

39131
STATIC const mp_rom_map_elem_t dotclockframebuffer_module_globals_table[] = {
40132
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_dotclockframebuffer) },
41133
{ 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) },
42135
};
43136

44137
STATIC MP_DEFINE_CONST_DICT(dotclockframebuffer_module_globals, dotclockframebuffer_module_globals_table);

shared-bindings/dotclockframebuffer/__init__.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,32 @@
2525
*/
2626

2727
#pragma once
28+
29+
#include "py/obj.h"
30+
#include "shared-bindings/busio/I2C.h"
31+
32+
typedef union {
33+
struct {
34+
uint8_t addr, data;
35+
} a8d8;
36+
struct {
37+
uint16_t addr, data; // in little endian
38+
} a16d16;
39+
struct {
40+
uint8_t addr;
41+
uint16_t data; // in little endian
42+
} __attribute__((packed)) a8d16;
43+
uint32_t u32;
44+
} dotclockframebuffer_ioexpander_addr_reg_t;
45+
46+
typedef struct {
47+
busio_i2c_obj_t *bus;
48+
uint8_t i2c_device_address;
49+
uint8_t i2c_write_size;
50+
dotclockframebuffer_ioexpander_addr_reg_t addr_reg_shadow;
51+
uint32_t cs_mask;
52+
uint32_t mosi_mask;
53+
uint32_t clk_mask;
54+
} dotclockframebuffer_ioexpander_spi_bus;
55+
56+
void dotclockframebuffer_ioexpander_send_init_sequence(dotclockframebuffer_ioexpander_spi_bus *bus, uint8_t *init_sequence, uint16_t init_sequence_len);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "shared-bindings/dotclockframebuffer/__init__.h"
2+
3+
#include <stdint.h>
4+
#include <stdbool.h>
5+
#include <stddef.h>
6+
7+
#define DELAY (0x80)
8+
9+
static void pin_change(dotclockframebuffer_ioexpander_spi_bus *bus, uint32_t set_bits, uint32_t clear_bits) {
10+
uint32_t data = (bus->addr_reg_shadow.u32 & ~clear_bits) | set_bits;
11+
// no way to signal failure to caller!
12+
(void)common_hal_busio_i2c_write(bus->bus, bus->i2c_device_address, (uint8_t *)&data, bus->i2c_write_size);
13+
bus->addr_reg_shadow.u32 = data;
14+
}
15+
16+
static void ioexpander_bus_send(dotclockframebuffer_ioexpander_spi_bus *bus,
17+
bool is_command,
18+
const uint8_t *data, uint32_t data_length) {
19+
20+
int dc_mask = is_command ? 0 : 0x100;
21+
for (uint32_t i = 0; i < data_length; i++) {
22+
int bits = data[i] | dc_mask;
23+
24+
for (uint32_t j = 0; j < 9; j++) {
25+
// CPOL=CPHA=0: output fresh data on falling edge of clk or cs
26+
if (bits & 0x100) {
27+
pin_change(bus, /* set */ bus->mosi_mask, /* clear */ bus->clk_mask | bus->cs_mask);
28+
} else {
29+
pin_change(bus, /* set */ 0, /* clear */ bus->mosi_mask | bus->clk_mask | bus->cs_mask);
30+
}
31+
// Display latches bit on rising edge of CLK
32+
pin_change(bus, /* set */ bus->clk_mask, /* clear */ 0);
33+
34+
// next bit
35+
bits <<= 1;
36+
}
37+
}
38+
}
39+
40+
// Send a circuitpython-style display initialization sequence over an i2c-attached bus expander
41+
// This always assumes
42+
// * 9-bit SPI (no DC pin)
43+
// * CPOL=CPHA=0
44+
// * CS deasserted after each init sequence step, but not otherwise just like
45+
// displayio fourwire bus without data_as_commands
46+
void dotclockframebuffer_ioexpander_send_init_sequence(dotclockframebuffer_ioexpander_spi_bus *bus, uint8_t *init_sequence, uint16_t init_sequence_len) {
47+
while (!common_hal_busio_i2c_try_lock(bus->bus)) {
48+
RUN_BACKGROUND_TASKS;
49+
}
50+
for (uint32_t i = 0; i < init_sequence_len; /* NO INCREMENT */) {
51+
uint8_t *cmd = init_sequence + i;
52+
uint8_t data_size = *(cmd + 1);
53+
bool delay = (data_size & DELAY) != 0;
54+
data_size &= ~DELAY;
55+
uint8_t *data = cmd + 2;
56+
57+
ioexpander_bus_send(bus, true, cmd, 1);
58+
ioexpander_bus_send(bus, false, data, data_size);
59+
60+
// deassert CS
61+
pin_change(bus, /* set */ bus->cs_mask, /* clear */ 0);
62+
63+
uint16_t delay_length_ms = 10;
64+
if (delay) {
65+
data_size++;
66+
delay_length_ms = *(cmd + 1 + data_size);
67+
if (delay_length_ms == 255) {
68+
delay_length_ms = 500;
69+
}
70+
}
71+
mp_hal_delay_ms(delay_length_ms);
72+
i += 2 + data_size;
73+
}
74+
common_hal_busio_i2c_unlock(bus->bus);
75+
}

0 commit comments

Comments
 (0)