1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
#include "rtconfig.h"
#ifdef BSP_USING_CAN
#include <rtdevice.h>
#include "drv_can.h"
#define LOG_TAG "can_drv"
#include "drv_log.h"
#include "fcan.h"
#include "fio_mux.h"
#include "interrupt.h"
#include "fcpu_info.h"
/*can test example*/
static rt_device_t can0_dev; /* CAN device handle */
static rt_device_t can1_dev; /* CAN device handle */
static struct rt_semaphore can0_rx_sem;
static struct rt_semaphore can1_rx_sem;
static struct rt_can_msg rxmsg = {0};
static rt_err_t can0_rx_call(rt_device_t dev, rt_size_t size)
{
/* The CAN generates an interrupt after receiving data, calls this callback function, and then sends the received semaphore */
rt_sem_release(&can0_rx_sem);
return RT_EOK;
}
static void can0_rx_thread(void *parameter)
{
int i;
rt_err_t res = RT_EOK;
rt_device_set_rx_indicate(can0_dev, can0_rx_call);
while (1)
{
/* The hdr value is - 1, which means reading data directly from the uselist */
rxmsg.hdr_index = -1;
/* Blocking waiting to receive semaphore */
res = rt_sem_take(&can0_rx_sem, RT_WAITING_FOREVER);
RT_ASSERT(res == RT_EOK);
/* Read a frame of data from CAN */
rt_device_read(can0_dev, 0, &rxmsg, sizeof(rxmsg));
/* Print data ID and conten */
LOG_D("ID:%x\n", rxmsg.id);
LOG_D("DATA: ");
for (i = 0; i < 8; i++)
{
LOG_D("%2x ", rxmsg.data[i]);
}
LOG_D("\n");
}
}
static rt_err_t can1_rx_call(rt_device_t dev, rt_size_t size)
{
/* The CAN generates an interrupt after receiving data, calls this callback function, and then sends the received semaphore */
rt_sem_release(&can1_rx_sem);
return RT_EOK;
}
static void can1_rx_thread(void *parameter)
{
int i;
rt_err_t res = RT_EOK;
rt_device_set_rx_indicate(can1_dev, can1_rx_call);
while (1)
{
/* The hdr value is - 1, which means reading data directly from the uselist */
rxmsg.hdr_index = -1;
/* Blocking waiting to receive semaphore */
res = rt_sem_take(&can1_rx_sem, RT_WAITING_FOREVER);
RT_ASSERT(res == RT_EOK);
/* Read a frame of data from CAN */
rt_device_read(can1_dev, 0, &rxmsg, sizeof(rxmsg));
/* Print data ID and conten */
LOG_D("ID:%x\n", rxmsg.id);
LOG_D("DATA: ");
for (i = 0; i < 8; i++)
{
LOG_D("%2x ", rxmsg.data[i]);
}
LOG_D("\n");
}
}
rt_err_t can_loopback_sample()
{
struct rt_can_msg msg = {0};
rt_err_t res = RT_EOK;;
rt_thread_t thread;
/* Find CAN device */
can0_dev = rt_device_find("CAN0");
if (!can0_dev)
{
rt_kprintf("Find CAN0 failed.\n");
return -RT_ERROR;
}
/* Find CAN device */
can1_dev = rt_device_find("CAN1");
if (!can1_dev)
{
rt_kprintf("Find CAN1 failed.\n");
return -RT_ERROR;
}
/* Initialize CAN receive signal quantity */
res = rt_sem_init(&can0_rx_sem, "can0_rx_sem", 0, RT_IPC_FLAG_FIFO);
RT_ASSERT(res == RT_EOK);
res = rt_sem_init(&can1_rx_sem, "can1_rx_sem", 0, RT_IPC_FLAG_FIFO);
RT_ASSERT(res == RT_EOK);
/* Open the CAN device in the way of interrupt reception and transmission */
res = rt_device_open(can0_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
rt_device_control(can0_dev, RT_CAN_CMD_SET_BAUD, CAN800kBaud);
RT_ASSERT(res == RT_EOK);
res = rt_device_open(can1_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
rt_device_control(can1_dev, RT_CAN_CMD_SET_BAUD, CAN800kBaud);
RT_ASSERT(res == RT_EOK);
/* Create data receiving thread */
thread = rt_thread_create("can0_rx", can0_rx_thread, RT_NULL, 4096, 10, 10);
if (thread != RT_NULL)
{
res = rt_thread_startup(thread);
RT_ASSERT(res == RT_EOK);
}
else
{
rt_kprintf("Create can0_rx thread failed.\n");
}
thread = rt_thread_create("can1_rx", can1_rx_thread, RT_NULL, 4096, 10, 10);
if (thread != RT_NULL)
{
res = rt_thread_startup(thread);
RT_ASSERT(res == RT_EOK);
}
else
{
rt_kprintf("Create can1_rx thread failed.\n");
}
msg.id = 0x78; /* ID = 0x78 */
msg.ide = RT_CAN_STDID; /* Standard format */
msg.rtr = RT_CAN_DTR; /* Data frame */
msg.len = 8; /* Data length is 8 */
/* Send CAN data */
for (int i = 0; i < 5; i++)
{
/* 8-byte data to be sent */
msg.data[0] = 0x0;
msg.data[1] = 0x1;
msg.data[2] = 0x2;
msg.data[3] = 0x3;
msg.data[4] = 0x4;
msg.data[5] = 0x5;
msg.data[6] = 0x6;
msg.data[7] = 0x7;
rt_device_write(can0_dev, 0, &msg, sizeof(msg));
rt_thread_mdelay(100);
for (int i = 0; i < 8; i++)
{
if (msg.data[i] != rxmsg.data[i])
{
res = RT_ERROR;
goto exit;
}
}
}
/* Send CAN data */
for (int i = 0; i < 5; i++)
{
/* 8-byte data to be sent */
msg.data[0] = 0x0;
msg.data[1] = 0x1;
msg.data[2] = 0x2;
msg.data[3] = 0x3;
msg.data[4] = 0x4;
msg.data[5] = 0x5;
msg.data[6] = 0x6;
msg.data[7] = 0x7;
rt_device_write(can1_dev, 0, &msg, sizeof(msg));
rt_thread_mdelay(100);
for (int i = 0; i < 8; i++)
{
if (msg.data[i] != rxmsg.data[i])
{
res = RT_ERROR;
goto exit;
}
}
}
exit:
/* print message on example run result */
if (res == RT_EOK)
{
rt_kprintf("%s@%d:Can loopback test example [success].\r\n", __func__, __LINE__);
}
else
{
rt_kprintf("%s@%d:Can loopback test example [failure], res = %d\r\n", __func__, __LINE__, res);
}
return res;
}
/* Enter can_sample command for testing */
MSH_CMD_EXPORT(can_loopback_sample, can device sample);
#endif
|