【AT-START-F425 Review】USB to CAN preview post https://en.eeworld.com/bbs/thread-1199896-1-1.html
【AT-START-F425 Review】USB to CAN Part 2https://en.eeworld.com/bbs/thread-1199901-1-1.html
Continuing from the above two posts
After a busy day, I finally figured out how to convert CAN to USB:
1. Modify the CAN interrupt receiving function:
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;
}
}
Add a forwarding function to the main function of 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));
}
[Effect]: The CAN transmission is received and sent back to the computer via USB virtual serial port. It is very cool and supports 921600! !
[Next step]: Add serial port received command parsing.
Because I have to go back to my hometown to visit my parents this weekend, I have stopped posting for two days. Please don’t miss it too much.
|