qp_ili9341.c (6520B)
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_ili9341.h" 7 #include "qp_ili9xxx_opcodes.h" 8 #include "qp_tft_panel.h" 9 10 #ifdef QUANTUM_PAINTER_ILI9341_SPI_ENABLE 11 # include <qp_comms_spi.h> 12 #endif // QUANTUM_PAINTER_ILI9341_SPI_ENABLE 13 14 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 15 // Common 16 17 // Driver storage 18 tft_panel_dc_reset_painter_device_t ili9341_drivers[ILI9341_NUM_DEVICES] = {0}; 19 20 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 21 // Initialization 22 23 __attribute__((weak)) bool qp_ili9341_init(painter_device_t device, painter_rotation_t rotation) { 24 // clang-format off 25 const uint8_t ili9341_init_sequence[] = { 26 // Command, Delay, N, Data[N] 27 ILI9XXX_CMD_RESET, 120, 0, 28 ILI9XXX_CMD_SLEEP_OFF, 5, 0, 29 ILI9XXX_POWER_CTL_A, 0, 5, 0x39, 0x2C, 0x00, 0x34, 0x02, 30 ILI9XXX_POWER_CTL_B, 0, 3, 0x00, 0xD9, 0x30, 31 ILI9XXX_POWER_ON_SEQ_CTL, 0, 4, 0x64, 0x03, 0x12, 0x81, 32 ILI9XXX_SET_PUMP_RATIO_CTL, 0, 1, 0x20, 33 ILI9XXX_SET_POWER_CTL_1, 0, 1, 0x26, 34 ILI9XXX_SET_POWER_CTL_2, 0, 1, 0x11, 35 ILI9XXX_SET_VCOM_CTL_1, 0, 2, 0x35, 0x3E, 36 ILI9XXX_SET_VCOM_CTL_2, 0, 1, 0xBE, 37 ILI9XXX_DRV_TIMING_CTL_A, 0, 3, 0x85, 0x10, 0x7A, 38 ILI9XXX_DRV_TIMING_CTL_B, 0, 2, 0x00, 0x00, 39 ILI9XXX_SET_BRIGHTNESS, 0, 1, 0xFF, 40 ILI9XXX_ENABLE_3_GAMMA, 0, 1, 0x00, 41 ILI9XXX_SET_GAMMA, 0, 1, 0x01, 42 ILI9XXX_SET_PGAMMA, 0, 15, 0x0F, 0x29, 0x24, 0x0C, 0x0E, 0x09, 0x4E, 0x78, 0x3C, 0x09, 0x13, 0x05, 0x17, 0x11, 0x00, 43 ILI9XXX_SET_NGAMMA, 0, 15, 0x00, 0x16, 0x1B, 0x04, 0x11, 0x07, 0x31, 0x33, 0x42, 0x05, 0x0C, 0x0A, 0x28, 0x2F, 0x0F, 44 ILI9XXX_SET_PIX_FMT, 0, 1, 0x05, 45 ILI9XXX_SET_FRAME_CTL_NORMAL, 0, 2, 0x00, 0x1B, 46 ILI9XXX_SET_FUNCTION_CTL, 0, 2, 0x0A, 0xA2, 47 ILI9XXX_CMD_PARTIAL_OFF, 0, 0, 48 ILI9XXX_CMD_DISPLAY_ON, 20, 0 49 }; 50 // clang-format on 51 if (!qp_comms_bulk_command_sequence(device, ili9341_init_sequence, sizeof(ili9341_init_sequence))) { 52 return false; 53 } 54 55 // Configure the rotation (i.e. the ordering and direction of memory writes in GRAM) 56 const uint8_t madctl[] = { 57 [QP_ROTATION_0] = ILI9XXX_MADCTL_BGR, 58 [QP_ROTATION_90] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MX | ILI9XXX_MADCTL_MV, 59 [QP_ROTATION_180] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MX | ILI9XXX_MADCTL_MY, 60 [QP_ROTATION_270] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MV | ILI9XXX_MADCTL_MY, 61 }; 62 return qp_comms_command_databyte(device, ILI9XXX_SET_MEM_ACS_CTL, madctl[rotation]); 63 } 64 65 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 66 // Driver vtable 67 68 const tft_panel_dc_reset_painter_driver_vtable_t ili9341_driver_vtable = { 69 .base = 70 { 71 .init = qp_ili9341_init, 72 .power = qp_tft_panel_power, 73 .clear = qp_tft_panel_clear, 74 .flush = qp_tft_panel_flush, 75 .pixdata = qp_tft_panel_pixdata, 76 .viewport = qp_tft_panel_viewport, 77 .palette_convert = qp_tft_panel_palette_convert_rgb565_swapped, 78 .append_pixels = qp_tft_panel_append_pixels_rgb565, 79 .append_pixdata = qp_tft_panel_append_pixdata, 80 }, 81 .num_window_bytes = 2, 82 .swap_window_coords = false, 83 .opcodes = 84 { 85 .display_on = ILI9XXX_CMD_DISPLAY_ON, 86 .display_off = ILI9XXX_CMD_DISPLAY_OFF, 87 .set_column_address = ILI9XXX_SET_COL_ADDR, 88 .set_row_address = ILI9XXX_SET_PAGE_ADDR, 89 .enable_writes = ILI9XXX_SET_MEM, 90 }, 91 }; 92 93 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 94 // SPI 95 96 #ifdef QUANTUM_PAINTER_ILI9341_SPI_ENABLE 97 98 // Factory function for creating a handle to the ILI9341 device 99 painter_device_t qp_ili9341_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) { 100 for (uint32_t i = 0; i < ILI9341_NUM_DEVICES; ++i) { 101 tft_panel_dc_reset_painter_device_t *driver = &ili9341_drivers[i]; 102 if (!driver->base.driver_vtable) { 103 driver->base.driver_vtable = (const painter_driver_vtable_t *)&ili9341_driver_vtable; 104 driver->base.comms_vtable = (const painter_comms_vtable_t *)&spi_comms_with_dc_vtable; 105 driver->base.native_bits_per_pixel = 16; // RGB565 106 driver->base.panel_width = panel_width; 107 driver->base.panel_height = panel_height; 108 driver->base.rotation = QP_ROTATION_0; 109 driver->base.offset_x = 0; 110 driver->base.offset_y = 0; 111 112 // SPI and other pin configuration 113 driver->base.comms_config = &driver->spi_dc_reset_config; 114 driver->spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin; 115 driver->spi_dc_reset_config.spi_config.divisor = spi_divisor; 116 driver->spi_dc_reset_config.spi_config.lsb_first = false; 117 driver->spi_dc_reset_config.spi_config.mode = spi_mode; 118 driver->spi_dc_reset_config.dc_pin = dc_pin; 119 driver->spi_dc_reset_config.reset_pin = reset_pin; 120 driver->spi_dc_reset_config.command_params_uses_command_pin = false; 121 122 if (!qp_internal_register_device((painter_device_t)driver)) { 123 memset(driver, 0, sizeof(tft_panel_dc_reset_painter_device_t)); 124 return NULL; 125 } 126 127 return (painter_device_t)driver; 128 } 129 } 130 return NULL; 131 } 132 133 #endif // QUANTUM_PAINTER_ILI9341_SPI_ENABLE 134 135 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////