Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "includes.h"
- #include "yfuns.h"
- extern Int32U SDRAM_VRAM_BASE_ADDR;
- // Fonts.
- extern FontType_t Terminal_6_8_6;
- extern FontType_t Terminal_9_12_6;
- extern FontType_t Terminal_18_24_12;
- // Function prototypes.
- void setLCDBackground(int pixColour);
- void initDarkGLCD();
- void delayVar(int ticks);
- void printMessage(char *newMessage);
- void trimMessage();
- int messageCount;
- char *currMessage;
- FontType_t currFont;
- #define MESSAGE_LIMIT 20
- /** external data **/
- #pragma section=".intvec"
- /** public functions **/
- /*************************************************************************
- * Function Name: InitClock
- * Parameters: void
- * Return: void
- *
- * Description: Initialize PLL0 and clocks' dividers. PLL = 96MHz,
- * CPU - 96MHz, PCLK - 48 MHz
- *
- *************************************************************************/
- void InitClock(void)
- {
- /*Sys Clock Select as CPU clock
- divider 1:1*/
- CLK_SetCpuClk(CPUSEL_CLKSYS,1);
- /*Select IRC oscilator as Sys clock*/
- CLK_SetSysClk(SYSSEL_IRCOSC);
- /*Enable Main oscilator*/
- CLK_MainOscSet(CLK_ENABLE,MOSCRNG_1_20MHZ);
- /*Select Main oscilator as Sys clock*/
- CLK_SetSysClk(SYSSEL_MOSC);
- /*Enable set PLL 96Hz (M = 8; P = 1)*/
- CLK_SetMainPll(CLK_ENABLE, 8-1, 0);
- /*Set peripheral divider 1:2. Peripheral clock 48MHz*/
- CLK_SetPeriphClk(2);
- /*PLL0 out is used as the input to the CPU clock divider
- divider 1:1. CPU runs at 96MHz*/
- CLK_SetCpuClk(CPUSEL_CLKPLL,1);
- }
- /*************************************************************************
- * Function Name: __low_level_init
- * Parameters: void
- * Return:
- *
- * Description: Low level system init (clock, flash accelerator,
- * SDRAM and vector table address)
- *
- *
- *************************************************************************/
- int __low_level_init (void)
- {
- /*if debug is in SDRAM
- clock and SDRAM initialization is made
- in mac file*/
- #ifndef SDRAM_DEBUG
- int cpuclk;
- /* Flash accelerator save value*/
- FLASHCFG = (6UL<<12) | 0x3AUL;
- /*Clock Init*/
- InitClock();
- /*Flash accelerator init*/
- cpuclk = CLK_GetClock(CLK_CPU);
- if( cpuclk < 20000000 ){
- FLASHCFG = (0x0UL<<12) | 0x3AUL;
- } else if( cpuclk < 40000000 ){
- FLASHCFG = (0x1UL<<12) | 0x3AUL;
- } else if( cpuclk< 60000000 ){
- FLASHCFG = (0x2UL<<12) | 0x3AUL;
- } else if( cpuclk < 80000000 ){
- FLASHCFG = (0x3UL<<12) | 0x3AUL;
- } else if( cpuclk < 100000000 ){
- FLASHCFG = (0x4UL<<12) | 0x3AUL;
- }
- /*SDRAM init*/
- SDRAM_Init();
- #endif
- /*Set vector table location*/
- VTOR = (unsigned int)__segment_begin(".intvec");
- return 1;
- }
- // Params: red - (off - 0, on - !off).
- // green - (off - 0, on - !off).
- // blue - (off - 0, on - !off).
- // alpha - (0 - 255).
- // Returns: None
- // Description: Sets the background to the specified RGBA value.
- void setLCDBackground(int pixColour)
- {
- int LCD_VRAM_BASE_ADDR = ((Int32U)&SDRAM_VRAM_BASE_ADDR);
- pInt32U pDst = (pInt32U) LCD_VRAM_BASE_ADDR;
- // Initialise colour pallette.
- Int32U pix = 0;
- // Set background.
- for(Int32U v=0; v < C_GLCD_V_SIZE; v++)
- {
- pDst = (pInt32U)LCD_VRAM_BASE_ADDR + v*C_GLCD_H_SIZE;
- Int32U i = 0;
- pix = pixColour;
- for(; i < C_GLCD_H_SIZE; i++)
- {
- *(pDst+i) = pix;
- }
- }
- for(volatile Int32U i = C_GLCD_ENA_DIS_DLY; i; i--);
- }
- // Params: None
- // Returns: None
- // Description: Makes the LCD display a dark background.
- void initDarkGLCD()
- {
- GLCD_Ctrl(FALSE);
- GLCD_Init(NULL, NULL);
- setLCDBackground(0x00332222);
- GLCD_Ctrl(TRUE);
- }
- // Params: number of ticks - determines duration of delay.
- // Returns: None
- // Description: Produces a delay of variable duration.
- void delayVar(int ticks)
- {
- unsigned int i;
- for (i = 0; i < ticks; i++)
- {
- }
- }
- // Params: newMessage - the message to be printed to the LCD.
- // Returns: None
- // Description: Prints the given message to the LCD.
- void printMessage(char *newMessage)
- {
- size_t lf = strlen(currMessage);
- size_t ls = strlen(newMessage);
- messageCount++;
- char *tempMessage = (char*)malloc((lf + ls + 3) * sizeof(char));
- strcpy(tempMessage, currMessage);
- tempMessage[lf] = '\n';
- strcpy(&tempMessage[lf+1], newMessage);
- tempMessage[lf + ls + 1] = '\r';
- // tempMessage = currMessage + '\n' + newMessage + '\r' + '\0'.
- currMessage = malloc((lf + ls + 3) * sizeof(char));
- for(int i=0; i < (lf + ls + 2); i++)
- {
- currMessage[i] = tempMessage[i];
- }
- currMessage[lf + ls + 2] = '\0';
- free(tempMessage);
- tempMessage = NULL;
- //while(messageCount > MESSAGE_LIMIT) trimMessage();
- GLCD_print(currMessage);
- }
- // Params: None
- // Returns: None
- // Description: Ensures that the messages fit on screen by deleting old
- // messages if necessary.
- // UNTESTED
- void trimMessage()
- {
- int trimIndex;
- size_t msgSize = strlen(currMessage);
- for(int i=0; i < msgSize; i++)
- {
- if(currMessage[i] == '\r')
- {
- trimIndex = i;
- break;
- }
- }
- // e.g.: "\fHello World\rHow are you?\r".
- char *trimMessage = malloc((msgSize - trimIndex) * sizeof(char));
- trimMessage[0] = '\f';
- strcpy(&trimMessage[1], &currMessage[trimIndex+1]);
- currMessage = malloc((msgSize - trimIndex) * sizeof(char));
- for(int i=0; i < msgSize - trimIndex; i++)
- {
- currMessage[i] = trimMessage[i];
- }
- currMessage[msgSize - trimIndex - 1] = '\0';
- free(trimMessage);
- trimMessage = NULL;
- messageCount--;
- }
- int main()
- {
- messageCount = 0;
- currMessage = "\fProgram started\r";
- currFont = Terminal_9_12_6;
- __disable_interrupt();
- /* Longest allocatable string. (972)
- char* message = malloc(strlen("0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
- "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
- "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
- "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
- "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
- "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
- "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
- "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
- "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
- "0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff "
- "0x00ffffff, 0x00ffffff"));
- printf("%p\n", message);
- */
- initDarkGLCD();
- // Configure LCD for printing to.
- GLCD_SetWindow(10, 10, 310, 230);
- GLCD_TextSetPos(0, 0);
- GLCD_SetFont(&Terminal_9_12_6, 0x0000FF00, 0x0);
- __enable_interrupt();
- for(int i=0; i < 20; i++)
- {
- printf("%i\n", i);
- //printf("currMessage: %s\n", currMessage);
- /*
- char buffer[40];
- int writeCount = sprintf(buffer, "Message %i", i+1);
- char *message = malloc((writeCount + 1) * sizeof(char));
- for(int i=0; i < writeCount; i++)
- {
- message[i] = buffer[i];
- }
- */
- printMessage("test message");
- }
- printf("currMessage: %s\n", currMessage);
- while(1)
- {
- delayVar(10000000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement