can.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. /** @defgroup CAN_receive_FIFO_number CAN Receive FIFO Number
  59. * @{
  60. */
  61. #define CAN_RX_FIFO0 (0x00000000U) /*!< CAN receive FIFO 0 */
  62. #define CAN_RX_FIFO1 (0x00000001U) /*!< CAN receive FIFO 1 */
  63. struct rt_can_filter_item
  64. {
  65. rt_uint32_t id : 29;
  66. rt_uint32_t ide : 1;
  67. rt_uint32_t rtr : 1;
  68. rt_uint32_t mode : 1;
  69. rt_uint32_t mask;
  70. rt_int32_t hdr_bank;/*Should be defined as:rx.FilterBank,which should be changed to rt_int32_t hdr_bank*/
  71. rt_uint32_t rxfifo;/*Add a configuration item that CAN_RX_FIFO0/CAN_RX_FIFO1*/
  72. #ifdef RT_CAN_USING_HDR
  73. rt_err_t (*ind)(rt_device_t dev, void *args , rt_int32_t hdr, rt_size_t size);
  74. void *args;
  75. #endif /*RT_CAN_USING_HDR*/
  76. };
  77. #ifdef RT_CAN_USING_HDR
  78. #define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask,ind,args) \
  79. {(id), (ide), (rtr), (mode),(mask), -1, CAN_RX_FIFO0,(ind), (args)}/*0:CAN_RX_FIFO0*/
  80. #define RT_CAN_FILTER_STD_INIT(id,ind,args) \
  81. RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF,ind,args)
  82. #define RT_CAN_FILTER_EXT_INIT(id,ind,args) \
  83. RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF,ind,args)
  84. #define RT_CAN_STD_RMT_FILTER_INIT(id,ind,args) \
  85. RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF,ind,args)
  86. #define RT_CAN_EXT_RMT_FILTER_INIT(id,ind,args) \
  87. RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF,ind,args)
  88. #define RT_CAN_STD_RMT_DATA_FILTER_INIT(id,ind,args) \
  89. RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF,ind,args)
  90. #define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id,ind,args) \
  91. RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF,ind,args)
  92. #else
  93. #define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask) \
  94. {(id), (ide), (rtr), (mode), (mask), -1, CAN_RX_FIFO0 }/*0:CAN_RX_FIFO0*/
  95. #define RT_CAN_FILTER_STD_INIT(id) \
  96. RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF)
  97. #define RT_CAN_FILTER_EXT_INIT(id) \
  98. RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF)
  99. #define RT_CAN_STD_RMT_FILTER_INIT(id) \
  100. RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF)
  101. #define RT_CAN_EXT_RMT_FILTER_INIT(id) \
  102. RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF)
  103. #define RT_CAN_STD_RMT_DATA_FILTER_INIT(id) \
  104. RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF)
  105. #define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id) \
  106. RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF)
  107. #endif
  108. struct rt_can_filter_config
  109. {
  110. rt_uint32_t count;
  111. rt_uint32_t actived;
  112. struct rt_can_filter_item *items;
  113. };
  114. struct rt_can_bit_timing
  115. {
  116. rt_uint16_t prescaler; /* Pre-scaler */
  117. rt_uint16_t num_seg1; /* Bit Timing Segment 1, in terms of Tq */
  118. rt_uint16_t num_seg2; /* Bit Timing Segment 2, in terms of Tq */
  119. rt_uint8_t num_sjw; /* Synchronization Jump Width, in terms of Tq */
  120. rt_uint8_t num_sspoff; /* Secondary Sample Point Offset, in terms of Tq */
  121. };
  122. /**
  123. * CAN bit timing configuration list
  124. * NOTE:
  125. * items[0] always for CAN2.0/CANFD Arbitration Phase
  126. * items[1] always for CANFD (if it exists)
  127. */
  128. struct rt_can_bit_timing_config
  129. {
  130. rt_uint32_t count;
  131. struct rt_can_bit_timing *items;
  132. };
  133. struct can_configure
  134. {
  135. rt_uint32_t baud_rate;
  136. rt_uint32_t msgboxsz;
  137. rt_uint32_t sndboxnumber;
  138. rt_uint32_t mode : 8;
  139. rt_uint32_t privmode : 8;
  140. rt_uint32_t reserved : 16;
  141. rt_uint32_t ticks;
  142. #ifdef RT_CAN_USING_HDR
  143. rt_uint32_t maxhdr;
  144. #endif
  145. #ifdef RT_CAN_USING_CANFD
  146. rt_uint32_t baud_rate_fd; /* CANFD data bit rate*/
  147. rt_uint32_t use_bit_timing: 8; /* Use the bit timing for CAN timing configuration */
  148. rt_uint32_t enable_canfd : 8; /* Enable CAN-FD mode */
  149. rt_uint32_t reserved1 : 16;
  150. /* The below fields take effect only if use_bit_timing is non-zero */
  151. struct rt_can_bit_timing can_timing; /* CAN bit-timing /CANFD bit-timing for arbitration phase */
  152. struct rt_can_bit_timing canfd_timing; /* CANFD bit-timing for datat phase */
  153. #endif
  154. };
  155. #define CANDEFAULTCONFIG \
  156. {\
  157. CAN1MBaud,\
  158. RT_CANMSG_BOX_SZ,\
  159. RT_CANSND_BOX_NUM,\
  160. RT_CAN_MODE_NORMAL,\
  161. };
  162. struct rt_can_ops;
  163. #define RT_CAN_CMD_SET_FILTER 0x13
  164. #define RT_CAN_CMD_SET_BAUD 0x14
  165. #define RT_CAN_CMD_SET_MODE 0x15
  166. #define RT_CAN_CMD_SET_PRIV 0x16
  167. #define RT_CAN_CMD_GET_STATUS 0x17
  168. #define RT_CAN_CMD_SET_STATUS_IND 0x18
  169. #define RT_CAN_CMD_SET_BUS_HOOK 0x19
  170. #define RT_CAN_CMD_SET_CANFD 0x1A
  171. #define RT_CAN_CMD_SET_BAUD_FD 0x1B
  172. #define RT_CAN_CMD_SET_BITTIMING 0x1C
  173. #define RT_DEVICE_CAN_INT_ERR 0x1000
  174. enum RT_CAN_STATUS_MODE
  175. {
  176. NORMAL = 0,
  177. ERRWARNING = 1,
  178. ERRPASSIVE = 2,
  179. BUSOFF = 4,
  180. };
  181. enum RT_CAN_BUS_ERR
  182. {
  183. RT_CAN_BUS_NO_ERR = 0,
  184. RT_CAN_BUS_BIT_PAD_ERR = 1,
  185. RT_CAN_BUS_FORMAT_ERR = 2,
  186. RT_CAN_BUS_ACK_ERR = 3,
  187. RT_CAN_BUS_IMPLICIT_BIT_ERR = 4,
  188. RT_CAN_BUS_EXPLICIT_BIT_ERR = 5,
  189. RT_CAN_BUS_CRC_ERR = 6,
  190. };
  191. struct rt_can_status
  192. {
  193. rt_uint32_t rcverrcnt;
  194. rt_uint32_t snderrcnt;
  195. rt_uint32_t errcode;
  196. rt_uint32_t rcvpkg;
  197. rt_uint32_t dropedrcvpkg;
  198. rt_uint32_t sndpkg;
  199. rt_uint32_t dropedsndpkg;
  200. rt_uint32_t bitpaderrcnt;
  201. rt_uint32_t formaterrcnt;
  202. rt_uint32_t ackerrcnt;
  203. rt_uint32_t biterrcnt;
  204. rt_uint32_t crcerrcnt;
  205. rt_uint32_t rcvchange;
  206. rt_uint32_t sndchange;
  207. rt_uint32_t lasterrtype;
  208. };
  209. #ifdef RT_CAN_USING_HDR
  210. struct rt_can_hdr
  211. {
  212. rt_uint32_t connected;
  213. rt_uint32_t msgs;
  214. struct rt_can_filter_item filter;
  215. struct rt_list_node list;
  216. };
  217. #endif
  218. struct rt_can_device;
  219. typedef rt_err_t (*rt_canstatus_ind)(struct rt_can_device *, void *);
  220. typedef struct rt_can_status_ind_type
  221. {
  222. rt_canstatus_ind ind;
  223. void *args;
  224. } *rt_can_status_ind_type_t;
  225. typedef void (*rt_can_bus_hook)(struct rt_can_device *);
  226. struct rt_can_device
  227. {
  228. struct rt_device parent;
  229. const struct rt_can_ops *ops;
  230. struct can_configure config;
  231. struct rt_can_status status;
  232. rt_uint32_t timerinitflag;
  233. struct rt_timer timer;
  234. struct rt_can_status_ind_type status_indicate;
  235. #ifdef RT_CAN_USING_HDR
  236. struct rt_can_hdr *hdr;
  237. #endif
  238. #ifdef RT_CAN_USING_BUS_HOOK
  239. rt_can_bus_hook bus_hook;
  240. #endif /*RT_CAN_USING_BUS_HOOK*/
  241. struct rt_mutex lock;
  242. void *can_rx;
  243. void *can_tx;
  244. };
  245. typedef struct rt_can_device *rt_can_t;
  246. #define RT_CAN_STDID 0
  247. #define RT_CAN_EXTID 1
  248. #define RT_CAN_DTR 0
  249. #define RT_CAN_RTR 1
  250. typedef struct rt_can_status *rt_can_status_t;
  251. struct rt_can_msg
  252. {
  253. rt_uint32_t id : 29;
  254. rt_uint32_t ide : 1;
  255. rt_uint32_t rtr : 1;
  256. rt_uint32_t rsv : 1;
  257. rt_uint32_t len : 8;
  258. rt_uint32_t priv : 8;
  259. rt_int32_t hdr_index : 8;/*Should be defined as:rx.FilterMatchIndex,which should be changed to rt_int32_t hdr_index : 8*/
  260. #ifdef RT_CAN_USING_CANFD
  261. rt_uint32_t fd_frame : 1;
  262. rt_uint32_t brs : 1;
  263. rt_uint32_t rxfifo : 2;/*Redefined to return :CAN RX FIFO0/CAN RX FIFO1*/
  264. rt_uint32_t reserved : 4;
  265. #else
  266. rt_uint32_t rxfifo : 2;/*Redefined to return :CAN RX FIFO0/CAN RX FIFO1*/
  267. rt_uint32_t reserved : 6;
  268. #endif
  269. #ifdef RT_CAN_USING_CANFD
  270. rt_uint8_t data[64];
  271. #else
  272. rt_uint8_t data[8];
  273. #endif
  274. };
  275. typedef struct rt_can_msg *rt_can_msg_t;
  276. struct rt_can_msg_list
  277. {
  278. struct rt_list_node list;
  279. #ifdef RT_CAN_USING_HDR
  280. struct rt_list_node hdrlist;
  281. struct rt_can_hdr *owner;
  282. #endif
  283. struct rt_can_msg data;
  284. };
  285. struct rt_can_rx_fifo
  286. {
  287. /* software fifo */
  288. struct rt_can_msg_list *buffer;
  289. rt_uint32_t freenumbers;
  290. struct rt_list_node freelist;
  291. struct rt_list_node uselist;
  292. };
  293. #define RT_CAN_SND_RESULT_OK 0
  294. #define RT_CAN_SND_RESULT_ERR 1
  295. #define RT_CAN_SND_RESULT_WAIT 2
  296. #define RT_CAN_EVENT_RX_IND 0x01 /* Rx indication */
  297. #define RT_CAN_EVENT_TX_DONE 0x02 /* Tx complete */
  298. #define RT_CAN_EVENT_TX_FAIL 0x03 /* Tx fail */
  299. #define RT_CAN_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
  300. #define RT_CAN_EVENT_RXOF_IND 0x06 /* Rx overflow */
  301. struct rt_can_sndbxinx_list
  302. {
  303. struct rt_list_node list;
  304. struct rt_completion completion;
  305. rt_uint32_t result;
  306. };
  307. struct rt_can_tx_fifo
  308. {
  309. struct rt_can_sndbxinx_list *buffer;
  310. struct rt_semaphore sem;
  311. struct rt_list_node freelist;
  312. };
  313. struct rt_can_ops
  314. {
  315. rt_err_t (*configure)(struct rt_can_device *can, struct can_configure *cfg);
  316. rt_err_t (*control)(struct rt_can_device *can, int cmd, void *arg);
  317. int (*sendmsg)(struct rt_can_device *can, const void *buf, rt_uint32_t boxno);
  318. int (*recvmsg)(struct rt_can_device *can, void *buf, rt_uint32_t boxno);
  319. };
  320. rt_err_t rt_hw_can_register(struct rt_can_device *can,
  321. const char *name,
  322. const struct rt_can_ops *ops,
  323. void *data);
  324. void rt_hw_can_isr(struct rt_can_device *can, int event);
  325. #endif /*_CAN_H*/