-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
421 lines (361 loc) · 16.2 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<!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>STMG431 FDCAN 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">STM32G4 FDCAN</h2>
</div>
<div class="small-4 cell">
</div>
<!-- 1 -->
<div class="small-3 cell">
<label class="text-right middle">FDCAN Clock</label>
</div>
<div class="small-5 cell">
<input id="input_clock" type="number" placeholder="FDCAN Clock">
</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">FDCAN Clock Divider</label>
</div>
<div class="small-5 cell">
<select id="input_clock_divider">
<option value=1>FDCAN_CLOCK_DIV1</option>
<option value=2>FDCAN_CLOCK_DIV2</option>
<option value=4>FDCAN_CLOCK_DIV4</option>
<option value=6>FDCAN_CLOCK_DIV6</option>
<option value=8>FDCAN_CLOCK_DIV8</option>
<option value=10>FDCAN_CLOCK_DIV10</option>
<option value=12>FDCAN_CLOCK_DIV12</option>
<option value=14>FDCAN_CLOCK_DIV14</option>
<option value=16>FDCAN_CLOCK_DIV16</option>
<option value=18>FDCAN_CLOCK_DIV18</option>
<option value=14>FDCAN_CLOCK_DIV14</option>
<option value=16>FDCAN_CLOCK_DIV16</option>
<option value=18>FDCAN_CLOCK_DIV18</option>
<option value=20>FDCAN_CLOCK_DIV20</option>
<option value=22>FDCAN_CLOCK_DIV22</option>
<option value=24>FDCAN_CLOCK_DIV24</option>
<option value=26>FDCAN_CLOCK_DIV26</option>
<option value=28>FDCAN_CLOCK_DIV28</option>
<option value=30>FDCAN_CLOCK_DIV30</option>
</select>
</div>
<div class="small-4 cell">
</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">Nominal rate (CLASSIC CAN)</label>
</div>
<div class="small-5 cell">
<input id="input_nominal_rate" type="number" placeholder="Nominal rate (CLASSIC CAN)">
</div>
<div class="small-4 cell">
<div class="text-left middle">[kbit/s]</div>
</div>
<!-- 5 -->
<div class="small-3 cell">
<label class="text-right middle">Sampling point (CLASSIC CAN)</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_nominal"></span>
<span class="slider-fill" data-slider-fill></span>
</div>
</div>
<div class="cell small-1">
<input type="number" id="input_sampling_point_nominal">
</div>
<div class="small-4 cell">
<div class="text-left middle">[%]</div>
</div>
<!-- 6 -->
<div class="small-3 cell">
<label class="text-right middle">Data rate (FD CAN)</label>
</div>
<div class="small-5 cell">
<input id="input_data_rate" type="number" placeholder="Data rate (FD CAN)">
</div>
<div class="small-4 cell">
<div class="text-left middle">[kbit/s]</div>
</div>
<!-- 7 -->
<div class="small-3 cell">
<label class="text-right middle">Sampling point (FD CAN)</label>
</div>
<div class="small-4 cell">
<div class="slider" data-slider data-start="5" data-initial-start="50" data-step="5">
<span class="slider-handle" data-slider-handle role="slider" tabindex="1"
aria-controls="input_sampling_point_data"></span>
<span class="slider-fill" data-slider-fill></span>
</div>
</div>
<div class="cell small-1">
<input type="number" id="input_sampling_point_data">
</div>
<div class="small-4 cell">
<div class="text-left middle">[%]</div>
</div>
<!-- 8 -->
<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 (CLASSIC)</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>
<!-- table with nominal results -->
<hr>
<p>Table with <b>DATA (FD)</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-data">
</tbody>
</table>
<!-- note -->
<hr>
<p><b>NOTE:</b> In typical situation value of <i>Nominal Sync Jump Width</i> is equal to <i>Time Seg2</i> for
Nominal and Data segment</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 fdcan = {};
_my_.fdcan = fdcan;
fdcan.NominalPrescalerMax = 512;
fdcan.NominalTimeSeg1Max = 256;
fdcan.NominalTimeSeg2Max = 128;
//
fdcan.DataPrescalerMax = 32;
fdcan.DataTimeSeg1Max = 32;
fdcan.DataTimeSeg2Max = 16;
_my_.input_clock = document.getElementById("input_clock");
_my_.button_resolve = document.getElementById("button_resolve");
_my_.input_clock_divider = document.getElementById("input_clock_divider");
_my_.input_nominal_rate = document.getElementById("input_nominal_rate");
_my_.input_sampling_point_nominal = document.getElementById("input_sampling_point_nominal");
_my_.input_data_rate = document.getElementById("input_data_rate");
_my_.input_sampling_point_data = document.getElementById("input_sampling_point_data");
_my_.input_baud_rate_tollerance = document.getElementById("input_baud_rate_tollerance");
_my_.display_table_rows = 5;
_my_.input_clock.value = 80;
_my_.input_nominal_rate.value = 1000;
_my_.input_data_rate.value = 8000;
_my_.button_resolve.onclick = () => {
// 1
const clock = filterInt(_my_.input_clock.value);
_my_.input_clock.style.backgroundColor = isNaN(clock) ? "red" : "";
// 2
const baud_rate_tolerance = parseFloat(_my_.input_baud_rate_tollerance.value);
// 3
const clock_divider = parseInt(_my_.input_clock_divider.selectedOptions[0].value);
// 4
const nominal_rate = filterInt(_my_.input_nominal_rate.value);
_my_.input_nominal_rate.style.backgroundColor = isNaN(nominal_rate) ? "red" : "";
// 5
const sampling_point_nominal = parseInt(_my_.input_sampling_point_nominal.value);
// 6
const data_rate = filterInt(_my_.input_data_rate.value);
_my_.input_data_rate.style.backgroundColor = isNaN(data_rate) ? "red" : "";
// 7
const sampling_point_data = parseInt(_my_.input_sampling_point_data.value);
const tfdcan_tq_clk = clock * 1e6 / clock_divider;
//-----------------------------------------------------------------
// solve for nominal
_my_.results_nominal = [];
for (let prescaler = 1; prescaler < fdcan.NominalPrescalerMax; prescaler++) {
const ftq = tfdcan_tq_clk / prescaler;
const tq_per_bit = Math.round(ftq / (nominal_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_nominal / 100);
const tBS2 = tq_per_bit - tBS1;
// reduce by tSyncSeg
tBS1--;
// are segments in allowed limits
if (isNaN(tBS1) || (tBS1 < 1) || (tBS1 > fdcan.NominalTimeSeg1Max)) {
continue;
}
if (isNaN(tBS2) || (tBS2 < 1) || (tBS2 > fdcan.NominalTimeSeg2Max)) {
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 * nominal_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({
tfdcan_tq_clk,
rate: nominal_rate,
prescaler,
tq_per_bit,
tBS1,
tBS2,
freq_diff
});
}
// fill table with nominal
updateTable(sampling_point_nominal, _my_.results_nominal, document.getElementById("table-nominal"));
//-----------------------------------------------------------------
// solve for data
_my_.results_data = [];
for (let prescaler = 1; prescaler < fdcan.DataPrescalerMax; prescaler++) {
const ftq = tfdcan_tq_clk / prescaler;
const tq_per_bit = Math.round(ftq / (data_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_data / 100);
const tBS2 = tq_per_bit - tBS1;
// reduce by tSyncSeg
tBS1--;
// are segments in allowed limits
if (isNaN(tBS1) || (tBS1 < 1) || (tBS1 > fdcan.DataTimeSeg1Max)) {
continue;
}
if (isNaN(tBS2) || (tBS2 < 1) || (tBS2 > fdcan.DataTimeSeg2Max)) {
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 * data_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_data.push({
tfdcan_tq_clk,
rate: data_rate,
prescaler,
tq_per_bit,
tBS1,
tBS2,
freq_diff
});
}
// fill table with nominal
updateTable(sampling_point_data, _my_.results_data, document.getElementById("table-data"));
};
//
//
//
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 + 1) * 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>