|
| 1 | +/** |
| 2 | + * @file ILI9163C.c |
| 3 | + * |
| 4 | + */ |
| 5 | + |
| 6 | +/********************* |
| 7 | + * INCLUDES |
| 8 | + *********************/ |
| 9 | +#include "ili9163c.h" |
| 10 | +#include "disp_spi.h" |
| 11 | +#include "driver/gpio.h" |
| 12 | +#include "esp_log.h" |
| 13 | +#include "freertos/FreeRTOS.h" |
| 14 | +#include "freertos/task.h" |
| 15 | +#include "assert.h" |
| 16 | + |
| 17 | +/********************* |
| 18 | + * DEFINES |
| 19 | + *********************/ |
| 20 | +#define TAG "ILI9163C" |
| 21 | + |
| 22 | +// ILI9163C specific commands used in init |
| 23 | +#define ILI9163C_NOP 0x00 |
| 24 | +#define ILI9163C_SWRESET 0x01 |
| 25 | +#define ILI9163C_RDDID 0x04 |
| 26 | +#define ILI9163C_RDDST 0x09 |
| 27 | + |
| 28 | +#define ILI9163C_SLPIN 0x10 |
| 29 | +#define ILI9163C_SLPOUT 0x11 |
| 30 | +#define ILI9163C_PTLON 0x12 |
| 31 | +#define ILI9163C_NORON 0x13 |
| 32 | + |
| 33 | +#define ILI9163C_INVOFF 0x20 |
| 34 | +#define ILI9163C_INVON 0x21 |
| 35 | +#define ILI9163C_CMD_GAMST 0x26 |
| 36 | +#define ILI9163C_DISPOFF 0x28 |
| 37 | +#define ILI9163C_DISPON 0x29 |
| 38 | +#define ILI9163C_CASET 0x2A |
| 39 | +#define ILI9163C_RASET 0x2B |
| 40 | +#define ILI9163C_RAMWR 0x2C |
| 41 | +#define ILI9163C_COLORSET 0x2D |
| 42 | +#define ILI9163C_RAMRD 0x2E |
| 43 | + |
| 44 | +#define ILI9163C_PTLAR 0x30 |
| 45 | +#define ILI9163C_VSCRDEF 0x33 |
| 46 | +#define ILI9163C_COLMOD 0x3A |
| 47 | +#define ILI9163C_MADCTL 0x36 |
| 48 | +#define ILI9163C_VSCRSADD 0x37 |
| 49 | + |
| 50 | +#define ILI9163C_FRMCTR1 0xB1 |
| 51 | +#define ILI9163C_FRMCTR2 0xB2 |
| 52 | +#define ILI9163C_FRMCTR3 0xB3 |
| 53 | +#define ILI9163C_INVCTR 0xB4 |
| 54 | +#define ILI9163C_DISSET5 0xB6 |
| 55 | +#define ILI9163C_SDDC 0xB7 |
| 56 | + |
| 57 | +#define ILI9163C_PWCTR1 0xC0 |
| 58 | +#define ILI9163C_PWCTR2 0xC1 |
| 59 | +#define ILI9163C_PWCTR3 0xC2 |
| 60 | +#define ILI9163C_PWCTR4 0xC3 |
| 61 | +#define ILI9163C_PWCTR5 0xC4 |
| 62 | +#define ILI9163C_VMCTR1 0xC5 |
| 63 | +#define ILI9163C_VMCOFFS 0xC7 |
| 64 | + |
| 65 | +#define ILI9163C_GAMCTL 0xF2 |
| 66 | + |
| 67 | +#define ILI9163C_GMCTRP1 0xE0 |
| 68 | +#define ILI9163C_GMCTRN1 0xE1 |
| 69 | + |
| 70 | +#define ST77XX_MADCTL_MY 0x80 |
| 71 | +#define ST77XX_MADCTL_MX 0x40 |
| 72 | +#define ST77XX_MADCTL_MV 0x20 #define |
| 73 | +#define ST77XX_MADCTL_ML 0x10 |
| 74 | +#define ST77XX_MADCTL_RGB 0x00 |
| 75 | +#define ST77XX_MADCTL_BGR 0x08 |
| 76 | + |
| 77 | +/********************** |
| 78 | + * TYPEDEFS |
| 79 | + **********************/ |
| 80 | + |
| 81 | +/*The LCD needs a bunch of command/argument values to be initialized. They are stored in this struct. */ |
| 82 | +typedef struct |
| 83 | +{ |
| 84 | + uint8_t cmd; |
| 85 | + uint8_t data[16]; |
| 86 | + uint8_t databytes; //No of data in data; bit 7 = delay after set; 0xFF = end of cmds. |
| 87 | +} lcd_init_cmd_t; |
| 88 | + |
| 89 | +/********************** |
| 90 | + * STATIC PROTOTYPES |
| 91 | + **********************/ |
| 92 | +static void ili9163c_set_orientation(uint8_t orientation); |
| 93 | + |
| 94 | +static void ili9163c_send_cmd(uint8_t cmd); |
| 95 | +static void ili9163c_send_data(void *data, uint16_t length); |
| 96 | +static void ili9163c_send_color(void *data, uint16_t length); |
| 97 | + |
| 98 | +/********************** |
| 99 | + * STATIC VARIABLES |
| 100 | + **********************/ |
| 101 | + |
| 102 | +/********************** |
| 103 | + * MACROS |
| 104 | + **********************/ |
| 105 | + |
| 106 | +/********************** |
| 107 | + * GLOBAL FUNCTIONS |
| 108 | + **********************/ |
| 109 | + |
| 110 | +void ili9163c_init(void) |
| 111 | +{ |
| 112 | + ESP_LOGD(TAG, "Init"); |
| 113 | + |
| 114 | + lcd_init_cmd_t ili_init_cmds[] = { |
| 115 | + {ILI9163C_SWRESET, {0}, 0x80}, // Software reset, 0 args, w/delay 120ms |
| 116 | + {ILI9163C_SLPOUT, {0}, 0x80}, // Out of sleep mode, 0 args, w/delay 5ms |
| 117 | + {ILI9163C_CMD_GAMST, {0x04}, 1}, // Gamma Curve |
| 118 | + {ILI9163C_FRMCTR1, {0x0C, 0x14}, 2}, // Frame rate ctrl - normal mode |
| 119 | + {ILI9163C_INVCTR, {0x07}, 1}, // Display inversion ctrl, 1 arg, no delay:No inversion |
| 120 | + {ILI9163C_PWCTR1, {0x0C, 0x05}, 2}, // Power control, 2 args, no delay |
| 121 | + {ILI9163C_PWCTR2, {0x02}, 1}, // Power control, 1 arg |
| 122 | + {ILI9163C_PWCTR3, {0x02}, 1}, // Power control, 1 arg |
| 123 | + {ILI9163C_VMCTR1, {0x20, 0x55}, 2}, // Power control, 1 arg, no delay: |
| 124 | + {ILI9163C_VMCOFFS, {0x40}, 1}, // VCOM Offset |
| 125 | +#if ILI9163C_INVERT_COLORS == 1 |
| 126 | + {ILI9163C_INVON, {0}, 0}, // set inverted mode |
| 127 | +#else |
| 128 | + {ILI9163C_INVOFF, {0}, 0}, // set non-inverted mode |
| 129 | +#endif |
| 130 | + {ILI9163C_COLMOD, {0x5}, 1}, // set color mode, 1 arg, no delay: 16-bit color |
| 131 | + {ILI9163C_SDDC, {0}, 1}, // set source driver direction control |
| 132 | + {ILI9163C_GAMCTL, {0x01}, 1}, // set source driver direction control |
| 133 | + {ILI9163C_GMCTRP1, {0x36, 0x29, 0x12, 0x22, 0x1C, 0x15, 0x42, 0xB7, 0x2F, 0x13, 0x12, 0x0A, 0x11, 0x0B, 0x06}, 16}, // 16 args, no delay: |
| 134 | + {ILI9163C_GMCTRN1, {0x09, 0x16, 0x2D, 0x0D, 0x13, 0x15, 0x40, 0x48, 0x53, 0x0C, 0x1D, 0x25, 0x2E, 0x34, 0x39}, 16}, // 16 args, no delay: |
| 135 | + {ILI9163C_NORON, {0}, 0x80}, // Normal display on, no args, w/delay 10 ms delay |
| 136 | + {ILI9163C_DISPON, {0}, 0x80}, // Main screen turn on, no args w/delay 100 ms delay |
| 137 | + {0, {0}, 0xff} |
| 138 | + }; |
| 139 | + |
| 140 | + //Initialize non-SPI GPIOs |
| 141 | + gpio_pad_select_gpio(ILI9163C_DC); |
| 142 | + gpio_set_direction(ILI9163C_DC, GPIO_MODE_OUTPUT); |
| 143 | + gpio_pad_select_gpio(ILI9163C_RST); |
| 144 | + gpio_set_direction(ILI9163C_RST, GPIO_MODE_OUTPUT); |
| 145 | + |
| 146 | +#if ILI9163C_ENABLE_BACKLIGHT_CONTROL |
| 147 | + gpio_pad_select_gpio(ILI9163C_BCKL); |
| 148 | + gpio_set_direction(ILI9163C_BCKL, GPIO_MODE_OUTPUT); |
| 149 | +#endif |
| 150 | + //Reset the display |
| 151 | + gpio_set_level(ILI9163C_RST, 0); |
| 152 | + vTaskDelay(100 / portTICK_RATE_MS); |
| 153 | + gpio_set_level(ILI9163C_RST, 1); |
| 154 | + vTaskDelay(150 / portTICK_RATE_MS); |
| 155 | + |
| 156 | + //Send all the commands |
| 157 | + uint16_t cmd = 0; |
| 158 | + while (ili_init_cmds[cmd].databytes != 0xff) |
| 159 | + { |
| 160 | + ili9163c_send_cmd(ili_init_cmds[cmd].cmd); |
| 161 | + ili9163c_send_data(ili_init_cmds[cmd].data, ili_init_cmds[cmd].databytes & 0x1F); |
| 162 | + if (ili_init_cmds[cmd].databytes & 0x80) |
| 163 | + { |
| 164 | + vTaskDelay(150 / portTICK_RATE_MS); |
| 165 | + } |
| 166 | + cmd++; |
| 167 | + } |
| 168 | + |
| 169 | + ili9163c_enable_backlight(true); |
| 170 | + |
| 171 | + ili9163c_set_orientation(CONFIG_LV_DISPLAY_ORIENTATION); |
| 172 | +} |
| 173 | + |
| 174 | +void ili9163c_flush(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map) |
| 175 | +{ |
| 176 | + uint8_t data[4]; |
| 177 | + |
| 178 | + /*Column addresses*/ |
| 179 | + ili9163c_send_cmd(ILI9163C_CASET); |
| 180 | + data[0] = (area->x1 >> 8) & 0xFF; |
| 181 | + data[1] = area->x1 & 0xFF; |
| 182 | + data[2] = (area->x2 >> 8) & 0xFF; |
| 183 | + data[3] = area->x2 & 0xFF; |
| 184 | + ili9163c_send_data(data, 4); |
| 185 | + |
| 186 | + /*Page addresses*/ |
| 187 | + ili9163c_send_cmd(ILI9163C_RASET); |
| 188 | + data[0] = (area->y1 >> 8) & 0xFF; |
| 189 | + data[1] = area->y1 & 0xFF; |
| 190 | + data[2] = (area->y2 >> 8) & 0xFF; |
| 191 | + data[3] = area->y2 & 0xFF; |
| 192 | + ili9163c_send_data(data, 4); |
| 193 | + |
| 194 | + /*Memory write*/ |
| 195 | + ili9163c_send_cmd(ILI9163C_RAMWR); |
| 196 | + |
| 197 | + uint32_t size = lv_area_get_width(area) * lv_area_get_height(area); |
| 198 | + |
| 199 | + ili9163c_send_color((void *)color_map, size * 2); |
| 200 | +} |
| 201 | + |
| 202 | +void ili9163c_enable_backlight(bool backlight) |
| 203 | +{ |
| 204 | +#if ILI9163C_ENABLE_BACKLIGHT_CONTROL |
| 205 | + ESP_LOGD(TAG, "%s backlight.", backlight ? "Enabling" : "Disabling"); |
| 206 | + uint32_t tmp = 0; |
| 207 | + |
| 208 | +#if (ILI9163C_BCKL_ACTIVE_LVL == 1) |
| 209 | + tmp = backlight ? 1 : 0; |
| 210 | +#else |
| 211 | + tmp = backlight ? 0 : 1; |
| 212 | +#endif |
| 213 | + |
| 214 | + gpio_set_level(ILI9163C_BCKL, tmp); |
| 215 | +#endif |
| 216 | +} |
| 217 | + |
| 218 | +void ili9163c_sleep_in() |
| 219 | +{ |
| 220 | + uint8_t data[] = {0x08}; |
| 221 | + ili9163c_send_cmd(ILI9163C_SLPIN); |
| 222 | + ili9163c_send_data(&data, 1); |
| 223 | +} |
| 224 | + |
| 225 | +void ili9163c_sleep_out() |
| 226 | +{ |
| 227 | + uint8_t data[] = {0x08}; |
| 228 | + ili9163c_send_cmd(ILI9163C_SLPOUT); |
| 229 | + ili9163c_send_data(&data, 1); |
| 230 | +} |
| 231 | + |
| 232 | +/********************** |
| 233 | + * STATIC FUNCTIONS |
| 234 | + **********************/ |
| 235 | + |
| 236 | +static void ili9163c_send_cmd(uint8_t cmd) |
| 237 | +{ |
| 238 | + disp_wait_for_pending_transactions(); |
| 239 | + gpio_set_level(ILI9163C_DC, 0); /*Command mode*/ |
| 240 | + disp_spi_send_data(&cmd, 1); |
| 241 | +} |
| 242 | + |
| 243 | +static void ili9163c_send_data(void *data, uint16_t length) |
| 244 | +{ |
| 245 | + disp_wait_for_pending_transactions(); |
| 246 | + gpio_set_level(ILI9163C_DC, 1); /*Data mode*/ |
| 247 | + disp_spi_send_data(data, length); |
| 248 | +} |
| 249 | + |
| 250 | +static void ili9163c_send_color(void *data, uint16_t length) |
| 251 | +{ |
| 252 | + disp_wait_for_pending_transactions(); |
| 253 | + gpio_set_level(ILI9163C_DC, 1); /*Data mode*/ |
| 254 | + disp_spi_send_colors(data, length); |
| 255 | +} |
| 256 | + |
| 257 | +static void ili9163c_set_orientation(uint8_t orientation) |
| 258 | +{ |
| 259 | + assert(orientation < 4); |
| 260 | + |
| 261 | + const char *orientation_str[] = { |
| 262 | + "PORTRAIT", "PORTRAIT_INVERTED", "LANDSCAPE", "LANDSCAPE_INVERTED"}; |
| 263 | + |
| 264 | + ESP_LOGD(TAG, "Display orientation: %s", orientation_str[orientation]); |
| 265 | + |
| 266 | + uint8_t data[] = {0x48, 0x88, 0xA8, 0x68}; |
| 267 | + |
| 268 | + ili9163c_send_cmd(ILI9163C_MADCTL); |
| 269 | + ili9163c_send_data((void *)&data[orientation], 1); |
| 270 | +} |
0 commit comments