【先楫HPM6750测评】测试CAN收发
<p><span style="font-family:楷体;"><span style="font-size:18px;"> HPM6750有4个CAN控制器,支持CANFD,目前在工业上用的还是CAN2.0比较多,本次测评使用CAN1控制器。CAN驱动芯片使用TI的VD230,电脑端使用了一个USB转CAN调试器,带有调试上位机软件。</span></span></p><p><span style="font-family:楷体;"><span style="font-size:18px;">连线如下:</span></span></p>
<p><span style="font-family:楷体;"><span style="font-size:18px;">1.初始化CAN引脚和时钟</span></span></p>
<p><span style="font-family:楷体;"><span style="font-size:18px;"> HPM_IOC->PAD.FUNC_CTL = IOC_PE31_FUNC_CTL_CAN1_TXD;<br />
HPM_IOC->PAD.FUNC_CTL = IOC_PE30_FUNC_CTL_CAN1_RXD;</span></span></p>
<p><span style="font-family:楷体;"><span style="font-size:18px;"> /* Set the CAN1 peripheral clock to 80MHz */<br />
clock_set_source_divider(clock_can1, clk_src_pll1_clk1, 5);<br />
freq = clock_get_frequency(clock_can1);</span></span></p>
<p><span style="font-family:楷体;"><span style="font-size:18px;">2.初始化CAN</span></span></p>
<p><span style="font-family:楷体;"><span style="font-size:18px;"> can_config_t can_config;<br />
can_get_default_config(&can_config);<br />
can_config.baudrate = 500000; /* 500kbps */<br />
can_config.mode = can_mode_normal;<br />
board_init_can(HPM_CAN1);<br />
uint32_t can_src_clk_freq = board_init_can_clock(BOARD_APP_CAN_BASE);<br />
hpm_stat_t status = can_init(HPM_CAN1, &can_config, can_src_clk_freq);<br />
if (status != status_success) {<br />
printf("CAN initialization failed, error code: %d\n", status);<br />
return;<br />
}</span></span></p>
<p><span style="font-family:楷体;"><span style="font-size:18px;">3.设置CAN中断</span></span></p>
<p><span style="font-family:楷体;"><span style="font-size:18px;"> can_enable_tx_rx_irq(HPM_CAN1, CAN_EVENT_RECEIVE);<br />
intc_m_enable_irq_with_priority(IRQn_CAN1, 1);</span></span></p>
<p><span style="font-family:楷体;"><span style="font-size:18px;">4.CAN中断</span></span></p>
<p> </p>
<p>SDK_DECLARE_EXT_ISR_M(IRQn_CAN1, test_can1_isr);</p>
<p>void test_can1_isr(void)<br />
{<br />
uint8_t flags = can_get_tx_rx_flags(HPM_CAN1);<br />
if ((flags & CAN_EVENT_RECEIVE) != 0) {<br />
can_read_received_message(HPM_CAN1, (can_receive_buf_t *)&s_can_rx_buf);<br />
has_new_rcv_msg = true;<br />
}<br />
if ((flags & (CAN_EVENT_TX_PRIMARY_BUF | CAN_EVENT_TX_SECONDARY_BUF))) {<br />
has_sent_out = true;<br />
}<br />
if ((flags & CAN_EVENT_ERROR) != 0) {<br />
has_error = true;<br />
}<br />
can_clear_tx_rx_flags(HPM_CAN1, flags);</p>
<p> error_flags = can_get_error_interrupt_flags(HPM_CAN1);<br />
can_clear_error_interrupt_flags(HPM_CAN1, error_flags);<br />
}</p>
<p><span style="font-family:楷体;"><span style="font-size:18px;">5.CAN发送接收到的数据</span></span></p>
<p><span style="font-family:楷体;"><span style="font-size:18px;"> </span></span></p>
<p><span style="font-family:楷体;"><span style="font-size:18px;"> while (!has_new_rcv_msg) {<br />
}<br />
has_new_rcv_msg = false;<br />
show_received_can_message((const can_receive_buf_t *)&s_can_rx_buf);</span></span></p>
<p><span style="font-family:楷体;"><span style="font-size:18px;"> can_transmit_buf_t tx_buf;<br />
memset(&tx_buf, 0, sizeof(tx_buf));<br />
tx_buf.dlc = s_can_rx_buf.dlc;<br />
tx_buf.id = 0x321;<br />
uint32_t msg_len = can_get_data_bytes_from_dlc(s_can_rx_buf.dlc);<br />
memcpy(&tx_buf.data, (uint8_t *)&s_can_rx_buf.data, msg_len);<br />
status = can_send_message_blocking(BOARD_APP_CAN_BASE, &tx_buf);<br />
if (status != status_success) {<br />
printf("CAN sent message failed, error_code:%d\n", status);<br />
return;<br />
}</span></span></p>
<p><span style="font-family:楷体;"><span style="font-size:18px;">6.测试程序,设置CAN调试器为CA2.0,波特率500k,标准模式,依次发送数据,接收均正常。</span></span></p>
<p></p>
<p> </p>
用CAN调式器测试可以通过,可以试试跟其他的开发板测评一下,这样更有说明力。 <p>CAN应用场景都有哪些?目前只用过串口,SPI,IIC等常用接口</p>
qzc飘曳 发表于 2022-8-9 11:00
CAN应用场景都有哪些?目前只用过串口,SPI,IIC等常用接口
<p>CAN在汽车电子方面用得比较多,相比串口、SPI、IIC的传输无论是距离、抗干扰等都相对较强。</p>
lugl4313820 发表于 2022-8-8 23:31
用CAN调式器测试可以通过,可以试试跟其他的开发板测评一下,这样更有说明力。
<p>我的CAN调试器就是用的STM32F103的CAN控制器,PHY用的是TJA1040</p>
qzc飘曳 发表于 2022-8-9 11:00
CAN应用场景都有哪些?目前只用过串口,SPI,IIC等常用接口
<p> CAN相对于串口最大的优势在自动仲裁,方便做多主站。串口做多主站通常需要令牌环</p>
<p>错误帧率呢?也就是给出来的时钟精确度?</p>
页:
[1]