can_sample.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-06-25 misonyo first implementation.
  9. */
  10. /*
  11. * 程序清单:这是一个 CAN 设备使用例程
  12. * 例程导出了 can_sample 命令到控制终端
  13. * 命令调用格式:can_sample can2
  14. * 命令解释:命令第二个参数是要使用的 CAN 设备名称,为空则使用默认的 CAN 设备
  15. * 程序功能:通过 CAN 设备发送一帧,并创建一个线程接收数据然后打印输出。
  16. */
  17. #include <rtthread.h>
  18. #include "rtdevice.h"
  19. #define CAN_DEV_NAME "can2" /* CAN 设备名称 */
  20. static struct rt_semaphore rx_sem; /* 用于接收消息的信号量 */
  21. static rt_device_t can_dev; /* CAN 设备句柄 */
  22. /* 接收数据回调函数 */
  23. static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
  24. {
  25. /* CAN 接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
  26. rt_sem_release(&rx_sem);
  27. return RT_EOK;
  28. }
  29. static void can_rx_thread(void *parameter)
  30. {
  31. int i;
  32. struct rt_can_msg rxmsg = {0};
  33. /* 设置接收回调函数 */
  34. rt_device_set_rx_indicate(can_dev, can_rx_call);
  35. #ifdef RT_CAN_USING_HDR
  36. rt_err_t res;
  37. struct rt_can_filter_item items[5] =
  38. {
  39. RT_CAN_FILTER_ITEM_INIT(0x100, 0, 0, 0, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x100~0x1ff,hdr为-1,设置默认过滤表 */
  40. RT_CAN_FILTER_ITEM_INIT(0x300, 0, 0, 0, 0x700, RT_NULL, RT_NULL), /* std,match ID:0x300~0x3ff,hdr为-1 */
  41. RT_CAN_FILTER_ITEM_INIT(0x211, 0, 0, 0, 0x7ff, RT_NULL, RT_NULL), /* std,match ID:0x211,hdr为-1 */
  42. RT_CAN_FILTER_STD_INIT(0x486, RT_NULL, RT_NULL), /* std,match ID:0x486,hdr为-1 */
  43. {0x555, 0, 0, 0, 0x7ff, 7,} /* std,match ID:0x555,hdr为7,指定设置7号过滤表 */
  44. };
  45. struct rt_can_filter_config cfg = {5, 1, items}; /* 一共有5个过滤表 */
  46. /* 设置硬件过滤表 */
  47. res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
  48. RT_ASSERT(res == RT_EOK);
  49. #endif
  50. while (1)
  51. {
  52. /* hdr值为-1,表示直接从uselist链表读取数据 */
  53. rxmsg.hdr = -1;
  54. /* 阻塞等待接收信号量 */
  55. rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
  56. /* 从CAN读取一帧数据 */
  57. rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
  58. /* 打印数据ID及内容 */
  59. rt_kprintf("ID:%x ", rxmsg.id);
  60. for (i = 0; i < 8; i++)
  61. {
  62. rt_kprintf("%2x ", rxmsg.data[i]);
  63. }
  64. rt_kprintf("\n");
  65. }
  66. }
  67. int can_sample(int argc, char *argv[])
  68. {
  69. struct rt_can_msg msg = {0};
  70. rt_err_t res;
  71. rt_size_t size;
  72. rt_thread_t thread;
  73. char can_name[RT_NAME_MAX];
  74. if (argc == 2)
  75. {
  76. rt_strncpy(can_name, argv[1], RT_NAME_MAX);
  77. }
  78. else
  79. {
  80. rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
  81. }
  82. can_dev = rt_device_find(can_name);
  83. if (!can_dev)
  84. {
  85. rt_kprintf("find %s failed!\n", can_name);
  86. return RT_ERROR;
  87. }
  88. /* 初始化CAN接收信号量 */
  89. rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
  90. /* 以中断接收及发送方式打开CAN设备 */
  91. res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
  92. RT_ASSERT(res == RT_EOK);
  93. thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 1024, 25, 10);
  94. if (thread != RT_NULL)
  95. {
  96. rt_thread_startup(thread);
  97. }
  98. else
  99. {
  100. rt_kprintf("create can_rx thread failed!\n");
  101. }
  102. msg.id = 0x78; /* ID为0x78 */
  103. msg.ide = RT_CAN_STDID; /* 标准格式 */
  104. msg.rtr = RT_CAN_DTR; /* 数据帧 */
  105. msg.len = 8; /* 数据长度为8 */
  106. /* 待发送的8字节数据 */
  107. msg.data[0] = 0x00;
  108. msg.data[1] = 0x11;
  109. msg.data[2] = 0x22;
  110. msg.data[3] = 0x33;
  111. msg.data[4] = 0x44;
  112. msg.data[5] = 0x55;
  113. msg.data[6] = 0x66;
  114. msg.data[7] = 0x77;
  115. /* 发送一帧CAN数据 */
  116. size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
  117. if (size == 0)
  118. {
  119. rt_kprintf("can dev write data failed!\n");
  120. }
  121. return res;
  122. }
  123. /* 导出到 msh 命令列表中 */
  124. MSH_CMD_EXPORT(can_sample, can device sample);