You are on page 1of 3

/****************************************************************************

Module
ReloadIRCaptureModule.c

SET UP FOR WIDE TIMER 1 PORT 1


THIS CORRESPONDS to PC7.
Revision
1.0.1

Description
Listens for IR events
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
#include "ReloadIRCaptureModule.h"
// Hardware
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_pwm.h"
#include "inc/hw_nvic.h"
#include "inc/hw_timer.h"

// Event & Services Framework


#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"
#include "PWM16Tiva.h"
#include "MasterSM.h"

/*----------------------------- Module Defines ----------------------------*/


#define TOLERANCE (10)
#define TICKS_PER_US (40)
#define REQUIRED_CONSEC_PULSES (5)
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this service.They should be functions
relevant to the behavior of this service
*/

/*------------------------------ Module Code ------------------------------*/


/****************************************************************************/

static uint32_t NumConsecutivePulses = 0;


static uint32_t lastCount = 0;
static uint32_t mostRecentPerioduS = 0;
static uint32_t newCount;
static uint32_t CurrentPeriod_US = 0;

//THIS IS THE ISR FOR THE TIMER


void ReloadIRISR(void)
{
//clear interrupt source
HWREG(WTIMER1_BASE + TIMER_O_ICR) = TIMER_ICR_CBECINT;
//grab event time from value register
newCount = HWREG(WTIMER1_BASE + TIMER_O_TBR);
//calculate ticks between events
mostRecentPerioduS = (newCount - lastCount) / TICKS_PER_US;
//printf("%d\r\n", mostRecentPerioduS);
//if period is within tolerance of period (in microseconds) then increment how many
good pulses we have.
if ((mostRecentPerioduS + TOLERANCE > CurrentPeriod_US) && (mostRecentPerioduS -
TOLERANCE < CurrentPeriod_US))
{
NumConsecutivePulses++;
}
//otherwise reset number of consecutive good pulses.
else
{
CurrentPeriod_US = mostRecentPerioduS;
NumConsecutivePulses = 0;
}
// if number of consecutive good pulses gets high enough, tell the main service
that we've found the beacon
if (NumConsecutivePulses == REQUIRED_CONSEC_PULSES)
{
NumConsecutivePulses = 0;
ES_Event_t FoundIR;
FoundIR.EventType = FOUND_RELOAD_IR;
FoundIR.EventParam = CurrentPeriod_US;
PostMasterSM(FoundIR);
//TODO: MAKE SURE WE"RE POSTING TO CORRECT STATEMACHINE
}
//store new tick count as old value.
lastCount = newCount;
}

void InitReloadIRCapture(void)
{
// enable wide timer 1, timer B
HWREG(SYSCTL_RCGCWTIMER) |= SYSCTL_RCGCWTIMER_R1;
// this will output/listen to pin pc4 (the first available pin)
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R2;
// disable timer
HWREG(WTIMER1_BASE + TIMER_O_CTL) &= ~TIMER_CTL_TBEN;
// make sure it's in 32bit long mode, this concatenates two 16bit timers.
HWREG(WTIMER1_BASE + TIMER_O_CFG) = TIMER_CFG_16_BIT;
//make roll-over of timer equal to the roll-over or 32bit int for simplicity/time
subtraction
HWREG(WTIMER1_BASE + TIMER_O_TBILR) = 0xffffffff;
// Set wide timer A to capture mode (TAMR=3, TAAMS = 0),
// store the edge time in the value register (TACMR = 1) and make it count up(TACDIR
= 1)
HWREG(WTIMER1_BASE + TIMER_O_TBMR) = (HWREG(WTIMER1_BASE + TIMER_O_TBMR) &
~TIMER_TBMR_TBAMS) | (TIMER_TBMR_TBCDIR | TIMER_TBMR_TBCMR | TIMER_TBMR_TBMR_CAP);
// To listen to rising edges, set TAEVENT bits in CTL register to clear
HWREG(WTIMER1_BASE + TIMER_O_CTL) &= ~TIMER_CTL_TBEVENT_M;

//tell PC7 that it will have an alternative function


HWREG(GPIO_PORTC_BASE + GPIO_O_AFSEL) |= BIT7HI;
//make bit 4's alternate function WT0CCP0. This requires writing 7 to it.
// so wrtie 7 shifted 16 times as it must be shifted 4 times * 4bits
HWREG(GPIO_PORTC_BASE + GPIO_O_PCTL) = (HWREG(GPIO_PORTC_BASE + GPIO_O_PCTL) &
0x0fffffff) + (7 << 28);
//enable pin c to be digital I/O
HWREG(GPIO_PORTC_BASE + GPIO_O_DEN) |= BIT7HI;
//MAke digital input
HWREG(GPIO_PORTC_BASE + GPIO_O_DIR) &= BIT7LO;
//turn local inteerrupt capture on
//HWREG(WTIMER1_BASE + TIMER_O_IMR) |= TIMER_IMR_CBEIM;
// enable interrupt in NVIC, it's bit 1 as it's event 97
HWREG(NVIC_EN3) |= BIT1HI;
// enable interrupts globally
__enable_irq();
// now enable timer and make sure ticks goes slowly when debugging.
HWREG(WTIMER1_BASE + TIMER_O_CTL) |= (TIMER_CTL_TBEN | TIMER_CTL_TBSTALL);
printf("Initializign ReloadIR input capture\n\r");
}

void DisableReloadIR()
{
CurrentPeriod_US = 0;
HWREG(WTIMER1_BASE + TIMER_O_IMR) &= (~TIMER_IMR_CBEIM);
}

void EnableReloadIR()
{
HWREG(WTIMER1_BASE + TIMER_O_IMR) |= TIMER_IMR_CBEIM;
}

You might also like