/*
Copyright 2003/2004, Freie Universitaet Berlin. All rights reserved.

These sources were developed at the Freie Universität Berlin, Computer
Systems and Telematics group.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
 
- Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

This software is provided by FUB and the contributors on an "as is"
basis, without any representations or warranties of any kind, express
or implied including, but not limited to, representations or
warranties of non-infringement, merchantability or fitness for a
particular purpose. In no event shall FUB or contributors be liable
for any direct, indirect, incidental, special, exemplary, or
consequential damages (including, but not limited to, procurement of
substitute goods or services; loss of use, data, or profits; or
business interruption) however caused and on any theory of liability,
whether in contract, strict liability, or tort (including negligence
or otherwise) arising in any way out of the use of this software, even
if advised of the possibility of such damage.

This implementation was developed by the CST group at the FUB.
Contributors: Marco Hutta

For documentation and questions please use the web site
http://scatterweb.mi.fu-berlin.de and the mailinglist
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
Berlin, 2003/2004/2005
*/

#ifndef SactterWeb_System
#define SactterWeb_System

// Must be either DCO_2458KHZ DCO_4506KHZ
#define DCO_4506KHZ
//#define DCO_2458KHZ
// Maximum size for normal terminal replies.
#define TERM_BUFSIZE        (82)
// Maximum size of a line received by uart, rest will be cut.
#define SERIAL_BUFSIZE      (82)
// Maximum size of packets that can be received, header included is 8 bytes.
#define RADIO_RXBUF_SIZE    (270)
// Size of the TX ring buffer.
#define RADIO_TXBUF_SIZE    (400)
// Maximum number of timers, each timer requires 6 bytes of RAM (==sizeof(::timerEntry)).
#define MAX_TIMERS          (20)
// Maximum number of tasks, each entry is 10 bytes.
#define MAX_TASKS           (10)
// Callback types, use these with ::registerCallback().
// Gets called everytime a whole line is received via serial port. 
#define C_SERIAL              1
// Gets called everytime an unknown packet (not handled by the firmware) is received. 
#define C_RADIO               2
// Gets called for sensor values.
#define C_SENSOR              3
// Is called periodically
#define C_PERIODIC            4
// The ::callbacks[] Array will get this size.
#define NUMBER_CALLBACKS	    4
// special section definitions for linking
#define INFOMEM            __attribute__((section(".infomem")))
#define INFOMEMNOBITS      __attribute__((section(".infomemnobits")))
// standard bit manipulation macros.
#define   SET(x, y)     ((x) |= (y))
#define   CLEAR(x, y)   ((x) &= (~y))
/// module flags for low power management
// set if scos_heartbeat should be called
#define MF_SCOS       (0x0001)
// set if radio_rx_heartbeat should be called
#define MF_RADIO_RX   (0x0002)
// set if radio_tx_heartbeat should be called
#define MF_RADIO_TX   (0x0004)
// set if timer_heartbeat should be called
#define MF_TIMER      (0x0008)
// set if a serial line is complete
#define MF_SERIAL_RX  (0x0010)
// set if a sensor is generated data
#define MF_SENSORS    (0x0020)
// set if a RC5 code is received via IR Receiver
#define MF_RC5        (0x0080)
// wake up (lpm1)
#define WAKEUPLPM1(irqname, bit)  do {     \
    __asm__ __volatile__ ("bic %0, .L__FrameOffset_" #irqname "(r1)": : "i" ((uint16_t)LPM1_bits)); \
    runModule |= bit; \
    }while(0)

// Bool
enum {
  True = 1,
  False = 0,
};
typedef char bool;
// typedef char -> UINT8
typedef unsigned char     UINT8;
// typedef int -> UINT16
typedef unsigned int      UINT16;
// typedef long -> UINT32
typedef unsigned long     UINT32;
// Temperature type
typedef union { unsigned int u; signed int s; } temp_t; 
// For functions like: void function(void).
typedef void(*fp_t)();
// For functions like: form: void function(UINT8*)
typedef void(*fp_string_t)(UINT8*);
// For functions like: void function(UINT8)
typedef void(*fp_char_t)(UINT8);
// For functions like: void function(UINT16)
typedef void(*fp_int_t)(UINT16);
// For functions like: void function(void*).
typedef void(*fp_vp)(void*);
// Table where callbacks are hold, defined in ScatterWeb.System.c
extern fp_vp callbacks[];
// present possible MF flags
extern volatile UINT16 runModule;

