qp_ili9488.c (6099B)
1 // Copyright 2021-2023 Nick Brassel (@tzarc) 2 // SPDX-License-Identifier: GPL-2.0-or-later 3 4 #include "qp_internal.h" 5 #include "qp_comms.h" 6 #include "qp_ili9488.h" 7 #include "qp_ili9xxx_opcodes.h" 8 #include "qp_tft_panel.h" 9 10 #ifdef QUANTUM_PAINTER_ILI9488_SPI_ENABLE 11 # include <qp_comms_spi.h> 12 #endif // QUANTUM_PAINTER_ILI9488_SPI_ENABLE 13 14 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 15 // Common 16 17 // Driver storage 18 tft_panel_dc_reset_painter_device_t ili9488_drivers[ILI9488_NUM_DEVICES] = {0}; 19 20 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 21 // Initialization 22 23 __attribute__((weak)) bool qp_ili9488_init(painter_device_t device, painter_rotation_t rotation) { 24 // clang-format off 25 const uint8_t ili9488_init_sequence[] = { 26 // Command, Delay, N, Data[N] 27 ILI9XXX_CMD_RESET, 120, 0, 28 ILI9XXX_SET_PGAMMA, 0, 15, 0x00, 0x03, 0x09, 0x08, 0x16, 0x0A, 0x3F, 0x78, 0x4C, 0x09, 0x0A, 0x08, 0x16, 0x1A, 0x0F, 29 ILI9XXX_SET_NGAMMA, 0, 15, 0x00, 0x16, 0x19, 0x03, 0x0F, 0x05, 0x32, 0x45, 0x46, 0x04, 0x0E, 0x0D, 0x35, 0x37, 0x0F, 30 ILI9XXX_SET_POWER_CTL_1, 0, 2, 0x17, 0x15, 31 ILI9XXX_SET_POWER_CTL_2, 0, 1, 0x41, 32 ILI9XXX_SET_VCOM_CTL_1, 0, 3, 0x00, 0x12, 0x80, 33 ILI9XXX_SET_PIX_FMT, 0, 1, 0x66, 34 ILI9XXX_SET_RGB_IF_SIG_CTL, 0, 1, 0x80, 35 ILI9XXX_SET_FRAME_CTL_NORMAL, 0, 1, 0xA0, 36 ILI9XXX_SET_INVERSION_CTL, 0, 1, 0x02, 37 ILI9XXX_SET_FUNCTION_CTL, 0, 2, 0x02, 0x02, 38 ILI9XXX_SET_IMAGE_FUNCTION, 0, 1, 0x00, 39 ILI9XXX_SET_PUMP_RATIO_CTL, 0, 4, 0xA9, 0x51, 0x2C, 0x82, 40 ILI9XXX_CMD_SLEEP_OFF, 5, 0, 41 ILI9XXX_CMD_DISPLAY_ON, 20, 0 42 }; 43 // clang-format on 44 if (!qp_comms_bulk_command_sequence(device, ili9488_init_sequence, sizeof(ili9488_init_sequence))) { 45 return false; 46 } 47 48 // Configure the rotation (i.e. the ordering and direction of memory writes in GRAM) 49 const uint8_t madctl[] = { 50 [QP_ROTATION_0] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MY, 51 [QP_ROTATION_90] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MX | ILI9XXX_MADCTL_MV | ILI9XXX_MADCTL_MY, 52 [QP_ROTATION_180] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MX, 53 [QP_ROTATION_270] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MV, 54 }; 55 return qp_comms_command_databyte(device, ILI9XXX_SET_MEM_ACS_CTL, madctl[rotation]); 56 } 57 58 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 59 // Driver vtable 60 61 const tft_panel_dc_reset_painter_driver_vtable_t ili9488_driver_vtable = { 62 .base = 63 { 64 .init = qp_ili9488_init, 65 .power = qp_tft_panel_power, 66 .clear = qp_tft_panel_clear, 67 .flush = qp_tft_panel_flush, 68 .pixdata = qp_tft_panel_pixdata, 69 .viewport = qp_tft_panel_viewport, 70 .palette_convert = qp_tft_panel_palette_convert_rgb888, 71 .append_pixels = qp_tft_panel_append_pixels_rgb888, 72 .append_pixdata = qp_tft_panel_append_pixdata, 73 }, 74 .num_window_bytes = 2, 75 .swap_window_coords = false, 76 .opcodes = 77 { 78 .display_on = ILI9XXX_CMD_DISPLAY_ON, 79 .display_off = ILI9XXX_CMD_DISPLAY_OFF, 80 .set_column_address = ILI9XXX_SET_COL_ADDR, 81 .set_row_address = ILI9XXX_SET_PAGE_ADDR, 82 .enable_writes = ILI9XXX_SET_MEM, 83 }, 84 }; 85 86 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 87 // SPI 88 89 #ifdef QUANTUM_PAINTER_ILI9488_SPI_ENABLE 90 91 // Factory function for creating a handle to the ILI9488 device 92 painter_device_t qp_ili9488_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) { 93 for (uint32_t i = 0; i < ILI9488_NUM_DEVICES; ++i) { 94 tft_panel_dc_reset_painter_device_t *driver = &ili9488_drivers[i]; 95 if (!driver->base.driver_vtable) { 96 driver->base.driver_vtable = (const painter_driver_vtable_t *)&ili9488_driver_vtable; 97 driver->base.comms_vtable = (const painter_comms_vtable_t *)&spi_comms_with_dc_vtable; 98 driver->base.native_bits_per_pixel = 24; // RGB888 99 driver->base.panel_width = panel_width; 100 driver->base.panel_height = panel_height; 101 driver->base.rotation = QP_ROTATION_0; 102 driver->base.offset_x = 0; 103 driver->base.offset_y = 0; 104 105 // SPI and other pin configuration 106 driver->base.comms_config = &driver->spi_dc_reset_config; 107 driver->spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin; 108 driver->spi_dc_reset_config.spi_config.divisor = spi_divisor; 109 driver->spi_dc_reset_config.spi_config.lsb_first = false; 110 driver->spi_dc_reset_config.spi_config.mode = spi_mode; 111 driver->spi_dc_reset_config.dc_pin = dc_pin; 112 driver->spi_dc_reset_config.reset_pin = reset_pin; 113 driver->spi_dc_reset_config.command_params_uses_command_pin = false; 114 115 if (!qp_internal_register_device((painter_device_t)driver)) { 116 memset(driver, 0, sizeof(tft_panel_dc_reset_painter_device_t)); 117 return NULL; 118 } 119 120 return (painter_device_t)driver; 121 } 122 } 123 return NULL; 124 } 125 126 #endif // QUANTUM_PAINTER_ILI9488_SPI_ENABLE 127 128 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////