/* 
 *  Copyright 2004 by Soos, Antal 
 *  All rights reserved. Property of Soos, Antal. 
 *  Restricted rights to use, duplicate or disclose this code are 
 *  granted through contract.   
 */ 
/***************************************************************************/ 
/*                                                                         */ 
/*     hlobal_var . hpp													   */ 
/*                                                                         */ 
/*																		   */ 
/***************************************************************************/ 
//  DSP 1 <=> TMS320C6711

#ifndef _GLOBAL_VAR_H_
#define _GLOBAL_VAR_H_

#include "mmaccfg.h"
#include <csl.h>

// Macro definitions:
//------------------------------------------------------------------------------------- 
#define  LIMIT(min, x, max)             ( ((x) < (min)) ? (min) :       \
                                         (((x) > (max)) ? (max) : x ) )


#define AD_BLOCK_SZ 8     /* size of data buffer */
#define AD_FAME_NO  (16-1)  /* Numer of frames-1 to process = filter */ 
#define DA_BLOCK_SZ 4     /* size of data buffer */

// A2D Constants:
#define POZ_10V_A2D 0x7FFF
#define ZERO_0V_A2D 0x0000
#define NEG_10V_A2D 0x8000
#define A2D_V_MAX  10.0 /* Voltage */
#define A2D_V_MIN  -10.0 /* Voltage */
#define A2D_MASK    0x0000FFFF
#define COUNT2VOLT  3.0501754e-4  /* 1/3278.5 <-> count for 1.0V */

#define SAMPLE_TIME ( 0x000002EE * (1e-6/150) *4 *(AD_FAME_NO+1))
#define Ts SAMPLE_TIME  /* Sample time period (3.2 ms) */
//const float  Ts = SAMPLE_TIME; /* Sample time period (3.2 ms) */

// D2A Costants:
#define POS_5V_D2A 0xFFFF
#define ZERO_V_D2A 0x8000
#define NEG_5V_D2A 0x0000
#define D2A_V_MAX  5.0 /* Voltage */
#define D2A_V_MIN -5.0 /* Voltage */
#define VOLT2COUNT 6553.4

// DSP  to DSP communication:
#define SCT_BLOCK_SZ 16     /* size of com transmit buffer */ 
#define SCR_BLOCK_SZ 16     /* size of com receive buffer */
#define McBSP_MARKER 0x5ABCDEF5


  // xflag definitions

#define XF_ExVoReStChP  0x00000001     /*Exciter voltage reference step change */
#define XF_ExVoReStChPX 0xFFFFFFFE     /*Exciter voltage reference step change */

#define XF_ExVoReStChN  0x00000002     /*Exciter voltage reference step change */
#define XF_ExVoReStChNX 0xFFFFFFFD     /*Exciter voltage reference step change */


//*********************************************************************

	typedef struct DSP2DSP_com {
		Uint32 TR_McBSP_MARKER;
		float x[SCR_BLOCK_SZ-2];
		Uint32 RE_McBSP_MARKER;
		} D2D_com;


#endif   /* _GLOBAL_VAR_H_ */

//*******************************************************************/ 
#ifdef _in_MAIN_CPP_ 
//**************************************************************/ 
#undef _in_MAIN_CPP_ 

/* Create the buffers. We want to align the buffers to be cache friendly */
/* by aligning them on an L2 cache line boundary.  for A/D and D/A       */

// A/D converter:
#pragma DATA_ALIGN((AD_BLOCK_SZ*(AD_FAME_NO+1)));
unsigned far int ad_buffer[(AD_BLOCK_SZ*(AD_FAME_NO+1))];    /* raw data from AD, written to by EDMA  */ 

//volatile far float        adFbuffer[AD_BLOCK_SZ];    /* float and weighted buffer of AD   */  


// D/A Converter:
#pragma  DATA_ALIGN(DA_BLOCK_SZ);
unsigned far int da_buffer[DA_BLOCK_SZ]; /* raw data from AD, written to by EDMA  */  

//volatile far float       daFbuffer[DA_BLOCK_SZ];    /* float and weighted  buffer of AD   */ 


volatile far unsigned int LED_State;   /* For the LED flashing */
unsigned far int TimerEventId0;  /* For the timer control */

// The MPC algorithm paramethers:
int selectionflag;


// Serial communication buffer:
#pragma  DATA_ALIGN(SCR_BLOCK_SZ);
Uint32 com_rec[SCR_BLOCK_SZ]; /*raw data for EDMA serial communication*/  

#pragma  DATA_ALIGN(SCT_BLOCK_SZ);
Uint32 com_tra[SCT_BLOCK_SZ]; /*raw data for EDMA serial communication*/  

 D2D_com *rec;
 D2D_com *tra;

  volatile float  If;     // Measured field current
  volatile float  It;     // Measured terminal current 
  volatile float  Vt;     // Measured terminal volatge

  volatile unsigned int xflag;     // Flags for AVR states,...
  
/************************************************************************/        
#else  /* Defnitions external to MAIN_CPP  folow                       */
/************************************************************************/  
    
// A/D converter:
extern far unsigned int ad_buffer[(AD_BLOCK_SZ*(AD_FAME_NO+1))];    /* raw data from AD, written to by EDMA  */    
//extern far volatile float  adFbuffer[AD_BLOCK_SZ];    /* float and weighted buffer of AD   */    

// D/A Converter:
extern far unsigned int da_buffer[DA_BLOCK_SZ];    /* raw data from AD, written to by EDMA  */    
//extern far volatile float   daFbuffer[DA_BLOCK_SZ];    /* float and weighted buffer of AD   */  
  

extern far volatile unsigned int  LED_State;   /* For the LED flashing */
extern far unsigned int TimerEventId0;  /* For the timer control */

// Serial communication references:
extern Uint32 com_tra[SCT_BLOCK_SZ]; /*raw data for EDMA serial communication*/  
extern Uint32 com_rec[SCR_BLOCK_SZ]; /*raw data for EDMA serial communication*/  
  extern D2D_com *rec;
  extern D2D_com *tra;

  extern  volatile float  If;     // Measured field current
  extern  volatile float  It;     // Measured terminal current 
  extern  volatile float  Vt;     // Measured terminal volatge
  
  extern  volatile unsigned int xflag;     // Flags for AVR states,...
/************************************************************************/  
#endif  /* Commom definitions follows : */
/************************************************************************/ 