#if defined(ESB430) || defined(EYE430)
#define MICIFG    (0x01)
#define MICIE     (0x02)
#define MICVOLTAGE        (ADC12MEM2)
#define EXTERNVOLTAGE     (ADC12MEM3)
#define BATTERYVOLTAGE    (ADC12MEM4)
#define RXPVOLTAGE        (ADC12MEM5)
extern volatile UINT8 MIC; ///< 0x01 is like IFG, 0x02 like IE and will set sensorFlags.MIC
#endif

#if defined(ECR430)
#define BATTERYVOLTAGE    (ADC12MEM4)
#endif

#define STARTUP_POR       (0) ///< Power on or reset button.
#define STARTUP_SWRESET   (1) ///< call via reset().
#define STARTUP_REPROGRAM (2) ///< reprogramming
#define STARTUP_PANIC     (3) ///< call via wd_panic(). All states <=STARTUP_PANIC are considered OK!
#define STARTUP_WATCHDOG  (4) ///< Reason for reset was watchdog expiry.
#define STARTUP_KEYV      (5) ///< Flash control register key violation.
#define STARTUP_UNKNOWN   (6) ///< Unknown type of reset.
#define WDS_RUNNING       (0xAB01) ///< System at startup.
#define WDS_SWRESET       (0xAB02) ///< Software sets this shortly before writing bad value into FCTL3 to recognize a wanted reset.
#define WDS_REPROGRAM     (0xAB03)
#define WDS_PANIC         (0xAB04)
// Last updated RXPower AD value.
extern volatile UINT16 rxpTemp;
// RXPower AD value for last received packet, cleared by ::rxClear().
extern volatile UINT16 rxpValue;

/*
 * Function to register callbacks
 * 
 * @param type    type has to be one of {::C_PERIODIC, ::C_SERIAL, ::C_RADIO, ::C_SENSORS}.
 * @param fp_t    eventHandler
 * 
 * @return bool   True always    
 *
 */  
bool System_registerCallback( UINT8 type, void(*fp_t)() ); 

/*
 * Function to register lib
 * 
 * @param position  position
 * @param fp_t      eventHandler
 * 
 * @return bool   True always    
 *
 */ 
bool System_registerLib( UINT8 position, void(*fp_t)() );

/*
 * Function to wait
 * 
 * @param ticks   ticks to wait  
 * 
 * @return bool   True always    
 *
 */  
bool System_wait(UINT32 ticks);

/*
 * Function to wait which is more precise than wait because it uses TimerA to produce its interval, also blocking.
 * 
 * @param ticks   ticks to wait  
 * 
 * @return bool   True always    
 *
 */  
bool System_waitA(UINT16 ticks);


/*
 * Function to check the reset
 * 
 * @return bool   True always    
 *
 */  
bool System_reset();         

/*
 * Function to init the system
 * 
 * @return bool   True always    
 *
 */  
bool System_init();

/*
 * Function to check the reset
 * 
 * @return UINT8*   pointer of reset message
 *
 */  
const UINT8* System_resetReason();

/*
 * Function to set the panic flags
 * 
 * @param *fmt    panic flag to write
 *  
 * @return bool   True always    
 *
 */  
bool System_panic(const char *fmt, ...);

/*
 * Function to check the panic flags
 * 
 * @param data      value of panic
 *  
 * @return UINT8*   pointer of panic message
 *
 */  
UINT8* System_panicReason(UINT16 data);

/*
 * Function to clear the panic flags
 * 
 * @return bool   True always    
 *
 */  
bool System_panicClear();

/*
 * Function to get temperature
 *  
 * @return temp_t  current temperature
 *  
 */
temp_t System_getTemperature();

/*
 * Function to start conversion
 *  
 * @return bool   True always
 *  
 */
bool System_clStartConversion();

/*
 * Function to set DCO
 *  
 * @return bool   True always
 *  
 */
bool System_setDCO();

/*
 * Function to start watchdog
 *  
 * @return bool   True always
 *  
 */
bool System_startWatchdog();

/*
 * Function to stop watchdog
 *  
 * @return bool   True always
 *  
 */
bool System_stopWatchdog();

// Include all headers here so other files only need to include this file.
#include <msp430x14x.h>
#include <io.h>
#include <signal.h>
#include <stdlib.h>
#include "ScatterWeb.Configuration.h"
#include "ScatterWeb.Data.h"
#include "ScatterWeb.Timers.h"
#include "ScatterWeb.Time.h"
#include "ScatterWeb.IO.h"
#include "ScatterWeb.Comm.h"
#include "ScatterWeb.Net.h"
#include "ScatterWeb.Messaging.h"
#include "ScatterWeb.Threading.h"
#include "ScatterWeb.String.h"

#endif
