//****************************************************************************																																																																																																																									   
// @Project       CANReal.dav
// origin: 		  CAN_Handler.c
//****************************************************************************
#include "SHARED_INT.H"


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


//****************************************************************************
// @Function      void SHINT_viXINTR6Isr(void) 
//
// 	ISR - for message object 0 - receive - ID5  - SRN1
//****************************************************************************

unsigned char ucCANRxd;

void SHINT_viXINTR6Isr(void) interrupt XINTR6INT
{ 

  _push_(SYSCON0);               				// push the current RMAP
  RESET_RMAP();                  				// resets RMAP

  CAN_pushAMRegs();              				// Push the CAN Access Mediator Registers
 
  SFR_PAGE(_su0, SST0);          				// switch to page 0
  IRCON1 &= ~(ubyte)0x20; 						// Clear CANSRC1
  SFR_PAGE(_su0, RST0);          				// restore the old SCU page

  CAN_vWriteCANAddress(CAN_MSPND0); 			// Addressing CAN_MSPND0
  CAN_DATA0 = ~(0x01); 						// clear message pending register
  CAN_vWriteEN(D0_VALID);  						

  CAN_vWriteCANAddress(CAN_MOCTR0); 			// Addressing CAN_MOCTR0
  CAN_DATA0 = 0x09; 							// Reset RxPND and NEWDAT flag for object 0
  CAN_vWriteEN(D0_VALID);
  CAN_vWriteCANAddress(CAN_MODATAL0);	 		// access MO_0
  CAN_vReadEN();								// and read
  ucCANRxd = CAN_DATA0;
		

   
  CAN_popAMRegs();               				// restore the CAN Access Mediator Registers
 
  _pop_(SYSCON0);                				// restore the old RMAP 
} 


//****************************************************************************
// @Function      void SHINT_viXINTR9Isr(void) 
//
// 	ISR - for message object 1 - transmit - ID7   - SRN3
//****************************************************************************


void SHINT_viXINTR9Isr(void) interrupt XINTR9INT
{

  _push_(SYSCON0);               				// push the current RMAP
  RESET_RMAP();                  				// resets RMAP		   	
  CAN_pushAMRegs();              				// Push the CAN Access Mediator Registers


  SFR_PAGE(_su0, SST0);          				// switch to page 0
  IRCON2 &= ~(ubyte)0x10; 						// Clear CANSRC3
  SFR_PAGE(_su0, RST0);          				// restore the old SCU page

  CAN_vWriteCANAddress(CAN_MSPND0); 			// Addressing CAN_MSPND0
  CAN_DATA0 = ~(0x02); 							// clear message pending register
  CAN_vWriteEN(D0_VALID);  						
	
  CAN_vWriteCANAddress(CAN_MOCTR1); 			// Addressing CAN_MOCTR1
  CAN_DATA0 = 0x02; 							// Reset TxPND flag for object 1
  CAN_vWriteEN(D0_VALID);
  CAN_popAMRegs();               				// restore the CAN Access Mediator Registers
  _pop_(SYSCON0);                				// restore the old RMAP
} 

