can.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 OTA keys S.A.
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser General
5  * Public License v2.1. See the file LICENSE in the top level directory for more
6  * details.
7  */
8 
26 #ifndef CAN_CAN_H
27 #define CAN_CAN_H
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <stdint.h>
34 
35 #if defined(__linux__)
36 
37 #include <linux/can.h>
38 #include <libsocketcan.h>
39 
40 #else
41 
45 #define CAN_MAX_DLEN (8)
46 
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)
65  enum can_state {
72  CAN_STATE_MAX
73  };
74 
83 typedef uint32_t canid_t;
84 
88 struct can_frame {
90  uint8_t can_dlc;
91  uint8_t __pad;
92  uint8_t __res0;
93  uint8_t __res1;
95  uint8_t data[CAN_MAX_DLEN] __attribute__((aligned(8)));
96 };
97 
101 struct can_filter {
104 };
105 
114  uint32_t bitrate;
115  uint32_t sample_point;
116  uint32_t tq;
117  uint32_t prop_seg;
118  uint32_t phase_seg1;
119  uint32_t phase_seg2;
120  uint32_t sjw;
121  uint32_t brp;
122 };
123 
130  uint32_t tseg1_min;
131  uint32_t tseg1_max;
132  uint32_t tseg2_min;
133  uint32_t tseg2_max;
134  uint32_t sjw_max;
135  uint32_t brp_min;
136  uint32_t brp_max;
137  uint32_t brp_inc;
138 };
139 
140 #endif /* defined(__linux__) */
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
146 #endif /* CAN_CAN_H */
147 
CAN_STATE_ERROR_PASSIVE
@ CAN_STATE_ERROR_PASSIVE
RX/TX error count < 256.
Definition: can.h:68
can_bittiming_const::brp_inc
uint32_t brp_inc
Bit-rate prescaler, increment.
Definition: can.h:137
CAN_MAX_DLEN
#define CAN_MAX_DLEN
Max data length for a CAN frame.
Definition: can.h:45
can_frame::__res1
uint8_t __res1
reserved / padding
Definition: can.h:93
can_frame::__res0
uint8_t __res0
reserved / padding
Definition: can.h:92
can_bittiming_const::brp_max
uint32_t brp_max
Bit-rate prescaler, max value.
Definition: can.h:136
can_bittiming_const::sjw_max
uint32_t sjw_max
Synchronisation jump width.
Definition: can.h:134
can_bittiming_const::tseg1_max
uint32_t tseg1_max
Time segment 1, max value.
Definition: can.h:131
CAN_STATE_BUS_OFF
@ CAN_STATE_BUS_OFF
RX/TX error count >= 256.
Definition: can.h:69
can_bittiming::sample_point
uint32_t sample_point
Sample point in one-tenth of a percent.
Definition: can.h:115
CAN_STATE_STOPPED
@ CAN_STATE_STOPPED
Device is stopped.
Definition: can.h:70
can_frame::__pad
uint8_t __pad
padding
Definition: can.h:91
CAN_STATE_ERROR_WARNING
@ CAN_STATE_ERROR_WARNING
RX/TX error count < 128.
Definition: can.h:67
CAN_STATE_ERROR_ACTIVE
@ CAN_STATE_ERROR_ACTIVE
RX/TX error count < 96.
Definition: can.h:66
can_filter::can_mask
canid_t can_mask
Mask.
Definition: can.h:103
can_bittiming::phase_seg2
uint32_t phase_seg2
Phase buffer segment 2 in TQs.
Definition: can.h:119
can_bittiming_const::tseg2_min
uint32_t tseg2_min
Time segment 2 = phase_seg2, min value.
Definition: can.h:132
can_bittiming::prop_seg
uint32_t prop_seg
Propagation segment in TQs.
Definition: can.h:117
CAN_STATE_SLEEPING
@ CAN_STATE_SLEEPING
Device is sleeping.
Definition: can.h:71
can_filter
Controller Area Network filter.
Definition: can.h:101
can_bittiming_const
CAN hardware-dependent bit-timing constant.
Definition: can.h:129
can_filter::can_id
canid_t can_id
CAN ID.
Definition: can.h:102
can_bittiming::tq
uint32_t tq
Time quanta (TQ) in nanoseconds.
Definition: can.h:116
can_frame
Controller Area Network frame.
Definition: can.h:88
can_bittiming_const::tseg2_max
uint32_t tseg2_max
Time segment 2, max value.
Definition: can.h:133
can_frame::can_id
canid_t can_id
32 bit CAN_ID + EFF/RTR/ERR flags
Definition: can.h:89
can_bittiming_const::tseg1_min
uint32_t tseg1_min
Time segment 1 = prop_seg + phase_seg1, min value.
Definition: can.h:130
can_bittiming::sjw
uint32_t sjw
Synchronisation jump width in TQs.
Definition: can.h:120
can_bittiming::phase_seg1
uint32_t phase_seg1
Phase buffer segment 1 in TQs.
Definition: can.h:118
can_bittiming
CAN bit-timing parameters.
Definition: can.h:113
can_bittiming::brp
uint32_t brp
Bit-rate prescaler.
Definition: can.h:121
can_frame::can_dlc
uint8_t can_dlc
frame payload length in byte (0 .
Definition: can.h:90
can_frame::data
uint8_t data[CAN_MAX_DLEN]
Frame data.
Definition: can.h:95
can_bittiming_const::brp_min
uint32_t brp_min
Bit-rate prescaler, min value.
Definition: can.h:135
can_bittiming::bitrate
uint32_t bitrate
Bit-rate in bits/second.
Definition: can.h:114
canid_t
uint32_t canid_t
Controller Area Network Identifier structure.
Definition: can.h:83