EZR32 Leopard Gecko Software Documentation  ezr32lg-doc-4.2.1
retargettft.c
Go to the documentation of this file.
1 /***************************************************************************/
17 #include <stdio.h>
18 #include <stdint.h>
19 #include "em_device.h"
20 #include "em_cmu.h"
21 #include "em_ebi.h"
22 #include "em_gpio.h"
23 #include "dmd/ssd2119/dmd_ssd2119.h"
24 #include "bsp.h"
25 #include "retargettft.h"
26 
27 #include "displayfont8x8.h"
28 #define fontBits chars_8x8_bits
30 #define CHARS 40
31 #define LINES 30
33 static uint8_t charBuffer[LINES][CHARS];
34 static uint8_t rgbColor[3];
37 static int xpos, ypos;
39 static bool fullUpdate = true;
40 static bool bufferReset = true;
41 static bool tftReset = true;
42 static bool LFtoCRLF = 0;
43 static bool initialized = false;
45 /* Static functions. */
46 static void tftTextReset(void);
47 static void tftTextScrollUp(void);
48 
49 
50 /**************************************************************************/
53 void RETARGET_TftInit(void)
54 {
55 #if !defined(__CROSSWORKS_ARM) && defined(__GNUC__)
56  setvbuf(stdout, NULL, _IONBF, 0); /*Set unbuffered mode for stdout (newlib)*/
57 #endif
58 
59  /* Reset the TFT text display state. */
60  tftTextReset();
61 }
62 
63 
64 /**************************************************************************/
68 void RETARGET_TftCrLf(int on)
69 {
70  if (on)
71  LFtoCRLF = true;
72  else
73  LFtoCRLF = false;
74 }
75 
76 
77 /**************************************************************************/
80 static void tftTextReset(void)
81 {
82  int x, y;
83  volatile int i;
84  EMSTATUS status;
85 
86  /* Initialize color for font */
87  /* Use \b for red text (bell/warning) */
88  rgbColor[0] = 0xff;
89  rgbColor[1] = 0xff;
90  rgbColor[2] = 0xff;
91 
92  /* Character buffer */
93  if (bufferReset)
94  {
95  /* Clear character buffer */
96  for (y = 0; y < LINES; y++)
97  {
98  for (x = 0; x < CHARS; x++)
99  {
100  charBuffer[y][x] = 0;
101  }
102  }
103  /* Set cursor position to upper left */
104  xpos = 0;
105  ypos = 0;
106  }
107 
108  /* Display controller */
109  if (tftReset)
110  {
111  /* Configure for EBI mode and reset display */
112  BSP_DisplayControl(BSP_Display_EBI);
113  BSP_DisplayControl(BSP_Display_ResetAssert);
114  BSP_DisplayControl(BSP_Display_PowerDisable);
115  /* Short delay */
116  for (i = 0; i < 10000; i++) ;
117  /* Configure display for Direct Drive + SPI mode */
118  BSP_DisplayControl(BSP_Display_Mode8080);
119  BSP_DisplayControl(BSP_Display_PowerEnable);
120  BSP_DisplayControl(BSP_Display_ResetRelease);
121 
122  /* Initialize graphics - abort on failure */
123  status = DMDIF_init(BC_SSD2119_BASE, BC_SSD2119_BASE + 2);
124  if (status == DMD_OK) status = DMD_init(0);
125  if ((status != DMD_OK) && (status != DMD_ERROR_DRIVER_ALREADY_INITIALIZED)) while (1) ;
126  /* Make sure display is configured with correct rotation */
127  if ((status == DMD_OK)) status = DMD_flipDisplay(1, 1);
128  }
129  initialized = true;
130 }
131 
132 /**************************************************************************/
135 static void tftTextScrollUp(void)
136 {
137  int y;
138  int x;
139 
140  /* copy all lines one line up */
141  for (y = 0; y < (LINES - 1); y++)
142  {
143  for (x = 0; x < CHARS; x++)
144  {
145  charBuffer[y][x] = charBuffer[y + 1][x];
146  }
147  }
148  /* clear last line */
149  for (x = 0; x < CHARS; x++)
150  {
151  charBuffer[LINES - 1][x] = 0;
152  }
153  xpos = 0;
154  ypos = LINES - 1;
155  fullUpdate = true;
156 }
157 
158 /***************************************************************************/
163 /**************************************************************************/
168 void RETARGET_TFTTX(int c)
169 {
170  /* check for CR */
171  if (c == '\r')
172  {
173  xpos = 0;
174  return;
175  }
176  /* check for LF */
177  if (c == '\n')
178  {
179  ypos = ypos + 1;
180  xpos = 0;
181  if (ypos >= LINES)
182  {
183  /* scroll characters one line up */
184  tftTextScrollUp();
185  ypos = (LINES - 1);
186  }
187  return;
188  }
189  /* check for bell character, changes color to red */
190  if (c == '\b')
191  {
192  if (rgbColor[1] == 0xff)
193  {
194  rgbColor[1] = 0x00;
195  rgbColor[2] = 0x00;
196  }
197  else
198  {
199  rgbColor[1] = 0xff;
200  rgbColor[2] = 0xff;
201  }
202  return;
203  }
204 
205  /* check for non-printable characters */
206  if (c < ' ' || c > '~')
207  {
208  c = ' ';
209  }
210  xpos = xpos + 1;
211  if (xpos >= CHARS)
212  {
213  xpos = 0;
214  ypos = ypos + 1;
215  }
216  if (ypos >= LINES)
217  {
218  tftTextScrollUp();
219  ypos = 29;
220  }
221  charBuffer[ypos][xpos] = c - ' ';
222 }
223 
224 
225 /**************************************************************************/
230 void RETARGET_TFTUpdate(bool fullFrame)
231 {
232  int x, y;
233  uint32_t pixelX, pixelY;
234  uint8_t c, bitField;
235  int i;
236 
237  /* Draw a full screen */
238  if (fullFrame)
239  {
240  for (y = 0; y < LINES; y++)
241  {
242  for (x = 0; x < CHARS; x++)
243  {
244  pixelX = x * 8;
245  pixelY = y * 8;
246 
247  c = charBuffer[y][x];
248  for (i = 0; i < 8; i++)
249  {
250  bitField = fontBits[c + 100 * i];
251  if (bitField == 0)
252  {
253  DMD_writeData(pixelX, pixelY + i, (uint8_t *) "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 8);
254  continue;
255  }
256 
257  if (bitField & 0x01)
258  {
259  DMD_writeColor(pixelX + 0, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
260  }
261  else
262  {
263  DMD_writeColor(pixelX + 0, pixelY + i, 0x00, 0x00, 0x00, 1);
264  }
265  if (bitField & 0x02)
266  {
267  DMD_writeColor(pixelX + 1, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
268  }
269  else
270  {
271  DMD_writeColor(pixelX + 1, pixelY + i, 0x00, 0x00, 0x00, 1);
272  }
273  if (bitField & 0x04)
274  {
275  DMD_writeColor(pixelX + 2, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
276  }
277  else
278  {
279  DMD_writeColor(pixelX + 2, pixelY + i, 0x00, 0x00, 0x00, 1);
280  }
281  if (bitField & 0x08)
282  {
283  DMD_writeColor(pixelX + 3, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
284  }
285  else
286  {
287  DMD_writeColor(pixelX + 3, pixelY + i, 0x00, 0x00, 0x00, 1);
288  }
289  if (bitField & 0x10)
290  {
291  DMD_writeColor(pixelX + 4, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
292  }
293  else
294  {
295  DMD_writeColor(pixelX + 4, pixelY + i, 0x00, 0x00, 0x00, 1);
296  }
297  if (bitField & 0x20)
298  {
299  DMD_writeColor(pixelX + 5, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
300  }
301  else
302  {
303  DMD_writeColor(pixelX + 5, pixelY + i, 0x00, 0x00, 0x00, 1);
304  }
305  if (bitField & 0x40)
306  {
307  DMD_writeColor(pixelX + 6, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
308  }
309  else
310  {
311  DMD_writeColor(pixelX + 6, pixelY + i, 0x00, 0x00, 0x00, 1);
312  }
313  if (bitField & 0x80)
314  {
315  DMD_writeColor(pixelX + 7, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
316  }
317  else
318  {
319  DMD_writeColor(pixelX + 7, pixelY + i, 0x00, 0x00, 0x00, 1);
320  }
321  }
322  }
323  }
324  }
325  else
326  {
327  /* Draw xpos, ypos only */
328  c = charBuffer[ypos][xpos];
329  pixelX = xpos * 8;
330  pixelY = ypos * 8;
331  for (i = 0; i < 8; i++)
332  {
333  bitField = fontBits[c + 100 * i];
334  if (bitField == 0)
335  {
336  DMD_writeData(pixelX, pixelY + i, (uint8_t *) "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 8);
337  continue;
338  }
339 
340  if (bitField & 0x01)
341 
342  {
343  DMD_writeColor(pixelX + 0, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
344  }
345  else
346  {
347  DMD_writeColor(pixelX + 0, pixelY + i, 0x00, 0x00, 0x00, 1);
348  }
349  if (bitField & 0x02)
350  {
351  DMD_writeColor(pixelX + 1, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
352  }
353  else
354  {
355  DMD_writeColor(pixelX + 1, pixelY + i, 0x00, 0x00, 0x00, 1);
356  }
357  if (bitField & 0x04)
358  {
359  DMD_writeColor(pixelX + 2, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
360  }
361  else
362  {
363  DMD_writeColor(pixelX + 2, pixelY + i, 0x00, 0x00, 0x00, 1);
364  }
365  if (bitField & 0x08)
366  {
367  DMD_writeColor(pixelX + 3, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
368  }
369  else
370  {
371  DMD_writeColor(pixelX + 3, pixelY + i, 0x00, 0x00, 0x00, 1);
372  }
373  if (bitField & 0x10)
374  {
375  DMD_writeColor(pixelX + 4, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
376  }
377  else
378  {
379  DMD_writeColor(pixelX + 4, pixelY + i, 0x00, 0x00, 0x00, 1);
380  }
381  if (bitField & 0x20)
382  {
383  DMD_writeColor(pixelX + 5, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
384  }
385  else
386  {
387  DMD_writeColor(pixelX + 5, pixelY + i, 0x00, 0x00, 0x00, 1);
388  }
389  if (bitField & 0x40)
390  {
391  DMD_writeColor(pixelX + 6, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
392  }
393  else
394  {
395  DMD_writeColor(pixelX + 6, pixelY + i, 0x00, 0x00, 0x00, 1);
396  }
397  if (bitField & 0x80)
398  {
399  DMD_writeColor(pixelX + 7, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
400  }
401  else
402  {
403  DMD_writeColor(pixelX + 7, pixelY + i, 0x00, 0x00, 0x00, 1);
404  }
405  }
406  }
407 }
408 
411 /**************************************************************************/
417 {
418  return -1;
419 }
420 
421 /**************************************************************************/
427 {
428  if ((BSP_RegisterRead(&BC_REGISTER->UIF_AEM) == BC_UIF_AEM_EFM))
429  {
430  if ((BSP_RegisterRead(&BC_REGISTER->ARB_CTRL) != BC_ARB_CTRL_EBI) || (initialized == false))
431  {
432  if (initialized)
433  {
434  bufferReset = false;
435  tftReset = true;
436  tftTextReset();
437  }
438  else
439  {
440  bufferReset = true;
441  tftReset = true;
442  tftTextReset();
443  }
444  fullUpdate = true;
445  }
446  }
447 
448  /* Check for form feed - clear screen */
449  if (c == '\f')
450  {
451  bufferReset = true;
452  tftReset = false;
453  tftTextReset();
454  fullUpdate = true;
455  return c;
456  }
457 
458  /* Add CR or LF to CRLF if enabled */
459  if (LFtoCRLF && (c == '\n'))
460  {
461  RETARGET_TFTTX('\r');
462  }
463  RETARGET_TFTTX(c);
464 
465  if (LFtoCRLF && (c == '\r'))
466  {
467  RETARGET_TFTTX('\n');
468  }
469 
470  /* Update display */
472  fullUpdate = false;
473  return c;
474 }
Clock management unit (CMU) API.
int RETARGET_ReadChar(void)
Receive a byte No input method from TFT is possible, thus we always return -1.
Definition: retargettft.c:416
Board support package API definitions.
static bool tftReset
Definition: retargettft.c:41
static int xpos
Definition: retargettft.c:37
void RETARGET_TftCrLf(int on)
Toggle LF to CRLF conversion.
Definition: retargettft.c:68
static void tftTextReset(void)
Reset TFT text display state.
Definition: retargettft.c:80
static uint8_t charBuffer[LINES][CHARS]
Definition: retargettft.c:33
8x8 font with all characters
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
void RETARGET_TFTTX(int c)
Transmit/display a character.
Definition: retargettft.c:168
#define CHARS
Definition: retargettft.c:30
void RETARGET_TftInit(void)
Intializes TFT text display.
Definition: retargettft.c:53
static bool initialized
Definition: retargettft.c:43
static uint8_t rgbColor[3]
Definition: retargettft.c:34
static bool fullUpdate
Definition: retargettft.c:39
#define BC_UIF_AEM_EFM
External Bus Iterface (EBI) peripheral API.
static bool LFtoCRLF
Definition: retargettft.c:42
General Purpose IO (GPIO) peripheral API.
int RETARGET_WriteChar(char c)
Transmit single byte to the TFT.
Definition: retargettft.c:426
#define LINES
Definition: retargettft.c:31
static void tftTextScrollUp(void)
Scroll one line of characters up on the screen.
Definition: retargettft.c:135
void RETARGET_TFTUpdate(bool fullFrame)
Display framebuffer.
Definition: retargettft.c:230
#define fontBits
Definition: retargettft.c:28
#define BC_ARB_CTRL_EBI
static int ypos
Definition: retargettft.c:37
static bool bufferReset
Definition: retargettft.c:40
#define BC_REGISTER
#define BC_SSD2119_BASE