retargettft.c

Go to the documentation of this file.
00001 /***************************************************************************/
00035 #include <stdio.h>
00036 #include <stdint.h>
00037 #include "em_device.h"
00038 #include "em_cmu.h"
00039 #include "em_ebi.h"
00040 #include "em_gpio.h"
00041 #include "glib/glib_font.h"
00042 #include "dmd/ssd2119/dmd_ssd2119.h"
00043 #include "bsp.h"
00044 #include "retargetserial.h"
00045 
00046 #define CHARS    40                         
00047 #define LINES    30                         
00049 static uint8_t charBuffer[LINES][CHARS];    
00050 static uint8_t rgbColor[3];                 
00053 static int  xpos, ypos;                     
00055 static bool fullUpdate  = true;             
00056 static bool bufferReset = true;             
00057 static bool tftReset    = true;             
00058 static bool LFtoCRLF    = 0;                
00059 static bool initialized = false;            
00061 /**************************************************************************/
00065 void RETARGET_SerialCrLf(int on)
00066 {
00067   if (on)
00068     LFtoCRLF = true;
00069   else
00070     LFtoCRLF = false;
00071 }
00072 
00073 
00074 /**************************************************************************/
00077 void RETARGET_SerialInit(void)
00078 {
00079   int          x, y;
00080   volatile int i;
00081   EMSTATUS     status;
00082 
00083   /* Initialize color for font */
00084   /* Use \b for red text (bell/warning) */
00085   rgbColor[0] = 0xff;
00086   rgbColor[1] = 0xff;
00087   rgbColor[2] = 0xff;
00088 
00089   /* Character buffer */
00090   if (bufferReset)
00091   {
00092     /* Clear character buffer */
00093     for (y = 0; y < LINES; y++)
00094     {
00095       for (x = 0; x < CHARS; x++)
00096       {
00097         charBuffer[y][x] = 0;
00098       }
00099     }
00100     /* Set cursor position to upper left */
00101     xpos = 0;
00102     ypos = 0;
00103   }
00104 
00105   /* Display controller */
00106   if (tftReset)
00107   {
00108     /* Configure for EBI mode and reset display */
00109     BSP_DisplayControl(BSP_Display_EBI);
00110     BSP_DisplayControl(BSP_Display_ResetAssert);
00111     BSP_DisplayControl(BSP_Display_PowerDisable);
00112     /* Short delay */
00113     for (i = 0; i < 10000; i++) ;
00114     /* Configure display for Direct Drive + SPI mode */
00115     BSP_DisplayControl(BSP_Display_Mode8080);
00116     BSP_DisplayControl(BSP_Display_PowerEnable);
00117     BSP_DisplayControl(BSP_Display_ResetRelease);
00118 
00119     /* Initialize graphics - abort on failure */
00120     status = DMD_init(BC_SSD2119_BASE, BC_SSD2119_BASE + 2);
00121     if ((status != DMD_OK) && (status != DMD_ERROR_DRIVER_ALREADY_INITIALIZED)) while (1) ;
00122     /* Make sure display is configured with correct rotation */
00123     if ((status == DMD_OK)) DMD_flipDisplay(1, 1);
00124 
00125 #if !defined(__CROSSWORKS_ARM) && defined(__GNUC__)
00126     setvbuf(stdout, NULL, _IONBF, 0);   /*Set unbuffered mode for stdout (newlib)*/
00127 #endif
00128   }
00129   initialized = true;
00130 }
00131 
00132 /**************************************************************************/
00135 static void scrollUp(void)
00136 {
00137   int y;
00138   int x;
00139 
00140   /* copy all lines one line up */
00141   for (y = 0; y < (LINES - 1); y++)
00142   {
00143     for (x = 0; x < CHARS; x++)
00144     {
00145       charBuffer[y][x] = charBuffer[y + 1][x];
00146     }
00147   }
00148   /* clear last line */
00149   for (x = 0; x < CHARS; x++)
00150   {
00151     charBuffer[LINES - 1][x] = 0;
00152   }
00153   xpos       = 0;
00154   ypos       = LINES - 1;
00155   fullUpdate = true;
00156 }
00157 
00158 /***************************************************************************/
00163 /**************************************************************************/
00168 void RETARGET_TFTTX(int c)
00169 {
00170   /* check for CR */
00171   if (c == '\r')
00172   {
00173     xpos = 0;
00174     return;
00175   }
00176   /* check for LF */
00177   if (c == '\n')
00178   {
00179     ypos = ypos + 1;
00180     xpos = 0;
00181     if (ypos >= LINES)
00182     {
00183       /* scroll characters one line up */
00184       scrollUp();
00185       ypos = (LINES - 1);
00186     }
00187     return;
00188   }
00189   /* check for bell character, changes color to red */
00190   if (c == '\b')
00191   {
00192     if (rgbColor[1] == 0xff)
00193     {
00194       rgbColor[1] = 0x00;
00195       rgbColor[2] = 0x00;
00196     }
00197     else
00198     {
00199       rgbColor[1] = 0xff;
00200       rgbColor[2] = 0xff;
00201     }
00202     return;
00203   }
00204 
00205   /* check for non-printable characters */
00206   if (c < ' ' || c > '~')
00207   {
00208     c = ' ';
00209   }
00210   xpos = xpos + 1;
00211   if (xpos >= CHARS)
00212   {
00213     xpos = 0;
00214     ypos = ypos + 1;
00215   }
00216   if (ypos >= LINES)
00217   {
00218     scrollUp();
00219     ypos = 29;
00220   }
00221   charBuffer[ypos][xpos] = c - ' ';
00222 }
00223 
00224 
00225 /**************************************************************************/
00230 void RETARGET_TFTUpdate(bool fullFrame)
00231 {
00232   int      x, y;
00233   uint32_t pixelX, pixelY;
00234   uint8_t  c, bitField;
00235   int      i;
00236 
00237   /* Draw a full screen */
00238   if (fullFrame)
00239   {
00240     for (y = 0; y < LINES; y++)
00241     {
00242       for (x = 0; x < CHARS; x++)
00243       {
00244         pixelX = x * 8;
00245         pixelY = y * 8;
00246 
00247         c = charBuffer[y][x];
00248         for (i = 0; i < 8; i++)
00249         {
00250           bitField = fontBits[c + 100 * i];
00251           if (bitField == 0)
00252           {
00253             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);
00254             continue;
00255           }
00256 
00257           if (bitField & 0x01)
00258           {
00259             DMD_writeColor(pixelX + 0, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00260           }
00261           else
00262           {
00263             DMD_writeColor(pixelX + 0, pixelY + i, 0x00, 0x00, 0x00, 1);
00264           }
00265           if (bitField & 0x02)
00266           {
00267             DMD_writeColor(pixelX + 1, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00268           }
00269           else
00270           {
00271             DMD_writeColor(pixelX + 1, pixelY + i, 0x00, 0x00, 0x00, 1);
00272           }
00273           if (bitField & 0x04)
00274           {
00275             DMD_writeColor(pixelX + 2, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00276           }
00277           else
00278           {
00279             DMD_writeColor(pixelX + 2, pixelY + i, 0x00, 0x00, 0x00, 1);
00280           }
00281           if (bitField & 0x08)
00282           {
00283             DMD_writeColor(pixelX + 3, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00284           }
00285           else
00286           {
00287             DMD_writeColor(pixelX + 3, pixelY + i, 0x00, 0x00, 0x00, 1);
00288           }
00289           if (bitField & 0x10)
00290           {
00291             DMD_writeColor(pixelX + 4, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00292           }
00293           else
00294           {
00295             DMD_writeColor(pixelX + 4, pixelY + i, 0x00, 0x00, 0x00, 1);
00296           }
00297           if (bitField & 0x20)
00298           {
00299             DMD_writeColor(pixelX + 5, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00300           }
00301           else
00302           {
00303             DMD_writeColor(pixelX + 5, pixelY + i, 0x00, 0x00, 0x00, 1);
00304           }
00305           if (bitField & 0x40)
00306           {
00307             DMD_writeColor(pixelX + 6, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00308           }
00309           else
00310           {
00311             DMD_writeColor(pixelX + 6, pixelY + i, 0x00, 0x00, 0x00, 1);
00312           }
00313           if (bitField & 0x80)
00314           {
00315             DMD_writeColor(pixelX + 7, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00316           }
00317           else
00318           {
00319             DMD_writeColor(pixelX + 7, pixelY + i, 0x00, 0x00, 0x00, 1);
00320           }
00321         }
00322       }
00323     }
00324   }
00325   else
00326   {
00327     /* Draw xpos, ypos only */
00328     c      = charBuffer[ypos][xpos];
00329     pixelX = xpos * 8;
00330     pixelY = ypos * 8;
00331     for (i = 0; i < 8; i++)
00332     {
00333       bitField = fontBits[c + 100 * i];
00334       if (bitField == 0)
00335       {
00336         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);
00337         continue;
00338       }
00339 
00340       if (bitField & 0x01)
00341 
00342       {
00343         DMD_writeColor(pixelX + 0, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00344       }
00345       else
00346       {
00347         DMD_writeColor(pixelX + 0, pixelY + i, 0x00, 0x00, 0x00, 1);
00348       }
00349       if (bitField & 0x02)
00350       {
00351         DMD_writeColor(pixelX + 1, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00352       }
00353       else
00354       {
00355         DMD_writeColor(pixelX + 1, pixelY + i, 0x00, 0x00, 0x00, 1);
00356       }
00357       if (bitField & 0x04)
00358       {
00359         DMD_writeColor(pixelX + 2, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00360       }
00361       else
00362       {
00363         DMD_writeColor(pixelX + 2, pixelY + i, 0x00, 0x00, 0x00, 1);
00364       }
00365       if (bitField & 0x08)
00366       {
00367         DMD_writeColor(pixelX + 3, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00368       }
00369       else
00370       {
00371         DMD_writeColor(pixelX + 3, pixelY + i, 0x00, 0x00, 0x00, 1);
00372       }
00373       if (bitField & 0x10)
00374       {
00375         DMD_writeColor(pixelX + 4, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00376       }
00377       else
00378       {
00379         DMD_writeColor(pixelX + 4, pixelY + i, 0x00, 0x00, 0x00, 1);
00380       }
00381       if (bitField & 0x20)
00382       {
00383         DMD_writeColor(pixelX + 5, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00384       }
00385       else
00386       {
00387         DMD_writeColor(pixelX + 5, pixelY + i, 0x00, 0x00, 0x00, 1);
00388       }
00389       if (bitField & 0x40)
00390       {
00391         DMD_writeColor(pixelX + 6, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00392       }
00393       else
00394       {
00395         DMD_writeColor(pixelX + 6, pixelY + i, 0x00, 0x00, 0x00, 1);
00396       }
00397       if (bitField & 0x80)
00398       {
00399         DMD_writeColor(pixelX + 7, pixelY + i, rgbColor[0], rgbColor[1], rgbColor[2], 1);
00400       }
00401       else
00402       {
00403         DMD_writeColor(pixelX + 7, pixelY + i, 0x00, 0x00, 0x00, 1);
00404       }
00405     }
00406   }
00407 }
00408 
00411 /**************************************************************************/
00416 int RETARGET_ReadChar(void)
00417 {
00418   return -1;
00419 }
00420 
00421 /**************************************************************************/
00426 int RETARGET_WriteChar(char c)
00427 {
00428   if ((BSP_RegisterRead(&BC_REGISTER->UIF_AEM) == BC_UIF_AEM_EFM))
00429   {
00430     if ((BSP_RegisterRead(&BC_REGISTER->ARB_CTRL) != BC_ARB_CTRL_EBI) || (initialized == false))
00431     {
00432       if (initialized)
00433       {
00434         bufferReset = false;
00435         tftReset    = true;
00436         RETARGET_SerialInit();
00437       }
00438       else
00439       {
00440         bufferReset = true;
00441         tftReset    = true;
00442         RETARGET_SerialInit();
00443       }
00444       fullUpdate = true;
00445     }
00446   }
00447 
00448   /* Check for form feed - clear screen */
00449   if (c == '\f')
00450   {
00451     bufferReset = true;
00452     tftReset    = false;
00453     RETARGET_SerialInit();
00454     fullUpdate = true;
00455     return c;
00456   }
00457 
00458   /* Add CR or LF to CRLF if enabled */
00459   if (LFtoCRLF && (c == '\n'))
00460   {
00461     RETARGET_TFTTX('\r');
00462   }
00463   RETARGET_TFTTX(c);
00464 
00465   if (LFtoCRLF && (c == '\r'))
00466   {
00467     RETARGET_TFTTX('\n');
00468   }
00469 
00470   /* Update display */
00471   RETARGET_TFTUpdate(fullUpdate);
00472   fullUpdate = false;
00473   return c;
00474 }