Example #1
0
u32 platform_can_setup( unsigned id, u32 clock )
{  
  LPC_CAN_TypeDef * canx;
  uint32_t div;

  switch (id)
  {
    case 0: canx = LPC_CAN1; break;
    case 1: canx = LPC_CAN2; break;
    default: return 0;
  }

  CAN_DeInit(canx); 
  CAN_Init(canx, clock);
  CAN_ModeConfig(canx, CAN_OPERATING_MODE, ENABLE); 
  CAN_IRQCmd(canx, CANINT_RIE, ENABLE);   // Receive IRQ 
  CAN_IRQCmd(canx, CANINT_EIE, ENABLE);   // Error IRQ 
  CAN_IRQCmd(canx, CANINT_BEIE, ENABLE);  // Bus error IRQ 
  LPC_CANAF->AFMR = 2;                    // Filter bypass (receive all messages) 
  NVIC_EnableIRQ(CAN_IRQn);               // Enable IRQs

  // Fix clock
  LPC_SC->PCLKSEL0 &= ~(3<<26 | 3<<28 | 3<<30); // PCLK / 2
  LPC_SC->PCLKSEL0 |=  (2<<26 | 2<<28 | 2<<30);
  div = (SystemCoreClock / 20) / clock;
  div --;
	canx->MOD = 0x01; // Enter reset mode
	canx->BTR  = (div & 0x3FF) | (3<<14) | (5<<16) | (2<<20) ; // Set bit timing
	canx->MOD = 0;    // Return to normal operating

  // Change pin function (for now using the MBED ones)
  // And read clock divider
  if (id == 0)
  {
    // Pin function
    LPC_PINCON->PINSEL0 &= ~(3<<0 | 3<<2);
    LPC_PINCON->PINSEL0 |= (1<<0 | 1<<2);

    // No pull up / pull down
    LPC_PINCON->PINMODE0 &= ~(3<<0 | 3<<2);
    LPC_PINCON->PINMODE0 |= (2<<0 | 2<<2);

    // NOT open drain
    LPC_PINCON->PINMODE_OD0 &= ~(1<<0 | 1<<1);
  }
  else
  {
    LPC_PINCON->PINSEL0 &= ~(3<<8 | 3<<10);
    LPC_PINCON->PINSEL0 |= (2<<8 | 2<<10);

    // No pull up / pull down
    LPC_PINCON->PINMODE0 &= ~(3<<8 | 3<<10);
    LPC_PINCON->PINMODE0 |= (2<<8 | 2<<10);

    // NOT open drain
    LPC_PINCON->PINMODE_OD0 &= ~(1<<4 | 1<<5);
  }

  return (SystemCoreClock / 20) / ((div & 0x3FF)+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);
}
/*********************************************************************//**
 * @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);
}
Example #4
0
/*********************************************************************//**
 * @brief		c_entry: Main CAN program body
 * @param[in]	none
 * @return 		int
 **********************************************************************/
