canapp.c 7.1 KB

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