//============================================================================
// PROJECT = RL78/F14
//============================================================================
// C O P Y R I G H T
//============================================================================
// Copyright (c) 2002 by Renesas Electronics (EUROPE) GmbH. All rights reserved.
// Kanzlerstrasse 2
// D-40472 Duesseldorf
//============================================================================
// Purpose: RSCAN Interrupt Vectors
//
// Warranty Disclaimer
/*******************************************************************************
 * DISCLAIMER
 * This software is supplied by Renesas Electronics Corporation and is only
 * intended for use with Renesas products. No other uses are authorized. This
 * software is owned by Renesas Electronics Corporation and is protected under
 * all applicable laws, including copyright laws.
 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * Renesas reserves the right, without notice, to make changes to this software
 * and to discontinue the availability of this software. By using this software,
 * you agree to the additional terms and conditions found by accessing the
 * following link:
 * http://www.renesas.com/disclaimer
 *
 * Copyright (C) 2013 Renesas Electronics Corporation. All rights reserved.
 *******************************************************************************/
/*******************************************************************************
 * File Name    : RL78_RCAN.c
 * Version      : 1.0
 * Description  : This is source file for CAN configuration.
 ******************************************************************************/
/*****************************************************************************
 * History      : DD.MM.YYYY Version Description
 *              : 03.29.2013 1.00     First Release
 ******************************************************************************/

/*===========================================================================*/
/* Includes                                                                  */
/*===========================================================================*/

#include "r_cg_macrodriver.h"
#include "r_cg_userdefine.h"
#include "r_rl78_can_sfr.h"
#include "r_rl78_can_drv.h"
#include "can_user.h"
#include "dcm.h"
#include "CanNw.h"
#include "Dcm_App.h"

#if DEBUG
#pragma interrupt RSCAN_INTCANGERR_interrupt(vect = INTCANGERR)
#pragma interrupt RSCAN_INTCANGRECC_interrupt(vect = INTCANGRFR)
#pragma interrupt RSCAN_INTCAN0ERR_interrupt(vect = INTCAN0ERR)
#pragma interrupt RSCAN_INTCAN0REC_interrupt(vect = INTCAN0CFR)
#pragma interrupt RSCAN_INTCAN0TRX_interrupt(vect = INTCAN0TRM)

#endif
unsigned char CAN_data_buf[8];
/*CAN global error*/
void __near RSCAN_INTCANGERR_interrupt(void)
{
}

/*CAN global reveive FIFO*/
void __near RSCAN_INTCANGRECC_interrupt(void)
{

    CAN_LostCount = 500;
    R_CAN_ReadRxFIFO(CAN_RXFIFO1, &RxCanMessage);
    if ((RxCanMessage.IDL == CANTP_ADDR_PHY_REQ) || (RxCanMessage.IDL == CANTP_ADDR_FUNC_REQ))
    {
        if (RxCanMessage.IDL == CANTP_ADDR_FUNC_REQ && RxCanMessage.DB[1] == 0x3E && RxCanMessage.DB[2] == 0x80)
            resetS3Server(); /*重置S3server计时*/
        else
            RxIndicationCanIf_Messages(0, RxCanMessage.IDL, RxCanMessage.DLC, RxCanMessage.DB);
        DiagMsgRcvFlag = 1;
        ucNwNetIdelCheck = 1; //网络空闲检测  300ms如果没有置位 默认为空闲
    }

    if ((RxCanMessage.IDL >= 0x0400) && (RxCanMessage.IDL <= 0x047F))
    // CAN Identifier range from 0x400 ~ 0x47F, are used for ECU network management control frames
    {
        ucRemoteWakupFlg = 1u;
        RepeatMessageReq = RxCanMessage.DB[1] & 0x01;
        ucNwNetIdelCheck = 1; //网络空闲检测  300ms如果没有置位 默认为空闲
    }

    if (RxCanMessage.IDL == 0x0403)
    {

        if (App28sTxRxStatus & 0x02 != 0) // Rx enable
        {
            Copy_Array_to_Array(CAN_403.Msg_Byte, RxCanMessage.DB, 8);
        }
    }
    if (RxCanMessage.IDL == 0x026D)
    {
        if (App28sTxRxStatus & 0x02 != 0) // Rx enable
        {
            //BackLightTimeOut_count = 500; // 500mS
            //Copy_Array_to_Array(CAN_26D.Msg_Byte, RxCanMessage.DB, 8);
        }
    }
}

/*CAN0 channel error*/
void __near RSCAN_INTCAN0ERR_interrupt(void)
{
    /*if(C0ERFLL&0x08)
    {
        BusOff_flag=1;//clear after can transmit successfully
    }	*/
    // C0ERFLL=0;
}

/*CAN0 transmit/receive FIFO receive interrutp*/
void __near RSCAN_INTCAN0REC_interrupt(void)
{
}

extern unsigned char ucBusOffModeState;
uint8_t rec_count;
/* CAN Channel transmit */
void __near RSCAN_INTCAN0TRX_interrupt(void)
{
    CFSTS0 &= 0xFFEF; /*Clear CFTXIF     FIFO transmit interrupt request flag*/
    if (ucBusOffModeState > 0)
    {
        rec_count++;
        if (rec_count > 3)
        {
            ucBusOffModeState = 0;
        }
    }
    else
    {
        rec_count = 0;
    }
}
