can.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * File : can.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2015-05-14 aubrcool@qq.com first version
  23. * 2015-07-06 Bernard remove RT_CAN_USING_LED.
  24. */
  25. #ifndef CAN_H_
  26. #define CAN_H_
  27. #include <rtthread.h>
  28. #ifndef RT_CANMSG_BOX_SZ
  29. #define RT_CANMSG_BOX_SZ 16
  30. #endif
  31. #ifndef RT_CANSND_BOX_NUM
  32. #define RT_CANSND_BOX_NUM 1
  33. #endif
  34. enum CANBAUD
  35. {
  36. CAN1MBaud = 1000UL * 1000,/* 1 MBit/sec */
  37. CAN800kBaud = 1000UL * 800, /* 800 kBit/sec */
  38. CAN500kBaud = 1000UL * 500, /* 500 kBit/sec */
  39. CAN250kBaud = 1000UL * 250, /* 250 kBit/sec */
  40. CAN125kBaud = 1000UL * 125, /* 125 kBit/sec */
  41. CAN100kBaud = 1000UL * 100, /* 100 kBit/sec */
  42. CAN50kBaud = 1000UL * 50, /* 50 kBit/sec */
  43. CAN20kBaud = 1000UL * 20, /* 20 kBit/sec */
  44. CAN10kBaud = 1000UL * 10 /* 10 kBit/sec */
  45. };
  46. #define RT_CAN_MODE_NORMAL 0
  47. #define RT_CAN_MODE_LISEN 1
  48. #define RT_CAN_MODE_LOOPBACK 2
  49. #define RT_CAN_MODE_LOOPBACKANLISEN 3
  50. #define RT_CAN_MODE_PRIV 0x01
  51. #define RT_CAN_MODE_NOPRIV 0x00
  52. struct rt_can_filter_item
  53. {
  54. rt_uint32_t id : 29;
  55. rt_uint32_t ide : 1;
  56. rt_uint32_t rtr : 1;
  57. rt_uint32_t mode : 1;
  58. rt_uint32_t mask;
  59. rt_int32_t hdr;
  60. #ifdef RT_CAN_USING_HDR
  61. rt_err_t (*ind)(rt_device_t dev, void *args , rt_int32_t hdr, rt_size_t size);
  62. void *args;
  63. #endif /*RT_CAN_USING_HDR*/
  64. };
  65. #ifdef RT_CAN_USING_HDR
  66. #define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask,ind,args) \
  67. {(id), (ide), (rtr), (mode), (mask), -1, (ind), (args)}
  68. #define RT_CAN_FILTER_STD_INIT(id,ind,args) \
  69. RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF,ind,args)
  70. #define RT_CAN_FILTER_EXT_INIT(id,ind,args) \
  71. RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF,ind,args)
  72. #define RT_CAN_STD_RMT_FILTER_INIT(id,ind,args) \
  73. RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF,ind,args)
  74. #define RT_CAN_EXT_RMT_FILTER_INIT(id,ind,args) \
  75. RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF,ind,args)
  76. #define RT_CAN_STD_RMT_DATA_FILTER_INIT(id,ind,args) \
  77. RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF,ind,args)
  78. #define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id,ind,args) \
  79. RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF,ind,args)
  80. #else
  81. #define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask) \
  82. {(id), (ide), (rtr), (mode), (mask), -1, }
  83. #define RT_CAN_FILTER_STD_INIT(id) \
  84. RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF)
  85. #define RT_CAN_FILTER_EXT_INIT(id) \
  86. RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF)
  87. #define RT_CAN_STD_RMT_FILTER_INIT(id) \
  88. RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF)
  89. #define RT_CAN_EXT_RMT_FILTER_INIT(id) \
  90. RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF)
  91. #define RT_CAN_STD_RMT_DATA_FILTER_INIT(id) \
  92. RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF)
  93. #define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id) \
  94. RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF)
  95. #endif
  96. struct rt_can_filter_config
  97. {
  98. rt_uint32_t count;
  99. rt_uint32_t actived;
  100. struct rt_can_filter_item *items;
  101. };
  102. struct can_configure
  103. {
  104. rt_uint32_t baud_rate;
  105. rt_uint32_t msgboxsz;
  106. rt_uint32_t sndboxnumber;
  107. rt_uint32_t mode : 8;
  108. rt_uint32_t privmode : 8;
  109. rt_uint32_t reserved : 16;
  110. rt_uint32_t ticks;
  111. #ifdef RT_CAN_USING_HDR
  112. rt_uint32_t maxhdr;
  113. #endif
  114. };
  115. #define CANDEFAULTCONFIG \
  116. {\
  117. CAN1MBaud,\
  118. RT_CANMSG_BOX_SZ,\
  119. RT_CANSND_BOX_NUM,\
  120. RT_CAN_MODE_NORMAL,\
  121. };
  122. struct rt_can_ops;
  123. #define RT_CAN_CMD_SET_FILTER 0x13
  124. #define RT_CAN_CMD_SET_BAUD 0x14
  125. #define RT_CAN_CMD_SET_MODE 0x15
  126. #define RT_CAN_CMD_SET_PRIV 0x16
  127. #define RT_CAN_CMD_GET_STATUS 0x17
  128. #define RT_CAN_CMD_SET_STATUS_IND 0x18
  129. #define RT_CAN_CMD_SET_BUS_HOOK 0x19
  130. #define RT_DEVICE_CAN_INT_ERR 0x1000
  131. enum RT_CAN_STATUS_MODE
  132. {
  133. NORMAL = 0,
  134. ERRWARNING = 1,
  135. ERRPASSIVE = 2,
  136. BUSOFF = 4,
  137. };
  138. enum RT_CAN_BUS_ERR
  139. {
  140. RT_CAN_BUS_NO_ERR = 0,
  141. RT_CAN_BUS_BIT_PAD_ERR = 1,
  142. RT_CAN_BUS_FORMAT_ERR = 2,
  143. RT_CAN_BUS_ACK_ERR = 3,
  144. RT_CAN_BUS_IMPLICIT_BIT_ERR = 4,
  145. RT_CAN_BUS_EXPLICIT_BIT_ERR = 5,
  146. RT_CAN_BUS_CRC_ERR = 6,
  147. };
  148. struct rt_can_status
  149. {
  150. rt_uint32_t rcverrcnt;
  151. rt_uint32_t snderrcnt;
  152. rt_uint32_t errcode;
  153. rt_uint32_t rcvpkg;
  154. rt_uint32_t dropedrcvpkg;
  155. rt_uint32_t sndpkg;
  156. rt_uint32_t dropedsndpkg;
  157. rt_uint32_t bitpaderrcnt;
  158. rt_uint32_t formaterrcnt;
  159. rt_uint32_t ackerrcnt;
  160. rt_uint32_t biterrcnt;
  161. rt_uint32_t crcerrcnt;
  162. rt_uint32_t rcvchange;
  163. rt_uint32_t sndchange;
  164. rt_uint32_t lasterrtype;
  165. };
  166. #ifdef RT_CAN_USING_HDR
  167. struct rt_can_hdr
  168. {
  169. rt_uint32_t connected;
  170. rt_uint32_t msgs;
  171. struct rt_can_filter_item filter;
  172. struct rt_list_node list;
  173. };
  174. #endif
  175. struct rt_can_device;
  176. typedef rt_err_t (*rt_canstatus_ind)(struct rt_can_device *, void *);
  177. typedef struct rt_can_status_ind_type
  178. {
  179. rt_canstatus_ind ind;
  180. void *args;
  181. } *rt_can_status_ind_type_t;
  182. typedef void (*rt_can_bus_hook)(struct rt_can_device *);
  183. struct rt_can_device
  184. {
  185. struct rt_device parent;
  186. const struct rt_can_ops *ops;
  187. struct can_configure config;
  188. struct rt_can_status status;
  189. rt_uint32_t timerinitflag;
  190. struct rt_timer timer;
  191. struct rt_can_status_ind_type status_indicate;
  192. #ifdef RT_CAN_USING_HDR
  193. struct rt_can_hdr *hdr;
  194. #endif
  195. #ifdef RT_CAN_USING_BUS_HOOK
  196. rt_can_bus_hook bus_hook;
  197. #endif /*RT_CAN_USING_BUS_HOOK*/
  198. struct rt_mutex lock;
  199. void *can_rx;
  200. void *can_tx;
  201. };
  202. typedef struct rt_can_device *rt_can_t;
  203. #define RT_CAN_STDID 0
  204. #define RT_CAN_EXTID 1
  205. #define RT_CAN_DTR 0
  206. #define RT_CAN_RTR 1
  207. typedef struct rt_can_status *rt_can_status_t;
  208. struct rt_can_msg
  209. {
  210. rt_uint32_t id : 29;
  211. rt_uint32_t ide : 1;
  212. rt_uint32_t rtr : 1;
  213. rt_uint32_t rsv : 1;
  214. rt_uint32_t len : 8;
  215. rt_uint32_t priv : 8;
  216. rt_uint32_t hdr : 8;
  217. rt_uint32_t reserved : 8;
  218. rt_uint8_t data[8];
  219. };
  220. typedef struct rt_can_msg *rt_can_msg_t;
  221. struct rt_can_msg_list
  222. {
  223. struct rt_list_node list;
  224. #ifdef RT_CAN_USING_HDR
  225. struct rt_list_node hdrlist;
  226. struct rt_can_hdr *owner;
  227. #endif
  228. struct rt_can_msg data;
  229. };
  230. struct rt_can_rx_fifo
  231. {
  232. /* software fifo */
  233. struct rt_can_msg_list *buffer;
  234. rt_uint32_t freenumbers;
  235. struct rt_list_node freelist;
  236. struct rt_list_node uselist;
  237. };
  238. #define RT_CAN_SND_RESULT_OK 0
  239. #define RT_CAN_SND_RESULT_ERR 1
  240. #define RT_CAN_SND_RESULT_WAIT 2
  241. #define RT_CAN_EVENT_RX_IND 0x01 /* Rx indication */
  242. #define RT_CAN_EVENT_TX_DONE 0x02 /* Tx complete */
  243. #define RT_CAN_EVENT_TX_FAIL 0x03 /* Tx complete */
  244. #define RT_CAN_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
  245. #define RT_CAN_EVENT_RXOF_IND 0x06 /* Rx overflow */
  246. struct rt_can_sndbxinx_list
  247. {
  248. struct rt_list_node list;
  249. struct rt_completion completion;
  250. rt_uint32_t result;
  251. };
  252. struct rt_can_tx_fifo
  253. {
  254. struct rt_can_sndbxinx_list *buffer;
  255. struct rt_completion completion;
  256. struct rt_list_node freelist;
  257. };
  258. struct rt_can_ops
  259. {
  260. rt_err_t (*configure)(struct rt_can_device *can, struct can_configure *cfg);
  261. rt_err_t (*control)(struct rt_can_device *can, int cmd, void *arg);
  262. int (*sendmsg)(struct rt_can_device *can, const void *buf, rt_uint32_t boxno);
  263. int (*recvmsg)(struct rt_can_device *can, void *buf, rt_uint32_t boxno);
  264. };
  265. rt_err_t rt_hw_can_register(struct rt_can_device *can,
  266. const char *name,
  267. const struct rt_can_ops *ops,
  268. void *data);
  269. void rt_hw_can_isr(struct rt_can_device *can, int event);
  270. #endif /*_CAN_H*/