/////////////////////////////////////////////////////////////////////////////
//
// Example C program using Background Telemetry Channel (BTC)
// Analog Devices 2004
//
// This program defines several BTCs to allow transfer of data over the BTC
// interface while the DSP is running.  Use the BTC Memory window in the
// debugger to view each channels data.  The defined channels are described
// below:
//
// Timer Interrupt Counter:  This channel is defined to be 1-word (4-bytes)
//		   				     in length and simply counts the number of timer
//							 interrupts that have occured.
//
// PB1 Counter: This channel is defined to be 1-word (4-bytes) in length and
//			     simply counts the number of times the PB1 pushbutton is pressed
//				 on the BF537 Ez-Kit.
//
// PB2 Counter: This channel is defined to be 1-word (4-bytes) in length and
//			     simply counts the number of times the PB2 pushbutton is pressed
//				 on the BF537 Ez-Kit.
//
// 256 word channel (PB1): This channel is defined to be 256-words (1024-bytes) in length and
//			     			simply counts the number of times the PB1 pushbutton is pressed
//				 			on the BF537 Ez-Kit.  Each word in the channel is updated with the count.
//
// 256 word channel (PB2): This channel is defined to be 256-words (1024-bytes) in length and
//			     			simply counts the number of times the PB2 pushbutton is pressed
//				 			on the BF537 Ez-Kit.  Each word in the channel is updated with the count.
//
// 4k byte channel:   This channel is defined to be 4-kbytes in length.  The first word of the
//					  channel is used to count the number of timer interrupts that have occured.
//
/////////////////////////////////////////////////////////////////////////////
#include <defbf537.h>
#include <def_lpblackfin.h>
#include <sys/exception.h>
#include "btc.h"

////////////////////////
// function prototypes
////////////////////////
void initTimer(void);
void initGPIO(void);

/////////////////////////////////
// interrupt handler prototypes
/////////////////////////////////
EX_INTERRUPT_HANDLER(timerISR);		// timer interrupt handler
EX_INTERRUPT_HANDLER(int12ISR);		// int12 interrupt handler (pushbuttons)

/////////////////////
// global variables
/////////////////////
int timerCounter;		// counts the number of timer interrupts that have fired
int pbCounter;			// counts the number of times any pushbutton on the ez-kit was pressed
int PB1Counter;			// counts the number of times the PB1 pushbutton on the ez-kit was pressed
int PB2Counter;			// counts the number of times the PB2 pushbutton on the ez-kit was pressed

int PB1Array[0x100];		// a 256-word array
int PB2Array[0x100];		// a 256-word array

char array1[0x1000];		// a 4k-byte array

////////////////////
// BTC Definitions
////////////////////
BTC_MAP_BEGIN
//             Channel Name,             Starting Address,    Length
BTC_MAP_ENTRY("Timer Interrupt Counter", (long)&timerCounter, sizeof(timerCounter))
BTC_MAP_ENTRY("PB1 Counter",            (long)&PB1Counter,  sizeof(PB1Counter))
BTC_MAP_ENTRY("PB2 Counter",            (long)&PB2Counter,  sizeof(PB2Counter))
BTC_MAP_ENTRY("256 word channel (PB1)", (long)&PB1Array, 	  sizeof(PB1Array))
BTC_MAP_ENTRY("256 word channel (PB2)", (long)&PB2Array, 	  sizeof(PB2Array))
BTC_MAP_ENTRY("4k byte channel",         (long)&array1, 	  sizeof(array1))
BTC_MAP_END

///////////////
// main
///////////////
void main()
{
	// an example of getting the starting address and length of
	// a defined channel using macros defined in btc.h
	int addr, len;
	addr = BTC_CHANNEL_ADDR(0);
	len  = BTC_CHANNEL_LEN(0);

	// install our interrupt handlers
   	register_handler(ik_ivg12, int12ISR);
   	register_handler(ik_timer, timerISR);

   	// initialize the timer and the programmable flags
   	initTimer();
   	initGPIO();

	// initialize the BTC
	btc_init();

	// this is the polling loop for servicing all BTC transactions
	while (1)
		btc_poll();

}

