-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathatpackrun.js
583 lines (543 loc) · 18.1 KB
/
atpackrun.js
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
* AtPack visualizer for Atmel (Microchip) MCU
* Copyright (C) 2018 B. Stultiens
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* atpacks.atpack parsed structure:
* atpack: [
* {
* family, // Family name
* version, // String 1.2.3
* vmaj, // Split version (numeric) major
* vmin, // minor
* vpnt, // point
* release, // Latest version release message
* devices: [ // Devices array
* {
* name, // Device name
* description, // Device's description
* atdf, // Device's atdf XML file (atmel device description)
* pic, // Device's pic XML file (microchip device description)
book: [ // Documentation array
* {
* name, // HTML link address
* title, // Link description
* }
* ]
* },
* ...
* ]
* }
* ...
* }
*/
// This is executed when the page is loaded
// - load all pdsc files
// - update the html to show the loaded files
$.when($.ready).then(function() {
$("#avrpacks").empty();
$("#avrpacks").append('<option value="-1">- select a pack -</option>');
atpacks.atpack = new Array(); // Where we load the pdsc files
// Load all atpack XML files
var fetchFiles = function(files) {
var fetchXml = function(pdsc, n) {
//console.log(Date.now(), "Request pack ", n, pdsc);
var jax = $.ajax({
url: pdsc,
method: "GET",
dataType: "xml",
}).done(function(doc) {
//console.log(Date.now(), "Loaded read ", n, pdsc);
var pack = new Object();
pack.version = "";
pack.vmaj = 0;
pack.vmin = 0;
pack.vpnt = 0;
pack.devices = new Array();
pack.name = $("package name", doc).text();
pack.description = $("package description", doc).text();
pack.family = $('package family', doc).attr("Dfamily");
// Find the latest release version
$("package releases release", doc).each(function(i, item) {
var ver = item.getAttribute("version");
var v = ver.match(/(\d+)\.(\d+)\.(\d+)/);
if(v && +v[1] >= pack.vmaj && +v[2] >= pack.vmin && +v[3] > pack.vpnt) {
pack.version = ver;
pack.vmaj = +v[1];
pack.vmin = +v[2];
pack.vpnt = +v[3];
pack.release = $(item).text();
}
});
// Get all devices
$('package family device', doc).each(function(i, item) {
var dev = new Object();
dev.name = item.getAttribute("Dname");
dev.description = $('description', item).first().text();
dev.atdf = $('environment[name="atmel"] at\\:extension at\\:atdf', item).attr("name");
dev.pic = $('environment[name="microchip"] mchp\\:extension mchp\\:pic', item).attr("name");
dev.book = new Array();
$('book', item).each(function(j, jtem) {
var book = new Object();
book.name = jtem.getAttribute('name');
book.title = jtem.getAttribute('title');
dev.book.push(book);
});
pack.devices.push(dev);
});
atpacks.atpack[n] = pack;
// Sort the devices list based on the numerical value of the type
pack.devices.sort(function(a, b) {
var am = a.name.match(/^(AT90\D*|ATmega|ATtiny|ATxmega|ATA|AT32UCS3.)(\d+)(.*)/);
var bm = b.name.match(/^(AT90\D*|ATmega|ATtiny|ATxmega|ATA|AT32UCS3.)(\d+)(.*)/);
if(!am || !bm)
return a.name.localeCompare(b.name);
if(am[1] == bm[1]) {
if(am[2] == bm[2])
return am[3].localeCompare(bm[3]);
return +am[2] - bm[2];
} else
return am[1].localeCompare(bm[1]);
});
//console.log(Date.now(), "Loaded pack ", n, atpacks.atpack[n].name);
});
return jax;
};
return $.map(files, fetchXml);
};
var promises = fetchFiles(atpacks.base); // Actually get them
$.when.apply(null, promises).then(function() { // Now handle the post-load operations
//console.log(Date.now(), "Loaded all packs");
$.each(atpacks.atpack, function(i, item) {
$("#avrpacks").append("<option value=\"" + i + "\">" + i + " - " + item.family + " (" + item.version + ")</option>");
});
$("#avrpacks").change(function() {
selectAtPack($("#avrpacks option:selected").val());
});
$("#avrdevices").change(function() {
selectDevice($("#avrdevices option:selected").val());
});
// Trigger a change on the show parts checkboxes to format the following table
$(".showpart").trigger("change");
}, function() {
console.log("Fetching failed", this, arguments);
});
});
var currentDevice = new Object(); // Ref to atdf, fuses, lockbits, signature and other stuff
function htmlEscape(s)
{
return s.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
}
function dirname(path)
{
var i;
if(path.length <= 0 || (i = path.lastIndexOf("/")) < 0)
return "";
return path.substr(0, i);
}
// When a device change occurs, clean out the previous data
function clearDetails()
{
$("#device").html("");
$(".variantsgen").remove();
$(".memoriesgen").remove();
$(".booksgen").remove();
$(".modulesgen").remove();
$(".canvasdivgen").remove();
$("#interface").empty();
$("#fusetable").empty();
$("#lockbittable").empty();
currentDevice = new Object();
}
// Top level AtPack selection
function selectAtPack(index)
{
// Clear the device-list
$("#avrdevices").empty();
$("#avrdevices").append('<option value="-1">- select a device -</option>');
clearDetails();
// Only do a fill of the device-list if we have a pack
if(!atpacks.atpack[index]) {
return;
}
$.each(atpacks.atpack[index].devices, function(i, item) {
var name = item.name
var desc = htmlEscape(item.description);
var descs = desc;
if(descs.length > 100)
descs = desc.substr(0, 100) + "...";
$("#avrdevices").append("<option title=\"" + desc + "\" value=\"" + i + "\">" + name + " - " + descs + "</option>");
});
}
// Count the # of ones in a values
function countBits(val)
{
var n = 0;
while(val > 0) {
if(val & 1)
n++;
val >>= 1;
}
return n;
}
// Count how far to shift to have bit 0 as a one
function countShifts(val)
{
var n = 0;
while(val > 0 && !(val & 1)) {
val >>= 1;
n++;
}
return n;
}
// Fake mask value based on word-size if none supplied
function getOnes(n)
{
switch(n) {
case 1: return 0xff;
case 2: return 0xffff;
case 3: return 0xffffff;
case 4: return 0xffffffff;
default: return -1;
}
}
/*
* Build an object to describe a module's bit-layout:
* fuses = {
* type, // Initial type
* regorder[], // offsets with reg's name as content
* regs: {
* "EXTENDED": {
* caption,
* offset,
* size,
* hasinitval, // true if initval is actually defined in source
* initval,
* bitfieldmask, // Mask of bitfields combined
* bitfield: [
* { caption, // Description of the bitfield
* mask, // Bitmask
* nbits, // calculated # of bits in mask
* nshift, // calculated from bits in mask
* name, // Field name
* values // Value enumeration reference ("ENUM_NAME" or null if none)
* },
* ...
* ]
* },
* ...
* },
* vals: {
* "ENUM_NAME": [
* {
* caption, // Description of value
* name, // Reference name
* value, // Bitfield value
* },
* ...
* ],
* ...
* }
* }
*/
function buildBitTable(atdf, modname)
{
// name^ because sometimes it is "FUSE" or "FUSES" and "LOCKBIT" or "LOCKBITS"
var reggroup = $('peripherals module instance[name^="'+modname+'"] register-group', atdf);
if(reggroup.length < 1)
return false;
var bt = new Array();
$.each(reggroup, function(i, item) {
bt.push(buildBitTableInstance(atdf, item));
});
return bt;
}
function buildBitTableInstance(atdf, reggroup)
{
var instname;
if(reggroup.hasAttribute("name-in-module"))
instname = reggroup.getAttribute("name-in-module");
else
instname = reggroup.getAttribute("name");
var modname = reggroup.parentNode.parentNode.getAttribute("name");
var regmod = $('modules module[name="'+modname+'"]', atdf)[0];
var regs = $('register-group[name="'+instname+'"] register', regmod);
var vals = $('value-group', regmod);
var fuses = new Object();
fuses.name = instname;
fuses.regs = new Object();
fuses.vals = new Object();
fuses.regorder = new Array();
fuses.type = modname;
$.each(regs, function(i, item) {
var fname = item.getAttribute("name");
fuses.regs[fname] = new Object();
fuses.regs[fname].caption = item.getAttribute("caption");
fuses.regs[fname].offset = parseInt(item.getAttribute("offset"));
fuses.regorder[fuses.regs[fname].offset] = fname;
fuses.regs[fname].size = parseInt(item.getAttribute("size"));
fuses.regs[fname].bitfieldmask = -1;
fuses.regs[fname].bitfield = new Array();
$('bitfield', item).each(function(j, jtem) {
var bf = new Object();
bf.name = jtem.getAttribute("name");
if(jtem.hasAttribute("caption"))
bf.caption = jtem.getAttribute("caption");
else
bf.caption = bf.name;
bf.mask = parseInt(jtem.getAttribute("mask"));
fuses.regs[fname].bitfieldmask ^= bf.mask;
bf.nbits = countBits(bf.mask);
bf.nshift = countShifts(bf.mask);
bf.values = jtem.getAttribute("values");
fuses.regs[fname].bitfield.push(bf);
});
fuses.regs[fname].bitfieldmask ^= -1;
fuses.regs[fname].hasinitval = item.hasAttribute("initval");
if(fuses.regs[fname].hasinitval)
fuses.regs[fname].initval = parseInt(item.getAttribute("initval"));
else
fuses.regs[fname].initval = getOnes(fuses.regs[fname].size) & fuses.regs[fname].bitfieldmask;
});
$.each(vals, function(i, item) {
var vname = item.getAttribute("name");
fuses.vals[vname] = new Array();
$('value', item).each(function(j, jtem) {
var vobj = new Object();
vobj.caption = jtem.getAttribute("caption");
vobj.name = jtem.getAttribute("name");
vobj.value = parseInt(jtem.getAttribute("value"));
fuses.vals[vname].push(vobj);
});
});
return fuses;
}
function formatBits(val, n)
{
var s = "";
while(n) {
s = ((val & 1) ? '1' : '0') + s;
val >>= 1;
n--;
}
return s;
}
function formatHexByte(val)
{
if(val < 16)
return '0' + val.toString(16);
else
return val.toString(16);
}
function formatHex(val, size)
{
if(size < 1)
size = 1;
else if(size > 4)
size = 4;
var s = '';
while(size--) {
s = formatHexByte(val & 0xff) + s;
val >>= 8;
}
return '0x' + s;
}
function changeBitfieldSel(obj, spantag, mask)
{
var s = $(spantag).first().text();
var v = parseInt(s);
v &= ~mask;
v |= parseInt($("option:selected", obj).first().val());
$(spantag).text(formatHex(v, (s.length-2)/2));
}
function changeBitfieldChk(obj, spantag, mask)
{
var s = $(spantag).first().text();
var v = parseInt(s);
if(obj.checked)
v &= ~mask;
else
v |= parseInt(obj.getAttribute("value"));
$(spantag).text(formatHex(v, (s.length-2)/2));
}
function changeShow(obj, tag, show)
{
$(tag).css('display', obj.checked ? show : 'none');
}
function buildSelect(modregtable, bitfield, initval, spantag)
{
var s = '<select class="input_'+modregtable.type+'" name="'+bitfield.name+'" onChange="changeBitfieldSel(this,\'#'+spantag+'\','+bitfield.mask+')">';
var bitfieldmask = (initval & bitfield.mask) >> bitfield.nshift;
$.each(modregtable.vals[bitfield.values], function(i, item) {
var bits = formatBits(item.value, bitfield.nbits)
var sel = item.value == bitfieldmask ? ' selected="1"' : '';
s += '<option name="'+item.name+'" value="'+(item.value << bitfield.nshift)+'"'+sel+'>'+item.caption+' ['+bits+']</option>';
});
return s + '</select>';
}
function buildModuleTable(htmltable, modregtable, caption)
{
if(caption !== false) {
$(htmltable).append('<tr><th colspan="2">'+caption+'</th></tr>');
}
for(var r = modregtable.regorder.length - 1; r >= 0; r--) {
var i = modregtable.regorder[r];
var item = modregtable.regs[modregtable.regorder[r]];
if(!item)
continue; // Some devices lack an offset
var spantag = "value_" + i;
var inithex = formatHex(item.initval, item.size);
var s = '<tr><th class="al" colspan="2">' + i + ' (<small>offset:'+formatHex(item.offset, item.size)+'</small>) ' + item.caption;
if(0 == item.bitfield.length || (item.bitfield.length == 1 && !modregtable.vals[item.bitfield[0].values] && item.bitfield[0].mask == getOnes(item.size)))
s += '</th></tr><tr><td colspan="2">(value-field)</td>';
else {
s += ' (';
if(item.hasinitval)
s += 'default: ' + inithex + ', ';
s += 'current: <span id="'+spantag+'">'+inithex+'</span>)</th>';
}
$(htmltable).append(s + '</tr>');
$.each(item.bitfield, function(j, jtem) {
if(jtem.nbits == 1 && !jtem.values) {
var sel = (item.initval & jtem.mask) == 0 ? ' checked="1"' : '';
$(htmltable).append('<tr><td>'+jtem.caption+'</td><td><input type="checkbox" class="input_'+modregtable.type+'" onChange="changeBitfieldChk(this,\'#'+spantag+'\','+jtem.mask+')" name="'+jtem.name+'" value="'+jtem.mask+'"'+sel+'/></td></tr>');
} else if(jtem.values && modregtable.vals[jtem.values]) {
$(htmltable).append('<tr><td>'+jtem.caption+'</td><td>'+buildSelect(modregtable, jtem, item.initval, spantag)+'</td></tr>');
} else {
$(htmltable).append('<tr><td>'+jtem.caption+'</td><td>(value-field, mask:'+formatHex(jtem.mask, item.size)+')</td></tr>');
}
});
}
$(".input_" + modregtable.type).trigger("change");
}
// When a new device is selected
// - clear the old device data
// - load the new atdf file
// - update the html with the new device's data
function selectDevice(devidx)
{
clearDetails();
var apidx = $("#avrpacks option:selected").val();
var ap = atpacks.atpack[apidx];
if(apidx < 0 || !ap.devices[devidx])
return;
var dev = ap.devices[devidx];
$("#device").html(dev.name + '<br />' + htmlEscape(dev.description));
$.each(dev.book, function(i, item) {
$("#bookshead").parent().append('<tr class="booksgen"><td><a href="'+item.name+'">'+item.title+'</a></td></tr>');
});
// Get the device's atdf file and use it
var pkgs = new Map();
$.get(dirname(atpacks.base[apidx]) + "/" + dev.atdf, {}, function(atdf) {
currentDevice.atdf = atdf;
currentDevice.name = dev.name;
// Get the (package, temp and Vcc) variants of the device
var vars = $("variant", atdf);
$.each(vars, function(i, item) {
var oc = item.getAttribute("ordercode");
var pa = item.getAttribute("package");
pkgs.set(pa, item.getAttribute("pinout"));
var sm = (item.getAttribute("speedmax") / 1000000) + " MHz";
var vi = item.getAttribute("vccmin");
var va = item.getAttribute("vccmax");
var ti = item.getAttribute("tempmin");
var ta = item.getAttribute("tempmax");
$("#variantshead").parent().append('<tr class="variantsgen"><td>'+oc+'</td><td>'+pa+'</td><td class="ar">'+sm+'</td><td class="ar">'+vi+'</td><td class="ar">'+va+'</td><td class="ar">'+ti+'</td><td class="ar">'+ta+'</td></tr>');
});
// Get the programming interfaces
var s = "";
var ifs = $("interface", atdf);
$.each(ifs, function(i, item) { s += " " + item.getAttribute("name"); });
$("#interface").html('<tr><td>Programmer</td><td>'+s+'</td></tr>');
// Get the device signature(s)
var sig = $('property-groups property-group[name="SIGNATURES"] property', atdf);
currentDevice.signature = new Array();
currentDevice.chipid = new Array();
currentDevice.jtagid = false;
$.each(sig, function(i, item) {
var m;
var name = item.getAttribute("name");
var val = item.getAttribute("value");
if(name == "JTAGID") {
currentDevice.jtagid = val;
} else if((m = name.match(/^SIGNATURE(\d*)/))) {
currentDevice.signature[+m[1]] = val;
} else if(name.match(/^CHIPID.*/)) {
var id = new Object();
id.name = name;
id.value = val;
currentDevice.chipid.push(id);
}
});
if(currentDevice.jtagid !== false)
$("#interface").append('<tr><td>JTAG ID</td><td>'+currentDevice.jtagid+'</td></tr>');
$.each(currentDevice.signature, function(i, item) {
$("#interface").append('<tr><td>Signature '+i+'</td><td>'+item+'</td></tr>');
});
$.each(currentDevice.chipid, function(i, item) {
$("#interface").append('<tr><td>'+item.name+'</td><td>'+item.value+'</td></tr>');
});
// Get the address space layout
var mems = $("address-space", atdf);
$.each(mems, function(i, item) {
var segs = $("memory-segment", item);
var t = "";
var rsp = "";
if(segs.length > 1) {
rsp = ' rowspan="'+(segs.length+1)+'"';
$.each(segs, function(j, jtem) {
t += '<tr class="memoriesgen">';
t += '<td class="ar">' + jtem.getAttribute("start") + '</td>';
t += '<td class="ar">' + jtem.getAttribute("size") + '</td>';
t += '<td>' + jtem.getAttribute("name") + '</td>';
t += '</tr>';
});
}
var ty = item.getAttribute("name");
var st = item.getAttribute("start");
var si = item.getAttribute("size");
$("#memorieshead").parent().append('<tr class="memoriesgen"><td'+rsp+'>'+ty+'</td><td class="ar">'+st+'</td><td class="ar">'+si+'</td></tr>' + t);
});
// Get the modules build into the device
var mods = $("instance", atdf);
var lines = new Array();
$.each(mods, function(i, item) {
var na = item.getAttribute("name");
var ca = item.getAttribute("caption");
if(!ca) ca = "";
lines.push('<tr class="modulesgen"><td>'+na+'</td><td>'+ca+'</td></tr>');
});
lines.sort();
for(var i = 0; i < lines.length; i++)
$("#moduleshead").parent().append(lines[i]);
// Register device fuses and lock-bits in global data
currentDevice.fuses = buildBitTable(atdf, "FUSE");
currentDevice.lockbits = buildBitTable(atdf, "LOCKBIT");
// Build tables
if(currentDevice.fuses.length) {
$.each(currentDevice.fuses, function(i, item) {
buildModuleTable("#fusetable", item, currentDevice.fuses.length > 1 ? item.name : false);
});
}
if(currentDevice.lockbits.length) {
$.each(currentDevice.lockbits, function(i, item) {
buildModuleTable("#lockbittable", item, currentDevice.lockbits.length > 1 ? item.name : false);
});
}
// Draw the packages
drawPkgs(dev.name, pkgs, atdf);
}, "xml");
}