/*
 *  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.   
 */ 
/***************************************************************************/ 
/*                                                                         */ 
/*     EDMA_hwi . cpp                                                      */ 
/*                                                                         */ 
/*     EDMA hardware interrupt function.                                   */ 
/*                                                                         */ 
/*                                                                         */ 
/***************************************************************************/ 
  #if 0
  /*	com_tra[0] = McBSP_MMARKER ;
		com_tra[1] = (ad_buffer[0] <<16);  <- Vt
		com_tra[2] = (ad_buffer[1] <<16);  <- delta_Pe
		com_tra[3] = (ad_buffer[2] <<16);  <- delta_omega
		com_tra[4] = (ad_buffer[3] <<16); 
		com_tra[5] = (ad_buffer[4] <<16);
		com_tra[6] = (ad_buffer[5] <<16);  
		com_tra[7] = (ad_buffer[6] <<16);
		com_tra[8] = (ad_buffer[7] <<16);
		com_tra[9] =	if A/D test "1"  <- imax;
		com_tra[10] =	if A/D test "1"  <- imin;  
		com_tra[11] =	if A/D test "1"  <- imax-imin;
		com_tra[12] =	if A/D test "1"  <- iaver;
		com_tra[13] = ;  <-- 
		com_tra[14] = ;  <-- omega 
		com_tra[15] = ;   <-Rec0 +1  */


  /*	da_buffer[0] =  com_rec[1];		//da_buffer[0] <- Main DSP
		da_buffer[1] =  com_rec[2]; 	//da_buffer[1] <- Main DSP  
		da_buffer[2] =  com_rec[3]; 	//da_buffer[2] <- Main DSP
		da_buffer[3] =  com_rec[4]; 	//da_buffer[3] <- Main DSP   
		com_rec[5]  -> Upss calculated by the DSP2     */

 #endif
/*-----------------------------------------------------------------------*/ 
#define _in_EDMA_HWI_CPP_ 
/*-----------------------------------------------------------------------*/ 
//  DSP 1 <=> TMS320C6711
#include "mmaccfg.h"
//#include <std.h>

#include "global_var.h"
#include "A2D2A.h"
#include "AVR_Exciter.h"
#include "dsk_lib.h"


extern "C" {
Void hwiDMA_isr(Void) ;}


