Subversion Repositories HomeAutomation

Rev

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