canapp.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #define CANRT1 8
  19. #define CANERR1 9
  20. #define CANRT2 37
  21. #define CANERR2 38
  22. static struct canledtype
  23. {
  24. struct stm32_hw_pin_userdata rtd;
  25. struct stm32_hw_pin_userdata err;
  26. } canled[] =
  27. {
  28. #ifdef USING_BXCAN1
  29. {
  30. {CANRT1, PIN_MODE_OUTPUT_OD,},
  31. {CANERR1, PIN_MODE_OUTPUT_OD,},
  32. },
  33. #endif /*USING_BXCAN1*/
  34. #ifdef USING_BXCAN2
  35. {
  36. {CANRT2, PIN_MODE_OUTPUT_OD,},
  37. {CANERR2, PIN_MODE_OUTPUT_OD,},
  38. },
  39. #endif /*USING_BXCAN2*/
  40. };
  41. void can_bus_hook(struct rt_can_device *can, struct canledtype *led)
  42. {
  43. if (can->timerinitflag == 1)
  44. {
  45. rt_pin_write(led->rtd.pin, 0);
  46. }
  47. else
  48. {
  49. if (can->status.rcvchange == 1 || can->status.sndchange == 1)
  50. {
  51. can->status.rcvchange = 0;
  52. can->status.sndchange = 0;
  53. rt_pin_write(led->rtd.pin, rt_pin_read(led->rtd.pin) ? 0 : 1);
  54. }
  55. else
  56. {
  57. rt_pin_write(led->rtd.pin, 1);
  58. }
  59. }
  60. if (can->timerinitflag == 1)
  61. {
  62. rt_pin_write(led->err.pin, 0);
  63. }
  64. else
  65. {
  66. if (can->status.errcode)
  67. {
  68. rt_pin_write(led->err.pin, 0);
  69. }
  70. else
  71. {
  72. rt_pin_write(led->err.pin, 1);
  73. }
  74. }
  75. }
  76. #ifdef USING_BXCAN1
  77. void can1_bus_hook(struct rt_can_device *can)
  78. {
  79. static rt_int32_t inited = 0;
  80. if (!inited)
  81. {
  82. inited = 1;
  83. rt_pin_mode(canled[0].rtd.pin, canled[0].rtd.mode);
  84. rt_pin_mode(canled[0].err.pin, canled[0].err.mode);
  85. }
  86. can_bus_hook(can, &canled[0]);
  87. }
  88. #endif /*USING_BXCAN1*/
  89. #ifdef USING_BXCAN2
  90. void can2_bus_hook(struct rt_can_device *can)
  91. {
  92. static rt_int32_t inited = 0;
  93. if (!inited)
  94. {
  95. inited = 1;
  96. rt_pin_mode(canled[1].rtd.pin, canled[1].rtd.mode);
  97. rt_pin_mode(canled[1].err.pin, canled[1].err.mode);
  98. }
  99. can_bus_hook(can, &canled[1]);
  100. }
  101. #endif /*USING_BXCAN2*/
  102. int can_bus_hook_init(void)
  103. {
  104. rt_device_t candev;
  105. #ifdef USING_BXCAN1
  106. candev = rt_device_find("bxcan1");
  107. RT_ASSERT(candev);
  108. rt_device_control(candev, RT_CAN_CMD_SET_BUS_HOOK, (void *)can1_bus_hook);
  109. #endif /*USING_BXCAN1*/
  110. #ifdef USING_BXCAN2
  111. candev = rt_device_find("bxcan2");
  112. RT_ASSERT(candev);
  113. rt_device_control(candev, RT_CAN_CMD_SET_BUS_HOOK, (void *)can2_bus_hook);
  114. #endif /*USING_BXCAN2*/
  115. return RT_EOK;
  116. }
  117. INIT_DEVICE_EXPORT(can_bus_hook_init);
  118. struct can_app_struct
  119. {
  120. const char *name;
  121. struct rt_event event;
  122. struct rt_can_filter_config *filter;
  123. rt_uint8_t eventopt;
  124. };
  125. static struct can_app_struct can_data[2];
  126. static rt_err_t can1ind(rt_device_t dev, void *args, rt_int32_t hdr, rt_size_t size)
  127. {
  128. rt_event_t pevent = (rt_event_t)args;
  129. rt_event_send(pevent, 1 << (hdr));
  130. return RT_EOK;
  131. }
  132. static rt_err_t can2ind(rt_device_t dev, void *args, rt_int32_t hdr, rt_size_t size)
  133. {
  134. rt_event_t pevent = (rt_event_t)args;
  135. rt_event_send(pevent, 1 << (hdr));
  136. return RT_EOK;
  137. }
  138. struct rt_can_filter_item filter1item[4] =
  139. {
  140. RT_CAN_FILTER_STD_INIT(1, can1ind, &can_data[0].event),
  141. RT_CAN_FILTER_STD_INIT(2, can1ind, &can_data[0].event),
  142. RT_CAN_STD_RMT_FILTER_INIT(3, can1ind, &can_data[0].event),
  143. RT_CAN_STD_RMT_DATA_FILTER_INIT(4, can1ind, &can_data[0].event),
  144. };
  145. struct rt_can_filter_item filter2item[4] =
  146. {
  147. RT_CAN_FILTER_STD_INIT(1, can2ind, &can_data[1].event),
  148. RT_CAN_FILTER_STD_INIT(2, can2ind, &can_data[1].event),
  149. RT_CAN_STD_RMT_FILTER_INIT(3, can2ind, &can_data[1].event),
  150. RT_CAN_STD_RMT_DATA_FILTER_INIT(4, can2ind, &can_data[1].event),
  151. };
  152. struct rt_can_filter_config filter1 =
  153. {
  154. .count = 4,
  155. .actived = 1,
  156. .items = filter1item,
  157. };
  158. struct rt_can_filter_config filter2 =
  159. {
  160. .count = 4,
  161. .actived = 1,
  162. .items = filter2item,
  163. };
  164. static struct can_app_struct can_data[2] =
  165. {
  166. {
  167. .name = "bxcan1",
  168. .filter = &filter1,
  169. .eventopt = RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  170. },
  171. {
  172. .name = "bxcan2",
  173. .filter = &filter2,
  174. .eventopt = RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
  175. },
  176. };
  177. void rt_can_thread_entry(void *parameter)
  178. {
  179. struct rt_can_msg msg;
  180. struct can_app_struct *canpara = (struct can_app_struct *) parameter;
  181. rt_device_t candev;
  182. rt_uint32_t e;
  183. candev = rt_device_find(canpara->name);
  184. RT_ASSERT(candev);
  185. rt_event_init(&canpara->event, canpara->name, RT_IPC_FLAG_FIFO);
  186. rt_device_open(candev, (RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX));
  187. rt_device_control(candev, RT_CAN_CMD_SET_FILTER, canpara->filter);
  188. while (1)
  189. {
  190. if (
  191. rt_event_recv(&canpara->event,
  192. ((1 << canpara->filter->items[0].hdr) |
  193. (1 << canpara->filter->items[1].hdr) |
  194. (1 << canpara->filter->items[2].hdr) |
  195. (1 << canpara->filter->items[3].hdr)),
  196. canpara->eventopt,
  197. RT_WAITING_FOREVER, &e) != RT_EOK
  198. )
  199. {
  200. continue;
  201. }
  202. if (e & (1 << canpara->filter->items[0].hdr))
  203. {
  204. msg.hdr = canpara->filter->items[0].hdr;
  205. while (rt_device_read(candev, 0, &msg, sizeof(msg)) == sizeof(msg))
  206. {
  207. rt_device_write(candev, 0, &msg, sizeof(msg));
  208. }
  209. }
  210. if (e & (1 << canpara->filter->items[1].hdr))
  211. {
  212. msg.hdr = canpara->filter->items[1].hdr;
  213. while (rt_device_read(candev, 0, &msg, sizeof(msg)) == sizeof(msg))
  214. {
  215. rt_device_write(candev, 0, &msg, sizeof(msg));
  216. }
  217. }
  218. if (e & (1 << canpara->filter->items[2].hdr))
  219. {
  220. msg.hdr = canpara->filter->items[2].hdr;
  221. while (rt_device_read(candev, 0, &msg, sizeof(msg)) == sizeof(msg))
  222. {
  223. rt_device_write(candev, 0, &msg, sizeof(msg));
  224. }
  225. }
  226. if (e & (1 << canpara->filter->items[3].hdr))
  227. {
  228. msg.hdr = canpara->filter->items[3].hdr;
  229. while (rt_device_read(candev, 0, &msg, sizeof(msg)) == sizeof(msg))
  230. {
  231. rt_device_write(candev, 0, &msg, sizeof(msg));
  232. }
  233. }
  234. }
  235. }
  236. int rt_can_app_init(void)
  237. {
  238. rt_thread_t tid;
  239. tid = rt_thread_create("canapp1",
  240. rt_can_thread_entry, &can_data[0],
  241. 512, RT_THREAD_PRIORITY_MAX / 3 - 1, 20);
  242. if (tid != RT_NULL) rt_thread_startup(tid);
  243. tid = rt_thread_create("canapp2",
  244. rt_can_thread_entry, &can_data[1],
  245. 512, RT_THREAD_PRIORITY_MAX / 3 - 1, 20);
  246. if (tid != RT_NULL) rt_thread_startup(tid);
  247. return 0;
  248. }
  249. INIT_APP_EXPORT(rt_can_app_init);
  250. #endif /*RT_USING_CAN*/