#include <stdint.h>
#include <stdbool.h>
#include "C:/ti/TivaWare_C_Series-1.1/inc/hw_types.h"
#include "C:/ti/TivaWare_C_Series-1.1/inc/hw_memmap.h"
#include "C:/ti/TivaWare_C_Series-1.1/driverlib/sysctl.h"
#include "C:/ti/TivaWare_C_Series-1.1/driverlib/gpio.h"
#include "C:/ti/TivaWare_C_Series-1.1/inc/hw_gpio.h"
#include "C:/ti/TivaWare_C_Series-1.1/inc/tm4c123gh6pm.h"

// Define pin to LED color mapping.

#define RED_LED   GPIO_PIN_1
#define BLUE_LED  GPIO_PIN_2
#define GREEN_LED GPIO_PIN_3
#define  B2 GPIO_PIN_0
#define  B1 GPIO_PIN_4

//Define counts for time periods clock tick is 20ns
#define T50ms 50000000/20
#define MAXcount 0x00ffffff
#define T750ms 750000000/20
#define T300ms 300000000/20
#define T500ms 500000000/20
#define T200ms 200000000/20


void SysTickDelay (int32_t );
void LEDon (int32_t );
int32_t TotalDelay(int32_t );
int32_t ReadSW1(void);
void Initialize	(void);
  

// The error routine that is called if the driver library encounters an error.
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif	

	

void SysTickDelay (int32_t Scount)
    {
					
     
    // Setup the system clock to run at 50 Mhz from PLL with crystal reference
    SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
	
	//to enable timer features 
        NVIC_ST_CTRL_R = 0;
	//to specifies the start value to load into the timer 
	      NVIC_ST_RELOAD_R = Scount;
	//containds the current value of the timer 
	      NVIC_ST_CURRENT_R = 0;
	//enable /disable time
	      NVIC_ST_CTRL_R = 0x00000005; 
		
	 while ((NVIC_ST_CTRL_R&0x10000)==0){}
	 }
	
	 void LEDon (int32_t LED)
	 {
   
	 GPIOPinWrite(GPIO_PORTF_BASE, 0x0e, LED);
	
	 
	 }

  
	 int32_t ReadSW1(void)
  {

	int32_t s1state;
	
	s1state = !GPIOPinRead(GPIO_PORTF_BASE, B1);
	
	return (s1state);
  }

	int32_t TotalDelay(int32_t Tcount)
				{
					int32_t STATUS = 0;
					
					
							while (Tcount > MAXcount)
							{
								STATUS = -1;
								SysTickDelay(MAXcount);
								Tcount = Tcount - MAXcount;
					    }
				      SysTickDelay(Tcount);
					    return (STATUS);
				}
				
		void Initialize	(void)	
		{		
    // Enable and configure the GPIO port for the LED and switches operation.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
	
  GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, RED_LED|BLUE_LED|GREEN_LED);
	
	//to unlock the GPIO at portF
	HWREG(GPIO_PORTF_BASE+GPIO_O_LOCK) = GPIO_LOCK_KEY;
  HWREG(GPIO_PORTF_BASE+GPIO_O_CR) = 0x11;
	
	
  GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, B1|B2);
	
	GPIOPadConfigSet(GPIO_PORTF_BASE, B1|B2 , GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

    }
				
int main()
{
	
 int32_t MStatus ;
 int32_t Mswitchonestate = 0;
 Initialize();
	
	
while(1)
		
	{	
	MStatus = TotalDelay(T300ms);
		if(MStatus == -1)
		{
			LEDon (BLUE_LED);
		}
		else
		{
			LEDon (RED_LED);
		}
		Mswitchonestate = ReadSW1();
		if (Mswitchonestate)
		{
		MStatus = TotalDelay(T750ms);
		}
		else
		{
		MStatus = TotalDelay(T200ms);
		}
		Mswitchonestate = ReadSW1();
		if(MStatus == -1)
		{
			LEDon( GREEN_LED);
		}
		else
		{
			LEDon( RED_LED|GREEN_LED);
		}
		MStatus = TotalDelay(T500ms);
		if (MStatus == -1)
		{ 
			LEDon( BLUE_LED|RED_LED);
		}
		else
		{
			LEDon( BLUE_LED|GREEN_LED);
		}
		TotalDelay(T300ms);
		
		LEDon(0);
	}
}

			



