can.h 8.1 KB

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