RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
dev_can.h
浏览该文件的文档.
1/*
2 * Copyright (c) 2006-2024 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
13#ifndef __DEV_CAN_H_
14#define __DEV_CAN_H_
15
16#include <rtthread.h>
17
18#ifndef RT_CANMSG_BOX_SZ
19#define RT_CANMSG_BOX_SZ 16
20#endif
21#ifndef RT_CANSND_BOX_NUM
22#define RT_CANSND_BOX_NUM 1
23#endif
24
44
46{
47 CAN1MBaud = 1000UL * 1000,/* 1 MBit/sec */
48 CAN800kBaud = 1000UL * 800, /* 800 kBit/sec */
49 CAN500kBaud = 1000UL * 500, /* 500 kBit/sec */
50 CAN250kBaud = 1000UL * 250, /* 250 kBit/sec */
51 CAN125kBaud = 1000UL * 125, /* 125 kBit/sec */
52 CAN100kBaud = 1000UL * 100, /* 100 kBit/sec */
53 CAN50kBaud = 1000UL * 50, /* 50 kBit/sec */
54 CAN20kBaud = 1000UL * 20, /* 20 kBit/sec */
55 CAN10kBaud = 1000UL * 10 /* 10 kBit/sec */
56};
57
58#define RT_CAN_MODE_NORMAL 0
59#define RT_CAN_MODE_LISTEN 1
60#define RT_CAN_MODE_LOOPBACK 2
61#define RT_CAN_MODE_LOOPBACKANLISTEN 3
62
63#define RT_CAN_MODE_PRIV 0x01
64#define RT_CAN_MODE_NOPRIV 0x00
65
205
206
211#define CAN_RX_FIFO0 (0x00000000U)
212#define CAN_RX_FIFO1 (0x00000001U)
213
218{
224 rt_int32_t hdr_bank;/*Should be defined as:rx.FilterBank,which should be changed to rt_int32_t hdr_bank*/
225 rt_uint32_t rxfifo;/*Add a configuration item that CAN_RX_FIFO0/CAN_RX_FIFO1*/
226#ifdef RT_CAN_USING_HDR
227 rt_err_t (*ind)(rt_device_t dev, void *args , rt_int32_t hdr, rt_size_t size);
228 void *args;
229#endif /*RT_CAN_USING_HDR*/
230};
231
232
233#ifdef RT_CAN_USING_HDR
234#define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask,ind,args) \
235 {(id), (ide), (rtr), (mode),(mask), -1, CAN_RX_FIFO0,(ind), (args)}/*0:CAN_RX_FIFO0*/
236#define RT_CAN_FILTER_STD_INIT(id,ind,args) \
237 RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF,ind,args)
238#define RT_CAN_FILTER_EXT_INIT(id,ind,args) \
239 RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF,ind,args)
240#define RT_CAN_STD_RMT_FILTER_INIT(id,ind,args) \
241 RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF,ind,args)
242#define RT_CAN_EXT_RMT_FILTER_INIT(id,ind,args) \
243 RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF,ind,args)
244#define RT_CAN_STD_RMT_DATA_FILTER_INIT(id,ind,args) \
245 RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF,ind,args)
246#define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id,ind,args) \
247 RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF,ind,args)
248#else
249
250#define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask) \
251 {(id), (ide), (rtr), (mode), (mask), -1, CAN_RX_FIFO0 }/*0:CAN_RX_FIFO0*/
252#define RT_CAN_FILTER_STD_INIT(id) \
253 RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF)
254#define RT_CAN_FILTER_EXT_INIT(id) \
255 RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF)
256#define RT_CAN_STD_RMT_FILTER_INIT(id) \
257 RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF)
258#define RT_CAN_EXT_RMT_FILTER_INIT(id) \
259 RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF)
260#define RT_CAN_STD_RMT_DATA_FILTER_INIT(id) \
261 RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF)
262#define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id) \
263 RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF)
264#endif
265
266
276
281{
282 rt_uint16_t prescaler; /* Pre-scaler */
283 rt_uint16_t num_seg1; /* Bit Timing Segment 1, in terms of Tq */
284 rt_uint16_t num_seg2; /* Bit Timing Segment 2, in terms of Tq */
285 rt_uint8_t num_sjw; /* Synchronization Jump Width, in terms of Tq */
286 rt_uint8_t num_sspoff; /* Secondary Sample Point Offset, in terms of Tq */
287};
288
300
301
306{
314#ifdef RT_CAN_USING_HDR
315 rt_uint32_t maxhdr;
316#endif
317
318#ifdef RT_CAN_USING_CANFD
319 rt_uint32_t baud_rate_fd; /* CANFD data bit rate*/
320 rt_uint32_t use_bit_timing: 8; /* Use the bit timing for CAN timing configuration */
321 rt_uint32_t enable_canfd : 8; /* Enable CAN-FD mode */
322 rt_uint32_t reserved1 : 16;
323
324 /* The below fields take effect only if use_bit_timing is non-zero */
325 struct rt_can_bit_timing can_timing; /* CAN bit-timing /CANFD bit-timing for arbitration phase */
326 struct rt_can_bit_timing canfd_timing; /* CANFD bit-timing for datat phase */
327#endif
328};
329
330#define CANDEFAULTCONFIG \
331{\
332 CAN1MBaud,\
333 RT_CANMSG_BOX_SZ,\
334 RT_CANSND_BOX_NUM,\
335 RT_CAN_MODE_NORMAL,\
336};
337
338struct rt_can_ops;
339#define RT_CAN_CMD_SET_FILTER 0x13
340#define RT_CAN_CMD_SET_BAUD 0x14
341#define RT_CAN_CMD_SET_MODE 0x15
342#define RT_CAN_CMD_SET_PRIV 0x16
343#define RT_CAN_CMD_GET_STATUS 0x17
344#define RT_CAN_CMD_SET_STATUS_IND 0x18
345#define RT_CAN_CMD_SET_BUS_HOOK 0x19
346#define RT_CAN_CMD_SET_CANFD 0x1A
347#define RT_CAN_CMD_SET_BAUD_FD 0x1B
348#define RT_CAN_CMD_SET_BITTIMING 0x1C
349#define RT_CAN_CMD_START 0x1D
350
351#define RT_DEVICE_CAN_INT_ERR 0x1000
352
370
392
393#ifdef RT_CAN_USING_HDR
394struct rt_can_hdr
395{
396 rt_uint32_t connected;
397 rt_uint32_t msgs;
398 struct rt_can_filter_item filter;
399 struct rt_list_node list;
400};
401#endif
402struct rt_can_device;
403typedef rt_err_t (*rt_canstatus_ind)(struct rt_can_device *, void *);
404
410typedef void (*rt_can_bus_hook)(struct rt_can_device *);
412{
414
415 const struct rt_can_ops *ops;
418
421
423#ifdef RT_CAN_USING_HDR
424 struct rt_can_hdr *hdr;
425#endif
426#ifdef RT_CAN_USING_BUS_HOOK
427 rt_can_bus_hook bus_hook;
428#endif /*RT_CAN_USING_BUS_HOOK*/
430 void *can_rx;
431 void *can_tx;
432};
433typedef struct rt_can_device *rt_can_t;
434
435#define RT_CAN_STDID 0
436#define RT_CAN_EXTID 1
437#define RT_CAN_DTR 0
438#define RT_CAN_RTR 1
439
441
443{
450 rt_int32_t hdr_index : 8;/*Should be defined as:rx.FilterMatchIndex,which should be changed to rt_int32_t hdr_index : 8*/
451#ifdef RT_CAN_USING_CANFD
452 rt_uint32_t fd_frame : 1;
453 rt_uint32_t brs : 1;
454 rt_uint32_t rxfifo : 2;/*Redefined to return :CAN RX FIFO0/CAN RX FIFO1*/
456#else
457 rt_uint32_t rxfifo : 2;/*Redefined to return :CAN RX FIFO0/CAN RX FIFO1*/
459#endif
460#ifdef RT_CAN_USING_CANFD
461 rt_uint8_t data[64];
462#else
464#endif
465};
466typedef struct rt_can_msg *rt_can_msg_t;
467
469{
471#ifdef RT_CAN_USING_HDR
472 struct rt_list_node hdrlist;
473 struct rt_can_hdr *owner;
474#endif
476};
477
486
487#define RT_CAN_SND_RESULT_OK 0
488#define RT_CAN_SND_RESULT_ERR 1
489#define RT_CAN_SND_RESULT_WAIT 2
490
491#define RT_CAN_EVENT_RX_IND 0x01 /* Rx indication */
492#define RT_CAN_EVENT_TX_DONE 0x02 /* Tx complete */
493#define RT_CAN_EVENT_TX_FAIL 0x03 /* Tx fail */
494#define RT_CAN_EVENT_RX_TIMEOUT 0x05 /* Rx timeout */
495#define RT_CAN_EVENT_RXOF_IND 0x06 /* Rx overflow */
496
498{
500 struct rt_completion completion;
502};
503
510
515{
516 rt_err_t (*configure)(struct rt_can_device *can, struct can_configure *cfg);
517 rt_err_t (*control)(struct rt_can_device *can, int cmd, void *arg);
518 rt_ssize_t (*sendmsg)(struct rt_can_device *can, const void *buf, rt_uint32_t boxno);
519 rt_ssize_t (*recvmsg)(struct rt_can_device *can, void *buf, rt_uint32_t boxno);
520};
521
533 const char *name,
534 const struct rt_can_ops *ops,
535 void *data);
536
543void rt_hw_can_isr(struct rt_can_device *can, int event);
544
546
547#endif /*__DEV_CAN_H*/
CAN_DLC
@ CAN_MSG_24BYTES
@ CAN_MSG_6BYTES
@ CAN_MSG_32BYTES
@ CAN_MSG_64BYTES
@ CAN_MSG_3BYTES
@ CAN_MSG_48BYTES
@ CAN_MSG_4BYTES
@ CAN_MSG_16BYTES
@ CAN_MSG_0BYTE
@ CAN_MSG_5BYTES
@ CAN_MSG_7BYTES
@ CAN_MSG_1BYTE
@ CAN_MSG_20BYTES
@ CAN_MSG_8BYTES
@ CAN_MSG_2BYTES
@ CAN_MSG_12BYTES
CANBAUD
@ CAN100kBaud
@ CAN125kBaud
@ CAN500kBaud
@ CAN800kBaud
@ CAN50kBaud
@ CAN20kBaud
@ CAN1MBaud
@ CAN250kBaud
@ CAN10kBaud
rt_err_t(* rt_canstatus_ind)(struct rt_can_device *, void *)
void rt_hw_can_isr(struct rt_can_device *can, int event)
CAN interrupt service routine
struct rt_can_msg * rt_can_msg_t
RT_CAN_STATUS_MODE
struct rt_can_status * rt_can_status_t
void(* rt_can_bus_hook)(struct rt_can_device *)
RT_CAN_BUS_ERR
struct rt_can_device * rt_can_t
rt_err_t rt_hw_can_register(struct rt_can_device *can, const char *name, const struct rt_can_ops *ops, void *data)
Register a CAN device to device list
struct rt_can_status_ind_type * rt_can_status_ind_type_t
@ BUSOFF
@ ERRWARNING
@ NORMAL
@ ERRPASSIVE
@ RT_CAN_BUS_CRC_ERR
@ RT_CAN_BUS_ACK_ERR
@ RT_CAN_BUS_NO_ERR
@ RT_CAN_BUS_BIT_PAD_ERR
@ RT_CAN_BUS_EXPLICIT_BIT_ERR
@ RT_CAN_BUS_IMPLICIT_BIT_ERR
@ RT_CAN_BUS_FORMAT_ERR
struct rt_device * rt_device_t
rt_base_t rt_ssize_t
rt_base_t rt_err_t
unsigned char rt_uint8_t
unsigned short rt_uint16_t
rt_ubase_t rt_size_t
unsigned int rt_uint32_t
signed int rt_int32_t
CAN configuration
rt_uint32_t mode
rt_uint32_t msgboxsz
rt_uint32_t baud_rate
rt_uint32_t privmode
rt_uint32_t sndboxnumber
rt_uint32_t reserved
rt_uint32_t ticks
CAN bit timing configuration list
struct rt_can_bit_timing * items
CAN timing configuration
rt_uint8_t num_sspoff
rt_uint16_t prescaler
rt_uint16_t num_seg2
rt_uint16_t num_seg1
struct rt_can_status_ind_type status_indicate
struct rt_timer timer
struct rt_mutex lock
rt_uint32_t timerinitflag
struct can_configure config
const struct rt_can_ops * ops
struct rt_can_status status
struct rt_device parent
CAN filter configuration
struct rt_can_filter_item * items
CAN filter item
struct rt_list_node list
struct rt_can_msg data
rt_uint32_t priv
rt_uint32_t id
rt_uint32_t ide
rt_uint32_t rtr
rt_uint32_t rsv
rt_uint32_t len
rt_uint8_t data[8]
rt_int32_t hdr_index
rt_uint32_t reserved
rt_uint32_t rxfifo
CAN operators
rt_err_t(* control)(struct rt_can_device *can, int cmd, void *arg)
rt_err_t(* configure)(struct rt_can_device *can, struct can_configure *cfg)
rt_ssize_t(* recvmsg)(struct rt_can_device *can, void *buf, rt_uint32_t boxno)
rt_ssize_t(* sendmsg)(struct rt_can_device *can, const void *buf, rt_uint32_t boxno)
struct rt_can_msg_list * buffer
rt_uint32_t freenumbers
struct rt_list_node uselist
struct rt_list_node freelist
struct rt_completion completion
struct rt_list_node list
rt_canstatus_ind ind
rt_uint32_t rcvchange
rt_uint32_t dropedsndpkg
rt_uint32_t dropedrcvpkg
rt_uint32_t sndchange
rt_uint32_t formaterrcnt
rt_uint32_t snderrcnt
rt_uint32_t ackerrcnt
rt_uint32_t rcvpkg
rt_uint32_t crcerrcnt
rt_uint32_t bitpaderrcnt
rt_uint32_t errcode
rt_uint32_t lasterrtype
rt_uint32_t sndpkg
rt_uint32_t rcverrcnt
rt_uint32_t biterrcnt
struct rt_semaphore sem
struct rt_can_sndbxinx_list * buffer
struct rt_list_node freelist