You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
5.7 KiB

  1. // Copyright 2021 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. 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. qp_comms_bulk_command_sequence(device, ssd1351_init_sequence, sizeof(ssd1351_init_sequence));
  41. // Configure the rotation (i.e. the ordering and direction of memory writes in GRAM)
  42. const uint8_t madctl[] = {
  43. [QP_ROTATION_0] = SSD1351_MADCTL_BGR | SSD1351_MADCTL_MY,
  44. [QP_ROTATION_90] = SSD1351_MADCTL_BGR | SSD1351_MADCTL_MX | SSD1351_MADCTL_MY | SSD1351_MADCTL_MV,
  45. [QP_ROTATION_180] = SSD1351_MADCTL_BGR | SSD1351_MADCTL_MX,
  46. [QP_ROTATION_270] = SSD1351_MADCTL_BGR | SSD1351_MADCTL_MV,
  47. };
  48. qp_comms_command_databyte(device, SSD1351_SETREMAP, madctl[rotation]);
  49. qp_comms_command_databyte(device, SSD1351_STARTLINE, (rotation == QP_ROTATION_0 || rotation == QP_ROTATION_90) ? driver->base.panel_height : 0);
  50. return true;
  51. }
  52. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. // Driver vtable
  54. const struct tft_panel_dc_reset_painter_driver_vtable_t ssd1351_driver_vtable = {
  55. .base =
  56. {
  57. .init = qp_ssd1351_init,
  58. .power = qp_tft_panel_power,
  59. .clear = qp_tft_panel_clear,
  60. .flush = qp_tft_panel_flush,
  61. .pixdata = qp_tft_panel_pixdata,
  62. .viewport = qp_tft_panel_viewport,
  63. .palette_convert = qp_tft_panel_palette_convert,
  64. .append_pixels = qp_tft_panel_append_pixels,
  65. },
  66. .rgb888_to_native16bit = qp_rgb888_to_rgb565_swapped,
  67. .num_window_bytes = 1,
  68. .swap_window_coords = true,
  69. .opcodes =
  70. {
  71. .display_on = SSD1351_DISPLAYON,
  72. .display_off = SSD1351_DISPLAYOFF,
  73. .set_column_address = SSD1351_SETCOLUMN,
  74. .set_row_address = SSD1351_SETROW,
  75. .enable_writes = SSD1351_WRITERAM,
  76. },
  77. };
  78. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  79. // SPI
  80. #ifdef QUANTUM_PAINTER_SSD1351_SPI_ENABLE
  81. // Factory function for creating a handle to the SSD1351 device
  82. 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) {
  83. for (uint32_t i = 0; i < SSD1351_NUM_DEVICES; ++i) {
  84. tft_panel_dc_reset_painter_device_t *driver = &ssd1351_drivers[i];
  85. if (!driver->base.driver_vtable) {
  86. driver->base.driver_vtable = (const struct painter_driver_vtable_t *)&ssd1351_driver_vtable;
  87. driver->base.comms_vtable = (const struct painter_comms_vtable_t *)&spi_comms_with_dc_vtable;
  88. driver->base.panel_width = panel_width;
  89. driver->base.panel_height = panel_height;
  90. driver->base.rotation = QP_ROTATION_0;
  91. driver->base.offset_x = 0;
  92. driver->base.offset_y = 0;
  93. driver->base.native_bits_per_pixel = 16; // RGB565
  94. // SPI and other pin configuration
  95. driver->base.comms_config = &driver->spi_dc_reset_config;
  96. driver->spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin;
  97. driver->spi_dc_reset_config.spi_config.divisor = spi_divisor;
  98. driver->spi_dc_reset_config.spi_config.lsb_first = false;
  99. driver->spi_dc_reset_config.spi_config.mode = spi_mode;
  100. driver->spi_dc_reset_config.dc_pin = dc_pin;
  101. driver->spi_dc_reset_config.reset_pin = reset_pin;
  102. return (painter_device_t)driver;
  103. }
  104. }
  105. return NULL;
  106. }
  107. #endif // QUANTUM_PAINTER_SSD1351_SPI_ENABLE
  108. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////