int c_entry(void) { /* Main Program */
	uint32_t i;
	uint32_t cnt;
	CAN_ERROR error;
	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 Receive Interrupt
	CAN_IRQCmd(LPC_CAN2, CANINT_FCE, ENABLE);
	CAN_IRQCmd(LPC_CAN2, CANINT_RIE, ENABLE);

	//Enable CAN Interrupt
	NVIC_EnableIRQ(CAN_IRQn);

	/* First, we send 10 messages:
	 * - message 0,2,4,6,8 have id in AFLUT >>> will be received
	 * - message 1,3,5,7,9 don't have id in AFLUT >>> will be ignored
	 * Then, we change AFLUT by load/remove entries in AFLUT and re-send messages
	 * - message 1,3,5,7,9 have id in AFLUT >>> will be received
	 * - message 0,2,4,6,8 don't have id in AFLUT >>> will be ignored
	 * Note that: FullCAN Object must be read from FullCAN Object Section next to AFLUT
	 */
	/*-------------------------Init Message & AF Look-up Table------------------------*/

	_DBG_("Test Acceptance Filter function...");
	_DBG_("Press '1' to initialize message and AF Loop-up Table...");_DBG_("");
	while(_DG !='1');
	CAN_InitAFMessage(); /* initialize Transmit Message */
	_DBG_("Init message finished!!!");
	CAN_SetupAFTable(); /* initialize AF Look-up Table sections*/
	error = CAN_SetupAFLUT(LPC_CANAF,&AFTable); /* install AF Look-up Table */
	if (error != CAN_OK) {
		_DBG_("Setup AF: ERROR...");
		while (1); // AF Table has error
	}
	else _DBG_("Setup AF: SUCCESSFUL!!!");_DBG_("");


	/*-------------------------Send messages------------------------*/
	_DBG_("Press '2' to start CAN transferring operation...");_DBG_("");
	while(_DG !='2');
	for (i = 0; i < CAN_TX_MSG_CNT; i++) {
		CAN_SendMsg(LPC_CAN1, &AFTxMsg[i]);
		PrintMessage(&AFTxMsg[i]);_DBG_("");
		for(cnt=0;cnt<10000;cnt++); //transmit delay
		CANTxCount++;
	}
	_DBG_("Sending finished !!!");

	/*-------------------------Display Received messages------------------------*/
	_DBG_("Press '3' to display received messages...");_DBG_("");
	while(_DG !='3');
	for (i = 0; i < CAN_RX_MSG_CNT; i++) {
		PrintMessage(&AFRxMsg[i]);_DBG_("");
	}

	/*-------------------------Change AFLUT Table --------------------*/
	_DBG_("Press '4' to change AF look-up table...");_DBG_("");
	while(_DG !='4');
	CAN_ChangeAFTable();
	_DBG_("Change AFLUT: FINISHED!!!");
	CAN_SetAFMode(LPC_CANAF, CAN_eFCAN);
	CAN_InitAFMessage();
	CANRxCount = CANTxCount = 0;

	/*-------------------------Re-Send messages------------------------*/
	_DBG_("Press '5' to re-send messages...");_DBG_("");
	while(_DG !='5');
	for (i = 0; i < CAN_TX_MSG_CNT; i++) {
		CAN_SendMsg(LPC_CAN1, &AFTxMsg[i]);
		PrintMessage(&AFTxMsg[i]);_DBG_("");
		for(cnt=0;cnt<10000;cnt++); //transmit delay
		CANTxCount++;
	}

	/*-------------------------Display received messages------------------------*/
	_DBG_("Re-Sending finished !!!");

	_DBG_("Press '6' to display received messages...");_DBG_("");
	while(_DG !='6');
	for (i = 0; i < CAN_RX_MSG_CNT; i++) {
		PrintMessage(&AFRxMsg[i]);_DBG_("");
	}
	_DBG_("Demo terminal !!!");

	CAN_DeInit(LPC_CAN1);
	CAN_DeInit(LPC_CAN2);
	while (1);
	return 0;
}
Example #5
0
/*********************************************************************//**
							 THE MAIN CAN BODY
 **********************************************************************/
