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