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