/**
*  @file hal_cc2530znp_target_board.h
*
*  @brief  public methods for hal_cc2530znp_target_board.c
*
* $Rev: 1 $
* $Author: Derek, Morten & Suyash
* $Date: 2010-07-13 10:23:50 -0700 (Tue, 13 Jul 2010) $
*
* YOU ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT WARRANTY 
* OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, 
* TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL TEXAS INSTRUMENTS 
* OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, 
* BREACH OF WARRANTY, OR OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
* INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, 
* LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY 
* CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*/
#ifndef hal_cc2530znp_target_board_H
#define hal_cc2530znp_target_board_H
#include "../HAL/hal_types.h"
#include "../HAL/hal_defs.h"

/*  Common Includes (will be included in all projects) */
#include "msp430x22x4.h"
#ifdef __IAR_SYSTEMS_ICC__ 	// IAR C Compiler
#include "../Common/utilities.h"
#else
#include "../Util/utilities.h"
#endif
 
#define HAL_ENTER_CRITICAL_SECTION(x) \
  st( x = __get_interrupt_state();  HAL_DISABLE_INTERRUPTS(); )
#define HAL_EXIT_CRITICAL_SECTION(x) __set_interrupt_state((x))
#define HAL_CRITICAL_STATEMENT(x) \
  st( halIntState_t s; HAL_ENTER_CRITICAL_SECTION(s); x; HAL_EXIT_CRITICAL_SECTION(s); )

#define HAL_SLEEP()     ( __bis_SR_register(LPM3_bits + GIE))
#define HAL_WAKEUP()    ( __bic_SR_register_on_exit(LPM3_bits))

#define ENABLE_SRDY_INTERRUPT()     (P2IE |= BIT6)
#define DISABLE_SRDY_INTERRUPT()    (P2IE &= ~BIT6)

#ifdef __IAR_SYSTEMS_ICC__  /* IAR C Compiler */
	#define HAL_ENABLE_INTERRUPTS()         (_EINT())
	#define HAL_DISABLE_INTERRUPTS()        (_DINT())
#else		
	#define HAL_ENABLE_INTERRUPTS() 	(__enable_interrupt())
	#define HAL_DISABLE_INTERRUPTS()	(__disable_interrupt())	
#endif 
 
/*options for halConfigureSrdyInterrrupt() */
#define SRDY_INTERRUPT_FALLING_EDGE 0x01
#define SRDY_INTERRUPT_RISING_EDGE  0x00

/* MACROS REQUIRED FOR ZNP*/
#define RADIO_ON()                  (P3OUT |= BIT7)  /*ZNP Reset Line */
#define RADIO_OFF()                 (P3OUT &= ~BIT7)

/*  ZNP SPI */
#define SPI_SS_SET()                (P3OUT &= ~(BIT0 | BIT6))  /*active low, control SS and MRDY */
#define SPI_SS_CLEAR()              (P3OUT |= (BIT0 | BIT6))  
#define SRDY_IS_HIGH()              (P2IN & BIT6)
#define SRDY_IS_LOW()               ((~P2IN) & BIT6) 
#define DEBUG_ON()                  (P4OUT |= BIT4)
#define DEBUG_OFF()                 (P4OUT &= BIT4)
#define NO_WAKEUP                   0
#define WAKEUP_AFTER_TIMER          1
#define WAKEUP_AFTER_ACCELEROMETER  2
#define WAKEUP_AFTER_SRDY           4

/*  MISC OTHER DEFINES*/
#define XTAL 8000000L
#define TICKS_PER_MS (XTAL / 1000 / 5 - 1)
#define TICKS_PER_US (TICKS_PER_MS / 1000)
#define VLO_NOMINAL 12000
#define VLO_MIN (VLO_NOMINAL - (VLO_NOMINAL/4)) /*VLO min max = +/- 25% of nominal */
#define VLO_MAX (VLO_NOMINAL + (VLO_NOMINAL/4))

#define GET_MCLK_FREQ()     8000000L    

/*Required for Light Sensor - NOT REQUIRED FOR ZNP */
#define LIGHT_SENSOR_ON()                  (P2OUT |= BIT1)  /*ZNP Reset Line*/
#define LIGHT_SENSOR_OFF()                 (P2OUT &= ~BIT1)

/*Required for Accelerometer - NOT REQUIRED FOR ZNP */
#define ACCELEROMETER_SS_SET()                (P2OUT &= ~(BIT2|BIT7))  /*active low*/
#define ACCELEROMETER_SS_CLEAR()              (P2OUT |= (BIT2|BIT7))  
#define ACCELEROMETER_RX_BUF    UCB0RXBUF

#define SW1_BV  BV(2)

/* Changing this constant requires the corresponding changes to both the HAL_TIMER_X bit flags
 * in halEventFlag_t and the HAL_TIMER_Y index values in halTimerIdx_t.
 */
#define HAL_TIMER_CNT       8