int c_entry(void) { /* Main Program */
	uint32_t i;
	uint32_t cnt;
	CAN_ERROR error;
	CAN_PinCFG_Type CAN1PinStruct, CAN2PinStruct;

	NVIC_DeInit();
	NVIC_SCBDeInit();

	/* Configure the NVIC Preemption Priority Bits:
	 * two (2) bits of preemption priority, six (6) bits of sub-priority.
	 * Since the Number of Bits used for Priority Levels is five (5), so the
	 * actual bit number of sub-priority is three (3)
	 */
	NVIC_SetPriorityGrouping(0x06);

	//  Set Vector table offset value
#if (__RAM_MODE__==1)
	NVIC_SetVTOR(0x10000000);
#else
	NVIC_SetVTOR(0x00000000);
#endif

	debug_frmwrk_init();
	print_menu();

	/* Pin Configuration */
	CAN1PinStruct.RD = CAN_RD1_P0_0;
	CAN1PinStruct.TD = CAN_TD1_P0_1;
	CAN2PinStruct.RD = CAN_RD2_P2_7;
	CAN2PinStruct.TD = CAN_TD2_P2_8;

	PINSEL_ConfigPin((PINSEL_CFG_Type *) (&can_rd1_pin[CAN1PinStruct.RD]));
	PINSEL_ConfigPin((PINSEL_CFG_Type *) (&can_td1_pin[CAN1PinStruct.TD]));
	PINSEL_ConfigPin((PINSEL_CFG_Type *) (&can_rd2_pin[CAN2PinStruct.RD]));
	PINSEL_ConfigPin((PINSEL_CFG_Type *) (&can_td2_pin[CAN2PinStruct.TD]));

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

	//Enable Receive Interrupt
	CAN_IRQCmd(LPC_CAN2, CANINT_FCE, ENABLE);
	CAN_IRQCmd(LPC_CAN2, CANINT_RIE, ENABLE);
	CAN_IRQCmd(LPC_CAN2, CANINT_DOIE, ENABLE);

	CAN_SetupCBS(CANINT_FCE,  CAN_Callback0);
	CAN_SetupCBS(CANINT_RIE,  CAN_Callback1);
	CAN_SetupCBS(CANINT_DOIE, CAN_Callback2);

	//Enable CAN Interrupt
	NVIC_EnableIRQ(CAN_IRQn);

	/* First, we send 10 messages:
	 * - message 0,2,4,6,8 have id in AFLUT >>> will be received
	 * - message 1,3,5,7,9 don't have id in AFLUT >>> will be ignored
	 * Then, we change AFLUT by load/remove entries in AFLUT and re-send messages
	 * - message 1,3,5,7,9 have id in AFLUT >>> will be received
	 * - message 0,2,4,6,8 don't have id in AFLUT >>> will be ignored
	 * Note that: FullCAN Object must be read from FullCAN Object Section next to AFLUT
	 */
	/*-------------------------Init Message & AF Look-up Table------------------------*/

	_DBG_("Test Acceptance Filter function...");
	_DBG_("Press '1' to initialize message and AF Loop-up Table...");_DBG_("");
	while(_DG !='1');
	CAN_InitAFMessage(); /* initialize Transmit Message */
	_DBG_("Init message finished!!!");
	CAN_SetupAFTable(); /* initialize AF Look-up Table sections*/
	error = CAN_SetupAFLUT(LPC_CANAF,&AFTable); /* install AF Look-up Table */
	if (error != CAN_OK) {
		_DBG_("Setup AF: ERROR...");
		while (1); // AF Table has error
	}
	else _DBG_("Setup AF: SUCCESSFUL!!!");_DBG_("");


	/*-------------------------Send messages------------------------*/
	_DBG_("Press '2' to start CAN transferring operation...");_DBG_("");
	while(_DG !='2');
	for (i = 0; i < CAN_TX_MSG_CNT; i++) {
		CAN_SendMsg(LPC_CAN1, &AFTxMsg[i]);
		PrintMessage(&AFTxMsg[i]);_DBG_("");
		for(cnt=0;cnt<10000;cnt++); //transmit delay
		CANTxCount++;
	}
	_DBG_("Sending finished !!!");

	/*-------------------------Display Received messages------------------------*/
	_DBG_("Press '3' to display received messages...");_DBG_("");
	while(_DG !='3');
	for (i = 0; i < CAN_RX_MSG_CNT; i++) {
		PrintMessage(&AFRxMsg[i]);_DBG_("");
	}

	/*-------------------------Change AFLUT Table --------------------*/
	_DBG_("Press '4' to change AF look-up table...");_DBG_("");
	while(_DG !='4');
	CAN_ChangeAFTable();
	_DBG_("Change AFLUT: FINISHED!!!");
	CAN_SetAFMode(LPC_CANAF, CAN_eFCAN);
	CAN_InitAFMessage();
	CANRxCount = CANTxCount = 0;

	/*-------------------------Re-Send messages------------------------*/
	_DBG_("Press '5' to re-send messages...");_DBG_("");
	while(_DG !='5');
	for (i = 0; i < CAN_TX_MSG_CNT; i++) {
		CAN_SendMsg(LPC_CAN1, &AFTxMsg[i]);
		PrintMessage(&AFTxMsg[i]);_DBG_("");
		for(cnt=0;cnt<10000;cnt++); //transmit delay
		CANTxCount++;
	}

	/*-------------------------Display received messages------------------------*/
	_DBG_("Re-Sending finished !!!");

	_DBG_("Press '6' to display received messages...");_DBG_("");
	while(_DG !='6');
	for (i = 0; i < CAN_RX_MSG_CNT; i++) {
		PrintMessage(&AFRxMsg[i]);_DBG_("");
	}
	_DBG_("Demo terminal !!!");

	CAN_DeInit(LPC_CAN1);
	CAN_DeInit(LPC_CAN2);
	while (1);
	return 0;
}
/*********************************************************************//**
 * @brief		c_entry: Main CAN program body
 * @param[in]	none
 * @return 		none
 **********************************************************************/
