//****************************************************************************
// @Module        Shared Interrupt Routines
// @Filename      SHARED_INT.C
// @Project       transceiver1.dav
//----------------------------------------------------------------------------
// @Controller    Infineon XC888CLM-8FF
//
// @Compiler      Keil
//
// @Codegenerator 1.1
//
// @Description   This file contains the Shared interrupt routines.
//
//----------------------------------------------------------------------------
// @Date          26.04.2007 15:38:18
//
//****************************************************************************

// USER CODE BEGIN (SHARED_INT_General,1)

// USER CODE END



//****************************************************************************
// @Project Includes
//****************************************************************************

#include "SHARED_INT.H"

// USER CODE BEGIN (SHARED_INT_General,2)

// USER CODE END


//****************************************************************************
// @Macros
//****************************************************************************

// USER CODE BEGIN (SHARED_INT_General,3)

// USER CODE END


//****************************************************************************
// @Defines
//****************************************************************************

// USER CODE BEGIN (SHARED_INT_General,4)

// USER CODE END


//****************************************************************************
// @Typedefs
//****************************************************************************

// USER CODE BEGIN (SHARED_INT_General,5)

// USER CODE END


//****************************************************************************
// @Imported Global Variables
//****************************************************************************

// USER CODE BEGIN (SHARED_INT_General,6)

// USER CODE END


//****************************************************************************
// @Global Variables
//****************************************************************************

// USER CODE BEGIN (SHARED_INT_General,7)

// USER CODE END


//****************************************************************************
// @External Prototypes
//****************************************************************************


// USER CODE BEGIN (SHARED_INT_General,8)

// USER CODE END


//****************************************************************************
// @Prototypes Of Local Functions
//****************************************************************************

// USER CODE BEGIN (SHARED_INT_General,9)

// USER CODE END


//****************************************************************************
// @Function      void SHINT_vInit(void) 
//
//----------------------------------------------------------------------------
// @Description   This function initializes the shared interrupts. 
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    None
//
//----------------------------------------------------------------------------
// @Date          26.04.2007
//
//****************************************************************************

// USER CODE BEGIN (SHINT_Init,1)

// USER CODE END

void SHINT_vInit(void)
{
  // USER CODE BEGIN (SHINT_Init,2)

  // USER CODE END

  ///  -----------------------------------------------------------------------
  ///  Configuration of the Shared  Interrupts:
  ///  -----------------------------------------------------------------------
  ///  - MultiCAN Node 0 Interrupt is Selected


  IEN0         |=  0x20;         // load interrupt enable register 0

  ///  - MultiCAN Node 1 Interrupt is Selected


  IEN1         |=  0x01;         // load interrupt enable register 1


  // USER CODE BEGIN (SHINT_Init,3)

  // USER CODE END


} //  End of function SHINT_vInit


//****************************************************************************
// @Function      void SHINT_viXINTR5Isr(void) 
//
//----------------------------------------------------------------------------
// @Description   This is the service routine for the shared interrupt node 
//                XINTR5. XINTR5 ISR Node is shared by Timer 2, UART 
//                Fractional Divider(BRG Interrupt),MultiCAN Node 0 and LIN. 
//                Depending on the selected module interrupt it is called.
//                T2 - Depending on the selected operating mode it is called 
//                when TF2 is set by an overflow or underflow of the timer 2 
//                register or when EXF2 is set by a negative transition on 
//                T2EX.
//                UART - It is called after the BRG timer overflows and sets 
//                the NDOV bit.
//                CAN -  It is called for the  Service Request Node 0 of the 
//                MultiCAN module.
//                Please note that you have to add application specific code 
//                to this function.
//
//----------------------------------------------------------------------------
// @Returnvalue   none
//
//----------------------------------------------------------------------------
// @Parameters    None
//
//----------------------------------------------------------------------------
// @Date          26.04.2007
//
//****************************************************************************

//   You have two choices for interrupt type select in Project Settings Page 
//   under Global Settings Section.
//   If you select CHOICE 1 then ISR will be generated with push and pop.
//   If you select CHOICE 2 then ISR will be generated without push and pop.
//   Default choice is CHOICE 2.
//   Current selection is CHOICE 2 

