Skip to content

Commit

Permalink
adding spi-3wire as C module
Browse files Browse the repository at this point in the history
  • Loading branch information
kdschlosser committed Oct 21, 2024
1 parent 4c62391 commit 2d84510
Show file tree
Hide file tree
Showing 8 changed files with 517 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ext_mod/lcd_bus/esp32_include/i80_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#if SOC_LCD_I80_SUPPORTED
// esp-idf includes
#include "esp_lcd_panel_io.h"
#include "spi_3wire.h"


typedef struct _mp_lcd_i80_bus_obj_t {
Expand All @@ -34,6 +35,9 @@
esp_lcd_panel_io_i80_config_t panel_io_config;
esp_lcd_i80_bus_config_t bus_config;
esp_lcd_i80_bus_handle_t bus_handle;

mp_spi_3wire_obj_t *spi_3wire;

} mp_lcd_i80_bus_obj_t;

extern const mp_obj_type_t mp_lcd_i80_bus_type;
Expand Down
5 changes: 4 additions & 1 deletion ext_mod/lcd_bus/esp32_include/rgb_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

//local_includes
#include "lcd_types.h"
#include "spi_3wire.h"

// esp-idf includes
#include "esp_lcd_panel_io.h"
Expand All @@ -15,7 +16,7 @@
#include "mphalport.h"
#include "py/obj.h"
#include "py/objarray.h"
#include "soc/soc_caps.h"


typedef struct _mp_lcd_rgb_bus_obj_t {
mp_obj_base_t base;
Expand All @@ -41,6 +42,8 @@

void *transmitting_buf;

mp_spi_3wire_obj_t *spi_3wire;

} mp_lcd_rgb_bus_obj_t;


Expand Down
54 changes: 54 additions & 0 deletions ext_mod/lcd_bus/esp32_include/spi_3wire.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//local_includes
#include "lcd_types.h"

// micropython includes
#include "mphalport.h"
#include "py/obj.h"
#include "py/objarray.h"

// esp-idf includes
#include "soc/soc_caps.h"

#ifndef _ESP32_SPI_3WIRE_H_
#define _ESP32_SPI_3WIRE_H_

#if SOC_LCD_I80_SUPPORTED || SOC_LCD_RGB_SUPPORTED

#define LCD_SPI_3WIRE_CMD_BITS_MAX (sizeof(uint32_t) * 8) // Maximum number of bytes for LCD command
#define LCD_SPI_3WIRE_PARAM_BITS_MAX (sizeof(uint32_t) * 8) // Maximum number of bytes for LCD parameter
#define LCD_SPI_3WIRE_CLK_MAX (500 * 1000UL)

#define LCD_SPI_3WIRE_DATA_DC_BIT_0 (0) // DC bit = 0
#define LCD_SPI_3WIRE_DATA_DC_BIT_1 (1) // DC bit = 1
#define LCD_SPI_3WIRE_DATA_NO_DC_BIT (2) // No DC bit
#define LCD_SPI_3WIRE_WRITE_ORDER_LSB_MASK (0x01) // Bit mask for LSB first write order
#define LCD_SPI_3WIRE_WRITE_ORDER_MSB_MASK (0x80) // Bit mask for MSB first write order


typedef struct _mp_spi_3wire_obj_t {
mp_obj_base_t base;
int cs;
int scl;
int sda;

uint32_t scl_half_period_us;
uint32_t cmd_bytes: 3;
uint32_t cmd_dc_bit: 2;
uint32_t param_bytes: 3;
uint32_t param_dc_bit: 2;
uint32_t write_order_mask: 8;
uint32_t cs_high_active: 1;
uint32_t sda_scl_idle_high: 1;
uint32_t scl_active_rising_edge: 1;
uint32_t del_keep_cs_inactive: 1;

} mp_spi_3wire_obj_t;

extern const mp_obj_type_t mp_spi_3wire_type;

mp_lcd_err_t mp_spi_3wire_init(mp_spi_3wire_obj_t *self, uint8_t cmd_bits, uint8_t param_bits);
esp_err_t mp_spi_3wire_tx_param(mp_spi_3wire_obj_t *self, int lcd_cmd, const void *param, size_t param_size);
void mp_spi_3wire_deinit(mp_spi_3wire_obj_t *self);

#endif /* SOC_LCD_I80_SUPPORTED */
#endif /* _ESP32_SPI_3WIRE_H_ */
43 changes: 42 additions & 1 deletion ext_mod/lcd_bus/esp32_src/i80_bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
#include "esp_lcd_panel_io.h"
#include "esp_heap_caps.h"
#include "hal/lcd_types.h"
#include "spi_3wire.h"


mp_lcd_err_t i80_del(mp_obj_t obj);
mp_lcd_err_t i80_init(mp_obj_t obj, uint16_t width, uint16_t height, uint8_t bpp, uint32_t buffer_size, bool rgb565_byte_swap, uint8_t cmd_bits, uint8_t param_bits);
mp_lcd_err_t i80_get_lane_count(mp_obj_t obj, uint8_t *lane_count);
mp_lcd_err_t i80_tx_param(mp_obj_t obj, int lcd_cmd, void *param, size_t param_size);

static mp_obj_t mp_lcd_i80_bus_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args)
{
Expand Down Expand Up @@ -57,6 +59,7 @@
ARG_reverse_color_bits,
ARG_pclk_active_low,
ARG_pclk_idle_low,
ARG_spi_3wire,
};

