/* you can remove the copyright

 */

/* USER CODE END Header */
#include "Mcal.h"

//#define CAN_ID_TYPE_FD_FRAME_MASK 		(0x40000000U)
//#define CAN_ID_TYPE_EXTENDED_FRAME_MASK (0x80000000U)

// 变量
uint8 FeedBackData[8] = {0x00};

uint8 sduData[8] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

Can_PduType TempPduInfo = {
    .swPduHandle = 6U,
	.id = 0x123U,
    .length = 8U,
    .sdu = sduData,
};

Can_PduType FeedBackPduInfo = {
    .swPduHandle = 6U,
	.id = 0x456U,
    .length = 8U,
    .sdu = FeedBackData,
};

uint32 tempSent, handleAcc;

// 延时1ms函数
void Delay_1ms(uint32 Ms)
{
    while (Ms > 0)
    {
        --Ms;
        for (uint16 Cnt = 0; Cnt < 25000; ++Cnt)
        {
            __NOP();
            __NOP();
            __NOP();
            __NOP();
            __NOP();
            __NOP();
            __NOP();
            __NOP();
            __NOP();
            __NOP();
        }
    }
}

// 发送完成回调函数，返回的是发送报文的swPduHandle
void CanTransmitCallOut(PduIdType TxPduId)
{
    handleAcc = TxPduId;
    tempSent ++;
}

// 接收完成回调函数，CanId即为帧id号，据此解析报文
uint32 test_count[2]={0};
boolean CanReceiveCallOut(uint8 Hrh, Can_IdType CanId, uint8 CanDataLength, const uint8* CanSduPtr)
{
//    FeedBackPduInfo.sdu[0] = ((uint8*)(&CanId))[3];
//    FeedBackPduInfo.sdu[1] = ((uint8*)(&CanId))[2];
//    FeedBackPduInfo.sdu[2] = ((uint8*)(&CanId))[1];
//    FeedBackPduInfo.sdu[3] = ((uint8*)(&CanId))[0];
    test_count[1]++;
    return TRUE;
}
/**
 * @brief  The application entry point.
 * @retval int
 */


int main(void)
{
    // 时钟初始化
    Mcu_Init(&Mcu_Config);
    Mcu_InitClock(0);
#if (MCU_NO_PLL == STD_OFF)
    while ( MCU_PLL_LOCKED != Mcu_GetPllStatus());
    Mcu_DistributePllClock();
#endif
    
    // IO口初始化
    Port_Init(&Port_Config);
    
    // CAN初始化，
    Can_Init(&Can_Config);
    Can_SetControllerMode(CanConf_CanController_CAN0, CAN_CS_STARTED);
    
    // 中断初始化
    Platform_Init(NULL_PTR);

    // 1s间隔发送报文
    uint32 Cnt = 0U;
    while (1)
    {
        if (Cnt % 1000 == 0)
        {
            Can_Write(CanConf_CanHardwareObject_CAN0_TX0, &TempPduInfo);
            
            test_count[0]++; 
        }
        Delay_1ms(1);
        Cnt++;
    }
}

/* USER CODE BEGIN 4 */