void c_entry(void)
{
	uint32_t i;
	volatile uint32_t cnt;
	CAN_ERROR error;

	CANRxCount = CANTxCount = 0;

	/* 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 P0.4 as RD2, P0.5 as RD2
	 */

	PINSEL_ConfigPin (0, 0, 1);

	PINSEL_ConfigPin (0, 1, 1);

#if (RECVD_CAN_NO != CAN_1)
	PINSEL_ConfigPin (0, 4, 2);

	PINSEL_ConfigPin (0, 5, 2);
#endif

	//Initialize CAN1 & CAN2
	CAN_Init(CAN_1, 125000);

#if (RECVD_CAN_NO != CAN_1)
	CAN_Init(RECVD_CAN_NO, 125000);
#endif

	//Enable Receive Interrupt
	CAN_IRQCmd(RECVD_CAN_NO, CANINT_FCE, ENABLE);
	CAN_IRQCmd(RECVD_CAN_NO, CANINT_RIE, ENABLE);

	//Enable CAN Interrupt
	NVIC_EnableIRQ(CAN_IRQn);

	/* First, we send 10 messages:
	 * - message 0,2,4,6,8 have id in AFLUT >>> will be received
	 * - message 1,3,5,7,9 don't have id in AFLUT >>> will be ignored
	 * Then, we change AFLUT by load/remove entries in AFLUT and re-send messages
	 * - message 1,3,5,7,9 have id in AFLUT >>> will be received
	 * - message 0,2,4,6,8 don't have id in AFLUT >>> will be ignored
	 * Note that: FullCAN Object must be read from FullCAN Object Section next to AFLUT
	 */
	/*-------------------------Init Message & AF Look-up Table------------------------*/

	_DBG_("Test Acceptance Filter function...");

	_DBG_("Press '1' to initialize message and AF Loop-up Table...");_DBG_("");
	while(_DG !='1');

	/* initialize Transmit Message */
	CAN_InitAFMessage();

	_DBG_("Init message finished!!!");

	/* initialize AF Look-up Table sections*/
	CAN_SetupAFTable();

	/* install AF Look-up Table */
	error = CAN_SetupAFLUT(&AFTable);
	if (error != CAN_OK)
	{
		_DBG_("Setup AF: ERROR...");
		while (1); // AF Table has error
	}
	else
	{
		_DBG_("Setup AF: SUCCESSFUL!!!");_DBG_("");
	}

	/*-------------------------Send messages------------------------*/
	_DBG_("Press '2' to start CAN transferring operation...");_DBG_("");
	while(_DG !='2');

	for (i = 0; i < CAN_TX_MSG_CNT; i++)
	{
		CAN_SendMsg(CAN_1, (CAN_MSG_Type *)&AFTxMsg[i]);

		PrintMessage((CAN_MSG_Type *)&AFTxMsg[i]);_DBG_("");

		for(cnt=0;cnt<10000;cnt++); //transmit delay

		CANTxCount++;
	}

	_DBG_("Sending finished !!!");_DBG_("");

	if(CANRxCount == 0)
	{
		_DBG_(">>> No message is recieved. Please check the connection between 2 CANs!!!");_DBG_("");
	}

	/*-------------------------Display Received messages------------------------*/
	_DBG_("Press '3' to display received messages...");_DBG_("");
	while(_DG !='3');

	for (i = 0; i < CAN_RX_MSG_CNT; i++)
	{
		PrintMessage((CAN_MSG_Type *)&AFRxMsg[i]);_DBG_("");
	}

	/*-------------------------Change AFLUT Table --------------------*/
	_DBG_("Press '4' to change AF look-up table...");_DBG_("");
	while(_DG !='4');

	CAN_ChangeAFTable();

	_DBG_("Change AFLUT: FINISHED!!!");

	//CAN_SetAFMode(LPC_CANAF, CAN_eFCAN);
	CAN_SetAFMode(CAN_EFCAN);

	CAN_InitAFMessage();

	CANRxCount = CANTxCount = 0;

	/*-------------------------Re-Send messages------------------------*/
	_DBG_("Press '5' to re-send messages...");_DBG_("");
	while(_DG !='5');

	for (i = 0; i < CAN_TX_MSG_CNT; i++)
	{
		CAN_SendMsg(CAN_1, (CAN_MSG_Type *)&AFTxMsg[i]);

		PrintMessage((CAN_MSG_Type *)&AFTxMsg[i]);_DBG_("");

		for(cnt=0;cnt<10000;cnt++); //transmit delay

		CANTxCount++;
	}

	/*-------------------------Display received messages------------------------*/
	_DBG_("Re-Sending finished !!!");_DBG_("");

	if(CANRxCount == 0)
	{
		_DBG_(">>> No message is recieved. Please check the connection between 2 CANs!!!");_DBG_("");
	}

	_DBG_("Press '6' to display received messages...");_DBG_("");
	while(_DG !='6');

	for (i = 0; i < CAN_RX_MSG_CNT; i++)
	{
		PrintMessage((CAN_MSG_Type *)&AFRxMsg[i]);_DBG_("");
	}

	_DBG_("Demo terminal !!!");

	CAN_DeInit(CAN_1);

#if (RECVD_CAN_NO != CAN_1)
	CAN_DeInit(CAN_2);
#endif

	while (1);

}