/*********************************************************************//**
 * @brief		c_entry: Main CAN program body
 * @param[in]	none
 * @return 		int
 **********************************************************************/
int c_entry(void) { /* Main Program */
	PINSEL_CFG_Type PinCfg;

	/* Initialize debug via UART0
	 * – 115200bps
	 * – 8 data bit
	 * – No parity
	 * – 1 stop bit
	 * – No flow control
	 */
	debug_frmwrk_init();
	print_menu();

	/* Pin configuration
	 * CAN1: select P0.0 as RD1. P0.1 as TD1
	 * CAN2: select P2.7 as RD2, P2.8 as RD2
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Pinnum = 0;
	PinCfg.Portnum = 0;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 1;
	PINSEL_ConfigPin(&PinCfg);

	PinCfg.Pinnum = 7;
	PinCfg.Portnum = 2;
	PINSEL_ConfigPin(&PinCfg);
	PinCfg.Pinnum = 8;
	PINSEL_ConfigPin(&PinCfg);

	//Initialize CAN1 & CAN2
	CAN_Init(LPC_CAN1, 125000);
	CAN_Init(LPC_CAN2, 125000);

	//Enable Interrupt
	CAN_IRQCmd(LPC_CAN2, CANINT_RIE, ENABLE);

	//Enable CAN Interrupt
	NVIC_EnableIRQ(CAN_IRQn);

	_DBG_("CAN test Bypass Mode function...");
	_DBG_("Press '1' to initialize CAN message...");_DBG_("");
	while(_DG !='1');
	CAN_SetAFMode(LPC_CANAF,CAN_AccBP);
	CAN_InitMessage();
	PrintMessage(&TXMsg);
	_DBG_("Message ID and data will be increased continuously...");

	_DBG_("Press '2' to start CAN operation...");
	while(_DG !='2');

	/** To test Bypass Mode: we send infinite messages to CAN2 and check
	 * receive process via COM1
	 */
	CAN_SendMsg(LPC_CAN1, &TXMsg);

	while (1);
}
/*********************************************************************//**
 * @brief       c_entry: Main CAN program body
 * @param[in]   none
 * @return      none
 **********************************************************************/
void c_entry(void)
{
    /* Initialize debug via UART0
     * – 115200bps
     * – 8 data bit
     * – No parity
     * – 1 stop bit
     * – No flow control
     */
    debug_frmwrk_init();
    print_menu();

    /* Initialize CAN1 peripheral
     * Note: Self-test mode doesn't require pin selection
     */
    CAN_Init(_USING_CAN_NO, 125000);

    //Enable self-test mode
    CAN_ModeConfig(_USING_CAN_NO, CAN_SELFTEST_MODE, ENABLE);

    //Enable Interrupt
    CAN_IRQCmd(_USING_CAN_NO, CANINT_RIE, ENABLE);
    CAN_IRQCmd(_USING_CAN_NO, CANINT_TIE1, ENABLE);

    //Enable CAN Interrupt
    NVIC_EnableIRQ(CAN_IRQn);

    CAN_SetAFMode(CAN_ACC_BP);

    CAN_InitMessage();

    _DBG_("Transmitted buffer:");

    PrintMessage(&TXMsg);

    /** To test Bypass Mode: we send infinite messages to CAN2 and check
     * receive process via COM1
     */
    CAN_SendMsg(_USING_CAN_NO, &TXMsg);

#if (_USING_CAN_NO == CAN_1)
    LPC_CAN1->CMR |=(1<<4); //Self Reception Request
#else
    LPC_CAN2->CMR |=(1<<4);
#endif

    while (1);
}