// USER CODE BEGIN (SHINT_XINTR5Isr,1)

// USER CODE END

void SHINT_viXINTR5Isr(void) interrupt XINTR5INT
{

ubyte ubTempMsgID = 0;
ubyte ubTempVarObjHandler = 0;
un_32bit ulBit_Pos_Mask;

  // USER CODE BEGIN (SHINT_XINTR5Isr,2)

  // USER CODE END

  CAN_pushAMRegs();              // push the CAN Access Mediator Registers

  SFR_PAGE(_su0, SST0);          // switch to page 0

  //   MultiCAN Node 0 (SRN0) interrupt handling section...


  //   Check Interrupt Request Register 2 Interrupt Flag 0
  if (((IRCON2 & 0x01) != 0))
  {

    IRCON2 &= ~(ubyte)0x01; //   clear CANSRC0

    //   ---------------------------------------------------------------------
    //   Interrupts of CAN Msg Obj x 
    //   ---------------------------------------------------------------------

    CAN_vWriteCANAddress(CAN_MSIMASK);    // set message index mask register
    CAN_DATA0   =   0x01;        // set message index mask register
    CAN_DATA1   =   0x00;        // set message index mask register
    CAN_DATA2   =   0x00;        // set message index mask register
    CAN_DATA3   =   0x00;        // set message index mask register

    CAN_vWriteEN(ALL_DATA_VALID); // Write mode Enabled

    // USER CODE BEGIN (SRN0,1)

    // USER CODE END
    CAN_vWriteCANAddress(CAN_MSID0);   // message index register
    CAN_vReadEN();               // Read Mode is enabled

    ubTempMsgID = CAN_DATA0;

    if(ubTempMsgID != 0x20)
    {
      do
      {
      CAN_vWriteCANAddress(CAN_MOCTR(ubTempMsgID)); // Addressing CAN_MOCTRn
      CAN_vReadEN();               // Read Mode is enabled
      ubTempVarObjHandler = CAN_DATA0;

      ulBit_Pos_Mask.ulVal = ((unsigned long) 1 << ubTempMsgID);
      CAN_vWriteCANAddress(CAN_MSPND0); // Addressing CAN_MSPND0

      CAN_DATA0 = ~ulBit_Pos_Mask.ubDB[3]; // clear message pending register
      CAN_DATA1 = ~ulBit_Pos_Mask.ubDB[2]; // clear message pending register
      CAN_DATA2 = ~ulBit_Pos_Mask.ubDB[1]; // clear message pending register
      CAN_DATA3 = ~ulBit_Pos_Mask.ubDB[0]; // clear message pending register
      CAN_vWriteEN(ALL_DATA_VALID);  // Write mode Enabled

          ///  Note: Below commented code could be copied by the user to user 
          ///  defined section for Interrupt handling.

          ///  Handling of MO TXPND for transmit data/remote frames (Generic 
          ///  Code).
          ///  (ubTempVarObjHandle & MOSTAT_TXPND)

          // if( ubTempVarObjHandler & MOSTAT_TXPND)   // if TXPND is set
          // {
          //// The transmission of the last message object was successful.
          //// reset TXPND, NEWDAT
          //    CAN_vWriteCANAddress(CAN_MOCTR(ubTempMsgID)); // Addressing CAN_MOCTRn
          //
          //    CAN_DATA0 = (MOSTAT_TXPND + MOSTAT_NEWDAT); // load CAN Data Register 0
          //    CAN_DATA1 = 0x00; // load CAN Data Register 1
          //    CAN_DATA2 = 0x00; // load CAN Data Register 2
          //    CAN_DATA3 = 0x00; // load CAN Data Register 3
          //    CAN_vWriteEN(ALL_DATA_VALID); // Writemode is Enabled
          //
          // }  // End of TXPNDn

      // USER CODE BEGIN (SRN0_OBJ,2)

      // USER CODE END

      CAN_vWriteCANAddress(CAN_MSIMASK);    // set message index mask register
      CAN_DATA0  =   0x01;       // set message index mask register
      CAN_DATA1  =   0x00;       // set message index mask register
      CAN_DATA2  =   0x00;       // set message index mask register
      CAN_DATA3  =   0x00;       // set message index mask register

      CAN_vWriteEN(ALL_DATA_VALID); // Write mode Enabled

      // USER CODE BEGIN (SRN0,5)

      // USER CODE END

      CAN_vWriteCANAddress(CAN_MSID0);   // message index register
      CAN_vReadEN();               // Read Mode is enabled
      ubTempMsgID = CAN_DATA0;

      }while (ubTempMsgID != 0x20); // end while

    }  // end if

    // USER CODE BEGIN (SHINT_XINTR5Isr,6)

    // USER CODE END

  }


  // USER CODE BEGIN (SHINT_XINTR5Isr,7)

  // USER CODE END

  SFR_PAGE(_su0, RST0);          // restore the old SCU page

  CAN_popAMRegs();               // restore the CAN Access Mediator Registers
} //  End of function SHINT_viXINTR5Isr


