can.h 8.1 KB

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