// Copyright (c) 2006, 2008 Rowley Associates Limited.
//
// This file may be distributed under the terms of the License Agreement
// provided with this software.
//
// THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// CTL template file to implement the timer tick using TIMERA, led using P1 and button using P2
//

#include <msp430.h>
#include <ctl_api.h>
        
static void (*timer_isr)(void);
static void (*button_pressed_isr)(void);

void ctl_board_init(void)
{
  P1DIR |= 1;  
}

#define SMCLK_FREQUENCY 1000000

#if defined(__MSP430_HAS_T0A5__) || defined(__MSP430_HAS_T0A3__)
#define TACTL TA0CTL
#define CCTL0 TA0CCTL0
#define CCR0  TA0CCR0
#define TIMERA0_VECTOR TIMER0_A0_VECTOR
#endif

void ctl_start_timer(CTL_ISR_FN_t isr)
{
  ctl_time_increment = 10; // increment ctl timer by 10 to get a 1 millisecond counter
  timer_isr = isr;
  TACTL = TASSEL1+TACLR;
  CCTL0 = CCIE;
  CCR0 = SMCLK_FREQUENCY/100; // interupt every 10 milliseconds
  TACTL |= MC0;
}

unsigned long ctl_get_ticks_per_second(void)
{
  return 1000;
}

void TimerA0_IRQ(void) __ctl_interrupt[TIMERA0_VECTOR]
{
  timer_isr(); 
}

void ctl_board_on_button_pressed(CTL_ISR_FN_t isr)
{
  button_pressed_isr = isr;
  P2IE |= 0xff; // P2 Interrupt enabled
  P2IES |= 0xff; // P2 hi/low edge
  P2IFG |= 0xff; // P2 IFG Cleared
}

void Port2_IRQ(void) __ctl_interrupt[PORT2_VECTOR]
{   
  P2IFG = 0;
  button_pressed_isr();  
}

void ctl_board_set_leds(unsigned v)
{
  if (v)
    P1OUT = 1;
  else
    P1OUT = 0;
}

                             
                                          

