【AT-START-F425测评】USB转CAN 预告帖 https://bbs.eeworld.com.cn/thread-1199896-1-1.html
【AT-START-F425测评】USB转CAN之二 https://bbs.eeworld.com.cn/thread-1199901-1-1.html
接上面两篇帖子
忙了一天了,终于把CAN转USB弄通了:
1、修改CAN中断接收函数:
void CAN1_IRQHandler(void)
{
__IO uint8_t can_tx_buff[5];
__IO uint32_t err_index = 0;
can_rx_message_type rx_message_struct;
if(can_flag_get(CAN1,CAN_ETR_FLAG) != RESET)
{
err_index = CAN1->ests & 0x70;
can_flag_clear(CAN1, CAN_ETR_FLAG);
if(err_index == 0x00000010)
{
can_reset(CAN1);
can_configuration();
}
}
if(can_flag_get(CAN1,CAN_RF0MN_FLAG) != RESET)
{
can_message_receive(CAN1, CAN_RX_FIFO0, &rx_message_struct);
//收到信息
sprintf(buff1,"id:%04X len:%d,data:%02x %02x %02x %02x %02x %02x %02x %02x",\
rx_message_struct.standard_id, \
rx_message_struct.dlc,\
rx_message_struct.data[0],\
rx_message_struct.data[1],\
rx_message_struct.data[2],\
rx_message_struct.data[3],\
rx_message_struct.data[4],\
rx_message_struct.data[5],\
rx_message_struct.data[6],\
rx_message_struct.data[7]);
can_rec_state = 1;
}
}
man.c主函数中增加转发函数:
while(1)
{
test_usb_uart();
if(can_rec_state ==1)
{
can_test();
can_rec_state = 0;
}
}
}
void test_usb_uart(void)
{
uint16_t data_len;
uint32_t timeout;
uint8_t buff2[] = "mytest";
data_len = usb_vcp_get_rxdata(&otg_core_struct.dev, usb_buffer);
if(data_len > 0)
{
//这里因为要写接收到的数据的处理,所以复制了一个字串符来做测试
usb_vcp_send_data(&otg_core_struct.dev, &buff2[0], sizeof(buff2));
// hw_usart_read_index = hw_usart_read_index + usart_rx_data_len;
}
}
void can_test(void)
{
//转发收到CAN的信息,不知道什么原因,这个函数放到CAN里面就会出现错误。
usb_vcp_send_data(&otg_core_struct.dev, buff1, sizeof(buff1));
}
【效果】:CAN发送的接收到,并通过USB虚拟串品发回到电脑上,很爽呀,支持921600!!
因为周未要回老家看父母,所以暂停写帖了两天,大家不要太想哦。