// ***** 1. Pre-processor Directives Section *****
#include "TExaS.h"
#include "tm4c123gh6pm.h"
#include "PLL.h"
#include "SysTick.c"
#define TrafficLIGHT    (*((volatile unsigned long *)0x400050FC))
#define WalkersLight            (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTB_OUT    (*((volatile unsigned long *)0x400050FC)) // bits 5-0
#define GPIO_PORTB_DIR_R    (*((volatile unsigned long *)0x40005400))
#define GPIO_PORTB_AFSEL_R    (*((volatile unsigned long *)0x40005420))
#define GPIO_PORTB_DEN_R    (*((volatile unsigned long *)0x4000551C))
#define GPIO_PORTB_AMSEL_R    (*((volatile unsigned long *)0x40005528))
#define GPIO_PORTB_PCTL_R    (*((volatile unsigned long *)0x4000552C))
#define GPIO_PORTE_IN    (*((volatile unsigned long *)0x4002401C)) // bits 2 -0
#define SENSOR    (*((volatile unsigned long *)0x4002401C))
#define GPIO_PORTE_DIR_R    (*((volatile unsigned long *)0x40024400))
#define GPIO_PORTE_AFSEL_R    (*((volatile unsigned long *)0x40024420))
#define GPIO_PORTE_DEN_R    (*((volatile unsigned long *)0x4002451C))
#define GPIO_PORTE_AMSEL_R    (*((volatile unsigned long *)0x40024528))
#define GPIO_PORTE_PCTL_R    (*((volatile unsigned long *)0x4002452C))
#define SYSCTL_RCGC2_R    (*((volatile unsigned long *)0x400FE108))
#define SYSCTL_RCGC2_GPIOE    0x00000010  // port E Clock Gating Control
#define SYSCTL_RCGC2_GPIOB    0x00000002  // port B Clock Gating Control
#define GPIO_PORTF_DATA_R    (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R    (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_AFSEL_R    (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_PUR_R    (*((volatile unsigned long *)0x40025510))
#define GPIO_PORTF_DEN_R    (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_LOCK_R    (*((volatile unsigned long *)0x40025520))
#define GPIO_PORTF_CR_R    (*((volatile unsigned long *)0x40025524))
#define GPIO_PORTF_AMSEL_R    (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_PCTL_R    (*((volatile unsigned long *)0x4002552C))
#define PF3    (*((volatile unsigned long *)0x40025020))
#define PF1    (*((volatile unsigned long *)0x40025008))
#define GPIO_PORTF_DR2R_R    (*((volatile unsigned long *)0x40025500))
#define GPIO_PORTF_DR4R_R    (*((volatile unsigned long *)0x40025504))
#define GPIO_PORTF_DR8R_R    (*((volatile unsigned long *)0x40025508))
#define GPIO_LOCK_KEY    0x4C4F434B  // Unlocks the GPIO_CR register
#define SYSCTL_RCGC2_R    (*((volatile unsigned long *)0x400FE108))
// ***** 2. Global Declarations Section *****
// FUNCTION PROTOTYPES: Each subroutine defined
void DisableInterrupts(void); // Disable interrupts
void EnableInterrupts(void);  // Enable interrupts
// Linked Data Structure
struct State {
unsigned long Out;
unsigned long WalkOut;
unsigned long Time;
unsigned long Next [8];};
typedef const struct State STyp;
#define GoWest           0
#define SlowWest          1
#define GoSouth            2
#define SlowSouth          3
#define Walk                4
#define DontWalkA            5
#define DontWalkOff         6
#define DontWalkB            7
#define AllBusy              8
// Traffic Lights, Walker Lights, Delay, Next State for Input
STyp FSM [9] ={
   {0x0C, 0x02, 300, {GoWest, GoWest, SlowWest, SlowWest, SlowWest, SlowWest, SlowWest, SlowWest}},
   {0x14, 0x02, 50,  {GoSouth, GoSouth, GoSouth, GoSouth, Walk, Walk, GoSouth, GoSouth}},
   {0x21, 0x02, 300, {GoSouth, SlowSouth, GoSouth, SlowSouth, SlowSouth, SlowSouth, SlowSouth, SlowSouth}},
   {0x22, 0x02, 50,  {GoWest, GoWest, GoWest, GoWest, Walk, Walk, Walk, Walk}},
   {0x24, 0x02, 300, {Walk, DontWalkA, DontWalkA, DontWalkA, DontWalkA, DontWalkA, DontWalkA, DontWalkA}},
   {0x24, 0x08, 15,  {DontWalkOff, DontWalkOff, DontWalkOff, DontWalkOff, DontWalkOff, DontWalkOff, DontWalkOff, DontWalkOff}},
   {0x24, 0x02, 15,  {DontWalkB, DontWalkB, DontWalkB, DontWalkB, DontWalkB, DontWalkB, DontWalkB, DontWalkB}},
   {0x24, 0x02, 15, {AllBusy, AllBusy, AllBusy, AllBusy, AllBusy, AllBusy, AllBusy, AllBusy}},
   {0x24, 0x00, 50, {GoWest, GoWest, GoSouth, GoWest, Walk, GoWest, GoSouth, GoWest}}};
unsigned long S;  // index to the current state
unsigned long Input;
// ***** 3. Subroutines Section *****
int main(void){ volatile unsigned long delay;
TExaS_Init(SW_PIN_PE210, LED_PIN_PB543210); // activate grader and set system clock to 80 MHz
SysTick_Init();  // Program 10.2
SYSCTL_RCGC2_R |= 0x32;
delay = SYSCTL_RCGC2_R;    // 2) no need to unlock
GPIO_PORTE_DEN_R |= 0x07;  // 7) enable digital on PE2-0
GPIO_PORTE_DIR_R &= ~0x07;  // 5) inputs on PE2-0
GPIO_PORTB_DIR_R |= 0x3F;  // 5) outputs on PB5-0
GPIO_PORTB_AFSEL_R &= ~0x3F; // 6) regular function on PB5-0
GPIO_PORTB_DEN_R |= 0x3F;  // 7) enable digital on PB5-0
GPIO_PORTF_LOCK_R = 0x4C4F434B;  // 2) unlock PortF PF0
GPIO_PORTF_CR_R |= 0x1F;    // allow changes to PF4-0
GPIO_PORTF_DIR_R |= 0x08;    // 5.2) PF3 green output
GPIO_PORTF_DIR_R |= 0x02;    // 5.2) PF1 red output
GPIO_PORTF_DEN_R |= 0x1F;    // 7) enable digital pins PF4-PF0
GPIO_PORTF_PCTL_R &= 0x00000000;  // 4) GPIO clear bit PCTL
GPIO_PORTE_PCTL_R &= 0x00000000; // 4) enable regular GPIO
GPIO_PORTB_PCTL_R &= 0x00000000; // 4) enable regular GPIO
EnableInterrupts();
while(1){
  TrafficLIGHT = FSM.Out;  // set lights
   WalkersLight = FSM.WalkOut; //set walker lights
  SysTick_Wait10ms(FSM.Time);
  Input = SENSOR;   // read sensors
  S = FSM.Next[Input];  
}
}