⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memorybankscratchshaee.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
   {      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//      if(DEBUG)      {         Debug.debug("-----------------------------------------------------------");         Debug.debug("MemoryBankScratchSHAEE.computeNextSecret(int, byte[], int) called");         Debug.debug("  romID=" + owc33.getAddressAsString());         Debug.debug("  addr=0x" + Convert.toHexString((byte)addr));         Debug.debug("  partialsecret", partialsecret, offset, 8);      }      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//      byte[] send_block = new byte [3];      byte[] scratch = new byte [8];      byte[] next_secret = null;      writeScratchpad(addr, partialsecret, 0, 8);      // check to see if secret is set      if(owc33.isContainerSecretSet())      {         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//         if(DEBUG)            Debug.debug("Calculating next secret for container");         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//         byte[] memory     = new byte [32];         byte[] secret     = new byte [8];         byte[] MT         = new byte [64];         readMemory(addr&0xE0, false, memory, 0, 32);         owc33.getContainerSecret(secret, 0);         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//         if(DEBUG)            Debug.debug("currentSecret", secret, 0, 8);         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//         System.arraycopy(secret,0,MT,0,4);         System.arraycopy(memory,0,MT,4,32);         System.arraycopy(ffBlock,0,MT,36,4);         MT[40] = ( byte ) (partialsecret[0] & ( byte ) 0x3F);         System.arraycopy(partialsecret,1,MT,41,7);         System.arraycopy(secret,4,MT,48,4);         System.arraycopy(ffBlock,0,MT,52,3);         // message padding         MT[55] = ( byte ) 0x80;         System.arraycopy(zeroBlock,0,MT,56,6);         MT[62] = ( byte ) 0x01;         MT[63] = ( byte ) 0xB8;         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//         if(DEBUG)            Debug.debug("MT", MT, 0, 64);         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//         int[] AtoE = new int [5];         SHA.ComputeSHA(MT,AtoE);         //copy E into secret         for(int temp=AtoE[4],i=0;i<4;i++)         {            secret[i] = (byte)(temp & 0x0FF);            temp >>= 8;         }         //copy D into secret         for(int temp=AtoE[3],i=4;i<8;i++)         {            secret[i] = (byte)(temp & 0x0FF);            temp >>= 8;         }         next_secret = secret;         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//         if(DEBUG)            Debug.debug("nextSecret=", secret, 0, 8);         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//      }      // access the device      if (ib.adapter.select(ib.getAddress()))      {         // Next Secret command         send_block[0] = COMPUTE_NEXT_SECRET;         // address 1         send_block[1] = ( byte ) (addr & 0xFF);         // address 2         send_block[2] = ( byte ) (((addr & 0xFFFF) >>> 8) & 0xFF);         // now send the block         ib.adapter.dataBlock(send_block,0,2);         // provide strong pull-up for compute next secret         ib.adapter.setPowerDuration(DSPortAdapter.DELIVERY_INFINITE);         ib.adapter.startPowerDelivery(DSPortAdapter.CONDITION_AFTER_BYTE);         ib.adapter.putByte(send_block[2]);         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//         if(DEBUG)            Debug.debug("sendblock ", send_block, 0, 3);         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//         try         {            Thread.sleep(14);         }         catch (InterruptedException e){}         ib.adapter.setPowerNormal();         readScratchpad(scratch,0,8,null);         for(int i=0;i<8;i++)         {            if(scratch[i] != ( byte ) 0xAA)            {               throw new OneWireIOException("Next secret not calculated.");            }         }         if(next_secret!=null)         {            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//            if(DEBUG)               Debug.debug("setting container secret", next_secret, 0, 8);            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//            owc33.setContainerSecret(next_secret, 0);         }      }      else         throw new OneWireIOException("Device select failed.");      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//      if(DEBUG)         Debug.debug("-----------------------------------------------------------");      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//   }   /**    * Refreshes the scratchpad for DS1961S.  Command has no effect on DS2432    * devices.  After this command is executed, the data at the address    * specified will be loaded into the scratchpad.  The Load First Secret    * command can then be used to re-write the data back to the page, correcting    * any weakly-programmed EEPROM bits.    *    * @param addr the address to load the data from into the scratchpad    *    * @throws OneWireIOException    * @throws OneWireException    */   public void refreshScratchpad(int addr)      throws OneWireIOException, OneWireException   {      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//      if(DEBUG)      {         Debug.debug("-----------------------------------------------------------");         Debug.debug("MemoryBankScratchSHAEE.refreshScratchpad(int) called");         Debug.debug("  romID=" + owc33.getAddressAsString());         Debug.debug("  addr=0x" + Convert.toHexString((byte)addr));         Debug.stackTrace();      }      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//      checkSpeed();      // access the device      if (ib.adapter.select(ib.getAddress()))      {         byte[] send_block = new byte[13];         send_block [0] = REFRESH_SCRATCHPAD;         send_block [1] = ( byte ) (addr & 0x00FF);         send_block [2] = ( byte ) ((addr >>> 8) & 0x00FF);         for(int i=3; i<11; i++)            send_block[i] = (byte)0x00;         send_block[11] = (byte)0xFF;         send_block[12] = (byte)0xFF;          // now send the block         ib.adapter.dataBlock(send_block, 0, 13);         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\         if(DEBUG)            Debug.debug("send_block", send_block, 0, 13);         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\         if(CRC16.compute(send_block, 0, 13, 0) != 0x0B001)         {            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//            if(DEBUG)               Debug.debug("   Refresh Scratchpad failed because of bad CRC16");            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//            throw new OneWireException("Bad CRC16 on Refresh Scratchpad");         }      }      else      {         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//         if(DEBUG)            Debug.debug("   Refresh Scratchpad failed because there is no device");         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//         throw new OneWireIOException("Device select failed.");      }      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//      if(DEBUG)         Debug.debug("-----------------------------------------------------------");      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//   }   /**    * Reads actual memory (not scratchpad memory) with no CRC checking (device or    * data). The resulting data from this API may or may not be what is on    * the 1-Wire device.  It is recommends that the data contain some kind    * of checking (CRC) like in the readPagePacket() method or have    * the 1-Wire device provide the CRC as in readPageCRC().  readPageCRC()    * however is not supported on all memory types, see 'hasPageAutoCRC()'.    * If neither is an option then this method could be called more    * then once to at least verify that the same thing is read consistantly.    *    * @param  startAddr     starting physical address    * @param  readContinue  if 'true' then device read is continued without    *                       re-selecting.  This can only be used if the new    *                       read() continious where the last one led off    *                       and it is inside a 'beginExclusive/endExclusive'    *                       block.    * @param  readBuf       byte array to place read data into    * @param  offset        offset into readBuf to place data    * @param  len           length in bytes to read    *    * @throws OneWireIOException    * @throws OneWireException    */   private void readMemory (int startAddr, boolean readContinue, byte[] readBuf,                            int offset, int len)      throws OneWireIOException, OneWireException   {      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//      if(DEBUG)      {         Debug.debug("-----------------------------------------------------------");         Debug.debug("MemoryBankScratchSHAEE.readMemory(int, boolean, byte[], int, int) called");         Debug.debug("  romID=" + owc33.getAddressAsString());         Debug.debug("  startAddr=0x" + Convert.toHexString((byte)startAddr));         Debug.debug("  readContinue=" + readContinue);         Debug.debug("  offset=" + offset);         Debug.debug("  len=" + len);      }      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//      // attempt to put device at max desired speed      if (!readContinue)         checkSpeed();      // see if need to access the device      if (!readContinue)      {         // select the device         if (!ib.adapter.select(ib.getAddress()))            throw new OneWireIOException("Device select failed.");         // build start reading memory block         readBuf [offset] = ( byte ) 0xF0; // READ MEMORY, no CRC, no MAC         readBuf [offset+1] = ( byte ) (startAddr & 0xFF);         readBuf [offset+2] = ( byte ) (((startAddr & 0xFFFF) >>> 8) & 0xFF);         // do the first block for command, address         ib.adapter.dataBlock(readBuf, offset, 3);         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//         if(DEBUG)         {            Debug.debug("  readBuf", readBuf, offset, 3);         }         //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//      }      // pre-fill readBuf with 0xFF      int pgs   = len / 32;      int extra = len % 32;      for (int i = 0; i < pgs; i++)         System.arraycopy(ffBlock, 0, readBuf, offset + i * 32,                          32);      if(extra>0)         System.arraycopy(ffBlock, 0, readBuf, offset + pgs * 32, extra);      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//      // send second block to read data, return result      ib.adapter.dataBlock(readBuf, offset, len);      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//      if(DEBUG)      {         Debug.debug("  readBuf", readBuf, offset, len);         Debug.debug("-----------------------------------------------------------");      }      //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//   }   /**    * helper method to pause for specified milliseconds    */   private static final void msWait(final long ms)   {      try      {         Thread.sleep(ms);      }      catch(InterruptedException ie)      {;}   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -