diff options
Diffstat (limited to 'drivers/painter/st77xx')
-rw-r--r-- | drivers/painter/st77xx/qp_st7789.c | 144 | ||||
-rw-r--r-- | drivers/painter/st77xx/qp_st7789.h | 44 | ||||
-rw-r--r-- | drivers/painter/st77xx/qp_st7789_opcodes.h | 64 | ||||
-rw-r--r-- | drivers/painter/st77xx/qp_st77xx_opcodes.h | 51 |
4 files changed, 303 insertions, 0 deletions
diff --git a/drivers/painter/st77xx/qp_st7789.c b/drivers/painter/st77xx/qp_st7789.c new file mode 100644 index 0000000000..d005ece050 --- /dev/null +++ b/drivers/painter/st77xx/qp_st7789.c @@ -0,0 +1,144 @@ +// Copyright 2021 Paul Cotter (@gr1mr3aver) +// Copyright 2021 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "qp_internal.h" +#include "qp_comms.h" +#include "qp_st7789.h" +#include "qp_st77xx_opcodes.h" +#include "qp_st7789_opcodes.h" +#include "qp_tft_panel.h" + +#ifdef QUANTUM_PAINTER_ST7789_SPI_ENABLE +# include "qp_comms_spi.h" +#endif // QUANTUM_PAINTER_ST7789_SPI_ENABLE + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Common + +// Driver storage +tft_panel_dc_reset_painter_device_t st7789_drivers[ST7789_NUM_DEVICES] = {0}; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Automatic viewport offsets + +#ifndef ST7789_NO_AUTOMATIC_OFFSETS +static inline void st7789_automatic_viewport_offsets(painter_device_t device, painter_rotation_t rotation) { + struct painter_driver_t *driver = (struct painter_driver_t *)device; + + // clang-format off + const struct { + uint16_t offset_x; + uint16_t offset_y; + } rotation_offsets_240x240[] = { + [QP_ROTATION_0] = { .offset_x = 0, .offset_y = 0 }, + [QP_ROTATION_90] = { .offset_x = 0, .offset_y = 0 }, + [QP_ROTATION_180] = { .offset_x = 0, .offset_y = 80 }, + [QP_ROTATION_270] = { .offset_x = 80, .offset_y = 0 }, + }; + // clang-format on + + if (driver->panel_width == 240 && driver->panel_height == 240) { + driver->offset_x = rotation_offsets_240x240[rotation].offset_x; + driver->offset_y = rotation_offsets_240x240[rotation].offset_y; + } +} +#endif // ST7789_NO_AUTOMATIC_OFFSETS + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Initialization + +bool qp_st7789_init(painter_device_t device, painter_rotation_t rotation) { + // clang-format off + const uint8_t st7789_init_sequence[] = { + // Command, Delay, N, Data[N] + ST77XX_CMD_RESET, 120, 0, + ST77XX_CMD_SLEEP_OFF, 5, 0, + ST77XX_SET_PIX_FMT, 0, 1, 0x55, + ST77XX_CMD_INVERT_ON, 0, 0, + ST77XX_CMD_NORMAL_ON, 0, 0, + ST77XX_CMD_DISPLAY_ON, 20, 0 + }; + // clang-format on + qp_comms_bulk_command_sequence(device, st7789_init_sequence, sizeof(st7789_init_sequence)); + + // Configure the rotation (i.e. the ordering and direction of memory writes in GRAM) + const uint8_t madctl[] = { + [QP_ROTATION_0] = ST77XX_MADCTL_RGB, + [QP_ROTATION_90] = ST77XX_MADCTL_RGB | ST77XX_MADCTL_MX | ST77XX_MADCTL_MV, + [QP_ROTATION_180] = ST77XX_MADCTL_RGB | ST77XX_MADCTL_MX | ST77XX_MADCTL_MY, + [QP_ROTATION_270] = ST77XX_MADCTL_RGB | ST77XX_MADCTL_MV | ST77XX_MADCTL_MY, + }; + qp_comms_command_databyte(device, ST77XX_SET_MADCTL, madctl[rotation]); + +#ifndef ST7789_NO_AUTOMATIC_VIEWPORT_OFFSETS + st7789_automatic_viewport_offsets(device, rotation); +#endif // ST7789_NO_AUTOMATIC_VIEWPORT_OFFSETS + + return true; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Driver vtable + +const struct tft_panel_dc_reset_painter_driver_vtable_t st7789_driver_vtable = { + .base = + { + .init = qp_st7789_init, + .power = qp_tft_panel_power, + .clear = qp_tft_panel_clear, + .flush = qp_tft_panel_flush, + .pixdata = qp_tft_panel_pixdata, + .viewport = qp_tft_panel_viewport, + .palette_convert = qp_tft_panel_palette_convert, + .append_pixels = qp_tft_panel_append_pixels, + }, + .rgb888_to_native16bit = qp_rgb888_to_rgb565_swapped, + .num_window_bytes = 2, + .swap_window_coords = false, + .opcodes = + { + .display_on = ST77XX_CMD_DISPLAY_ON, + .display_off = ST77XX_CMD_DISPLAY_OFF, + .set_column_address = ST77XX_SET_COL_ADDR, + .set_row_address = ST77XX_SET_ROW_ADDR, + .enable_writes = ST77XX_SET_MEM, + }, +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// SPI + +#ifdef QUANTUM_PAINTER_ST7789_SPI_ENABLE + +// Factory function for creating a handle to the ST7789 device +painter_device_t qp_st7789_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) { + for (uint32_t i = 0; i < ST7789_NUM_DEVICES; ++i) { + tft_panel_dc_reset_painter_device_t *driver = &st7789_drivers[i]; + if (!driver->base.driver_vtable) { + driver->base.driver_vtable = (const struct painter_driver_vtable_t *)&st7789_driver_vtable; + driver->base.comms_vtable = (const struct painter_comms_vtable_t *)&spi_comms_with_dc_vtable; + driver->base.panel_width = panel_width; + driver->base.panel_height = panel_height; + driver->base.rotation = QP_ROTATION_0; + driver->base.offset_x = 0; + driver->base.offset_y = 0; + driver->base.native_bits_per_pixel = 16; // RGB565 + + // SPI and other pin configuration + driver->base.comms_config = &driver->spi_dc_reset_config; + driver->spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin; + driver->spi_dc_reset_config.spi_config.divisor = spi_divisor; + driver->spi_dc_reset_config.spi_config.lsb_first = false; + driver->spi_dc_reset_config.spi_config.mode = spi_mode; + driver->spi_dc_reset_config.dc_pin = dc_pin; + driver->spi_dc_reset_config.reset_pin = reset_pin; + return (painter_device_t)driver; + } + } + return NULL; +} + +#endif // QUANTUM_PAINTER_ST7789_SPI_ENABLE + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/drivers/painter/st77xx/qp_st7789.h b/drivers/painter/st77xx/qp_st7789.h new file mode 100644 index 0000000000..ec61f5d70b --- /dev/null +++ b/drivers/painter/st77xx/qp_st7789.h @@ -0,0 +1,44 @@ +// Copyright 2021 Paul Cotter (@gr1mr3aver) +// Copyright 2021 Nick Brassel (@tzarc) +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "gpio.h" +#include "qp_internal.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Quantum Painter ST7789 configurables (add to your keyboard's config.h) + +#ifndef ST7789_NUM_DEVICES +/** + * @def This controls the maximum number of ST7789 devices that Quantum Painter can communicate with at any one time. + * Increasing this number allows for multiple displays to be used. + */ +# define ST7789_NUM_DEVICES 1 +#endif + +// Additional configuration options to be copied to your keyboard's config.h (don't change here): + +// If you know exactly which offsets should be used on your panel with respect to selected rotation, then this config +// option allows you to save some flash space -- you'll need to invoke qp_set_viewport_offsets() instead from your keyboard. +// #define ST7789_NO_AUTOMATIC_VIEWPORT_OFFSETS + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Quantum Painter ST7789 device factories + +#ifdef QUANTUM_PAINTER_ST7789_SPI_ENABLE +/** + * Factory method for an ST7789 SPI LCD device. + * + * @param panel_width[in] the width of the display panel + * @param panel_height[in] the height of the display panel + * @param chip_select_pin[in] the GPIO pin used for SPI chip select + * @param dc_pin[in] the GPIO pin used for D/C control + * @param reset_pin[in] the GPIO pin used for RST + * @param spi_divisor[in] the SPI divisor to use when communicating with the display + * @param spi_mode[in] the SPI mode to use when communicating with the display + * @return the device handle used with all drawing routines in Quantum Painter + */ +painter_device_t qp_st7789_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); +#endif // QUANTUM_PAINTER_ST7789_SPI_ENABLE diff --git a/drivers/painter/st77xx/qp_st7789_opcodes.h b/drivers/painter/st77xx/qp_st7789_opcodes.h new file mode 100644 index 0000000000..b5baba7184 --- /dev/null +++ b/drivers/painter/st77xx/qp_st7789_opcodes.h @@ -0,0 +1,64 @@ +// Copyright 2021 Paul Cotter (@gr1mr3aver) +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Quantum Painter ST7789 additional command opcodes + +// System function commands +#define ST7789_GET_SELF_DIAG 0x0F // Get self-diagnostic result +#define ST7789_SET_VERT_SCRL 0x33 // Set vertical scroll definition +#define ST7789_SET_VERT_SCRL_ADDR 0x37 // SEt Vertical scroll start address +#define ST7789_SET_MEM_CONT 0x3C // Memory Write continue +#define ST7789_GET_MEM_CONT 0x3E // Memory Read continue +#define ST7789_SET_TEAR_LINE 0x44 // Set tear scanline +#define ST7789_GET_TEAR_LINE 0x45 // Get tear scanline +#define ST7789_SET_BRIGHTNESS 0x51 // Set display brightness +#define ST7789_GET_BRIGHTNESS 0x52 // Get display brightness +#define ST7789_SET_CTRL 0x53 // Set CTRL display +#define ST7789_GET_CTRL 0x54 // Get CTRL display value +#define ST7789_SET_CAB_COLOR 0x55 // Set content adaptive brightness control and color enhancement +#define ST7789_GET_CAB_COLOR 0x56 // Get content adaptive brightness control and color enhancement +#define ST7789_SET_CAB_BRIGHTNESS 0x5E // Set content adaptive minimum brightness +#define ST7789_GET_CAB_BRIGHTNESS 0x5F // Get content adaptive minimum brightness +#define ST7789_GET_ABC_SELF_DIAG 0x68 // Get Auto brightness control self diagnostics + +// Panel Function Commands +#define ST7789_SET_RAM_CTL 0xB0 // Set RAM control +#define ST7789_SET_RGB_CTL 0xB1 // Set RGB control +#define ST7789_SET_PORCH_CTL 0xB2 // Set Porch control +#define ST7789_SET_FRAME_RATE_CTL_1 0xB3 // Set frame rate control 1 +#define ST7789_SET_PARTIAL_CTL 0xB5 // Set Partial control +#define ST7789_SET_GATE_CTL 0xB7 // Set gate control +#define ST7789_SET_GATE_ON_TIMING 0xB8 // Set gate on timing adjustment +#define ST7789_SET_DIGITAL_GAMMA_ON 0xBA // Enable digital gamma +#define ST7789_SET_VCOM 0xBB // Set VCOM +#define ST7789_SET_POWER_SAVE 0xBC // Set power saving mode +#define ST7789_SET_DISP_OFF_POWER 0xBD // Set display off power saving +#define ST7789_SET_LCM_CTL 0xC0 // Set LCM control +#define ST7789_SET_IDS 0xC1 // Set IDs +#define ST7789_SET_VDV_VRH_ON 0xC2 // Set VDV and VRH command enable +#define ST7789_SET_VRH 0xC3 // Set VRH +#define ST7789_SET_VDV 0xC4 // Set VDV +#define ST7789_SET_VCOM_OFFSET 0xC5 // Set VCOM offset ctl +#define ST7789_SET_FRAME_RATE_CTL_2 0xC6 // Set frame rate control 2 +#define ST7789_SET_CABC_CTL 0xC7 // Set CABC Control +#define ST7789_GET_REG_1 0xC8 // Get register value selection1 +#define ST7789_GET_REG_2 0xCA // Get register value selection2 +#define ST7789_SET_PWM_FREQ 0xCC // Set PWM frequency +#define ST7789_SET_POWER_CTL_1 0xD0 // Set power ctl 1 +#define ST7789_SET_VAP_VAN_ON 0xD2 // Enable VAP/VAN signal output +#define ST7789_SET_CMD2_ENABLE 0xDF // Enable command 2 +#define ST7789_SET_PGAMMA 0xE0 // Set positive gamma +#define ST7789_SET_NGAMMA 0xE1 // Set negative gamma +#define ST7789_SET_DIGITAL_GAMMA_RED 0xE2 // Set digital gamma lookup table for red +#define ST7789_SET_DIGITAL_GAMMA_BLUE 0xE3 // Get digital gamma lookup table for blue +#define ST7789_SET_GATE_CTL_2 0xE4 // Set gate control 2 +#define ST7789_SET_SPI2_ENABLE 0xE7 // Enable SPI2 +#define ST7789_SET_POWER_CTL_2 0xE8 // Set power ctl 2 +#define ST7789_SET_EQ_TIME_CTL 0xE9 // Set equalize time control +#define ST7789_SET_PROG_CTL 0xEC // Set program control +#define ST7789_SET_PROG_MODE_ENABLE 0xFA // Set program mode enable +#define ST7789_SET_NVMEM 0xFC // Set NVMEM data +#define ST7789_SET_PROG_ACTION 0xFE // Set program action diff --git a/drivers/painter/st77xx/qp_st77xx_opcodes.h b/drivers/painter/st77xx/qp_st77xx_opcodes.h new file mode 100644 index 0000000000..131378d832 --- /dev/null +++ b/drivers/painter/st77xx/qp_st77xx_opcodes.h @@ -0,0 +1,51 @@ +// Copyright 2021 Paul Cotter (@gr1mr3aver) +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Quantum Painter ST77XX command opcodes + +// System function commands +#define ST77XX_CMD_NOP 0x00 // No operation +#define ST77XX_CMD_RESET 0x01 // Software reset +#define ST77XX_GET_ID_INFO 0x04 // Get ID information +#define ST77XX_GET_STATUS 0x09 // Get status +#define ST77XX_GET_PWR_MODE 0x0A // Get power mode +#define ST77XX_GET_MADCTL 0x0B // Get mem access ctl +#define ST77XX_GET_PIX_FMT 0x0C // Get pixel format +#define ST77XX_GET_IMG_FMT 0x0D // Get image format +#define ST77XX_GET_SIG_MODE 0x0E // Get signal mode +#define ST77XX_CMD_SLEEP_ON 0x10 // Enter sleep mode +#define ST77XX_CMD_SLEEP_OFF 0x11 // Exist sleep mode +#define ST77XX_CMD_PARTIAL_ON 0x12 // Enter partial mode +#define ST77XX_CMD_NORMAL_ON 0x13 // Exit partial mode +#define ST77XX_CMD_INVERT_OFF 0x20 // Exit inverted mode +#define ST77XX_CMD_INVERT_ON 0x21 // Enter inverted mode +#define ST77XX_SET_GAMMA 0x26 // Set gamma params +#define ST77XX_CMD_DISPLAY_OFF 0x28 // Disable display +#define ST77XX_CMD_DISPLAY_ON 0x29 // Enable display +#define ST77XX_SET_COL_ADDR 0x2A // Set column address +#define ST77XX_SET_ROW_ADDR 0x2B // Set page (row) address +#define ST77XX_SET_MEM 0x2C // Set memory +#define ST77XX_GET_MEM 0x2E // Get memory +#define ST77XX_SET_PARTIAL_AREA 0x30 // Set partial area +#define ST77XX_CMD_TEARING_OFF 0x34 // Tearing line disabled +#define ST77XX_CMD_TEARING_ON 0x35 // Tearing line enabled +#define ST77XX_SET_MADCTL 0x36 // Set mem access ctl +#define ST77XX_CMD_IDLE_OFF 0x38 // Exit idle mode +#define ST77XX_CMD_IDLE_ON 0x39 // Enter idle mode +#define ST77XX_SET_PIX_FMT 0x3A // Set pixel format +#define ST77XX_GET_ID1 0xDA // Get ID1 +#define ST77XX_GET_ID2 0xDB // Get ID2 +#define ST77XX_GET_ID3 0xDC // Get ID3 + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// MADCTL Flags +#define ST77XX_MADCTL_MY 0b10000000 +#define ST77XX_MADCTL_MX 0b01000000 +#define ST77XX_MADCTL_MV 0b00100000 +#define ST77XX_MADCTL_ML 0b00010000 +#define ST77XX_MADCTL_RGB 0b00000000 +#define ST77XX_MADCTL_BGR 0b00001000 +#define ST77XX_MADCTL_MH 0b00000100 |