qp_st7789.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2021 Paul Cotter (@gr1mr3aver)
  2. // Copyright 2021 Nick Brassel (@tzarc)
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. #include "qp_internal.h"
  5. #include "qp_comms.h"
  6. #include "qp_st7789.h"
  7. #include "qp_st77xx_opcodes.h"
  8. #include "qp_st7789_opcodes.h"
  9. #include "qp_tft_panel.h"
  10. #ifdef QUANTUM_PAINTER_ST7789_SPI_ENABLE
  11. # include "qp_comms_spi.h"
  12. #endif // QUANTUM_PAINTER_ST7789_SPI_ENABLE
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // Common
  15. // Driver storage
  16. tft_panel_dc_reset_painter_device_t st7789_drivers[ST7789_NUM_DEVICES] = {0};
  17. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. // Automatic viewport offsets
  19. #ifndef ST7789_NO_AUTOMATIC_OFFSETS
  20. static inline void st7789_automatic_viewport_offsets(painter_device_t device, painter_rotation_t rotation) {
  21. struct painter_driver_t *driver = (struct painter_driver_t *)device;
  22. // clang-format off
  23. const struct {
  24. uint16_t offset_x;
  25. uint16_t offset_y;
  26. } rotation_offsets_240x240[] = {
  27. [QP_ROTATION_0] = { .offset_x = 0, .offset_y = 0 },
  28. [QP_ROTATION_90] = { .offset_x = 0, .offset_y = 0 },
  29. [QP_ROTATION_180] = { .offset_x = 0, .offset_y = 80 },
  30. [QP_ROTATION_270] = { .offset_x = 80, .offset_y = 0 },
  31. };
  32. // clang-format on
  33. if (driver->panel_width == 240 && driver->panel_height == 240) {
  34. driver->offset_x = rotation_offsets_240x240[rotation].offset_x;
  35. driver->offset_y = rotation_offsets_240x240[rotation].offset_y;
  36. }
  37. }
  38. #endif // ST7789_NO_AUTOMATIC_OFFSETS
  39. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. // Initialization
  41. bool qp_st7789_init(painter_device_t device, painter_rotation_t rotation) {
  42. // clang-format off
  43. const uint8_t st7789_init_sequence[] = {
  44. // Command, Delay, N, Data[N]
  45. ST77XX_CMD_RESET, 120, 0,
  46. ST77XX_CMD_SLEEP_OFF, 5, 0,
  47. ST77XX_SET_PIX_FMT, 0, 1, 0x55,
  48. ST77XX_CMD_INVERT_ON, 0, 0,
  49. ST77XX_CMD_NORMAL_ON, 0, 0,
  50. ST77XX_CMD_DISPLAY_ON, 20, 0
  51. };
  52. // clang-format on
  53. qp_comms_bulk_command_sequence(device, st7789_init_sequence, sizeof(st7789_init_sequence));
  54. // Configure the rotation (i.e. the ordering and direction of memory writes in GRAM)
  55. const uint8_t madctl[] = {
  56. [QP_ROTATION_0] = ST77XX_MADCTL_RGB,
  57. [QP_ROTATION_90] = ST77XX_MADCTL_RGB | ST77XX_MADCTL_MX | ST77XX_MADCTL_MV,
  58. [QP_ROTATION_180] = ST77XX_MADCTL_RGB | ST77XX_MADCTL_MX | ST77XX_MADCTL_MY,
  59. [QP_ROTATION_270] = ST77XX_MADCTL_RGB | ST77XX_MADCTL_MV | ST77XX_MADCTL_MY,
  60. };
  61. qp_comms_command_databyte(device, ST77XX_SET_MADCTL, madctl[rotation]);
  62. #ifndef ST7789_NO_AUTOMATIC_VIEWPORT_OFFSETS
  63. st7789_automatic_viewport_offsets(device, rotation);
  64. #endif // ST7789_NO_AUTOMATIC_VIEWPORT_OFFSETS
  65. return true;
  66. }
  67. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. // Driver vtable
  69. const struct tft_panel_dc_reset_painter_driver_vtable_t st7789_driver_vtable = {
  70. .base =
  71. {
  72. .init = qp_st7789_init,
  73. .power = qp_tft_panel_power,
  74. .clear = qp_tft_panel_clear,
  75. .flush = qp_tft_panel_flush,
  76. .pixdata = qp_tft_panel_pixdata,
  77. .viewport = qp_tft_panel_viewport,
  78. .palette_convert = qp_tft_panel_palette_convert,
  79. .append_pixels = qp_tft_panel_append_pixels,
  80. },
  81. .rgb888_to_native16bit = qp_rgb888_to_rgb565_swapped,
  82. .num_window_bytes = 2,
  83. .swap_window_coords = false,
  84. .opcodes =
  85. {
  86. .display_on = ST77XX_CMD_DISPLAY_ON,
  87. .display_off = ST77XX_CMD_DISPLAY_OFF,
  88. .set_column_address = ST77XX_SET_COL_ADDR,
  89. .set_row_address = ST77XX_SET_ROW_ADDR,
  90. .enable_writes = ST77XX_SET_MEM,
  91. },
  92. };
  93. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  94. // SPI
  95. #ifdef QUANTUM_PAINTER_ST7789_SPI_ENABLE
  96. // Factory function for creating a handle to the ST7789 device
  97. 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) {
  98. for (uint32_t i = 0; i < ST7789_NUM_DEVICES; ++i) {
  99. tft_panel_dc_reset_painter_device_t *driver = &st7789_drivers[i];
  100. if (!driver->base.driver_vtable) {
  101. driver->base.driver_vtable = (const struct painter_driver_vtable_t *)&st7789_driver_vtable;
  102. driver->base.comms_vtable = (const struct painter_comms_vtable_t *)&spi_comms_with_dc_vtable;
  103. driver->base.panel_width = panel_width;
  104. driver->base.panel_height = panel_height;
  105. driver->base.rotation = QP_ROTATION_0;
  106. driver->base.offset_x = 0;
  107. driver->base.offset_y = 0;
  108. driver->base.native_bits_per_pixel = 16; // RGB565
  109. // SPI and other pin configuration
  110. driver->base.comms_config = &driver->spi_dc_reset_config;
  111. driver->spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin;
  112. driver->spi_dc_reset_config.spi_config.divisor = spi_divisor;
  113. driver->spi_dc_reset_config.spi_config.lsb_first = false;
  114. driver->spi_dc_reset_config.spi_config.mode = spi_mode;
  115. driver->spi_dc_reset_config.dc_pin = dc_pin;
  116. driver->spi_dc_reset_config.reset_pin = reset_pin;
  117. return (painter_device_t)driver;
  118. }
  119. }
  120. return NULL;
  121. }
  122. #endif // QUANTUM_PAINTER_ST7789_SPI_ENABLE
  123. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////