📄 memorybankee.java
字号:
checkSpeed(); // read the scratchpad, discard extra information readPage(page, readContinue, raw_buf, 0); // check if length is realistic if (raw_buf [0] > (PAGE_LENGTH - 3)) { forceVerify(); throw new OneWireIOException("Invalid length in packet"); } // verify the CRC is correct if (CRC16.compute(raw_buf, 0, raw_buf [0] + 3, page) == 0x0000B001) { // extract the data out of the packet System.arraycopy(raw_buf, 1, readBuf, offset, raw_buf [0]); // return the length return raw_buf [0]; } else { forceVerify(); throw new OneWireIOException("Invalid CRC16 in packet read"); } } /** * Read a Universal Data Packet and extra information. See the * method 'readPagePacket()' for a description of the packet structure. * See the method 'hasExtraInfo()' for a description of the optional * extra information some devices have. * * @param page page number to read packet from * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPagePacket() continious where the last one * stopped and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to put data read. Must have at least * 'getMaxPacketDataLength()' elements. * @param offset offset into readBuf to place data * @param extraInfo byte array to put extra info read into * * @return number of data bytes read from the device and written to * readBuf at the offset. * * @throws OneWireIOException * @throws OneWireException */ public int readPagePacket (int page, boolean readContinue, byte[] readBuf, int offset, byte[] extraInfo) throws OneWireIOException, OneWireException { // only needs to be implemented if supported by hardware throw new OneWireException( "Read page packet with extra-info not supported by this memory bank"); } /** * Write a Universal Data Packet. See the method 'readPagePacket()' * for a description of the packet structure. * * @param page page number to write packet to * @param writeBuf data byte array to write * @param offset offset into writeBuf where data to write is * @param len number of bytes to write * * @throws OneWireIOException * @throws OneWireException */ public void writePagePacket (int page, byte[] writeBuf, int offset, int len) throws OneWireIOException, OneWireException { // make sure length does not exceed max if (len > (PAGE_LENGTH - 3)) throw new OneWireIOException( "Length of packet requested exceeds page size"); // construct the packet to write byte[] raw_buf = new byte [len + 3]; raw_buf [0] = ( byte ) len; System.arraycopy(writeBuf, offset, raw_buf, 1, len); int crc = CRC16.compute(raw_buf, 0, len + 1, page); raw_buf [len + 1] = ( byte ) (~crc & 0xFF); raw_buf [len + 2] = ( byte ) (((~crc & 0xFFFF) >>> 8) & 0xFF); // write the packet, return result write(page * PAGE_LENGTH, raw_buf, 0, len + 3); } /** * Read a complete memory page with CRC verification provided by the * device. Not supported by all devices. See the method * 'hasPageAutoCRC()'. * * @param page page number to read * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPagePacket() continious where the last one * stopped and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to put data read. Must have at least * 'getMaxPacketDataLength()' elements. * @param offset offset into readBuf to place data * * @throws OneWireIOException * @throws OneWireException */ public void readPageCRC (int page, boolean readContinue, byte[] readBuf, int offset) throws OneWireIOException, OneWireException { // only needs to be implemented if supported by hardware throw new OneWireException( "Read page with CRC not supported by this memory bank"); } /** * Read a complete memory page with CRC verification provided by the * device with extra information. Not supported by all devices. * See the method 'hasPageAutoCRC()'. * See the method 'hasExtraInfo()' for a description of the optional * extra information. * * @param page page number to read * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPagePacket() continious where the last one * stopped and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to put data read. Must have at least * 'getMaxPacketDataLength()' elements. * @param offset offset into readBuf to place data * @param extraInfo byte array to put extra info read into * * @throws OneWireIOException * @throws OneWireException */ public void readPageCRC (int page, boolean readContinue, byte[] readBuf, int offset, byte[] extraInfo) throws OneWireIOException, OneWireException { // only needs to be implemented if supported by hardware throw new OneWireException( "Read page with CRC and extra-info not supported by this memory bank"); } //-------- //-------- Bank specific methods //-------- /** * Read the scratchpad page of memory from the device * This method reads and returns the entire scratchpad after the byte * offset regardless of the actual ending offset * * @param readBuf byte array to place read data into * length of array is always pageLength. * @param startAddr address to start to read from scratchPad * @param offset offset into readBuf to pug data * @param len length in bytes to read * * @throws OneWireIOException * @throws OneWireException */ protected void readScratchpad (byte[] readBuf, int startAddr, int offset, int len) throws OneWireIOException, OneWireException { // select the device if (!ib.adapter.select(ib.address)) { forceVerify(); throw new OneWireIOException("Device select failed"); } // build first block byte[] raw_buf = new byte [2]; raw_buf [0] = READ_SCRATCHPAD_COMMAND; raw_buf [1] = ( byte ) startAddr; // do the first block for address ib.adapter.dataBlock(raw_buf, 0, 2); // build the next block System.arraycopy(ffBlock, 0, readBuf, offset, len); // send second block to read data, return result ib.adapter.dataBlock(readBuf, offset, len); } /** * Write to the scratchpad page of memory device. * * @param startAddr starting address * @param writeBuf byte array containing data to write * @param offset offset into readBuf to place data * @param len length in bytes to write * * @throws OneWireIOException * @throws OneWireException */ protected void writeScratchpad (int startAddr, byte[] writeBuf, int offset, int len) throws OneWireIOException, OneWireException { // select the device if (!ib.adapter.select(ib.address)) { forceVerify(); throw new OneWireIOException("Device select failed"); } // build block to send byte[] raw_buf = new byte [len + 2]; raw_buf [0] = WRITE_SCRATCHPAD_COMMAND; raw_buf [1] = ( byte ) (startAddr & 0xFF); System.arraycopy(writeBuf, offset, raw_buf, 2, len); // send block, return result ib.adapter.dataBlock(raw_buf, 0, len + 2); } /** * Copy the scratchpad page to memory. * * @throws OneWireIOException * @throws OneWireException */ protected void copyScratchpad () throws OneWireIOException, OneWireException { // select the device if (!ib.adapter.select(ib.address)) { forceVerify(); throw new OneWireIOException("Device select failed"); } try { // copy scratch ib.adapter.putByte(COPY_SCRATCHPAD_COMMAND); // setup strong pullup ib.adapter.setPowerDuration(DSPortAdapter.DELIVERY_INFINITE); ib.adapter.startPowerDelivery(DSPortAdapter.CONDITION_AFTER_BYTE); // send validation and start strong power delivery ib.adapter.putByte(( byte ) 0xA5); // delay for 10ms Thread.sleep(10); // disable power ib.adapter.setPowerNormal(); } catch (InterruptedException e){} ; } //-------- //-------- checkSpeed methods //-------- /** * Check the device speed if has not been done before or if * an error was detected. * * @throws OneWireIOException * @throws OneWireException */ public void checkSpeed () throws OneWireIOException, OneWireException { synchronized (this) { // only check the speed if (doSetSpeed) { // attempt to set the correct speed and verify device present ib.doSpeed(); // no execptions so clear flag doSetSpeed = false; } } } /** * Set the flag to indicate the next 'checkSpeed()' will force * a speed set and verify 'doSpeed()'. */ public void forceVerify () { synchronized (this) { doSetSpeed = true; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -