logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

qp_gc9107.c (5634B)


  1. // Copyright 2024 Fernando Birra
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "qp_internal.h"
  4. #include "qp_comms.h"
  5. #include "qp_gc9107.h"
  6. #include "qp_gc9xxx_opcodes.h"
  7. #include "qp_gc9107_opcodes.h"
  8. #include "qp_tft_panel.h"
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Driver storage
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  12. tft_panel_dc_reset_painter_device_t gc9107_drivers[GC9107_NUM_DEVICES] = {0};
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // Initialization
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. __attribute__((weak)) bool qp_gc9107_init(painter_device_t device, painter_rotation_t rotation) {
  17. // A lot of these "unknown" opcodes are sourced from other OSS projects and are seemingly required for this display to function.
  18. // clang-format off
  19. const uint8_t gc9107_init_sequence[] = {
  20. GC9XXX_SET_INTER_REG_ENABLE1, 5, 0,
  21. GC9XXX_SET_INTER_REG_ENABLE2, 5, 0,
  22. GC9107_SET_FUNCTION_CTL6, 0, 1, GC9107_ALLOW_SET_COMPLEMENT_RGB | 0x08 | GC9107_ALLOW_SET_FRAMERATE,
  23. GC9107_SET_COMPLEMENT_RGB, 0, 1, GC9107_COMPLEMENT_WITH_LSB,
  24. 0xAB, 0, 1, 0x0E,
  25. GC9107_SET_FRAME_RATE, 0, 1, 0x19,
  26. GC9XXX_SET_PIXEL_FORMAT, 0, 1, GC9107_PIXEL_FORMAT_16_BPP_IFPF,
  27. GC9XXX_CMD_SLEEP_OFF, 120, 0,
  28. GC9XXX_CMD_DISPLAY_ON, 20, 0
  29. };
  30. // clang-format on
  31. qp_comms_bulk_command_sequence(device, gc9107_init_sequence, sizeof(gc9107_init_sequence));
  32. // Configure the rotation (i.e. the ordering and direction of memory writes in GRAM)
  33. const uint8_t madctl[] = {
  34. [QP_ROTATION_0] = GC9XXX_MADCTL_BGR,
  35. [QP_ROTATION_90] = GC9XXX_MADCTL_BGR | GC9XXX_MADCTL_MX | GC9XXX_MADCTL_MV,
  36. [QP_ROTATION_180] = GC9XXX_MADCTL_BGR | GC9XXX_MADCTL_MX | GC9XXX_MADCTL_MY,
  37. [QP_ROTATION_270] = GC9XXX_MADCTL_BGR | GC9XXX_MADCTL_MV | GC9XXX_MADCTL_MY,
  38. };
  39. qp_comms_command_databyte(device, GC9XXX_SET_MEM_ACS_CTL, madctl[rotation]);
  40. return true;
  41. }
  42. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  43. // Driver vtable
  44. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. const tft_panel_dc_reset_painter_driver_vtable_t gc9107_driver_vtable = {
  46. .base =
  47. {
  48. .init = qp_gc9107_init,
  49. .power = qp_tft_panel_power,
  50. .clear = qp_tft_panel_clear,
  51. .flush = qp_tft_panel_flush,
  52. .pixdata = qp_tft_panel_pixdata,
  53. .viewport = qp_tft_panel_viewport,
  54. .palette_convert = qp_tft_panel_palette_convert_rgb565_swapped,
  55. .append_pixels = qp_tft_panel_append_pixels_rgb565,
  56. .append_pixdata = qp_tft_panel_append_pixdata,
  57. },
  58. .num_window_bytes = 2,
  59. .swap_window_coords = false,
  60. .opcodes =
  61. {
  62. .display_on = GC9XXX_CMD_DISPLAY_ON,
  63. .display_off = GC9XXX_CMD_DISPLAY_OFF,
  64. .set_column_address = GC9XXX_SET_COL_ADDR,
  65. .set_row_address = GC9XXX_SET_ROW_ADDR,
  66. .enable_writes = GC9XXX_SET_MEM,
  67. },
  68. };
  69. #ifdef QUANTUM_PAINTER_GC9107_SPI_ENABLE
  70. // Factory function for creating a handle to the GC9107 device
  71. painter_device_t qp_gc9107_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) {
  72. for (uint32_t i = 0; i < GC9107_NUM_DEVICES; ++i) {
  73. tft_panel_dc_reset_painter_device_t *driver = &gc9107_drivers[i];
  74. if (!driver->base.driver_vtable) {
  75. driver->base.driver_vtable = (const painter_driver_vtable_t *)&gc9107_driver_vtable;
  76. driver->base.comms_vtable = (const painter_comms_vtable_t *)&spi_comms_with_dc_vtable;
  77. driver->base.native_bits_per_pixel = 16; // RGB565
  78. driver->base.panel_width = panel_width;
  79. driver->base.panel_height = panel_height;
  80. driver->base.rotation = QP_ROTATION_0;
  81. driver->base.offset_x = 2;
  82. driver->base.offset_y = 1;
  83. // SPI and other pin configuration
  84. driver->base.comms_config = &driver->spi_dc_reset_config;
  85. driver->spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin;
  86. driver->spi_dc_reset_config.spi_config.divisor = spi_divisor;
  87. driver->spi_dc_reset_config.spi_config.lsb_first = false;
  88. driver->spi_dc_reset_config.spi_config.mode = spi_mode;
  89. driver->spi_dc_reset_config.dc_pin = dc_pin;
  90. driver->spi_dc_reset_config.reset_pin = reset_pin;
  91. driver->spi_dc_reset_config.command_params_uses_command_pin = false;
  92. if (!qp_internal_register_device((painter_device_t)driver)) {
  93. memset(driver, 0, sizeof(tft_panel_dc_reset_painter_device_t));
  94. return NULL;
  95. }
  96. return (painter_device_t)driver;
  97. }
  98. }
  99. return NULL;
  100. }
  101. #endif // QUANTUM_PAINTER_GC9107_SPI_ENABLE