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