//****************************************************************************
// @Function      void SHINT_viXINTR6Isr(void) 
//
//----------------------------------------------------------------------------
// @Description   This is the service routine for the shared interrupt node 
//                XINTR6. XINTR6 ISR Node is shared by ADC[1:0] and MultiCAN 
//                Nodes 1 & 2. Depending on the selected module interrupt it 
//                is called. 
//                ADC -  It is activated by different interrupt sources 
//                (Channel Interrupts or Event Interrupts).The user obtains 
//                the conversion result by calling the function 
//                ADC_uwGetResultDataX(0,1,2,3).
//                CAN - It is called for the  Service Request Node 1 and 2 of 
//                the MultiCAN module.
//                Please note that you have to add application specific code 
//                to this function.
//
//----------------------------------------------------------------------------
// @Returnvalue   none
//
//----------------------------------------------------------------------------
// @Parameters    None
//
//----------------------------------------------------------------------------
// @Date          26.04.2007
//
//****************************************************************************

//   You have two choices for interrupt type select in Project Settings Page 
//   under Global Settings Section.
//   If you select CHOICE 1 then ISR will be generated with push and pop.
//   If you select CHOICE 2 then ISR will be generated without push and pop.
//   Default choice is CHOICE 2.
//   Current selection is CHOICE 2 

// USER CODE BEGIN (SHINT_XINTR6Isr,1)

// USER CODE END

