qp_ssd1351.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2021-2023 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "qp_internal.h"
  4. #include "qp_comms.h"
  5. #include "qp_ssd1351.h"
  6. #include "qp_ssd1351_opcodes.h"
  7. #include "qp_tft_panel.h"
  8. #ifdef QUANTUM_PAINTER_SSD1351_SPI_ENABLE
  9. # include "qp_comms_spi.h"
  10. #endif // QUANTUM_PAINTER_SSD1351_SPI_ENABLE
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  12. // Common
  13. // Driver storage
  14. tft_panel_dc_reset_painter_device_t ssd1351_drivers[SSD1351_NUM_DEVICES] = {0};
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. // Initialization
  17. __attribute__((weak)) bool qp_ssd1351_init(painter_device_t device, painter_rotation_t rotation) {
  18. tft_panel_dc_reset_painter_device_t *driver = (tft_panel_dc_reset_painter_device_t *)device;
  19. // clang-format off
  20. const uint8_t ssd1351_init_sequence[] = {
  21. // Command, Delay, N, Data[N]
  22. SSD1351_COMMANDLOCK, 5, 1, 0x12,
  23. SSD1351_COMMANDLOCK, 5, 1, 0xB1,
  24. SSD1351_DISPLAYOFF, 5, 0,
  25. SSD1351_CLOCKDIV, 5, 1, 0xF1,
  26. SSD1351_MUXRATIO, 5, 1, 0x7F,
  27. SSD1351_DISPLAYOFFSET, 5, 1, 0x00,
  28. SSD1351_SETGPIO, 5, 1, 0x00,
  29. SSD1351_FUNCTIONSELECT, 5, 1, 0x01,
  30. SSD1351_PRECHARGE, 5, 1, 0x32,
  31. SSD1351_VCOMH, 5, 1, 0x05,
  32. SSD1351_NORMALDISPLAY, 5, 0,
  33. SSD1351_CONTRASTABC, 5, 3, 0xC8, 0x80, 0xC8,
  34. SSD1351_CONTRASTMASTER, 5, 1, 0x0F,
  35. SSD1351_SETVSL, 5, 3, 0xA0, 0xB5, 0x55,
  36. SSD1351_PRECHARGE2, 5, 1, 0x01,
  37. SSD1351_DISPLAYON, 5, 0,
  38. };
  39. // clang-format on
  40. if (!qp_comms_bulk_command_sequence(device, ssd1351_init_sequence, sizeof(ssd1351_init_sequence))) {
  41. return false;
  42. }
  43. // Configure the rotation (i.e. the ordering and direction of memory writes in GRAM)
  44. const uint8_t madctl[] = {
  45. [QP_ROTATION_0] = SSD1351_MADCTL_BGR | SSD1351_MADCTL_MY,
  46. [QP_ROTATION_90] = SSD1351_MADCTL_BGR | SSD1351_MADCTL_MX | SSD1351_MADCTL_MY | SSD1351_MADCTL_MV,
  47. [QP_ROTATION_180] = SSD1351_MADCTL_BGR | SSD1351_MADCTL_MX,
  48. [QP_ROTATION_270] = SSD1351_MADCTL_BGR | SSD1351_MADCTL_MV,
  49. };
  50. if (!qp_comms_command_databyte(device, SSD1351_SETREMAP, madctl[rotation])) {
  51. return false;
  52. }
  53. return qp_comms_command_databyte(device, SSD1351_STARTLINE, (rotation == QP_ROTATION_0 || rotation == QP_ROTATION_90) ? driver->base.panel_height : 0);
  54. }
  55. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. // Driver vtable
  57. const tft_panel_dc_reset_painter_driver_vtable_t ssd1351_driver_vtable = {
  58. .base =
  59. {
  60. .init = qp_ssd1351_init,
  61. .power = qp_tft_panel_power,
  62. .clear = qp_tft_panel_clear,
  63. .flush = qp_tft_panel_flush,
  64. .pixdata = qp_tft_panel_pixdata,
  65. .viewport = qp_tft_panel_viewport,
  66. .palette_convert = qp_tft_panel_palette_convert_rgb565_swapped,
  67. .append_pixels = qp_tft_panel_append_pixels_rgb565,
  68. .append_pixdata = qp_tft_panel_append_pixdata,
  69. },
  70. .num_window_bytes = 1,
  71. .swap_window_coords = true,
  72. .opcodes =
  73. {
  74. .display_on = SSD1351_DISPLAYON,
  75. .display_off = SSD1351_DISPLAYOFF,
  76. .set_column_address = SSD1351_SETCOLUMN,
  77. .set_row_address = SSD1351_SETROW,
  78. .enable_writes = SSD1351_WRITERAM,
  79. },
  80. };
  81. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. // SPI
  83. #ifdef QUANTUM_PAINTER_SSD1351_SPI_ENABLE
  84. // Factory function for creating a handle to the SSD1351 device
  85. painter_device_t qp_ssd1351_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) {
  86. for (uint32_t i = 0; i < SSD1351_NUM_DEVICES; ++i) {
  87. tft_panel_dc_reset_painter_device_t *driver = &ssd1351_drivers[i];
  88. if (!driver->base.driver_vtable) {
  89. driver->base.driver_vtable = (const painter_driver_vtable_t *)&ssd1351_driver_vtable;
  90. driver->base.comms_vtable = (const painter_comms_vtable_t *)&spi_comms_with_dc_vtable;
  91. driver->base.panel_width = panel_width;
  92. driver->base.panel_height = panel_height;
  93. driver->base.rotation = QP_ROTATION_0;
  94. driver->base.offset_x = 0;
  95. driver->base.offset_y = 0;
  96. driver->base.native_bits_per_pixel = 16; // RGB565
  97. // SPI and other pin configuration
  98. driver->base.comms_config = &driver->spi_dc_reset_config;
  99. driver->spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin;
  100. driver->spi_dc_reset_config.spi_config.divisor = spi_divisor;
  101. driver->spi_dc_reset_config.spi_config.lsb_first = false;
  102. driver->spi_dc_reset_config.spi_config.mode = spi_mode;
  103. driver->spi_dc_reset_config.dc_pin = dc_pin;
  104. driver->spi_dc_reset_config.reset_pin = reset_pin;
  105. driver->spi_dc_reset_config.command_params_uses_command_pin = false;
  106. if (!qp_internal_register_device((painter_device_t)driver)) {
  107. memset(driver, 0, sizeof(tft_panel_dc_reset_painter_device_t));
  108. return NULL;
  109. }
  110. return (painter_device_t)driver;
  111. }
  112. }
  113. return NULL;
  114. }
  115. #endif // QUANTUM_PAINTER_SSD1351_SPI_ENABLE
  116. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////