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) ```
This commit is contained in:
parent
843fca1874
commit
4b41fdb586
@ -603,6 +603,7 @@ SRC_SHARED_MODULE_ALL = \
|
||||
displayio/TileGrid.c \
|
||||
displayio/area.c \
|
||||
displayio/__init__.c \
|
||||
dotclockframebuffer/__init__.c \
|
||||
floppyio/__init__.c \
|
||||
fontio/BuiltinFont.c \
|
||||
fontio/__init__.c \
|
||||
|
@ -34,11 +34,104 @@
|
||||
#include "shared-bindings/dotclockframebuffer/DotClockFramebuffer.h"
|
||||
|
||||
//| """Native helpers for driving parallel displays"""
|
||||
//|
|
||||
//| Length = typing.Literal[1, 2]
|
||||
//|
|
||||
//| def ioexpander_send_init_sequence(
|
||||
//| bus: busio.I2C,
|
||||
//| i2c_address: int,
|
||||
//| reg_addr: int,
|
||||
//| gpio_data_len: Length,
|
||||
//| gpio_address: int,
|
||||
//| gpio_data: int,
|
||||
//| cs_bit: int,
|
||||
//| mosi_bit: int,
|
||||
//| clk_bit: int,
|
||||
//| init_sequence: ReadableBuffer,
|
||||
//| ):
|
||||
//| """Send a displayio-style initialization sequence over an I2C I/O expander
|
||||
//|
|
||||
//| This function is highly generic in order to support various I/O expanders.
|
||||
//| What's assumed is that all the GPIO can be updated by writing to a single I2C register.
|
||||
//| Normal output polarity is assumed (CS and CLK are active low, data is not inverted).
|
||||
//| Only 8-bit I2C addresses are supported.
|
||||
//| 8- and 16-bit I2C addresses and data registers are supported.
|
||||
//| The Data/Command bit is sent as part of the serial data.
|
||||
//|
|
||||
//| Normally this function is used via a convenience library that is specific to the display & I/O expander in use.
|
||||
//|
|
||||
//| :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 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: 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 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
|
||||
//| """
|
||||
//|
|
||||
|
||||
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 };
|
||||
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ 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_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
MP_STATIC_ASSERT(MP_ARRAY_SIZE(allowed_args) == NUM_ARGS);
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
mp_obj_t bus_obj = mp_arg_validate_type(args[ARG_bus].u_obj, &busio_i2c_type, MP_QSTR_bus);
|
||||
mp_int_t i2c_address = args[ARG_i2c_address].u_int;
|
||||
mp_int_t gpio_address = args[ARG_gpio_address].u_int;
|
||||
mp_int_t gpio_data_len = args[ARG_gpio_data_len].u_int;
|
||||
mp_int_t gpio_data = args[ARG_gpio_data].u_int;
|
||||
mp_int_t cs_bit = args[ARG_cs_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_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(args[ARG_init_sequence].u_obj, &bufinfo, MP_BUFFER_READ);
|
||||
|
||||
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);
|
||||
|
||||
int max_bit = gpio_data_len * 8 - 1;
|
||||
mp_arg_validate_int_range(cs_bit, 0, max_bit, MP_QSTR_cs_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(gpio_data, 0, (1 << (max_bit * 8)) - 1, MP_QSTR_gpio_data);
|
||||
|
||||
dotclockframebuffer_ioexpander_spi_bus b = {
|
||||
.bus = bus_obj,
|
||||
.i2c_device_address = i2c_address,
|
||||
.i2c_write_size = 1 + gpio_data_len,
|
||||
// ASSERT(LITTLE_ENDIAN, "don't have to differentiate 8- vs 16-bits here")
|
||||
.addr_reg_shadow = { .u32 = gpio_address | (gpio_data << 8) },
|
||||
.cs_mask = 0x100 << cs_bit,
|
||||
.mosi_mask = 0x100 << mosi_bit,
|
||||
.clk_mask = 0x100 << clk_bit,
|
||||
};
|
||||
|
||||
dotclockframebuffer_ioexpander_send_init_sequence(&b, bufinfo.buf, bufinfo.len);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ioexpander_send_init_sequence_obj, 0, ioexpander_send_init_sequence);
|
||||
|
||||
STATIC const mp_rom_map_elem_t dotclockframebuffer_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_dotclockframebuffer) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_DotClockFramebuffer), MP_ROM_PTR(&dotclockframebuffer_framebuffer_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ioexpander_send_init_sequence), MP_ROM_PTR(&ioexpander_send_init_sequence_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(dotclockframebuffer_module_globals, dotclockframebuffer_module_globals_table);
|
||||
|
@ -25,3 +25,32 @@
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "shared-bindings/busio/I2C.h"
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
uint8_t addr, data;
|
||||
} a8d8;
|
||||
struct {
|
||||
uint16_t addr, data; // in little endian
|
||||
} a16d16;
|
||||
struct {
|
||||
uint8_t addr;
|
||||
uint16_t data; // in little endian
|
||||
} __attribute__((packed)) a8d16;
|
||||
uint32_t u32;
|
||||
} dotclockframebuffer_ioexpander_addr_reg_t;
|
||||
|
||||
typedef struct {
|
||||
busio_i2c_obj_t *bus;
|
||||
uint8_t i2c_device_address;
|
||||
uint8_t i2c_write_size;
|
||||
dotclockframebuffer_ioexpander_addr_reg_t addr_reg_shadow;
|
||||
uint32_t cs_mask;
|
||||
uint32_t mosi_mask;
|
||||
uint32_t clk_mask;
|
||||
} dotclockframebuffer_ioexpander_spi_bus;
|
||||
|
||||
void dotclockframebuffer_ioexpander_send_init_sequence(dotclockframebuffer_ioexpander_spi_bus *bus, uint8_t *init_sequence, uint16_t init_sequence_len);
|
||||
|
75
shared-module/dotclockframebuffer/__init__.c
Normal file
75
shared-module/dotclockframebuffer/__init__.c
Normal file
@ -0,0 +1,75 @@
|
||||
#include "shared-bindings/dotclockframebuffer/__init__.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define DELAY (0x80)
|
||||
|
||||
static void pin_change(dotclockframebuffer_ioexpander_spi_bus *bus, uint32_t set_bits, uint32_t clear_bits) {
|
||||
uint32_t data = (bus->addr_reg_shadow.u32 & ~clear_bits) | set_bits;
|
||||
// no way to signal failure to caller!
|
||||
(void)common_hal_busio_i2c_write(bus->bus, bus->i2c_device_address, (uint8_t *)&data, bus->i2c_write_size);
|
||||
bus->addr_reg_shadow.u32 = data;
|
||||
}
|
||||
|
||||
static void ioexpander_bus_send(dotclockframebuffer_ioexpander_spi_bus *bus,
|
||||
bool is_command,
|
||||
const uint8_t *data, uint32_t data_length) {
|
||||
|
||||
int dc_mask = is_command ? 0 : 0x100;
|
||||
for (uint32_t i = 0; i < data_length; i++) {
|
||||
int bits = data[i] | dc_mask;
|
||||
|
||||
for (uint32_t j = 0; j < 9; j++) {
|
||||
// CPOL=CPHA=0: output fresh data on falling edge of clk or cs
|
||||
if (bits & 0x100) {
|
||||
pin_change(bus, /* set */ bus->mosi_mask, /* clear */ bus->clk_mask | bus->cs_mask);
|
||||
} else {
|
||||
pin_change(bus, /* set */ 0, /* clear */ bus->mosi_mask | bus->clk_mask | bus->cs_mask);
|
||||
}
|
||||
// Display latches bit on rising edge of CLK
|
||||
pin_change(bus, /* set */ bus->clk_mask, /* clear */ 0);
|
||||
|
||||
// next bit
|
||||
bits <<= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Send a circuitpython-style display initialization sequence over an i2c-attached bus expander
|
||||
// This always assumes
|
||||
// * 9-bit SPI (no DC pin)
|
||||
// * CPOL=CPHA=0
|
||||
// * CS deasserted after each init sequence step, but not otherwise just like
|
||||
// displayio fourwire bus without data_as_commands
|
||||
void dotclockframebuffer_ioexpander_send_init_sequence(dotclockframebuffer_ioexpander_spi_bus *bus, uint8_t *init_sequence, uint16_t init_sequence_len) {
|
||||
while (!common_hal_busio_i2c_try_lock(bus->bus)) {
|
||||
RUN_BACKGROUND_TASKS;
|
||||
}
|
||||
for (uint32_t i = 0; i < init_sequence_len; /* NO INCREMENT */) {
|
||||
uint8_t *cmd = init_sequence + i;
|
||||
uint8_t data_size = *(cmd + 1);
|
||||
bool delay = (data_size & DELAY) != 0;
|
||||
data_size &= ~DELAY;
|
||||
uint8_t *data = cmd + 2;
|
||||
|
||||
ioexpander_bus_send(bus, true, cmd, 1);
|
||||
ioexpander_bus_send(bus, false, data, data_size);
|
||||
|
||||
// deassert CS
|
||||
pin_change(bus, /* set */ bus->cs_mask, /* clear */ 0);
|
||||
|
||||
uint16_t delay_length_ms = 10;
|
||||
if (delay) {
|
||||
data_size++;
|
||||
delay_length_ms = *(cmd + 1 + data_size);
|
||||
if (delay_length_ms == 255) {
|
||||
delay_length_ms = 500;
|
||||
}
|
||||
}
|
||||
mp_hal_delay_ms(delay_length_ms);
|
||||
i += 2 + data_size;
|
||||
}
|
||||
common_hal_busio_i2c_unlock(bus->bus);
|
||||
}
|
Loading…
Reference in New Issue
Block a user