/*

                         \\\|///
                       \\  - -  //
                        (  @ @  )
+---------------------oOOo-(_)-oOOo-------------------------+
|              Luminaryボードのサンプルプログラム           |
|                        UART実験                           |
|                      by Xiaoran Liu                       |
|                        2008.4.16                          |
|                                                           |
|                  株式会社 日昇テクノロジー                |
|                    http://www.csun.co.jp                  |
|                             Oooo                          |
+-----------------------oooO--(   )-------------------------+
                       (   )   ) /
                        \ (   (_/
                         \_)     

*/
#include "hw_memmap.h"
#include "hw_types.h"
#include "debug.h"
#include "gpio.h"
#include "timer.h"
#include "sysctl.h"

#include "uart.h"
#include "interrupt.h"
#include "hw_ints.h"

#include "PCD5544.h"
/*----------------------------------------------------------*\
 | The error routine that is called			                |
 | if the driver library encounters an error.               |
\*----------------------------------------------------------*/
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
//*****************************************************************************
//
// The UART interrupt handler.
//
//*****************************************************************************
void
UARTIntHandler(void)
{
    unsigned long ulStatus;

    //
    // Get the interrrupt status.
    //
    ulStatus = UARTIntStatus(UART0_BASE, true);

    //
    // Clear the asserted interrupts.
    //
    UARTIntClear(UART0_BASE, ulStatus);

    //
    // Loop while there are characters in the receive FIFO.
    //
    while(UARTCharsAvail(UART0_BASE))
    {
        //
        // Read the next character from the UART and write it back to the UART.
        //
        UARTCharPutNonBlocking(UART0_BASE, UARTCharGetNonBlocking(UART0_BASE));
    }
}

//*****************************************************************************
//
// Send a string to the UART.
//
//*****************************************************************************
void
UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
    //
    // Loop while there are more characters to send.
    //
    while(ulCount--)
    {
        //
        // Write the next character to the UART.
        //
        UARTCharPutNonBlocking(UART0_BASE, *pucBuffer++);
    }
}
/*----------------------------------------------------------*\
 | MIAN ENTRY                                               |
\*----------------------------------------------------------*/
int main(void) {
    volatile unsigned long ulLoop;

    // Set the clocking to run directly from the crystal.
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_6MHZ);

  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
  // Enable the GPIO pin to read the select button.
  GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_4, GPIO_DIR_MODE_IN);
  GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); 
  if( GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_4) == 0 ) for(;;);

    // Configure the GPIO that drives the on-board LED.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_5, GPIO_DIR_MODE_OUT);

	LCD_Backlight_On();
	LCD_Init();
	LCD_Cls();
	LCD_GotoXY(22, 0);	LCD_PrintStr("LM3S102", 0);
	LCD_GotoXY(12, 1);	LCD_PrintStr("Eval Board", 0);
	LCD_GotoXY(16, 2);	LCD_PrintStr("UART TEST", 1);
	LCD_GotoXY(10, 3);	LCD_PrintStr("9600, 8-N-1", 1);
	LCD_GotoXY(12, 4);	LCD_PrintStr("CSUN Tech.", 0);
	LCD_GotoXY(0, 5);	LCD_PrintStr("www.csun.co.jp", 0);
	//LCD_Backlight_Off();

    // Enable the peripherals used by this example.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    // Enable processor interrupts.
    IntMasterEnable();
    // Set GPIO A0 and A1 as UART pins.
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    // Configure the UART for 9600, 8-N-1 operation.
    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 9600,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));
    // Enable the UART interrupt.
    IntEnable(INT_UART0);
    UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);
    // Prompt for text to be entered.
    UARTSend((unsigned char *)"Hello! Enter text: ", 19);

    // Loop forever.
    while(1) {
		if( GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_4) == 0 ) {
	        // Turn on the LED.
	        GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0);
			}
		else {
	        // Turn off the LED.
	        GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, GPIO_PIN_5);
			}
	    }
	}
/*----------------------------------------------------------*\
 | END OF FILE                                              |
\*----------------------------------------------------------*/
