Skip to content

Commit 2d84510

Browse files
committed
adding spi-3wire as C module
1 parent 4c62391 commit 2d84510

File tree

8 files changed

+517
-7
lines changed

8 files changed

+517
-7
lines changed

ext_mod/lcd_bus/esp32_include/i80_bus.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#if SOC_LCD_I80_SUPPORTED
1616
// esp-idf includes
1717
#include "esp_lcd_panel_io.h"
18+
#include "spi_3wire.h"
1819

1920

2021
typedef struct _mp_lcd_i80_bus_obj_t {
@@ -34,6 +35,9 @@
3435
esp_lcd_panel_io_i80_config_t panel_io_config;
3536
esp_lcd_i80_bus_config_t bus_config;
3637
esp_lcd_i80_bus_handle_t bus_handle;
38+
39+
mp_spi_3wire_obj_t *spi_3wire;
40+
3741
} mp_lcd_i80_bus_obj_t;
3842

3943
extern const mp_obj_type_t mp_lcd_i80_bus_type;

ext_mod/lcd_bus/esp32_include/rgb_bus.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
//local_includes
88
#include "lcd_types.h"
9+
#include "spi_3wire.h"
910

1011
// esp-idf includes
1112
#include "esp_lcd_panel_io.h"
@@ -15,7 +16,7 @@
1516
#include "mphalport.h"
1617
#include "py/obj.h"
1718
#include "py/objarray.h"
18-
#include "soc/soc_caps.h"
19+
1920

2021
typedef struct _mp_lcd_rgb_bus_obj_t {
2122
mp_obj_base_t base;
@@ -41,6 +42,8 @@
4142

4243
void *transmitting_buf;
4344

45+
mp_spi_3wire_obj_t *spi_3wire;
46+
4447
} mp_lcd_rgb_bus_obj_t;
4548

4649

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//local_includes
2+
#include "lcd_types.h"
3+
4+
// micropython includes
5+
#include "mphalport.h"
6+
#include "py/obj.h"
7+
#include "py/objarray.h"
8+
9+
// esp-idf includes
10+
#include "soc/soc_caps.h"
11+
12+
#ifndef _ESP32_SPI_3WIRE_H_
13+
#define _ESP32_SPI_3WIRE_H_
14+
15+
#if SOC_LCD_I80_SUPPORTED || SOC_LCD_RGB_SUPPORTED
16+
17+
#define LCD_SPI_3WIRE_CMD_BITS_MAX (sizeof(uint32_t) * 8) // Maximum number of bytes for LCD command
18+
#define LCD_SPI_3WIRE_PARAM_BITS_MAX (sizeof(uint32_t) * 8) // Maximum number of bytes for LCD parameter
19+
#define LCD_SPI_3WIRE_CLK_MAX (500 * 1000UL)
20+
21+
#define LCD_SPI_3WIRE_DATA_DC_BIT_0 (0) // DC bit = 0
22+
#define LCD_SPI_3WIRE_DATA_DC_BIT_1 (1) // DC bit = 1
23+
#define LCD_SPI_3WIRE_DATA_NO_DC_BIT (2) // No DC bit
24+
#define LCD_SPI_3WIRE_WRITE_ORDER_LSB_MASK (0x01) // Bit mask for LSB first write order
25+
#define LCD_SPI_3WIRE_WRITE_ORDER_MSB_MASK (0x80) // Bit mask for MSB first write order
26+
27+
28+
typedef struct _mp_spi_3wire_obj_t {
29+
mp_obj_base_t base;
30+
int cs;
31+
int scl;
32+
int sda;
33+
34+
uint32_t scl_half_period_us;
35+
uint32_t cmd_bytes: 3;
36+
uint32_t cmd_dc_bit: 2;
37+
uint32_t param_bytes: 3;
38+
uint32_t param_dc_bit: 2;
39+
uint32_t write_order_mask: 8;
40+
uint32_t cs_high_active: 1;
41+
uint32_t sda_scl_idle_high: 1;
42+
uint32_t scl_active_rising_edge: 1;
43+
uint32_t del_keep_cs_inactive: 1;
44+
45+
} mp_spi_3wire_obj_t;
46+
47+
extern const mp_obj_type_t mp_spi_3wire_type;
48+
49+
mp_lcd_err_t mp_spi_3wire_init(mp_spi_3wire_obj_t *self, uint8_t cmd_bits, uint8_t param_bits);
50+
esp_err_t mp_spi_3wire_tx_param(mp_spi_3wire_obj_t *self, int lcd_cmd, const void *param, size_t param_size);
51+
void mp_spi_3wire_deinit(mp_spi_3wire_obj_t *self);
52+
53+
#endif /* SOC_LCD_I80_SUPPORTED */
54+
#endif /* _ESP32_SPI_3WIRE_H_ */

