#include "x_int13.h"

#include <281xEvTimersInit.h>
#include <project.h>

#include "DSP281x_Examples.h"   // DSP281x Examples Include File
#include "DSP281x_SWPrioritizedIsrLevels.h"   // DSP281x Examples Include File
#include "DSP281x_Device.h"
#include "MemoryFunctions.h"
#include "Spartan2E_Adr.h"
#include "TuneUpPlane.h"


//Pointers to handler functions
void (*int13_handler)() = NULL;


//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

unsigned int  enable_profile_led1_pwm = 1;
unsigned int  enable_profile_led2_pwm = 1;



int InitXilinxSpartan2E(void (*int_handler)())
{
  int err;

  project.controller.status = component_NotReady;

  err =	load_xilinx_new(0x130000, SIZE_XILINX200);
  if (err)
     return err;

  err = test_xilinx_live();


#ifdef ENABLE_XINTC_INT13
  if (int_handler)
     XIntcInterruptSetup(int_handler);
  else
     err = 1;
#endif

  if (err == 0)
    project.controller.status = component_Ready;

  return err;
}

interrupt void XIntc_INT13_Handler(void)
{

    IER &= MINT13;                 // Set "global" priority


#if (_ENABLE_INTERRUPT_PROFILE_LED1)
    if (enable_profile_led1_pwm)
    i_led1_on_off_special(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
    if (enable_profile_led2_pwm)
    i_led2_on_off_special(1);
#endif

    EINT;

    // Insert ISR Code here.......


//	i_led2_on_off(1);
//  IER &= 0xEFFF;

	if (project.controller.write.setup.bit.use_int13 == 1)
	{
	  
//      EnableInterrupts();
      //������������� ����������, � ������� ���������� �������, ��-�� ������� ������� ���������� ���
//      stop_eva_timer1();

      if(int13_handler)
      {
          int13_handler();
      }
      //��������� �������
////      start_eva_timer1();
//	  DINT;
//
//	  IFR &= 0xefff; 			// ������� ���� ����� ���������� ��� ������!
//      IER |= M_INT13;
	}
	else
	{
//	  IFR &= 0xefff; 			// ������� ���� ����� ���������� ��� ������!
//      IER |= M_INT13;

	}


//	EnableInterrupts();
//	c = IFR; // & 0x0100
//	if (c)
//	{
//	  count_lost_interrupt++;
//	  IFR &= 0xfeff; // ������� ���� ����� ���������� ��� ������!
//	}
//    EnableInterrupts();
//	i_led2_on_off(0);

//	IFR &= 0xfeff; 			// ������� ���� ����� ���������� ��� ������!
//	EINT;


#if (_ENABLE_INTERRUPT_PROFILE_LED1)
    if (enable_profile_led1_pwm)
    i_led1_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
    if (enable_profile_led2_pwm)
    i_led2_on_off_special(0);
#endif


} 

int XIntcInterruptSetup(void (*int_handler)())
{
	int result = 0;

     EALLOW;

     GpioMuxRegs.GPEMUX.bit.XNMI_XINT13_GPIOE2=1;
//   GpioMuxRegs.GPEDIR.bit.GPIOE2 = 0;
//   GpioDataRegs.GPESET.bit.GPIOE2 = 1;

     PieVectTable.XINT13=&XIntc_INT13_Handler;
     int13_handler = int_handler;
//   PieCtrlRegs.PIECRTL.bit.
     XIntruptRegs.XNMICR.bit.POLARITY=0;
     XIntruptRegs.XNMICR.bit.SELECT=1;
     XIntruptRegs.XNMICR.bit.ENABLE=0;
    
//  Enable interrupt 13
 //   IER |= M_INT13;

	project.controller.read.status.bit.int13_inited = 1;

//    EDIS;
//	EnableInterrupts();

    /*
     * Start the interrupt controller in simulation mode.
     */
  //  result = XIntc_Start(Ptr, intc_mode_is_Sim); // sim mode
//	if (!(result == status_Success))
		return result;

//	return status_Success;
} 

void start_int13_interrupt(void)
{
    //  Enable interrupt 13
        IER |= M_INT13;
}

void stop_int13_interrupt(void)
{
    //  Disable interrupt 13
//    IER &= ~(M_INT13);
    IER &= MINT13;                 // Set "global" priority
}