Skip to content

Instantly share code, notes, and snippets.

@benjic
Last active August 29, 2015 14:17
Show Gist options
  • Save benjic/a64cf1db8e3b5d3a8805 to your computer and use it in GitHub Desktop.
Save benjic/a64cf1db8e3b5d3a8805 to your computer and use it in GitHub Desktop.
// GistID: 105a8238cde06b80a8a7
#ifndef LIGHT_FSM
#define LIGHT_FSM
#define G_WEST 0
#define W_WEST 1
#define G_SOUTH 2
#define W_SOUTH 3
#define WALK 4
#define D_WALK_ON 5
#define D_WALK_OFF 6
struct State {
// A structure to represent light states and given transitions
unsigned long TrafficOutput; // 6 bits for each traffic light
unsigned long WalkOutput; // 2 bits for walk/don'tWalk
unsigned long Time; // Duration of state in 10*ms
unsigned long Next[8]; // Indices to next state traversal
};
typedef const struct State FSMState;
FSMState FSM[7] = { //0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07
{0x0C, 0x02, 10, G_WEST, G_WEST, W_WEST, W_WEST, W_WEST, W_WEST, W_WEST, W_WEST},
{0x14, 0x02, 5, G_SOUTH, G_SOUTH, G_SOUTH, G_SOUTH, WALK, WALK, WALK, G_SOUTH},
{0x21, 0x02, 10, G_SOUTH, W_SOUTH, G_SOUTH, W_SOUTH, W_SOUTH, W_SOUTH, W_SOUTH, W_SOUTH},
{0x22, 0x02, 5, G_WEST, G_WEST, G_WEST, G_WEST, WALK, WALK, WALK, WALK},
{0x24, 0x08, 2, D_WALK_ON, D_WALK_ON, D_WALK_ON, D_WALK_ON, WALK, WALK, WALK, D_WALK_ON},
{0x24, 0x02, 5, D_WALK_OFF, D_WALK_OFF, D_WALK_OFF, D_WALK_OFF, WALK, WALK, WALK, D_WALK_OFF},
{0x24, 0x00, 5, D_WALK_ON, G_WEST, G_SOUTH, G_SOUTH, WALK, WALK, WALK, G_WEST}
};
#endif
// GistID: 105a8238cde06b80a8a7
#ifndef PORT_INIT
#define PORT_INIT
#include "portinit.h"
#include "tm4c123gh6pm.h"
/*
* Initialize Port B to be used for traffic lights. Bits 0 through 5 are used to
* signal active light for GYRG|YR. This function has no input or output but
* does affect registers related to Port B including the clock.
*/
void PortB_Init(void)
{
volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x2;
delay = SYSCTL_RCGC2_R;
GPIO_PORTB_AMSEL_R = 0x000000;
GPIO_PORTB_PCTL_R = 0x000000;
GPIO_PORTB_DIR_R = 0x00003F;
GPIO_PORTB_AFSEL_R = 0x000000;
GPIO_PORTB_PUR_R = 0x000000;
GPIO_PORTB_DEN_R = 0x00003F;
}
/*
* Initialize Port E to be used for traffic/pedestrian sensors. Bits 0 through 2
* are used for E/W Sensor | N/S Sensor |Pedestrian.
*/
void PortE_Init(void)
{
volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x10;
delay = SYSCTL_RCGC2_R;
GPIO_PORTE_AMSEL_R = 0x000000;
GPIO_PORTE_PCTL_R = 0x000000;
GPIO_PORTE_DIR_R &= ~0x000007;
GPIO_PORTE_AFSEL_R = 0x000000;
GPIO_PORTE_PUR_R = 0x000000;
GPIO_PORTE_DEN_R |= 0x000007;
}
/*
* A helper function to enable Port F to allow manipulation of the on board
* LED's to signal the pedestrian lights. We use bits 3 and 1 to for WALK and DO
* NOT WALK respectively.
*/
void PortF_Init(void)
{
volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000020;
delay = SYSCTL_RCGC2_R;
GPIO_PORTF_LOCK_R = 0x4C4F434B;
GPIO_PORTF_CR_R = 0x0000001F;
GPIO_PORTF_AMSEL_R = 0x00000000;
GPIO_PORTF_PCTL_R = 0x00000000;
GPIO_PORTF_DIR_R = 0x0000000A;
GPIO_PORTF_AFSEL_R = 0x00000000;
GPIO_PORTF_PUR_R = 0x00000000;
GPIO_PORTF_DEN_R = 0x0000000A;
}
#endif
// GistID: 105a8238cde06b80a8a7
#ifndef PORT_INIT
#define PORT_INIT
void PortB_Init(void);
void PortE_Init(void);
void PortF_Init(void);
#endif
// GistID: 105a8238cde06b80a8a7
#include "systick.h"
// Enable systick timer
void SysTick_Init(void)
{
NVIC_ST_CTRL_R = 0;
NVIC_ST_CTRL_R = 0x00000005;
}
// A simple block on systick counter of given value
void SysTick_Wait(unsigned long delay){
NVIC_ST_RELOAD_R = delay-1;
NVIC_ST_CURRENT_R = 0;
while((NVIC_ST_CTRL_R&0x00010000)==0);
}
// A wrapper for blocking on 10ms increments
void SysTick_Wait10ms(unsigned long delay)
{
unsigned long i;
for (i=0; i < delay; i++) {
SysTick_Wait(SYSTICK_TEN_MS_COUNT);
}
}
// GistID: 105a8238cde06b80a8a7
#ifndef SYSTICK_FUNC
#define SYSTICK_FUNC
#include "tm4c123gh6pm.h"
#define SYSTICK_TEN_MS_COUNT 800000
// Declare Systick Functions
void SysTick_Init(void);
void SysTick_Wait10ms(unsigned long);
#endif
// GistID: 105a8238cde06b80a8a7
#include "TExaS.h"
#include "tm4c123gh6pm.h"
#include "systick.h"
#include "portinit.h"
#include "lightFSM.h"
// FUNCTION PROTOTYPES: Each subroutine defined
void DisableInterrupts(void); // Disable interrupts
void EnableInterrupts(void); // Enable interrupts
int main(void)
{
unsigned char S;
unsigned long INPUT;
TExaS_Init(SW_PIN_PE210, LED_PIN_PB543210); // activate grader and set system clock to 80 MHz
EnableInterrupts();
SysTick_Init();
PortB_Init();
PortE_Init();
PortF_Init();
// Initial State is 0
S = 0;
while(1){
// Write light state to GPIO Ports
GPIO_PORTB_DATA_R = FSM[S].TrafficOutput;
GPIO_PORTF_DATA_R = FSM[S].WalkOutput;
// Wait duration
SysTick_Wait10ms(FSM[S].Time);
// Obtain input state
INPUT = GPIO_PORTE_DATA_R&0x07;
// Move to next state given input value
S = FSM[S].Next[INPUT];
}
}
// GistID: 105a8238cde06b80a8a7
#ifndef SYSTICK_FUNC
#define SYSTICK_FUNC
#include "tm4c123gh6pm.h"
#define SYSTICK_TEN_MS_COUNT 800000
// Declare Systick Functions
void SysTick_Init(void);
void SysTick_Wait10ms(unsigned long);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment