📄 memorybankappreg.java
字号:
* methods with an 'extraInfo' parameter can be used: * {@link #readPage(int,boolean,byte[],int,byte[]) readPage}, * {@link #readPageCRC(int,boolean,byte[],int,byte[]) readPageCRC}, and * {@link #readPagePacket(int,boolean,byte[],int,byte[]) readPagePacket}. * * @return <CODE> true </CODE> if reading the this memory bank's * pages provides extra information * * @see #readPage(int,boolean,byte[],int,byte[]) readPage(extra) * @see #readPageCRC(int,boolean,byte[],int,byte[]) readPageCRC(extra) * @see #readPagePacket(int,boolean,byte[],int,byte[]) readPagePacket(extra) * @since 1-Wire API 0.01 */ public boolean hasExtraInfo () { return true; } /** * Query to get the length in bytes of extra information that * is read when read a page in the current memory bank. See * 'hasExtraInfo()'. * * @return number of bytes in Extra Information read when reading * pages in the current memory bank. */ public int getExtraInfoLength () { return 1; } /** * Query to get a string description of what is contained in * the Extra Informationed return when reading pages in the current * memory bank. See 'hasExtraInfo()'. * * @return string describing extra information. */ public String getExtraInfoDescription () { return extraInfoDescription; } /** * Set the write verification for the 'write()' method. * * @param doReadVerf true (default) verify write in 'write' * false, don't verify write (used on * Write-Once bit manipulation) */ public void setWriteVerification (boolean doReadVerf) { writeVerification = doReadVerf; } //-------- //-------- OTPMemoryBank query methods //-------- /** * Query to see if current memory bank pages can be redirected * to another pages. This is mostly used in Write-Once memory * to provide a means to update. * * @return 'true' if current memory bank pages can be redirected * to a new page. */ public boolean canRedirectPage () { return false; } /** * Query to see if current memory bank pages can be locked. A * locked page would prevent any changes to the memory. * * @return 'true' if current memory bank pages can be redirected * to a new page. */ public boolean canLockPage () { return true; } /** * Query to see if current memory bank pages can be locked from * being redirected. This would prevent a Write-Once memory from * being updated. * * @return 'true' if current memory bank pages can be locked from * being redirected to a new page. */ public boolean canLockRedirectPage () { return false; } //-------- //-------- MemoryBank I/O methods //-------- /** * Read memory in the current bank 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 */ public void read (int startAddr, boolean readContinue, byte[] readBuf, int offset, int len) throws OneWireIOException, OneWireException { // check if read exceeds memory if ((startAddr + len) > PAGE_SIZE) throw new OneWireException("Read exceeds memory bank end"); // ignore readContinue, silly with a 8 byte memory bank // attempt to put device at the correct speed ib.doSpeed(); // select the device if (!ib.adapter.select(ib.address)) throw new OneWireIOException("Device select failed"); // start the read ib.adapter.putByte(READ_MEMORY_COMMAND); ib.adapter.putByte(startAddr & 0xFF); // file the read block with 0xFF System.arraycopy(ffBlock, 0, readBuf, offset, len); // do the read ib.adapter.dataBlock(readBuf, offset, len); } /** * 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 { // return if nothing to do if (len == 0) return; // check if power delivery is available if (!ib.adapter.canDeliverPower()) throw new OneWireException( "Power delivery required but not available"); // check if write exceeds memory if ((startAddr + len) > PAGE_SIZE) throw new OneWireException("Write exceeds memory bank end"); // attempt to put device at the correct speed ib.doSpeed(); // select the device if (!ib.adapter.select(ib.address)) throw new OneWireIOException("Device select failed"); // start the write ib.adapter.putByte(WRITE_MEMORY_COMMAND); ib.adapter.putByte(startAddr & 0xFF); // do the write ib.adapter.dataBlock(writeBuf, offset, len); // check for write verification if (writeVerification) { // read back byte[] read_buf = new byte [len]; read(startAddr, true, read_buf, 0, len); // compare for (int i = 0; i < len; i++) { if (( byte ) read_buf [i] != ( byte ) writeBuf [i + offset]) throw new OneWireIOException( "Read back from write compare is incorrect, page may be locked"); } } } //-------- //-------- PagedMemoryBank I/O methods //-------- /** * Read page in the current bank 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 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 * readPage() 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 * * @throws OneWireIOException * @throws OneWireException */ public void readPage (int page, boolean readContinue, byte[] readBuf, int offset) throws OneWireIOException, OneWireException { // check if for valid page if (page != 0) throw new OneWireException( "Invalid page number for this memory bank"); // do the read read(0, true, readBuf, offset, PAGE_SIZE); } /** * Read page with extra information in the current bank 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. * 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 * readPage() 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 extraInfo byte array to put extra info read into * * @throws OneWireIOException * @throws OneWireException */ public void readPage (int page, boolean readContinue, byte[] readBuf, int offset, byte[] extraInfo) throws OneWireIOException, OneWireException { // read the page data read(page, true, readBuf, offset, PAGE_SIZE); // read the extra information (status) readStatus(extraInfo); } /** * 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 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 { byte[] raw_buf = new byte [PAGE_SIZE]; // read the entire page data read(page, true, raw_buf, 0, PAGE_SIZE); // check if length is realistic if (raw_buf [0] > (PAGE_SIZE - 2)) 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]); // read the extra info readStatus(extraInfo); // return the length return raw_buf [0]; } else throw new OneWireIOException("Invalid CRC16 in packet read");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -