alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Kshitij Sisodia | d5679cc | 2022-03-03 16:30:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame^] | 17 | #include "glcd.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 18 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 19 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 20 | #include "font_9x15_h.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 21 | |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame^] | 22 | #define CLCD_CS_Pos 0 |
| 23 | #define CLCD_CS_Msk (1UL<<CLCD_CS_Pos) |
| 24 | #define SHIELD_0_nCS_Pos 1 |
| 25 | #define SHIELD_0_nCS_Msk (1UL<<SHIELD_0_nCS_Pos) |
| 26 | #define SHIELD_1_nCS_Pos 2 |
| 27 | #define SHIELD_1_nCS_Msk (1UL<<SHIELD_1_nCS_Pos) |
| 28 | #define CLCD_RESET_Pos 3 |
| 29 | #define CLCD_RESET_Msk (1UL<<CLCD_RESET_Pos) |
| 30 | #define CLCD_RS_Pos 4 |
| 31 | #define CLCD_RS_Msk (1UL<<CLCD_RS_Pos) |
| 32 | #define CLCD_RD_Pos 5 |
| 33 | #define CLCD_RD_Msk (1UL<<CLCD_RD_Pos) |
| 34 | #define CLCD_BL_Pos 6 |
| 35 | #define CLCD_BL_Msk (1UL<<CLCD_BL_Pos) |
Kshitij Sisodia | d5679cc | 2022-03-03 16:30:07 +0000 | [diff] [blame] | 36 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 37 | /*-------------- CLCD Controller Internal Register addresses ----------------*/ |
| 38 | #define CHAR_COM ((volatile unsigned int *)(CLCD_CONFIG_BASE + 0x000)) |
| 39 | #define CHAR_DAT ((volatile unsigned int *)(CLCD_CONFIG_BASE + 0x004)) |
| 40 | #define CHAR_RD ((volatile unsigned int *)(CLCD_CONFIG_BASE + 0x008)) |
| 41 | #define CHAR_RAW ((volatile unsigned int *)(CLCD_CONFIG_BASE + 0x00C)) |
| 42 | #define CHAR_MASK ((volatile unsigned int *)(CLCD_CONFIG_BASE + 0x010)) |
| 43 | #define CHAR_STAT ((volatile unsigned int *)(CLCD_CONFIG_BASE + 0x014)) |
| 44 | #define CHAR_MISC ((volatile unsigned int *)(CLCD_CONFIG_BASE + 0x04C)) |
| 45 | |
| 46 | /*--------------- Graphic LCD interface hardware definitions -----------------*/ |
| 47 | /* Pin CS setting to 0 or 1 */ |
| 48 | #define LCD_CS(x) ((x) ? (*CHAR_MISC |= CLCD_CS_Msk) : (*CHAR_MISC &= ~CLCD_CS_Msk)) |
| 49 | #define LCD_RST(x) ((x) ? (*CHAR_MISC |= CLCD_RESET_Msk) : (*CHAR_MISC &= ~CLCD_RESET_Msk)) |
| 50 | #define LCD_BL(x) ((x) ? (*CHAR_MISC |= CLCD_BL_Msk) : (*CHAR_MISC &= ~CLCD_BL_Msk)) |
| 51 | |
| 52 | #define BG_COLOR 0 /* Background colour */ |
| 53 | #define TXT_COLOR 1 /* Text colour */ |
| 54 | |
| 55 | /** |
| 56 | * Text and background colour |
| 57 | */ |
| 58 | static volatile unsigned short Color[2] = {Black, White}; |
| 59 | |
| 60 | /** |
| 61 | * @brief Delay in while loop cycles. |
| 62 | * @param[in] cnt Number of while cycles to delay. |
| 63 | **/ |
| 64 | static void delay (int cnt) |
| 65 | { |
| 66 | cnt <<= DELAY_2N; |
| 67 | while (cnt != 0) { |
| 68 | --cnt; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @brief Write a command the LCD controller. |
| 74 | * @param[in] cmd Command to be written. |
| 75 | */ |
| 76 | static __inline void wr_cmd(unsigned char cmd) |
| 77 | { |
| 78 | LCD_CS(0); |
| 79 | *CHAR_COM = cmd; |
| 80 | LCD_CS(1); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @brief Start of data writing to the LCD controller. |
| 85 | */ |
| 86 | static __inline void wr_dat_start (void) |
| 87 | { |
| 88 | LCD_CS(0); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @brief Stop of data writing to the LCD controller. |
| 93 | */ |
| 94 | static __inline void wr_dat_stop (void) |
| 95 | { |
| 96 | LCD_CS(1); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @brief Data writing to the LCD controller. |
| 101 | * @param[in] dat Data to be written. |
| 102 | */ |
| 103 | static __inline void wr_dat_only(unsigned short dat) |
| 104 | { |
| 105 | *CHAR_DAT = (dat >> 8); /* Write D8..D15 */ |
| 106 | *CHAR_DAT = (dat & 0xFF); /* Write D0..D7 */ |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @brief Write a value to the to LCD register. |
| 111 | * @param[in] reg Register to be written. |
| 112 | * @param[in] val Value to write to the register. |
| 113 | */ |
| 114 | static __inline void wr_reg(unsigned char reg, unsigned short val) |
| 115 | { |
| 116 | LCD_CS(0); |
| 117 | *CHAR_COM = reg; |
| 118 | wr_dat_only(val); |
| 119 | LCD_CS(1); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @brief Converts a gray value to RGB565 representation. |
| 124 | * @param[in] src_uchar Pointer to the source pixel. |
| 125 | * @return 16 bit RGB565 value. |
| 126 | */ |
| 127 | static inline uint16_t _GLCD_Gray8_to_RGB565(uint8_t *src_uchar) |
| 128 | { |
| 129 | uint16_t val_r = (*src_uchar >> 3); |
| 130 | uint16_t val_g = (*src_uchar >> 2); |
| 131 | return ((val_r << 11) | (val_g << 5) | val_r); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @brief Converts an RGB888 value to RGB565 representation. |
| 136 | * @param[in] src_uchar Pointer to the source pixel for R (assumed to |
| 137 | * be RGB format). |
| 138 | * @return 16 bit RGB565 value. |
| 139 | */ |
| 140 | static inline uint16_t _GLCD_RGB888_to_RGB565(uint8_t *src_uchar) |
| 141 | { |
| 142 | uint16_t val_r = (*src_uchar >> 3) & 0x1F; |
| 143 | uint16_t val_g = (*(src_uchar+1) >> 2) & 0x3F; |
| 144 | uint16_t val_b = (*(src_uchar+2) >> 3) & 0x1F; |
| 145 | return ((val_r << 11) | (val_g << 5) | val_b); |
| 146 | } |
| 147 | |
| 148 | /* Helper typedef to encapsulate the colour conversion function |
| 149 | * signatures */ |
| 150 | typedef uint16_t (* std_clr_2_lcd_clr_fn)(uint8_t *src_uchar); |
| 151 | |
| 152 | void GLCD_SetWindow(unsigned int x, unsigned int y, unsigned int w, unsigned int h) { |
| 153 | unsigned int xe, ye; |
| 154 | |
| 155 | xe = x+w-1; |
| 156 | ye = y+h-1; |
| 157 | |
| 158 | wr_reg(0x02, x >> 8); /* Column address start MSB */ |
| 159 | wr_reg(0x03, x & 0xFF); /* Column address start LSB */ |
| 160 | wr_reg(0x04, xe >> 8); /* Column address end MSB */ |
| 161 | wr_reg(0x05, xe & 0xFF); /* Column address end LSB */ |
| 162 | |
| 163 | wr_reg(0x06, y >> 8); /* Row address start MSB */ |
| 164 | wr_reg(0x07, y & 0xFF); /* Row address start LSB */ |
| 165 | wr_reg(0x08, ye >> 8); /* Row address end MSB */ |
| 166 | wr_reg(0x09, ye & 0xFF); /* Row address end LSB */ |
| 167 | } |
| 168 | |
| 169 | void GLCD_WindowMax(void) |
| 170 | { |
| 171 | GLCD_SetWindow (0, 0, GLCD_WIDTH, GLCD_HEIGHT); |
| 172 | } |
| 173 | |
| 174 | void GLCD_SetTextColor(unsigned short color) |
| 175 | { |
| 176 | Color[TXT_COLOR] = color; |
| 177 | } |
| 178 | |
| 179 | void GLCD_SetBackColor(unsigned short color) |
| 180 | { |
| 181 | Color[BG_COLOR] = color; |
| 182 | } |
| 183 | |
| 184 | void GLCD_Clear(unsigned short color) |
| 185 | { |
| 186 | unsigned int i; |
| 187 | |
| 188 | GLCD_WindowMax(); |
| 189 | wr_cmd(0x22); |
| 190 | wr_dat_start(); |
| 191 | |
| 192 | for(i = 0; i < (GLCD_WIDTH*GLCD_HEIGHT); ++i) { |
| 193 | wr_dat_only(color); |
| 194 | } |
| 195 | wr_dat_stop(); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | void GLCD_DrawChar( |
| 200 | unsigned int x, unsigned int y, |
| 201 | unsigned int cw, unsigned int ch, |
| 202 | unsigned char *c) |
| 203 | { |
| 204 | unsigned int i, j, k, pixs; |
| 205 | |
| 206 | /* Sanity check: out of bounds? */ |
| 207 | if ((x + cw) > GLCD_WIDTH || (y + ch) > GLCD_HEIGHT) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | GLCD_SetWindow(x, y, cw, ch); |
| 212 | |
| 213 | wr_cmd(0x22); |
| 214 | wr_dat_start(); |
| 215 | |
| 216 | k = (cw + 7)/8; |
| 217 | |
| 218 | if (k == 1) { |
| 219 | for (j = 0; j < ch; ++j) { |
| 220 | pixs = *(unsigned char *)c; |
| 221 | c += 1; |
| 222 | |
| 223 | for (i = 0; i < cw; ++i) { |
| 224 | wr_dat_only (Color[(pixs >> i) & 1]); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | else if (k == 2) { |
| 229 | for (j = 0; j < ch; ++j) { |
| 230 | pixs = *(unsigned short *)c; |
| 231 | c += 2; |
| 232 | |
| 233 | for (i = 0; i < cw; ++i) { |
| 234 | wr_dat_only (Color[(pixs >> i) & 1]); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | wr_dat_stop(); |
| 239 | } |
| 240 | |
| 241 | void GLCD_DisplayChar( |
| 242 | unsigned int ln, unsigned int col, |
| 243 | unsigned char fi, unsigned char c) |
| 244 | { |
| 245 | c -= 32; |
| 246 | switch (fi) { |
| 247 | case 0: /* Font 9 x 15. */ |
| 248 | GLCD_DrawChar(col * 9, ln * 15, 9, 15, |
| 249 | (unsigned char *)&Font_9x15_h[c * 15]); |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void GLCD_DisplayString( |
| 255 | unsigned int ln, unsigned int col, |
| 256 | unsigned char fi, char *s) |
| 257 | { |
| 258 | while (*s) { |
| 259 | GLCD_DisplayChar(ln, col++, fi, *s++); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | void GLCD_ClearLn(unsigned int ln, unsigned char fi) |
| 266 | { |
| 267 | unsigned char i; |
| 268 | char buf[60]; |
| 269 | |
| 270 | GLCD_WindowMax(); |
| 271 | switch (fi) { |
| 272 | case 0: /* Font 9x15*/ |
| 273 | for (i = 0; i < (GLCD_WIDTH+8)/9; ++i) { |
| 274 | buf[i] = ' '; |
| 275 | } |
| 276 | buf[i+1] = 0; |
| 277 | break; |
| 278 | } |
| 279 | GLCD_DisplayString (ln, 0, fi, buf); |
| 280 | } |
| 281 | |
| 282 | void GLCD_Bitmap(unsigned int x, unsigned int y, |
| 283 | unsigned int w, unsigned int h, |
| 284 | unsigned short *bitmap) |
| 285 | { |
| 286 | unsigned int i; |
| 287 | unsigned short *bitmap_ptr = bitmap; |
| 288 | |
| 289 | GLCD_SetWindow (x, y, w, h); |
| 290 | |
| 291 | wr_cmd(0x22); |
| 292 | wr_dat_start(); |
| 293 | |
| 294 | for (i = 0; i < (w*h); ++i) { |
| 295 | wr_dat_only (bitmap_ptr[i]); |
| 296 | } |
| 297 | wr_dat_stop(); |
| 298 | } |
| 299 | |
Richard Burton | 9c54990 | 2022-02-15 16:39:18 +0000 | [diff] [blame] | 300 | void GLCD_Image(const void *data, const uint32_t width, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 301 | const uint32_t height, const uint32_t channels, |
| 302 | const uint32_t pos_x, const uint32_t pos_y, |
| 303 | const uint32_t downsample_factor) |
| 304 | { |
| 305 | uint32_t i, j = 0; /* for loops */ |
| 306 | const uint32_t x_incr = channels * downsample_factor; /* stride. */ |
| 307 | const uint32_t y_incr = channels * width * (downsample_factor - 1); /* skip rows. */ |
| 308 | uint8_t* src_unsigned = (uint8_t *)data; /* temporary pointer. */ |
| 309 | std_clr_2_lcd_clr_fn cvt_clr_fn = 0; /* colour conversion function. */ |
| 310 | |
| 311 | /* Based on number of channels, we decide which of the above functions to use. */ |
| 312 | switch (channels) { |
| 313 | case 1: |
| 314 | cvt_clr_fn = _GLCD_Gray8_to_RGB565; |
| 315 | break; |
| 316 | |
| 317 | case 3: |
| 318 | cvt_clr_fn = _GLCD_RGB888_to_RGB565; |
| 319 | break; |
| 320 | |
| 321 | default: |
| 322 | printf_err("number of channels not supported by display\n"); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | /* Set the window position expected. Note: this is integer div. */ |
| 327 | GLCD_SetWindow(pos_x, pos_y, |
| 328 | width/downsample_factor, height/downsample_factor); |
| 329 | wr_cmd(0x22); |
| 330 | wr_dat_start(); |
| 331 | |
| 332 | /* Loop over the image. */ |
| 333 | for (j = height; j != 0; j -= downsample_factor) { |
| 334 | for (i = width; i != 0; i -= downsample_factor) { |
| 335 | wr_dat_only(cvt_clr_fn(src_unsigned)); |
| 336 | src_unsigned += x_incr; |
| 337 | } |
| 338 | |
| 339 | /* Skip rows if needed. */ |
| 340 | src_unsigned += y_incr; |
| 341 | } |
| 342 | |
| 343 | wr_dat_stop(); |
| 344 | } |
| 345 | |
| 346 | void GLCD_Box( |
| 347 | unsigned int x, unsigned int y, |
| 348 | unsigned int w, unsigned int h, |
| 349 | unsigned short color) |
| 350 | { |
| 351 | unsigned int i; |
| 352 | |
| 353 | GLCD_SetWindow (x, y, w, h); |
| 354 | |
| 355 | wr_cmd(0x22); |
| 356 | wr_dat_start(); |
| 357 | for(i = 0; i < (w*h); ++i){ |
| 358 | wr_dat_only (color); |
| 359 | } |
| 360 | wr_dat_stop(); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | void GLCD_Initialize (void) |
| 365 | { |
| 366 | /* CLCD screen setup (Default CLCD screen interface state) ------------- */ |
| 367 | LCD_CS(1); /* deassert nCS0. */ |
| 368 | LCD_RST(1); /* deassert Reset. */ |
| 369 | LCD_BL(0); /* switch off backlight. */ |
| 370 | |
| 371 | /* Reset CLCD screen --------------------------------------------------- */ |
| 372 | LCD_RST(0); /* assert Reset. */ |
| 373 | delay(1); |
| 374 | LCD_RST(1); /* deassert Reset. */ |
| 375 | delay(10); |
| 376 | |
| 377 | /* Driving ability settings ----------------------------------------------*/ |
| 378 | wr_reg(0xEA, 0x00); /* Power control internal used (1). */ |
| 379 | wr_reg(0xEB, 0x20); /* Power control internal used (2). */ |
| 380 | wr_reg(0xEC, 0x0C); /* Source control internal used (1). */ |
| 381 | wr_reg(0xED, 0xC7); /* Source control internal used (2). */ |
| 382 | wr_reg(0xE8, 0x38); /* Source output period Normal mode. */ |
| 383 | wr_reg(0xE9, 0x10); /* Source output period Idle mode. */ |
| 384 | wr_reg(0xF1, 0x01); /* RGB 18-bit interface ;0x0110. */ |
| 385 | wr_reg(0xF2, 0x10); |
| 386 | |
| 387 | /* Adjust the Gamma Curve ------------------------------------------------*/ |
| 388 | wr_reg(0x40, 0x01); |
| 389 | wr_reg(0x41, 0x00); |
| 390 | wr_reg(0x42, 0x00); |
| 391 | wr_reg(0x43, 0x10); |
| 392 | wr_reg(0x44, 0x0E); |
| 393 | wr_reg(0x45, 0x24); |
| 394 | wr_reg(0x46, 0x04); |
| 395 | wr_reg(0x47, 0x50); |
| 396 | wr_reg(0x48, 0x02); |
| 397 | wr_reg(0x49, 0x13); |
| 398 | wr_reg(0x4A, 0x19); |
| 399 | wr_reg(0x4B, 0x19); |
| 400 | wr_reg(0x4C, 0x16); |
| 401 | |
| 402 | wr_reg(0x50, 0x1B); |
| 403 | wr_reg(0x51, 0x31); |
| 404 | wr_reg(0x52, 0x2F); |
| 405 | wr_reg(0x53, 0x3F); |
| 406 | wr_reg(0x54, 0x3F); |
| 407 | wr_reg(0x55, 0x3E); |
| 408 | wr_reg(0x56, 0x2F); |
| 409 | wr_reg(0x57, 0x7B); |
| 410 | wr_reg(0x58, 0x09); |
| 411 | wr_reg(0x59, 0x06); |
| 412 | wr_reg(0x5A, 0x06); |
| 413 | wr_reg(0x5B, 0x0C); |
| 414 | wr_reg(0x5C, 0x1D); |
| 415 | wr_reg(0x5D, 0xCC); |
| 416 | |
| 417 | /* Power voltage setting -------------------------------------------------*/ |
| 418 | wr_reg(0x1B, 0x1B); |
| 419 | wr_reg(0x1A, 0x01); |
| 420 | wr_reg(0x24, 0x2F); |
| 421 | wr_reg(0x25, 0x57); |
| 422 | wr_reg(0x23, 0x88); |
| 423 | |
| 424 | /* Power on setting ------------------------------------------------------*/ |
| 425 | wr_reg(0x18, 0x36); /* Internal oscillator frequency adj. */ |
| 426 | wr_reg(0x19, 0x01); /* Enable internal oscillator. */ |
| 427 | wr_reg(0x01, 0x00); /* Normal mode, no scroll. */ |
| 428 | wr_reg(0x1F, 0x88); /* Power control 6 - DDVDH Off. */ |
| 429 | delay(20); |
| 430 | wr_reg(0x1F, 0x82); /* Power control 6 - Step-up: 3 x VCI. */ |
| 431 | delay(5); |
| 432 | wr_reg(0x1F, 0x92); /* Power control 6 - Step-up: On. */ |
| 433 | delay(5); |
| 434 | wr_reg(0x1F, 0xD2); /* Power control 6 - VCOML active. */ |
| 435 | delay(5); |
| 436 | |
| 437 | /* Color selection -------------------------------------------------------*/ |
| 438 | wr_reg(0x17, 0x55); /* RGB, System interface: 16 Bit/Pixel. */ |
| 439 | wr_reg(0x00, 0x00); /* Scrolling off, no standby. */ |
| 440 | |
| 441 | /* Interface config ------------------------------------------------------*/ |
| 442 | wr_reg(0x2F, 0x11); /* LCD Drive: 1-line inversion. */ |
| 443 | wr_reg(0x31, 0x00); |
| 444 | wr_reg(0x32, 0x00); /* DPL=0, HSPL=0, VSPL=0, EPL=0. */ |
| 445 | |
| 446 | /* Display on setting ----------------------------------------------------*/ |
| 447 | wr_reg(0x28, 0x38); /* PT(0,0) active, VGL/VGL. */ |
| 448 | delay(20); |
| 449 | wr_reg(0x28, 0x3C); /* Display active, VGL/VGL. */ |
| 450 | |
| 451 | #if (LANDSCAPE == 1) |
| 452 | #if (ROTATE180 == 0) |
| 453 | wr_reg (0x16, 0xA8); |
| 454 | #else /* (ROTATE180 == 0) */ |
| 455 | wr_reg (0x16, 0x68); |
| 456 | #endif /* (ROTATE180 == 0) */ |
| 457 | #else /* (LANDSCAPE == 1) */ |
| 458 | #if (ROTATE180 == 0) |
| 459 | wr_reg (0x16, 0x08); |
| 460 | #else /* (ROTATE180 == 0) */ |
| 461 | wr_reg (0x16, 0xC8); |
| 462 | #endif /* (ROTATE180 == 0) */ |
| 463 | #endif /* (LANDSCAPE == 1) */ |
| 464 | |
| 465 | /* Display scrolling settings --------------------------------------------*/ |
| 466 | wr_reg(0x0E, 0x00); /* TFA MSB */ |
| 467 | wr_reg(0x0F, 0x00); /* TFA LSB */ |
| 468 | wr_reg(0x10, 320 >> 8); /* VSA MSB */ |
| 469 | wr_reg(0x11, 320 & 0xFF); /* VSA LSB */ |
| 470 | wr_reg(0x12, 0x00); /* BFA MSB */ |
| 471 | wr_reg(0x13, 0x00); /* BFA LSB */ |
| 472 | |
| 473 | LCD_BL(1); /* turn on backlight */ |
| 474 | } |