summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Hoelscher <infinityis@users.noreply.github.com>2022-07-31 21:58:25 -0500
committerGitHub <noreply@github.com>2022-07-31 19:58:25 -0700
commit2bdd73f80135965c1047b24b78fdf15c1ebbbee7 (patch)
treeaac4992fdf46dac8a2e6c4d43a245ba558f6febb
parent09ea5f6337452fa5f15b225da2873377f46b1bbc (diff)
Add ST7735 driver to Quantum Painter (#17848)
-rw-r--r--docs/quantum_painter.md29
-rw-r--r--drivers/painter/st77xx/qp_st7735.c144
-rw-r--r--drivers/painter/st77xx/qp_st7735.h45
-rw-r--r--drivers/painter/st77xx/qp_st7735_opcodes.h31
-rw-r--r--quantum/painter/qp.h4
-rw-r--r--quantum/painter/rules.mk13
6 files changed, 264 insertions, 2 deletions
diff --git a/docs/quantum_painter.md b/docs/quantum_painter.md
index 2386a70933..6d4e2764d4 100644
--- a/docs/quantum_painter.md
+++ b/docs/quantum_painter.md
@@ -27,6 +27,7 @@ Hardware supported:
27| ILI9488 | RGB LCD | 320x480 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS = ili9488_spi` | 27| ILI9488 | RGB LCD | 320x480 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS = ili9488_spi` |
28| SSD1351 | RGB OLED | 128x128 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS = ssd1351_spi` | 28| SSD1351 | RGB OLED | 128x128 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS = ssd1351_spi` |
29| ST7789 | RGB LCD | 240x320, 240x240 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS = st7789_spi` | 29| ST7789 | RGB LCD | 240x320, 240x240 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS = st7789_spi` |
30| ST7735 | RGB LCD | 132x162, 80x160 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS = st7735_spi` |
30 31
31## Quantum Painter Configuration :id=quantum-painter-config 32## Quantum Painter Configuration :id=quantum-painter-config
32 33
@@ -727,4 +728,30 @@ The maximum number of displays can be configured by changing the following in yo
727#define ST7789_NUM_DEVICES 3 728#define ST7789_NUM_DEVICES 3
728``` 729```
729 730
730!> Some ST7789 devices are known to have different drawing offsets -- despite being a 240x320 pixel display controller internally, some display panels are only 240x240, or smaller. These may require an offset to be applied; see `qp_set_viewport_offsets` above for information on how to override the offsets if they aren't correctly rendered. \ No newline at end of file 731!> Some ST7789 devices are known to have different drawing offsets -- despite being a 240x320 pixel display controller internally, some display panels are only 240x240, or smaller. These may require an offset to be applied; see `qp_set_viewport_offsets` above for information on how to override the offsets if they aren't correctly rendered.
732
733### ST7735 :id=qp-driver-st7735
734
735Enabling support for the ST7735 in Quantum Painter is done by adding the following to `rules.mk`:
736
737```make
738QUANTUM_PAINTER_ENABLE = yes
739QUANTUM_PAINTER_DRIVERS = st7735_spi
740```
741
742Creating a ST7735 device in firmware can then be done with the following API:
743
744```c
745painter_device_t qp_st7735_make_spi_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode);
746```
747
748The device handle returned from the `qp_st7735_make_spi_device` function can be used to perform all other drawing operations.
749
750The maximum number of displays can be configured by changing the following in your `config.h` (default is 1):
751
752```c
753// 3 displays:
754#define ST7735_NUM_DEVICES 3
755```
756
757!> Some ST7735 devices are known to have different drawing offsets -- despite being a 132x162 pixel display controller internally, some display panels are only 80x160, or smaller. These may require an offset to be applied; see `qp_set_viewport_offsets` above for information on how to override the offsets if they aren't correctly rendered. \ No newline at end of file
diff --git a/drivers/painter/st77xx/qp_st7735.c b/drivers/painter/st77xx/qp_st7735.c
new file mode 100644
index 0000000000..e434e31b92
--- /dev/null
+++ b/drivers/painter/st77xx/qp_st7735.c
@@ -0,0 +1,144 @@
1// Copyright 2021 Paul Cotter (@gr1mr3aver)
2// Copyright 2021 Nick Brassel (@tzarc)
3// Copyright 2022 David Hoelscher (@customMK)
4// SPDX-License-Identifier: GPL-2.0-or-later
5
6#include "qp_internal.h"
7#include "qp_comms.h"
8#include "qp_st7735.h"
9#include "qp_st77xx_opcodes.h"
10#include "qp_st7735_opcodes.h"
11#include "qp_tft_panel.h"
12
13#ifdef QUANTUM_PAINTER_ST7735_SPI_ENABLE
14# include "qp_comms_spi.h"
15#endif // QUANTUM_PAINTER_ST7735_SPI_ENABLE
16
17////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
18// Common
19
20// Driver storage
21tft_panel_dc_reset_painter_device_t st7735_drivers[ST7735_NUM_DEVICES] = {0};
22
23////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
24// Automatic viewport offsets
25
26#ifndef ST7735_NO_AUTOMATIC_OFFSETS
27static inline void st7735_automatic_viewport_offsets(painter_device_t device, painter_rotation_t rotation) {
28 struct painter_driver_t *driver = (struct painter_driver_t *)device;
29
30 // clang-format off
31 const struct {
32 uint16_t offset_x;
33 uint16_t offset_y;
34 } rotation_offsets_80x160[] = {
35 [QP_ROTATION_0] = { .offset_x = 24, .offset_y = 0 },
36 [QP_ROTATION_90] = { .offset_x = 0, .offset_y = 24 },
37 [QP_ROTATION_180] = { .offset_x = 24, .offset_y = 0 },
38 [QP_ROTATION_270] = { .offset_x = 0, .offset_y = 24 },
39 };
40 // clang-format on
41
42 if (driver->panel_width == 80 && driver->panel_height == 160) {
43 driver->offset_x = rotation_offsets_80x160[rotation].offset_x;
44 driver->offset_y = rotation_offsets_80x160[rotation].offset_y;
45 }
46}
47#endif // ST7735_NO_AUTOMATIC_OFFSETS
48
49////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
50// Initialization
51
52bool qp_st7735_init(painter_device_t device, painter_rotation_t rotation) {
53 // clang-format off
54 const uint8_t st7735_init_sequence[] = {
55 // Command, Delay, N, Data[N]
56 ST77XX_CMD_RESET, 120, 0,
57 ST77XX_CMD_SLEEP_OFF, 5, 0,
58 ST77XX_SET_PIX_FMT, 0, 1, 0x55,
59 ST77XX_CMD_INVERT_OFF, 0, 0,
60 ST77XX_CMD_NORMAL_ON, 0, 0,
61 ST77XX_CMD_DISPLAY_ON, 20, 0
62 };
63 // clang-format on
64 qp_comms_bulk_command_sequence(device, st7735_init_sequence, sizeof(st7735_init_sequence));
65
66 // Configure the rotation (i.e. the ordering and direction of memory writes in GRAM)
67 const uint8_t madctl[] = {
68 [QP_ROTATION_0] = ST77XX_MADCTL_BGR,
69 [QP_ROTATION_90] = ST77XX_MADCTL_BGR | ST77XX_MADCTL_MX | ST77XX_MADCTL_MV,
70 [QP_ROTATION_180] = ST77XX_MADCTL_BGR | ST77XX_MADCTL_MX | ST77XX_MADCTL_MY,
71 [QP_ROTATION_270] = ST77XX_MADCTL_BGR | ST77XX_MADCTL_MV | ST77XX_MADCTL_MY,
72 };
73 qp_comms_command_databyte(device, ST77XX_SET_MADCTL, madctl[rotation]);
74
75#ifndef ST7735_NO_AUTOMATIC_VIEWPORT_OFFSETS
76 st7735_automatic_viewport_offsets(device, rotation);
77#endif // ST7735_NO_AUTOMATIC_VIEWPORT_OFFSETS
78
79 return true;
80}
81
82////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
83// Driver vtable
84
85const struct tft_panel_dc_reset_painter_driver_vtable_t st7735_driver_vtable = {
86 .base =
87 {
88 .init = qp_st7735_init,
89 .power = qp_tft_panel_power,
90 .clear = qp_tft_panel_clear,
91 .flush = qp_tft_panel_flush,
92 .pixdata = qp_tft_panel_pixdata,
93 .viewport = qp_tft_panel_viewport,
94 .palette_convert = qp_tft_panel_palette_convert_rgb565_swapped,
95 .append_pixels = qp_tft_panel_append_pixels_rgb565,
96 },
97 .num_window_bytes = 2,
98 .swap_window_coords = false,
99 .opcodes =
100 {
101 .display_on = ST77XX_CMD_DISPLAY_ON,
102 .display_off = ST77XX_CMD_DISPLAY_OFF,
103 .set_column_address = ST77XX_SET_COL_ADDR,
104 .set_row_address = ST77XX_SET_ROW_ADDR,
105 .enable_writes = ST77XX_SET_MEM,
106 },
107};
108
109////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
110// SPI
111
112#ifdef QUANTUM_PAINTER_ST7735_SPI_ENABLE
113
114// Factory function for creating a handle to the ST7735 device
115painter_device_t qp_st7735_make_spi_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode) {
116 for (uint32_t i = 0; i < ST7735_NUM_DEVICES; ++i) {
117 tft_panel_dc_reset_painter_device_t *driver = &st7735_drivers[i];
118 if (!driver->base.driver_vtable) {
119 driver->base.driver_vtable = (const struct painter_driver_vtable_t *)&st7735_driver_vtable;
120 driver->base.comms_vtable = (const struct painter_comms_vtable_t *)&spi_comms_with_dc_vtable;
121 driver->base.panel_width = panel_width;
122 driver->base.panel_height = panel_height;
123 driver->base.rotation = QP_ROTATION_0;
124 driver->base.offset_x = 0;
125 driver->base.offset_y = 0;
126 driver->base.native_bits_per_pixel = 16; // RGB565
127
128 // SPI and other pin configuration
129 driver->base.comms_config = &driver->spi_dc_reset_config;
130 driver->spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin;
131 driver->spi_dc_reset_config.spi_config.divisor = spi_divisor;
132 driver->spi_dc_reset_config.spi_config.lsb_first = false;
133 driver->spi_dc_reset_config.spi_config.mode = spi_mode;
134 driver->spi_dc_reset_config.dc_pin = dc_pin;
135 driver->spi_dc_reset_config.reset_pin = reset_pin;
136 return (painter_device_t)driver;
137 }
138 }
139 return NULL;
140}
141
142#endif // QUANTUM_PAINTER_ST7735_SPI_ENABLE
143
144//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// \ No newline at end of file
diff --git a/drivers/painter/st77xx/qp_st7735.h b/drivers/painter/st77xx/qp_st7735.h
new file mode 100644
index 0000000000..a9ce16bef1
--- /dev/null
+++ b/drivers/painter/st77xx/qp_st7735.h
@@ -0,0 +1,45 @@
1// Copyright 2021 Paul Cotter (@gr1mr3aver)
2// Copyright 2021 Nick Brassel (@tzarc)
3// Copyright 2022 David Hoelscher (@customMK)
4// SPDX-License-Identifier: GPL-2.0-or-later
5
6#pragma once
7
8#include "gpio.h"
9#include "qp_internal.h"
10
11////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
12// Quantum Painter ST7735 configurables (add to your keyboard's config.h)
13
14#ifndef ST7735_NUM_DEVICES
15/**
16 * @def This controls the maximum number of ST7735 devices that Quantum Painter can communicate with at any one time.
17 * Increasing this number allows for multiple displays to be used.
18 */
19# define ST7735_NUM_DEVICES 1
20#endif
21
22// Additional configuration options to be copied to your keyboard's config.h (don't change here):
23
24// If you know exactly which offsets should be used on your panel with respect to selected rotation, then this config
25// option allows you to save some flash space -- you'll need to invoke qp_set_viewport_offsets() instead from your keyboard.
26// #define ST7735_NO_AUTOMATIC_VIEWPORT_OFFSETS
27
28////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
29// Quantum Painter ST7735 device factories
30
31#ifdef QUANTUM_PAINTER_ST7735_SPI_ENABLE
32/**
33 * Factory method for an ST7735 SPI LCD device.
34 *
35 * @param panel_width[in] the width of the display panel
36 * @param panel_height[in] the height of the display panel
37 * @param chip_select_pin[in] the GPIO pin used for SPI chip select
38 * @param dc_pin[in] the GPIO pin used for D/C control
39 * @param reset_pin[in] the GPIO pin used for RST
40 * @param spi_divisor[in] the SPI divisor to use when communicating with the display
41 * @param spi_mode[in] the SPI mode to use when communicating with the display
42 * @return the device handle used with all drawing routines in Quantum Painter
43 */
44painter_device_t qp_st7735_make_spi_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode);
45#endif // QUANTUM_PAINTER_ST7735_SPI_ENABLE \ No newline at end of file
diff --git a/drivers/painter/st77xx/qp_st7735_opcodes.h b/drivers/painter/st77xx/qp_st7735_opcodes.h
new file mode 100644
index 0000000000..3cbef78537
--- /dev/null
+++ b/drivers/painter/st77xx/qp_st7735_opcodes.h
@@ -0,0 +1,31 @@
1// Copyright 2022 David Hoelscher (@customMK)
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7// Quantum Painter ST7735 additional command opcodes
8
9// Panel Function Commands
10#define ST7735_SET_FRAME_RATE_CTL_1 0xB1 // Set frame rate control 1
11#define ST7735_SET_FRAME_RATE_CTL_2 0xB2 // Set frame rate control 2
12#define ST7735_SET_FRAME_RATE_CTL_3 0xB3 // Set frame rate control 3
13#define ST7735_SET_INVERSION_CTL 0xB4 // Set inversion mode control
14#define ST7735_SET_DISPLAY_CTL 0xB6 // Set display control 5
15#define ST7735_SET_POWER_CTL_1 0xC0 // Set GVDD
16#define ST7735_SET_POWER_CTL_2 0xC1 // Set VGH and VGL
17#define ST7735_SET_POWER_CTL_3 0xC2 // Set normal mode op amp current
18#define ST7735_SET_POWER_CTL_4 0xC3 // Set idle mode op amp current
19#define ST7735_SET_POWER_CTL_5 0xC4 // Set partial mode op amp current
20#define ST7735_SET_VCOM_CTL 0xC5 // Set VCOM voltages
21#define ST7735_SET_VCOM_OFFSET_CTL 0xC7 // Set VCOM offset ctl
22#define ST7735_SET_LCD_ID 0xD1 // Set LCD module version
23#define ST7735_SET_PROJECT_ID 0xD2 // Set product project ID
24#define ST7735_SET_POWER_CTL_6 0xFC // Set partial+idle op amp current
25#define ST7735_SET_NVMEM_CTL_STATUS 0xD9 // EEPROM Control Status
26#define ST7735_SET_NVMEM_READ_CMD 0xCC // EEPROM Read Command
27#define ST7735_SET_NVMEM_WRITE_CMD 0xDF // EEPROM Write Command
28#define ST7735_SET_PGAMMA 0xE0 // Set positive gamma
29#define ST7735_SET_NGAMMA 0xE1 // Set negative gamma
30#define ST7735_SET_EXTENSION_ENABLE 0xF0 // Enable extension command
31#define ST7735_SET_VCOM_DELAY 0xFF // Set VCOM delay time
diff --git a/quantum/painter/qp.h b/quantum/painter/qp.h
index 47f077d0cf..fb6904de22 100644
--- a/quantum/painter/qp.h
+++ b/quantum/painter/qp.h
@@ -448,6 +448,10 @@ int16_t qp_drawtext_recolor(painter_device_t device, uint16_t x, uint16_t y, pai
448# include "qp_st7789.h" 448# include "qp_st7789.h"
449#endif // QUANTUM_PAINTER_ST7789_ENABLE 449#endif // QUANTUM_PAINTER_ST7789_ENABLE
450 450
451#ifdef QUANTUM_PAINTER_ST7735_ENABLE
452# include "qp_st7735.h"
453#endif // QUANTUM_PAINTER_ST7735_ENABLE
454
451#ifdef QUANTUM_PAINTER_GC9A01_ENABLE 455#ifdef QUANTUM_PAINTER_GC9A01_ENABLE
452# include "qp_gc9a01.h" 456# include "qp_gc9a01.h"
453#endif // QUANTUM_PAINTER_GC9A01_ENABLE 457#endif // QUANTUM_PAINTER_GC9A01_ENABLE
diff --git a/quantum/painter/rules.mk b/quantum/painter/rules.mk
index 675a1a5460..91787dfe0e 100644
--- a/quantum/painter/rules.mk
+++ b/quantum/painter/rules.mk
@@ -3,7 +3,7 @@ QUANTUM_PAINTER_DRIVERS ?=
3QUANTUM_PAINTER_ANIMATIONS_ENABLE ?= yes 3QUANTUM_PAINTER_ANIMATIONS_ENABLE ?= yes
4 4
5# The list of permissible drivers that can be listed in QUANTUM_PAINTER_DRIVERS 5# The list of permissible drivers that can be listed in QUANTUM_PAINTER_DRIVERS
6VALID_QUANTUM_PAINTER_DRIVERS := ili9163_spi ili9341_spi ili9488_spi st7789_spi gc9a01_spi ssd1351_spi 6VALID_QUANTUM_PAINTER_DRIVERS := ili9163_spi ili9341_spi ili9488_spi st7789_spi st7735_spi gc9a01_spi ssd1351_spi
7 7
8#------------------------------------------------------------------------------- 8#-------------------------------------------------------------------------------
9 9
@@ -83,6 +83,17 @@ define handle_quantum_painter_driver
83 $(DRIVER_PATH)/painter/tft_panel/qp_tft_panel.c \ 83 $(DRIVER_PATH)/painter/tft_panel/qp_tft_panel.c \
84 $(DRIVER_PATH)/painter/st77xx/qp_st7789.c 84 $(DRIVER_PATH)/painter/st77xx/qp_st7789.c
85 85
86 else ifeq ($$(strip $$(CURRENT_PAINTER_DRIVER)),st7735_spi)
87 QUANTUM_PAINTER_NEEDS_COMMS_SPI := yes
88 QUANTUM_PAINTER_NEEDS_COMMS_SPI_DC_RESET := yes
89 OPT_DEFS += -DQUANTUM_PAINTER_ST7735_ENABLE -DQUANTUM_PAINTER_ST7735_SPI_ENABLE
90 COMMON_VPATH += \
91 $(DRIVER_PATH)/painter/tft_panel \
92 $(DRIVER_PATH)/painter/st77xx
93 SRC += \
94 $(DRIVER_PATH)/painter/tft_panel/qp_tft_panel.c \
95 $(DRIVER_PATH)/painter/st77xx/qp_st7735.c
96
86 else ifeq ($$(strip $$(CURRENT_PAINTER_DRIVER)),gc9a01_spi) 97 else ifeq ($$(strip $$(CURRENT_PAINTER_DRIVER)),gc9a01_spi)
87 QUANTUM_PAINTER_NEEDS_COMMS_SPI := yes 98 QUANTUM_PAINTER_NEEDS_COMMS_SPI := yes
88 QUANTUM_PAINTER_NEEDS_COMMS_SPI_DC_RESET := yes 99 QUANTUM_PAINTER_NEEDS_COMMS_SPI_DC_RESET := yes