Subversion Repositories HomeAutomation

Rev

Rev 1063 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1063 Rev 1065
Line 2... Line 2...
2
const MAX_SIZE = 524288;
2
const MAX_SIZE = 524288;
3
 
3
 
-
 
4
/* Call constructor with the contents of an Intel Hex file (rows as arrays)
-
 
5
   http://en.wikipedia.org/wiki/.hex
-
 
6
*/
4
function IntelHex(hexLines)
7
function IntelHex(hexLines)
5
{
8
{
6
//print("constructor of IntelHex\n");
-
 
7
    this.myValid = false;
9
    this.myValid = false;
8
    this.myAddrLower = 0x7FFFFFFF;
10
    this.myAddrLower = 0x7FFFFFFF;
9
    this.myAddrUpper = 0;
11
    this.myAddrUpper = 0;
10
    this.myHexData = new Array(MAX_SIZE);
12
    this.myHexData = new Array(MAX_SIZE);
11
    for (var i = 0; i < MAX_SIZE; i++) this.myHexData[i] = 0xFF;
13
    for (var i = 0; i < MAX_SIZE; i++) this.myHexData[i] = 0xFF;
Line 14... Line 16...
14
    var lineAddrHigh = 0;
16
    var lineAddrHigh = 0;
15
    var lineAddr = 0;
17
    var lineAddr = 0;
16
    var lineCode = 0;
18
    var lineCode = 0;
17
    var fullAddr = 0;
19
    var fullAddr = 0;
18
 
20
   
-
 
21
    /* Parse hexfile to binary (array of bytes) */
19
    for (var n = 0; n < hexLines.length; n++)
22
    for (var n = 0; n < hexLines.length; n++)
20
    {
23
    {
21
        if (hexLines[n].length>=11 && hexLines[n].substr(0,1)==":") {
24
        if (hexLines[n].length>=11 && hexLines[n].substr(0,1)==":") {
22
            lineDataCount = hex2uint(hexLines[n].substr(1,2));
25
            lineDataCount = hex2uint(hexLines[n].substr(1,2));      /* Byte count */
23
            lineAddr =  hex2uint(hexLines[n].substr(3,4)) & 65535;   
26
            lineAddr =  hex2uint(hexLines[n].substr(3,4)) & 65535;  /* Address */
24
            lineCode = hex2uint(hexLines[n].substr(7,2));  
27
            lineCode = hex2uint(hexLines[n].substr(7,2));           /* Record type */
25
            fullAddr = (lineAddrHigh * 65536) + lineAddr;
28
            fullAddr = (lineAddrHigh * 65536) + lineAddr;
26
 
29
 
27
            switch(lineCode)
30
            switch(lineCode)
28
            {
31
            {
29
                case 0:
32
                case 0: /* If Data record */
30
                    if ((fullAddr + lineDataCount - 1) > this.myAddrUpper) this.myAddrUpper = fullAddr + lineDataCount - 1;
33
                    if ((fullAddr + lineDataCount - 1) > this.myAddrUpper) this.myAddrUpper = fullAddr + lineDataCount - 1;
31
                    if (lineCode == 0 && fullAddr < this.myAddrLower) this.myAddrLower = fullAddr;
34
                    if (lineCode == 0 && fullAddr < this.myAddrLower) this.myAddrLower = fullAddr;
32
                    for (var j = 0; j < lineDataCount; j++) {
35
                    for (var j = 0; j < lineDataCount; j++) {
33
                        this.myHexData[fullAddr] = hex2uint(hexLines[n].substr(j * 2 + 9, 2));
36
                        this.myHexData[fullAddr] = hex2uint(hexLines[n].substr(j * 2 + 9, 2));
34
                        //this.myHexData[fullAddr] &= 255;
-
 
35
                        fullAddr++;
37
                        fullAddr++;
36
                    }
38
                    }
37
//print("found a record of type 0, fulladdr: "+fullAddr+"\nhexLines.length: "+hexLines.length+"\n"+hexLines[n]+"\n");
-
 
38
                    break;
39
                    break;
39
                case 4:
40
                case 4: /* If Extended Linear Address Record */
40
                    lineAddrHigh = hex2uint(hexLines[n].substr(9, 4)) & 65535;
41
                    lineAddrHigh = hex2uint(hexLines[n].substr(9, 4)) & 65535;
41
                    break;
42
                    break;
42
                case 1:
-
 
43
//print("found a record of type 1");
43
                case 1: /* If End Of File record */
44
                    this.myValid = true;
44
                    this.myValid = true;
45
                    break;
45
                    break;
46
               
46
               
47
            }
47
            }
48
        }
48
        }
Line 52... Line 52...
52
IntelHex.prototype.myHexData = null;
52
IntelHex.prototype.myHexData = null;
53
IntelHex.prototype.myAddrLower = null;
53
IntelHex.prototype.myAddrLower = null;
54
IntelHex.prototype.myAddrUpper = null;
54
IntelHex.prototype.myAddrUpper = null;
55
IntelHex.prototype.myValid = null;
55
IntelHex.prototype.myValid = null;
56
 
56
 
-
 
57
/* Get the CRC of data, includes "holes" in the middle */
57
IntelHex.prototype.getCRC16 = function()
58
IntelHex.prototype.getCRC16 = function()
58
{
59
{
59
    var crc=0;
60
    var crc=0;
60
    for (var i = this.myAddrLower; i <= this.myAddrUpper; i++)
61
    for (var i = this.myAddrLower; i <= this.myAddrUpper; i++)
61
    {
62
    {
Line 79... Line 80...
79
        }
80
        }
80
    }
81
    }
81
    return crc;
82
    return crc;
82
}
83
}
83
 
84
 
-
 
85
/* Get the length of the data */
84
IntelHex.prototype.getLength = function()
86
IntelHex.prototype.getLength = function()
85
{
87
{
86
    if (this.myAddrUpper+1>this.myAddrLower)
88
    if (this.myAddrUpper+1>this.myAddrLower)
87
    {
89
    {
88
        return (this.myAddrUpper - this.myAddrLower + 1);
90
        return (this.myAddrUpper - this.myAddrLower + 1);
Line 91... Line 93...
91
    {
93
    {
92
        return 0;
94
        return 0;
93
    }
95
    }
94
}
96
}
95
 
97
 
-
 
98
/* Get the validity of the data (check if end of file record was found) */
96
IntelHex.prototype.isValid = function()
99
IntelHex.prototype.isValid = function()
97
{
100
{
98
    return this.myValid;
101
    return this.myValid;
99
}
102
}
100
 
103
 
-
 
104
/* Get the lowest address of the data */
101
IntelHex.prototype.getAddrLower = function()
105
IntelHex.prototype.getAddrLower = function()
102
{
106
{
103
    return this.myAddrLower;
107
    return this.myAddrLower;
104
}
108
}
105
 
109
 
-
 
110
/* Get the highest address of the data */
106
IntelHex.prototype.getAddrUpper = function()
111
IntelHex.prototype.getAddrUpper = function()
107
{
112
{
108
    return this.myAddrUpper;
113
    return this.myAddrUpper;
109
}
114
}
110
 
115
 
-
 
116
/* Get a byte at a certain address */
111
IntelHex.prototype.getByte = function(address)
117
IntelHex.prototype.getByte = function(address)
112
{
118
{
113
    var byte = 0xff;
119
    var byte = 0xff;
114
    if (address < MAX_SIZE && this.myValid) {
120
    if (address < MAX_SIZE && this.myValid) {
115
        byte = this.myHexData[address];
121
        byte = this.myHexData[address];