1 
2 #include "rtconfig.h"
3 #ifdef BSP_USING_CAN
4 #include <rtdevice.h>
5 #include "drv_can.h"
6 #define LOG_TAG      "can_drv"
7 #include "drv_log.h"
8 #include "fcan.h"
9 #include "fio_mux.h"
10 #include "interrupt.h"
11 #include "fcpu_info.h"
12 /*can test example*/
13 static rt_device_t can_dev;            /* CAN device handle */
14 static struct rt_semaphore rx_sem;
can_rx_call(rt_device_t dev,rt_size_t size)15 static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
16 {
17     /* The CAN generates an interrupt after receiving data, calls this callback function, and then sends the received semaphore */
18     rt_sem_release(&rx_sem);
19     return RT_EOK;
20 }
can_rx_thread(void * parameter)21 static void can_rx_thread(void *parameter)
22 {
23     int i;
24     rt_err_t res = RT_EOK;
25     struct rt_can_msg rxmsg = {0};
26     rt_device_set_rx_indicate(can_dev, can_rx_call);
27     while (1)
28     {
29         /* The hdr value is - 1, which means reading data directly from the uselist */
30         rxmsg.hdr_index = -1;
31         /* Blocking waiting to receive semaphore */
32         res = rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
33         RT_ASSERT(res == RT_EOK);
34         /* Read a frame of data from CAN */
35         rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
36         /* Print data ID and conten */
37         rt_kprintf("ID:%x\n", rxmsg.id);
38         rt_kprintf("DATA: ");
39         for (i = 0; i < 8; i++)
40         {
41             rt_kprintf("%2x ", rxmsg.data[i]);
42         }
43 
44         rt_kprintf("\n");
45     }
46 }
47 
can_sample(int argc,char * argv[])48 int can_sample(int argc, char *argv[])
49 {
50     struct rt_can_msg msg = {0};
51     rt_err_t res = RT_EOK;;
52     rt_thread_t thread;
53     char can_name[RT_NAME_MAX];
54 
55     if (argc == 2)
56     {
57         rt_strncpy(can_name, argv[1], RT_NAME_MAX);
58     }
59     else
60     {
61         rt_strncpy(can_name, "CAN0", RT_NAME_MAX);
62     }
63     /* Find CAN device */
64     can_dev = rt_device_find(can_name);
65     if (!can_dev)
66     {
67         rt_kprintf("Find %s failed.\n", can_name);
68         return -RT_ERROR;
69     }
70 
71     /* Initialize CAN receive signal quantity */
72     res = rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
73     RT_ASSERT(res == RT_EOK);
74 
75     /* Open the CAN device in the way of interrupt reception and transmission */
76     res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
77     rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD, CAN1MBaud);
78     RT_ASSERT(res == RT_EOK);
79 
80 #ifdef RT_CAN_USING_HDR
81     struct rt_can_filter_item items[4] =
82     {
83         RT_CAN_FILTER_ITEM_INIT(0x3, 0, 0, 0, 0, RT_NULL, RT_NULL),
84         RT_CAN_FILTER_ITEM_INIT(0x3, 0, 0, 0, 0, RT_NULL, RT_NULL),
85         RT_CAN_FILTER_ITEM_INIT(0x3, 0, 0, 0, 0, RT_NULL, RT_NULL),
86         RT_CAN_FILTER_ITEM_INIT(0x3, 0, 0, 0, 0, RT_NULL, RT_NULL)
87 
88     };
89     struct rt_can_filter_config cfg = {4, 1, items}; /* There are 4 filter tables in total */
90 
91     /* Set the hardware filter table. After setting, only frames with id=0x03 can be received*/
92     res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
93     RT_ASSERT(res == RT_EOK);
94 #endif
95 
96     /* Create data receiving thread */
97     thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 4096, 25, 10);
98     if (thread != RT_NULL)
99     {
100         res = rt_thread_startup(thread);
101         RT_ASSERT(res == RT_EOK);
102     }
103     else
104     {
105         rt_kprintf("Create can_rx thread failed.\n");
106     }
107 
108     msg.id = 0x78;              /* ID = 0x78 */
109     msg.ide = RT_CAN_STDID;     /* Standard format */
110     msg.rtr = RT_CAN_DTR;       /* Data frame */
111     msg.len = 8;                /* Data length is 8 */
112     /* Send CAN data */
113     for (int i = 0; i < 1; i++)
114     {
115         /* 8-byte data to be sent */
116         msg.data[0] = 0x00 + i;
117         msg.data[1] = 0x11 + i;
118         msg.data[2] = 0x22 + i;
119         msg.data[3] = 0x33 + i;
120         msg.data[4] = 0x44 + i;
121         msg.data[5] = 0x55 + i;
122         msg.data[6] = 0x66 + i;
123         msg.data[7] = 0x77 + i;
124         rt_device_write(can_dev, 0, &msg, sizeof(msg));
125     }
126 
127     return res;
128 }
129 /* Enter can_sample command for testing */
130 MSH_CMD_EXPORT(can_sample, can device sample);
131 #endif