can.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016 OTA keys S.A.
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #include <stdint.h>
30 
31 #if defined(__linux__)
32 
33 #include <linux/can.h>
34 #include <libsocketcan.h>
35 
36 #else
37 
43 #define CAN_MAX_DLEN (8)
44 #define CANFD_MAX_DLEN (64)
51 /* special address description flags for the CAN_ID */
52 #define CAN_EFF_FLAG (0x80000000U)
53 #define CAN_RTR_FLAG (0x40000000U)
54 #define CAN_ERR_FLAG (0x20000000U)
56 /* valid bits in CAN ID for frame formats */
57 #define CAN_SFF_MASK (0x000007FFU)
58 #define CAN_EFF_MASK (0x1FFFFFFFU)
59 #define CAN_ERR_MASK (0x1FFFFFFFU)
66 #define CANFD_BRS 0x01
67 #define CANFD_ESI 0x02
68 #define CANFD_FDF 0x04
74  enum can_state {
81  CAN_STATE_MAX
82  };
83 
92 typedef uint32_t canid_t;
93 
97 struct can_frame {
99  union {
100  uint8_t len;
101  uint8_t can_dlc;
102  };
103  uint8_t __pad;
104  uint8_t __res0;
105  uint8_t __res1;
107  uint8_t data[CAN_MAX_DLEN] __attribute__((aligned(8)));
108 };
109 
110 #ifdef MODULE_FDCAN
111 struct canfd_frame {
112  canid_t can_id;
113  uint8_t len;
114  uint8_t flags;
115  uint8_t __res0;
116  uint8_t __res1;
118  uint8_t data[CANFD_MAX_DLEN] __attribute__((aligned(8)));
119 };
120 #endif
121 
125 struct can_filter {
128 #if (MODULE_CAN_RX_MAILBOX)
129  uint8_t target_mailbox;
130 #endif
131 };
132 
141  uint32_t bitrate;
142  uint32_t sample_point;
143  uint32_t tq;
144  uint32_t prop_seg;
145  uint32_t phase_seg1;
146  uint32_t phase_seg2;
147  uint32_t sjw;
148  uint32_t brp;
149 };
150 
157  uint32_t tseg1_min;
158  uint32_t tseg1_max;
159  uint32_t tseg2_min;
160  uint32_t tseg2_max;
161  uint32_t sjw_max;
162  uint32_t brp_min;
163  uint32_t brp_max;
164  uint32_t brp_inc;
165 };
166 
167 #endif /* defined(__linux__) */
168 
172 #ifdef MODULE_FDCAN
173 typedef struct canfd_frame can_frame_t;
174 #else
175 typedef struct can_frame can_frame_t;
176 #endif
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 
#define CANFD_MAX_DLEN
CAN FD maximum data length.
Definition: can.h:44
can_state
CAN operational and error states.
Definition: can.h:74
uint32_t canid_t
Controller Area Network Identifier structure.
Definition: can.h:92
#define CAN_MAX_DLEN
Max data length for classic and FD CAN frames (compliant with libsocketcan macros)
Definition: can.h:43
@ CAN_STATE_ERROR_ACTIVE
RX/TX error count < 96.
Definition: can.h:75
@ CAN_STATE_ERROR_WARNING
RX/TX error count < 128.
Definition: can.h:76
@ CAN_STATE_SLEEPING
Device is sleeping.
Definition: can.h:80
@ CAN_STATE_STOPPED
Device is stopped.
Definition: can.h:79
@ CAN_STATE_BUS_OFF
RX/TX error count >= 256.
Definition: can.h:78
@ CAN_STATE_ERROR_PASSIVE
RX/TX error count < 256.
Definition: can.h:77
CAN hardware-dependent bit-timing constant.
Definition: can.h:156
uint32_t brp_min
Bit-rate prescaler, min value.
Definition: can.h:162
uint32_t tseg1_min
Time segment 1 = prop_seg + phase_seg1, min value.
Definition: can.h:157
uint32_t brp_max
Bit-rate prescaler, max value.
Definition: can.h:163
uint32_t tseg1_max
Time segment 1, max value.
Definition: can.h:158
uint32_t tseg2_min
Time segment 2 = phase_seg2, min value.
Definition: can.h:159
uint32_t brp_inc
Bit-rate prescaler, increment.
Definition: can.h:164
uint32_t tseg2_max
Time segment 2, max value.
Definition: can.h:160
uint32_t sjw_max
Synchronisation jump width.
Definition: can.h:161
CAN bit-timing parameters.
Definition: can.h:140
uint32_t tq
Time quanta (TQ) in nanoseconds.
Definition: can.h:143
uint32_t sample_point
Sample point in one-tenth of a percent.
Definition: can.h:142
uint32_t phase_seg1
Phase buffer segment 1 in TQs.
Definition: can.h:145
uint32_t bitrate
Bit-rate in bits/second.
Definition: can.h:141
uint32_t brp
Bit-rate prescaler.
Definition: can.h:148
uint32_t phase_seg2
Phase buffer segment 2 in TQs.
Definition: can.h:146
uint32_t sjw
Synchronisation jump width in TQs.
Definition: can.h:147
uint32_t prop_seg
Propagation segment in TQs.
Definition: can.h:144
Controller Area Network filter.
Definition: can.h:125
canid_t can_mask
Mask.
Definition: can.h:127
canid_t can_id
CAN ID.
Definition: can.h:126
Controller Area Network frame.
Definition: can.h:97
uint8_t __pad
padding
Definition: can.h:103
uint8_t len
frame payload length in byte (0 .
Definition: can.h:100
uint8_t __res1
reserved / padding
Definition: can.h:105
uint8_t can_dlc
deprecated - see SocketCAN documentation
Definition: can.h:101
uint8_t __res0
reserved / padding
Definition: can.h:104
uint8_t data[CAN_MAX_DLEN]
Frame data.
Definition: can.h:107
canid_t can_id
32 bit CAN_ID + EFF/RTR/ERR flags
Definition: can.h:98