can_test.c 4.4 KB

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