const mp_arg_t make_new_args[] = {
Expand Down Expand Up @@ -87,7 +90,8 @@
{ MP_QSTR_cs_active_high, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
{ MP_QSTR_reverse_color_bits, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
{ MP_QSTR_pclk_active_low, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
{ MP_QSTR_pclk_idle_low, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } }
{ MP_QSTR_pclk_idle_low, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
{ MP_QSTR_spi_3wire, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } },
};

mp_arg_val_t args[MP_ARRAY_SIZE(make_new_args)];
Expand Down Expand Up @@ -149,6 +153,12 @@
self->panel_io_config.flags.pclk_active_neg = (unsigned int)args[ARG_pclk_active_low].u_bool;
self->panel_io_config.flags.pclk_idle_low = (unsigned int)args[ARG_pclk_idle_low].u_bool;

if (args[ARG_spi_3wire].u_obj == mp_const_none) {
self->spi_3wire = NULL;
} else {
self->spi_3wire = (mp_spi_3wire_obj_t *)MP_OBJ_TO_PTR(args[ARG_spi_3wire].u_obj);
}

#if CONFIG_LCD_ENABLE_DEBUG_LOG
printf("dc_gpio_num=%d\n", self->bus_config.dc_gpio_num);
printf("wr_gpio_num=%d\n", self->bus_config.wr_gpio_num);
Expand Down Expand Up @@ -186,6 +196,7 @@
self->panel_io_handle.init = &i80_init;
self->panel_io_handle.del = &i80_del;
self->panel_io_handle.get_lane_count = &i80_get_lane_count;
self->panel_io_handle.tx_param = &i80_tx_param;

return MP_OBJ_FROM_PTR(self);
}
Expand Down Expand Up @@ -213,6 +224,11 @@
printf("lcd_param_bits=%d\n", self->panel_io_config.lcd_param_bits);
printf("max_transfer_bytes=%d\n", self->bus_config.max_transfer_bytes);
#endif

if (self->spi_3wire != NULL) {
mp_spi_3wire_init(self->spi_3wire, cmd_bits, param_bits);
}

esp_err_t ret = esp_lcd_new_i80_bus(&self->bus_config, &self->bus_handle);

if (ret != 0) {
Expand All @@ -228,6 +244,26 @@
return ret;
}

mp_lcd_err_t i80_tx_param(mp_obj_t obj, int lcd_cmd, void *param, size_t param_size)
{
#if CONFIG_LCD_ENABLE_DEBUG_LOG
printf("i80_tx_param(self, lcd_cmd=%d, param, param_size=%d)\n", lcd_cmd, param_size);
#endif

mp_lcd_i80_bus_obj_t *self = (mp_lcd_i80_bus_obj_t *)obj;

mp_lcd_err_t ret;

if (self->spi_3wire != NULL) {
mp_spi_3wire_tx_param(self->spi_3wire, lcd_cmd, param, param_size);
ret = LCD_OK
} else {
ret = esp_lcd_panel_io_tx_param(self->panel_io_handle.panel_io, lcd_cmd, param, param_size);
}

return ret;
}


mp_lcd_err_t i80_del(mp_obj_t obj)
{
Expand All @@ -237,6 +273,11 @@

mp_lcd_i80_bus_obj_t *self = (mp_lcd_i80_bus_obj_t *)obj;

if (self->spi_3wire != NULL) {
mp_spi_3wire_deinit(self->spi_3wire);
self->spi_3wire = NULL;
}

mp_lcd_err_t ret = esp_lcd_panel_io_del(self->panel_io_handle.panel_io);
if (ret != 0) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(esp_lcd_panel_io_del)"), ret);
Expand Down
32 changes: 27 additions & 5 deletions ext_mod/lcd_bus/esp32_src/rgb_bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "lcd_types.h"
#include "modlcd_bus.h"
#include "rgb_bus.h"
#include "spi_3wire.h"

// esp-idf includes
#include "hal/lcd_hal.h"
Expand Down Expand Up @@ -113,6 +114,7 @@
ARG_pclk_idle_high,
ARG_pclk_active_low,
ARG_refresh_on_demand,
ARG_spi_3wire,
};

const mp_arg_t allowed_args[] = {
Expand Down Expand Up @@ -149,6 +151,7 @@
{ MP_QSTR_pclk_idle_high, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
{ MP_QSTR_pclk_active_low, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
{ MP_QSTR_refresh_on_demand, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
{ MP_QSTR_spi_3wire, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } },
};

mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
Expand Down Expand Up @@ -210,6 +213,12 @@

self->panel_io_config.data_width = (size_t) i;

if (args[ARG_spi_3wire].u_obj == mp_const_none) {
self->spi_3wire = NULL;
} else {
self->spi_3wire = (mp_spi_3wire_obj_t *)MP_OBJ_TO_PTR(args[ARG_spi_3wire].u_obj);
}

#if CONFIG_LCD_ENABLE_DEBUG_LOG
printf("pclk_hz=%lu\n", self->bus_config.pclk_hz);
printf("hsync_pulse_width=%lu\n", self->bus_config.hsync_pulse_width);
Expand Down Expand Up @@ -272,6 +281,11 @@
printf("rgb_del(self)\n");
#endif

if (self->spi_3wire != NULL) {
mp_spi_3wire_deinit(self->spi_3wire);
self->spi_3wire = NULL;
}

mp_lcd_err_t ret = esp_lcd_panel_del(self->panel_handle);
return ret;
}
Expand All @@ -293,14 +307,22 @@

mp_lcd_err_t rgb_tx_param(mp_obj_t obj, int lcd_cmd, void *param, size_t param_size)
{
LCD_UNUSED(obj);
LCD_UNUSED(param);
mp_lcd_rgb_bus_obj_t *self = (mp_lcd_rgb_bus_obj_t *)obj;

if (self->spi_3wire != NULL) {
mp_spi_3wire_tx_param(self->spi_3wire, lcd_cmd, param, param_size);
} else {
LCD_UNUSED(param);

#if !CONFIG_LCD_ENABLE_DEBUG_LOG
LCD_UNUSED(lcd_cmd);
LCD_UNUSED(param_size);
#endif

}

#if CONFIG_LCD_ENABLE_DEBUG_LOG
printf("rgb_tx_param(self, lcd_cmd=%d, param, param_size=%d)\n", lcd_cmd, param_size);
#else
LCD_UNUSED(lcd_cmd);
LCD_UNUSED(param_size);
#endif

return LCD_OK;
Expand Down
Loading

0 comments on commit 2d84510

Please sign in to comment.