qp_st7735.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2021 Paul Cotter (@gr1mr3aver)
  2. // Copyright 2021-2023 Nick Brassel (@tzarc)
  3. // Copyright 2022 David Hoelscher (@customMK)
  4. // SPDX-License-Identifier: GPL-2.0-or-later
  5. #include "qp_internal.h"
  6. #include "qp_comms.h"
  7. #include "qp_st7735.h"
  8. #include "qp_st77xx_opcodes.h"
  9. #include "qp_st7735_opcodes.h"
  10. #include "qp_tft_panel.h"
  11. #ifdef QUANTUM_PAINTER_ST7735_SPI_ENABLE
  12. # include "qp_comms_spi.h"
  13. #endif // QUANTUM_PAINTER_ST7735_SPI_ENABLE
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  15. // Common
  16. // Driver storage
  17. tft_panel_dc_reset_painter_device_t st7735_drivers[ST7735_NUM_DEVICES] = {0};
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. // Automatic viewport offsets
  20. #ifndef ST7735_NO_AUTOMATIC_OFFSETS
  21. static inline void st7735_automatic_viewport_offsets(painter_device_t device, painter_rotation_t rotation) {
  22. painter_driver_t *driver = (painter_driver_t *)device;
  23. // clang-format off
  24. const struct {
  25. uint16_t offset_x;
  26. uint16_t offset_y;
  27. } rotation_offsets_80x160[] = {
  28. [QP_ROTATION_0] = { .offset_x = 24, .offset_y = 0 },
  29. [QP_ROTATION_90] = { .offset_x = 0, .offset_y = 24 },
  30. [QP_ROTATION_180] = { .offset_x = 24, .offset_y = 0 },
  31. [QP_ROTATION_270] = { .offset_x = 0, .offset_y = 24 },
  32. };
  33. // clang-format on
  34. if (driver->panel_width == 80 && driver->panel_height == 160) {
  35. driver->offset_x = rotation_offsets_80x160[rotation].offset_x;
  36. driver->offset_y = rotation_offsets_80x160[rotation].offset_y;
  37. }
  38. }
  39. #endif // ST7735_NO_AUTOMATIC_OFFSETS
  40. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. // Initialization
  42. __attribute__((weak)) bool qp_st7735_init(painter_device_t device, painter_rotation_t rotation) {
  43. // clang-format off
  44. const uint8_t st7735_init_sequence[] = {
  45. // Command, Delay, N, Data[N]
  46. ST77XX_CMD_RESET, 120, 0,
  47. ST77XX_CMD_SLEEP_OFF, 5, 0,
  48. ST77XX_SET_PIX_FMT, 0, 1, 0x55,
  49. ST77XX_CMD_INVERT_OFF, 0, 0,
  50. ST77XX_CMD_NORMAL_ON, 0, 0,
  51. ST7735_SET_PGAMMA, 0, 16, 0x02, 0x1C, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2D, 0x29, 0x25, 0x2B, 0x39, 0x00, 0x01, 0x03, 0x10,
  52. ST7735_SET_NGAMMA, 0, 16, 0x03, 0x1D, 0x07, 0x06, 0x2E, 0x2C, 0x29, 0x2D, 0x2E, 0x2E, 0x37, 0x3F, 0x00, 0x00, 0x02, 0x10,
  53. ST77XX_CMD_DISPLAY_ON, 20, 0
  54. };
  55. // clang-format on
  56. if (!qp_comms_bulk_command_sequence(device, st7735_init_sequence, sizeof(st7735_init_sequence))) {
  57. return false;
  58. }
  59. // Configure the rotation (i.e. the ordering and direction of memory writes in GRAM)
  60. const uint8_t madctl[] = {
  61. [QP_ROTATION_0] = ST77XX_MADCTL_BGR,
  62. [QP_ROTATION_90] = ST77XX_MADCTL_BGR | ST77XX_MADCTL_MX | ST77XX_MADCTL_MV,
  63. [QP_ROTATION_180] = ST77XX_MADCTL_BGR | ST77XX_MADCTL_MX | ST77XX_MADCTL_MY,
  64. [QP_ROTATION_270] = ST77XX_MADCTL_BGR | ST77XX_MADCTL_MV | ST77XX_MADCTL_MY,
  65. };
  66. if (!qp_comms_command_databyte(device, ST77XX_SET_MADCTL, madctl[rotation])) {
  67. return false;
  68. }
  69. #ifndef ST7735_NO_AUTOMATIC_VIEWPORT_OFFSETS
  70. st7735_automatic_viewport_offsets(device, rotation);
  71. #endif // ST7735_NO_AUTOMATIC_VIEWPORT_OFFSETS
  72. return true;
  73. }
  74. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  75. // Driver vtable
  76. const tft_panel_dc_reset_painter_driver_vtable_t st7735_driver_vtable = {
  77. .base =
  78. {
  79. .init = qp_st7735_init,
  80. .power = qp_tft_panel_power,
  81. .clear = qp_tft_panel_clear,
  82. .flush = qp_tft_panel_flush,
  83. .pixdata = qp_tft_panel_pixdata,
  84. .viewport = qp_tft_panel_viewport,
  85. .palette_convert = qp_tft_panel_palette_convert_rgb565_swapped,
  86. .append_pixels = qp_tft_panel_append_pixels_rgb565,
  87. .append_pixdata = qp_tft_panel_append_pixdata,
  88. },
  89. .num_window_bytes = 2,
  90. .swap_window_coords = false,
  91. .opcodes =
  92. {
  93. .display_on = ST77XX_CMD_DISPLAY_ON,
  94. .display_off = ST77XX_CMD_DISPLAY_OFF,
  95. .set_column_address = ST77XX_SET_COL_ADDR,
  96. .set_row_address = ST77XX_SET_ROW_ADDR,
  97. .enable_writes = ST77XX_SET_MEM,
  98. },
  99. };
  100. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  101. // SPI
  102. #ifdef QUANTUM_PAINTER_ST7735_SPI_ENABLE
  103. // Factory function for creating a handle to the ST7735 device
  104. painter_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) {
  105. for (uint32_t i = 0; i < ST7735_NUM_DEVICES; ++i) {
  106. tft_panel_dc_reset_painter_device_t *driver = &st7735_drivers[i];
  107. if (!driver->base.driver_vtable) {
  108. driver->base.driver_vtable = (const painter_driver_vtable_t *)&st7735_driver_vtable;
  109. driver->base.comms_vtable = (const painter_comms_vtable_t *)&spi_comms_with_dc_vtable;
  110. driver->base.panel_width = panel_width;
  111. driver->base.panel_height = panel_height;
  112. driver->base.rotation = QP_ROTATION_0;
  113. driver->base.offset_x = 0;
  114. driver->base.offset_y = 0;
  115. driver->base.native_bits_per_pixel = 16; // RGB565
  116. // SPI and other pin configuration
  117. driver->base.comms_config = &driver->spi_dc_reset_config;
  118. driver->spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin;
  119. driver->spi_dc_reset_config.spi_config.divisor = spi_divisor;
  120. driver->spi_dc_reset_config.spi_config.lsb_first = false;
  121. driver->spi_dc_reset_config.spi_config.mode = spi_mode;
  122. driver->spi_dc_reset_config.dc_pin = dc_pin;
  123. driver->spi_dc_reset_config.reset_pin = reset_pin;
  124. driver->spi_dc_reset_config.command_params_uses_command_pin = false;
  125. if (!qp_internal_register_device((painter_device_t)driver)) {
  126. memset(driver, 0, sizeof(tft_panel_dc_reset_painter_device_t));
  127. return NULL;
  128. }
  129. return (painter_device_t)driver;
  130. }
  131. }
  132. return NULL;
  133. }
  134. #endif // QUANTUM_PAINTER_ST7735_SPI_ENABLE
  135. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////