MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / can_sample

Function can_sample

bsp/hc32/tests/test_can.c:335–407  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

333MSH_CMD_EXPORT(can, can function configuration);
334
335int can_sample(int argc, char **argv)
336{
337 char can_name[RT_NAME_MAX];
338 char sem_name[RT_NAME_MAX] = "can_sem";
339 char mutex_name[RT_NAME_MAX] = "can_mtx";
340 rt_err_t res;
341
342 if (argc == 2)
343 {
344 rt_strcpy(can_name, argv[1]);
345 /* 设备已经打开则关闭 */
346 if (can_dev != RT_NULL)
347 {
348 rt_device_close(can_dev);
349 }
350 /* 查找设备 */
351 can_dev = rt_device_find(can_name);
352 if (can_dev == RT_NULL)
353 {
354 rt_kprintf("find %s failed!\n", can_name);
355 return -RT_ERROR;
356 }
357 rt_kprintf("found %s\n", can_name);
358
359 if (can_mutex == RT_NULL)
360 {
361 rt_sem_init(&can_rx_sem, sem_name, 0, RT_IPC_FLAG_FIFO);
362 can_mutex = rt_mutex_create(mutex_name, RT_IPC_FLAG_FIFO);
363 }
364
365 res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
366 RT_ASSERT(res == RT_EOK);
367 res = rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD, (void *)CAN500kBaud);
368 RT_ASSERT(res == RT_EOK);
369 rt_kprintf("baud = %ld\n", CAN500kBaud);
370 res = rt_device_control(can_dev, RT_CAN_CMD_SET_MODE, (void *)RT_CAN_MODE_NORMAL);
371 RT_ASSERT(res == RT_EOK);
372
373#ifdef RT_CAN_USING_CANFD
374 /* 使能CAN_FD BRS功能 */
375 res = rt_device_control(can_dev, RT_CAN_CMD_SET_CANFD, (void *)CAN_FRAME_ISO_FD);
376 RT_ASSERT(res == RT_EOK);
377 res = rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD_FD, (void *)CANFD_DATA_BAUD_4M);
378 RT_ASSERT(res == RT_EOK);
379 rt_kprintf("baudfd = %ld\n", CANFD_DATA_BAUD_4M);
380#endif
381 /* 设置接收回调函数 */
382 rt_device_set_rx_indicate(can_dev, can_rx_call);
383 /* 设置过滤器 */
384 _set_default_filter();
385
386 if (rx_thread == RT_NULL)
387 {
388 rx_thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 2048, 15, 10);
389 if (rx_thread != RT_NULL)
390 {
391 rt_thread_startup(rx_thread);
392 }

Callers

nothing calls this directly

Calls 12

rt_strcpyFunction · 0.85
rt_device_closeFunction · 0.85
rt_device_findFunction · 0.85
rt_kprintfFunction · 0.85
rt_sem_initFunction · 0.85
rt_mutex_createFunction · 0.85
rt_device_openFunction · 0.85
rt_device_controlFunction · 0.85
_set_default_filterFunction · 0.85
rt_thread_createFunction · 0.85
rt_thread_startupFunction · 0.85

Tested by

no test coverage detected