Пример #1
0
/**
 * \brief Initializes and enables CAN0 & CAN1 tranceivers and clocks. 
 * CAN0/CAN1 mailboxes are reset and interrupts disabled.
 */
void can_initialize(void)
{
	uint32_t ul_sysclk;
	uint32_t x = 1;

	/* Initialize CAN0 Transceiver. */
	sn65hvd234_set_rs(&can0_transceiver, PIN_CAN0_TR_RS_IDX);
	sn65hvd234_set_en(&can0_transceiver, PIN_CAN0_TR_EN_IDX);
	/* Enable CAN0 Transceiver. */
	sn65hvd234_disable_low_power(&can0_transceiver);
	sn65hvd234_enable(&can0_transceiver);

	/* Initialize CAN1 Transceiver. */
	sn65hvd234_set_rs(&can1_transceiver, PIN_CAN1_TR_RS_IDX);
	sn65hvd234_set_en(&can1_transceiver, PIN_CAN1_TR_EN_IDX);
	/* Enable CAN1 Transceiver. */
	sn65hvd234_disable_low_power(&can1_transceiver);
	sn65hvd234_enable(&can1_transceiver);

	/* Enable CAN0 & CAN1 clock. */
	pmc_enable_periph_clk(ID_CAN0);
	pmc_enable_periph_clk(ID_CAN1);

	ul_sysclk = sysclk_get_cpu_hz();
	if (can_init(CAN0, ul_sysclk, CAN_BPS_250K) &&
	can_init(CAN1, ul_sysclk, CAN_BPS_250K)) {

	/* Disable all CAN0 & CAN1 interrupts. */
	can_disable_interrupt(CAN0, CAN_DISABLE_ALL_INTERRUPT_MASK);
	can_disable_interrupt(CAN1, CAN_DISABLE_ALL_INTERRUPT_MASK);
		
	NVIC_EnableIRQ(CAN0_IRQn);
	NVIC_EnableIRQ(CAN1_IRQn);
	
	can_reset_all_mailbox(CAN0);
	can_reset_all_mailbox(CAN1);
	
	/* Initialize the CAN0 & CAN1 mailboxes */
	x = can_init_mailboxes(x); // Prevent Random PC jumps to this point.
	//configASSERT(x);
	
	
	}
	return;
}
Пример #2
0
/**
 * \brief Responds to he command from CAN0 and sends to CAN1
 **/
