/** * \brief Test the communication between CAN1 Mailbox 3 and CAN0 Mailbox 3. */ static void test_4(void) { can_reset_all_mailbox(CAN0); can_reset_all_mailbox(CAN1); puts("\n\rTest4: CAN1 Mailbox 3 requesting"); puts("CAN0 Mailbox 3 to transmit a data frame" STRING_EOL); /* Init CAN0 Mailbox 3 to Producer Mailbox. */ reset_mailbox_conf(&can0_mailbox); can0_mailbox.ul_mb_idx = TEST4_CAN0_MB_IDX; can0_mailbox.uc_obj_type = CAN_MB_PRODUCER_MODE; can0_mailbox.ul_id_msk = 0; can0_mailbox.ul_id = CAN_MID_MIDvA(TEST4_CAN0_MB_ID); can_mailbox_init(CAN0, &can0_mailbox); /* Prepare the response information when it receives a remote frame. */ can0_mailbox.ul_datal = CAN_MSG_TOGGLE_LED_0; can0_mailbox.ul_datah = CAN_MSG_DUMMY_DATA; can0_mailbox.uc_length = MAX_CAN_FRAME_DATA_LEN; can_mailbox_write(CAN0, &can0_mailbox); can_global_send_transfer_cmd(CAN0, CAN_TCR_MB3); /* Init CAN1 Mailbox 3 to Consumer Mailbox. It sends a remote frame and waits for an answer. */ reset_mailbox_conf(&can1_mailbox); can1_mailbox.ul_mb_idx = TEST4_CAN1_TX_MB_IDX; can1_mailbox.uc_obj_type = CAN_MB_CONSUMER_MODE; can1_mailbox.uc_tx_prio = TEST4_CAN1_TX_MB_PRIO; can1_mailbox.ul_id_msk = CAN_MID_MIDvA_Msk | CAN_MID_MIDvB_Msk; can1_mailbox.ul_id = CAN_MID_MIDvA(TEST4_CAN1_TX_MB_ID); can_mailbox_init(CAN1, &can1_mailbox); /* Enable CAN1 mailbox 3 interrupt. */ can_enable_interrupt(CAN1, CAN_IER_MB3); can_global_send_transfer_cmd(CAN1, CAN_TCR_MB3); while (!g_ul_recv_status) { } if ((can1_mailbox.ul_datal == CAN_MSG_TOGGLE_LED_0) && (can1_mailbox.uc_length == MAX_CAN_FRAME_DATA_LEN)) { puts("Test4 passed" STRING_EOL); decode_can_msg(&can1_mailbox); } else { puts("Test4 ERROR" STRING_EOL); } }
/** * \brief Test the transmission from CAN0 Mailbox 0 to CAN1 Mailbox 0. */ static void test_1(void) { can_reset_all_mailbox(CAN0); can_reset_all_mailbox(CAN1); puts("\n\rTest1: CAN0 Mailbox 0 transmitting to CAN1 Mailbox 0" STRING_EOL); /* Init CAN1 Mailbox 0 to Reception Mailbox. */ reset_mailbox_conf(&can1_mailbox); can1_mailbox.ul_mb_idx = TEST1_CAN_COMM_MB_IDX; 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(TEST1_CAN_TRANSFER_ID); can_mailbox_init(CAN1, &can1_mailbox); /* Init CAN0 Mailbox 0 to Transmit Mailbox. */ reset_mailbox_conf(&can0_mailbox); can0_mailbox.ul_mb_idx = TEST1_CAN_COMM_MB_IDX; can0_mailbox.uc_obj_type = CAN_MB_TX_MODE; can0_mailbox.uc_tx_prio = TEST1_CAN0_TX_PRIO; 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(TEST1_CAN_TRANSFER_ID); can0_mailbox.ul_datal = CAN_MSG_TOGGLE_LED_0; 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_MB0); /* Send out the information in the mailbox. */ can_global_send_transfer_cmd(CAN0, CAN_TCR_MB0); while (!g_ul_recv_status) { } if ((can1_mailbox.ul_datal == CAN_MSG_TOGGLE_LED_0) && (can1_mailbox.uc_length == MAX_CAN_FRAME_DATA_LEN)) { puts("Test1 passed" STRING_EOL); decode_can_msg(&can1_mailbox); } else { puts("Test1 ERROR" STRING_EOL); } }
/** * \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; }
/** * \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) { } }
/** * \brief Test the transmission from CAN0 Mailboxes 1 & 2 to CAN1 Mailbox 7 with overwrite. */ static void test_3(void) { can_reset_all_mailbox(CAN0); can_reset_all_mailbox(CAN1); puts("\n\rTest3: CAN0 Mailboxes 1 & 2 transmitting to"); puts("CAN1 Mailbox 7 with overwrite." STRING_EOL); /* Init CAN1 Mailbox 7 to Reception Mailbox with overwrite. */ reset_mailbox_conf(&can1_mailbox); can1_mailbox.ul_mb_idx = TEST3_CAN1_RX_MB_IDX; can1_mailbox.uc_obj_type = CAN_MB_RX_OVER_WR_MODE; can1_mailbox.ul_id_msk = 0; can1_mailbox.ul_id = 0; can_mailbox_init(CAN1, &can1_mailbox); /* Init CAN0 Mailbox 1 to Transmit Mailbox. This message will be discarded. */ reset_mailbox_conf(&can0_mailbox); can0_mailbox.ul_mb_idx = TEST3_CAN0_TX_MB1_IDX; can0_mailbox.uc_obj_type = CAN_MB_TX_MODE; can0_mailbox.uc_tx_prio = TEST3_CAN0_TX_MB1_PRIO; 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(TEST3_CAN0_TX_MB1_ID); can0_mailbox.ul_datal = CAN_MSG_TOGGLE_LED_0; can0_mailbox.ul_datah = CAN_MSG_DUMMY_DATA; can0_mailbox.uc_length = MAX_CAN_FRAME_DATA_LEN; can_mailbox_write(CAN0, &can0_mailbox); /* Init CAN0 Mailbox 2 to Transmit Mailbox. This message will be received first. */ reset_mailbox_conf(&can0_mailbox); can0_mailbox.ul_mb_idx = TEST3_CAN0_TX_MB2_IDX; can0_mailbox.uc_obj_type = CAN_MB_TX_MODE; can0_mailbox.uc_tx_prio = TEST3_CAN0_TX_MB2_PRIO; 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(TEST3_CAN0_TX_MB2_ID); can0_mailbox.ul_datal = CAN_MSG_TOGGLE_LED_1; can0_mailbox.ul_datah = CAN_MSG_DUMMY_DATA; can0_mailbox.uc_length = MAX_CAN_FRAME_DATA_LEN; can_mailbox_write(CAN0, &can0_mailbox); /* Send out the information in mailbox 1 & 2 at the same time. */ can_global_send_transfer_cmd(CAN0, (CAN_TCR_MB1 | CAN_TCR_MB2)); puts("Wait for all the transmit mailboxes done." STRING_EOL); /* Enable CAN1 mailbox 7 interrupt. */ can_enable_interrupt(CAN1, CAN_IER_MB7); while (!g_ul_recv_status) { } if ((can1_mailbox.ul_datal == CAN_MSG_TOGGLE_LED_0) && (can1_mailbox.uc_length == MAX_CAN_FRAME_DATA_LEN)) { puts("Test3 passed" STRING_EOL); decode_can_msg(&can1_mailbox); } else { puts("Test3 ERROR" STRING_EOL); } }