can.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 2006-2022, 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. * 2022-05-08 hpmicro add CANFD support, fixed typos
  11. */
  12. #ifndef CAN_H_
  13. #define CAN_H_
  14. #include <rtthread.h>
  15. #ifndef RT_CANMSG_BOX_SZ
  16. #define RT_CANMSG_BOX_SZ 16
  17. #endif
  18. #ifndef RT_CANSND_BOX_NUM
  19. #define RT_CANSND_BOX_NUM 1
  20. #endif
  21. enum CAN_DLC
  22. {
  23. CAN_MSG_0BYTE = 0,
  24. CAN_MSG_1BYTE,
  25. CAN_MSG_2BYTES,
  26. CAN_MSG_3BYTES,
  27. CAN_MSG_4BYTES,
  28. CAN_MSG_5BYTES,
  29. CAN_MSG_6BYTES,
  30. CAN_MSG_7BYTES,
  31. CAN_MSG_8BYTES,
  32. CAN_MSG_12BYTES,
  33. CAN_MSG_16BYTES,
  34. CAN_MSG_20BYTES,
  35. CAN_MSG_24BYTES,
  36. CAN_MSG_32BYTES,
  37. CAN_MSG_48BYTES,
  38. CAN_MSG_64BYTES,
  39. };
  40. enum CANBAUD
  41. {
  42. CAN1MBaud = 1000UL * 1000,/* 1 MBit/sec */
  43. CAN800kBaud = 1000UL * 800, /* 800 kBit/sec */
  44. CAN500kBaud = 1000UL * 500, /* 500 kBit/sec */
  45. CAN250kBaud = 1000UL * 250, /* 250 kBit/sec */
  46. CAN125kBaud = 1000UL * 125, /* 125 kBit/sec */
  47. CAN100kBaud = 1000UL * 100, /* 100 kBit/sec */
  48. CAN50kBaud = 1000UL * 50, /* 50 kBit/sec */
  49. CAN20kBaud = 1000UL * 20, /* 20 kBit/sec */
  50. CAN10kBaud = 1000UL * 10 /* 10 kBit/sec */
  51. };
  52. #define RT_CAN_MODE_NORMAL 0
  53. #define RT_CAN_MODE_LISTEN 1
  54. #define RT_CAN_MODE_LOOPBACK 2
  55. #define RT_CAN_MODE_LOOPBACKANLISTEN 3
  56. #define RT_CAN_MODE_PRIV 0x01
  57. #define RT_CAN_MODE_NOPRIV 0x00
  58. struct rt_can_filter_item
  59. {
  60. rt_uint32_t id : 29;
  61. rt_uint32_t ide : 1;
  62. rt_uint32_t rtr : 1;
  63. rt_uint32_t mode : 1;
  64. rt_uint32_t mask;
  65. rt_int32_t hdr;
  66. #ifdef RT_CAN_USING_HDR
  67. rt_err_t (*ind)(rt_device_t dev, void *args , rt_int32_t hdr, rt_size_t size);
  68. void *args;
  69. #endif /*RT_CAN_USING_HDR*/
  70. };
  71. #ifdef RT_CAN_USING_HDR
  72. #define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask,ind,args) \
  73. {(id), (ide), (rtr), (mode), (mask), -1, (ind), (args)}
  74. #define RT_CAN_FILTER_STD_INIT(id,ind,args) \
  75. RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF,ind,args)
  76. #define RT_CAN_FILTER_EXT_INIT(id,ind,args) \
  77. RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF,ind,args)
  78. #define RT_CAN_STD_RMT_FILTER_INIT(id,ind,args) \
  79. RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF,ind,args)
  80. #define RT_CAN_EXT_RMT_FILTER_INIT(id,ind,args) \
  81. RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF,ind,args)
  82. #define RT_CAN_STD_RMT_DATA_FILTER_INIT(id,ind,args) \
  83. RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF,ind,args)
  84. #define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id,ind,args) \
  85. RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF,ind,args)
  86. #else
  87. #define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask) \
  88. {(id), (ide), (rtr), (mode), (mask), -1, }
  89. #define RT_CAN_FILTER_STD_INIT(id) \
  90. RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF)
  91. #define RT_CAN_FILTER_EXT_INIT(id) \
  92. RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF)
  93. #define RT_CAN_STD_RMT_FILTER_INIT(id) \
  94. RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF)
  95. #define RT_CAN_EXT_RMT_FILTER_INIT(id) \
  96. RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF)
  97. #define RT_CAN_STD_RMT_DATA_FILTER_INIT(id) \
  98. RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF)
  99. #define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id) \
  100. RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF)
  101. #endif
  102. struct rt_can_filter_config
  103. {
  104. rt_uint32_t count;
  105. rt_uint32_t actived;
  106. struct rt_can_filter_item *items;
  107. };
  108. struct rt_can_bit_timing
  109. {
  110. rt_uint16_t prescaler; /* Pre-scaler */
  111. rt_uint16_t num_seg1; /* Bit Timing Segment 1, in terms of Tq */
  112. rt_uint16_t num_seg2; /* Bit Timing Segment 2, in terms of Tq */
  113. rt_uint8_t num_sjw; /* Synchronization Jump Width, in terms of Tq */
  114. rt_uint8_t num_sspoff; /* Secondary Sample Point Offset, in terms of Tq */
  115. };
  116. /**
  117. * CAN bit timing configuration list
  118. * NOTE:
  119. * items[0] always for CAN2.0/CANFD Arbitration Phase
  120. * items[1] always for CANFD (if it exists)
  121. */
  122. struct rt_can_bit_timing_config
  123. {
  124. rt_uint32_t count;
  125. struct rt_can_bit_timing *items;
  126. };
  127. struct can_configure
  128. {
  129. rt_uint32_t baud_rate;
  130. rt_uint32_t msgboxsz;
  131. rt_uint32_t sndboxnumber;
  132. rt_uint32_t mode : 8;
  133. rt_uint32_t privmode : 8;
  134. rt_uint32_t reserved : 16;
  135. rt_uint32_t ticks;
  136. #ifdef RT_CAN_USING_HDR
  137. rt_uint32_t maxhdr;
  138. #endif
  139. #ifdef RT_CAN_USING_CANFD
  140. rt_uint32_t baud_rate_fd; /* CANFD data bit rate*/
  141. rt_uint32_t use_bit_timing: 8; /* Use the bit timing for CAN timing configuration */
  142. rt_uint32_t enable_canfd : 8; /* Enable CAN-FD mode */
  143. rt_uint32_t reserved1 : 16;
  144. /* The below fields take effect only if use_bit_timing is non-zero */
  145. struct rt_can_bit_timing can_timing; /* CAN bit-timing /CANFD bit-timing for arbitration phase */
  146. struct rt_can_bit_timing canfd_timing; /* CANFD bit-timing for datat phase */
  147. #endif
  148. };
  149. #define CANDEFAULTCONFIG \
  150. {\
  151. CAN1MBaud,\
  152. RT_CANMSG_BOX_SZ,\
  153. RT_CANSND_BOX_NUM,\
  154. RT_CAN_MODE_NORMAL,\
  155. };
  156. struct rt_can_ops;
  157. #define RT_CAN_CMD_SET_FILTER 0x13
  158. #define RT_CAN_CMD_SET_BAUD 0x14
  159. #define RT_CAN_CMD_SET_MODE 0x15
  160. #define RT_CAN_CMD_SET_PRIV 0x16
  161. #define RT_CAN_CMD_GET_STATUS 0x17
  162. #define RT_CAN_CMD_SET_STATUS_IND 0x18
  163. #define RT_CAN_CMD_SET_BUS_HOOK 0x19
  164. #define RT_CAN_CMD_SET_CANFD 0x1A
  165. #define RT_CAN_CMD_SET_BAUD_FD 0x1B
  166. #define RT_CAN_CMD_SET_BITTIMING 0x1C
  167. #define RT_DEVICE_CAN_INT_ERR 0x1000
  168. enum RT_CAN_STATUS_MODE
  169. {
  170. NORMAL = 0,
  171. ERRWARNING = 1,
  172. ERRPASSIVE = 2,
  173. BUSOFF = 4,
  174. };
  175. enum RT_CAN_BUS_ERR
  176. {
  177. RT_CAN_BUS_NO_ERR = 0,
  178. RT_CAN_BUS_BIT_PAD_ERR = 1,
  179. RT_CAN_BUS_FORMAT_ERR = 2,
  180. RT_CAN_BUS_ACK_ERR = 3,
  181. RT_CAN_BUS_IMPLICIT_BIT_ERR = 4,
  182. RT_CAN_BUS_EXPLICIT_BIT_ERR = 5,
  183. RT_CAN_BUS_CRC_ERR = 6,
  184. };
  185. struct rt_can_status
  186. {
  187. rt_uint32_t rcverrcnt;
  188. rt_uint32_t snderrcnt;
  189. rt_uint32_t errcode;
  190. rt_uint32_t rcvpkg;
  191. rt_uint32_t dropedrcvpkg;
  192. rt_uint32_t sndpkg;
  193. rt_uint32_t dropedsndpkg;
  194. rt_uint32_t bitpaderrcnt;
  195. rt_uint32_t formaterrcnt;
  196. rt_uint32_t ackerrcnt;
  197. rt_uint32_t biterrcnt;
  198. rt_uint32_t crcerrcnt;
  199. rt_uint32_t rcvchange;
  200. rt_uint32_t sndchange;
  201. rt_uint32_t lasterrtype;
  202. };
  203. #ifdef RT_CAN_USING_HDR
  204. struct rt_can_hdr
  205. {
  206. rt_uint32_t connected;
  207. rt_uint32_t msgs;
  208. struct rt_can_filter_item filter;
  209. struct rt_list_node list;
  210. };
  211. #endif
  212. struct rt_can_device;
  213. typedef rt_err_t (*rt_canstatus_ind)(struct rt_can_device *, void *);
  214. typedef struct rt_can_status_ind_type
  215. {
  216. rt_canstatus_ind ind;
  217. void *args;
  218. } *rt_can_status_ind_type_t;
  219. typedef void (*rt_can_bus_hook)(struct rt_can_device *);
  220. struct rt_can_device
  221. {
  222. struct rt_device parent;
  223. const struct rt_can_ops *ops;
  224. struct can_configure config;
  225. struct rt_can_status status;
  226. rt_uint32_t timerinitflag;
  227. struct rt_timer timer;
  228. struct rt_can_status_ind_type status_indicate;
  229. #ifdef RT_CAN_USING_HDR
  230. struct rt_can_hdr *hdr;
  231. #endif
  232. #ifdef RT_CAN_USING_BUS_HOOK
  233. rt_can_bus_hook bus_hook;
  234. #endif /*RT_CAN_USING_BUS_HOOK*/
  235. struct rt_mutex lock;
  236. void *can_rx;
  237. void *can_tx;
  238. };
  239. typedef struct rt_can_device *rt_can_t;
  240. #define RT_CAN_STDID 0
  241. #define RT_CAN_EXTID 1
  242. #define RT_CAN_DTR 0
  243. #define RT_CAN_RTR 1
  244. typedef struct rt_can_status *rt_can_status_t;
  245. struct rt_can_msg
  246. {
  247. rt_uint32_t id : 29;
  248. rt_uint32_t ide : 1;
  249. rt_uint32_t rtr : 1;
  250. rt_uint32_t rsv : 1;
  251. rt_uint32_t len : 8;
  252. rt_uint32_t priv : 8;
  253. rt_int32_t hdr : 8;
  254. #ifdef RT_CAN_USING_CANFD
  255. rt_uint32_t fd_frame : 1;
  256. rt_uint32_t reserved : 7;
  257. #else
  258. rt_uint32_t reserved : 8;
  259. #endif
  260. #ifdef RT_CAN_USING_CANFD
  261. rt_uint8_t data[64];
  262. #else
  263. rt_uint8_t data[8];
  264. #endif
  265. };
  266. typedef struct rt_can_msg *rt_can_msg_t;
  267. struct rt_can_msg_list
  268. {
  269. struct rt_list_node list;
  270. #ifdef RT_CAN_USING_HDR
  271. struct rt_list_node hdrlist;
  272. struct rt_can_hdr *owner;
  273. #endif
  274. struct rt_can_msg data;
  275. };
  276. struct rt_can_rx_fifo
  277. {
  278. /* software fifo */
  279. struct rt_can_msg_list *buffer;
  280. rt_uint32_t freenumbers;
  281. struct rt_list_node freelist;
  282. struct rt_list_node uselist;
  283. };
  284. #define RT_CAN_SND_RESULT_OK 0
  285. #define RT_CAN_SND_RESULT_ERR 1
  286. #define RT_CAN_SND_RESULT_WAIT 2
  287. #define RT_CAN_EVENT_RX_IND 0x01 /* Rx indication */
  288. #define RT_CAN_EVENT_TX_DONE 0x02 /* Tx complete */
  289. #define RT_CAN_EVENT_TX_FAIL 0x03 /* Tx fail */
  290. #define RT_CAN_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
  291. #define RT_CAN_EVENT_RXOF_IND 0x06 /* Rx overflow */
  292. struct rt_can_sndbxinx_list
  293. {
  294. struct rt_list_node list;
  295. struct rt_completion completion;
  296. rt_uint32_t result;
  297. };
  298. struct rt_can_tx_fifo
  299. {
  300. struct rt_can_sndbxinx_list *buffer;
  301. struct rt_semaphore sem;
  302. struct rt_list_node freelist;
  303. };
  304. struct rt_can_ops
  305. {
  306. rt_err_t (*configure)(struct rt_can_device *can, struct can_configure *cfg);
  307. rt_err_t (*control)(struct rt_can_device *can, int cmd, void *arg);
  308. int (*sendmsg)(struct rt_can_device *can, const void *buf, rt_uint32_t boxno);
  309. int (*recvmsg)(struct rt_can_device *can, void *buf, rt_uint32_t boxno);
  310. };
  311. rt_err_t rt_hw_can_register(struct rt_can_device *can,
  312. const char *name,
  313. const struct rt_can_ops *ops,
  314. void *data);
  315. void rt_hw_can_isr(struct rt_can_device *can, int event);
  316. #endif /*_CAN_H*/