Skip to content

Commit c09cc5f

Browse files
committed
feature: support spi panel
Signed-off-by: lbuque <1102390310@qq.com>
1 parent 8cceb5f commit c09cc5f

File tree

13 files changed

+451
-84
lines changed

13 files changed

+451
-84
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Supported boards:
2727

2828
- [LILYGO T-DisplayS3](https://github.com/Xinyuan-LilyGO/T-Display-S3)
2929
- [LILYGO T-RGB](https://github.com/Xinyuan-LilyGO/T-RGB)
30+
- [LILYGO T-Displat](https://github.com/Xinyuan-LilyGO/TTGO-T-Display)
3031

3132
## Documentation
3233

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from machine import Pin, SPI
2+
import lcd
3+
4+
def config():
5+
hspi = SPI(2, baudrate=40000000, sck=Pin(18), mosi=Pin(19), miso=None)
6+
panel = lcd.SPIPanel(spi=hspi, command=Pin(16), cs=Pin(5), pclk=60000000, width=135, height=240)
7+
st = lcd.ST7789(panel, reset=Pin(23), backlight=Pin(4))
8+
st.backlight_on()
9+
st.reset()
10+
st.init()
11+
st.invert_color(True)
12+
st.rotation(3)
13+
return st

lcd/hal/esp32.c

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,64 @@
55
#include "esp_lcd_panel_ops.h"
66
#include "esp_lcd_panel_rgb.h"
77

8+
#include "machine_hw_spi.c"
89
#include "py/runtime.h"
910

10-
#define DEBUG_printf(...)
11+
#define DEBUG_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__);
12+
13+
void hal_lcd_spi_panel_construct(lcd_spi_panel_obj_t *self) {
14+
machine_hw_spi_obj_t *spi_obj = ((machine_hw_spi_obj_t *)self->spi_obj);
15+
// if (spi_obj == NULL) {
16+
// mp_raise_msg_varg(&mp_type_OSError, "null");
17+
// }
18+
// machine_hw_spi_deinit_internal(spi_obj);
19+
// spi_bus_config_t buscfg = {
20+
// .sclk_io_num = spi_obj->sck,
21+
// .mosi_io_num = spi_obj->mosi,
22+
// .miso_io_num = -1,
23+
// .quadwp_io_num = -1,
24+
// .quadhd_io_num = -1,
25+
// .max_transfer_sz = self->width * self->height * 2 + 8
26+
// };
27+
// esp_err_t ret = spi_bus_initialize(spi_obj->host, &buscfg, SPI_DMA_CH_AUTO);
28+
// if (ret != 0) {
29+
// mp_raise_msg_varg(&mp_type_OSError, "%d(spi_bus_initialize)", ret);
30+
// }
31+
32+
esp_lcd_panel_io_spi_config_t io_config = {
33+
.dc_gpio_num = mp_hal_get_pin_obj(self->dc),
34+
.cs_gpio_num = mp_hal_get_pin_obj(self->cs),
35+
.pclk_hz = self->pclk,
36+
.lcd_cmd_bits = self->cmd_bits,
37+
.lcd_param_bits = self->param_bits,
38+
.spi_mode = 0,
39+
.trans_queue_depth = 4,
40+
};
41+
42+
// Attach the LCD to the SPI bus
43+
esp_err_t ret = esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)spi_obj->host, &io_config, &self->io_handle);
44+
if (ret != 0) {
45+
mp_raise_msg_varg(&mp_type_OSError, "%d(esp_lcd_new_panel_io_spi)", ret);
46+
}
47+
}
48+
49+
50+
inline void hal_lcd_spi_panel_tx_param(lcd_spi_panel_obj_t *self, int lcd_cmd, const void *param, size_t param_size) {
51+
DEBUG_printf("tx_param cmd: %x, param_size: %u\n", lcd_cmd, param_size);
52+
esp_lcd_panel_io_tx_param(self->io_handle, lcd_cmd, param, param_size);
53+
}
54+
55+
56+
inline void hal_lcd_spi_panel_tx_color(lcd_spi_panel_obj_t *self, int lcd_cmd, const void *color, size_t color_size) {
57+
DEBUG_printf("tx_color cmd: %x, color_size: %u\n", lcd_cmd, color_size);
58+
esp_lcd_panel_io_tx_color(self->io_handle, lcd_cmd, color, color_size);
59+
}
60+
61+
62+
inline void hal_lcd_spi_panel_deinit(lcd_spi_panel_obj_t *self) {
63+
esp_lcd_panel_io_del(self->io_handle);
64+
}
65+
1166

1267
void hal_lcd_i80_construct(lcd_i80_obj_t *self) {
1368
esp_lcd_i80_bus_config_t bus_config = {
@@ -58,24 +113,25 @@ void hal_lcd_i80_construct(lcd_i80_obj_t *self) {
58113
}
59114

60115

61-
void hal_lcd_i80_tx_param(lcd_i80_obj_t *self, int lcd_cmd, const void *param, size_t param_size) {
116+
inline void hal_lcd_i80_tx_param(lcd_i80_obj_t *self, int lcd_cmd, const void *param, size_t param_size) {
62117
DEBUG_printf("tx_param cmd: %x, param_size: %u\n", lcd_cmd, param_size);
63118
esp_lcd_panel_io_tx_param(self->io_handle, lcd_cmd, param, param_size);
64119
}
65120

66121

67-
void hal_lcd_i80_tx_color(lcd_i80_obj_t *self, int lcd_cmd, const void *color, size_t color_size) {
122+
inline void hal_lcd_i80_tx_color(lcd_i80_obj_t *self, int lcd_cmd, const void *color, size_t color_size) {
68123
DEBUG_printf("tx_color cmd: %x, color_size: %u\n", lcd_cmd, color_size);
69124
esp_lcd_panel_io_tx_color(self->io_handle, lcd_cmd, color, color_size);
70125
}
71126

72127

73-
void hal_lcd_i80_deinit(lcd_i80_obj_t *self) {
128+
inline void hal_lcd_i80_deinit(lcd_i80_obj_t *self) {
74129
esp_lcd_panel_io_del(self->io_handle);
75130
esp_lcd_del_i80_bus(self->i80_bus);
76131
}
77132

78133

134+
#if RGB_LCD_SUPPORTED
79135
void hal_lcd_rgb_construct(lcd_rgb_obj_t *self) {
80136
esp_lcd_rgb_panel_config_t panel_config = {
81137
.clk_src = LCD_CLK_SRC_PLL160M,
@@ -183,3 +239,4 @@ inline void hal_lcd_rgb_invert_color(lcd_rgb_obj_t *self, bool invert_color_data
183239
inline void hal_lcd_rgb_disp_off(lcd_rgb_obj_t *self, bool off) {
184240
esp_lcd_panel_disp_off(self->panel_handle, off);
185241
}
242+
#endif

lcd/hal/esp32.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
#ifndef _ESP32_H_
22
#define _ESP32_H_
3+
#include "spi_panel.h"
34
#include "i80_panel.h"
45
#include "rgb_panel.h"
56

7+
// spi
8+
void hal_lcd_spi_panel_construct(lcd_spi_panel_obj_t *self);
9+
10+
void hal_lcd_spi_panel_tx_param(lcd_spi_panel_obj_t *self, int lcd_cmd, const void *param, size_t param_size);
11+
12+
void hal_lcd_spi_panel_tx_color(lcd_spi_panel_obj_t *self, int lcd_cmd, const void *color, size_t color_size);
13+
14+
void hal_lcd_spi_panel_deinit(lcd_spi_panel_obj_t *self);
15+
616
// i8080
717
void hal_lcd_i80_construct(lcd_i80_obj_t *self);
818

@@ -13,6 +23,7 @@ void hal_lcd_i80_tx_color(lcd_i80_obj_t *self, int lcd_cmd, const void *color, s
1323
void hal_lcd_i80_deinit(lcd_i80_obj_t *self);
1424

1525
// rgb
26+
#if RGB_LCD_SUPPORTED
1627
void hal_lcd_rgb_construct(lcd_rgb_obj_t *self);
1728

1829
void hal_lcd_rgb_reset(lcd_rgb_obj_t *self);
@@ -32,5 +43,6 @@ void hal_lcd_rgb_set_gap(lcd_rgb_obj_t *self, int x_gap, int y_gap);
3243
void hal_lcd_rgb_invert_color(lcd_rgb_obj_t *self, bool invert_color_data);
3344

3445
void hal_lcd_rgb_disp_off(lcd_rgb_obj_t *self, bool off);
46+
#endif
3547

3648
#endif

lcd/i80_panel.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#include "py/obj.h"
1212
#include "py/runtime.h"
13-
// #include "py/mphal.h"
1413
#include "py/gc.h"
1514

1615
#include <string.h>

lcd/i80_panel.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include "mphalport.h"
55
#include "py/obj.h"
6-
// #include "py/mphal.h"
76
#if USE_ESP_LCD
87
#include "esp_lcd_panel_io.h"
98
#endif

lcd/micropython.cmake

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,21 @@
33
add_library(usermod_lcd INTERFACE)
44

55
# Add our source files to the lib
6-
set(SRC
7-
${CMAKE_CURRENT_LIST_DIR}/modlcd.c
8-
${CMAKE_CURRENT_LIST_DIR}/st7789.c
9-
${CMAKE_CURRENT_LIST_DIR}/i80_panel.c
6+
set(SRC ${CMAKE_CURRENT_LIST_DIR}/modlcd.c
7+
${CMAKE_CURRENT_LIST_DIR}/st7789.c
8+
${CMAKE_CURRENT_LIST_DIR}/i80_panel.c
9+
${CMAKE_CURRENT_LIST_DIR}/spi_panel.c
1010
)
1111

12-
# LIST(APPEND SRC
13-
# ${CMAKE_CURRENT_LIST_DIR}/hal/soft8080.c
14-
# )
15-
16-
if (CONFIG_IDF_TARGET_ESP32S3)
17-
LIST(APPEND SRC
18-
${CMAKE_CURRENT_LIST_DIR}/hal/esp32.c
19-
${CMAKE_CURRENT_LIST_DIR}/rgb_panel.c
20-
)
21-
target_compile_definitions(usermod_lcd INTERFACE
22-
USE_ESP_LCD=1
23-
)
12+
if (CONFIG_IDF_TARGET_ESP32 OR CONFIG_IDF_TARGET_ESP32S3)
13+
LIST(APPEND SRC ${CMAKE_CURRENT_LIST_DIR}/hal/esp32.c)
14+
target_compile_definitions(usermod_lcd INTERFACE USE_ESP_LCD=1)
15+
if (CONFIG_IDF_TARGET_ESP32S3)
16+
LIST(APPEND SRC ${CMAKE_CURRENT_LIST_DIR}/rgb_panel.c)
17+
target_compile_definitions(usermod_lcd INTERFACE RGB_LCD_SUPPORTED=1)
18+
endif()
2419
else()
25-
LIST(APPEND SRC
26-
${CMAKE_CURRENT_LIST_DIR}/hal/soft8080.c
27-
)
20+
LIST(APPEND SRC ${CMAKE_CURRENT_LIST_DIR}/hal/soft8080.c)
2821
endif()
2922

3023
target_sources(usermod_lcd INTERFACE ${SRC})

lcd/modlcd.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
#include "st7789.h"
3+
#include "spi_panel.h"
34
#include "i80_panel.h"
4-
#if USE_ESP_LCD
5+
#if RGB_LCD_SUPPORTED
56
#include "rgb_panel.h"
67
#endif
78

@@ -12,7 +13,8 @@ STATIC const mp_map_elem_t mp_module_lcd_globals_table[] = {
1213
{ MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_lcd) },
1314
{ MP_ROM_QSTR(MP_QSTR_ST7789), (mp_obj_t)&lcd_st7789_type },
1415
{ MP_ROM_QSTR(MP_QSTR_I8080), (mp_obj_t)&lcd_i80_type },
15-
#if USE_ESP_LCD
16+
{ MP_ROM_QSTR(MP_QSTR_SPIPanel), (mp_obj_t)&lcd_spi_panel_type },
17+
#if RGB_LCD_SUPPORTED
1618
{ MP_ROM_QSTR(MP_QSTR_RGB), (mp_obj_t)&lcd_rgb_type },
1719
#endif
1820
};

lcd/rgb_panel.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "py/obj.h"
88
#include "py/runtime.h"
99

10-
// #include "py/mphal.h"
1110
#include "mphalport.h"
1211
#include "py/gc.h"
1312

lcd/rgb_panel.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define LCD_RGB_H_
33

44
#include "py/obj.h"
5-
// #include "py/mphal.h"
65
#include "mphalport.h"
76
#if USE_ESP_LCD
87
#include "esp_lcd_panel_io.h"

0 commit comments

Comments
 (0)