Subversion Repositories HomeAutomation

Rev

Rev 1063 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

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