Subversion Repositories HomeAutomation

Rev

Rev 1063 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. const MAX_SIZE = 524288;
  3.  
  4. /* Call constructor with the contents of an Intel Hex file (rows as arrays)
  5.    http://en.wikipedia.org/wiki/.hex
  6. */
  7. function IntelHex(hexLines)
  8. {
  9.     this.myValid = false;
  10.     this.myAddrLower = 0x7FFFFFFF;
  11.     this.myAddrUpper = 0;
  12.     this.myHexData = new Array(MAX_SIZE);
  13.     for (var i = 0; i < MAX_SIZE; i++) this.myHexData[i] = 0xFF;
  14.  
  15.     var lineDataCount = 0;
  16.     var lineAddrHigh = 0;
  17.     var lineAddr = 0;
  18.     var lineCode = 0;
  19.     var fullAddr = 0;
  20.    
  21.     /* Parse hexfile to binary (array of bytes) */
  22.     for (var n = 0; n < hexLines.length; n++)
  23.     {
  24.         if (hexLines[n].length>=11 && hexLines[n].substr(0,1)==":") {
  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 */
  28.             fullAddr = (lineAddrHigh * 65536) + lineAddr;
  29.  
  30.             switch(lineCode)
  31.             {
  32.                 case 0: /* If Data record */
  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;
  40.                 case 4: /* If Extended Linear Address Record */
  41.                     lineAddrHigh = hex2uint(hexLines[n].substr(9, 4)) & 65535;
  42.                     break;
  43.                 case 1: /* If End Of File record */
  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. /* Get the CRC of data, includes "holes" in the middle */
  58. IntelHex.prototype.getCRC16 = function()
  59. {
  60.     var crc=0;
  61.     for (var i = this.myAddrLower; i <= this.myAddrUpper; i++)
  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.  
  85. /* Get the length of the data */
  86. IntelHex.prototype.getLength = function()
  87. {
  88.     if (this.myAddrUpper+1>this.myAddrLower)
  89.     {
  90.         return (this.myAddrUpper - this.myAddrLower + 1);
  91.     }
  92.     else
  93.     {
  94.         return 0;
  95.     }
  96. }
  97.  
  98. /* Get the validity of the data (check if end of file record was found) */
  99. IntelHex.prototype.isValid = function()
  100. {
  101.     return this.myValid;
  102. }
  103.  
  104. /* Get the lowest address of the data */
  105. IntelHex.prototype.getAddrLower = function()
  106. {
  107.     return this.myAddrLower;
  108. }
  109.  
  110. /* Get the highest address of the data */
  111. IntelHex.prototype.getAddrUpper = function()
  112. {
  113.     return this.myAddrUpper;
  114. }
  115.  
  116. /* Get a byte at a certain address */
  117. IntelHex.prototype.getByte = function(address)
  118. {
  119.     var byte = 0xff;
  120.     if (address < MAX_SIZE && this.myValid) {
  121.         byte = this.myHexData[address];
  122.     }
  123.  
  124.     return byte;
  125. }
  126.