void command_in(void)
{
	pio_toggle_pin(LED0_GPIO);
	
	can_disable_interrupt(CAN0, CAN_IER_MB0);
	NVIC_DisableIRQ(CAN0_IRQn);
	
	can_reset_all_mailbox(CAN0);
	can_reset_all_mailbox(CAN1);

	/* Init CAN1 Mailbox 0 to Reception Mailbox. */
	reset_mailbox_conf(&can0_mailbox);
	can1_mailbox.ul_mb_idx = 1;
	can1_mailbox.uc_obj_type = CAN_MB_RX_MODE;
	can1_mailbox.ul_id_msk = CAN_MAM_MIDvA_Msk | CAN_MAM_MIDvB_Msk;
	can1_mailbox.ul_id = CAN_MID_MIDvA(7);
	can_mailbox_init(CAN1, &can1_mailbox);

	/* Init CAN0 Mailbox 0 to Transmit Mailbox. */
	reset_mailbox_conf(&can0_mailbox);
	can0_mailbox.ul_mb_idx = 1;
	can0_mailbox.uc_obj_type = CAN_MB_TX_MODE;
	can0_mailbox.uc_tx_prio = 15;
	can0_mailbox.uc_id_ver = 0;
	can0_mailbox.ul_id_msk = 0;
	can_mailbox_init(CAN0, &can0_mailbox);

	/* Write transmit information into mailbox. */
	can0_mailbox.ul_id = CAN_MID_MIDvA(7);
	can0_mailbox.ul_datal = COMMAND_OUT;
	can0_mailbox.ul_datah = CAN_MSG_DUMMY_DATA;
	can0_mailbox.uc_length = MAX_CAN_FRAME_DATA_LEN;
	can_mailbox_write(CAN0, &can0_mailbox);

	/* Enable CAN1 mailbox 0 interrupt. */
	can_enable_interrupt(CAN1, CAN_IER_MB1);

	/* Send out the information in the mailbox. */
	can_global_send_transfer_cmd(CAN0, CAN_TCR_MB1);

	/* potentially @non-terminating@ */
	while (!g_ul_recv_status) {
	}
}
Пример #3
0
/**
 *  \brief can_example application entry point.
 *
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint32_t ul_sysclk;
	uint8_t uc_char;

	/* Initialize the SAM system. */
	sysclk_init();
	board_init();

	/* Configure UART for debug message output. */
	configure_console();

	/* Output example information. */
	puts(STRING_HEADER);

	/* Initialize CAN0 Transceiver. */
	sn65hvd234_set_rs(&can0_transceiver, PIN_CAN0_TR_RS_IDX);
	sn65hvd234_set_en(&can0_transceiver, PIN_CAN0_TR_EN_IDX);
	/* Enable CAN0 Transceiver. */
	sn65hvd234_disable_low_power(&can0_transceiver);
	sn65hvd234_enable(&can0_transceiver);

	/* Initialize CAN1 Transceiver. */
	sn65hvd234_set_rs(&can1_transceiver, PIN_CAN1_TR_RS_IDX);
	sn65hvd234_set_en(&can1_transceiver, PIN_CAN1_TR_EN_IDX);
	/* Enable CAN1 Transceiver. */
	sn65hvd234_disable_low_power(&can1_transceiver);
	sn65hvd234_enable(&can1_transceiver);

	/* Enable CAN0 & CAN1 clock. */
	pmc_enable_periph_clk(ID_CAN0);
	pmc_enable_periph_clk(ID_CAN1);

	ul_sysclk = sysclk_get_cpu_hz();
	if (can_init(CAN0, ul_sysclk, CAN_BPS_1000K) &&
		can_init(CAN1, ul_sysclk, CAN_BPS_1000K)) {
		puts("CAN initialization is completed." STRING_EOL);

		/* Disable all CAN0 & CAN1 interrupts. */
		can_disable_interrupt(CAN0, CAN_DISABLE_ALL_INTERRUPT_MASK);
		can_disable_interrupt(CAN1, CAN_DISABLE_ALL_INTERRUPT_MASK);

		/* Configure and enable interrupt of CAN1, as the tests will use receiver interrupt. */
		NVIC_EnableIRQ(CAN1_IRQn);

		/* Run tests. */
		puts("Press any key to start test" STRING_EOL);
		while (uart_read(CONSOLE_UART, &uc_char)) {
		}
		test_1();
		g_ul_recv_status = 0;

		puts("Press any key to continue..." STRING_EOL);
		while (uart_read(CONSOLE_UART, &uc_char)) {
		}
		test_2();
		g_ul_recv_status = 0;

		puts("Press any key to continue..." STRING_EOL);
		while (uart_read(CONSOLE_UART, &uc_char)) {
		}
		test_3();
		g_ul_recv_status = 0;

		puts("Press any key to continue..." STRING_EOL);
		while (uart_read(CONSOLE_UART, &uc_char)) {
		}
		test_4();
		g_ul_recv_status = 0;

		/* Disable CAN0 Controller */
		can_disable(CAN0);
		/* Disable CAN0 Transceiver */
		sn65hvd234_enable_low_power(&can0_transceiver);
		sn65hvd234_disable(&can0_transceiver);

		/* Disable CAN1 Controller */
		can_disable(CAN1);
		/* Disable CAN1 Transceiver */
		sn65hvd234_enable_low_power(&can1_transceiver);
		sn65hvd234_disable(&can1_transceiver);

		puts("End of all tests" STRING_EOL);
	} else {
		puts("CAN initialization (sync) ERROR" STRING_EOL);
	}

	while (1) {
	}
}