//****************************************************************************
// @Function      void SHINT_viXINTR10Isr(void) 
//
// 	ISR - for error code on node 1 - SRN4
//****************************************************************************
void SHINT_viXINTR10Isr(void) interrupt XINTR10INT
{

ubyte ubNSRL;
ubyte ubNSRH;
ubyte ubResetLEC = 0x3F;

  // USER CODE BEGIN (NodeI0,2)

  // USER CODE END

  _push_(SYSCON0);               // push the current RMAP
  RESET_RMAP();                  // resets RMAP

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

  SFR_PAGE(_su3, SST0);          // switch to page 3

  //   MultiCAN Node 4 (SRN4) interrupt handling section...

  //   Check Interrupt Request Register 3 Interrupt Flag 4
  if (((IRCON3 & 0x02) != 0))
  {

    IRCON3 &= ~(ubyte)0x02; //   clear CANSRC4

    //   ---------------------------------------------------------------------
    //   Interrupts of CAN node 1
    //   ---------------------------------------------------------------------

    CAN_vWriteCANAddress(CAN_NSR1);
    CAN_vReadEN();
    ubNSRL = CAN_DATA0;
    ubNSRH = CAN_DATA1;

    // USER CODE BEGIN (SRN4_NODE1,1)

    // USER CODE END
    //   ---------------------------------------------------------------------
    //   CAN node 1 Alert Interrupt
    //   ---------------------------------------------------------------------
    if (ubNSRL & 0x20)      // if ALERT
    {

          ///  Note: The ALERT bit is set upon the occurrence of one ofthe 
          ///  following events. User need to handle these in user defined 
          ///  section.

          // BOFF : ubNSRL & 0x80
                // Bus OFF
          // EWRN : ubNSRL & 0x40
                // One of the error counters REC or TEC reached the
                // warning limit of 96.
          // LLE : ubNSRH & 0x01
                // A list length error has been detected during message
                // acceptance filtering. The number of elements in the list
                // that belongs to this CAN node differs from the list SIZE 
                // given in the list termination pointer
          // LOE : ubNSRH & 0x02
                // A list object error has been detected during message
                // acceptance filtering. A message object with wrong LIST 
                // index entry in the Message Object Control Register has 
                // been detected

      // USER CODE BEGIN (SRN4_NODE_1_ALIE,2)

      // USER CODE END
    }

    //   ---------------------------------------------------------------------
    //   CAN node 1 Last Error Code
    //   ---------------------------------------------------------------------
    if (ubNSRL & 0x07)  // if LEC
    {
          ///  Note: a CAN protocol error with a last error code, details of 
          ///  bit field are mentioned below. User need to handle these in 
          ///  user defined section.

          // Stuff Error: (ubNSRL & 0x07) == 1
            ///  More than 5 equal bits in a sequence have occurredin a part 
            ///  of a received message where this is not allowed.

          // Form Error: (ubNSRL & 0x07) == 2
            ///  A fixed format part of a received frame has the wrong format.

          // Ack Error: (ubNSRL & 0x07) == 3
            ///  The message this CAN controller transmitted wasnot 
            ///  acknowledged by another node.

          // Bit1 Error: (ubNSRL & 0x07) == 4
            ///  During the transmission of a message (with the exeption of 
            ///  the arbitration field), the device wanted to send a 
            ///  recessive level ("1"), but the monitored bus value was 
            ///  dominant.

          // Bit0 Error: (ubNSRL & 0x07) == 5
            ///  During the transmission of a message (or acknow- ledge bit, 
            ///  active error flag, or overload flag), the device wanted to 
            ///  send a dominant level ("0"), but the monitored bus value was 
            ///  recessive.

            ///  During busoff recovery this status is set each time  a 
            ///  sequence of 11 recessive bits has been monitored. The CPU 
            ///  may use this code as indication that the  bus is not 
            ///  continuously disturbed).

          // CRC Error: (ubNSRL & 0x07) == 6
            ///  The CRC check sum was incorrect in the message received.

      ubResetLEC = 0x38;

      // USER CODE BEGIN (SRN4_NODE1_LEC,7)

      // USER CODE END
    }

    // USER CODE BEGIN (SRN4_NODE1,8)

    // USER CODE END

    //// Reset LEC, TXOK, RXOK, ALERT, EWRN, BOFF, LLE, LOE (if set)

    CAN_vWriteCANAddress(CAN_NSR1); // Addressing CAN_NSR1
    CAN_DATA0   =  ~(ubNSRL & ubResetLEC);       // load CAN_NSR1 status register[7-0]
    CAN_DATA1   =  ~(ubNSRH);       // load CAN_NSR1 status register[15-8]
    CAN_vWriteEN(D0_VALID+D1_VALID);  // Data0 and Data1 are Valid for
                                 // transmission and Write is Enabled.

    // USER CODE BEGIN (SRN4 ,4)

    // USER CODE END

  }

  // USER CODE BEGIN (NodeI0,5)

  // USER CODE END

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

  CAN_popAMRegs();               // restore the CAN Access Mediator Registers

  _pop_(SYSCON0);                // restore the old RMAP

} //  End of function SHINT_viXINTR10Isr
