Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. const MAX_SIZE = 524288;
  3.  
  4. function IntelHex(hexLines)
  5. {
  6. //print("constructor of IntelHex\n");
  7.     this.myValid = false;
  8.     this.myAddrLower = 0x7FFFFFFF;
  9.     this.myAddrUpper = 0;
  10.     this.myHexData = new Array(MAX_SIZE);
  11.     for (var i = 0; i < MAX_SIZE; i++) this.myHexData[i] = 0xFF;
  12.  
  13.     var lineDataCount = 0;
  14.     var lineAddrHigh = 0;
  15.     var lineAddr = 0;
  16.     var lineCode = 0;
  17.     var fullAddr = 0;
  18.  
  19.     for (var n = 0; n < hexLines.length; n++)
  20.     {
  21.         if (hexLines[n].length>=11 && hexLines[n].substr(0,1)==":") {
  22.             lineDataCount = hex2uint(hexLines[n].substr(1,2));
  23.             lineAddr =  hex2uint(hexLines[n].substr(3,4)) & 65535;   
  24.             lineCode = hex2uint(hexLines[n].substr(7,2));  
  25.             fullAddr = (lineAddrHigh * 65536) + lineAddr;
  26.  
  27.             switch(lineCode)
  28.             {
  29.                 case 0:
  30.                     if ((fullAddr + lineDataCount - 1) > this.myAddrUpper) this.myAddrUpper = fullAddr + lineDataCount - 1;
  31.                     if (lineCode == 0 && fullAddr < this.myAddrLower) this.myAddrLower = fullAddr;
  32.                     for (var j = 0; j < lineDataCount; j++) {
  33.                         this.myHexData[fullAddr] = hex2uint(hexLines[n].substr(j * 2 + 9, 2));
  34.                         //this.myHexData[fullAddr] &= 255;
  35.                         fullAddr++;
  36.                     }
  37. //print("found a record of type 0, fulladdr: "+fullAddr+"\nhexLines.length: "+hexLines.length+"\n"+hexLines[n]+"\n");
  38.                     break;
  39.                 case 4:
  40.                     lineAddrHigh = hex2uint(hexLines[n].substr(9, 4)) & 65535;
  41.                     break;
  42.                 case 1:
  43. //print("found a record of type 1");
  44.                     this.myValid = true;
  45.                     break;
  46.                
  47.             }
  48.         }
  49.     }
  50. }
  51.  
  52. IntelHex.prototype.myHexData = null;
  53. IntelHex.prototype.myAddrLower = null;
  54. IntelHex.prototype.myAddrUpper = null;
  55. IntelHex.prototype.myValid = null;
  56.  
  57. IntelHex.prototype.getCRC16 = function()
  58. {
  59.     var crc=0;
  60.     for (var i = this.myAddrLower; i <= this.myAddrUpper; i++)
  61.     {
  62.         crc = this.crc16_update(crc, this.myHexData[i]);
  63.     }
  64.     return crc&0xffff;
  65. }
  66.  
  67. IntelHex.prototype.crc16_update = function(crc, byte)
  68. {
  69.     crc ^= byte;
  70.     for (var i = 0; i<8; ++i)
  71.     {
  72.         if ((crc & 1) != 0)
  73.         {
  74.             crc = (crc >> 1) ^ 0xA001;
  75.         }
  76.         else
  77.         {
  78.             crc = (crc >> 1);
  79.         }
  80.     }
  81.     return crc;
  82. }
  83.  
  84. IntelHex.prototype.getLength = function()
  85. {
  86.     if (this.myAddrUpper+1>this.myAddrLower)
  87.     {
  88.         return (this.myAddrUpper - this.myAddrLower + 1);
  89.     }
  90.     else
  91.     {
  92.         return 0;
  93.     }
  94. }
  95.  
  96. IntelHex.prototype.isValid = function()
  97. {
  98.     return this.myValid;
  99. }
  100.  
  101. IntelHex.prototype.getAddrLower = function()
  102. {
  103.     return this.myAddrLower;
  104. }
  105.  
  106. IntelHex.prototype.getAddrUpper = function()
  107. {
  108.     return this.myAddrUpper;
  109. }
  110.  
  111. IntelHex.prototype.getByte = function(address)
  112. {
  113.     var byte = 0xff;
  114.     if (address < MAX_SIZE && this.myValid) {
  115.         byte = this.myHexData[address];
  116.     }
  117.  
  118.     return byte;
  119. }
  120.