canapp.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * File : canapp.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2015-05-14 aubrcool@qq.com first version
  13. */
  14. #include <board.h>
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #ifdef RT_USING_CAN
  18. #include "drv_lpccan.h"
  19. struct can_app_struct
  20. {
  21. const char* name;
  22. struct rt_can_filter_config * filter;
  23. rt_uint8_t eventopt;
  24. struct rt_semaphore sem;
  25. };
  26. static struct can_app_struct can_data[1];
  27. struct rt_can_filter_item filter1item[4] =
  28. {
  29. LPC_CAN_AF_STD_INIT(1),
  30. LPC_CAN_AF_STD_GRP_INIT(3,5),
  31. LPC_CAN_AF_EXT_INIT(2),
  32. LPC_CAN_AF_EXT_GRP_INIT(4,6),
  33. };
  34. struct rt_can_filter_config filter1 =
  35. {
  36. 4,
  37. 1,
  38. filter1item,
  39. };
  40. static struct can_app_struct can_data[1] = {
  41. {
  42. "lpccan1",
  43. &filter1,
  44. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  45. },
  46. };
  47. static rt_err_t lpccanind(rt_device_t dev, rt_size_t size)
  48. {
  49. rt_sem_release(&can_data[0].sem);
  50. }
  51. void rt_can_thread_entry(void* parameter)
  52. {
  53. struct rt_can_msg msg;
  54. struct can_app_struct* canpara = (struct can_app_struct*) parameter;
  55. rt_device_t candev;
  56. candev = rt_device_find(canpara->name);
  57. RT_ASSERT(candev);
  58. rt_sem_init(&canpara->sem, canpara->name, 0, RT_IPC_FLAG_FIFO);
  59. rt_device_open(candev, (RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX));
  60. rt_device_control(candev,RT_CAN_CMD_SET_FILTER,canpara->filter);
  61. rt_device_set_rx_indicate(candev, lpccanind);
  62. while(1) {
  63. rt_sem_take(&canpara->sem, RT_WAITING_FOREVER);
  64. while (rt_device_read(candev, 0, &msg, sizeof(msg)) == sizeof(msg)) {
  65. rt_device_write(candev, 0, &msg, sizeof(msg));
  66. }
  67. }
  68. }
  69. int rt_can_app_init(void)
  70. {
  71. rt_thread_t tid;
  72. tid = rt_thread_create("canapp1",
  73. rt_can_thread_entry, &can_data[0],
  74. 512, RT_THREAD_PRIORITY_MAX /3 - 1, 20);
  75. if (tid != RT_NULL) rt_thread_startup(tid);
  76. return 0;
  77. }
  78. INIT_APP_EXPORT(rt_can_app_init);
  79. #endif /*RT_USING_CAN*/