📄 memorybanknvcrcpw.java
字号:
byte[] raw_buf = new byte [11]; // attempt to put device at max desired speed if (!readContinue) { sp.checkSpeed(); } // check if read exceeds memory if ((startAddr + len) > size) throw new OneWireException("Read exceeds memory bank end"); // see if need to access the device if (!readContinue) { // select the device if (!ib.adapter.select(ib.address)) { sp.forceVerify(); throw new OneWireIOException("Device select failed"); } // build start reading memory block int addr = startAddr + startPhysicalAddress; raw_buf [0] = READ_MEMORY_CRC_PW_COMMAND; raw_buf [1] = ( byte ) (addr & 0xFF); raw_buf [2] = ( byte ) (((addr & 0xFFFF) >>> 8) & 0xFF); if(ibPass.isContainerReadWritePasswordSet()) ibPass.getContainerReadWritePassword(raw_buf, 3); else ibPass.getContainerReadOnlyPassword(raw_buf, 3); // do the first block for command, address. password if(enablePower) { ib.adapter.dataBlock(raw_buf, 0, 10); ib.adapter.startPowerDelivery(ib.adapter.CONDITION_AFTER_BYTE); ib.adapter.putByte(raw_buf[10]); msWait(10); ib.adapter.setPowerNormal(); } else { ib.adapter.dataBlock(raw_buf, 0, 11); } } // pre-fill readBuf with 0xFF boolean finished = false; int pgs = len / pageLength; int extra = len % pageLength; int add = pageLength - (startAddr % pageLength); if( (len < pageLength) && enablePower ) { if( ((startAddr % pageLength) > ((startAddr+len) % pageLength)) && (((startAddr+len) % pageLength) != 0) ) { System.arraycopy(ffBlock, 0, readBuf, offset, add); System.arraycopy(ffBlock, 0, raw_buf, 0, 2); ib.adapter.dataBlock(readBuf, offset, add); raw_buf[0] = (byte) ib.adapter.getByte(); ib.adapter.startPowerDelivery(ib.adapter.CONDITION_AFTER_BYTE); raw_buf[1] = (byte) ib.adapter.getByte(); msWait(10); ib.adapter.setPowerNormal(); System.arraycopy(ffBlock, 0, readBuf, offset + add, len - add); ib.adapter.dataBlock(readBuf, (offset + add), extra); finished = true; } else { System.arraycopy(ffBlock, 0, readBuf, offset, len); ib.adapter.dataBlock(readBuf, offset, len); finished = true; } } for (i = 0; i < pgs; i++) { if(!enablePower) { if( i == 0) { System.arraycopy(ffBlock, 0, readBuf, offset, add); ib.adapter.dataBlock(readBuf,offset,add); raw_buf[0] = (byte) ib.adapter.getByte(); raw_buf[1] = (byte) ib.adapter.getByte(); } else { System.arraycopy(ffBlock, 0, readBuf, offset + add + ((i-1)*pageLength),pageLength); ib.adapter.dataBlock(readBuf, offset+ add + ((i-1)*pageLength), pageLength); raw_buf[0] = (byte) ib.adapter.getByte(); raw_buf[1] = (byte) ib.adapter.getByte(); } } else { if( i == 0 ) { System.arraycopy(ffBlock, 0, readBuf, offset, add); System.arraycopy(ffBlock, 0, raw_buf, 0, 2); ib.adapter.dataBlock(readBuf, offset, add); raw_buf[0] = (byte) ib.adapter.getByte(); ib.adapter.startPowerDelivery(ib.adapter.CONDITION_AFTER_BYTE); raw_buf[1] = (byte) ib.adapter.getByte(); msWait(10); ib.adapter.setPowerNormal(); } else { System.arraycopy(ffBlock, 0, readBuf, offset + add + ((i-1)*pageLength),pageLength); System.arraycopy(ffBlock, 0, raw_buf, 0, 2); ib.adapter.dataBlock(readBuf, offset + add + ((i-1)*pageLength), pageLength); raw_buf[0] = (byte) ib.adapter.getByte(); ib.adapter.startPowerDelivery(ib.adapter.CONDITION_AFTER_BYTE); raw_buf[1] = (byte) ib.adapter.getByte(); msWait(10); ib.adapter.setPowerNormal(); } } } if(!enablePower) { System.arraycopy(ffBlock, 0, readBuf, offset + pgs * pageLength, extra); // send second block to read data, return result ib.adapter.dataBlock(readBuf, offset + pgs * pageLength, extra); } else { if(!finished) { System.arraycopy(ffBlock, 0, readBuf, offset + add + ((i-1)*pageLength), len - add - ((i-1)*pageLength)); ib.adapter.dataBlock(readBuf, offset + add + ((i-1)*pageLength), len - add - ((i-1)*pageLength)); } } } /** * Write memory in the current bank. It is recommended that * when writing data that some structure in the data is created * to provide error free reading back with read(). Or the * method 'writePagePacket()' could be used which automatically * wraps the data in a length and CRC. * * When using on Write-Once devices care must be taken to write into * into empty space. If write() is used to write over an unlocked * page on a Write-Once device it will fail. If write verification * is turned off with the method 'setWriteVerification(false)' then * the result will be an 'AND' of the existing data and the new data. * * @param startAddr starting address * @param writeBuf byte array containing data to write * @param offset offset into writeBuf to get data * @param len length in bytes to write * * @throws OneWireIOException * @throws OneWireException */ public void write (int startAddr, byte[] writeBuf, int offset, int len) throws OneWireIOException, OneWireException { // find the last (non-inclusive) address for this write int endingOffset = (startAddr + len); if( ((endingOffset & 0x1F) > 0) && (!enablePower)) { // find the number of bytes left until the end of the page int numBytes = pageLength - (endingOffset & 0x1F); if( // endingOffset == 0x250 ???? why?? ( ibPass.hasReadWritePassword() && (0xFFE0&endingOffset) == (0xFFE0&ibPass.getReadWritePasswordAddress()) && endingOffset < (ibPass.getReadWritePasswordAddress()+ibPass.getReadWritePasswordLength()) ) || ( ibPass.hasReadOnlyPassword() && (0xFFE0&endingOffset) == (0xFFE0&ibPass.getReadOnlyPasswordAddress()) && endingOffset < (ibPass.getReadOnlyPasswordAddress()+ibPass.getReadOnlyPasswordLength()) ) || ( ibPass.hasWriteOnlyPassword() && (0xFFE0&endingOffset) == (0xFFE0&ibPass.getWriteOnlyPasswordAddress()) && endingOffset < (ibPass.getWriteOnlyPasswordAddress()+ibPass.getWriteOnlyPasswordLength()) ) ) { // password block would be written to with potentially bad data throw new OneWireException( "Executing write would overwrite password control registers with " + "potentially invalid data. Please ensure write does not occur over" + "password control register page, or the password control data is " + "specified exactly in the write buffer."); } byte[] tempBuf = new byte[len + numBytes]; System.arraycopy(writeBuf, offset, tempBuf, 0, len); read(endingOffset, false, tempBuf, len, numBytes); super.write(startAddr, tempBuf, 0, tempBuf.length); } else { // write does extend to end of page super.write(startAddr, writeBuf, offset, len); } } /** * 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 + -