//////////////////////
// initTimer
//////////////////////
void initTimer()
{
	unsigned int *mmrPtr;

	mmrPtr  = (unsigned int*)TCNTL;		// timer control register
	*mmrPtr = TMPWR | TAUTORLD;

	mmrPtr  = (unsigned int*)TPERIOD;	// timer period register
	*mmrPtr = 0x00040000;

	mmrPtr  = (unsigned int*)TSCALE;		// timer scale register
	*mmrPtr = 0x00000080;

	mmrPtr  = (unsigned int*)TCOUNT;		// timer count register
	*mmrPtr = 0x00040000;

	mmrPtr  = (unsigned int*)TCNTL;		// timer control register
	*mmrPtr = TMPWR | TMREN | TAUTORLD;	// enable the timer
}


/////////////////////
// initGPIO
/////////////////////
void initGPIO()
{
	unsigned short *smmrPtr;
	unsigned int *mmrPtr;
	unsigned int nSiRev;

	smmrPtr = (unsigned short*)PORTFIO_DIR;		// PF Config register
	*smmrPtr = 0xFC0;						// PF2-PF21 as outputs, everything else as inputs

	smmrPtr = (unsigned short*)PORTFIO_INEN;
	*smmrPtr = 0x1C;

	smmrPtr = (unsigned short*)PORTFIO_SET;	// PF Set register
	*smmrPtr = 0xFC0;						// turn all LEDs on

	smmrPtr = (unsigned short*)PORTFIO_CLEAR;	// PF Clear register
	*smmrPtr = 0xFC0;						// turn all LEDs off

	smmrPtr = (unsigned short*)PORTFIO_MASKA_SET;	// PF Interrupt A Mask Set register
	*smmrPtr = 0x1C;						// unmask PF2-PF21

	smmrPtr = (unsigned short*)PORTFIO_POLAR;	// PF Polarity register
	*smmrPtr = 0x0000;						// all PFs are active high

	smmrPtr = (unsigned short*)PORTFIO_EDGE;	// PF Interrupt Sensitivity register
	*smmrPtr = 0x1C;						// make PF2-PF21 edge triggered ints

	mmrPtr = (unsigned int*)SIC_IMASK;		// System Interrupt Control Mask register
	*mmrPtr = *mmrPtr | 0x08000000;			// enable peripheral #17 (PF Int A), set the bit

}



////////////////////////////
// timer interrupt handler
////////////////////////////
EX_INTERRUPT_HANDLER(timerISR)
{
	unsigned short *smmrPtr;
	int i;

	++timerCounter;							// count the timer interrupts
	for (i = 0; i < 4; ++i)
	{
		// update the first location of array1 with the timerCounter
		array1[i] = ((char*)&timerCounter)[i];
	}

	// toggle the timer LED
	smmrPtr = (unsigned short*)PORTFIO_TOGGLE;
	*smmrPtr = (*smmrPtr & 0x40) | ( (timerCounter & 0x00000001) << 6);	// turn on the timer LED

}

////////////////////////
// interrupt12 handler
////////////////////////
EX_INTERRUPT_HANDLER(int12ISR)
{
	unsigned short *smmrPtr;
	unsigned long *memPtr;
	unsigned long length;
	unsigned short temp;
	bool bFlag = false;
	unsigned long i;
	unsigned char ucShift = 0;

	// figure out which push button was pressed and increment the appropriate
	// counter...if PF2-PF21 did not generate the interrupt then just return
	smmrPtr = (unsigned short*)PORTFIO_SET;	// PF Set register
	temp = *smmrPtr;						// get which flags are currently set

	// check if PB1 is set
	if (temp & 0x4)
	{
		++PB1Counter;
		PB1Array[0] = PB1Counter;
		bFlag = true;
		ucShift = 0xB;
	}
	// check if PB2 is set
	if (temp & 0x8)
	{
		++PB2Counter;
		PB2Array[0] = PB2Counter;
		bFlag = true;
		ucShift = 0xA;
	}

	// if any of PB1 or PB2 were set then update the LEDs
	if (bFlag)
	{
		smmrPtr = (unsigned short*)PORTFIO_CLEAR;	// PF Clear register
		*smmrPtr = temp;							// clear the input pins (PF2-PF5)

		++pbCounter;								// inc the global pushbutton counter

		// toggle the PB LED
		smmrPtr = (unsigned short*)PORTFIO_TOGGLE;
		*smmrPtr = (*smmrPtr & 0xC00) | ((pbCounter & 0x00000001) << ucShift);

	}
}