void SHINT_viXINTR6Isr(void) interrupt XINTR6INT
{


ubyte ubTempMsgID = 0;
ubyte ubTempVarObjHandler = 0;
un_32bit ulBit_Pos_Mask;

  // USER CODE BEGIN (SHINT_XINTR6Isr,2)

  // USER CODE END

  CAN_pushAMRegs();              // push the CAN Access Mediator Registers

  SFR_PAGE(_su0, SST0);          // switch to page 0

  //   MultiCAN Node 1 (SRN1) interrupt handling section...

  //   Check Interrupt Request Register 1 Interrupt Flag 1
  if (((IRCON1 & 0x20) != 0))
  {

    IRCON1 &= ~(ubyte)0x20; //   clear CANSRC1

    //   ---------------------------------------------------------------------
    //   Interrupts of CAN Msg Obj x 
    //   ---------------------------------------------------------------------

    CAN_vWriteCANAddress(CAN_MSIMASK);    // set message index mask register
    CAN_DATA0   =   0x00;        // set message index mask register
    CAN_DATA1   =   0x00;        // set message index mask register
    CAN_DATA2   =   0x01;        // set message index mask register
    CAN_DATA3   =   0x00;        // set message index mask register

    CAN_vWriteEN(ALL_DATA_VALID); // Write mode Enabled

    // USER CODE BEGIN (SRN1,1)

    // USER CODE END
    CAN_vWriteCANAddress(CAN_MSID0);   // message index register
    CAN_vReadEN();               // Read Mode is enabled

    ubTempMsgID = CAN_DATA0;

    if(ubTempMsgID != 0x20)
    {
      do
      {
      CAN_vWriteCANAddress(CAN_MOCTR(ubTempMsgID)); // Addressing CAN_MOCTRn
      CAN_vReadEN();               // Read Mode is enabled
      ubTempVarObjHandler = CAN_DATA0;

      ulBit_Pos_Mask.ulVal = ((unsigned long) 1 << ubTempMsgID);
      CAN_vWriteCANAddress(CAN_MSPND0); // Addressing CAN_MSPND0

      CAN_DATA0 = ~ulBit_Pos_Mask.ubDB[3]; // clear message pending register
      CAN_DATA1 = ~ulBit_Pos_Mask.ubDB[2]; // clear message pending register
      CAN_DATA2 = ~ulBit_Pos_Mask.ubDB[1]; // clear message pending register
      CAN_DATA3 = ~ulBit_Pos_Mask.ubDB[0]; // clear message pending register
      CAN_vWriteEN(ALL_DATA_VALID);  // Write mode Enabled

          ///  Note: Below commented code could be copied by the user to user 
          ///  defined section for Interrupt handling.

          ///  Handling of MO RXPND for receive data/remote frames (Generic 
          ///  Code).
          ///  (ubTempVarObjHandle & MOSTAT_RXPND)

          // if( ubTempVarObjHandler & MOSTAT_RXPND)   // if RXPND is set
          // {
          //
          //   if( ubTempVarObjHandler & MOSTAT_NEWDAT)   // if NEWDAT is set
          //   {
          //
          //     if( ubTempVarObjHandler & MOSTAT_MSGLST)  // if MSGLST is set
          //     {
          //// Indicates that the CAN controller has stored a new message into this object,
          //// while NEWDAT was still set, ie. the previously stored message is lost.
          //
          //      }
          //     else
          //     {
          //// The CAN controller has stored a new message into this object.
          //
          //      }
          //
          //    }
          //
          //// Reset RXPND, NEWDAT, MSGLST (if set)
          //
          //  CAN_vWriteCANAddress(CAN_MOCTR(ubTempMsgID)); // Addressing CAN_MOCTRn
          //
          //  CAN_DATA0 = (ubTempVarObjHandler & MOSTAT_RST_MNR); // load CAN Data Register 0
          //  CAN_DATA1 = 0x00; // load CAN Data Register 1
          //  CAN_DATA2 = 0x00; // load CAN Data Register 2
          //  CAN_DATA3 = 0x00; // load CAN Data Register 3
          //  CAN_vWriteEN(ALL_DATA_VALID);   // Writemode is Enabled
          // }  // End of RXPNDn

      // USER CODE BEGIN (SRN1_OBJ,1)

      // USER CODE END

      CAN_vWriteCANAddress(CAN_MSIMASK);    // set message index mask register
      CAN_DATA0  =   0x00;       // set message index mask register
      CAN_DATA1  =   0x00;       // set message index mask register
      CAN_DATA2  =   0x01;       // set message index mask register
      CAN_DATA3  =   0x00;       // set message index mask register

      CAN_vWriteEN(ALL_DATA_VALID); // Write mode Enabled

      // USER CODE BEGIN (SRN1,5)

      // USER CODE END

      CAN_vWriteCANAddress(CAN_MSID0);   // message index register
      CAN_vReadEN();               // Read Mode is enabled
      ubTempMsgID = CAN_DATA0;

      }while (ubTempMsgID != 0x20); // end while

    }  // end if

    // USER CODE BEGIN (SHINT_XINTR6Isr,5)

    // USER CODE END

  }


  // USER CODE BEGIN (SHINT_XINTR6Isr,7)

  // USER CODE END

  SFR_PAGE(_su0, RST0);          // restore the old SCU page

  CAN_popAMRegs();               // restore the CAN Access Mediator Registers
} //  End of function SHINT_viXINTR6Isr


// USER CODE BEGIN (SHARED_INT_General,10)

// USER CODE END


