Mcuzone 野芯科技
标题:
SAM4E-EK开发板代码解读04——CAN
[打印本页]
作者:
Mcuzone_TKN
时间:
2020-5-13 13:48
标题:
SAM4E-EK开发板代码解读04——CAN
本帖最后由 Mcuzone_TKN 于 2020-5-15 09:13 编辑
关键词:
Microchip Atmel SAM4E SAM4E-EK SAM4E16E 芯片CAN 控制器局域网络
概述:实现板载两路之间的收发
开发板有两路CAN,每个CAN有8个信箱。CAN总线上的逻辑数值是用显性电平和隐性电平表示,显性是指在同时传输显性电平和隐性电平时,总线上呈现的是显性电平。显性电平表示逻辑0,隐性电平表示逻辑1。
打开产品光盘SAM4E16E-EK/SAM4E16E-EK中文资料/softpack软件包/Atmel Studio 7,打开03_CAN_EXAMPLE例子。
int main(void)
{
uint32_t ul_sysclk;
uint8_t uc_char;
sysclk_init();
board_init();
configure_console();
//输出示例信息
puts(STRING_HEADER);
//初始化CAN0收发信机
sn65hvd234_set_rs(&can0_transceiver, PIN_CAN0_TR_RS_IDX);
sn65hvd234_set_en(&can0_transceiver, PIN_CAN0_TR_EN_IDX);
//启用CAN0收发信机
sn65hvd234_disable_low_power(&can0_transceiver);
sn65hvd234_enable(&can0_transceiver);
/初始化CAN1收发信机/
sn65hvd234_set_rs(&can1_transceiver, PIN_CAN1_TR_RS_IDX);
sn65hvd234_set_en(&can1_transceiver, PIN_CAN1_TR_EN_IDX);
//使能CAN1收发信机
sn65hvd234_disable_low_power(&can1_transceiver);
sn65hvd234_enable(&can1_transceiver);
//使能CAN0和CAN1时钟
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);
//关闭CAN0和CAN1的中断
can_disable_interrupt(CAN0, CAN_DISABLE_ALL_INTERRUPT_MASK);
can_disable_interrupt(CAN1, CAN_DISABLE_ALL_INTERRUPT_MASK);
//配置并启用CAN1的中断
NVIC_EnableIRQ(CAN1_IRQn);
//运行测试
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;
//关闭CAN0
can_disable(CAN0);
//关闭CAN0收发信机
sn65hvd234_enable_low_power(&can0_transceiver);
sn65hvd234_disable(&can0_transceiver);
//关闭CAN1
can_disable(CAN1);
//关闭CAN1收发器
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) {
}
}
图1.png
(55.8 KB, 下载次数: 9877)
下载附件
2020-5-13 13:32 上传
原理图
作者:
Mcuzone_TKN
时间:
2020-5-13 13:56
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);
//初始化CAN1邮箱0到接收邮箱
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);
//初始化CAN0邮箱0来传输邮箱
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);
//写发送信息到邮箱
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);
//使能CAN1邮箱0中断
can_enable_interrupt(CAN1, CAN_IER_MB0);
//把邮箱里的信息发出去
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);
}
}
复制代码
欢迎光临 Mcuzone 野芯科技 (http://testbbs.mcuzone.com/)
Powered by Discuz! X3.1