1385 views|2 replies

6821

Posts

11

Resources
The OP
 

【AT-START-F425 Review】USB to CAN Part 3 [Copy link]

 

【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.

This post is from Domestic Chip Exchange

Latest reply

Going a step further, it can be made into a CAN analyzer.   Details Published on 2022-4-19 17:43
 
 

2w

Posts

74

Resources
2
 

:)

This post is from Domestic Chip Exchange
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
Personal signature

加油!在电子行业默默贡献自己的力量!:)

 
 
 

7422

Posts

2

Resources
3
 

Going a step further, it can be made into a CAN analyzer.

This post is from Domestic Chip Exchange
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list