#include "r_cg_macrodriver.h"
#include "r_cg_userdefine.h"
#include "r_rl78_can_sfr.h"
#include "r_rl78_can_drv.h"
#include "appTask.h"
#include "can_user.h"
#include "CanNw.h"
#include "PINdef.h"
#include "extern.h"
#include "hwCtrl.h"


#define C_100MS_1MS 100

unsigned char App28sTxRxStatus = 3;
unsigned char Nw28sTxRxStatus = 3;


can_frame_t TxCanMessage;
can_frame_t TxCanMessage1;
can_frame_t RxCanMessage;

unsigned char CAN_SEND_COMPLETE;
uint16_t air_req_timer = C_100MS_1MS;

unsigned char BusOff_flag = 0;
unsigned char BusOff_IGN_cycle_count = 0;
unsigned int BusOff_Detect_delay_count = 1000;


unsigned char Can_2F_SET_run = 0;
unsigned char DiagnosticSession_Present = 1; //
unsigned char Tx_Rx_Enable = 3;              // 0:none 1:Tx 2:Rx 3:Tx Rx


Can_Msg_Type_403 CAN_403;
Can_Msg_Type_26D CAN_26D;
Can_Msg_Type_212 CAN_212;
extern uint16_t g_adval[12];

extern uint16_t ZYMotorLocation;
extern uint16_t KBMotorLocation;

void Copy_Array_to_Array(unsigned char *dest, unsigned char *src, unsigned char count)
{
    unsigned char i = 0;

    while (i < count)
    {
        i++;
        *dest++ = *src++;
    }
}

void Can_Msg_Change_Bcm(void)
{
    Copy_Array_to_Array(TxCanMessage.DB, CAN_212.Msg_Byte, 8);
}

void CAN_TX_MESSAGE_INIT(void)
{
    CAN_STB = 0;
    TxCanMessage.IDE = 0;     /* IDE  0:Standard 1:Extend           */
    TxCanMessage.RTR = 0;     /* RTR  0:Data 1:Remote               */
    TxCanMessage.THDSE = 0;   /* Transmit History Data Store Enable */
    TxCanMessage.IDL = 0x212; /* ID Data (low)                      */
    TxCanMessage.IDH = 0;     /* ID Data (high)                     */
    TxCanMessage.DLC = 8;     /* DLC Data                           */
    TxCanMessage.LBL = 0;     /* Label Data                         */
    TxCanMessage.TS = 0;      /* Timestamp Data    */
    Can_Msg_Change_Bcm();
}

//extern uint8_t flagOverCurrentKB,flagOverCurrentZY;
uint8_t txbuf1[8],txbuf2[8],txbuf3[8];
void TxTestMsg(void)
{
    uint8_t testdata[8] = {0};
    if ((App28sTxRxStatus & DCM28S_MASK_TX_DISABLE) == 0)
    {
        return;
    }
    TransmitCAN_Message(0x333,8,testdata);
}
//立即发送
void ClearTxCounter(void)
{
    Can_Msg_Change();
    air_req_timer = 0;
}

void TxServe(void)
{
    Can_RtnType re_flag;
    static unsigned char sucNwSendCnt = 0;

    ////////////////////////////////////////////////////////////////////////
    if ((ucNwSendPduType == SEND_NWAPP_PDU) || (ucNwSendPduType == SEND_APP_PDU))
    {
        if (ucNwSendPduType == SEND_NWAPP_PDU)
        {
            if (uiNwPduCycCnt == 0)
            {
                uiNwPduCycCnt = uiSetNwPduCycVal;
                if ((Nw28sTxRxStatus & DCM28S_MASK_TX_DISABLE) != 0)
                {
                    //R_CAN_TrmByTRFIFO0_CH0(&TxCanNwPdu_436);
                    if (ImmediateNM_count < 5)
                        ImmediateNM_count++;
                }
            }
            sucNwSendCnt = 0;
        }
        else
        {
            if (sucNwSendCnt > 0)
            {
                if (uiNwPduCycCnt == 0)
                {
                    sucNwSendCnt--;
                    uiNwPduCycCnt = uiSetNwPduCycVal;
                    if ((Nw28sTxRxStatus & DCM28S_MASK_TX_DISABLE) != 0)
                    {
                        R_CAN_TrmByTRFIFO0_CH0(&TxCanNwPdu_436);
                        if (ImmediateNM_count < 5)
                            ImmediateNM_count++;
                    }
                }
            }
        }
        
    }
    else
    {
        ; /*sotp can transmit*/
    }
    //CAN_STB = 0;
    if ((air_req_timer == 0) && ((App28sTxRxStatus & DCM28S_MASK_TX_DISABLE) != 0))// 
    {
        BusOff_flag = 0;
        Can_Msg_Change_Bcm();
        re_flag = R_CAN_TrmByTRFIFO0_CH0(&TxCanMessage);
        
        //TxTestMsg();
        air_req_timer = C_100MS_1MS;
    }
}

void Can_init_id(can_frame_t *TxMessage, unsigned int canid)
{
    TxMessage->IDE = 0;
    TxMessage->RTR = 0;
    TxMessage->THDSE = 0;
    TxMessage->IDL = canid;
    TxMessage->IDH = 0x0000;
    TxMessage->DLC = 8;
    TxMessage->LBL = 0;
    TxMessage->TS = 0;
}