can.h 7.6 KB

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