test_can.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-12-30 CDT first version
  9. */
  10. /*
  11. * 功能
  12. * 展示 CAN1、CAN2、CAN3 接收消息和回发消息。
  13. * 代码使用方法
  14. * 在终端执行:can_sample 参数选择:can1 | can2 | can3 以启动CAN收发测试
  15. *
  16. * 默认波特率
  17. * 仲裁段:波特率500K,采样率80%
  18. * 数据段:波特率为4M,采样率80% (仅支持CAN FD的单元)
  19. *
  20. * 接收和发送消息
  21. * CAN1:
  22. * 仅接收满足以下过滤条件的消息,并发送接收到的消息
  23. * 1)标准帧:match ID:0x100~0x1ff
  24. * 2)扩展帧:match ID:0x12345100~0x123451ff
  25. * 3)固定ID帧: match ID: 0x555
  26. * 测试设备发送满足以上过滤条件的消息后,会在终端打印接收到的ID和消息,并将消息原样发回给测试设备。
  27. *
  28. * 命令行命令
  29. * 1)设置时序: (仅支持CAN FD的单元)
  30. * 注意:使用此项设置前,需修改 MSH 最大参数格式为 20
  31. * (menuconfig-->RT-Thread Components-->MSH: command shell-->The number of arguments for a shell command)
  32. * 格式:
  33. * can set_bittiming <count> <rt_can_bit_timing_arbitration> <rt_can_bit_timing_data>
  34. * 示例:
  35. * MSH >can set_bittiming 1 1 64 16 16 0 (设置can 仲裁段波特率500K)
  36. * MSH >can set_bittiming 2 1 64 16 16 0 1 16 4 4 16 (设置can 仲裁段波特率500K,数据段波特率2M)
  37. * 2)设置仲裁段波特率:
  38. * 格式:
  39. * can set_baud <baud>
  40. * 示例:
  41. * MSH >can set_baud 1000000 (设置can仲裁段波特率1M)
  42. * 3)设置数据段波特率: (仅支持CAN FD的单元)
  43. * 格式:
  44. * can set_baudfd <baudfd>
  45. * 示例:
  46. * MSH >can set_baudfd 2000000 (设置can数据段波特率2M)
  47. * 4)发送消息:
  48. * 格式:
  49. * can send_msg
  50. * 示例:
  51. * MSH >can send_msg (触发can发送数据)
  52. */
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <rtthread.h>
  56. #include "rtdevice.h"
  57. #include "drv_can.h"
  58. #define MSH_USAGE_CAN_SAMPLE "can_sample <can1 | can2 | mcan1 | mcan2> - open can device and test\n"
  59. #define MSH_USAGE_CAN_SET_BAUD "can set_baud <baud> - set can baud\n"
  60. #define MSH_USAGE_CAN_SET_BAUDFD "can set_baudfd <baudfd> - set can baudfd\n"
  61. #define MSH_USAGE_CAN_SET_BITTIMING "can set_bittiming <count> <rt_can_bit_timing_arbitration> <rt_can_bit_timing_data> - set can bit timing,\n"
  62. #define MSH_USAGE_CAN_SEND_MSG "can send_msg \n"
  63. #define MSH_RESULT_STR(result) ((result == RT_EOK) ? "success" : "failure")
  64. static rt_device_t can_dev = RT_NULL;
  65. static struct rt_semaphore can_rx_sem;
  66. static rt_mutex_t can_mutex = RT_NULL;
  67. static rt_thread_t rx_thread;
  68. #ifdef RT_CAN_USING_CANFD
  69. static const uint8_t mcan_data_size[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64};
  70. #endif
  71. #define CAN_IF_INIT() do { \
  72. if (can_dev == RT_NULL || can_mutex == RT_NULL) { \
  73. rt_kprintf("failed! please first execute can_sample cmd!\n"); \
  74. return; \
  75. } \
  76. } while (0)
  77. static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
  78. {
  79. rt_sem_release(&can_rx_sem);
  80. return RT_EOK;
  81. }
  82. static void _set_default_filter(void)
  83. {
  84. #ifdef RT_CAN_USING_HDR
  85. struct rt_can_filter_item can_items[3] =
  86. {
  87. RT_CAN_FILTER_ITEM_INIT(0x100, RT_CAN_STDID, RT_CAN_DTR, 1, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x100~0x1ff,过滤表模式为1(0表示标识符列表模式,1表示标识符屏蔽位模式),hdr = -1(表示不指定过滤表号),设置默认过滤表,过滤表回调函数和参数均为NULL */
  88. RT_CAN_FILTER_ITEM_INIT(0x12345100, RT_CAN_EXTID, RT_CAN_DTR, 1, 0xFFFFFF00, RT_NULL, RT_NULL), /* ext,match ID:0x12345100~0x123451ff,hdr = -1 */
  89. {0x555, RT_CAN_STDID, RT_CAN_DTR, 1, 0x7ff, 7} /* std,match ID:0x555,hdr= 7,指定设置7号过滤表 */
  90. };
  91. struct rt_can_filter_config cfg = {3, 1, can_items}; /* 一共有3个过滤表,1表示初始化过滤表控制块 */
  92. rt_err_t res;
  93. res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
  94. RT_ASSERT(res == RT_EOK);
  95. #endif
  96. }
  97. static uint8_t _get_can_data_bytes_len(uint32_t dlc)
  98. {
  99. uint8_t data_bytes = 0;
  100. dlc &= 0xFU;
  101. if (dlc <= 8U)
  102. {
  103. data_bytes = dlc;
  104. }
  105. #ifdef RT_CAN_USING_CANFD
  106. data_bytes = mcan_data_size[dlc];
  107. #endif
  108. return data_bytes;
  109. }
  110. static void can_rx_thread(void *parameter)
  111. {
  112. struct rt_can_msg rxmsg = {0};
  113. rt_size_t size;
  114. uint8_t data_len;
  115. while (1)
  116. {
  117. rt_memset(&rxmsg, 0, sizeof(struct rt_can_msg));
  118. rt_sem_take(&can_rx_sem, RT_WAITING_FOREVER);
  119. rt_mutex_take(can_mutex, RT_WAITING_FOREVER);
  120. /* hdr 值为 - 1,表示直接从 uselist 链表读取数据 */
  121. rxmsg.hdr_index = -1;
  122. /* 从 CAN 读取一帧数据 */
  123. rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
  124. /* 打印数据 ID 及内容 */
  125. rt_kprintf("ID:%x Data:", rxmsg.id);
  126. data_len = _get_can_data_bytes_len(rxmsg.len);
  127. for (int i = 0; i < data_len; i++)
  128. {
  129. rt_kprintf("%2x ", rxmsg.data[i]);
  130. }
  131. rt_kprintf("\n");
  132. /* 发送接收到的消息 */
  133. size = rt_device_write(can_dev, 0, &rxmsg, sizeof(rxmsg));
  134. rt_mutex_release(can_mutex);
  135. if (size == 0)
  136. {
  137. rt_kprintf("can dev write data failed!\n");
  138. }
  139. }
  140. }
  141. static void _msh_cmd_set_baud(int argc, char **argv)
  142. {
  143. rt_err_t result;
  144. if (argc == 3)
  145. {
  146. uint32_t baud = atoi(argv[2]);
  147. CAN_IF_INIT();
  148. rt_mutex_take(can_mutex, RT_WAITING_FOREVER);
  149. result = rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD, (void *)baud);
  150. rt_mutex_release(can_mutex);
  151. rt_kprintf("set %s \n", MSH_RESULT_STR(result));
  152. }
  153. else
  154. {
  155. rt_kprintf(MSH_USAGE_CAN_SET_BAUD);
  156. rt_kprintf(" e.g. MSH >can set_baud 500000\n");
  157. }
  158. }
  159. #ifdef RT_CAN_USING_CANFD
  160. void _msh_cmd_set_timing(int argc, char **argv)
  161. {
  162. rt_err_t result;
  163. if (argc == 8 || argc == 13)
  164. {
  165. uint32_t count = atoi(argv[2]);
  166. if (count > 2)
  167. {
  168. rt_kprintf("param error: count exceed max value 2 \n");
  169. return;
  170. }
  171. struct rt_can_bit_timing items[2];
  172. struct rt_can_bit_timing_config cfg;
  173. uint32_t pos = 3;
  174. items[0].prescaler = atoi(argv[pos++]);
  175. items[0].num_seg1 = atoi(argv[pos++]);
  176. items[0].num_seg2 = atoi(argv[pos++]);
  177. items[0].num_sjw = atoi(argv[pos++]);
  178. items[0].num_sspoff = atoi(argv[pos++]);
  179. if (count > 1)
  180. {
  181. items[1].prescaler = atoi(argv[pos++]);
  182. items[1].num_seg1 = atoi(argv[pos++]);
  183. items[1].num_seg2 = atoi(argv[pos++]);
  184. items[1].num_sjw = atoi(argv[pos++]);
  185. items[1].num_sspoff = atoi(argv[pos]);
  186. }
  187. cfg.count = count;
  188. cfg.items = items;
  189. CAN_IF_INIT();
  190. rt_mutex_take(can_mutex, RT_WAITING_FOREVER);
  191. result = rt_device_control(can_dev, RT_CAN_CMD_SET_BITTIMING, &cfg);
  192. rt_mutex_release(can_mutex);
  193. rt_kprintf("set %s \n", MSH_RESULT_STR(result));
  194. }
  195. else
  196. {
  197. rt_kprintf(MSH_USAGE_CAN_SET_BITTIMING);
  198. rt_kprintf(" e.g. MSH >can set_bittiming 1 1 64 16 16 0\n");
  199. rt_kprintf(" e.g. MSH >can set_bittiming 2 1 64 16 16 0 1 16 4 4 16\n");
  200. }
  201. }
  202. void _msh_cmd_set_baudfd(int argc, char **argv)
  203. {
  204. rt_err_t result;
  205. if (argc == 3)
  206. {
  207. uint32_t baudfd = atoi(argv[2]);
  208. CAN_IF_INIT();
  209. rt_mutex_take(can_mutex, RT_WAITING_FOREVER);
  210. result = rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD_FD, (void *)baudfd);
  211. rt_mutex_release(can_mutex);
  212. rt_kprintf("set %s \n", MSH_RESULT_STR(result));
  213. }
  214. else
  215. {
  216. rt_kprintf(MSH_USAGE_CAN_SET_BAUDFD);
  217. rt_kprintf(" e.g. MSH >can set_baudfd 4000000\n");
  218. }
  219. }
  220. #endif
  221. void _msh_cmd_send_msg(int argc, char **argv)
  222. {
  223. rt_size_t size;
  224. struct rt_can_msg msg = {0};
  225. uint8_t u8Tick;
  226. if (argc == 2)
  227. {
  228. CAN_IF_INIT();
  229. rt_mutex_take(can_mutex, RT_WAITING_FOREVER);
  230. #ifdef RT_CAN_USING_CANFD
  231. msg.id = 0x300;
  232. msg.ide = RT_CAN_STDID;
  233. msg.rtr = RT_CAN_DTR;
  234. msg.len = 0xFU;
  235. msg.fd_frame = 1;
  236. msg.brs = 1;
  237. for (u8Tick = 0; u8Tick < 64; u8Tick++)
  238. {
  239. msg.data[u8Tick] = u8Tick + 1 + 0xA0;
  240. }
  241. #else
  242. msg.id = 0x300;
  243. msg.ide = RT_CAN_STDID;
  244. msg.rtr = RT_CAN_DTR;
  245. #ifdef BSP_USING_MCAN
  246. msg.len = MCAN_DLC8;
  247. #else
  248. msg.len = CAN_DLC8;
  249. #endif
  250. for (u8Tick = 0; u8Tick < 8; u8Tick++)
  251. {
  252. msg.data[u8Tick] = u8Tick + 1 + 0xA0;
  253. }
  254. #endif
  255. /* 发送一帧 CAN 数据 */
  256. size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
  257. if (size == 0)
  258. {
  259. rt_kprintf("can dev write data failed!\n");
  260. }
  261. rt_mutex_release(can_mutex);
  262. rt_kprintf("send msg ok! \n");
  263. }
  264. else
  265. {
  266. rt_kprintf(MSH_USAGE_CAN_SET_BAUD);
  267. rt_kprintf(" e.g. MSH >can send_msg \n");
  268. }
  269. }
  270. void _show_usage(void)
  271. {
  272. rt_kprintf("Usage: \n");
  273. rt_kprintf(MSH_USAGE_CAN_SET_BAUD);
  274. #ifdef RT_CAN_USING_CANFD
  275. rt_kprintf(MSH_USAGE_CAN_SET_BAUDFD);
  276. rt_kprintf(MSH_USAGE_CAN_SET_BITTIMING);
  277. #endif
  278. rt_kprintf(MSH_USAGE_CAN_SEND_MSG);
  279. }
  280. int can(int argc, char **argv)
  281. {
  282. if (!strcmp(argv[1], "set_baud"))
  283. {
  284. _msh_cmd_set_baud(argc, argv);
  285. }
  286. #ifdef RT_CAN_USING_CANFD
  287. else if (!strcmp(argv[1], "set_baudfd"))
  288. {
  289. _msh_cmd_set_baudfd(argc, argv);
  290. }
  291. else if (!strcmp(argv[1], "set_bittiming"))
  292. {
  293. _msh_cmd_set_timing(argc, argv);
  294. }
  295. #endif
  296. else if (!strcmp(argv[1], "send_msg"))
  297. {
  298. _msh_cmd_send_msg(argc, argv);
  299. }
  300. else
  301. {
  302. _show_usage();
  303. return -RT_ERROR;
  304. }
  305. return RT_EOK;
  306. }
  307. MSH_CMD_EXPORT(can, can function configuration);
  308. int can_sample(int argc, char **argv)
  309. {
  310. char can_name[RT_NAME_MAX];
  311. char sem_name[RT_NAME_MAX] = "can_sem";
  312. char mutex_name[RT_NAME_MAX] = "can_mtx";
  313. rt_err_t res;
  314. if (argc == 2)
  315. {
  316. rt_strcpy(can_name, argv[1]);
  317. /* 设备已经打开则关闭 */
  318. if (can_dev != RT_NULL)
  319. {
  320. rt_device_close(can_dev);
  321. }
  322. /* 查找设备 */
  323. can_dev = rt_device_find(can_name);
  324. if (can_dev == RT_NULL)
  325. {
  326. rt_kprintf("find %s failed!\n", can_name);
  327. return -RT_ERROR;
  328. }
  329. rt_kprintf("found %s\n", can_name);
  330. if (can_mutex == RT_NULL)
  331. {
  332. rt_sem_init(&can_rx_sem, sem_name, 0, RT_IPC_FLAG_FIFO);
  333. can_mutex = rt_mutex_create(mutex_name, RT_IPC_FLAG_FIFO);
  334. }
  335. res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
  336. RT_ASSERT(res == RT_EOK);
  337. res = rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD, (void *)CAN500kBaud);
  338. RT_ASSERT(res == RT_EOK);
  339. rt_kprintf("baud = %ld\n", CAN500kBaud);
  340. res = rt_device_control(can_dev, RT_CAN_CMD_SET_MODE, (void *)RT_CAN_MODE_NORMAL);
  341. RT_ASSERT(res == RT_EOK);
  342. #ifdef RT_CAN_USING_CANFD
  343. /* 使能CAN_FD BRS功能 */
  344. res = rt_device_control(can_dev, RT_CAN_CMD_SET_CANFD, (void *)CAN_FRAME_ISO_FD);
  345. RT_ASSERT(res == RT_EOK);
  346. res = rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD_FD, (void *)CANFD_DATA_BAUD_4M);
  347. RT_ASSERT(res == RT_EOK);
  348. rt_kprintf("baudfd = %ld\n", CANFD_DATA_BAUD_4M);
  349. #endif
  350. /* 设置接收回调函数 */
  351. rt_device_set_rx_indicate(can_dev, can_rx_call);
  352. /* 设置过滤器 */
  353. _set_default_filter();
  354. if (rx_thread == RT_NULL)
  355. {
  356. rx_thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 2048, 15, 10);
  357. if (rx_thread != RT_NULL)
  358. {
  359. rt_thread_startup(rx_thread);
  360. }
  361. else
  362. {
  363. rt_kprintf("create can_rx rx_thread failed!\n");
  364. }
  365. }
  366. return RT_EOK;
  367. }
  368. else
  369. {
  370. rt_kprintf(MSH_USAGE_CAN_SAMPLE);
  371. rt_kprintf(" e.g. MSH >can_sample can1\n");
  372. return -RT_ERROR;
  373. }
  374. }
  375. MSH_CMD_EXPORT(can_sample, can sample: select < can1 | can2 | mcan1 | mcan2 >);