ext_mod/lcd_bus/esp32_src/i80_bus.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
#include "esp_lcd_panel_io.h"
2020
#include "esp_heap_caps.h"
2121
#include "hal/lcd_types.h"
22+
#include "spi_3wire.h"
2223

2324

2425
mp_lcd_err_t i80_del(mp_obj_t obj);
2526
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);
2627
mp_lcd_err_t i80_get_lane_count(mp_obj_t obj, uint8_t *lane_count);
28+
mp_lcd_err_t i80_tx_param(mp_obj_t obj, int lcd_cmd, void *param, size_t param_size);
2729

2830
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)
2931
{
@@ -57,6 +59,7 @@
5759
ARG_reverse_color_bits,
5860
ARG_pclk_active_low,
5961
ARG_pclk_idle_low,
62+
ARG_spi_3wire,
6063
};
6164

6265
const mp_arg_t make_new_args[] = {
@@ -87,7 +90,8 @@
8790
{ MP_QSTR_cs_active_high, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
8891
{ MP_QSTR_reverse_color_bits, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
8992
{ MP_QSTR_pclk_active_low, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
90-
{ MP_QSTR_pclk_idle_low, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } }
93+
{ MP_QSTR_pclk_idle_low, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
94+
{ MP_QSTR_spi_3wire, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } },
9195
};
9296

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

156+
if (args[ARG_spi_3wire].u_obj == mp_const_none) {
157+
self->spi_3wire = NULL;
158+
} else {
159+
self->spi_3wire = (mp_spi_3wire_obj_t *)MP_OBJ_TO_PTR(args[ARG_spi_3wire].u_obj);
160+
}
161+
152162
#if CONFIG_LCD_ENABLE_DEBUG_LOG
153163
printf("dc_gpio_num=%d\n", self->bus_config.dc_gpio_num);
154164
printf("wr_gpio_num=%d\n", self->bus_config.wr_gpio_num);
@@ -186,6 +196,7 @@
186196
self->panel_io_handle.init = &i80_init;
187197
self->panel_io_handle.del = &i80_del;
188198
self->panel_io_handle.get_lane_count = &i80_get_lane_count;
199+
self->panel_io_handle.tx_param = &i80_tx_param;
189200

190201
return MP_OBJ_FROM_PTR(self);
191202
}
@@ -213,6 +224,11 @@
213224
printf("lcd_param_bits=%d\n", self->panel_io_config.lcd_param_bits);
214225
printf("max_transfer_bytes=%d\n", self->bus_config.max_transfer_bytes);
215226
#endif
227+
228+
if (self->spi_3wire != NULL) {
229+
mp_spi_3wire_init(self->spi_3wire, cmd_bits, param_bits);
230+
}
231+
216232
esp_err_t ret = esp_lcd_new_i80_bus(&self->bus_config, &self->bus_handle);
217233

218234
if (ret != 0) {
@@ -228,6 +244,26 @@
228244
return ret;
229245
}
230246

247+
mp_lcd_err_t i80_tx_param(mp_obj_t obj, int lcd_cmd, void *param, size_t param_size)
248+
{
249+
#if CONFIG_LCD_ENABLE_DEBUG_LOG
250+
printf("i80_tx_param(self, lcd_cmd=%d, param, param_size=%d)\n", lcd_cmd, param_size);
251+
#endif
252+
253+
mp_lcd_i80_bus_obj_t *self = (mp_lcd_i80_bus_obj_t *)obj;
254+
255+
mp_lcd_err_t ret;
256+
257+
if (self->spi_3wire != NULL) {
258+
mp_spi_3wire_tx_param(self->spi_3wire, lcd_cmd, param, param_size);
259+
ret = LCD_OK
260+
} else {
261+
ret = esp_lcd_panel_io_tx_param(self->panel_io_handle.panel_io, lcd_cmd, param, param_size);
262+
}
263+
264+
return ret;
265+
}
266+
231267

232268
mp_lcd_err_t i80_del(mp_obj_t obj)
233269
{
@@ -237,6 +273,11 @@
237273

238274
mp_lcd_i80_bus_obj_t *self = (mp_lcd_i80_bus_obj_t *)obj;
239275

276+
if (self->spi_3wire != NULL) {
277+
mp_spi_3wire_deinit(self->spi_3wire);
278+
self->spi_3wire = NULL;
279+
}
280+
240281
mp_lcd_err_t ret = esp_lcd_panel_io_del(self->panel_io_handle.panel_io);
241282
if (ret != 0) {
242283
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("%d(esp_lcd_panel_io_del)"), ret);

ext_mod/lcd_bus/esp32_src/rgb_bus.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "lcd_types.h"
77
#include "modlcd_bus.h"
88
#include "rgb_bus.h"
9+
#include "spi_3wire.h"
910

1011
// esp-idf includes
1112
#include "hal/lcd_hal.h"
@@ -113,6 +114,7 @@
113114
ARG_pclk_idle_high,
114115
ARG_pclk_active_low,
115116
ARG_refresh_on_demand,
117+
ARG_spi_3wire,
116118
};
117119

118120
const mp_arg_t allowed_args[] = {
@@ -149,6 +151,7 @@
149151
{ MP_QSTR_pclk_idle_high, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
150152
{ MP_QSTR_pclk_active_low, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
151153
{ MP_QSTR_refresh_on_demand, MP_ARG_BOOL | MP_ARG_KW_ONLY, { .u_bool = false } },
154+
{ MP_QSTR_spi_3wire, MP_ARG_OBJ | MP_ARG_KW_ONLY, { .u_obj = mp_const_none } },
152155
};
153156

154157
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -210,6 +213,12 @@
210213

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

216+
if (args[ARG_spi_3wire].u_obj == mp_const_none) {
217+
self->spi_3wire = NULL;
218+
} else {
219+
self->spi_3wire = (mp_spi_3wire_obj_t *)MP_OBJ_TO_PTR(args[ARG_spi_3wire].u_obj);
220+
}
221+
213222
#if CONFIG_LCD_ENABLE_DEBUG_LOG
214223
printf("pclk_hz=%lu\n", self->bus_config.pclk_hz);
215224
printf("hsync_pulse_width=%lu\n", self->bus_config.hsync_pulse_width);
@@ -272,6 +281,11 @@
272281
printf("rgb_del(self)\n");
273282
#endif
274283

284+
if (self->spi_3wire != NULL) {
285+
mp_spi_3wire_deinit(self->spi_3wire);
286+
self->spi_3wire = NULL;
287+
}
288+
275289
mp_lcd_err_t ret = esp_lcd_panel_del(self->panel_handle);
276290
return ret;
277291
}
@@ -293,14 +307,22 @@
293307

294308
mp_lcd_err_t rgb_tx_param(mp_obj_t obj, int lcd_cmd, void *param, size_t param_size)
295309
{
296-
LCD_UNUSED(obj);
297-
LCD_UNUSED(param);
310+
mp_lcd_rgb_bus_obj_t *self = (mp_lcd_rgb_bus_obj_t *)obj;
311+
312+
if (self->spi_3wire != NULL) {
313+
mp_spi_3wire_tx_param(self->spi_3wire, lcd_cmd, param, param_size);
314+
} else {
315+
LCD_UNUSED(param);
316+
317+
#if !CONFIG_LCD_ENABLE_DEBUG_LOG
318+
LCD_UNUSED(lcd_cmd);
319+
LCD_UNUSED(param_size);
320+
#endif
321+
322+
}
298323

299324
#if CONFIG_LCD_ENABLE_DEBUG_LOG
300325
printf("rgb_tx_param(self, lcd_cmd=%d, param, param_size=%d)\n", lcd_cmd, param_size);
301-
#else
302-
LCD_UNUSED(lcd_cmd);
303-
LCD_UNUSED(param_size);
304326
#endif
305327

306328
return LCD_OK;

0 commit comments

Comments
 (0)