|
| 1 | +### NAME |
| 2 | + |
| 3 | +> *can_bitrate_t* - CAN bit-rate settings for arbitration phase and data phase |
| 4 | +
|
| 5 | +### SYNOPSIS |
| 6 | + |
| 7 | +**C/C++ Defines** |
| 8 | +```C++ |
| 9 | +#define CANBTR_INDEX_1M (0) |
| 10 | +#define CANBTR_INDEX_800K (-1) |
| 11 | +#define CANBTR_INDEX_500K (-2) |
| 12 | +#define CANBTR_INDEX_250K (-3) |
| 13 | +#define CANBTR_INDEX_125K (-4) |
| 14 | +#define CANBTR_INDEX_100K (-5) |
| 15 | +#define CANBTR_INDEX_50K (-6) |
| 16 | +#define CANBTR_INDEX_20K (-7) |
| 17 | +#define CANBTR_INDEX_10K (-8) |
| 18 | +``` |
| 19 | +<a id="can_bitrate_t"></a> |
| 20 | +<a id="can_speed_t"></a> |
| 21 | +**C/C++ Typedef** |
| 22 | +```C++ |
| 23 | +typedef union can_bitrate_t_ { |
| 24 | + int32_t index; |
| 25 | + struct { |
| 26 | + int32_t frequency; |
| 27 | + struct { |
| 28 | + uint16_t brp; |
| 29 | + uint16_t tseg1; |
| 30 | + uint16_t tseg2; |
| 31 | + uint16_t sjw; |
| 32 | + uint8_t sam; |
| 33 | + } nominal; |
| 34 | + struct { |
| 35 | + uint16_t brp; |
| 36 | + uint16_t tseg1; |
| 37 | + uint16_t tseg2; |
| 38 | + uint16_t sjw; |
| 39 | + } data; |
| 40 | + } btr; |
| 41 | +} can_bitrate_t; |
| 42 | +
|
| 43 | +typedef struct can_speed_t_ { |
| 44 | + struct { |
| 45 | + bool _reserved; |
| 46 | + float speed; |
| 47 | + float samplepoint; |
| 48 | + } nominal; |
| 49 | + struct { |
| 50 | + bool _reserved; |
| 51 | + float speed; |
| 52 | + float samplepoint; |
| 53 | + } data; |
| 54 | +} can_speed_t; |
| 55 | +``` |
| 56 | +**C/C++ Alias** |
| 57 | +```C++ |
| 58 | +typedef can_bitrate_t CANAPI_Bitrate_t; |
| 59 | + |
| 60 | +typedef can_speed_t CANAPI_BusSpeed_t; |
| 61 | +``` |
| 62 | +<a id="struct_bitrate"></a> |
| 63 | +<a id="struct_speed"></a> |
| 64 | +**Swift Type** |
| 65 | +```Swift |
| 66 | +public enum CiaIndex: Int { |
| 67 | + case Index1000kbps = 0 |
| 68 | + case Index800kbps = 1 |
| 69 | + case Index500kbps = 2 |
| 70 | + case Index250kbps = 3 |
| 71 | + case Index125kbps = 4 |
| 72 | + case Index100kbps = 5 |
| 73 | + case Index50kbps = 6 |
| 74 | + case Index20kbps = 7 |
| 75 | + case Index10kbps = 8 |
| 76 | +} |
| 77 | + |
| 78 | +public struct Bitrate { |
| 79 | + public struct Nominal { |
| 80 | + public var brp: UInt16 |
| 81 | + public var tseg1: UInt16 |
| 82 | + public var tseg2: UInt16 |
| 83 | + public var sjw: UInt16 |
| 84 | + public var sam: UInt8 |
| 85 | + } |
| 86 | + public struct DataPhase { |
| 87 | + public var brp: UInt16 |
| 88 | + public var tseg1: UInt16 |
| 89 | + public var tseg2: UInt16 |
| 90 | + public var sjw: UInt16 |
| 91 | + } |
| 92 | + public var frequency: Int32 |
| 93 | + public var nominal: Nominal |
| 94 | + public var data: DataPhase |
| 95 | +} |
| 96 | + |
| 97 | +public struct Speed { |
| 98 | + public struct Nominal { |
| 99 | + public var busSpeed: Float |
| 100 | + public var samplePoint: Float |
| 101 | + } |
| 102 | + public struct DataPhase { |
| 103 | + public var busSpeed: Float |
| 104 | + public var samplePoint: Float |
| 105 | + } |
| 106 | + public var nominal: Nominal |
| 107 | + public var data: DataPhase |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +### DESCRIPTION |
| 112 | + |
| 113 | +CAN API V3 provides the data type [can_bitrate_t](#can_bitrate_t) (**C/C++**) respectively [struct Bitrate](#struct_bitrate) (**Swift**) for adjusting the bit-rate of the CAN controller associated with a CAN channel. |
| 114 | +The data types contain the bit-timing settings for Classic CAN operation mode as well as for CAN FD operation mode. |
| 115 | +The bit-timing settings are based upon the CAN clock frequency of the CAN controller. |
| 116 | +The formula to calculate the bit-rate from frequency-based bit-timing settings is given by [(1)](#formula-1) |
| 117 | +and to calculated the sample-point within a bit by [(2)](#formula-2). |
| 118 | + |
| 119 | +Additionally CAN API V3 provides the data type [can_speed_t](#can_speed_t) (**C/C++**) respectively [struct Speed](#struct_speed) (**Swift**) for the data transmission speed (bit-rate in [bit/s]) and the sample-point in Classic CAN operation mode as well as in CAN FD operation mode. |
| 120 | +The data transmission speed is a read-only property that cannot be used for adjusting the bit-rate of the CAN controller. |
| 121 | + |
| 122 | +<a id="bitrate_frequency"></a> |
| 123 | +Field **frequency** (CAN clock) determines the operating frequency of the CAN controller in hertz (Hz). |
| 124 | +It is a crucial parameter as it determines the overall timing and data transmission speed on the CAN bus. |
| 125 | + |
| 126 | +<a id="bitrate_brp"></a> |
| 127 | +Field **brp** (bit-rate prescaler) determines the devider of the CAN clock. |
| 128 | +The division of the CAN clock by the bit-rate prescaler (BRP) results in the time quanta (TQ). |
| 129 | +Time quanta are discrete time intervals used to define various aspects of the bit timing on the CAN bus, such as the length of synchronization segment (SYNC_SEG), propagation segment (PROP_SEG), and phase segments (PHASE_SEG1 and PHASE_SEG2). |
| 130 | +The BRP allows for adjusting the bit-rate by dividing the CAN clock frequency accordingly. |
| 131 | + |
| 132 | +<a id="bitrate_tseg1"></a> |
| 133 | +Field **tseg1** (time segment 1) determines the length of the first part of the phase segment within a bit time. |
| 134 | +Specifically, it sets the duration during which the bit can be changed (dominant to recessive or vice versa) before the sample point. |
| 135 | +TSEG1, together with TSEG2, influences the overall bit time and bit-rate. |
| 136 | +TSEG1 is equivalent to sum of the propagation segment PROP_SEG and the phase segment PHASE_SEG1. |
| 137 | + |
| 138 | +<a id="bitrate_tseg2"></a> |
| 139 | +Field **tseg2** (time segment 2) determines the length of the second part of the phase segment within a bit time. |
| 140 | +It defines the time after the sample point where the bit level must remain stable for successful bit reception. |
| 141 | +TSEG2 is equivalent to the phase segment PHASE_SEG2. |
| 142 | + |
| 143 | +<a id="bitrate_sjw"></a> |
| 144 | +Field **sjw** (synchronization jump width) determines the maximum allowable variation in the synchronization segment (SYNC_SEG) to account for synchronization errors. |
| 145 | +In other words, SJW defines how much the bit timing can be adjusted to accommodate variations in clock synchronization between different nodes on the CAN bus. |
| 146 | + |
| 147 | +<a id="bitrate_sam"></a> |
| 148 | +Field **sam** (number of samples) determines how many times the CAN controller samples the bus to determine the bit's value during each bit time. |
| 149 | +It can be set to either single (1 sample per bit) or triple (3 samples per bit) sampling. |
| 150 | +Triple sampling is more robust in noisy environments but requires more processing resources. |
| 151 | +It depends on the used CAN controller if triple sampling is sopported. |
| 152 | + |
| 153 | +<a id="bitrate_index"></a> |
| 154 | +Field **index** (C/C++ only) determines an index to the [table with predefined bit-timings](#bit-timing-table) (Classic CAN only). |
| 155 | +The index to the bit-timing table must be given as a negative value, |
| 156 | +or 0 for the bit-rate of 1 MBit/s. |
| 157 | + |
| 158 | +#### CALCULATION OF BIT-RATES :id=formula-1 |
| 159 | + |
| 160 | +A bit-rate is calculated according to the following formula: |
| 161 | + |
| 162 | + bit-rate = frequency / (brp * (1 + tseg1 + tseg2)) |
| 163 | + |
| 164 | +#### CALCULATION OF SAMPLE-POINTS :id=formula-2 |
| 165 | + |
| 166 | +A sample-point is calculated according to the following formula: |
| 167 | + |
| 168 | + sample-point = (1 + tseg1) / (1 + tseg1 + tseg2) |
| 169 | + |
| 170 | +#### BIT-TIMING TABLE |
| 171 | + |
| 172 | +| Index | Bit-rate | Bit time | Sample-point recommendation | Remarks | |
| 173 | +| :---- | ---------: | -------: | :-------------------------: | :------ | |
| 174 | +| 0 | 1 MBit/s | 1.00 µs | 75% to 90% | | |
| 175 | +| 1 | 800 kBit/s | 1.25 µs | 75% to 90% | *Not supported by all devices* | |
| 176 | +| 2 | 500 kBit/s | 2.00 µs | 85% to 90% | | |
| 177 | +| 3 | 250 kBit/s | 4.00 µs | 85% to 90% | | |
| 178 | +| 4 | 125 kBit/s | 8.00 µs | 85% to 90% | | |
| 179 | +| 5 | 100 kBit/s | 10.0 µs | 85% to 90% | *Supported for compatibility* | |
| 180 | +| 6 | 50 kBit/s | 20.0 µs | 85% to 90% | | |
| 181 | +| 7 | 20 kBit/s | 50.0 µs | 85% to 90% | | |
| 182 | +| 8 | 10 kBit/s | 100.0 µs | 85% to 90% | | |
| 183 | + |
| 184 | +Bit-timing table according to CiA CANopen specification. |
| 185 | + |
| 186 | +#### BIT-TIMING SETTINGS FOR CAN CONTROLLER SJA1000 |
| 187 | + |
| 188 | +| Bit-rate | BRP | TSeg1 | TSeg2 | SJW | SAM | Sample-point | |
| 189 | +| ----------: | --: | ----: | ----: | --: | --: | :----------- | |
| 190 | +| 1 MBit/s | 1 | 5 | 2 | 1 | 0 | 75.0% | |
| 191 | +| 800 kBit/s | 1 | 7 | 2 | 1 | 0 | 80.0% | |
| 192 | +| 500 kBit/s | 1 | 13 | 2 | 1 | 0 | 87.5% | |
| 193 | +| 250 kBit/s | 2 | 13 | 2 | 1 | 0 | 87.5% | |
| 194 | +| 125 kBit/s | 4 | 13 | 2 | 1 | 0 | 87.5% | |
| 195 | +| 100 kBit/s | 5 | 13 | 2 | 2 | 0 | 87.5% | |
| 196 | +| 50 kBit/s | 10 | 13 | 2 | 2 | 0 | 87.5% | |
| 197 | +| 20 kBit/s | 25 | 13 | 2 | 2 | 0 | 87.5% | |
| 198 | +| 10 kBit/s | 50 | 13 | 2 | 2 | 0 | 87.5% | |
| 199 | +| 5 kBit/s | 64 | 16 | 8 | 2 | 0 | 68.0% | |
| 200 | + |
| 201 | +The CAN controller SJA1000 is running at 8.0 MHz. |
| 202 | + |
| 203 | +#### SYNTAX FOR BIT-RATE STRINGS USED BY CAN API V3 UTILITIES |
| 204 | + |
| 205 | +##### Classic CAN: |
| 206 | + f_clock_mhz=<frequency-in-MHz>,nom_brp=<brp>,nom_tseg1=<tseg1>,nom_tseg2=<tseg2>,nom_sjw=<sjw>,nom_sam=<sam> |
| 207 | +##### CAN FD: |
| 208 | + f_clock_mhz=<frequency-in-MHz>,nom_brp=<brp>,nom_tseg1=<tseg1>,nom_tseg2=<tseg2>,nom_sjw=<sjw>,data_brp=<brp>,data_tseg1=<tseg1>,data_tseg2=<tseg2>,data_sjw=<sjw> |
| 209 | + |
| 210 | +### SEE ALSO |
| 211 | + |
| 212 | +[can_start()](/reference/can_start#can_start) respectively [StartController()](/reference/can_start#startcontroller) \ |
| 213 | +[can_bitrate()](/reference/can_bitrate#can_bitrate) respectively [GetBitrate()](/reference/can_bitrate#getbitrate) and [GetBusSpeed()](/reference/can_bitrate#getbusspeed) |
| 214 | + |
| 215 | + |
| 216 | +[copyright](../copyright.md ':include') |
0 commit comments