Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 31st, 2012  |  syntax: C  |  size: 7.87 KB  |  hits: 46  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include "includes.h"
  3. #include "yfuns.h"
  4.  
  5. extern Int32U SDRAM_VRAM_BASE_ADDR;
  6.  
  7. // Fonts.
  8. extern FontType_t Terminal_6_8_6;
  9. extern FontType_t Terminal_9_12_6;
  10. extern FontType_t Terminal_18_24_12;
  11.  
  12.  
  13. // Function prototypes.
  14. void setLCDBackground(int pixColour);
  15. void initDarkGLCD();
  16. void delayVar(int ticks);
  17. void printMessage(char *newMessage);
  18. void trimMessage();
  19.  
  20. int        messageCount;
  21. char       *currMessage;
  22. FontType_t currFont;
  23.  
  24. #define MESSAGE_LIMIT 20
  25.  
  26. /** external data **/
  27. #pragma section=".intvec"
  28. /** public functions **/
  29. /*************************************************************************
  30.  * Function Name: InitClock
  31.  * Parameters: void
  32.  * Return: void
  33.  *
  34.   * Description: Initialize PLL0 and clocks' dividers. PLL = 96MHz,
  35.  *               CPU - 96MHz, PCLK - 48 MHz
  36.  *
  37.  *************************************************************************/
  38. void InitClock(void)
  39. {
  40.   /*Sys Clock Select as CPU clock
  41.     divider 1:1*/
  42.   CLK_SetCpuClk(CPUSEL_CLKSYS,1);
  43.   /*Select IRC oscilator as Sys clock*/
  44.   CLK_SetSysClk(SYSSEL_IRCOSC);
  45.   /*Enable Main oscilator*/
  46.   CLK_MainOscSet(CLK_ENABLE,MOSCRNG_1_20MHZ);
  47.   /*Select Main oscilator as Sys clock*/
  48.   CLK_SetSysClk(SYSSEL_MOSC);
  49.   /*Enable set PLL 96Hz (M = 8; P = 1)*/
  50.   CLK_SetMainPll(CLK_ENABLE, 8-1, 0);
  51.   /*Set peripheral divider 1:2. Peripheral clock 48MHz*/
  52.   CLK_SetPeriphClk(2);
  53.   /*PLL0 out is used as the input to the CPU clock divider
  54.   divider 1:1. CPU runs at 96MHz*/
  55.   CLK_SetCpuClk(CPUSEL_CLKPLL,1);
  56. }
  57.  
  58. /*************************************************************************
  59.  * Function Name: __low_level_init
  60.  * Parameters: void
  61.  * Return:
  62.  *
  63.  * Description: Low level system init (clock, flash accelerator,
  64.  *              SDRAM and vector table address)
  65.  *              
  66.  *
  67.  *************************************************************************/
  68. int __low_level_init (void)
  69. {
  70.   /*if debug is in SDRAM
  71.     clock and SDRAM initialization is made
  72.     in mac file*/
  73. #ifndef SDRAM_DEBUG
  74.   int cpuclk;
  75.   /* Flash accelerator save value*/
  76.   FLASHCFG = (6UL<<12) | 0x3AUL;
  77.   /*Clock Init*/
  78.   InitClock();
  79.   /*Flash accelerator init*/
  80.   cpuclk = CLK_GetClock(CLK_CPU);
  81.  
  82.   if( cpuclk < 20000000 ){
  83.     FLASHCFG = (0x0UL<<12) | 0x3AUL;
  84.   } else if( cpuclk < 40000000 ){
  85.     FLASHCFG = (0x1UL<<12) | 0x3AUL;
  86.   } else if( cpuclk< 60000000 ){
  87.     FLASHCFG = (0x2UL<<12) | 0x3AUL;
  88.   } else if( cpuclk < 80000000 ){
  89.     FLASHCFG = (0x3UL<<12) | 0x3AUL;
  90.   } else if( cpuclk < 100000000 ){
  91.     FLASHCFG = (0x4UL<<12) | 0x3AUL;
  92.   }
  93.   /*SDRAM init*/
  94.   SDRAM_Init();
  95. #endif
  96.   /*Set vector table location*/
  97.   VTOR  = (unsigned int)__segment_begin(".intvec");
  98.  
  99.   return  1;
  100. }
  101.  
  102. // Params: red   - (off - 0, on - !off).
  103. //         green - (off - 0, on - !off).
  104. //         blue  - (off - 0, on - !off).
  105. //         alpha - (0 - 255).
  106. // Returns: None
  107. // Description: Sets the background to the specified RGBA value.
  108. void setLCDBackground(int pixColour)
  109. {
  110.  
  111.   int LCD_VRAM_BASE_ADDR   = ((Int32U)&SDRAM_VRAM_BASE_ADDR);
  112.   pInt32U pDst             = (pInt32U) LCD_VRAM_BASE_ADDR;
  113.  
  114.    // Initialise colour pallette.
  115.   Int32U pix = 0;
  116.  
  117.   // Set background.
  118.   for(Int32U v=0; v < C_GLCD_V_SIZE; v++)
  119.   {
  120.     pDst = (pInt32U)LCD_VRAM_BASE_ADDR + v*C_GLCD_H_SIZE;
  121.     Int32U i = 0;
  122.      
  123.     pix = pixColour;
  124.      
  125.     for(; i < C_GLCD_H_SIZE; i++)
  126.     {
  127.       *(pDst+i) = pix;
  128.     }
  129.   }
  130.  
  131.   for(volatile Int32U i = C_GLCD_ENA_DIS_DLY; i; i--);
  132. }
  133.  
  134. // Params: None
  135. // Returns: None
  136. // Description: Makes the LCD display a dark background.
  137. void initDarkGLCD()
  138. {
  139.   GLCD_Ctrl(FALSE);
  140.   GLCD_Init(NULL, NULL);
  141.   setLCDBackground(0x00332222);
  142.   GLCD_Ctrl(TRUE);
  143. }
  144.  
  145. // Params: number of ticks - determines duration of delay.
  146. // Returns: None
  147. // Description: Produces a delay of variable duration.
  148. void delayVar(int ticks)
  149. {
  150.   unsigned int i;
  151.  
  152.   for (i = 0; i < ticks; i++)
  153.   {
  154.  
  155.   }
  156. }
  157.  
  158. // Params: newMessage - the message to be printed to the LCD.
  159. // Returns: None
  160. // Description: Prints the given message to the LCD.
  161. void printMessage(char *newMessage)
  162. {
  163.   size_t lf = strlen(currMessage);
  164.   size_t ls = strlen(newMessage);
  165.  
  166.   messageCount++;
  167.  
  168.   char *tempMessage = (char*)malloc((lf + ls + 3) * sizeof(char));
  169.  
  170.   strcpy(tempMessage, currMessage);
  171.   tempMessage[lf] = '\n';
  172.   strcpy(&tempMessage[lf+1], newMessage);
  173.   tempMessage[lf + ls + 1] = '\r';
  174.  
  175.   // tempMessage = currMessage + '\n' + newMessage + '\r' + '\0'.
  176.  
  177.   currMessage = malloc((lf + ls + 3) * sizeof(char));
  178.   for(int i=0; i < (lf + ls + 2); i++)
  179.   {
  180.     currMessage[i] = tempMessage[i];
  181.   }
  182.  
  183.   currMessage[lf + ls + 2] = '\0';
  184.  
  185.   free(tempMessage);
  186.   tempMessage = NULL;
  187.   //while(messageCount > MESSAGE_LIMIT) trimMessage();
  188.  
  189.   GLCD_print(currMessage);
  190. }
  191.  
  192. // Params: None
  193. // Returns: None
  194. // Description: Ensures that the messages fit on screen by deleting old
  195. //              messages if necessary.
  196. // UNTESTED
  197. void trimMessage()
  198. {
  199.   int trimIndex;
  200.   size_t msgSize = strlen(currMessage);
  201.  
  202.   for(int i=0; i < msgSize; i++)
  203.   {
  204.     if(currMessage[i] == '\r')
  205.     {
  206.       trimIndex = i;
  207.       break;
  208.     }
  209.   }
  210.   // e.g.: "\fHello World\rHow are you?\r".
  211.   char *trimMessage = malloc((msgSize - trimIndex) * sizeof(char));
  212.   trimMessage[0] = '\f';
  213.   strcpy(&trimMessage[1], &currMessage[trimIndex+1]);
  214.  
  215.   currMessage = malloc((msgSize - trimIndex) * sizeof(char));
  216.   for(int i=0; i < msgSize - trimIndex; i++)
  217.   {
  218.     currMessage[i] = trimMessage[i];
  219.   }
  220.  
  221.   currMessage[msgSize - trimIndex - 1] = '\0';
  222.  
  223.   free(trimMessage);
  224.   trimMessage = NULL;
  225.  
  226.   messageCount--;
  227. }
  228.  
  229.  
  230. int main()
  231. {
  232.   messageCount = 0;
  233.   currMessage  = "\fProgram started\r";
  234.   currFont     = Terminal_9_12_6;
  235.  
  236.   __disable_interrupt();
  237.   /* Longest allocatable string. (972)
  238.   char* message = malloc(strlen("0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
  239.                                 "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
  240.                                 "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
  241.                                 "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
  242.                                 "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
  243.                                 "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
  244.                                 "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
  245.                                 "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
  246.                                 "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
  247.                                 "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
  248.                                 "0x00ffffff, 0x00ffffff"));
  249.   printf("%p\n", message);
  250.   */
  251.   initDarkGLCD();
  252.  
  253.   // Configure LCD for printing to.
  254.   GLCD_SetWindow(10, 10, 310, 230);
  255.   GLCD_TextSetPos(0, 0);
  256.   GLCD_SetFont(&Terminal_9_12_6, 0x0000FF00, 0x0);
  257.  
  258.   __enable_interrupt();
  259.  
  260.   for(int i=0; i < 20; i++)
  261.   {
  262.     printf("%i\n", i);
  263.     //printf("currMessage: %s\n", currMessage);
  264.     /*
  265.     char buffer[40];
  266.     int writeCount = sprintf(buffer, "Message %i", i+1);
  267.     char *message = malloc((writeCount + 1) * sizeof(char));
  268.     for(int i=0; i < writeCount; i++)
  269.     {
  270.       message[i] = buffer[i];
  271.     }
  272.     */
  273.     printMessage("test message");
  274.   }
  275.  
  276.   printf("currMessage: %s\n", currMessage);
  277.  
  278.   while(1)
  279.   {
  280.     delayVar(10000000);
  281.   }
  282. }