-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
306 lines (259 loc) · 11.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="author" content="Pawel Hryniszak">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/jpg" href="./canfd.jpg">
<title>MCP2515 CAN SOLVER</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/foundation-sites@6/dist/css/foundation.min.css">
</head>
<body>
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="small-5 cell small-offset-3">
<h2 class="text-center">MCP2515 CAN SOLVER</h2>
</div>
<div class="small-4 cell">
</div>
<!-- 1 -->
<div class="small-3 cell">
<label class="text-right middle">FOSC (xtal)</label>
</div>
<div class="small-5 cell">
<input id="input_clock" type="number" placeholder="Fosc (xtal)">
</div>
<div class="small-4 cell">
<div class="text-left middle">[MHz]</div>
</div>
<!-- 2 -->
<div class="small-3 cell">
<label class="text-right middle">CAN baud rate</label>
</div>
<div class="small-5 cell">
<input id="input_baud_rate" type="number" placeholder="CAN baud rate">
</div>
<div class="small-4 cell">
<div class="text-left middle">[kbit/s]</div>
</div>
<!-- 3 -->
<div class="small-3 cell">
<label class="text-right middle">Baudrate tolerance</label>
</div>
<div class="small-4 cell">
<div class="slider" data-slider data-start="0" data-initial-start="0" data-step="0.5" data-end="1.5">
<span class="slider-handle" data-slider-handle role="slider" tabindex="1"
aria-controls="input_baud_rate_tollerance"></span>
<span class="slider-fill" data-slider-fill></span>
</div>
</div>
<div class="cell small-1">
<input type="number" id="input_baud_rate_tollerance">
</div>
<div class="small-4 cell">
<div class="text-left middle">[%]</div>
</div>
<!-- 4 -->
<div class="small-3 cell">
<label class="text-right middle">Sampling point</label>
</div>
<div class="small-4 cell">
<div class="slider" data-slider data-start="5" data-initial-start="75" data-step="5">
<span class="slider-handle" data-slider-handle role="slider" tabindex="1"
aria-controls="input_sampling_point"></span>
<span class="slider-fill" data-slider-fill></span>
</div>
</div>
<div class="cell small-1">
<input type="number" id="input_sampling_point">
</div>
<div class="small-4 cell">
<div class="text-left middle">[%]</div>
</div>
<!-- 5 -->
<div class="small-5 cell small-offset-3">
<button id="button_resolve" class="submit success button expanded">SOLVE</button>
</div>
</div>
<!-- table with nominal results -->
<hr>
<p>Table with <b>NOMINAL</b> bit time:</p>
<table>
<thead>
<tr>
<th>Baudrate</th>
<th>Prescaler</th>
<th>Time quanta</th>
<th>Time Seg1</th>
<th>Time Seg2</th>
<th>Sample point [%]</th>
<th>Sample point diff [%]</th>
</tr>
</thead>
<tbody id="table-nominal">
</tbody>
</table>
<!-- note -->
<hr>
<p>
<b>NOTE:</b><br><i>Time Seg2</i> is equal to <i>PhaseSeg2</i><br>
<i>Time Seg1</i> is sum of <i>SyncSeg</i> equal 1, <i>PropSeg</i> and
<i>PhaseSeg1</i><br><br>
<i>DS20001801J-page 43</i>: For example, assuming that a 125 kHz CAN baud rate
with FOSC = 20 MHz is desired:
TOSC = 50 ns, choose BRP[5:0] = 04h, then TQ = 500 ns.
To obtain 125 kHz, the bit time must be 16 TQs.
Typically, the sampling of the bit should take place at
about 60-70% of the bit time, depending on the system
parameters. Also, typically, the TDELAY is 1-2 TQs.
SyncSeg = 1 TQ and PropSeg = 2 TQs. So setting
PS1 = 7 TQs would place the sample at 10 TQs after the
transition. This would leave 6 TQs for PS2.
Since PS2 is 6, according to the rules, SJW could be a
maximum of 4 TQs. However, a large SJW is typically
only necessary when the clock generation of the different nodes is inaccurate or unstable, such as using
ceramic resonators. So a SJW of 1 is usually enough.
</p>
<!-- footer -->
<hr>
<div class="footer">
<p> Create by : <a href="https://github.com/phryniszak" target="_blank">phryniszak</a></p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/combine/npm/jquery@3/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/what-input@5/dist/what-input.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/foundation-sites@6/dist/js/foundation.min.js"></script>
<script>
$(document).foundation();
// globals
const _my_ = {};
globalThis._my_ = _my_;
const can = {};
_my_.can = can;
can.timing_min = {
sjw: 0x01,
prop_seg: 0x01,
phase_seg1: 0x01,
phase_seg2: 0x02,
prescaler: 0x01
};
can.timing_max = {
sjw: 0x04,
prop_seg: 0x08,
phase_seg1: 0x08,
phase_seg2: 0x08,
prescaler: 0x40
};
_my_.input_clock = document.getElementById("input_clock");
_my_.input_baud_rate = document.getElementById("input_baud_rate");
_my_.input_baud_rate_tollerance = document.getElementById("input_baud_rate_tollerance");
_my_.input_sampling_point = document.getElementById("input_sampling_point");
_my_.button_resolve = document.getElementById("button_resolve");
_my_.display_table_rows = 5;
_my_.input_clock.value = 16;
_my_.input_baud_rate.value = 1000;
_my_.button_resolve.onclick = () => {
// 1
const clock = filterInt(_my_.input_clock.value);
_my_.input_clock.style.backgroundColor = isNaN(clock) ? "red" : "";
// 2
const baud_rate = filterInt(_my_.input_baud_rate.value);
_my_.input_baud_rate.style.backgroundColor = isNaN(baud_rate) ? "red" : "";
// 3
const baud_rate_tolerance = parseFloat(_my_.input_baud_rate_tollerance.value);
// 4
const sampling_point = parseInt(_my_.input_sampling_point.value);
const clock_divider = 2;
const can_tq_clk = clock * 1e6 / clock_divider;
//-----------------------------------------------------------------
// solve for nominal
_my_.results_nominal = [];
for (let prescaler = can.timing_min.prescaler; prescaler < can.timing_max.prescaler; prescaler++) {
const ftq = can_tq_clk / prescaler;
const tq_per_bit = Math.round(ftq / (baud_rate * 1000));
if (isNaN(ftq) || isNaN(tq_per_bit)) {
continue;
}
// can not find working solution with less then 4 tq per bit
// NominalTimeSeg1Min(1) + NominalTimeSeg2Min(1) + tSyncSeg(1)
if (tq_per_bit < 4) {
continue;
}
let tBS1 = Math.round(tq_per_bit * sampling_point / 100);
const tBS2 = tq_per_bit - tBS1;
// reduce by tSyncSeg = 1
// tBS1--;
// are segments in allowed limits
if (isNaN(tBS1) ||
(tBS1 < (can.timing_min.phase_seg1 + can.timing_min.prop_seg)) ||
(tBS1 > (can.timing_max.phase_seg1 + can.timing_max.prop_seg))) {
continue;
}
// are segments in allowed limits
if (isNaN(tBS2) || (tBS2 < can.timing_min.phase_seg2) || (tBS2 > can.timing_max.phase_seg2)) {
continue;
}
// A maximum node-to-node oscillator variation of 1.7% is allowed.
// https://www.calculatorsoup.com/calculators/algebra/percent-difference-calculator.php
let tfq_real = tq_per_bit * baud_rate * 1000;
let freq_diff = (ftq - tfq_real) * 2 * 100 / (ftq + tfq_real);
// is baudrate in limits
if (Math.abs(freq_diff) > baud_rate_tolerance) {
continue;
}
_my_.results_nominal.push({
can_tq_clk,
rate: baud_rate,
prescaler,
tq_per_bit,
tBS1,
tBS2,
freq_diff
});
}
// fill table with nominal
updateTable(sampling_point, _my_.results_nominal, document.getElementById("table-nominal"));
};
//
//
//
function updateTable(sampling_point, results, tableRef) {
// hide table when we don't have results
// tableRef.parentElement.style.display = (_my_.results_nominal.length > 0) ? "" : "none";
// remove previous results
while (tableRef.rows.length > 0) {
tableRef.deleteRow(0);
}
// now add new results
let rows = 0
results.forEach(res => {
if (rows < _my_.display_table_rows) {
console.log(res);
// Insert a row at the end of the table
let newRow = tableRef.insertRow(-1);
newRow.insertCell(0).appendChild(document.createTextNode(res.rate));
newRow.insertCell(1).appendChild(document.createTextNode(res.prescaler));
newRow.insertCell(2).appendChild(document.createTextNode(res.tq_per_bit));
newRow.insertCell(3).appendChild(document.createTextNode(res.tBS1));
newRow.insertCell(4).appendChild(document.createTextNode(res.tBS2));
const diff = Math.round(res.tBS1 * 100 / res.tq_per_bit);
newRow.insertCell(5).appendChild(document.createTextNode(diff));
newRow.insertCell(6).appendChild(document.createTextNode(sampling_point - diff));
rows++;
}
});
}
//
//
//
function filterInt(value) {
if (/^[-+]?(\d+|Infinity)$/.test(value)) {
return Number(value)
} else {
return NaN
}
}
</script>
</body>
</html>