/*

                         \\\|///
                       \\  - -  //
                        (  @ @  )
+---------------------oOOo-(_)-oOOo-------------------------+
|              Luminaryボードのサンプルプログラム           |
|                      ビット処理実験                       |
|                      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 "systick.h"

#include "PCD5544.h"

static volatile unsigned long g_ulValue;

/*----------------------------------------------------------*\
 | The error routine that is called			                |
 | if the driver library encounters an error.               |
\*----------------------------------------------------------*/
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
/*---------------------------------------------------------------------------*\
 | Delay for the specified number of seconds.  Depending upon the current    |
 | SysTick value, the delay will be between N-1 and N seconds (i.e. N-1 full |
 | seconds are guaranteed, along with the remainder of the current second).	 |
\*---------------------------------------------------------------------------*/
void
DelaySec(unsigned long ulSeconds)
{
    //
    // Loop while there are more seconds to wait.
    //
    while(ulSeconds--)
    {
        //
        // Wait until the SysTick value is less than 1000.
        //
        while(SysTickValueGet() > 1000)
        {
        }

        //
        // Wait until the SysTick value is greater than 1000.
        //
        while(SysTickValueGet() < 1000)
        {
        }
    }
}
/*----------------------------------------------------------*\
 | Print the given value as a hexadecimal string on the LCD.|
\*----------------------------------------------------------*/
static const char * const pcHex = "0123456789ABCDEF";
void PrintValue(unsigned long ulValue) {
    LCD_GotoXY(12, 3);
	LCD_Putchar( ' ' , 1 );
	LCD_Putchar( pcHex[((ulValue >> 28) & 15)] , 1 );
	LCD_Putchar( pcHex[((ulValue >> 24) & 15)] , 1 );
	LCD_Putchar( pcHex[((ulValue >> 20) & 15)] , 1 );
	LCD_Putchar( pcHex[((ulValue >> 16) & 15)] , 1 );
	LCD_Putchar( pcHex[((ulValue >> 12) & 15)] , 1 );
	LCD_Putchar( pcHex[((ulValue >>  8) & 15)] , 1 );
	LCD_Putchar( pcHex[((ulValue >>  4) & 15)] , 1 );
	LCD_Putchar( pcHex[((ulValue >>  0) & 15)] , 1 );
	LCD_Putchar( ' ' , 1 );
    }
/*----------------------------------------------------------*\
 | MIAN ENTRY                                               |
\*----------------------------------------------------------*/
int main(void) {
//    volatile unsigned long ulLoop;
    unsigned long ulErrors, ulIdx;

    // 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 and buzzer.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_5 | GPIO_PIN_6, 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(12, 2);	LCD_PrintStr("GPIO Input", 0);
	LCD_GotoXY(12, 4);	LCD_PrintStr("CSUN Tech.", 0);
	LCD_GotoXY(0, 5);	LCD_PrintStr("www.csun.co.jp", 0);
	//LCD_Backlight_Off();

    // Set up and enable the SysTick timer.  It will be used as a reference
    // for delay loops.  The SysTick timer period will be set up for one
    // second.
    SysTickPeriodSet(SysCtlClockGet());
    SysTickEnable();
    // Set the value and error count to zero.
    g_ulValue = 0;
    ulErrors = 0;
    // Print the initial value to the LCD.
    PrintValue(g_ulValue);
    // Delay for 1 second.
    DelaySec(1);

    // Set the value to 0xdecafbad using bit band accesses to each individual
    // bit.
    for(ulIdx = 0; ulIdx < 32; ulIdx++)
    {
        //
        // Set this bit.
        //
        HWREGBITW(&g_ulValue, 31 - ulIdx) = (0xdecafbad >> (31 - ulIdx)) & 1;

        //
        // Print the current value to the LCD.
        //
        PrintValue(g_ulValue);

        //
        // Delay for 1 second.
        //
        DelaySec(1);
    }

    // Make sure that the value is 0xdecafbad.
    //
    if(g_ulValue != 0xdecafbad)
    {
        ulErrors++;
    }

    //
    // Make sure that the individual bits read back correctly.
    //
    for(ulIdx = 0; ulIdx < 32; ulIdx++)
    {
        if(HWREGBITW(&g_ulValue, ulIdx) != ((0xdecafbad >> ulIdx) & 1))
        {
            ulErrors++;
        }
    }

    // Delay for 2 seconds.
    DelaySec(2);

    if(ulErrors)
    {
        LCD_GotoXY(12, 3); LCD_PrintStr("  Errors! ", 1);
    }
    else
    {
        LCD_GotoXY(12, 3); LCD_PrintStr(" Success! ", 1);
    }

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