can.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file can.h
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef CAN_H_
  35. #define CAN_H_
  36. #include <rtthread.h>
  37. #include "completion.h"
  38. #include "stdbool.h"
  39. #ifndef RT_CANMSG_BOX_SZ
  40. #define RT_CANMSG_BOX_SZ 16
  41. #endif
  42. #ifndef RT_CANSND_BOX_NUM
  43. #define RT_CANSND_BOX_NUM 1
  44. #endif
  45. enum CANBAUD
  46. {
  47. CAN1MBaud = 1000UL * 1000,/* 1 MBit/sec */
  48. CAN500kBaud = 1000UL * 500, /* 500 kBit/sec */
  49. CAN250kBaud = 1000UL * 250, /* 250 kBit/sec */
  50. CAN125kBaud = 1000UL * 125, /* 125 kBit/sec */
  51. CAN100kBaud = 1000UL * 100, /* 100 kBit/sec */
  52. CAN50kBaud = 1000UL * 50, /* 50 kBit/sec */
  53. CAN20kBaud = 1000UL * 20, /* 20 kBit/sec */
  54. CAN10kBaud = 1000UL * 10 /* 10 kBit/sec */
  55. };
  56. #define RT_CAN_MODE_NORMAL 0
  57. #define RT_CAN_MODE_LISEN 1
  58. #define RT_CAN_MODE_LOOPBACK 2
  59. #define RT_CAN_MODE_LOOPBACKANLISEN 3
  60. #define RT_CAN_MODE_PRIV 0x01
  61. #define RT_CAN_MODE_NOPRIV 0x00
  62. struct rt_can_filter_item
  63. {
  64. rt_uint32_t id : 29;
  65. rt_uint32_t ide : 1;
  66. rt_uint32_t rtr : 1;
  67. rt_uint32_t mode : 1;
  68. rt_uint32_t mask;
  69. rt_int32_t hdr;
  70. #ifdef RT_CAN_USING_HDR
  71. rt_err_t (*ind)(rt_device_t dev, void *args , rt_int32_t hdr, rt_size_t size);
  72. void *args;
  73. #endif /*RT_CAN_USING_HDR*/
  74. };
  75. #ifdef RT_CAN_USING_HDR
  76. #define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask,ind,args) \
  77. {(id), (ide), (rtr), (mode), (mask), -1, (ind), (args)}
  78. #define RT_CAN_FILTER_STD_INIT(id,ind,args) \
  79. RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF,ind,args)
  80. #define RT_CAN_FILTER_EXT_INIT(id,ind,args) \
  81. RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF,ind,args)
  82. #define RT_CAN_STD_RMT_FILTER_INIT(id,ind,args) \
  83. RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF,ind,args)
  84. #define RT_CAN_EXT_RMT_FILTER_INIT(id,ind,args) \
  85. RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF,ind,args)
  86. #define RT_CAN_STD_RMT_DATA_FILTER_INIT(id,ind,args) \
  87. RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF,ind,args)
  88. #define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id,ind,args) \
  89. RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF,ind,args)
  90. #else
  91. #define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask,ind,args) \
  92. {(id), (ide), (rtr), (mode), (mask), -1, (ind), (args)}
  93. #define RT_CAN_FILTER_STD_INIT(id) \
  94. RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF)
  95. #define RT_CAN_FILTER_EXT_INIT(id) \
  96. RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF)
  97. #define RT_CAN_STD_RMT_FILTER_INIT(id) \
  98. RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF)
  99. #define RT_CAN_EXT_RMT_FILTER_INIT(id) \
  100. RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF)
  101. #define RT_CAN_STD_RMT_DATA_FILTER_INIT(id) \
  102. RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF)
  103. #define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id) \
  104. RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF)
  105. #endif
  106. struct rt_can_filter_config
  107. {
  108. rt_uint32_t count;
  109. rt_uint32_t actived;
  110. struct rt_can_filter_item *items;
  111. };
  112. struct can_configure
  113. {
  114. rt_uint32_t baud_rate;
  115. rt_uint32_t msgboxsz;
  116. rt_uint32_t sndboxnumber;
  117. rt_uint32_t mode : 8;
  118. rt_uint32_t privmode : 8;
  119. rt_uint32_t reserved : 16;
  120. rt_uint32_t ticks;
  121. #ifdef RT_CAN_USING_HDR
  122. rt_uint32_t maxhdr;
  123. #endif
  124. };
  125. #define CANDEFAULTCONFIG \
  126. {\
  127. CAN500kBaud,\
  128. RT_CANMSG_BOX_SZ,\
  129. RT_CANSND_BOX_NUM,\
  130. RT_CAN_MODE_LOOPBACK,\
  131. };
  132. struct rt_can_ops;
  133. #define RT_CAN_CMD_SET_FILTER 0x13
  134. #define RT_CAN_CMD_SET_BAUD 0x14
  135. #define RT_CAN_CMD_SET_MODE 0x15
  136. #define RT_CAN_CMD_SET_PRIV 0x16
  137. #define RT_CAN_CMD_GET_STATUS 0x17
  138. #define RT_CAN_CMD_SET_STATUS_IND 0x18
  139. #define RT_CAN_CMD_SET_BUS_HOOK 0x19
  140. #define RT_DEVICE_CAN_INT_ERR 0x1000
  141. enum RT_CAN_STATUS_MODE
  142. {
  143. NORMAL = 0,
  144. ERRWARNING = 1,
  145. ERRPASSIVE = 2,
  146. BUSOFF = 4,
  147. };
  148. enum RT_CAN_BUS_ERR
  149. {
  150. RT_CAN_BUS_NO_ERR = 0,
  151. RT_CAN_BUS_BIT_PAD_ERR = 1,
  152. RT_CAN_BUS_FORMAT_ERR = 2,
  153. RT_CAN_BUS_ACK_ERR = 3,
  154. RT_CAN_BUS_IMPLICIT_BIT_ERR = 4,
  155. RT_CAN_BUS_EXPLICIT_BIT_ERR = 5,
  156. RT_CAN_BUS_CRC_ERR = 6,
  157. };
  158. struct rt_can_status
  159. {
  160. rt_uint32_t rcverrcnt;
  161. rt_uint32_t snderrcnt;
  162. rt_uint32_t errcode;
  163. rt_uint32_t rcvpkg;
  164. rt_uint32_t dropedrcvpkg;
  165. rt_uint32_t sndpkg;
  166. rt_uint32_t dropedsndpkg;
  167. rt_uint32_t bitpaderrcnt;
  168. rt_uint32_t formaterrcnt;
  169. rt_uint32_t ackerrcnt;
  170. rt_uint32_t biterrcnt;
  171. rt_uint32_t crcerrcnt;
  172. rt_uint32_t rcvchange;
  173. rt_uint32_t sndchange;
  174. rt_uint32_t lasterrtype;
  175. };
  176. #ifdef RT_CAN_USING_HDR
  177. struct rt_can_hdr
  178. {
  179. rt_uint32_t connected;
  180. rt_uint32_t msgs;
  181. struct rt_can_filter_item filter;
  182. struct rt_list_node list;
  183. };
  184. #endif
  185. struct rt_can_device;
  186. typedef rt_err_t (*rt_canstatus_ind)(struct rt_can_device *, void *);
  187. typedef struct rt_can_status_ind_type
  188. {
  189. rt_canstatus_ind ind;
  190. void *args;
  191. } *rt_can_status_ind_type_t;
  192. typedef void (*rt_can_bus_hook)(struct rt_can_device *);
  193. struct rt_can_device
  194. {
  195. struct rt_device parent;
  196. const struct rt_can_ops *ops;
  197. struct can_configure config;
  198. struct rt_can_status status;
  199. rt_uint32_t timerinitflag;
  200. struct rt_timer timer;
  201. struct rt_can_status_ind_type status_indicate;
  202. #ifdef RT_CAN_USING_HDR
  203. struct rt_can_hdr *hdr;
  204. #endif
  205. #ifdef RT_CAN_USING_BUS_HOOK
  206. rt_can_bus_hook bus_hook;
  207. #endif /*RT_CAN_USING_BUS_HOOK*/
  208. struct rt_mutex lock;
  209. void *can_rx;
  210. void *can_tx;
  211. };
  212. typedef struct rt_can_device *rt_can_t;
  213. #define RT_CAN_STDID 0
  214. #define RT_CAN_EXTID 1
  215. #define RT_CAN_DTR 0
  216. #define RT_CAN_RTR 1
  217. typedef struct rt_can_status *rt_can_status_t;
  218. struct rt_can_msg
  219. {
  220. rt_uint32_t id : 29;
  221. rt_uint32_t ide : 1;
  222. rt_uint32_t rtr : 1;
  223. rt_uint32_t rsv : 1;
  224. rt_uint32_t len : 8;
  225. rt_uint32_t priv : 8;
  226. rt_int32_t hdr : 8;
  227. rt_uint32_t reserved : 8;
  228. rt_uint8_t data[8];
  229. };
  230. typedef struct rt_can_msg *rt_can_msg_t;
  231. struct rt_can_msg_list
  232. {
  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_RESULT_OK 0
  249. #define RT_CAN_SND_RESULT_ERR 1
  250. #define RT_CAN_SND_RESULT_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 fail */
  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. {
  258. struct rt_list_node list;
  259. struct rt_completion completion;
  260. rt_uint32_t result;
  261. };
  262. struct rt_can_tx_fifo
  263. {
  264. struct rt_can_sndbxinx_list *buffer;
  265. struct rt_semaphore sem;
  266. struct rt_list_node freelist;
  267. };
  268. struct rt_can_ops
  269. {
  270. rt_err_t (*configure)(struct rt_can_device *can, struct can_configure *cfg);
  271. rt_err_t (*control)(struct rt_can_device *can, int cmd, void *arg);
  272. int (*sendmsg)(struct rt_can_device *can, const void *buf, rt_uint32_t boxno);
  273. int (*recvmsg)(struct rt_can_device *can, void *buf, rt_uint32_t boxno);
  274. };
  275. rt_err_t rt_hw_can_register(struct rt_can_device *can,
  276. const char *name,
  277. const struct rt_can_ops *ops,
  278. void *data);
  279. void rt_hw_can_isr(struct rt_can_device *can, int event);
  280. #endif /*_CAN_H*/