/************************************************************/
/*hwiDMA_isr():	 >>>> HWI INT 8  <<<   						*/
/* 	Hardware Interrupt Function disables EDMA channels and 	*/
/*	Timer0, Then post software interrupt.					*/
/************************************************************/
Void hwiDMA_isr(Void) 
{  
 #define LED1_PERIOD 32*12
 static unsigned int iperiod;
  int itemp1,itemp2,i,j;
  float ftemp;
  
  //----> 
  LED_on(3); // execution time measurement
            
 //---------------------------------------------------------         
 //------------------- A/D ------ test  --------------------
 //--------------------------------------------------------- 
 #define A_D_TEST 1
 
  #if A_D_TEST
  			static int imax,imin,isum,iaver;
  			static int icount=0;
  #endif
 //------------------- A/D ------ test  END    -------------
 //--------------------------------------------------------- 

			

  //-----------------------------------------------------------------
  //  A/D data processing 
  //-----------------------------------------------------------------
  
  
     // The A/D sample time is 20.0 us or 50kHz (all 8-th channel)
     //The 50KHz is dounsampled by 16-in time to obtain less noise 
     // on the samples.
     //Processing the input data: 
    // a1,a2,...,a8,a1,a2,..a8,...a8 <- AD_FAME_NO * AD_BLOCK_SZ
  	  for(i=0;i<AD_BLOCK_SZ;i++) 
  	  {
  			//itemp1 = ( ad_buffer[(AD_FAME_NO*AD_BLOCK_SZ)+i] << 16 );
  	  		itemp2 = 0;
  	  		for(j=0;j<=AD_FAME_NO;j++)
  	  		{
  	  			itemp1 = (ad_buffer[(i+(j*AD_BLOCK_SZ))] <<16);
  	  			itemp1 = ( itemp1 >> 16 );
  	  			itemp2 += itemp1;
  	  		}
  	  
  		itemp2 = ( itemp2 >> 4 );
  	//	com_tra[i+1] = itemp2;   // <-- For A/D data in counts
  		ftemp = (float) (itemp1*COUNT2VOLT);
  		tra->x[i] = ftemp;
  	  }
       
       
       
    //----- == Measred signals == ------------------------------------  
       
      If = 0.0;     // Measured field current
  	  It = 0.0;     // Measured terminal current 
  	  Vt = (tra->x[0]);     // Measured terminal volatge

       
       
   //-----------------------------------------------------------------    
   //-----------------------------------------------------------------    
   // D/A data processing
   //-----------------------------------------------------------------  
    
  //------------------------------------------------------------------ 
  //  AVR_Exciter implementation 
  //------------------------------------------------------------------
   
   ftemp = rec->x[4];  //  PSS from the DSP-II 
    // The first channel D/A as AVR + PSS control signal 
    ftemp = AVR_Exciter(ftemp);  // AVR and PSS calculation 
    //ftemp = D2A_V_MAX * ftemp; /* Convert the pu signal to voltage */
    
    	if      (ftemp > D2A_V_MAX) itemp1 = POS_5V_D2A;
 		else if (ftemp < D2A_V_MIN) itemp1 = NEG_5V_D2A; 
 		else
 			{
 				ftemp = (VOLT2COUNT * ftemp);
 				itemp1 = _spint(ftemp);
 				//itemp1 = (int) ftemp;
 				itemp1 = itemp1 + ZERO_V_D2A;
 			}
 		da_buffer[0] = itemp1;		// the final control signal for the micro generator
  //------------------------------------------------------------------
 
 
  //------------------------------------------------------------------
  // DSP2 signal observations
  //------------------------------------------------------------------	
  //Select wich D/A originates from DSP2 : D2A_DATA_FROM_DSP2 =
  //                                           if 0 -> all 4 (1,2,3,4)
  //                                           if 1 -> 2,3,4 
  //										   if 2 -> 3,4    ...
  #define D2A_DATA_FROM_DSP2   1  /* D/A CH2,CH3,CH4 from DSP2 */ 
   
 	for(i=D2A_DATA_FROM_DSP2;i<DA_BLOCK_SZ;i++) 
 	{
 	
 	 	ftemp = rec->x[i];		
 		if      (ftemp > D2A_V_MAX) itemp1 = POS_5V_D2A;
 		else if (ftemp < D2A_V_MIN) itemp1 = NEG_5V_D2A; 
 		else
 			{
 				ftemp = (VOLT2COUNT * ftemp);
 				itemp1 = _spint(ftemp);
 				//itemp1 = (int) ftemp;
 				itemp1 = itemp1 + ZERO_V_D2A;
 			}
 		da_buffer[i] = itemp1;		
 	}   
  //------------------------------------------------------------------   
 
        
                   
 //---------------------------------------------------------         
 //------------------- A/D ------ test  --------------------
 //--------------------------------------------------------- 
  #if A_D_TEST
  
    // A/D test by statistic:       
       j=0; // j = 0,1,..,16  <= AD_FAME_NO
       i=0; // i = 0,1,..,7  A/D channel 
       #define  NN 1000
        
	  itemp1 = (ad_buffer[(i+(j*AD_BLOCK_SZ))] <<16);
      itemp1 = (itemp1 >> 16);	
      isum += itemp1;
      if(imax < itemp1) imax = itemp1; 
      if(imin > itemp1) imin = itemp1;
      icount++;
           
      
      if(icount > NN)
      {
      	iaver = isum/NN;
      
      	com_tra[9]  =  (imax >> 2);
      	com_tra[10] =  (imin >> 2);
      	com_tra[11] =  ((imax-imin) >> 2);
      	com_tra[12] =  (iaver >> 2);
     
      	icount = 0;
      	imin = itemp1;
      	imax = itemp1;
      	isum = 0;       
      }
   #endif   
 //-----------------END of  A/D test  ----------------------
 //---------------------------------------------------------         
 //---------------------------------------------------------   
  
  
  // Swich the LED1 in visenle period:
    iperiod++;
   if (iperiod > LED1_PERIOD) 
   {
   LED_toggle(1);
   iperiod = 0;
   }
   
   //----> 
   LED_off(3); // execution time measurement

	EDMA_intClear(TCCINTNUM);
}