/* Flag bits for halTimerSet() arg.*/
#define HAL_TIMER_AUTO     0x01  /* Auto-reset timer period on every expiration (i.e. real-time.)*/

/* Flag bits for halTimerGet() arg.*/
#define HAL_TIMER_INTERVAL 0x01  /* The timer period (zero if not auto-resetting.)*/
#define HAL_TIMER_TICKS    0x02  /*Ticks of msecs left on the timer.*/

/* Changing the position of the 1st timer flag requires a change to the timer update logic.*/
#define HAL_EVT_NONE       0x0000
#define HAL_EVT_TIMER0     0x0001
#define HAL_EVT_TIMER1     0x0002
#define HAL_EVT_TIMER2     0x0004
#define HAL_EVT_TIMER3     0x0008
#define HAL_EVT_TIMER4     0x0010
#define HAL_EVT_TIMER5     0x0020
#define HAL_EVT_TIMER6     0x0040
#define HAL_EVT_ZNP_RESET  0x0080
#define HAL_EVT_BTN_PRESS  0x0100
#define HAL_EVT_ADC        0x0200
#define HAL_EVT_MT_RX_RDY  0x0400
#define HAL_EVT_SPB        0x0800
#define HAL_EVT_SPC        0x1000
#define HAL_EVT_SPD        0x2000
#define HAL_EVT_SPE        0x4000



#define HAL_EVT_TIMER_LED  HAL_EVT_TIMER0  /* Note the application uses three timers T0, T1, T2 for the three different event */
#define HAL_EVT_TIMER_APP  HAL_EVT_TIMER1  /* So the timer Ids for initiailizing the three type of interrupts or events for the three */
#define HAL_EVT_TIMER_BTN  HAL_EVT_TIMER2  /* timers will come from these timers getting expired. */

/*Macros for hardware functions */
#define HAL_BTN_PRESSED          ((P1IN & SW1_BV) == 0)
#define HAL_TURN_OFF_RED()       (P1OUT &= ~BV(0))
#define HAL_TURN_OFF_GRN()       (P1OUT &= ~BV(1))
#define HAL_TURN_ON_RED()        (P1OUT |= BV(0))
#define HAL_TURN_ON_GRN()        (P1OUT |= BV(1))
#define HAL_TOGGLE_RED()         (P1OUT ^= BV(0))
#define HAL_TOGGLE_GRN()         (P1OUT ^= BV(1))
 
#ifdef __IAR_SYSTEMS_ICC__  /* IAR C Compiler */
	typedef istate_t halIntState_t;
#else
	typedef unsigned char halIntState_t;	
#endif

typedef enum {
  HAL_IDX_TIMER_LED,
  HAL_IDX_TIMER_APP,
  HAL_IDX_TIMER_BTN,
  HAL_IDX_TIMER3,
  HAL_IDX_TIMER4,
  HAL_IDX_TIMER5,
  HAL_IDX_TIMER6,
  HAL_IDX_TIMER7
} halTimerIdx_t;

/* For Sensor Monitor Network demo code and Monitor and Test Interface */
#if HOST_MT
#define HAL_PORT_MT        0
uint8 halUARTRead(uint8 port, uint8 *pBuf, uint8 len);
void halUARTWrite(uint8 port, uint8 *pBuf, uint8 len);
#endif
/* Software timers w/ +/- 1 sec resolution and 65535 msec max.*/

static volatile uint16 tmrTicks[HAL_TIMER_CNT];
static uint16 tmrPeriod[HAL_TIMER_CNT];
extern uint16 TACCR0_INIT;

/* Flags set by ISR's to indicate events without blocking or invoking background functions. */
extern volatile uint16 halEventFlags;

/* Last value read from the ADC by ISR.*/
extern volatile uint16 halAdcVal;

/* Count of switch presses; updated by ISR.*/
extern volatile uint8 halSwCnt;

/*  Methods required for ZNP */
void halInit();
void delayMs(unsigned int ms);
void delayUs(unsigned int us);
void halDelay(uint8 msecs, uint8 sleep);
signed int toggleLed(unsigned char led);
signed int setLed(unsigned char led);
void clearLeds();
void halSpiInitZnp();
void spiWrite(unsigned char *bytes, unsigned char numBytes);
signed int calibrateVlo();
signed int initTimer(unsigned char seconds, unsigned char wakeOnTimer);
void halReadBusV(void);

/*  Methods not required for ZNP */
unsigned int getVcc3();
unsigned int getLightSensor();
void halReadTemp(void);
void setZnpInterfaceToInputs(void);
signed int halConfigureSrdyInterrrupt(unsigned char flags);
void halSpiInitAccelerometer();
signed int halEnableAccelerometerInterrupt(unsigned char wakeOnAccelerometer);
unsigned char spiWriteAccelerometer(unsigned char b);
static void halTimerInit(void);
void halTimerSet(uint8 tIdx, uint16 period, uint8 ctlFlags);

#endif /*Header Guard */
