MCPcopy Create free account
hub / github.com/ArduPilot/ArduRemoteID / computeTimings

Method computeTimings

RemoteIDModule/CANDriver.cpp:100–219  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

98}
99
100bool CANDriver::computeTimings(uint32_t target_bitrate, Timings& out_timings)
101{
102 if (target_bitrate < 1) {
103 return false;
104 }
105
106 /*
107 * Hardware configuration
108 */
109 const uint32_t pclk = 100000;
110
111 static const int MaxBS1 = 16;
112 static const int MaxBS2 = 8;
113
114 /*
115 * Ref. "Automatic Baudrate Detection in CANopen Networks", U. Koppe, MicroControl GmbH & Co. KG
116 * CAN in Automation, 2003
117 *
118 * According to the source, optimal quanta per bit are:
119 * Bitrate Optimal Maximum
120 * 1000 kbps 8 10
121 * 500 kbps 16 17
122 * 250 kbps 16 17
123 * 125 kbps 16 17
124 */
125 const int max_quanta_per_bit = (target_bitrate >= 1000000) ? 10 : 17;
126
127 static const int MaxSamplePointLocation = 900;
128
129 /*
130 * Computing (prescaler * BS):
131 * BITRATE = 1 / (PRESCALER * (1 / PCLK) * (1 + BS1 + BS2)) -- See the Reference Manual
132 * BITRATE = PCLK / (PRESCALER * (1 + BS1 + BS2)) -- Simplified
133 * let:
134 * BS = 1 + BS1 + BS2 -- Number of time quanta per bit
135 * PRESCALER_BS = PRESCALER * BS
136 * ==>
137 * PRESCALER_BS = PCLK / BITRATE
138 */
139 const uint32_t prescaler_bs = pclk / target_bitrate;
140
141 /*
142 * Searching for such prescaler value so that the number of quanta per bit is highest.
143 */
144 uint8_t bs1_bs2_sum = uint8_t(max_quanta_per_bit - 1);
145
146 while ((prescaler_bs % (1 + bs1_bs2_sum)) != 0) {
147 if (bs1_bs2_sum <= 2) {
148 return false; // No solution
149 }
150 bs1_bs2_sum--;
151 }
152
153 const uint32_t prescaler = prescaler_bs / (1 + bs1_bs2_sum);
154 if ((prescaler < 1U) || (prescaler > 1024U)) {
155 return false; // No solution
156 }
157

Callers

nothing calls this directly

Calls 2

BsPairClass · 0.85
isValidMethod · 0.80

Tested by

no test coverage detected